An open API service indexing awesome lists of open source software.

https://github.com/curusarn/xml-config-parser

Python module that parses XML config files with defined structure
https://github.com/curusarn/xml-config-parser

python xml xml-config-parser

Last synced: 3 months ago
JSON representation

Python module that parses XML config files with defined structure

Awesome Lists containing this project

README

        

# xml-config-parser
Python module that parses XML config files with defined structure.

## Example

Xml file:
```


Value
Value


False
WUBBA LUBA DUB-DUB

```

Python code:
```
from xmlConfigParser import XmlConfigParser

# NOTE: specify config options like this
# NOTE: options with "None" as default value are required
# NOTE: sections with no required options are optional:
cfgValues = {
"basicConfig": {
"optionalOption": "default_value",
"requiredOption": None
},
"nextConfig": {
"bool": "True",
"whatever": None
},
"optionalConfigSection": {
"opt1": "def_val1",
"opt2": "def_val2"
}
}

cfg = XmlConfigParser(cfgValues, "/path/to/xml/config", "basicConfig")

# HACK: you can write your custom conditions and mess with default values and their optionality\
before you run parseXmlConfig()
# if /optionalConfigSection is present: set default value for /nextConfig/whatever
if not cfg.getRoot().find("basicConfig") is None:
cfg.set("new_value", "whatever", "nextConfig")
# HACK: this makes /nextConfig/whatever optional

# NOTE: parseXmlConfig will raise XmlConfigParserException if any of required options are missing
cfg.parseXmlConfig()

# returns value of "whatever" from "nextConfig" section
someValue = cfg.get("whatever", "nextConfig")

# converts to bool value of "bool" from "nextConfig" section
boolValue = cfg.getBool("bool", "nextConfig")

# returns value of "whatever" from default section specified in constructor
someOtherValue = cfg.get("optionalOption")

```

## Dependencies
`xml` python library.
You can install this dependency using `pip`