Initial commit
This commit is contained in:
17
flashcards/config.py
Normal file
17
flashcards/config.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
import yaml
|
||||
|
||||
|
||||
|
||||
config_file = '/home/nate/aoe/flashcards/settings.yaml'
|
||||
with open(config_file, 'r') as f:
|
||||
c = yaml.safe_load(f)
|
||||
|
||||
root_path = Path(c['storage']['root'])
|
||||
|
||||
config = SimpleNamespace()
|
||||
config.data_file = root_path / c['storage']['data'] / c['data_file']
|
||||
config.namelists_dir = root_path / c['storage']['namelists']
|
||||
config.maps_dir = root_path / c['storage']['maps']
|
||||
config.decks_dir = root_path / c['storage']['decks']
|
||||
45
flashcards/create_full_namelists.py
Normal file
45
flashcards/create_full_namelists.py
Normal file
@@ -0,0 +1,45 @@
|
||||
# run with `python -m flashcards.create_full_namelists`
|
||||
# Create two lists -- once for each high-level key in the data file
|
||||
# * units_buildings
|
||||
# * techs
|
||||
# The resulting lists will likely still have a lot of junk.
|
||||
# You will want to manually remove what you don't need before creating maps
|
||||
# from the lists.
|
||||
from flashcards.config import config
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
|
||||
def generate_namelist(big, key, namelist_fname):
|
||||
added = set()
|
||||
with open(namelist_fname, 'w') as f:
|
||||
for k, v in big[key].items():
|
||||
lname = v['localised_name'].strip()
|
||||
costs = sum([v for k, v in v['cost'].items()])
|
||||
# for some reason, "cost" of a civ "research" is 1
|
||||
#if lname.strip() != '' and costs != 0 and lname not in added:
|
||||
if lname.strip() != '' and costs > 1 and lname not in added:
|
||||
added.add(lname)
|
||||
f.write(lname + '\n')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
namelist_info = {
|
||||
'units_buildings': 'full-units-namelist.txt',
|
||||
'techs': 'full-research-namelist.txt',
|
||||
}
|
||||
|
||||
# Read in the full data dictionary
|
||||
with open(config.data_file, 'r') as f:
|
||||
big = json.load(f)
|
||||
|
||||
for key, fname in namelist_info.items():
|
||||
namelist_path = config.namelists_dir / fname
|
||||
|
||||
if not namelist_path.exists():
|
||||
# if the global namelist does not exist, create it and exit
|
||||
generate_namelist(big, key, namelist_path)
|
||||
else:
|
||||
print(f'File {namelist_path} already exists. Skipping.')
|
||||
24
flashcards/decks.py
Normal file
24
flashcards/decks.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from flashcards.models import simple
|
||||
from flashcards.random import random_anki_identifier
|
||||
from flashcards.config import config
|
||||
import genanki
|
||||
|
||||
|
||||
|
||||
def create_deck(notes, deck_name, deck_fname):
|
||||
# Create anki deck
|
||||
deck = genanki.Deck(
|
||||
random_anki_identifier(),
|
||||
deck_name)
|
||||
|
||||
# Add notes to deck
|
||||
for front, back in notes:
|
||||
note = genanki.Note(
|
||||
model=simple,
|
||||
fields=[front, back])
|
||||
deck.add_note(note)
|
||||
|
||||
# Write deck to file
|
||||
out_path = config.decks_dir / f'{deck_name}.apkg'
|
||||
print(f'Writing to {out_path}')
|
||||
genanki.Package(deck).write_to_file(out_path)
|
||||
57
flashcards/maps.py
Normal file
57
flashcards/maps.py
Normal file
@@ -0,0 +1,57 @@
|
||||
# Converts a list of lnames to two maps:
|
||||
# lname -> num and num -> lname
|
||||
from flashcards.config import config
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
|
||||
def _fill_costs(big, key, whitelist):
|
||||
names = {} # num -> localized name
|
||||
nums = {} # localized name -> num
|
||||
|
||||
for k, v in big[key].items():
|
||||
lname = v['localised_name'].strip()
|
||||
costs = sum([v for k, v in v['cost'].items()])
|
||||
if lname in whitelist and lname not in nums.keys():
|
||||
names[k] = lname
|
||||
nums[lname] = k
|
||||
|
||||
return names, nums
|
||||
|
||||
|
||||
def _write_map(fname, mapping):
|
||||
out_path = config.maps_dir / fname
|
||||
with open(out_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(mapping, f, ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
def create_maps(namelist_fname, key, out_base_fname):
|
||||
# Read in the full data dictionary
|
||||
with open(config.data_file, 'r') as f:
|
||||
big = json.load(f)
|
||||
|
||||
# Use whitelist to filter data and map only as needed
|
||||
namelist_path = config.namelists_dir / namelist_fname
|
||||
with open(namelist_path, 'r') as f:
|
||||
whitelist = [s.strip() for s in f.readlines()]
|
||||
names, nums = _fill_costs(big, key, whitelist)
|
||||
|
||||
# Write names -> nums map
|
||||
_write_map(f'{out_base_fname}-lname-to-num.json', nums)
|
||||
|
||||
# Write nums -> names map
|
||||
_write_map(f'{out_base_fname}-num-to-lname.json', names)
|
||||
|
||||
|
||||
def read_data_to_dicts(basename, key):
|
||||
with open(config.data_file, 'r') as f:
|
||||
all_data = json.load(f)
|
||||
|
||||
data = all_data[key]
|
||||
with open(config.maps_dir / f'{basename}-lname-to-num.json', 'r') as f:
|
||||
nums = json.load(f)
|
||||
with open(config.maps_dir / f'{basename}-num-to-lname.json', 'r') as f:
|
||||
names = json.load(f)
|
||||
|
||||
return data, nums, names
|
||||
31
flashcards/models.py
Normal file
31
flashcards/models.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import genanki
|
||||
|
||||
|
||||
|
||||
general_css = '''
|
||||
.card {
|
||||
font-family: arial;
|
||||
font-size: 32px;
|
||||
text-align: center;
|
||||
color: black;
|
||||
background-color: white;
|
||||
white-space: pre-line;
|
||||
}
|
||||
'''
|
||||
|
||||
simple = genanki.Model(
|
||||
1653277989,
|
||||
'Simple Model',
|
||||
fields=[
|
||||
{'name': 'Question'},
|
||||
{'name': 'Answer'},
|
||||
],
|
||||
templates=[
|
||||
{
|
||||
'name': 'Card 1',
|
||||
'qfmt': '{{Question}}',
|
||||
'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
|
||||
},
|
||||
],
|
||||
css=general_css
|
||||
)
|
||||
8
flashcards/random.py
Normal file
8
flashcards/random.py
Normal file
@@ -0,0 +1,8 @@
|
||||
# If doing this via the command line, could run:
|
||||
#python3 -c "import random; print(random.randrange(1 << 30, 1 << 31))"
|
||||
import random
|
||||
|
||||
|
||||
|
||||
def random_anki_identifier():
|
||||
return random.randrange(1 << 30, 1 << 31)
|
||||
Reference in New Issue
Block a user