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.
-
static
-
class
acg.templates.CheckChipOptionsField[source]¶ Bases:
acg.fields.OptionsFieldPick 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.
Return dict in a form as used in
custom_widgets.CustomSpeedDial.button_dicts.
-
-
class
acg.templates.DualLongTextField[source]¶ Bases:
acg.fields.TranslationMixin,acg.fields.OptionsFieldPick a single options using
custom_widgets.selection_widgets.CardCarousel.Useful for longer text, i.e. examples or explanations.
Inheritance from
TranslationMixinguarantees 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.
-
get_content(self)¶ If
widgetis set, usekv_bidictto extract.Calls
post_process()on the data before returning it.
-
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()andupdate_widget_data().
-
update_widget_data(self)¶ Check if widget is present and update the attributes of the kivy-widget from
template.data.
-
widget_kv¶ If set, widget gets constructed.
-
-
class
acg.templates.ImgField[source]¶ Bases:
acg.fields.OptionsField,acg.fields.MediaFieldLet user choose between multiple images.
-
construct_widget(self)¶ Bind
on_error()to child’son_errorevent.
-
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.FieldHandles 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:
ExceptionError 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_urlformatted with all class attributes.
-
from_lang= ¶ Target language.
-
headers¶ Headers for the request. Defaults to
DEFAULT_HEADERS.
-
make_request(self, url=None)¶ Use
headersto make an http-request viaget().Parameters: url – If None, will be set to format_url_with_attribs(). (Default value = None)Returns: Responseobject
-
parse_response(self, response: requests.Response)¶ Parse
requests.responseand return dict with result.
-
phrase= ¶ The word or phrase to search the site for.
-
result_dict(self, phrase=None)¶ Use
make_request()andparse_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.FloatLayoutTwo
MDSeparators with a heading in between.-
heading¶ StringPropertywith string used as heading.
-
-
class
acg.templates.Template(**kwargs)[source]¶ Bases:
kivy.uix.boxlayout.BoxLayoutMain class handling the data for the card-generation, database-access and user-selection.
-
add_parser(self, parser_name, **kwargs)[source]¶ Add a parser to
parsersby construction from parser_cookbook.
-
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 infieldsto obtain the processed data and user selection.Return result as merged dict from all
fields.Field\ s.
-
manual_search(self, search_term)[source]¶ Call
search()but withmake_suggestion=Trueand @app_busy-decorator.
-
parsers[source]¶ Each parser fetches data as a dict. The union of all these dicts will be collected in
data.
-
search(self, search_term, make_suggestion=False)[source]¶ Look up card with name
search_termin 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.
-
-
class
acg.templates.TextInputField[source]¶ Bases:
acg.fields.FieldDisplays 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_validateof the kivy-widget.
-
construct_widget(self)¶ Furthermore add
field_nameas hint-text and bindon_text_validate.
-
widget_kv¶ kv-string describing the widget.
-
-
class
acg.templates.TransChipOptionsField[source]¶ Bases:
acg.fields.TranslationMixin,acg.fields.OptionsFieldPick a single options using
custom_widgets.selection_widgets.MyTransChips.Inheritance from
TranslationMixinguarantees that translations are available.
-
class
acg.templates.VocabTemplate(from_lang, to_lang, **kwargs)[source]¶ Bases:
acg.templates.TemplateTemplate to generate vocabulary cards for brazilian portuguese.
-
acg.templates.app_busy(func)[source]¶ Call function in new thread and set app.busy =
Truein the meantime.