METADATA 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. Metadata-Version: 2.4
  2. Name: pyaml
  3. Version: 26.7.0
  4. Summary: PyYAML-based module to produce a bit more pretty and readable YAML-serialized data
  5. Home-page: https://github.com/mk-fg/pretty-yaml
  6. Author: Mike Kazantsev
  7. Author-email: Mike Kazantsev <mk.fraggod@gmail.com>
  8. License: WTFPL
  9. Project-URL: Homepage, https://github.com/mk-fg/pretty-yaml
  10. Keywords: yaml,serialization,pretty-print,formatter,human,readability
  11. Classifier: Development Status :: 4 - Beta
  12. Classifier: Intended Audience :: Developers
  13. Classifier: License :: Public Domain
  14. Classifier: Programming Language :: Python
  15. Classifier: Programming Language :: Python :: 3.8
  16. Classifier: Topic :: Software Development
  17. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  18. Classifier: Topic :: Utilities
  19. Requires-Python: >=3.8
  20. Description-Content-Type: text/x-rst
  21. License-File: COPYING
  22. Requires-Dist: PyYAML
  23. Provides-Extra: anchors
  24. Requires-Dist: unidecode; extra == "anchors"
  25. Dynamic: author
  26. Dynamic: home-page
  27. Dynamic: license-file
  28. pretty-yaml (or pyaml)
  29. ======================
  30. PyYAML_-based python module to produce a bit more pretty and human-readable YAML-serialized data.
  31. This module is for serialization only, see `ruamel.yaml`_ module for literate
  32. YAML parsing (keeping track of comments, spacing, line/column numbers of values, etc).
  33. (side-note: to dump stuff parsed by ruamel.yaml with this module, use only ``YAML(typ='safe')`` there)
  34. It's a small module, and for projects that only need part of its functionality,
  35. I'd recommend copy-pasting that in, instead of adding janky dependency.
  36. .. _PyYAML: http://pyyaml.org/
  37. .. _ruamel.yaml: https://bitbucket.org/ruamel/yaml/
  38. .. contents::
  39. :backlinks: none
  40. Repository URLs:
  41. - https://github.com/mk-fg/pretty-yaml
  42. - https://codeberg.org/mk-fg/pretty-yaml
  43. - https://fraggod.net/code/git/pretty-yaml
  44. Warning
  45. -------
  46. Prime goal of this module is to produce human-readable output that can be
  47. easily diff'ed, manipulated and re-used, but maybe with occasional issues.
  48. So please do not rely on the thing to produce output that can always be
  49. deserialized exactly to what was exported, at least - use PyYAML directly
  50. for that (but maybe with options from the next section).
  51. Output also isn't guaranteed to be stable between module versions,
  52. as new representation tweaks get added occasionally, but these can usually
  53. be disabled via module parameters, to get output stability over improvements.
  54. What this module does and why
  55. -----------------------------
  56. YAML is generally nice and easy format to read *if* it was written by humans.
  57. PyYAML can a do fairly decent job of making stuff readable, and the best
  58. combination of parameters for such output that I've seen so far is probably this one::
  59. >>> m = [123, 45.67, {1: None, 2: False}, 'some text']
  60. >>> data = dict(a='asldnsa\nasldpáknsa\n', b='whatever text', ma=m, mb=m)
  61. >>> yaml.safe_dump( data, sys.stdout,
  62. width=100, allow_unicode=True, default_flow_style=False )
  63. a: 'asldnsa
  64. asldpáknsa
  65. '
  66. b: whatever text
  67. ma: &id001
  68. - 123
  69. - 45.67
  70. - 1: null
  71. 2: false
  72. - some text
  73. mb: *id001
  74. pyaml (this module) tries to improve on that a bit, with the following tweaks:
  75. * Most human-friendly representation options in PyYAML (that I know of)
  76. are used as defaults - unicode, flow-style, width=100 (old default is 80).
  77. * Dump "null" values as empty values, if possible, which have the same meaning
  78. but reduce visual clutter and are easier to edit.
  79. * Dicts, sets, OrderedDicts, defaultdicts, namedtuples, enums, dataclasses, etc
  80. are represented as their safe YAML-compatible base (like int, list or mapping),
  81. with mappings key-sorted by default for more diff-friendly output.
  82. * Use shorter and simpler yes/no for booleans.
  83. * List items get indented, as they should be.
  84. * Attempt is made to pick more readable string representation styles, depending
  85. on the value, e.g.::
  86. >>> yaml.safe_dump(cert, sys.stdout)
  87. cert: '-----BEGIN CERTIFICATE-----
  88. MIIH3jCCBcagAwIBAgIJAJi7AjQ4Z87OMA0GCSqGSIb3DQEBCwUAMIHBMRcwFQYD
  89. VQQKFA52YWxlcm9uLm5vX2lzcDEeMBwGA1UECxMVQ2VydGlmaWNhdGUgQXV0aG9y
  90. ...
  91. >>> pyaml.p(cert):
  92. cert: |
  93. -----BEGIN CERTIFICATE-----
  94. MIIH3jCCBcagAwIBAgIJAJi7AjQ4Z87OMA0GCSqGSIb3DQEBCwUAMIHBMRcwFQYD
  95. VQQKFA52YWxlcm9uLm5vX2lzcDEeMBwGA1UECxMVQ2VydGlmaWNhdGUgQXV0aG9y
  96. ...
  97. * "force_embed" option (default=yes) to avoid having &id stuff scattered all
  98. over the output. Might be more useful to disable it in some specific cases though.
  99. * "&idXYZ" anchors, when needed, get labels from the keys they get attached to,
  100. not just meaningless enumerators, e.g. "&users_-_admin" instead.
  101. * "string_val_style" option to only apply to strings that are values, not keys,
  102. i.e::
  103. >>> pyaml.p(data, string_val_style='"')
  104. key: "value\nasldpáknsa\n"
  105. >>> yaml.safe_dump(data, sys.stdout, allow_unicode=True, default_style='"')
  106. "key": "value\nasldpáknsa\n"
  107. * Add vertical spacing (empty lines) between keys on different depths,
  108. to separate long YAML sections in the output visually, make it more seekable.
  109. * Discard end-of-document "..." indicators for simple values.
  110. Result for the (rather meaningless) example above::
  111. >>> pyaml.p(data, force_embed=False, vspacing=dict(split_lines=10))
  112. a: |
  113. asldnsa
  114. asldpáknsa
  115. b: whatever text
  116. ma: &ma
  117. - 123
  118. - 45.67
  119. - 1:
  120. 2: no
  121. - some text
  122. mb: *ma
  123. (force_embed=False enabled deduplication with ``&ma`` anchor,
  124. vspacing is adjusted to split even this tiny output)
  125. ----------
  126. Extended example::
  127. >>> pyaml.dump(data, vspacing=dict(split_lines=10))
  128. destination:
  129. encoding:
  130. xz:
  131. enabled: yes
  132. min_size: 5120
  133. options:
  134. path_filter:
  135. - \.(gz|bz2|t[gb]z2?|xz|lzma|7z|zip|rar)$
  136. - \.(rpm|deb|iso)$
  137. - \.(jpe?g|gif|png|mov|avi|ogg|mkv|webm|mp[34g]|flv|flac|ape|pdf|djvu)$
  138. - \.(sqlite3?|fossil|fsl)$
  139. - \.git/objects/[0-9a-f]+/[0-9a-f]+$
  140. result:
  141. append_to_file:
  142. append_to_lafs_dir:
  143. print_to_stdout: yes
  144. url: http://localhost:3456/uri
  145. filter:
  146. - /(CVS|RCS|SCCS|_darcs|\{arch\})/$
  147. - /\.(git|hg|bzr|svn|cvs)(/|ignore|attributes|tags)?$
  148. - /=(RELEASE-ID|meta-update|update)$
  149. http:
  150. ca_certs_files: /etc/ssl/certs/ca-certificates.crt
  151. debug_requests: no
  152. request_pool_options:
  153. cachedConnectionTimeout: 600
  154. maxPersistentPerHost: 10
  155. retryAutomatically: yes
  156. logging:
  157. formatters:
  158. basic:
  159. datefmt: '%Y-%m-%d %H:%M:%S'
  160. format: '%(asctime)s :: %(name)s :: %(levelname)s: %(message)s'
  161. handlers:
  162. console:
  163. class: logging.StreamHandler
  164. formatter: basic
  165. level: custom
  166. stream: ext://sys.stderr
  167. loggers:
  168. twisted:
  169. handlers:
  170. - console
  171. level: 0
  172. root:
  173. handlers:
  174. - console
  175. level: custom
  176. Note that unless there are many moderately wide and deep trees of data,
  177. which are expected to be read and edited by people, it might be preferrable
  178. to directly use PyYAML regardless, as it won't introduce another
  179. (rather pointless in that case) dependency and a point of failure.
  180. Features and Tricks
  181. -------------------
  182. * Pretty-print any yaml or json (yaml subset) file from the shell::
  183. % python -m pyaml /path/to/some/file.yaml
  184. % pyaml < myfile.yml
  185. % curl -s https://www.githubstatus.com/api/v2/summary.json | pyaml
  186. ``pipx install pyaml`` can be a good way to only install "pyaml" command-line script.
  187. * Process and replace json/yaml file in-place::
  188. % python -m pyaml -r mydata.yml
  189. * Easier "debug printf to YAML" for some complex data::
  190. pyaml.debug(data)
  191. pyaml.debug(node_graph, ifaces, group_vars)
  192. debug() is an alias for pprint(repr_unknown=True) to dump non-YAML types as well.
  193. More strict YAML-print funcs (all aliases to same thing)::
  194. pyaml.p(stuff)
  195. pyaml.pprint(my_data)
  196. pyaml.pprint('----- HOW DOES THAT BREAKS!?!?', input_data, some_var, more_stuff)
  197. pyaml.print(data, file=sys.stderr)
  198. * Force all string values to a certain style (see info on these in `PyYAML docs`_)::
  199. pyaml.dump(many_weird_strings, string_val_style='|')
  200. pyaml.dump(multiline_words, string_val_style='>')
  201. pyaml.dump(no_want_quotes, string_val_style='plain')
  202. Using ``pyaml.add_representer()`` (note \*p\*yaml) as suggested in
  203. `this SO thread`_ (or `github-issue-7`_) should also work.
  204. See also this `amazing reply to StackOverflow#3790454`_ for everything about
  205. the many different string styles in YAML.
  206. * Control indent and width of the results::
  207. pyaml.dump(wide_and_deep, indent=4, width=120)
  208. These are actually keywords for PyYAML Emitter (passed to it from Dumper),
  209. see more info on these in `PyYAML docs`_.
  210. * Dump multiple yaml documents into a file: ``pyaml.dump_all([data1, data2, data3], dst_file)``
  211. explicit_start=True is implied, unless overidden by explicit_start=False.
  212. * Control thresholds for vertical spacing of values (0 = always space stuff out),
  213. and clump all oneliner ones at the top::
  214. >>> pyaml.p( data,
  215. sort_dicts=pyaml.PYAMLSort.oneline_group,
  216. vspacing=dict(split_lines=0, split_count=0) )
  217. chart:
  218. axisCenteredZero: no
  219. axisColorMode: text
  220. axisLabel: ''
  221. axisPlacement: auto
  222. barAlignment: 0
  223. drawStyle: line
  224. ...
  225. hideFrom:
  226. legend: no
  227. tooltip: no
  228. viz: no
  229. scaleDistribution:
  230. type: linear
  231. stacking:
  232. group: A
  233. mode: none
  234. Or same thing with cli tool ``-v/--vspacing`` option: ``pyaml -v 0/0g mydata.yaml``
  235. * Dump any non-YAML-type values when debugging or to replace later::
  236. >>> test2 = type('test2', (object,), dict(a='b'))()
  237. >>> test3 = type('test3', (object,), dict(__repr__=lambda s: '# test3 repr'+' '*80))()
  238. >>> pyaml.p(dict(test1='test1', test2=test2, test3=test3), repr_unknown=True)
  239. test1: test1
  240. test2: <__main__.test2 object at 0x7f00ccd1ade0> # python value
  241. test3: '# test3 repr ...[50/92]' # python __main__.test3
  242. Such unknown-type values get truncated if their repr() is too long, which can
  243. be controlled by passing int max-length to repr_unknown instead of bool.
  244. pyaml.debug() alias also implies repr_unknown=True flag.
  245. .. _PyYAML docs: http://pyyaml.org/wiki/PyYAMLDocumentation#Scalars
  246. .. _this SO thread: http://stackoverflow.com/a/7445560
  247. .. _github-issue-7: https://github.com/mk-fg/pretty-yaml/issues/7
  248. .. _amazing reply to StackOverflow#3790454:
  249. https://stackoverflow.com/questions/3790454/how-do-i-break-a-string-in-yaml-over-multiple-lines/21699210#21699210
  250. Installation
  251. ------------
  252. It's a regular Python 3.8+ module/package, published on PyPI (as pyaml_).
  253. Module uses PyYAML_ for processing of the actual YAML files
  254. and should pull it in as a dependency.
  255. Dependency on unidecode_ module is optional and only used with
  256. force_embed=False keyword (defaults to True), and same-id objects
  257. or recursion within serialized data - i.e. only when generating
  258. &some_key_id anchors is actually needed.
  259. If module is unavailable at runtime, anchor ids might be less like their
  260. keys and maybe not as nice.
  261. Using pip_ is how you generally install it, usually coupled with venv_ usage
  262. (which will also provide "pip" tool itself)::
  263. % pip install pyaml
  264. Current-git version can be installed like this::
  265. % pip install git+https://github.com/mk-fg/pretty-yaml
  266. pip will default to installing into currently-active venv, then user's home
  267. directory (under ``~/.local/lib/python...``), and maybe system-wide when running
  268. as root (only useful in specialized environments like docker containers).
  269. There are many other python packaging tools - pipenv_, poetry_, pdm_, etc -
  270. use whatever is most suitable for specific project/environment.
  271. pipx_ can be used to install command-line script without a module.
  272. More general info on python packaging can be found at `packaging.python.org`_.
  273. When changing code, unit tests can be run with ``python -m unittest``
  274. from a local repository checkout.
  275. .. _pyaml: https://pypi.org/project/pyaml/
  276. .. _unidecode: https://pypi.python.org/pypi/Unidecode
  277. .. _pip: https://pip.pypa.io/en/stable/
  278. .. _venv: https://docs.python.org/3/library/venv.html
  279. .. _poetry: https://python-poetry.org/
  280. .. _pipenv: https://pipenv.pypa.io/
  281. .. _pdm: https://pdm.fming.dev/
  282. .. _pipx: https://pypa.github.io/pipx/
  283. .. _packaging.python.org: https://packaging.python.org/installing/