| 1 |
Metadata-Version: 1.1 |
| 2 |
Name: anymarkup |
| 3 |
Version: 0.8.1 |
| 4 |
Summary: Parse/serialize any markup format |
| 5 |
Home-page: https://github.com/bkabrda/anymarkup |
| 6 |
Author: Slavek Kabrda |
| 7 |
Author-email: [email protected] |
| 8 |
License: BSD |
| 9 |
Description: anymarkup |
| 10 |
========= |
| 11 |
|
| 12 |
.. image:: https://travis-ci.org/bkabrda/anymarkup.svg?branch=master |
| 13 |
:target: https://travis-ci.org/bkabrda/anymarkup |
| 14 |
:alt: Build Status |
| 15 |
|
| 16 |
.. image:: https://landscape.io/github/bkabrda/anymarkup/master/landscape.svg?style=flat |
| 17 |
:target: https://landscape.io/github/bkabrda/anymarkup/master |
| 18 |
:alt: Code Health |
| 19 |
|
| 20 |
.. image:: https://coveralls.io/repos/bkabrda/anymarkup/badge.svg?branch=master |
| 21 |
:target: https://coveralls.io/r/bkabrda/anymarkup?branch=master |
| 22 |
:alt: Coverage |
| 23 |
|
| 24 |
Parse or serialize any markup. Currently supports ini, json, json5, toml, xml and yaml. |
| 25 |
Report bugs and new functionality requests at https://github.com/bkabrda/anymarkup/issues. |
| 26 |
|
| 27 |
Parsing:: |
| 28 |
|
| 29 |
>>> import anymarkup |
| 30 |
>>> anymarkup.parse('foo: bar') |
| 31 |
{'foo': 'bar'} |
| 32 |
>>> anymarkup.parse_file('foo/bar.ini') |
| 33 |
{'section': {'subsection': {'opt2': 'bar'}, 'opt1': 'foo'}} |
| 34 |
|
| 35 |
$ cat foo/bar.ini |
| 36 |
[section] |
| 37 |
opt1=foo |
| 38 |
[[subsection]] |
| 39 |
opt2=bar |
| 40 |
|
| 41 |
Serializing:: |
| 42 |
|
| 43 |
>>> import anymarkup |
| 44 |
>>> anymarkup.serialize({'foo': 'bar'}, 'json') |
| 45 |
b'{\n "foo": "bar"\n}' |
| 46 |
>>> anymarkup.serialize_file({'foo': 'bar'}, 'foo/bar.json') |
| 47 |
|
| 48 |
$ cat foo/bar.json |
| 49 |
{ |
| 50 |
"foo": "bar" |
| 51 |
} |
| 52 |
|
| 53 |
``anymarkup`` is licensed under BSD license. You can download official releases |
| 54 |
from https://pypi.python.org/pypi/anymarkup or install them via ``pip install anymarkup``. |
| 55 |
|
| 56 |
``anymarkup`` works with Python 2.7 and >= 3.3. |
| 57 |
|
| 58 |
Automatic Markup Language Recognition |
| 59 |
------------------------------------- |
| 60 |
|
| 61 |
When using ``anymarkup.parse(input)``, anymarkup will try to guess markup language of input. |
| 62 |
This usually works fine except: |
| 63 |
|
| 64 |
* ini vs toml: These two look almost the same and in fact have common subset (which, |
| 65 |
however, yields different parsing result). Because of this, anything with an ini-like |
| 66 |
look will be parsed with ini parser. If you want an input string to be parsed as toml, |
| 67 |
you have to explicitly specify that using ``format=toml`` (see below for examples). |
| 68 |
* json vs json5: json5 is superset of json, but not very widely used. Because of practicality |
| 69 |
of json usage, everything that looks like json is parsed as json. If you want input string |
| 70 |
to be parsed as json5, you have to explicitly specify that using ``format=json5``. |
| 71 |
|
| 72 |
When using ``anymarkup.parse_file(path)``, anymarkup will try to guess format based on file |
| 73 |
extension and then fallback to guessing as explained above. This means that if the file has |
| 74 |
``.toml`` pr ``.json5`` extension, you don't have to provide ``format=<format>`` explicitly. |
| 75 |
|
| 76 |
Notes on Parsing Basic Types |
| 77 |
---------------------------- |
| 78 |
|
| 79 |
When parsing, ``anymarkup`` recognizes basic types - ``NoneType``, ``int``, ``float`` and ``bool`` |
| 80 |
(and ``long`` on Python 2) and converts all values to these types. If you want to get |
| 81 |
everything as strings, just use ``force_types=False`` with ``parse`` or ``parse_file``. Finally, |
| 82 |
you can also use ``force_types=None`` to get whatever the parsing backend returned:: |
| 83 |
|
| 84 |
>>> anymarkup.parse('a: 1') |
| 85 |
{'a': 1} |
| 86 |
>>> anymarkup.parse('a: 1', force_types=False) |
| 87 |
{'a': '1'} |
| 88 |
>>> anymarkup.parse('a: 1', force_types=None) |
| 89 |
{'a': 1} |
| 90 |
|
| 91 |
|
| 92 |
CLI |
| 93 |
-------- |
| 94 |
To install the CLI, run the following command: |
| 95 |
|
| 96 |
pip install anymarkup |
| 97 |
|
| 98 |
Example of conversion from JSON to XML: |
| 99 |
|
| 100 |
anymarkup convert --from-format json --to-format xml <somefile.json |
| 101 |
|
| 102 |
For full help on the CLI run the following commands: |
| 103 |
|
| 104 |
anymarkup --help |
| 105 |
anymarkup convert --help |
| 106 |
|
| 107 |
|
| 108 |
Backends |
| 109 |
-------- |
| 110 |
|
| 111 |
``anymarkup`` uses: |
| 112 |
|
| 113 |
- https://pypi.python.org/pypi/configobj/ for ``ini`` parsing |
| 114 |
- https://docs.python.org/library/json.html for ``json`` parsing |
| 115 |
- https://pypi.python.org/pypi/toml/ for ``toml`` parsing |
| 116 |
- https://pypi.python.org/pypi/xmltodict for ``xml`` parsing |
| 117 |
- https://pypi.python.org/pypi/PyYAML for ``yaml`` parsing |
| 118 |
|
| 119 |
Notes on OrderedDict |
| 120 |
-------------------- |
| 121 |
|
| 122 |
Parsing certain types of markup can yield Python's ``OrderedDict`` type - namely |
| 123 |
XML documents and YAML ``!!omap`` (see http://yaml.org/type/omap.html). ``anymarkup`` |
| 124 |
handles this without a problem, but note that if you serialize these as JSON or INI |
| 125 |
and then parse again, you'll lose the ordering information (meaning you'll get just |
| 126 |
``dict`` back). |
| 127 |
|
| 128 |
This is because JSON and INI parsers (to my knowledge) don't consider |
| 129 |
ordering key-value structures important and there's no direct means in these |
| 130 |
markup languages to express ordering key-value structures. |
| 131 |
|
| 132 |
|
| 133 |
Notes on Dependencies |
| 134 |
--------------------- |
| 135 |
|
| 136 |
Read this section if you want anymarkup functionality only for subset of supported |
| 137 |
markup languages without the need to install all parsers. |
| 138 |
|
| 139 |
Since version 0.5.0, anymarkup is just a wrapper library around anymarkup-core |
| 140 |
(https://github.com/bkabrda/anymarkup-core) and doesn't actually contain any code, |
| 141 |
except of imports from anymarkup-core. |
| 142 |
|
| 143 |
anymarkup-core goal is to not explicitly depend on any of the parsers, so people |
| 144 |
can install it with only a specified subset of dependencies. For example, you can |
| 145 |
install anymarkup-core only with PyYAML, if you know you'll only be parsing YAML. |
| 146 |
|
| 147 |
If you install anymarkup, you will always get a full set of dependencies |
| 148 |
and you will be able to parse any markup language that's supported. |
| 149 |
|
| 150 |
The CLI requires click as indicated in the requirements.txt file. |
| 151 |
|
| 152 |
Examples |
| 153 |
-------- |
| 154 |
|
| 155 |
Parsing examples:: |
| 156 |
|
| 157 |
ini = """ |
| 158 |
[a] |
| 159 |
foo = bar""" |
| 160 |
|
| 161 |
json = """ |
| 162 |
{"a": { |
| 163 |
"foo": "bar" |
| 164 |
}}""" |
| 165 |
|
| 166 |
xml = """<?xml version="1.0" encoding="UTF-8"?> |
| 167 |
<a> |
| 168 |
<foo>bar</foo> |
| 169 |
</a>""" |
| 170 |
|
| 171 |
yaml = """ |
| 172 |
a: |
| 173 |
foo: bar |
| 174 |
""" |
| 175 |
|
| 176 |
# these will all yield the same value (except that xml parsing will yield OrderedDict) |
| 177 |
anymarkup.parse(ini) |
| 178 |
anymarkup.parse(json) |
| 179 |
anymarkup.parse(xml) |
| 180 |
anymarkup.parse(yaml) |
| 181 |
|
| 182 |
# explicitly specify a type of format to expect and/or encoding (utf-8 is default) |
| 183 |
anymarkup.parse('foo: bar', format='yaml', encoding='ascii') |
| 184 |
|
| 185 |
# by default, anymarkup recognizes basic types (None, booleans, ints and floats) |
| 186 |
# if you want to get everything as strings, just use force_types=False |
| 187 |
|
| 188 |
# will yield {'a': 1, 'b': True, 'c': None} |
| 189 |
anymarkup.parse('a: 1\nb: True\nc: None') |
| 190 |
# will yield {'a': '1', 'b': 'True', 'c': 'None'} |
| 191 |
anymarkup.parse('a: 1\nb: True\nc: None', force_types=False) |
| 192 |
|
| 193 |
# or parse a file |
| 194 |
anymarkup.parse_file('foo.ini') |
| 195 |
|
| 196 |
# if a file doesn't have a format extension, pass it explicitly |
| 197 |
anymarkup.parse_file('foo', format='json') |
| 198 |
|
| 199 |
# you can also pass encoding explicitly (utf-8 is default) |
| 200 |
anymarkup.parse_file('bar', format='xml', encoding='ascii') |
| 201 |
|
| 202 |
|
| 203 |
Serializing examples:: |
| 204 |
|
| 205 |
struct = {'a': ['b', 'c']} |
| 206 |
|
| 207 |
for fmt in ['ini', 'json', 'xml', 'yaml']: |
| 208 |
# any of the above formats can be used for serializing |
| 209 |
anymarkup.serialize(struct, fmt) |
| 210 |
|
| 211 |
# explicitly specify encoding (utf-8 is default) |
| 212 |
anymarkup.serialize(struct, 'json', encoding='utf-8') |
| 213 |
|
| 214 |
# or serialize directly to a file |
| 215 |
anymarkup.serialize_file(struct, 'foo/bar.ini') |
| 216 |
|
| 217 |
# if a file doesn't have a format extension, pass it explicitly |
| 218 |
anymarkup.serialize_file(struct, 'foo/bar', format='json') |
| 219 |
|
| 220 |
# you can also pass encoding explicitly (utf-8 is default) |
| 221 |
anymarkup.serialize_file(struct, 'foo/bar', format='json', encoding='ascii') |
| 222 |
|
| 223 |
Keywords: xml,yaml,toml,json,json5,ini |
| 224 |
Platform: UNKNOWN |
| 225 |
Classifier: Development Status :: 4 - Beta |
| 226 |
Classifier: Intended Audience :: Developers |
| 227 |
Classifier: License :: OSI Approved :: BSD License |
| 228 |
Classifier: Operating System :: POSIX :: Linux |
| 229 |
Classifier: Programming Language :: Python |
| 230 |
Classifier: Programming Language :: Python :: 2.7 |
| 231 |
Classifier: Programming Language :: Python :: 3.3 |
| 232 |
Classifier: Programming Language :: Python :: 3.4 |
| 233 |
Classifier: Programming Language :: Python :: 3.5 |