Templates

Implementation of Template class and sub-classes.

To add a new template, inherit from Template or a subclass and define the parsers and fields attributes. See e.g. the definition of PtTemplate.

class acg.templates.AsyncParser[source]

Base-Class for asynchronous parsers.

static format_str(some_str)

Replace spaces and special characters.

parse_response(self, response: dict or list or str)

Placeholder-function.

request(self, url=None, request_params=None)

Make http-request using :package:`aiohttp`.

request_params(self)

Return request-params.

result_dict(self, phrase)

Return results synchronously.

url(self, url=None)

Return url for request.

class acg.templates.CheckChipOptionsField[source]

Bases: acg.fields.OptionsField

Pick multiple options using custom_widgets.selection_widgets.CheckChips.

class acg.templates.CookBook[source]

Save recipes for construction of Classes with default values.

cook(self, name, **kwargs)

Generate object from recipe.

default_kwargs in recipe can be overridden by kwargs.

get_recipe_names(self)

Return list of all registered recipes.

get_recipes(self)

Return recipes.

register(self, recipe_name, info=None, **kwargs)

Add recipe to recipes.

to_button_dict(self)

Return dict in a form as used in custom_widgets.CustomSpeedDial.button_dicts.

class acg.templates.DualLongTextField[source]

Bases: acg.fields.TranslationMixin, acg.fields.OptionsField

Pick a single options using custom_widgets.selection_widgets.CardCarousel.

Useful for longer text, i.e. examples or explanations.

Inheritance from TranslationMixin guarantees that translations are available.

class acg.templates.Field[source]

Base-class for fields.

Accesses the data in template.data, can perform actions on the obtained data and/or display it in a widget to allow user input to change it.

construct_widget(self)

Construct widget that is used for the selection of content.

field_name

Key of template.data which this field is handling.

get_content(self)

If widget is set, use kv_bidict to extract.

Calls post_process() on the data before returning it.

get_data(self)

Get dictionary to construct widget.

heading

Heading to be shown over widget.

kv_bidict

Getter function for _audio_url.

post_process(self, content)

Placeholder-function.

pre_process(self)

Placeholder-function.

template

Reference to templates.Template.

update(self)

Apply pre_process() and update_widget_data().

update_widget_data(self)

Check if widget is present and update the attributes of the kivy-widget from template.data.

widget

Gets constructed by load_string() from widget_kv if set.

widget_kv

If set, widget gets constructed.

class acg.templates.ImgField[source]

Bases: acg.fields.OptionsField, acg.fields.MediaField

Let user choose between multiple images.

construct_widget(self)

Bind on_error() to child’s on_error event.

on_error(self, _widget, child, *_)

Remove urls that could not be loaded from template.data.

post_process(self, content)

Download user choice and save to data-base.

class acg.templates.MediaField[source]

Bases: acg.fields.Field

Handles download of a single media-file and content of corresponding field.

The field should only contain the a single url.

file_type

File-type of media-file.

get_file_strings(self)

Get string that embeds the file in the anki-card.

static get_media_file(url)

Download file using requests.get().

post_process(self, content)

Return strings for the fields of the anki-card.

pre_process(self)

Obtain url, download and save file.

save_media_file(self, media_file)

Save media_file to the data-base.

exception acg.templates.NoMatchError(site='')[source]

Bases: Exception

Error if no match can be found for the current search.

class acg.templates.Parser[source]

Base class for parsers.

Main functionality is the result_dict function.

base_url =

URL to make request to. Can contain every class attribute. E.g. https://some.url/{phrase}/dest={from_lang};src={to_lang}.html

format_url_with_attribs(self, url=None)

Get format_url_with_attribs for http-request.

Returns:base_url formatted with all class attributes.
from_lang =

Target language.

headers

Headers for the request. Defaults to DEFAULT_HEADERS.

make_request(self, url=None)

Use headers to make an http-request via get().

Parameters:url – If None, will be set to format_url_with_attribs(). (Default value = None)
Returns:Response object
parse_response(self, response: requests.Response)

