From 93259f9aeda8ce235075ae4a48c54ded45ed7078 Mon Sep 17 00:00:00 2001 From: gabriel becker Date: Wed, 23 Nov 2022 22:42:47 +1100 Subject: [PATCH] Make it an installable package. --- .gitignore | 160 ++++++++++++++++++ LICENCE | 8 + ankimaker/commands/from_epub.py | 0 ankimaker/tasks/basic_csv_to_anki.py | 62 ------- pyproject.toml | 3 + requirements.txt | 3 +- setup.py | 32 ++++ src/ankimaker/__init__.py | 1 + ankimaker/__main__.py => src/ankimaker/cli.py | 3 - .../ankimaker}/commands/__init__.py | 3 +- .../ankimaker}/commands/from_csv.py | 6 +- src/ankimaker/config/__init__.py | 2 + src/ankimaker/config/configuration.py | 23 +++ src/ankimaker/config/load_config.py | 19 +++ src/ankimaker/generator/__init__.py | 5 + src/ankimaker/generator/deck.py | 19 +++ .../ankimaker}/tasks/__init__.py | 0 src/ankimaker/tasks/basic_csv_to_anki.py | 69 ++++++++ 18 files changed, 348 insertions(+), 70 deletions(-) create mode 100644 .gitignore create mode 100644 LICENCE delete mode 100644 ankimaker/commands/from_epub.py delete mode 100644 ankimaker/tasks/basic_csv_to_anki.py create mode 100644 pyproject.toml create mode 100644 setup.py create mode 100644 src/ankimaker/__init__.py rename ankimaker/__main__.py => src/ankimaker/cli.py (67%) rename {ankimaker => src/ankimaker}/commands/__init__.py (50%) rename {ankimaker => src/ankimaker}/commands/from_csv.py (81%) create mode 100644 src/ankimaker/config/__init__.py create mode 100644 src/ankimaker/config/configuration.py create mode 100644 src/ankimaker/config/load_config.py create mode 100644 src/ankimaker/generator/__init__.py create mode 100644 src/ankimaker/generator/deck.py rename {ankimaker => src/ankimaker}/tasks/__init__.py (100%) create mode 100644 src/ankimaker/tasks/basic_csv_to_anki.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..68bc17f --- /dev/null +++ b/.gitignore @@ -0,0 +1,160 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/LICENCE b/LICENCE new file mode 100644 index 0000000..13d97e6 --- /dev/null +++ b/LICENCE @@ -0,0 +1,8 @@ +Copyright 2014 Pallets + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/ankimaker/commands/from_epub.py b/ankimaker/commands/from_epub.py deleted file mode 100644 index e69de29..0000000 diff --git a/ankimaker/tasks/basic_csv_to_anki.py b/ankimaker/tasks/basic_csv_to_anki.py deleted file mode 100644 index 7384955..0000000 --- a/ankimaker/tasks/basic_csv_to_anki.py +++ /dev/null @@ -1,62 +0,0 @@ -import genanki -import pandas as pd - - -def create_model(): - my_model = genanki.Model( - 1607392319, - 'Simple Model', - fields=[ - {'name': 'Question'}, - {'name': 'Answer'}, - ], - templates=[ - { - 'name': 'Card 1', - 'qfmt': '{{Question}}', - 'afmt': '{{FrontSide}}
{{Answer}}', - }, - ]) - return my_model - - -def create_note(model, fields): - note = genanki.Note( - model=model, - fields=fields - ) - return note - - -def create_deck(name): - deck = genanki.Deck( - 2059400110, - name - ) - return deck - - -def save_deck(deck, destination): - my_package = genanki.Package(deck) - # my_package.media_files = ['sound.mp3', 'images/image.jpg'] - my_package.write_to_file(destination) - - -def load_csv(path): - df = pd.read_csv(path, header=None) - return df - - -def add_df_to_deck(df: pd.DataFrame, deck: genanki.Deck): - model = create_model() - for x in df.to_dict('records'): - note = create_note(model, fields=(x[2], x[3])) - deck.add_note(note) - return deck - - -def basic_pandas_to_anki(csv_path, output_path, name): - df = load_csv(csv_path) - deck = create_deck(name) - add_df_to_deck(df, deck) - save_deck(deck, output_path) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..fed528d --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" diff --git a/requirements.txt b/requirements.txt index a7792c2..77d9e4f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ click genanki -pandas \ No newline at end of file +pandas +pyyaml \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..09f4ab5 --- /dev/null +++ b/setup.py @@ -0,0 +1,32 @@ +from setuptools import setup, find_packages + + +def readme(): + with open("README.md") as f: + return f.read() + + +setup( + name='ankimaker', + version='0.0.4', + description='Makes anki with files', + url="https://git.lgoon.xyz/gabriel/ankimaker", + license="BSD-3-Clause", + license_files='LICENSE', + packages=find_packages('src', include=['ankimaker*']), + package_dir={"": "src"}, + entry_points={ + 'console_scripts': [ + 'ankimaker=ankimaker.cli:main', + ] + }, + include_package_data=True, + long_description=readme(), + install_requirements=[ + "click", + "genanki", + "pandas", + "pyyaml", + ], + long_description_content_type='text/markdown', +) diff --git a/src/ankimaker/__init__.py b/src/ankimaker/__init__.py new file mode 100644 index 0000000..401da57 --- /dev/null +++ b/src/ankimaker/__init__.py @@ -0,0 +1 @@ +from .cli import main diff --git a/ankimaker/__main__.py b/src/ankimaker/cli.py similarity index 67% rename from ankimaker/__main__.py rename to src/ankimaker/cli.py index c50f7e5..52b8655 100644 --- a/ankimaker/__main__.py +++ b/src/ankimaker/cli.py @@ -4,6 +4,3 @@ from ankimaker.commands import cli def main(): cli(prog_name='ankimaker') - -if __name__ == '__main__': - main() diff --git a/ankimaker/commands/__init__.py b/src/ankimaker/commands/__init__.py similarity index 50% rename from ankimaker/commands/__init__.py rename to src/ankimaker/commands/__init__.py index 10cefc4..25b0e8e 100644 --- a/ankimaker/commands/__init__.py +++ b/src/ankimaker/commands/__init__.py @@ -4,4 +4,5 @@ import click def cli(): pass -from ankimaker.commands.from_csv import generate_anki + +from ..commands.from_csv import generate_anki diff --git a/ankimaker/commands/from_csv.py b/src/ankimaker/commands/from_csv.py similarity index 81% rename from ankimaker/commands/from_csv.py rename to src/ankimaker/commands/from_csv.py index 22b3f9c..8a5c5f4 100644 --- a/ankimaker/commands/from_csv.py +++ b/src/ankimaker/commands/from_csv.py @@ -5,8 +5,8 @@ from ankimaker.tasks import basic_pandas_to_anki @cli.command('csv') -@click.option('-i', '--input', 'input_file', type=click.Path(exists=True)) -@click.option('-o', '--output', 'output_file', type=click.Path(exists=False)) +@click.argument('-i', '--input', 'input_file', type=click.Path(exists=True)) +@click.argument('-o', '--output', 'output_file', type=click.Path(exists=False)) @click.option('-c', '--conf', 'config_file', default=None, type=click.STRING) @click.option('-n', '--name', 'name', default=None, type=click.STRING) def generate_anki( @@ -18,7 +18,7 @@ def generate_anki( output_file = parse_output(output_file) if name is None: name = get_name_from_output(output_file) - basic_pandas_to_anki(input_file, output_file, name) + basic_pandas_to_anki(input_file, output_file, name, config_file) def parse_output(filename): diff --git a/src/ankimaker/config/__init__.py b/src/ankimaker/config/__init__.py new file mode 100644 index 0000000..fccb81a --- /dev/null +++ b/src/ankimaker/config/__init__.py @@ -0,0 +1,2 @@ +from .load_config import load_config_file +from .configuration import AnkimakerConfig as Config diff --git a/src/ankimaker/config/configuration.py b/src/ankimaker/config/configuration.py new file mode 100644 index 0000000..f0e09c8 --- /dev/null +++ b/src/ankimaker/config/configuration.py @@ -0,0 +1,23 @@ +import yaml + + +class AnkimakerConfig(yaml.YAMLObject): + yaml_tag = '!ankimakerconfig' + header = None + question_column = None + answer_column = None + separators = ',' + + def __init__(self, header=None, answer_column=None, question_column=None): + AnkimakerConfig.answer_column = answer_column + AnkimakerConfig.question_column = question_column + AnkimakerConfig.header = header + AnkimakerConfig.AnkimakerConfig = AnkimakerConfig + + @staticmethod + def loader(configuration_content): + content = configuration_content['AnkimakerConfig'] + AnkimakerConfig.header = content.header + AnkimakerConfig.question_column = content.question_column + AnkimakerConfig.answer_column = content.answer_column + AnkimakerConfig.separators = content.separators diff --git a/src/ankimaker/config/load_config.py b/src/ankimaker/config/load_config.py new file mode 100644 index 0000000..892b4b4 --- /dev/null +++ b/src/ankimaker/config/load_config.py @@ -0,0 +1,19 @@ +from pathlib import Path +import yaml + +from .configuration import AnkimakerConfig + + +def load_config_file(file_path: str): + """ + Load yaml configuration file. + :param file_path: Path to yaml file with configuration + :return: Dict config + """ + file_path = Path(file_path) + assert file_path.exists() + assert file_path.is_file() + with open(file_path, 'r') as file: + yaml.add_path_resolver('!ankimakerconfig', ['AnkimakerConfig'], dict) + configuration = yaml.load(file.read(), Loader=yaml.Loader) + AnkimakerConfig.loader(configuration) diff --git a/src/ankimaker/generator/__init__.py b/src/ankimaker/generator/__init__.py new file mode 100644 index 0000000..dfafe21 --- /dev/null +++ b/src/ankimaker/generator/__init__.py @@ -0,0 +1,5 @@ +from . import ( + deck, + # models, + # card +) diff --git a/src/ankimaker/generator/deck.py b/src/ankimaker/generator/deck.py new file mode 100644 index 0000000..d767bbc --- /dev/null +++ b/src/ankimaker/generator/deck.py @@ -0,0 +1,19 @@ +import uuid +import genanki + + +def create_deck(name, deck_id=None): + if deck_id is None: + deck_id = int(int(uuid.uuid4())//1e20) + deck = genanki.Deck( + deck_id, + name + ) + return deck + + +def save_deck(deck, destination, media_files=None): + my_package = genanki.Package(deck) + if media_files is not None: + my_package.media_files = ['sound.mp3', 'images/image.jpg'] + my_package.write_to_file(destination) diff --git a/ankimaker/tasks/__init__.py b/src/ankimaker/tasks/__init__.py similarity index 100% rename from ankimaker/tasks/__init__.py rename to src/ankimaker/tasks/__init__.py diff --git a/src/ankimaker/tasks/basic_csv_to_anki.py b/src/ankimaker/tasks/basic_csv_to_anki.py new file mode 100644 index 0000000..25b1836 --- /dev/null +++ b/src/ankimaker/tasks/basic_csv_to_anki.py @@ -0,0 +1,69 @@ +import genanki +import pandas as pd + +from ankimaker.config import Config +from ankimaker import generator, config + + +def create_model(): + my_model = genanki.Model( + 1607392319, + 'Simple Model', + fields=[ + {'name': 'Question'}, + {'name': 'Answer'}, + ], + templates=[ + { + 'name': 'Card 1', + 'qfmt': '
{{Question}}
', + 'afmt': '{{FrontSide}}
{{Answer}}
', + }, + ]) + return my_model + + +def create_note(model, fields): + note = genanki.Note( + model=model, + fields=fields + ) + return note + + +def load_csv(path): + df = pd.read_csv(path, header=Config.header, sep=Config.separators) + df_columns_are_unnamed = all(map(lambda x: str(x).isnumeric(), df.columns)) + if df_columns_are_unnamed: + Config.answer_column = int(Config.answer_column) + Config.question_column = int(Config.question_column) + return df + + +def add_df_to_deck(df: pd.DataFrame, deck: genanki.Deck): + model = create_model() + + for entry in df.to_dict('records'): + question = entry[Config.question_column] + answer = entry[Config.answer_column] + content_fields = (question, answer) + note = create_note(model, fields=content_fields) + deck.add_note(note) + return deck + + +def handle_config(config_file_path): + if config_file_path is None: + Config.header = None + Config.question_column = 0 + Config.answer_column = 1 + else: + config.load_config_file(config_file_path) + + +def basic_pandas_to_anki(csv_path, output_path, name, config_file_path): + handle_config(config_file_path) + df = load_csv(csv_path) + deck = generator.deck.create_deck(name) + add_df_to_deck(df, deck) + generator.deck.save_deck(deck, output_path)