tim_common package
Contents
tim_common package#
Submodules#
tim_common.common_schemas module#
The state model defined in this module is used by both textfield and numericfield.
- class tim_common.common_schemas.TextfieldStateModel(c: str | int | float | None, styles: dict[str, str] | marshmallow.utils._Missing = <marshmallow.missing>)[source]#
Bases:
object
Model for the information that is stored in TIM database for each answer.
- c: str | int | float | None#
- styles: dict[str, str] | marshmallow.utils._Missing = <marshmallow.missing>#
tim_common.cs_sanitizer module#
Functions for HTML sanitization used by csplugin.
tim_common.fileParams module#
- class tim_common.fileParams.FileParams(query: tim_common.fileParams.QueryClass, nr: str, url: str)[source]#
Bases:
object
- tim_common.fileParams.check_key(query: tim_common.fileParams.QueryClass, key: str)[source]#
- tim_common.fileParams.get_all_templates(dirname: str) dict [source]#
Find list of all templates from dirname. Dir should include file tabs.txt where there is one line for every tab needed for TIM editor. Then there should be directories 0, 1, … for each corresponding tab-line. So if tehre is two lines in tabs.txt tehre is directories 0 and 1 for first and second tab.
- Parameters
dirname – the directory name where to find template list file and sub directories for templates
- Returns
dict with list of lif to template items (templates) and texts (text)
- tim_common.fileParams.get_clean_param(query: tim_common.fileParams.QueryClass, key, default)[source]#
- tim_common.fileParams.get_file_to_output(query: tim_common.fileParams.QueryClass, show_html: bool)[source]#
- tim_common.fileParams.get_heading(query: tim_common.fileParams.QueryClass, key: str, def_elem: str)[source]#
- tim_common.fileParams.get_json_eparam(jso: dict[str, Any], key1: str, key2: str, default: Any, escape_html_special_chars: bool = True)[source]#
- tim_common.fileParams.get_json_param(jso: dict[str, Any], key1: str, key2: str, default: Any)[source]#
- tim_common.fileParams.get_md_heading(query: tim_common.fileParams.QueryClass, key: str, def_elem: str)[source]#
- tim_common.fileParams.get_param(query: tim_common.fileParams.QueryClass, key, default)[source]#
- tim_common.fileParams.get_param_del(query: tim_common.fileParams.QueryClass, key: str, default: Any)[source]#
- tim_common.fileParams.get_param_table(query: tim_common.fileParams.QueryClass, key: str)[source]#
- tim_common.fileParams.get_query_from_json(jso) tim_common.fileParams.QueryClass [source]#
- tim_common.fileParams.get_surrounding_headers(query: tim_common.fileParams.QueryClass, inside)[source]#
- tim_common.fileParams.get_surrounding_headers2(query: tim_common.fileParams.QueryClass)[source]#
- tim_common.fileParams.get_surrounding_md_headers(query: tim_common.fileParams.QueryClass, inside, extra)[source]#
- tim_common.fileParams.get_surrounding_md_headers2(query: tim_common.fileParams.QueryClass, header_style, footer_style)[source]#
- tim_common.fileParams.get_template(dirname: str, idx: str, filename: str) str [source]#
Returns the template file from line 2 to end of file.
- Parameters
dirname – from directory (be sure this is valid)
idx – index for the template (only numbers allowed)
filename – from file (validity of this is checked)
- Returns
lines starting from line 2.
- tim_common.fileParams.get_templates(dirname: str) object [source]#
Find all templates from dirname. Each template file should include.
template text - first line template explanation - second line template content - rest of lines
- Parameters
dirname – the directory name where to find template files
- Returns
list of template items, one item is file: text: explanation
- tim_common.fileParams.get_tiny_surrounding_headers(query: tim_common.fileParams.QueryClass, inside)[source]#
- tim_common.fileParams.get_url_lines_as_string(url: str, headers: Optional[dict[str, str]] = None)[source]#
- tim_common.fileParams.is_lazy(query: tim_common.fileParams.QueryClass)[source]#
- tim_common.fileParams.is_review(query: tim_common.fileParams.QueryClass)[source]#
- tim_common.fileParams.is_user_lazy(query: tim_common.fileParams.QueryClass)[source]#
- tim_common.fileParams.join_dict(a: dict, b: dict)[source]#
Joins two dict and returns a new one.
- Return type
dict
- Parameters
a – first dict to join
b – next dict to join
- Returns
“a+b”
- tim_common.fileParams.make_lazy(plugin_html: str, query: tim_common.fileParams.QueryClass, htmlfunc: Callable[[tim_common.fileParams.QueryClass], str]) str [source]#
Makes plugin string to lazy.
- Parameters
plugin_html – ready html for the plugin
query – query params where lazy options can be read
htmlfunc – function to generate the lazy version of plugin html
:return true if lazy plugin is needed
- tim_common.fileParams.multi_post_params(self) list[tim_common.fileParams.QueryClass] [source]#
- tim_common.fileParams.post_params(self: http.server.BaseHTTPRequestHandler) tim_common.fileParams.QueryClass [source]#
- tim_common.fileParams.query_params_to_map(query: dict[str, Any], transform=None, deny: Optional[dict[str, Any]] = None)[source]#
- tim_common.fileParams.query_params_to_map_check_parts(query: tim_common.fileParams.QueryClass, deny: Optional[dict[str, Any]] = None)[source]#
- tim_common.fileParams.replace_random(query: tim_common.fileParams.QueryClass, s)[source]#
- tim_common.fileParams.replace_template_param(query: tim_common.fileParams.QueryClass, template: str, cond_itemname: str, default='') str [source]#
Replaces all occurances of itemnames and cond_item_name in template by their value in query if cond_itemname exists in query.
- Parameters
query – query params where items can be read
template – string that may include items like {{userword}} that are replaced
cond_itemname – name for the item that decides if the template is non empty. None means no condition
default – value to be used if item is default or no item at all, if defult==””, return “” if used
:return replaced template or “”
- tim_common.fileParams.replace_template_params(query: tim_common.fileParams.QueryClass, template: str, cond_itemname: str, itemnames=None) str [source]#
Replaces all occurances of itemnames and cond_item_name in template by their value in query if cond_itemname exists in query.
- Parameters
query – query params where items can be read
template – string that may include items like {{userword}} that are replaced
cond_itemname – name for the item that decides if the template is non empty. None means no condition
itemnames – list of other item names that are replaced by their value in Query
:return true if lazy plugin is needed
- tim_common.fileParams.scan_lines(lines: list[str], n: int, i: int, scanner, direction: int)[source]#
- tim_common.fileParams.string_to_string_replace_attribute(line: str, what_to_replace: str, query: tim_common.fileParams.QueryClass)[source]#
- tim_common.fileParams.string_to_string_replace_url(line: str, what_to_replace: str, query: tim_common.fileParams.QueryClass)[source]#
tim_common.html_sanitize module#
- tim_common.html_sanitize.escape_data_svg(svg_string: str) str [source]#
Converts data:image/svg+xml;base64, to data:image/safe;base64,
- tim_common.html_sanitize.fromstring(html_string: str) Any [source]#
Parses string into an LXML document or element. Unlike LXML’s fromstring, calls document_fromstring or fragment_fromstring, based on whether the string looks like a full document, or just a fragment.
- Parameters
html_string – String to parse
- Returns
An LXML document
- tim_common.html_sanitize.looks_like_full_html(string, pos=0, endpos=9223372036854775807)#
Matches zero or more characters at the beginning of the string.
- tim_common.html_sanitize.presanitize_html_body(html_string: str) str [source]#
Apply basic <html> tag sanitization. This may be needed in cases where user-given yet un-sanitized HTML is parsed by lxml before proper sanitization.
- Parameters
html_string – HTML to sanitize
- Returns
HTML string with <html> tag sanitized in a basic way for LXML to parse it
- tim_common.html_sanitize.replace_data_escaped(repl, string, count=0)#
Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.
- tim_common.html_sanitize.replace_data_svg(repl, string, count=0)#
Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.
- tim_common.html_sanitize.sanitize_with_cleaner(html_string: str, cleaner: lxml.html.clean.Cleaner) str [source]#
- tim_common.html_sanitize.unescape_data_svg(svg_string: str) str [source]#
Converts data:image/safe;base64, back to data:image/svg+xml;base64,
tim_common.markupmodels module#
- class tim_common.markupmodels.GenericMarkupModel(accessDuration: int | None | marshmallow.utils._Missing = <marshmallow.missing>, accessEndText: str | None | marshmallow.utils._Missing = <marshmallow.missing>, anonymous: bool | None | marshmallow.utils._Missing = <marshmallow.missing>, answerLimit: int | None | marshmallow.utils._Missing = <marshmallow.missing>, automd: bool | None | marshmallow.utils._Missing = <marshmallow.missing>, buttonNewTask: str | None | marshmallow.utils._Missing = <marshmallow.missing>, cache: bool | None | marshmallow.utils._Missing = <marshmallow.missing>, deadline: typing.Union[tim_common.markupmodels.PluginDateTime, datetime.datetime, None, marshmallow.utils._Missing] = <marshmallow.missing>, fields: list[str] | None | marshmallow.utils._Missing = <marshmallow.missing>, floatHeader: str | None | marshmallow.utils._Missing = <marshmallow.missing>, floatSize: tuple[int, int] | None | marshmallow.utils._Missing = <marshmallow.missing>, header: str | None | marshmallow.utils._Missing = <marshmallow.missing>, headerText: str | None | marshmallow.utils._Missing = <marshmallow.missing>, hideBrowser: bool | marshmallow.utils._Missing | None = <marshmallow.missing>, initNewAnswer: str | None | marshmallow.utils._Missing = <marshmallow.missing>, lazy: bool | marshmallow.utils._Missing = <marshmallow.missing>, maxHeight: str | None | marshmallow.utils._Missing = <marshmallow.missing>, minHeight: str | None | marshmallow.utils._Missing = <marshmallow.missing>, pointsRule: tim_common.markupmodels.PointsRule | None | marshmallow.utils._Missing = <marshmallow.missing>, pointsText: str | None | marshmallow.utils._Missing = <marshmallow.missing>, postprogram: str | marshmallow.utils._Missing = <marshmallow.missing>, postoutput: str | marshmallow.utils._Missing = <marshmallow.missing>, showPoints: bool | None | marshmallow.utils._Missing = <marshmallow.missing>, starttime: typing.Union[tim_common.markupmodels.PluginDateTime, datetime.datetime, None, marshmallow.utils._Missing] = <marshmallow.missing>, showInView: bool | marshmallow.utils._Missing = <marshmallow.missing>, stem: str | None | marshmallow.utils._Missing = <marshmallow.missing>, triesText: str | None | marshmallow.utils._Missing = <marshmallow.missing>, useCurrentUser: bool | None | marshmallow.utils._Missing = <marshmallow.missing>, texafterprint: str | None | marshmallow.utils._Missing = <marshmallow.missing>, texbeforeprint: str | None | marshmallow.utils._Missing = <marshmallow.missing>, texprint: str | None | marshmallow.utils._Missing = <marshmallow.missing>, hidden_keys: list[str] | marshmallow.utils._Missing = <marshmallow.missing>, button: str | None | marshmallow.utils._Missing = <marshmallow.missing>, buttonText: str | None | marshmallow.utils._Missing = <marshmallow.missing>, allowUnsavedLeave: bool | marshmallow.utils._Missing | None = <marshmallow.missing>, disableUnchanged: bool | marshmallow.utils._Missing | None = <marshmallow.missing>, footer: str | marshmallow.utils._Missing = <marshmallow.missing>, forceBrowser: bool | marshmallow.utils._Missing | None = <marshmallow.missing>, globalField: bool | marshmallow.utils._Missing | None = <marshmallow.missing>, readonly: bool | marshmallow.utils._Missing | None = <marshmallow.missing>, lang: str | None | marshmallow.utils._Missing = <marshmallow.missing>, resetText: str | marshmallow.utils._Missing | None = <marshmallow.missing>, connectionErrorMessage: str | marshmallow.utils._Missing = <marshmallow.missing>, undo: tim_common.markupmodels.UndoInfo | marshmallow.utils._Missing | None = <marshmallow.missing>)[source]#
Bases:
tim_common.markupmodels.KnownMarkupFields
Specifies which fields the editor can use in the plugin markup. This base class defines some fields that are applicable to all plugins.
The difference to KnownMarkupFields is that this class should define fields that are not used by TIM.
TODO: Some fields here should be moved to KnownMarkupFields.
- allowUnsavedLeave: bool | marshmallow.utils._Missing | None = <marshmallow.missing>#
- button: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- buttonText: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- connectionErrorMessage: str | marshmallow.utils._Missing = <marshmallow.missing>#
- disableUnchanged: bool | marshmallow.utils._Missing | None = <marshmallow.missing>#
- forceBrowser: bool | marshmallow.utils._Missing | None = <marshmallow.missing>#
- globalField: bool | marshmallow.utils._Missing | None = <marshmallow.missing>#
Meta field that keeps track which markup fields were hidden (that is, prefixed with “-“). Hidden keys are never sent to browser.
- lang: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- readonly: bool | marshmallow.utils._Missing | None = <marshmallow.missing>#
- resetText: str | marshmallow.utils._Missing | None = <marshmallow.missing>#
- undo: tim_common.markupmodels.UndoInfo | marshmallow.utils._Missing | None = <marshmallow.missing>#
- class tim_common.markupmodels.KnownMarkupFields(accessDuration: int | None | marshmallow.utils._Missing = <marshmallow.missing>, accessEndText: str | None | marshmallow.utils._Missing = <marshmallow.missing>, anonymous: bool | None | marshmallow.utils._Missing = <marshmallow.missing>, answerLimit: int | None | marshmallow.utils._Missing = <marshmallow.missing>, automd: bool | None | marshmallow.utils._Missing = <marshmallow.missing>, buttonNewTask: str | None | marshmallow.utils._Missing = <marshmallow.missing>, cache: bool | None | marshmallow.utils._Missing = <marshmallow.missing>, deadline: typing.Union[tim_common.markupmodels.PluginDateTime, datetime.datetime, None, marshmallow.utils._Missing] = <marshmallow.missing>, fields: list[str] | None | marshmallow.utils._Missing = <marshmallow.missing>, floatHeader: str | None | marshmallow.utils._Missing = <marshmallow.missing>, floatSize: tuple[int, int] | None | marshmallow.utils._Missing = <marshmallow.missing>, header: str | None | marshmallow.utils._Missing = <marshmallow.missing>, headerText: str | None | marshmallow.utils._Missing = <marshmallow.missing>, hideBrowser: bool | marshmallow.utils._Missing | None = <marshmallow.missing>, initNewAnswer: str | None | marshmallow.utils._Missing = <marshmallow.missing>, lazy: bool | marshmallow.utils._Missing = <marshmallow.missing>, maxHeight: str | None | marshmallow.utils._Missing = <marshmallow.missing>, minHeight: str | None | marshmallow.utils._Missing = <marshmallow.missing>, pointsRule: tim_common.markupmodels.PointsRule | None | marshmallow.utils._Missing = <marshmallow.missing>, pointsText: str | None | marshmallow.utils._Missing = <marshmallow.missing>, postprogram: str | marshmallow.utils._Missing = <marshmallow.missing>, postoutput: str | marshmallow.utils._Missing = <marshmallow.missing>, showPoints: bool | None | marshmallow.utils._Missing = <marshmallow.missing>, starttime: typing.Union[tim_common.markupmodels.PluginDateTime, datetime.datetime, None, marshmallow.utils._Missing] = <marshmallow.missing>, showInView: bool | marshmallow.utils._Missing = <marshmallow.missing>, stem: str | None | marshmallow.utils._Missing = <marshmallow.missing>, triesText: str | None | marshmallow.utils._Missing = <marshmallow.missing>, useCurrentUser: bool | None | marshmallow.utils._Missing = <marshmallow.missing>, texafterprint: str | None | marshmallow.utils._Missing = <marshmallow.missing>, texbeforeprint: str | None | marshmallow.utils._Missing = <marshmallow.missing>, texprint: str | None | marshmallow.utils._Missing = <marshmallow.missing>)[source]#
Bases:
tim_common.markupmodels.HiddenFieldsMixin
Represents the plugin markup fields that are known and used by TIM.
- accessDuration: int | None | marshmallow.utils._Missing = <marshmallow.missing>#
- accessEndText: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- anonymous: bool | None | marshmallow.utils._Missing = <marshmallow.missing>#
- answerLimit: int | None | marshmallow.utils._Missing = <marshmallow.missing>#
- automd: bool | None | marshmallow.utils._Missing = <marshmallow.missing>#
- buttonNewTask: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- cache: bool | None | marshmallow.utils._Missing = <marshmallow.missing>#
- deadline: Union[tim_common.markupmodels.PluginDateTime, datetime.datetime, None, marshmallow.utils._Missing] = <marshmallow.missing>#
- fields: list[str] | None | marshmallow.utils._Missing = <marshmallow.missing>#
- floatHeader: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- floatSize: tuple[int, int] | None | marshmallow.utils._Missing = <marshmallow.missing>#
- header: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- headerText: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- hideBrowser: bool | marshmallow.utils._Missing | None = <marshmallow.missing>#
- initNewAnswer: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- lazy: bool | marshmallow.utils._Missing = <marshmallow.missing>#
- maxHeight: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- minHeight: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- pointsRule: tim_common.markupmodels.PointsRule | None | marshmallow.utils._Missing = <marshmallow.missing>#
- pointsText: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- postoutput: str | marshmallow.utils._Missing = <marshmallow.missing>#
- postprogram: str | marshmallow.utils._Missing = <marshmallow.missing>#
- showInView: bool | marshmallow.utils._Missing = <marshmallow.missing>#
- showPoints: bool | None | marshmallow.utils._Missing = <marshmallow.missing>#
- starttime: Union[tim_common.markupmodels.PluginDateTime, datetime.datetime, None, marshmallow.utils._Missing] = <marshmallow.missing>#
- stem: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- texafterprint: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- texbeforeprint: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- texprint: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- triesText: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- useCurrentUser: bool | None | marshmallow.utils._Missing = <marshmallow.missing>#
- class tim_common.markupmodels.PluginDateTimeField(*, load_default: typing.Any = <marshmallow.missing>, missing: typing.Any = <marshmallow.missing>, dump_default: typing.Any = <marshmallow.missing>, default: typing.Any = <marshmallow.missing>, data_key: typing.Optional[str] = None, attribute: typing.Optional[str] = None, validate: typing.Union[None, typing.Callable[[typing.Any], typing.Any], typing.Iterable[typing.Callable[[typing.Any], typing.Any]]] = None, required: bool = False, allow_none: typing.Optional[bool] = None, load_only: bool = False, dump_only: bool = False, error_messages: typing.Optional[dict[str, str]] = None, metadata: typing.Optional[typing.Mapping[str, typing.Any]] = None, **additional_metadata)[source]#
Bases:
marshmallow.fields.Field
- class tim_common.markupmodels.PointsRule(maxPoints: str | int | float | None | marshmallow.utils._Missing = <marshmallow.missing>, allowUserMin: int | float | None | marshmallow.utils._Missing = <marshmallow.missing>, allowUserMax: int | float | None | marshmallow.utils._Missing = <marshmallow.missing>, multiplier: int | float | None | marshmallow.utils._Missing = <marshmallow.missing>, penalties: dict[str, float] | None | marshmallow.utils._Missing = <marshmallow.missing>)[source]#
Bases:
object
- allowUserMax: int | float | None | marshmallow.utils._Missing = <marshmallow.missing>#
- allowUserMin: int | float | None | marshmallow.utils._Missing = <marshmallow.missing>#
- maxPoints: str | int | float | None | marshmallow.utils._Missing = <marshmallow.missing>#
- multiplier: int | float | None | marshmallow.utils._Missing = <marshmallow.missing>#
- penalties: dict[str, float] | None | marshmallow.utils._Missing = <marshmallow.missing>#
- class tim_common.markupmodels.UndoInfo(button: str | None | marshmallow.utils._Missing = <marshmallow.missing>, title: str | None | marshmallow.utils._Missing = <marshmallow.missing>, confirmation: str | None | marshmallow.utils._Missing = <marshmallow.missing>, confirmationTitle: str | None | marshmallow.utils._Missing = <marshmallow.missing>)[source]#
Bases:
object
- button: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- confirmation: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- confirmationTitle: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
- title: str | None | marshmallow.utils._Missing = <marshmallow.missing>#
tim_common.marshmallow_dataclass module#
This library allows the conversion of python 3.7’s dataclasses
to marshmallow
schemas.
It takes a python class, and generates a marshmallow schema for it.
Simple example:
from marshmallow import Schema
from marshmallow_dataclass import dataclass
@dataclass
class Point:
x:float
y:float
point = Point(x=0, y=0)
point_json = Point.Schema().dumps(point)
Full example:
from marshmallow import Schema
from dataclasses import field
from marshmallow_dataclass import dataclass
import datetime
@dataclass
class User:
birth: datetime.date = field(metadata= {
"required": True # A parameter to pass to marshmallow's field
})
website:str = field(metadata = {
"marshmallow_field": marshmallow.fields.Url() # Custom marshmallow field
})
Schema: ClassVar[Type[Schema]] = Schema # For the type checker
- tim_common.marshmallow_dataclass.add_schema(_cls: type[_U]) type[_U] [source]#
- tim_common.marshmallow_dataclass.add_schema(base_schema: type[marshmallow.schema.Schema] = None) Callable[[type[_U]], type[_U]]
- tim_common.marshmallow_dataclass.add_schema(_cls: type[_U], base_schema: type[marshmallow.schema.Schema] = None) type[_U]
This decorator adds a marshmallow schema as the ‘Schema’ attribute in a dataclass. It uses
class_schema()
internally.- Parameters
cls (type) – The dataclass to which a Schema should be added
base_schema – marshmallow schema used as a base class when deriving dataclass schema
>>> class BaseSchema(marshmallow.Schema): ... def on_bind_field(self, field_name, field_obj): ... field_obj.data_key = (field_obj.data_key or field_name).upper()
>>> @add_schema(base_schema=BaseSchema) ... @dataclasses.dataclass ... class Artist: ... names: Tuple[str, str] >>> artist = Artist.Schema().loads('{"NAMES": ["Martin", "Ramirez"]}') >>> artist Artist(names=('Martin', 'Ramirez'))
- tim_common.marshmallow_dataclass.class_schema(clazz: type, base_schema: Optional[type[marshmallow.schema.Schema]] = None) type[marshmallow.schema.Schema] [source]#
Convert a class to a marshmallow schema
- Parameters
clazz – A python class (may be a dataclass)
base_schema – marshmallow schema used as a base class when deriving dataclass schema
- Returns
A marshmallow Schema corresponding to the dataclass
Note
All the arguments supported by marshmallow field classes are can be passed in the metadata dictionary of a field.
If you want to use a custom marshmallow field (one that has no equivalent python type), you can pass it as the
marshmallow_field
key in the metadata dictionary.>>> import typing >>> Meters = typing.NewType('Meters', float) >>> @dataclasses.dataclass() ... class Building: ... height: Optional[Meters] ... name: str = dataclasses.field(default="anonymous") ... class Meta: ... ordered = True ... >>> class_schema(Building) # Returns a marshmallow schema class (not an instance) <class 'marshmallow.schema.Building'> >>> @dataclasses.dataclass() ... class City: ... name: str = dataclasses.field(metadata={'required':True}) ... best_building: Building # Reference to another dataclasses. A schema will be created for it too. ... other_buildings: List[Building] = dataclasses.field(default_factory=lambda: []) ... >>> citySchema = class_schema(City)() >>> city = citySchema.load({"name":"Paris", "best_building": {"name": "Eiffel Tower"}}) >>> city City(name='Paris', best_building=Building(height=None, name='Eiffel Tower'), other_buildings=[])
>>> citySchema.load({"name":"Paris"}) Traceback (most recent call last): ... marshmallow.exceptions.ValidationError: {'best_building': ['Missing data for required field.']}
>>> city_json = citySchema.dump(city) >>> city_json['best_building'] # We get an OrderedDict because we specified order = True in the Meta class OrderedDict([('height', None), ('name', 'Eiffel Tower')])
>>> @dataclasses.dataclass() ... class Person: ... name: str = dataclasses.field(default="Anonymous") ... friends: List['Person'] = dataclasses.field(default_factory=lambda:[]) # Recursive field ... >>> person = class_schema(Person)().load({ ... "friends": [{"name": "Roger Boucher"}] ... }) >>> person Person(name='Anonymous', friends=[Person(name='Roger Boucher', friends=[])])
>>> @dataclasses.dataclass() ... class C: ... important: int = dataclasses.field(init=True, default=0) ... # Only fields that are in the __init__ method will be added: ... unimportant: int = dataclasses.field(init=False, default=0) ... >>> c = class_schema(C)().load({ ... "important": 9, # This field will be imported ... "unimportant": 9 # This field will NOT be imported ... }, unknown=marshmallow.EXCLUDE) >>> c C(important=9, unimportant=0)
>>> @dataclasses.dataclass ... class Website: ... url:str = dataclasses.field(metadata = { ... "marshmallow_field": marshmallow.fields.Url() # Custom marshmallow field ... }) ... >>> class_schema(Website)().load({"url": "I am not a good URL !"}) Traceback (most recent call last): ... marshmallow.exceptions.ValidationError: {'url': ['Not a valid URL.']}
>>> @dataclasses.dataclass ... class NeverValid: ... @marshmallow.validates_schema ... def validate(self, data, **_): ... raise marshmallow.ValidationError('never valid') ... >>> class_schema(NeverValid)().load({}) Traceback (most recent call last): ... marshmallow.exceptions.ValidationError: {'_schema': ['never valid']}
>>> # noinspection PyTypeChecker >>> class_schema(None) # unsupported type Traceback (most recent call last): ... TypeError: None is not a dataclass and cannot be turned into one.
>>> @dataclasses.dataclass ... class Anything: ... name: str ... @marshmallow.validates('name') ... def validates(self, value): ... if len(value) > 5: raise marshmallow.ValidationError("Name too long") >>> class_schema(Anything)().load({"name": "aaaaaargh"}) Traceback (most recent call last): ... marshmallow.exceptions.ValidationError: {'name': ['Name too long']}
- tim_common.marshmallow_dataclass.dataclass(_cls: Optional[type[_U]] = None, *, repr: bool = True, eq: bool = True, order: bool = False, unsafe_hash: bool = False, frozen: bool = False, base_schema: Optional[type[marshmallow.schema.Schema]] = None) Union[type[_U], Callable[[type[_U]], type[_U]]] [source]#
This decorator does the same as dataclasses.dataclass, but also applies
add_schema()
. It adds a .Schema attribute to the class object- Parameters
base_schema – marshmallow schema used as a base class when deriving dataclass schema
>>> @dataclass ... class Artist: ... name: str >>> Artist.Schema <class 'marshmallow.schema.Artist'>
>>> from typing import ClassVar >>> from marshmallow import Schema >>> @dataclass(order=True) # preserve field order ... class Point: ... x:float ... y:float ... Schema: ClassVar[Type[Schema]] = Schema # For the type checker ... >>> Point.Schema().load({'x':0, 'y':0}) # This line can be statically type checked Point(x=0.0, y=0.0)
- tim_common.marshmallow_dataclass.field_for_schema(typ: type, default=<dataclasses._MISSING_TYPE object>, metadata: typing.Optional[typing.Mapping[str, typing.Any]] = None, clazz: typing.Optional[type] = None, name=None, base_schema: typing.Optional[type[marshmallow.schema.Schema]] = None) marshmallow.fields.Field [source]#
Get a marshmallow Field corresponding to the given python type. The metadata of the dataclass field is used as arguments to the marshmallow Field.
- Parameters
typ – The type for which a field should be generated
default – value to use for (de)serialization when the field is missing
metadata – Additional parameters to pass to the marshmallow field constructor
base_schema – marshmallow schema used as a base class when deriving dataclass schema
>>> int_field = field_for_schema(int, default=9, metadata=dict(required=True)) >>> int_field.__class__ <class 'marshmallow.fields.Integer'>
>>> int_field.default 9
>>> field_for_schema(str, metadata={"marshmallow_field": marshmallow.fields.Url()}).__class__ <class 'marshmallow.fields.Url'>
tim_common.plugin_gunicorn module#
tim_common.pluginserver_flask module#
Defines the skeleton for all TIM plugins.
TIM interacts with plugins through certain HTTP routes:
- multihtml: Renders the plugin instances as HTML.
Called with a list of JSONs of the same plugin type. The route returns a list of corresponding HTMLs.
answer: Renders the plugin response when an answer is posted to the plugin.
The accepted data of each route is defined by a dataclass. Upon successful validation, the JSON is converted to the corresponding Model object that can be used in code.
If validation fails, the plugin returns an error with status code 422.
- class tim_common.pluginserver_flask.EditorMenuItem[source]#
Bases:
TypedDict
- data: str#
- expl: str#
- text: str#
- class tim_common.pluginserver_flask.GenericAnswerModel(info: tim_common.pluginserver_flask.InfoModel, markup: tim_common.pluginserver_flask.PluginMarkup, state: Optional[tim_common.pluginserver_flask.PluginState], taskID: str, input: tim_common.pluginserver_flask.PluginInput)[source]#
Bases:
tim_common.pluginserver_flask.GenericRouteModel
[tim_common.pluginserver_flask.PluginInput
,tim_common.pluginserver_flask.PluginMarkup
,tim_common.pluginserver_flask.PluginState
]Generic base class for answer route models.
- input: tim_common.pluginserver_flask.PluginInput#
- make_answer_error(msg: str) tim_common.pluginserver_flask.PluginAnswerResp [source]#
- class tim_common.pluginserver_flask.GenericHtmlModel(info: tim_common.pluginserver_flask.InfoModel | None, markup: tim_common.pluginserver_flask.PluginMarkup, state: typing.Optional[tim_common.pluginserver_flask.PluginState], taskID: str, anonymous: bool, current_user_id: str, doLazy: tim_common.pluginserver_flask.Laziness, preview: bool, review: bool, targetFormat: str, taskIDExt: str, user_id: str, userPrint: bool, viewmode: bool, access: str | marshmallow.utils._Missing = <marshmallow.missing>)[source]#
Bases:
tim_common.pluginserver_flask.GenericRouteModel
[tim_common.pluginserver_flask.PluginInput
,tim_common.pluginserver_flask.PluginMarkup
,tim_common.pluginserver_flask.PluginState
]Generic base class for HTML route models.
- access: str | marshmallow.utils._Missing = <marshmallow.missing>#
- anonymous: bool#
- current_user_id: str#
- get_component_html_name() str [source]#
Gets the name of the Angular component as it should be in HTML.
- get_maybe_empty_static_html() str [source]#
Renders a static version of the plugin. When plugin is not shown in view mode, return empty string
- get_static_html() str [source]#
Renders a static version of the plugin.
This is meant to be a static lightweight lookalike version of the plugin.
- preview: bool#
- review: bool#
- show_in_view() bool [source]#
If component is not show in view mode return False :return: is component shown in view mode
- show_in_view_default() bool [source]#
If component is not show in view mode return False. This method is for to be overridden in child classes if they should not be shown on View-mode :return: is component shown in view as deafault
- targetFormat: str#
- taskIDExt: str#
- userPrint: bool#
- user_id: str#
- viewmode: bool#
- class tim_common.pluginserver_flask.GenericHtmlModelWithContext(info: tim_common.pluginserver_flask.InfoModel | None, markup: ~PluginMarkup, state: Optional[~PluginState], taskID: str, anonymous: bool, current_user_id: str, doLazy: tim_common.pluginserver_flask.Laziness, preview: bool, review: bool, targetFormat: str, taskIDExt: str, user_id: str, userPrint: bool, viewmode: bool, access: str | marshmallow.utils._Missing = <marshmallow.missing>)[source]#
Bases:
Generic
[tim_common.pluginserver_flask.PluginInput
,tim_common.pluginserver_flask.PluginMarkup
,tim_common.pluginserver_flask.PluginState
,tim_common.pluginserver_flask.PluginContext
],tim_common.pluginserver_flask.GenericHtmlModel
[tim_common.pluginserver_flask.PluginInput
,tim_common.pluginserver_flask.PluginMarkup
,tim_common.pluginserver_flask.PluginState
],abc.ABC
- ctx: Optional[tim_common.pluginserver_flask.PluginContext] = None#
- class tim_common.pluginserver_flask.GenericRouteModel(info: tim_common.pluginserver_flask.InfoModel | None, markup: tim_common.pluginserver_flask.PluginMarkup, state: Optional[tim_common.pluginserver_flask.PluginState], taskID: str)[source]#
Bases:
Generic
[tim_common.pluginserver_flask.PluginInput
,tim_common.pluginserver_flask.PluginMarkup
,tim_common.pluginserver_flask.PluginState
]Base class for answer and HTML route models. Contains the fields that the routes have in common.
- info: tim_common.pluginserver_flask.InfoModel | None#
- markup: tim_common.pluginserver_flask.PluginMarkup#
- state: Optional[tim_common.pluginserver_flask.PluginState]#
- taskID: str#
- class tim_common.pluginserver_flask.InfoModel(earlier_answers: int, look_answer: bool, max_answers: int | None, user_id: str, valid: bool)[source]#
Bases:
object
Model for the information that is given by TIM in an answer request.
- earlier_answers: int#
- look_answer: bool#
- max_answers: int | None#
- property primary_user: str#
- user_id: str#
- valid: bool#
- class tim_common.pluginserver_flask.Laziness(value)[source]#
Bases:
enum.Enum
An enumeration.
- Never = 'NEVERLAZY'#
- No = False#
- Yes = True#
- class tim_common.pluginserver_flask.PluginAnswerResp[source]#
Bases:
TypedDict
- save: dict[str, Any]#
- tim_info: tim_common.pluginserver_flask.TimInfo#
- class tim_common.pluginserver_flask.PluginAnswerWeb[source]#
Bases:
TypedDict
- error: str#
- result: str#
- class tim_common.pluginserver_flask.PluginJsonEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]#
Bases:
json.encoder.JSONEncoder
- default(o: Any) Optional[Any] [source]#
Implement this method in a subclass such that it returns a serializable object for
o
, or calls the base implementation (to raise aTypeError
).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
- class tim_common.pluginserver_flask.PluginReqs[source]#
Bases:
TypedDict
- css: list[str]#
- editor_tabs: list[tim_common.pluginserver_flask.EditorTab]#
- js: list[str]#
- multihtml: bool#
- multimd: bool#
- tim_common.pluginserver_flask.create_app(name: str) flask.app.Flask [source]#
Creates the Flask app for the plugin server.
- Parameters
name – Name of import. Usually __name__ should be passed.
- Returns
The app.
- tim_common.pluginserver_flask.create_blueprint(name: str, plugin_name: str, html_model: type[HtmlModel], answer_model: type[AnswerModel], answer_handler: Callable[[tim_common.pluginserver_flask.AnswerModel], tim_common.pluginserver_flask.PluginAnswerResp], reqs_handler: Callable[[], tim_common.pluginserver_flask.PluginReqs], csrf: Optional[flask_wtf.csrf.CSRFProtect] = None, multihtml_preprocessor: Optional[Callable[[list[HtmlModel]], None]] = None) flask.blueprints.Blueprint [source]#
- tim_common.pluginserver_flask.create_nontask_blueprint(name: str, plugin_name: str, html_model: type[HtmlModel], reqs_handler: Callable[[], tim_common.pluginserver_flask.PluginReqs], csrf: Optional[flask_wtf.csrf.CSRFProtect] = None, multihtml_preprocessor: Optional[Callable[[list[HtmlModel]], None]] = None) flask.blueprints.Blueprint [source]#
- tim_common.pluginserver_flask.is_lazy(q: tim_common.pluginserver_flask.GenericHtmlModel) bool [source]#
Determines if the server should render a lazy version of the plugin.
- tim_common.pluginserver_flask.make_base64(d: dict, json_encoder: Optional[type[json.encoder.JSONEncoder]] = None) str [source]#
Converts the given dict to a base64-encoded JSON string.
- tim_common.pluginserver_flask.register_answer_route(app: flask.app.Flask | flask.blueprints.Blueprint, answer_model: type[AnswerModel], answer_handler: Callable[[tim_common.pluginserver_flask.AnswerModel], tim_common.pluginserver_flask.PluginAnswerResp], csrf: Optional[flask_wtf.csrf.CSRFProtect] = None) None [source]#
- tim_common.pluginserver_flask.register_html_routes(app: flask.app.Flask | flask.blueprints.Blueprint, html_schema: type[marshmallow.schema.Schema], reqs_handler: Callable[[], tim_common.pluginserver_flask.PluginReqs], csrf: Optional[flask_wtf.csrf.CSRFProtect] = None, multihtml_preprocessor: Optional[Callable[[list[HtmlModel]], None]] = None) None [source]#
- tim_common.pluginserver_flask.register_plugin_app(name: str, html_model: type[HtmlModel], answer_model: type[AnswerModel], answer_handler: Callable[[tim_common.pluginserver_flask.AnswerModel], tim_common.pluginserver_flask.PluginAnswerResp], reqs_handler: Callable[[], tim_common.pluginserver_flask.PluginReqs]) flask.app.Flask [source]#
- tim_common.pluginserver_flask.render_multimd(args: list[dict], schema: marshmallow.schema.Schema) flask.wrappers.Response [source]#
Renders HTMLs according to the given Schema.
- Parameters
args – Partially validated HTML arguments.
schema – The marshmallow schema to use for validating the plugin data.
- Returns
List of HTMLs.
- tim_common.pluginserver_flask.render_plugin_html(m: tim_common.pluginserver_flask.GenericHtmlModel[tim_common.pluginserver_flask.PluginInput, tim_common.pluginserver_flask.PluginMarkup, tim_common.pluginserver_flask.PluginState]) str [source]#
Renders HTML for a plugin.
- Parameters
m – The plugin HTML schema.
- Returns
HTML.
- tim_common.pluginserver_flask.render_plugin_lazy(m: tim_common.pluginserver_flask.GenericHtmlModel[tim_common.pluginserver_flask.PluginInput, tim_common.pluginserver_flask.PluginMarkup, tim_common.pluginserver_flask.PluginState]) str [source]#
Renders lazy HTML for a plugin.
The lazy HTML displays the static version of the plugin and has the real HTML in an attribute that will be activated on mouse hover.
The end “<!–lazy” is needed because it is the old style of storing the real HTML and TIM does not recognize the new style yet, so otherwise TIM would wrap this inside <!–lazy … lazy–>.
- Parameters
m – The plugin HTML schema.
- Returns
HTML.
- tim_common.pluginserver_flask.render_plugin_md(m: tim_common.pluginserver_flask.GenericHtmlModel[tim_common.pluginserver_flask.PluginInput, tim_common.pluginserver_flask.PluginMarkup, tim_common.pluginserver_flask.PluginState]) str [source]#
Renders HTML for a plugin.
- Parameters
m – The plugin HTML schema.
- Returns
HTML.
- tim_common.pluginserver_flask.render_plugin_with_login_request(m: tim_common.pluginserver_flask.GenericHtmlModel[tim_common.pluginserver_flask.PluginInput, tim_common.pluginserver_flask.PluginMarkup, tim_common.pluginserver_flask.PluginState]) str [source]#
Renders a static version of the plugin as HTML along with a request to log in.
tim_common.tim_server module#
Base class for TIM plugin server. THIS IS DEPRECATED, DO NOT USE IN NEW CODE! Serving from local port 5000.
- class tim_common.tim_server.ThreadedHTTPServer(server_address, RequestHandlerClass, bind_and_activate=True)[source]#
Bases:
socketserver.ThreadingMixIn
,http.server.HTTPServer
Handle requests in a separate thread.
- class tim_common.tim_server.TimServer(request, client_address, _server)[source]#
Bases:
http.server.BaseHTTPRequestHandler
Base class for TIM-server. THIS IS DEPRECATED, DO NOT USE IN NEW CODE!
- do_POST()[source]#
Do needed things for POST request This may be a f.ex a request single html-plugin or multiple plugins.
- Returns
nothing
- do_all(query: tim_common.fileParams.QueryClass)[source]#
Do all other routes.
- Parameters
query – post and get params
- Returns
nothing
- do_answer(query: tim_common.fileParams.QueryClass)[source]#
Do answer route.
- Parameters
query – post and get params
- Returns
nothing
- do_template(query: tim_common.fileParams.QueryClass)[source]#
Gets a template.
:rtype : str :param query: get or put params :return: template result as json
- get_html(query: tim_common.fileParams.QueryClass) str [source]#
Return the html for this query. Params are dumbed as hexstring to avoid problems with html input and so on.
:rtype : str :param query: get or put params :return : html string for this markup
- send_text(txt: str, content_type: str)[source]#
Sends a txt to server.
- Parameters
txt – text to send
content_type – files_content type
- Returns
nothing
- send_text_file(name: str, ftype: str, content_type: str)[source]#
Sends a file to server from directory ftype with contect_type.
- Parameters
name – files name part, possible extra directories
ftype – files type (js, html, css), specifies also the directory where to get the file
content_type – files_content type
- Returns
nothing
- tim_common.tim_server.log(request: tim_common.tim_server.TimServer)[source]#
Log the time and user.
- Parameters
request –
- Returns
Nothing
tim_common.typing_inspect_ext module#
tim_common.utils module#
- class tim_common.utils.DurationField(*, load_default: typing.Any = <marshmallow.missing>, missing: typing.Any = <marshmallow.missing>, dump_default: typing.Any = <marshmallow.missing>, default: typing.Any = <marshmallow.missing>, data_key: typing.Optional[str] = None, attribute: typing.Optional[str] = None, validate: typing.Union[None, typing.Callable[[typing.Any], typing.Any], typing.Iterable[typing.Callable[[typing.Any], typing.Any]]] = None, required: bool = False, allow_none: typing.Optional[bool] = None, load_only: bool = False, dump_only: bool = False, error_messages: typing.Optional[dict[str, str]] = None, metadata: typing.Optional[typing.Mapping[str, typing.Any]] = None, **additional_metadata)[source]#
Bases:
marshmallow.fields.Field
- class tim_common.utils.DurationSchema(*, only: Optional[Union[Sequence[str], Set[str]]] = None, exclude: Union[Sequence[str], Set[str]] = (), many: bool = False, context: Optional[dict] = None, load_only: Union[Sequence[str], Set[str]] = (), dump_only: Union[Sequence[str], Set[str]] = (), partial: Union[bool, Sequence[str], Set[str]] = False, unknown: Optional[str] = None)[source]#
Bases:
marshmallow.schema.Schema
- TYPE_MAPPING: Dict[type, Type[ma_fields.Field]] = {<class 'isodate.duration.Duration'>: <class 'tim_common.utils.DurationField'>}#
- opts: SchemaOpts = <marshmallow.schema.SchemaOpts object>#