Created
June 27, 2020 19:12
-
-
Save edipretoro/cca3bdb3f258da42304e2870ed1db142 to your computer and use it in GitHub Desktop.
Simple CLI to produce an Anki deck about UNIMARC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from sqlalchemy import create_engine | |
from sqlalchemy.orm import sessionmaker, Session | |
from unimarc2anki.db import Field, SubField | |
import genanki | |
import random | |
model_id = random.randrange(1 << 30, 1 << 31) | |
model = genanki.Model( | |
model_id, | |
"Révision des champs et sous-champs d'UNIMARC", | |
fields=[ | |
{'name': 'Question'}, | |
{'name': 'Réponse'} | |
], | |
templates=[ | |
{ | |
'name': 'Carte', | |
'qfmt': '{{Question}}', | |
'afmt': '{{FrontSide}}<hr id="answer" />{{Réponse}}' | |
} | |
] | |
) | |
deck_id = random.randrange(1 << 30, 1 << 31) | |
deck = genanki.Deck( | |
deck_id, | |
"Révision des champs et sous-champs d'UNIMARC" | |
) | |
e = create_engine('sqlite:///./data/koha_unimarc.db', echo=True) | |
Session = sessionmaker(bind=e) | |
s = Session() | |
fields = s.query(Field) | |
for i in fields: | |
# Knowing the label | |
note = genanki.Note( | |
model=model, | |
fields=[f'À quoi correspond le champ « {i.tagfield} » ?', f'{i.liblibrarian}'] | |
) | |
deck.add_note(note) | |
# Knowing if it is mandatory | |
note = genanki.Note( | |
model=model, | |
fields=[f'Le champ « {i.tagfield} » est-il obligatoire ?', "Oui" if i.mandatory == 1 else "Non"] | |
) | |
deck.add_note(note) | |
# Knowing if it is repeatable | |
note = genanki.Note( | |
model=model, | |
fields=[f'Le champ « {i.tagfield} » est-il répétable ?', "Oui" if i.repeatable == 1 else "Non"] | |
) | |
deck.add_note(note) | |
subfields = s.query(SubField) | |
for i in subfields: | |
# Knowing the label | |
note = genanki.Note( | |
model=model, | |
fields=[f'À quoi correspond le champ « {i.tagfield}${i.tagsubfield} » ?', f'{i.liblibrarian}'] | |
) | |
deck.add_note(note) | |
# Knowing if it is mandatory | |
note = genanki.Note( | |
model=model, | |
fields=[f'Le sous-champ « {i.tagfield}${i.tagsubfield} » est-il obligatoire ?', "Oui" if i.mandatory == 1 else "Non"] | |
) | |
deck.add_note(note) | |
# Knowing if it is repeatable | |
note = genanki.Note( | |
model=model, | |
fields=[f'Le sous-champ « {i.tagfield}${i.tagsubfield} » est-il répétable ?', "Oui" if i.repeatable == 1 else "Non"] | |
) | |
deck.add_note(note) | |
genanki.Package(deck).write_to_file('unimarc.apkg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment