Skip to content

Commit

Permalink
Added xul.utils; removed legacy xul.log.
Browse files Browse the repository at this point in the history
  • Loading branch information
peteradrichem committed Jan 19, 2025
1 parent 3cef921 commit 031a526
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 118 deletions.
3 changes: 2 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Changelog

This document records all notable changes to `Xul <https://xul.readthedocs.io/>`_.

`Unreleased <https://github.com/peteradrichem/Xul/compare/2.5.1...py3k>`_ (2025-01-18)
`3.0.0-rc.1 <https://github.com/peteradrichem/Xul/compare/2.5.1...py3k>`_ (2025-01-19)
--------------------------------------------------------------------------------------
* Drop support for Python < 3.9.
* :doc:`xp <xp>`: fix boolean result (Python >= 3.12).
Expand All @@ -17,6 +17,7 @@ This document records all notable changes to `Xul <https://xul.readthedocs.io/>`
* Improved documentation; grouped CLI options.
* Code checks: ruff, black, isort, mypy (GitHub Action).
* Test script for local testing with Docker Compose.
* Removed legacy code.
* Typing.
* Updated Sphinx configuration.
* Output formatting (f-strings).
Expand Down
2 changes: 1 addition & 1 deletion src/xul/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"""

# Xul version.
__version_info__ = ("3", "0", "0-beta1")
__version_info__ = ("3", "0", "0-rc.1")
__version__ = ".".join(__version_info__)
4 changes: 2 additions & 2 deletions src/xul/cmd/ppx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from lxml.etree import XMLParser

from .. import __version__
from ..log import setup_logger_console
from ..ppxml import pp_xml
from ..utils import config_logger


def parse_cl() -> argparse.Namespace:
Expand Down Expand Up @@ -42,7 +42,7 @@ def parse_cl() -> argparse.Namespace:
def main() -> None:
"""Entry point for command line script ppx."""
# Logging to the console.
setup_logger_console()
config_logger()

# Command line.
args = parse_cl()
Expand Down
4 changes: 2 additions & 2 deletions src/xul/cmd/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from lxml import etree

from .. import __version__
from ..log import setup_logger_console
from ..ppxml import prettyprint
from ..utils import config_logger
from ..xsl import build_xsl_transform, xml_transformer


Expand Down Expand Up @@ -89,7 +89,7 @@ def output_xslt(
def main():
"""Entry point for command line script transform."""
# Logging to the console.
setup_logger_console()
config_logger()

# Command line.
args = parse_cl()
Expand Down
5 changes: 2 additions & 3 deletions src/xul/cmd/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from lxml import etree

from .. import __version__
from ..etree import get_source_name
from ..log import setup_logger_console
from ..utils import config_logger, get_source_name
from ..validate import build_dtd, build_relaxng, build_xml_schema, validate_xml


Expand Down Expand Up @@ -86,7 +85,7 @@ def apply_validator(
def main() -> None:
"""Entry point for command line script validate."""
# Logging to the console.
setup_logger_console()
config_logger()

# Command line.
args = parse_cl()
Expand Down
6 changes: 3 additions & 3 deletions src/xul/cmd/xp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from lxml import etree

from .. import __version__
from ..etree import build_etree, get_source_name
from ..log import setup_logger_console
from ..etree import build_etree
from ..ppxml import prettyprint
from ..utils import config_logger, get_source_name
from ..xpath import build_xpath, etree_xpath, namespaces


Expand Down Expand Up @@ -460,7 +460,7 @@ def xpath_on_xml(
def main() -> None:
"""Entry point for command line script xp."""
# Logging to the console.
setup_logger_console()
config_logger()

# Command line.
args = parse_cl()
Expand Down
16 changes: 2 additions & 14 deletions src/xul/etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,14 @@
https://lxml.de/tutorial.html
"""

import io
from logging import getLogger
from typing import Optional, TextIO, Union

from lxml import etree

logger = getLogger(__name__)

from .utils import get_source_name

def get_source_name(xml_source: Union[TextIO, str]) -> str:
"""Return the name of XML source."""
if isinstance(xml_source, str):
return xml_source
if isinstance(xml_source, io.TextIOWrapper):
# e.g. sys.stdin
return xml_source.name
if isinstance(xml_source, io.StringIO):
return "StringIO"
# ?
return str(xml_source)
logger = getLogger(__name__)


def build_etree(
Expand Down
92 changes: 0 additions & 92 deletions src/xul/log.py

This file was deleted.

36 changes: 36 additions & 0 deletions src/xul/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Xul utilities."""

import io
import logging
from typing import TextIO, Union


def config_logger(log_level: int = logging.INFO) -> None:
"""Configure the root logger and add console handler.
log_level -- console log level [default 'info']
Console logging (sys.stderr) for the command line scripts.
"""
# Configure threshold log level for the root logger.
logging.getLogger("").setLevel(logging.DEBUG)

# Configure console handler (StreamHandler).
console_handler = logging.StreamHandler()
console_handler.setLevel(log_level)

# Attach the console handler to the root logger.
logging.getLogger("").addHandler(console_handler)


def get_source_name(xml_source: Union[TextIO, str]) -> str:
"""Return the name of XML source."""
if isinstance(xml_source, str):
return xml_source
if isinstance(xml_source, io.TextIOWrapper):
# e.g. sys.stdin
return xml_source.name
if isinstance(xml_source, io.StringIO):
return "StringIO"
# ?
return str(xml_source)

0 comments on commit 031a526

Please sign in to comment.