Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
.. image:: https://travis-ci.org/bkabrda/anymarkup.svg?branch=master :target: https://travis-ci.org/bkabrda/anymarkup :alt: Build Status
.. image:: https://landscape.io/github/bkabrda/anymarkup/master/landscape.svg?style=flat :target: https://landscape.io/github/bkabrda/anymarkup/master :alt: Code Health
.. image:: https://coveralls.io/repos/bkabrda/anymarkup/badge.svg?branch=master :target: https://coveralls.io/r/bkabrda/anymarkup?branch=master :alt: Coverage
Parse or serialize any markup. Currently supports ini, json, json5, toml, xml and yaml. Report bugs and new functionality requests at https://github.com/bkabrda/anymarkup/issues.
Parsing::
import anymarkup anymarkup.parse('foo: bar') {'foo': 'bar'} anymarkup.parse_file('foo/bar.ini') {'section': {'subsection': {'opt2': 'bar'}, 'opt1': 'foo'}}
$ cat foo/bar.ini [section] opt1=foo [[subsection]] opt2=bar
Serializing::
import anymarkup anymarkup.serialize({'foo': 'bar'}, 'json') b'{\n "foo": "bar"\n}' anymarkup.serialize_file({'foo': 'bar'}, 'foo/bar.json')
$ cat foo/bar.json { "foo": "bar" }
anymarkup
is licensed under BSD license. You can download official releases
from https://pypi.python.org/pypi/anymarkup or install them via pip install anymarkup
.
anymarkup
works with Python 2.7 and >= 3.3.
When using anymarkup.parse(input)
, anymarkup will try to guess markup language of input.
This usually works fine except:
format=toml
(see below for examples).format=json5
.When using anymarkup.parse_file(path)
, anymarkup will try to guess format based on file
extension and then fallback to guessing as explained above. This means that if the file has
.toml
pr .json5
extension, you don't have to provide format=<format>
explicitly.
When parsing, anymarkup
recognizes basic types - NoneType
, int
, float
and bool
(and long
on Python 2) and converts all values to these types. If you want to get
everything as strings, just use force_types=False
with parse
or parse_file
. Finally,
you can also use force_types=None
to get whatever the parsing backend returned::
anymarkup.parse('a: 1') {'a': 1} anymarkup.parse('a: 1', force_types=False) {'a': '1'} anymarkup.parse('a: 1', force_types=None) {'a': 1}
To install the CLI, run the following command:
pip install anymarkup
Example of conversion from JSON to XML:
anymarkup convert --from-format json --to-format xml <somefile.json
For full help on the CLI run the following commands:
anymarkup --help anymarkup convert --help
anymarkup
uses:
ini
parsingjson
parsingtoml
parsingxml
parsingyaml
parsingParsing certain types of markup can yield Python's OrderedDict
type - namely
XML documents and YAML !!omap
(see http://yaml.org/type/omap.html). anymarkup
handles this without a problem, but note that if you serialize these as JSON or INI
and then parse again, you'll lose the ordering information (meaning you'll get just
dict
back).
This is because JSON and INI parsers (to my knowledge) don't consider ordering key-value structures important and there's no direct means in these markup languages to express ordering key-value structures.
Read this section if you want anymarkup functionality only for subset of supported markup languages without the need to install all parsers.
Since version 0.5.0, anymarkup is just a wrapper library around anymarkup-core (https://github.com/bkabrda/anymarkup-core) and doesn't actually contain any code, except of imports from anymarkup-core.
anymarkup-core goal is to not explicitly depend on any of the parsers, so people can install it with only a specified subset of dependencies. For example, you can install anymarkup-core only with PyYAML, if you know you'll only be parsing YAML.
If you install anymarkup, you will always get a full set of dependencies and you will be able to parse any markup language that's supported.
The CLI requires click as indicated in the requirements.txt file.
Parsing examples::
ini = """ [a] foo = bar"""
json = """ {"a": { "foo": "bar" }}"""
xml = """ bar """
yaml = """ a: foo: bar """
anymarkup.parse(ini) anymarkup.parse(json) anymarkup.parse(xml) anymarkup.parse(yaml)
anymarkup.parse('foo: bar', format='yaml', encoding='ascii')
anymarkup.parse('a: 1\nb: True\nc: None')
anymarkup.parse('a: 1\nb: True\nc: None', force_types=False)
anymarkup.parse_file('foo.ini')
anymarkup.parse_file('foo', format='json')
anymarkup.parse_file('bar', format='xml', encoding='ascii')
Serializing examples::
struct = {'a': ['b', 'c']}
for fmt in ['ini', 'json', 'xml', 'yaml']: # any of the above formats can be used for serializing anymarkup.serialize(struct, fmt)
anymarkup.serialize(struct, 'json', encoding='utf-8')
anymarkup.serialize_file(struct, 'foo/bar.ini')
anymarkup.serialize_file(struct, 'foo/bar', format='json')
anymarkup.serialize_file(struct, 'foo/bar', format='json', encoding='ascii')
FAQs
Parse/serialize any markup format
We found that anymarkup demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.