Stvad / CrowdAnki

Plugin for Anki SRS designed to facilitate cooperation on creation of notes and decks.
MIT License
515 stars 44 forks source link

Support Yaml as alternative serialization format #38

Open Stvad opened 5 years ago

Stvad commented 5 years ago

Yaml is nice and potentially friendlier to git diffs than JSON

evandrocoan commented 1 year ago

It would be pretty simple to implement the YAML serialization/deserialization. The only problem would be to ship the pyyaml dependency with anki because looks like it uses .c extensions and requires a compiler.

I wrote this script so I can use it now .yaml instead of .json, by external conversion between one and another after I export the .json as .yml to versioning it with git: https://gist.github.com/evandrocoan/7804f10e442e5a379bf1c604b96dc123

aplaice commented 1 year ago

The only problem would be to ship the pyyaml dependency with anki because looks like it uses .c extensions and requires a compiler.

I don't think the C extension (libyaml) is a hard dependency of pyyaml — installing with --without-libyaml should install without it.

Testing installation with pip3 install pyyaml --global-option=--without-libyaml into a virtualenv and very quickly inspecting the virtualenv I indeed don't find any compiled (e.g. .so files).

Hence, I don't think that pyyaml is problematic as a dependency.

Edit: besides we already have pyyaml (an old version that didn't rely on libyaml at all) as a dependency, for the personal fields file.


Unrelated edit2 (to avoid spamming with more comments, but so that I don't forget): Another, very human-readable option might be Nested Text. It's main disadvantages are:

  1. It only has string fields, in order to correctly deal with some of the numerical values we'd need a schema when reading to correctly specify the type of the fields.

  2. It's "non-standard" (i.e. not very popular).

evandrocoan commented 1 year ago

I see, good to know it can be used without c extensions. Although I just stopped using it due to this problem: https://stackoverflow.com/questions/71836675/force-pyyaml-to-write-multiline-string-literals-regardless-of-string-content, it was causing trouble in templates, and they would not be viewed as plain text but as escaped strings, i.e., "my\nsome". The change was trivial; I updated my code post with it.


--- a/convert_yaml_json.py
+++ b/convert_yaml_json.py
@@ -9,7 +9,10 @@ import tempfile
 import pathlib

 # python3 -m pip install pyyaml
-import yaml
+# import yaml
+# python3 -m pip install ruamel.yaml
+import ruamel.yaml as yaml
 import sys
 import json

@@ -80,6 +83,7 @@ def fast_scandir(current_directory: Path, max_recursion=1, to_json=False, recurs
                         print(f"Converting to yml {fullpath}.", file=sys.stderr)
                         yaml.dump(json.load(readjson), temporaryfile,
                             default_flow_style=False,
+                            default_style='|',
                             allow_unicode=True)
                         temporaryfile.flush()  # atomic write for safety
                         os.replace(temporaryfile.name, newymlpath)