Parse requests.response and return dict with result.

phrase =

The word or phrase to search the site for.

result_dict(self, phrase=None)

Use make_request() and parse_request() to return dict with result.

setup(self)

Stuff that needs to be executed before format_url_with_attribs() is called.

to_lang =

Source Language.

class acg.templates.SeparatorWithHeading(**kwargs)[source]

Bases: kivy.uix.floatlayout.FloatLayout

Two MDSeparators with a heading in between.

heading

StringProperty with string used as heading.

class acg.templates.Template(**kwargs)[source]

Bases: kivy.uix.boxlayout.BoxLayout

Main class handling the data for the card-generation, database-access and user-selection.

add_content_to_db(self)[source]

Write content to card.

add_field_widgets(self)[source]

For all fields.Field with a widget, add it to the Template itself.

add_parser(self, parser_name, **kwargs)[source]

Add a parser to parsers by construction from parser_cookbook.

content[source]

Dict of the form {"field_on_anki_card": "content"}.

current_card_db(self)[source]

If existent get card by name, else create new one.

data[source]

Base data to generate card from. Dict of the form {"key": ["list","of","options"]} or {"key":"single_value"}.

fields[source]

Each field can have individual pre- and post-process functions and an optional widget so the user can make a selection between multiple options.

get_content_from_fields(self)[source]

Iterate through fields.Fields in fields to obtain the processed data and user selection.

Return result as merged dict from all fields.Field\ s.

get_results(self)[source]

Get final results for the card fields as dictionary.

Call search() but with make_suggestion=True and @app_busy-decorator.

name[source]

Name of the template as saved in database.

parsers[source]

Each parser fetches data as a dict. The union of all these dicts will be collected in data.

post_process(self)[source]

Placeholder-function.

save_base_data_to_db(self)[source]

Save base_data to card.

search(self, search_term, make_suggestion=False)[source]

Look up card with name search_term in data-base.

Try to load data from card, if not possible use set_data_from_parsers() to fetch data and save it to data-base.

set_data_from_parsers(self)[source]

Collect all data obtained by the parsers.parser in data.

sort_field[source]

The field that should be unique on all cards. For language cards e.g. the word to learn.

template_db(self)[source]

If existent, get entry in the database by name, else create new one.

update_fields(self)[source]

Update all fields.

update_from_single_parser(self, search_term, parser_key)[source]

Use only a single parser to update :attr`data`.

class acg.templates.TextInputField[source]

Bases: acg.fields.Field

Displays text and lets user edit it.

If callback is set, it will be called on_text_validate, i.e. when the user presses enter while field is in focus.

Useful to bind to templates.Template.search() or templates.Template.update_from_single_parser.

callback

Gets called on_text_validate of the kivy-widget.

construct_widget(self)

Furthermore add field_name as hint-text and bind on_text_validate.

on_text_validate(self, widget)

Wrapper-function for the possible call of callback.

widget_kv

kv-string describing the widget.

class acg.templates.TransChipOptionsField[source]

Bases: acg.fields.TranslationMixin, acg.fields.OptionsField

Pick a single options using custom_widgets.selection_widgets.MyTransChips.

Inheritance from TranslationMixin guarantees that translations are available.

class acg.templates.VocabTemplate(from_lang, to_lang, **kwargs)[source]

Bases: acg.templates.Template

Template to generate vocabulary cards for brazilian portuguese.

post_process(self)[source]

Tag search_term in "explanation" and "example" fields.

translate(self, string)[source]

Translate string from from_lang to to_lang.

acg.templates.app_busy(func)[source]

Call function in new thread and set app.busy = True in the meantime.

acg.templates.smart_dict_merge(*args)[source]

If values are lists they are merged, else the last element is chosen as new value.

acg.templates.tag_word_in_sentence(sentence, tag_word)[source]

Use regex to wrap every derived form of a given tag_word in sentence in an html-tag.

Parameters:
  • sentence – String containing of multiple words.
  • tag_word – Word that should be wrapped.
Returns:

Sentence with replacements.