Metadata-Version: 2.4
Name: xmlable
Version: 2.0.4
Summary: A decorator for generating xsd, xml and parsers from dataclasses
Project-URL: Homepage, https://github.com/OliverKillane/xmlable
Project-URL: Bug Tracker, https://github.com/OliverKillane/xmlable/issues
Project-URL: Source, https://github.com/OliverKillane/xmlable
Author-email: Oliver Killane <oliverkillane.business@gmail.com>
License: MIT License
        
        Copyright (c) 2023 Oliver Killane
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: lxml,xml,xmlschema,xsd
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.11
Requires-Dist: lxml-stubs==0.5.1
Requires-Dist: lxml==5.3.0
Requires-Dist: pyhumps==3.8.0
Requires-Dist: termcolor==2.5.0
Provides-Extra: dev
Requires-Dist: black==24.10.0; extra == 'dev'
Requires-Dist: mypy==1.14.1; extra == 'dev'
Requires-Dist: pytest==8.3.4; extra == 'dev'
Description-Content-Type: text/markdown

# XMLable

## An easy xml/xsd generator and parser for python dataclasses!

```python
@xmlify
@dataclass
class Config:
    date: str
    number_of_cores: int
    codes: list[int]
    show_logs: bool


write_xsd(THIS_DIR / "config.xsd", Config)
write_xml_template(THIS_DIR / "config_xml_template.xml", Config)

original = Config(
    date="31/02/2023",
    number_of_cores=48,
    codes=[101, 345, 42, 67],
    show_logs=False,
)
write_xml_value(THIS_DIR / "config_xml_example.xml", original)

read_config: Config = parse_file(Config, THIS_DIR / "config_xml_example.xml")

assert read_config == original
```

See more in [examples](https://github.com/OliverKillane/xmlable/tree/master/examples)

## Capabilities

### Types

Currently supports the types:

```python
int, float, str, dict, tuple, set, list, None
# as well as unions!
int | float | None
```

And dataclasses that have been `@xmlify`-ed.

These can be combined for types such as:

```python
@xmlify
@dataclass
class Complex:
    a: dict[tuple[int, str], list[tuple[dict[int, float | str], set[bool]]]]

c1 = Complex(
    a={(3, "hello"): [({3: 0.4}, {True, False}), ({2: "str"}, {False})]}
)
```

### Custom Classes

The xmlify interface can be implemented by adding methods described in [xmlify](src/xmlable/_xmlify.py)
Once the class `is_xmlified` it can be used just as if generated by `@xmlify`

```python
from xmlable._xobject import XObject
from xmlable._user import IXmlify
from xmlable._manual import manual_xmlify

@manual_xmlify
class MyClass(IXmlify):
    def get_xobject() -> XObject:
        class XMyClass(XObject):
            def xsd_out(self, name: str, attribs: dict[str, str] = {}, add_ns: dict[str, str] = {}) -> _Element:
                pass

            def xml_temp(self, name: str) -> _Element:
                pass

            def xml_out(self, name: str, val: Any, ctx: XErrorCtx) -> _Element:
                pass

            def xml_in(self, obj: ObjectifiedElement, ctx: XErrorCtx) -> Any:
                pass

        return XMyClass() # must be an instance of XMyClass, not the class

    def xsd_forward(add_ns: dict[str, str]) -> _Element:
        pass

    def xsd_dependencies() -> set[type]:
        return {MyClass}
```

See the [user define example](https://github.com/OliverKillane/xmlable/tree/master/examples/userdefined) for implementation.

## Limitations

### Unions of Generic Types

Generating xsd works, parsing works, however generating an xml template can fail
if they type is not determinable at runtime.

- Values do not have type arguments carried with them
- Many types are indistinguishable in python

For example:

```python
@xmlify
@dataclass
class GenericUnion:
    u: dict[int, float] | dict[int, str]

GenericUnion(u={}) # which variant in the xml should {} have??named_
```

In this case an error is raised

## To Develop

```bash
git clone # this project

# Can use hatch to build, run
hatch run check:test      # run tests/
hatch run check:lint      # check formatting
hatch run check:typecheck # mypy for src/ and all examples

hatch run auto:examplegen # regenerate the example code
hatch run auto:lint       # format code

# Alternatively can just create a normal env
python3.11 -m venv .venv
source .venv/bin/activate # activate virtual environment

pip install -e .      # install this project in the venv
pip install -e .[dev] # install optional dev dependencies (mypy, black and pytest)

black . # to reformat
mypy    # type check
pytest  # to run tests
```

[Hatch](https://hatch.pypa.io/) is used for build, test and pypi publish.

## To Improve

### Fuzzing

(As a fun weekend project) generate arbitrary python data types with values, and dataclasses.
Then `@xmlify` all and validate as in the current tests

### Etree vs Objectify

Currently using objectify for parsing and etree for construction, I want to move parsing to use `etree`

- previously used objectify for quick prototype.
