smores.smores

Smores

Smores(self, default_template_name='_default_template')

Provides a method of defining a schema for string templates. Presents a tag syntax easy enough for end users to use.

Arguments:

default_template_name (str): The name you'd like to use for default schema templates

render

Smores.render(self, data, template_string, sub_templates=None, fallback_value='', pre_process=None)

Recursively populates the 'template_string' with data gathered from dumping 'data' through the Marshmallow 'schema'. Variables are evaluated and will return the '_default_template' if one exists. Prettifies end result.

Arguments

  • data (dict): data to be dumped via the 'schema' (likely an ORM model instance) accepts both objects and dicts/lists
  • template_string (str): text generated by end-users
  • sub_templates (dict): mapping of subtemplate tag to expanded sub template string
  • fallback_value (str|function|None): either string or function returning a string to serve as default value for tag attrs that cannot be resolved
  • pre_process (function): function that modifies the parsed version of the template

Returns:

string: rendered template

remove_schemas

Smores.remove_schemas(self, schemas)

Unregisters schema(s)

Arguments:

schemas (Schema|list): schemas to unregister

autocomplete

Smores.autocomplete(self, fragment, only=None, exclude=None)

Evaluates a tag fragment, returns a named tuple with the status of the fragment as it is, and possible options that could be used to expand/add to the fragment.

Arguments:

fragment (string): a tag fragment ex: user.addresses
only (list): a list of schemas that should be included
exclude (list): a list of schemas that should be excluded

Returns:

AutocompleteResponse: NamedTuple with both the status of the current tag fragment as well as possible options

Example:

>>> smores.autocomplete("")
AutocompleteResponse(tagStatus='INVALID', options=['address', 'coordinates', 'user'])

>>> smores.autocomplete('user')
AutocompleteResponse(tagStatus='VALID', options=['_default_template', 'address', 'email', 'id', 'name'])

>>> smores.autocomplete('us')
AutocompleteResponse(tagStatus='INVALID', options=['user'])

>>> smores.autocomplete("user.address.coordinates")
AutocompleteResponse(tagStatus='VALID', options=['_default_template', 'lat', 'lng'])

schemas

Property that returns a list of registered schemas

Returns:

list: Currently registered schemas

add_schemas

Smores.add_schemas(self, schemas)

Registers schema(s)

Arguments:

schemas (Schema|list): schemas to register

with_schemas

Smores.with_schemas(*args, **kwds)

Context manager that registers schemas temporarily

Arguments:

schemas (list|Schema):  single schema or list of schemas

Example:

class Event(Schema):
    time = fields.DateTime()
    description = fields.String()

with smores.with_schemas(Event):

Event schema available here

    smores.render(someDate, someTemplate)

Event schema is removed on exit

add_module_schemas

Smores.add_module_schemas(self, module_)

Adds all Schema classes found in module_

Arguments:

module_ (module): Registers schema classes from this module

schema

Smores.schema(self, schema)

A class decorator that registers a marshmallow schema

Example:

smores = Smores()

@smores.schema
class User(Schema):
    name = fields.String()
    email = fields.Email()