API¶
zsl.application.containers¶
Module containing various IoC containers. IoC container holdsa collection of modules.
zsl.application.containers.celery_container¶
- class zsl.application.containers.celery_container.CeleryContainer[source]¶
Configuration for celery application.
- celery_cli¶
alias of
CeleryCliModule
- worker¶
alias of
CeleryTaskQueueMainWorkerModule
- class zsl.application.containers.celery_container.CeleryStandAloneContainer[source]¶
Configuration for application run with help of celery cli tools.
- celery_cli¶
alias of
CeleryCliModule
- worker¶
alias of
CeleryTaskQueueOutsideWorkerModule
zsl.application.containers.container¶
- class zsl.application.containers.container.IoCContainer[source]¶
Collection of DI modules set as class attributes.
This is used for declarative DI configuration, which can be easily extended upon.
>>> class MyContainer(IoCContainer): ... cache = MyCacheModule ... redis = MyRedisModule
>>> class MyTestContainer(MyContainer): ... redis = MockRedisModule
zsl.application.containers.core_container¶
- class zsl.application.containers.core_container.CoreContainer[source]¶
Modules for basic Zsl application.
- cache¶
alias of
RedisCacheInjectionModule
- context¶
alias of
DefaultContextModule
- database¶
alias of
AlchemyModule
- error_handler¶
alias of
ErrorHandlerModule
- logger¶
alias of
LoggerModule
- task_router¶
alias of
TaskRouterModule
zsl.application.containers.web_container¶
- class zsl.application.containers.web_container.WebContainer[source]¶
Configuration for web application.
- web_context¶
alias of
WebContextModule
zsl.application.error_handler¶
This module does the error handling. It allows users to register an error handler for a given exception type. It also provides default error handlers.
- class zsl.application.error_handler.DefaultErrorHandler[source]¶
- ERROR_CODE = 'UNKNOWN_ERROR'¶
- ERROR_MESSAGE = 'An error occurred!'¶
- class zsl.application.error_handler.ForbiddenErrorHandler[source]¶
- ERROR_CODE = 'FORBIDDEN'¶
- class zsl.application.error_handler.ModelConversionErrorHandler[source]¶
- ERROR_CODE = 'INVALID_REQUEST'¶
- class zsl.application.error_handler.RoutingErrorHandler[source]¶
- ERROR_CODE = 'NOT_FOUND'¶
- zsl.application.error_handler.error_handler(f)[source]¶
- Default error handler.
On server side error shows a message ‘An error occurred!’ and returns 500 status code.
Also serves well in the case when the resource/task/method is not found - returns 404 status code.
- zsl.application.error_handler.register(e: ErrorHandler | ErrorProcessor) None[source]¶
zsl.application.initialization_context¶
The modules deals with the initialization of the basics - python path and then loads and initializes the application if necessary.
- class zsl.application.initialization_context.InitializationContext(unit_test=False, initializers=None)[source]¶
-
- property is_initialized¶
- property unit_testing¶
zsl.application.initializers¶
Module for application parts needed to initialize on start.
zsl.application.initializers.library_initializer¶
- class zsl.application.initializers.library_initializer.LibraryInitializer[source]¶
Add vendor modules to current path.
zsl.application.initializers.service_initializer¶
- class zsl.application.initializers.service_initializer.ServiceInitializer[source]¶
Add outside services to application injector.
zsl.application.initializers.unittest_initializer¶
- class zsl.application.initializers.unittest_initializer.UnitTestInitializer[source]¶
Initializer handling the unit test settings.
zsl.application.modules¶
Module for DI modules.
zsl.application.modules.alchemy_module¶
- class zsl.application.modules.alchemy_module.AlchemyModule[source]¶
Adds SQLAlchemy to current configuration.
- provide_session_holder(engine: Engine) SessionHolder[source]¶
- provide_transaction_holder_factory() TransactionHolderFactory[source]¶
- class zsl.application.modules.alchemy_module.DefaultTransactionHolderFactory[source]¶
- create_transaction_holder() TransactionHolder[source]¶
- class zsl.application.modules.alchemy_module.SessionFactory(session_holder: SessionHolder)[source]¶
Creates a db session with an open transaction.
- class zsl.application.modules.alchemy_module.TransactionHolder[source]¶
Holds the transaction and session.
Works like a SQAlchemy session proxy.
- property in_transaction¶
- inject_session(session: Session) None[source]¶
Used to set the session in the @transactional decorator.
- property session: Session¶
- class zsl.application.modules.alchemy_module.TransactionHolderFactory[source]¶
- abstract create_transaction_holder() TransactionHolder[source]¶
zsl.application.modules.cache_module¶
- class zsl.application.modules.cache_module.RedisCacheInjectionModule[source]¶
Adds cache modules into to current configuration using reddis as backend.
- configure(binder: Binder) None[source]¶
Initializer of the cache - creates the Redis cache module as the default cache infrastructure. The module is bound to RedisCacheModule and CacheModule keys. The initializer also creates RedisIdHelper and bounds it to RedisIdHelper and IdHelper keys.
- Parameters:
binder (Binder) – The binder object holding the binding context, we add cache to the binder.
zsl.application.modules.celery_module¶
- class zsl.application.modules.celery_module.CeleryTaskQueueMainWorkerModule[source]¶
Adds celery worker to current configuration.
- class zsl.application.modules.celery_module.CeleryTaskQueueOutsideWorkerModule[source]¶
Adds celery outside worker to current configuration.
Outside worker is meant to run with help of celery cli tools.
zsl.application.modules.context_module¶
Application context describes the environment in which the application is runand is responsible for specific initializations on it.
- class zsl.application.modules.context_module.DefaultContextModule[source]¶
Adds default application context to current configuration.
- class zsl.application.modules.context_module.TestContextModule[source]¶
Test application context to current configuration
- class zsl.application.modules.context_module.WorkerContextModule[source]¶
Worker application context to current configuration.
- zsl.application.modules.context_module.default_initializers = (<class 'zsl.application.initializers.library_initializer.LibraryInitializer'>, <class 'zsl.application.initializers.service_initializer.ServiceInitializer'>)¶
Initializers used in all applications
- zsl.application.modules.context_module.unittest_initializers = (<class 'zsl.application.initializers.library_initializer.LibraryInitializer'>, <class 'zsl.application.initializers.service_initializer.ServiceInitializer'>, <class 'zsl.application.initializers.unittest_initializer.UnitTestInitializer'>)¶
Initializers used in unit test applications
- class zsl.application.modules.error_handler_module.ErrorHandlerModule[source]¶
-
- provide_error_config(config: Config) ErrorConfiguration[source]¶
zsl.application.modules.logger_module¶
- class zsl.application.modules.logger_module.LoggerModule[source]¶
Configure the application logger.
- LOGGING_CONFIG_NAME = 'LOGGING'¶
zsl.application.modules.task_router_module¶
- class zsl.application.modules.task_router.TaskRouterModule[source]¶
Adds task router to current configuration.
- task_provider = <injector.ClassProvider object>¶
- class zsl.application.modules.web.configuration.MethodConfiguration(package=None, packages=None, url_prefix='method')[source]¶
- property packages¶
- property url_prefix¶
- class zsl.application.modules.web.cors.CORSConfiguration(origin: str = '', allow_headers: List[str] | str = None, expose_headers: List[str] | str = None, max_age: int = 21600)[source]¶
- DEFAULT_ALLOW_HEADERS = ['Accept', 'Origin', 'Content-Type', 'Authorization']¶
- DEFAULT_EXPOSE_HEADERS = ['Location', 'X-Total-Count', 'Link']¶
- DEFAULT_MAX_AGE = 21600¶
- class zsl.application.modules.web.web_context_module.WebContextModule[source]¶
Adds web application context to current configuration.
- provide_cors_configuration(config: Config) CORSConfiguration[source]¶
- class zsl.application.modules.web.web_context_module.WebInitializer[source]¶
Initialize the web application.
- zsl.application.modules.web.web_context_module.web_initializers = (<class 'zsl.application.initializers.library_initializer.LibraryInitializer'>, <class 'zsl.application.initializers.service_initializer.ServiceInitializer'>, <class 'zsl.application.modules.web.web_context_module.WebInitializer'>)¶
Initializers used in unit web applications
zsl.application.service_application¶
The module contains the main Zsl class - ServiceApplication. It is
responsible for gluing everything together.
There is a simple profile support. Profile is usually tied to a configuration file for an environment. Usually environments slightly differ in settings - especially connection strings. These twists can be managed via additional configuration files with the cfg extension usually placed in the settings package. The name of the environment configuration file is controlled via ZSL_SETTINGS environment variable.
- zsl.application.service_application.SETTINGS_ENV_VAR_NAME¶
Name of the environment variable to be read for the profile configuration file.
- class zsl.application.service_application.ServiceApplication(import_name, static_url_path=None, static_folder='static', static_host=None, host_matching=False, subdomain_matching=False, template_folder='templates', instance_path=None, instance_relative_config=False, root_path=None, modules=None, version=None, config_object=None, default_settings_module='settings.default_settings')[source]¶
Atteq Service Flask application.
- VERSION = '1.0.0'¶
- property app_version¶
- property injector: Injector¶
- property version¶
- property zsl_version¶
- zsl.application.service_application.get_settings_from_profile(profile: str, profile_dir: Any = None) str[source]¶
“Returns the configuration file path for the given profile.
- Parameters:
profile – Profile name to be used.
profile_dir – The directory where the profile configuration file should reside. It may be also a module, and then the directory of the module is used.
- Returns:
Configuration file path.
- zsl.application.service_application.set_profile(profile)[source]¶
Sets the current profile.
- Parameters:
profile – The profile to be used.
zsl.cache.cache_module¶
- class zsl.cache.cache_module.CacheModule[source]¶
Cache abstraction layer - module for caching.
- abstract get_key(key)[source]¶
Returns the value associated with the
key.TODO this should be named get_by_key or just simply get(key), get_key sounds like it would return a key
zsl.cache.id_helper¶
- class zsl.cache.id_helper.IdHelper[source]¶
zsl.cache.redis_cache_module¶
- class zsl.cache.redis_cache_module.RedisCacheModule(app, config)[source]¶
Abstraction layer for caching.
- get_key(key)[source]¶
Returns the value associated with the
key.TODO this should be named get_by_key or just simply get(key), get_key sounds like it would return a key
zsl.cache.redis_id_helper¶
- class zsl.cache.redis_id_helper.RedisIdHelper(config, redis_cache_module)[source]¶
- CACHE_DEFAULT_TIMEOUT = 300¶
- class zsl.constants.HttpHeaders(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
- CONTENT_TYPE = 'Content-Type'¶
- class zsl.constants.MimeType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
- APPLICATION_JSON = 'application/json'¶
- class zsl.contrib.alembic.alembic_config.AlembicConfiguration(alembic_directory)¶
- alembic_directory¶
Alias for field number 0
zsl.contrib.alembic.alembic_module¶
Alembic module is responsible for handling database migrations for SqlAlchemy database backend. The complete documentation of the Alembic project, all the options and command may be found Alembic website.
If you need database migrations just install alembic extra of zsl package. Then you may use alembic commands from ZSL cli interface via alembic command.
Just use a standard way of calling cli with a container having AlembicModule.
class MyApplicationContainer(WebContainer):
alembic = AlembicModule
@inject(zsl_cli=ZslCli)
def main(zsl_cli: ZslCli) -> None:
zsl_cli()
if __name__ == "__main__":
os.environ[SETTINGS_ENV_VAR_NAME] = ... or set_profile('my-profile')
app = Zsl(__name__, modules=MyApplicationContainer.modules())
main()
Configuration¶
To configure one needs to provide a valid AlembicConfiguration usually in default_settings.py
ALEMBIC = AlembicConfiguration(alembic_directory="...")
Then all the required files will be stored in the directory. Also the alembic.ini file will be stored there although this is not a standard way.
When using AlembicCli interface the alembic commands will be functional though.
A little setup is required so that the connection is opened correctly. In alembic.ini in the given directory remove the following line.
sqlalchemy.url = driver://user:pass@localhost/dbname
Also set script_location to . so that it does not rely on absolute paths
script_location = .
Then in env.py, since we removed the sqlalchemy.url we need to use a ZSL engine created using the correct urls from settings. To do so define the routines run_migrations_offline and run_migrations_online so that they use a correct Engine.
@inject(zsl_config=Config)
def run_migrations_offline(zsl_config):
url = zsl_config['DATABASE_URI']
context.configure(url=url, target_metadata=target_metadata, literal_binds=True)
with context.begin_transaction():
context.run_migrations()
@inject(engine=Engine)
def run_migrations_online(engine):
with engine.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
- class zsl.contrib.alembic.alembic_module.AlembicCli(zsl_cli: ZslCli)[source]¶
Alembic Cli interface support.
- property alembic¶
- class zsl.contrib.alembic.alembic_module.AlembicModule[source]¶
Adds Alembic support for migrations.
- ALEMBIC_CONFIG_NAME = 'ALEMBIC'¶
- provide_alembic_configuration(config: Config) AlembicConfiguration[source]¶
- class zsl.contrib.sentry.sentry_config.SentryConfiguration(dsn, environment=None, tags=None, register_logging_handler=True, sentry_logging_handler_level=40)[source]¶
zsl.contrib.modules.sentry_module¶
Sentry module is responsible for handling errors and sending them to a sentry backend. Just provide a DSN (url) of the backend and you are all set up.
To add the Sentry support just add SentryModule to your container.
class MyApplicationContainer(WebContainer):
sentry = SentryModule
Configuration¶
To configure one needs to provide a valid SentryConfiguration usually in default_settings.py or in your profiles.
SENTRY = SentryConfiguration(dsn="...")
The following options are available:
dsn: DSN/URl of your Sentry project,
environment: the environment (release defaults to the version of your application),
tags: additional tags str to str dictionary,
register_sentry_logging_handler: bool value indicating if logging handler should be added to logging,
sentry_handler_logging_level: minimal log level of log messages sent to sentry logging handler.
To test the functionality use CLI
python app.py sentry test
- class zsl.contrib.sentry.sentry_module.SentryErrorProcessor(config: SentryConfiguration, zsl: ServiceApplication)[source]¶
- class zsl.contrib.sentry.sentry_module.SentryModule[source]¶
Adds Sentry support.
- SENTRY_CONFIG_NAME = 'SENTRY'¶
- provide_sentry_configuration(config: Config) SentryConfiguration[source]¶
- zsl.db.helpers.app_model_or_none(raw_model)[source]¶
Transforms raw_model to its application model. Function can handle None value.
- zsl.db.helpers.app_models(raw_models)[source]¶
For a list of raw models return the list of the corresponding app models.
zsl.db.helpers.nested¶
- zsl.db.helpers.nested.nested_model(model: ModelBase, nested_fields: Any) AppModel | None[source]¶
Return
zsl.db.model.app_model import AppModelwith the nested models attached.nested_fieldscan be a simple list as model fields, or it can be a tree definition in dict with leafs as keys withNonevalue
- zsl.db.helpers.nested.nested_models(models, nested_fields)[source]¶
For a list of
modelsapplyget_nested_modelwith givennested_fields.
zsl.db.helpers.pagination¶
- class zsl.db.helpers.pagination.Pagination(pagination: PaginationRequest | Dict[str, str | int] | None = None)[source]¶
- Pagination support. Allows to paginate a query. There are two choices.
paginate()- paginates a query and obtains the count automatically.apply_pagination()- paginates a query and assumes that record count is set in advance.
- apply_pagination(q: Query) Query[source]¶
Filters the query so that a given page is returned. The record count must be set in advance. :param q: Query to be paged. :return: Paged query.
- property offset¶
- property page_size¶
- paginate(q: Query) Query[source]¶
Filters the query so that a given page is returned. The record count is computed automatically from query. :param q: Query to be paged. :return: Paged query.
- property record_count¶
- class zsl.db.helpers.pagination.PaginationRequest(raw=None, id_name='id', hints=None)[source]¶
- static create(page_no: int = 1, page_size: int = 25) PaginationRequest[source]¶
- class zsl.db.helpers.pagination.PaginationResponse(record_count: int, page_size: int, pagination: PaginationRequest)[source]¶
zsl.db.helpers.query_filter¶
- class zsl.db.helpers.query_filter.OperatorCompareDates[source]¶
Compares only dates, year is not taken into account. Compared date value must be string in format ‘%m-%d’
- class zsl.db.helpers.query_filter.QueryFilter(query_filter, mappings=None, allow_null=False)[source]¶
Helper class for applying filter criteria to query.
zsl.db.helpers.query_helper¶
zsl.db.helpers.sorter¶
- class zsl.db.helpers.sorter.Sorter(sorter, mappings=None)[source]¶
Helper class for applying ordering criteria to query.
zsl.db.model.app_model¶
- class zsl.db.model.app_model.AppModel(raw, id_name='id', hints=None)[source]¶
AppModel’s are used as a thin and simple communication objects. Also they can be saved into cache. Basically they are known as Data Transfer Objects or DTOs.
- __init__(raw, id_name='id', hints=None)[source]¶
The application model model constructor.
- Parameters:
raw – Dictionary of properties of the raw data.
id_name – Name of the identifier property.
hints –
Tells which of the raw attributes are date or datetime string and what is theirs format. Example: ``` {
DATE_DATA: { ‘birthday’: ‘%d.%m.%Y’ }, DATETIME_DATA: { ‘created’: ‘%Y-%m-%d %H:%M:%S’ }
this attributes are then saved in the standard zsl service date/datetime format (consult
zsl.utils.date_helperfor more.)
zsl.db.model.app_model_json_decoder¶
zsl.db.model.app_model_json_encoder¶
- class zsl.db.model.app_model_json_encoder.AppModelJSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]¶
- default(o)[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)
zsl.db.model.raw_model¶
zsl.db.model.sql_alchemy¶
one base to rule them all
- class zsl.errors.ErrorConfiguration(handlers: List[ErrorHandler] = None, processors: List[ErrorProcessor] = None, use_flask_handler: bool = False)[source]¶
The configuration object for error handling.
- property handlers: List[ErrorHandler]¶
- property processors: List[ErrorProcessor]¶
- class zsl.errors.ErrorHandler[source]¶
Custom error handler providing a response on a particular error.
- abstract can_handle(e: ErrorHandler) bool[source]¶
Indicator if the handler is able to handle the given exception e.
- Parameters:
e – The exception that shall be determined if can be handled by the handler.
- Returns:
True or False depending on whether the handler can/should handle the method.
- abstract handle(e: ErrorHandler) bool[source]¶
Handle the exception.
- Parameters:
e – The handled exception.
- Returns:
The error response for the exception.
- class zsl.errors.ErrorProcessor[source]¶
Custom error processor handling an error state caused by an error. For example an error processor may only log an error, send an email, etc. The main difference between an ErrorProcessor and an ErrorHandler is that the latter one returns a response. There may be only a single ErrorHandler returning whereas there may be more ErrorProcessors handling the same error.
- abstract handle(e: ErrorHandler) None[source]¶
- exception zsl.errors.ZslError[source]¶
A base class intended for all exceptions thrown by ZSL or ZSL application.
This script is to specify an appropriate module working with Python 3 (python3_gearman) or Python 2 (gearman).
zsl.interface.celery.worker¶
Implementation of celery workers.
- class zsl.interface.celery.worker.CeleryTaskQueueMainWorker[source]¶
Worker implementation for Celery task queue.
- class zsl.interface.celery.worker.CeleryTaskQueueOutsideWorker(app: ServiceApplication = None, config: Config = None)[source]¶
Celery worker used only for task execution.
This can be used when the worker is managed with some other application, like celery worker or celery multi.
- class zsl.interface.celery.worker.CeleryTaskQueueWorkerBase(app: ServiceApplication = None, config: Config = None)[source]¶
Base class for celery task queue worker.
It contains only the task execution part of worker.
zsl.interface.cli¶
The module is responsible for creating the CLI interface of ZSL and provides the main click group for all the CLI commands and groups. If any command is to be added add it to this group cli defined in this module.
zsl.interface.resource¶
Helper types describing resource results for resource router.
- class zsl.interface.resource.ResourceResult(body, count, links, status, location, expose_headers)¶
- body¶
Alias for field number 0
- count¶
Alias for field number 1
- expose_headers¶
Alias for field number 5
- links¶
Alias for field number 2
- location¶
Alias for field number 4
- status¶
Alias for field number 3
zsl.interface.task¶
- exception zsl.interface.task.ModelConversionError(obj, attribute)[source]¶
- property attribute¶
- property obj¶
- class zsl.interface.task.RequestJSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]¶
- default(o)[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)
- zsl.interface.task.create_simple_model(name, items, defaults=None, parent=<class 'object'>, model_module=None)[source]¶
- zsl.interface.task.exec_task(task_path, data)[source]¶
Execute task.
- Parameters:
task_path (str|Callable) – task path
data (Any) – task’s data
- Returns:
- zsl.interface.task.payload_into_model(model_type: Callable, argument_name: str = 'request', remove_data: bool = True) Callable[source]¶
zsl.interface.task_queue¶
This module contains interfaces and functions to handle task queues in ZSL. A task queue handles asynchronous and distributed code executions.
- exception zsl.interface.task_queue.KillWorkerException[source]¶
If any task raises this exception a standalone worker will be killed.
- class zsl.interface.task_queue.TaskQueueWorker(app: ServiceApplication = None, config: Config = None)[source]¶
Task queue worker abstraction.
A task queue worker is responsible for communicating with a task queue and executing any task given by it. It should be able to run as a stand alone application.
- zsl.interface.task_queue.execute_job(job: Job, app: ServiceApplication = None, task_router: TaskRouter = None) JobResult[source]¶
Execute a job.
- Parameters:
job (Job) – job to execute
app (ServiceApplication) – service application instance, injected
task_router (TaskRouter) – task router instance, injected
- Returns:
task result
- Return type:
- zsl.interface.task_queue.run_worker(*args: Any, **kwargs: Any) None[source]¶
Run the app as a task queue worker.
The worker instance is given as a DI module.
zsl.interface.webservice.importer¶
zsl.interface.webservice.performers.default¶
zsl.interface.webservice.performers.method¶
zsl.interface.webservice.performers.resource¶
zsl.interface.webservice.performers.task¶
- zsl.interface.web.performers.task.create_task_mapping(app: ServiceApplication, task_configuration: TaskConfiguration) None[source]¶
- zsl.interface.web.utils.execution.execute_web_task(job_context: WebJobContext, callable: Callable, app: ServiceApplication) Response[source]¶
zsl.interface.webservice.utils.response_headers¶
- zsl.interface.web.utils.response_headers.append_crossdomain(response: Response, config: Config) None[source]¶
Adds the default crossdomain headers. Uses the
zsl.task.task_decorator.CrossdomainWebTaskResponderto generate the headers.- Parameters:
response – Current web response.
config – Current configuration.
- zsl.interface.web.utils.response_headers.append_headers(f)[source]¶
- Appends all the web headers:
ZSL version and information,
default CORS if not already set up,
cache.
- Parameters:
f – The decorated function.
- Returns:
The function which appends the web headers.
zsl.resource.guard¶
Guard module defines tools to inject security checks into a resource. With
help of the guard class decorator and ResourcePolicy declarative
policy class a complex security resource behaviour can be achieved.
- class zsl.resource.guard.Access(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
- ALLOW = 1¶
- CONTINUE = 3¶
- DENY = 2¶
- class zsl.resource.guard.GuardedMixin[source]¶
Add guarded CRUD methods to resource.
The
guardreplaces the CRUD guarded methods with a wrapper with security checks around these methods. It adds this mixin into the resource automatically, but it can be declared on the resource manually for IDEs to accept calls to the guarded methods.
- exception zsl.resource.guard.PolicyViolationError(message, code=403)[source]¶
Error raised when policy is violated.
It can bear a HTTP status code, 403 is by default.
- class zsl.resource.guard.ResourcePolicy[source]¶
Declarative policy class.
Every CRUD method has is corespondent can_method__before and can_method__after where method can be one of (create, read, update, delete). __before method will get the CRUD method parameters and __after will get the CRUD method result as parameter. On returning
Access.ALLOWaccess is granted. It should returnAccess.CONTINUEwhen the policy is not met, but is not broken, i. e. it is not its responsibility to decide. On returningAccess.DENYor raising aPolicyExceptionpolicy is broken and access is immediately denied.The default implementation of these method lookup for corresponding attribute can_method, so
can_read = Access.ALLOWwill allow access for reading without the declaration ofcan_read__beforeorcan_read__after. default attribute is used if can_method attribute is not declared. For more complex logic it can be declared as a property, see examples:class SimplePolicy(ResourcePolicy): '''Allow read and create''' default = Access.ALLOW can_delete = Access.CONTINUE can_update = Access.CONTINUE class AdminPolicy(ResourcePolicy): '''Only admin has access''' @inject(user_service=UserService) def __init__(self, user_service): self._user_service = user_service @property def default(self): if self._user_service.current_user.is_admin: return Access.ALLOW
- default = 3¶
- zsl.resource.guard.default_error_handler(e: PolicyViolationError, *_: Any) ResourceResult[source]¶
Default policy violation error handler.
It will create an empty resource result with an error HTTP code.
- class zsl.resource.guard.guard[source]¶
Guard decorator.
This decorator wraps the CRUD methods with security checks before and after CRUD method execution, so that the response can be stopped or manipulated. The original CRUD methods are renamed to guarded_method, where method can be [create, read, update, delete], so by using a GuardedResource as a base, you can still redeclare the guarded_methods and won’t loose the security checks.
It takes a list of policies, which will be always checked before and after executing the CRUD method.
Policy is met, when it returns
Access.ALLOW, onAccess.CONTINUEit will continue to check others and onAccess.DENYor raising aPolicyViolationErroraccess will be restricted. If there is no policy which grants the access aPolicyViolationErroris raised and access will be restricted.Guard can have a custom exception handlers or method wrappers to _wrap the CRUD method around.
class Policy(ResourcePolicy): default = Access.DENY can_read = Access.ALLOW # allow only read @guard([Policy()]) class GuardedResource(GuardedMixin): def read(self, param, args, data): return resources[param] class SpecificResource(GuardedResource): # override GuardedResource.read, but with its security checks def guarded_read(self, param, args, data): return specific_resources[param]
- exception_handlers = [<function default_error_handler>]¶
- method_wrappers = []¶
- resource_methods = ['create', 'read', 'update', 'delete']¶
- zsl.resource.guard.transactional_error_handler(e: Any, rv: Any, _: SessionFactory) Any[source]¶
Re-raise a violation error to be handled in the
_nested_transactional.
- class zsl.resource.guard.transactional_guard[source]¶
Security guard for
ModelResource.This add a transactional method wrapper and error handler which calls the rollback on
PolicyViolationError.- exception_handlers = [<function default_error_handler>, <function transactional_error_handler>]¶
- method_wrappers = [<function transactional>, <function _nested_transactional>]¶
zsl.resource.json_server_resource¶
- class zsl.resource.json_server_resource.JsonServerResource(model_cls=None)[source]¶
Model resource implementation to correspond with json-server.
This implements the same REST interface which json-server (https://github.com/typicode/json-server) uses. It transforms the given input arguments into ModelResource-like and then adds metadata to result.
zsl.resource.model_resource – REST for a DB model.¶
Resources provide a way how to directly access DB tables (raw models) and perform CRUD operations upon them. The default classes of the models should be overridden to provide more logic or restrictions if wanted.
- The basic way to use them is as follows:
The models can be defined in the __exposer__.py which is set up at settings.
To override just override the basic methods - create, read, update, delete.
- class zsl.resource.model_resource.CachedModelResource(model_cls, cache_module, id_helper, logger, timeout='short')[source]¶
The cached resource - uses redis to cache the resource for the given amount of seconds.
- class zsl.resource.model_resource.ModelResource(model_cls=None)[source]¶
- static _create_context(params: dict, args: list, data: dict) ResourceQueryContext[source]¶
Creates the resource query context - this an object holding the data alongside the querying of the resource. This object is always present as a parameter for each method during the query and users.py are free to create own properties so that they can optimize and perform the query (so the subsequent methods have an access to the already precomputed data).
- _delete_one(row_id, ctx)[source]¶
Deletes row by the given id – row_id. The method first created the query using the method
_create_delete_one_query()and then executes it.- Parameters:
row_id (int) – Identifier of the deleted row.
ctx (ResourceQueryContext) – The context of this delete query.
- _create_delete_one_query(row_id, ctx)[source]¶
Delete row by id query creation.
- Parameters:
row_id (int) – Identifier of the deleted row.
ctx (ResourceQueryContext) – The context of this delete query.
- create(params: str, args: dict, data: dict) AppModel[source]¶
POST /resource/model_cls/ data
Create new resource
- delete(params: str, args: dict, data: dict) None[source]¶
DELETE /resource/model_cls/[params]?[args]
delete resource/s
- read(params: str = None, args: dict = None, data: dict = None) List[AppModel] | AppModel[source]¶
GET /resource/model_cls/[params:id]?[args:{limit,offset,page,per_page,filter_by,order_by,related,fields}]
Get resource/s
:param params :type params list :param args :type args dict :param data :type data: dict
- class zsl.resource.model_resource.ModelResourceBase(model_cls=None)[source]¶
ModelResource works only for tables with a single-column identifier (key).
- class zsl.resource.model_resource.ReadOnlyResourceMixin[source]¶
The mixin to be used to forbid the update/delete and create operations. Remember the Python’s MRO and place this mixin at the right place in the inheritance declaration.
- static create(params, args, data)[source]¶
Raises exception.
Just raises ReadOnlyResourceUpdateOperationException to indicate that this method is not available.
- Raises:
ReadOnlyResourceUpdateOperationException – when accessed
- static update(params, args, data)[source]¶
Raises exception.
Just raises ReadOnlyResourceUpdateOperationException to indicate that this method is not available.
- Raises:
ReadOnlyResourceUpdateOperationException – when accessed
- static delete(params, args, data)[source]¶
Raises exception.
Just raises ReadOnlyResourceUpdateOperationException to indicate that this method is not available.
- Raises:
ReadOnlyResourceUpdateOperationException – when accessed
- OPERATION_CREATE = 'create'¶
- OPERATION_DELETE = 'delete'¶
- OPERATION_UPDATE = 'update'¶
- static create(params, args, data)[source]¶
Raises exception.
Just raises ReadOnlyResourceUpdateOperationException to indicate that this method is not available.
- Raises:
ReadOnlyResourceUpdateOperationException – when accessed
- static delete(params, args, data)[source]¶
Raises exception.
Just raises ReadOnlyResourceUpdateOperationException to indicate that this method is not available.
- Raises:
ReadOnlyResourceUpdateOperationException – when accessed
- static update(params, args, data)[source]¶
Raises exception.
Just raises ReadOnlyResourceUpdateOperationException to indicate that this method is not available.
- Raises:
ReadOnlyResourceUpdateOperationException – when accessed
- exception zsl.resource.model_resource.ReadOnlyResourceUpdateOperationException(operation)[source]¶
-
- property operation¶
- class zsl.resource.model_resource.ResourceQueryContext(params: dict, args: list, data: dict)[source]¶
- The context of the resource query.
holds the parameters and arguments of the query,
holds the related models which should be fetched (parsed from the arguments),
holds the given filter and splits it to the given field array (parsed from the arguments)
- property args¶
Args are in the query part of the url ?related=&filter_by etc.
- property data¶
Body of the request.
Related argument - parsed as array, original argument containing the list of comma delimited models which should be fetched along with the resource.
- property params¶
Params are given as the part of the path in URL. For example GET /entities/1 will have. 1 in the params.
- zsl.resource.model_resource.dict_pick(dictionary, allowed_keys)[source]¶
Return a dictionary only with keys found in allowed_keys
- zsl.resource.model_resource.page_to_offset(params)[source]¶
Transforms page/per_page from params to limit/offset suitable for SQL.
- Parameters:
params (dict) – The dictionary containing page and per_page values will be added the values limit and offset.
zsl.resource.resource_helper¶
- zsl.resource.resource_helper.filter_from_url_arg(model_cls, query, arg, query_operator=<function and_>, arg_types=None)[source]¶
Parse filter URL argument
argand apply toqueryExample: ‘column1<=value,column2==value’ -> query.filter(Model.column1 <= value, Model.column2 == value)
- zsl.resource.resource_helper.flat_model(tree)[source]¶
Flatten the tree into a list of properties adding parents as prefixes.
- zsl.resource.resource_helper.model_tree(name, model_cls, visited=None)[source]¶
Create a simple tree of model’s properties and its related models.
It traverse trough relations, but ignore any loops.
zsl.router.method¶
zsl.router.task¶
- class zsl.router.task.PathTaskRouterStrategy(task_configuration: TaskConfiguration)[source]¶
- class zsl.router.task.TaskConfiguration[source]¶
- create_namespace(namespace: str) TaskNamespace[source]¶
- property namespaces¶
- class zsl.router.task.TaskNamespace(namespace: str, task_configuration: TaskConfiguration)[source]¶
- add_packages(packages: List[str]) TaskNamespace[source]¶
Adds an automatic resolution of urls into tasks. :param packages: The url will determine package/module and the class. :return: self
- add_routes(routes: Dict[str, Callable]) TaskNamespace[source]¶
Adds the detailed mapping of urls to tasks.
- Parameters:
routes – Mapping which defines how urls of the namespace are mapped to tasks. Each url (string) is mapped to a callable which creates the task instance.
- Returns:
self
- get_configuration() TaskConfiguration[source]¶
- class zsl.router.task.TaskRouter(config: Config, task_configuration: TaskConfiguration)[source]¶
zsl.service.service¶
- class zsl.service.service.TransactionalSupportMixin[source]¶
This mixin allows the objects to access transactional holder.
- zsl.service.service.transactional(f: Callable) Callable[source]¶
Decorator for making a function transactional.
- Parameters:
f – Function to decorate.
- Returns:
Decorated function.
from zsl.service import transactional class MyService(Service): @transactional def my_function(self): self._repository.create(self._orm, 2, 3, 4)
- zsl.service.service.tx_session(service: TransactionalSupportMixin) Generator[Session, None, None][source]¶
Context manager for transactional session.
- Parameters:
service – Service instance.
- Returns:
Session.
from zsl.service import tx_session class MyService(Service): def my_method(self): with tx_session(self) as session: self._repository.create(session, 2, 3, 4)
zsl.task.job_context¶
- class zsl.task.job_context.DelegatingJobContext(job: Job, task: object, task_callable: Callable)[source]¶
- class zsl.task.job_context.JobContext(job: Job, task: object, task_callable: Callable)[source]¶
Job Context
- classmethod get_current_context() JobContext[source]¶
- class zsl.task.job_context.JobData[source]¶
A dictionary that represents the data associated with a job.
- Variables:
Example:¶
>>> job_data = JobData(path='/path/to/job', data={'name': 'John', 'age': 30})
- class zsl.task.job_context.WebJobContext(path: str, data: dict, task: object, task_callable: Callable, request: Request)[source]¶
- zsl.task.job_context.delegating_job_context(job: Job, task: object, task_callable: Callable) Generator[DelegatingJobContext, None, None][source]¶
- zsl.task.job_context.web_task_redirect(location) HeadersDict[source]¶
zsl.task.task_data¶
- class zsl.task.task_data.TaskData(payload, app=None, payload_type=<class 'str'>)[source]¶
-
- property payload¶
zsl.task.task_decorator¶
- class zsl.task.task_decorator.CrossdomainWebTaskResponder(origin: str = None, methods: List[str] = None, allow_headers: str = None, expose_headers: str = None, max_age: int | timedelta = None, app: ServiceApplication = None, cors_config: CORSConfiguration = None)[source]¶
source: https://github.com/fengsp/flask-snippets/blob/master/decorators/http_access_control.py
- zsl.task.task_decorator.append_get_parameters(accept_only_web: bool = True) Callable[source]¶
Task decorator which appends the GET data to the task data.
- Parameters:
accept_only_web – Parameter which limits using this task only with web requests.
- zsl.task.task_decorator.crossdomain(origin=None, methods=None, allow_headers=None, expose_headers=None, max_age=None)[source]¶
- zsl.task.task_decorator.error_and_result(f)[source]¶
Format task result into json dictionary {‘data’: task return value} if no exception was raised during the task execution. If there was raised an exception during task execution, formats task result into dictionary {‘error’: exception message with traceback}.
- zsl.task.task_decorator.file_upload(f)[source]¶
Return list of werkzeug.datastructures.FileStorage objects - files to be uploaded
- zsl.task.task_decorator.forbid_web_access(f)[source]¶
Forbids running task using http request.
- Parameters:
f – Callable
:return Callable
- zsl.task.task_decorator.jsend_output(fail_exception_classes=None)[source]¶
Format task result to json output in jsend specification format. See: https://github.com/omniti-labs. Task return value must be dict or None.
@param fail_exception_classes: exceptions which will produce ‘fail’ response status.
- zsl.task.task_decorator.json_input(f)[source]¶
Expects task input data in json format and parse this data.
- zsl.task.task_decorator.json_output(f)[source]¶
Format response to json and in case of web-request set response content type to ‘application/json’.
- zsl.task.task_decorator.jsonp_wrap(callback_key='callback')[source]¶
Format response to jsonp and add a callback to JSON data - a jsonp request
- zsl.task.task_decorator.required_data(*data)[source]¶
Task decorator which checks if the given variables (indices) are stored inside the task data.
- zsl.task.task_decorator.save_to_file(destination_filename, append=False)[source]¶
Save the output value to file.
- zsl.task.task_decorator.web_error_and_result(f)[source]¶
Same as error_and_result decorator, except: If no exception was raised during task execution, ONLY IN CASE OF WEB REQUEST formats task result into json dictionary {‘data’: task return value}
- zsl.task.task_decorator.web_task(f)[source]¶
Checks if the task is called through the web interface. Task return value should be in format {‘data’: …}.
- zsl.task.task_decorator.xml_output(f)[source]¶
Set content-type for response to WEB-REQUEST to ‘text/xml’
zsl.task.task_status¶
Status codes for REST services
zsl.tasks.asl.db_test_task¶
Created on 24.12.2012
- class zsl.tasks.zsl.db_test_task.DbTestTask(db, app)[source]¶
Connects to a database and executes a simple query. The result of the query should be 6.
<emph>Input</emph> No input.
<emph>Output</emph> Returns just a number 6.
@author: Martin Babka
zsl.tasks.asl.kill_worker_task¶
Created on 22.12.2012
zsl.tasks.asl.schedule_gearman_task¶
zsl.tasks.asl.sum_task¶
Created on 22.12.2012
..moduleauthor:: Martin Babka
zsl.tasks.asl.test_task¶
Created on 22.12.2012
..moduleauthor:: Martin Babka <babka@atteq.com>
zsl.tasks.asl.version_task¶
Created on 24.12.2012
..moduleauthor:: Martin Babka
- class zsl.tasks.zsl.version_task.VersionTask(app)[source]¶
Shows the versions of ASL and the various used libraries.
zsl.tasks.asl.version_task¶
Created on 18.02.2019
..moduleauthor:: Julius Flimmel
zsl.testing¶
zsl.testing.db¶
This module allows for database unit testing. For how to use the database testing in practice, a sample, refer to Testing with database.
The module works in the following way (methods setUp, tearDown): 1. Each test runs in a single transaction. 2. This transaction is always called a rollback.
All the tests are run in a single parent transaction (setUpClass, tearDownClass): 1. In general initialization phase the session/transaction is created and it is kept during all the testing. Also the database schema is created. 2. After this the transaction is called rollback.
This means that the tests may be conducted in the in the memory database or a persistent one which is kept clean.
The module provides class TestSessionFactory - it always returns
the same session. Also one should add DbTestModule to the test
container when creating Zsl instance, see Testing with a Zsl instance.
- class zsl.testing.db.DbTestCase[source]¶
DbTestCaseis a mixin to be used when testing with a database.- setUp(session_factory: TestSessionFactory = None) None[source]¶
- tearDown(session_factory: TestSessionFactory = None) None[source]¶
- class zsl.testing.db.DbTestModule[source]¶
Module fixing the
zsl.service.service.SessionFactoryto ourTestSessionFactory.- get_session_factory() SessionFactory[source]¶
- get_test_session_factory(session_factory: SessionFactory) SessionFactory[source]¶
- class zsl.testing.db.TestSessionFactory(session_holder: SessionHolder)[source]¶
Factory always returning the single test transaction.
zsl.testing.http¶
This module allows for querying the API via HTTP.
- class zsl.testing.http.HTTPTestCase[source]¶
Extends TestCase with methods for easier testing of HTTP requests.
- assertHTTPStatus(status: int | HTTPStatus, test_value: int, msg: AnyStr) None[source]¶
Assert HTTP status
- Parameters:
status – http status
test_value – flask respond status
msg – test message
- getHTTPClient(app: ServiceApplication) FlaskClient[source]¶
- requestTask(client: FlaskClient, task: str, data: dict, headers: dict = None) Response[source]¶
Request a task using POST and convert the given data to JSON.
- Parameters:
client – The client to ZSL which will be used for the request.
task – Url which will be requested using POST method.
data – Data which will be posted and first converted to JSON.
headers – Dictionary of headers that’ll be appended the Content-Type: application/json header.
- Returns:
Flask response.
- zsl.testing.http.json_loads(str_: AnyStr) Dict[str, str][source]¶
Parse JSON from Flask response which could be in bytes in Py3.
zsl.testing.test_utils¶
Test utilities.
- class zsl.testing.test_utils.TestTaskData(payload: Any)[source]¶
Data suitable when directly calling a task.
- zsl.testing.test_utils.parent_module(module_name: str) str[source]¶
Return the parent module name for a module.
>>> parent_module('zsl.application.module') 'zsl.application'
zsl.testing.zsl¶
This module allows for unit testing with a Zsl instance. Use
ZslTestCase as a mixin to create a predefined Zsl instance to be used
while testing. Refer to unit testing section Testing with a Zsl instance
for an example.
- class zsl.testing.zsl.ZslTestCase[source]¶
- ZSL_TEST_CONFIGURATION: ZslTestConfiguration = None¶
- class zsl.testing.zsl.ZslTestConfiguration(app_name, version, container, profile, config_object)¶
- app_name¶
Alias for field number 0
- config_object¶
Alias for field number 4
- container¶
Alias for field number 2
- profile¶
Alias for field number 3
- version¶
Alias for field number 1
zsl.utils.cache_helper¶
- zsl.utils.cache_helper.cache_filtered_page(filter_param='filter', pagination_param='pagination', sorter_param='sorter', timeout='default')[source]¶
- zsl.utils.cache_helper.cache_list(key_params, timeout='default')¶
Cache a page (slice) of a list of AppModels
- zsl.utils.cache_helper.cache_model(key_params, timeout='default')[source]¶
Caching decorator for app models in task.perform
- zsl.utils.cache_helper.cache_page(key_params, timeout='default')[source]¶
Cache a page (slice) of a list of AppModels
- zsl.utils.cache_helper.create_key_for_data(prefix, data, key_params)[source]¶
From
dataparams in task create corresponding key with help ofkey_params(defined in decorator)
zsl.utils.command_dispatcher¶
- class zsl.utils.command_dispatcher.CommandDispatcher[source]¶
A simple class for command dictionary. A command is a function which can take named parameters.
- add_function(fn)¶
Add method or function to dispatcher. Can be use as a nice decorator.
- Parameters:
fn (function) – function or method
- Returns:
the same function
- Return type:
function
- bound(instance)[source]¶
Return a new dispatcher, which will switch all command functions with bounded methods of given instance matched by name. It will match only regular methods.
- Parameters:
instance (object) – object instance
- Returns:
new Dispatcher
- Return type:
zsl.utils.date_helper¶
zsl.utils.deploy.apiary_doc_generator¶
Generates the API documentation suitable for the apiary.io. It parses all the files and finds the apiary.io definitions in the documentary comments. Then outputs it to a file.
- zsl.utils.deploy.apiari_doc_generator.generate_apiary_doc(task_router)[source]¶
Generate apiary documentation.
Create a Apiary generator and add application packages to it.
- Parameters:
task_router (TaskRouter) – task router, injected
- Returns:
apiary generator
- Return type:
zsl.utils.deploy.integrator¶
- zsl.utils.deploy.integrator.integrate_to_file(what, filename, start_line, end_line)[source]¶
WARNING this is working every second run.. so serious bug Integrate content into a file withing “line marks”
zsl.utils.deploy.js_model_generator¶
- class zsl.utils.deploy.js_model_generator.ModelGenerator(module, model_prefix='', collection_prefix='', model_fn='Atteq.bb.Model', collection_fn='Atteq.bb.Collection')[source]¶
- zsl.utils.deploy.js_model_generator.generate_js_models(module: str, models: str, collection_prefix: str, model_prefix: str, model_fn: str, collection_fn: str, marker: str, integrate: bool, js_file: str) str | None[source]¶
Generate models for Backbone Javascript applications.
- Parameters:
module – module from which models are imported
models – model name, can be a tuple WineCountry/WineCountries as singular/plural
model_prefix – namespace prefix for models (app.models.)
collection_prefix – namespace prefix for collection (App.collections.)
model_fn – name of model constructor (MyApp.bb.Model)
collection_fn – name of collection constructor (MyApp.bb.Collection)
marker – marker to indicate the auto generated code
integrate – integrate to file
js_file – file to integrate
- Returns:
generated models or nothing if writing into a file
- zsl.utils.deploy.js_model_generator.parse_model_arg(models: list[str]) list[str][source]¶
Parse the model argument definition.
- zsl.utils.dict_to_object_conversion.extend_object_by_dict(target, dict_data, hints=None)[source]¶
Extends the target object with data from the provided dictionary, using hints to handle specific data types and related objects.
- Parameters:
target (object) – The object to extend with data from the dictionary.
dict_data (dict) – The dictionary containing data to extend the target object with.
hints (dict, optional) –
A dictionary containing hints on how to handle specific fields. The hints should be organized in the following way:
{ 'date_data': { 'field_name': 'date_format', ... }, 'datetime_data': { 'field_name': 'datetime_format', ... }, 'related_fields': { 'field_name': { 'cls': RelatedClass, 'hints': { 'date_data': {...}, 'datetime_data': {...}, 'related_fields': {...} } }, ... } }
The ‘date_data’ and ‘datetime_data’ keys contain dictionaries with field names as keys and date or datetime formats as values. This helps to correctly parse date and datetime strings in the provided dictionary. The ‘related_fields’ key contains a dictionary with field names as keys and dictionaries as values. Each dictionary should have a ‘cls’ key, which should be a related class, and an optional ‘hints’ key containing another hints dictionary, used for nested related objects.
- Returns:
None. The function modifies the target object in-place.
zsl.utils.date_helper¶
- zsl.utils.documentation.documentation_link(chapter: str) str[source]¶
Creates a link to the documentation.
This method is useful for showing a link to the ZSL documentation in case of any misconfiguration, etc. :param chapter: Chapter name in to which the link points. Use underscores instead of spaces. :return: The absolute link to the documentation.
zsl.utils.email_helper¶
- zsl.utils.email_helper.send_email(sender, receivers, subject, text=None, html=None, charset='utf-8', config=None)[source]¶
Sends an email.
- Parameters:
sender – Sender as string or None for default got from config.
receivers – String or array of recipients.
subject – Subject.
text – Plain text message.
html – Html message.
charset – Charset.
config – Current configuration
zsl.utils.file_helper¶
- zsl.utils.file_helper.makedirs(path)[source]¶
Behaves like mkdir -p <path>. Without failure if the path exists.
- zsl.utils.http.get_http_status_code_value(status_code: int | HTTPStatus) int[source]¶
Py2/3 status code.
zsl.utils.import_helper¶
- zsl.utils.import_helper.fetch_class(full_class_name)[source]¶
Fetches the given class.
- Parameters:
full_class_name (string) – Name of the class to be fetched.
zsl.utils.injection_helper¶
- zsl.utils.injection_helper.inject(**bindings)[source]¶
Decorator for injecting parameters for ASL objects.
zsl.utils.model_helper¶
Helper module for working with models.
- zsl.utils.model_helper.update_model(raw_model, app_model, forbidden_keys=None, inverse=False)[source]¶
Updates the raw_model according to the values in the app_model.
- Parameters:
raw_model – Raw model which gets updated.
app_model – App model holding the data.
forbidden_keys (list) – Data/attributes which will not be updated.
inverse – If the value is True all app_model attributes which are contained in the raw_model are updated. If the value is False all raw_model properties which are in the app_model will be updated.
zsl.utils.nginx_push_helper¶
Helper for nginx push stream module https://github.com/wandenberg/nginx-push-stream-module
- class zsl.utils.nginx_push_helper.NginxPusher(server_path, channel_prefix=None)[source]¶
zsl.utils.params_helper¶
Helpers for parameter handling
- zsl.utils.params_helper.required_params(data, *r_params)[source]¶
Check if given parameters are in the given dict, if not raise an exception.
- Parameters:
data (dict) – data to check
r_params – required parameters
- Raises:
RequestException – if params not in data
- zsl.utils.params_helper.safe_args(fn, args)[source]¶
Check if
argsas a dictionary has the required parameters offnfunction and filter any waste parameters sofncan be safely called with them.
zsl.utils.php_helper¶
Module with functions to help with dealing with code written in PHP.
zsl.utils.redis_helper¶
Helper module for working with redis.
- class zsl.utils.redis_helper.Keymaker(keys, prefix=None, config=None)[source]¶
Keymaker is a class to generate an object to generate Redis keys.
- Example:
>>> article_keys = Keymaker(prefix='articles', keys={'full_article': 'full', 'short_article': 'short'}) >>> article_keys.full_article('today', 'ID214') '$PROJECT_PREFIX:articles:full:today:ID214'
zsl.utils.reflection_helper¶
Helper module for OOP operations.
- zsl.utils.reflection_helper.extend(instance, new_class)[source]¶
Adds new_class to the ancestors of instance.
- Parameters:
instance – Instance that will have a new ancestor.
new_class – Ancestor.
Helper module for working with request.
- zsl.utils.request_helper.args_to_dict(args)[source]¶
Converts request arguments to a simple dictionary. If an argument appears multiple times, the resulting dictionary will contain list of all of its values.
- Parameters:
args (request.args) – flask’s request args object
- Returns:
a simple dict from args
- Return type:
zsl.utils.resource_helper¶
Helper module for resource management.
- exception zsl.utils.resource_helper.MethodNotImplementedException[source]¶
Exception raised on missing method
- zsl.utils.resource_helper.create_model_resource(resource_map, name, app=None)[source]¶
Create a model resource from a dict
resource_map{‘resource name’: (‘model package’, ‘model class’)}
- zsl.utils.resource_helper.get_method(resource, method)[source]¶
Test and return a method by name on resource.
- Parameters:
- Returns:
bounded method
- Raises:
MethodNotImplementedException – when method not found
- zsl.utils.resource_helper.get_resource_task(resource_path: str, config: Config = None) Callable[[str, Dict, Dict], List[AppModel] | AppModel | ResourceResult][source]¶
Search and return a bounded method for given path.
zsl.utils.rss¶
Helper function for handling rss.
- zsl.utils.rss.complex_el_from_dict(parent, data, key)[source]¶
Create element from a dict definition and add it to
parent.- Parameters:
parent (Element) – parent element
data – dictionary with elements definitions, it can be a simple {element_name: ‘element_value’} or complex {element_name: {_attr: {name: value, name1: value1}, _text: ‘text’}}
key – element name and key in
data
- Returns:
created element
- zsl.utils.rss.element_from_dict(parent, data, element)[source]¶
Create
elementtoparentand sets its value to data[element], which will be removed from thedata.
- zsl.utils.rss.rss_create(channel, articles)[source]¶
Create RSS xml feed.
- Parameters:
- Returns:
root element
- Return type:
ElementTree.Element
zsl.utils.security_helper¶
Helper module with function dealing with security.
- zsl.utils.security_helper.compute_token(random_token, config)[source]¶
Compute a hash of the given token with a preconfigured secret.
- zsl.utils.security_helper.generate_security_data()[source]¶
Generate security token - a random token and its hashed version salted with a secret.
- zsl.utils.security_helper.verify_security_data(security)[source]¶
Verify an untrusted security token.
- zsl.utils.security_helper.wrap_plain_data_as_secure(data)[source]¶
Wrap task data with security token.
- Parameters:
data – data to be wrapped
- Returns:
wrapped data with security token
- Return type:
zsl.utils.string_helper¶
- zsl.utils.string_helper.addslashes(s, escaped_chars=None)[source]¶
Add slashes for given characters. Default is for
\and'.- Parameters:
s – string
escaped_chars – list of characters to prefix with a slash
\
- Returns:
string with slashed characters
- Return type:
- Example:
>>> addslashes("'") "\'"
- zsl.utils.string_helper.camelcase_to_underscore(name)[source]¶
Transform string from camelCase to underscore_string.
- zsl.utils.string_helper.et_node_to_string(et_node, default='')[source]¶
Simple method to get stripped text from node or
defaultstring if None is given.- Parameters:
et_node (xml.etree.ElementTree.Element, None) – Element or None
default (str) – string returned if None is given, default
''
- Returns:
text from node or default
- Return type:
- zsl.utils.string_helper.generate_random_string(size=6, chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')[source]¶
Generate random string.
- zsl.utils.string_helper.join_list()[source]¶
Concatenates the upper-cased values using the given delimiter if the given values variable is a list. Otherwise it is just returned. :param values: List of strings or string . :param delimiter: The delimiter used to join the values. :return: The concatenation or identity.
- zsl.utils.string_helper.strip_html_tags_unicode(s)[source]¶
Remove all html tags from string.
Deprecated since version Use:
strip_html_tags()instead.
- zsl.utils.string_helper.underscore_to_camelcase(value, first_upper=True)[source]¶
Transform string from underscore_string to camelCase.
- Parameters:
value (str) – string with underscores
first_upper – the result will have its first character in upper case
- Returns:
string in CamelCase or camelCase according to the first_upper
- Return type:
- Example:
>>> underscore_to_camelcase('camel_case') 'CamelCase' >>> underscore_to_camelcase('camel_case', False) 'camelCase'
- zsl.utils.string_helper.xstr(s)[source]¶
If
sis None return empty string.- Parameters:
s – string
- Returns:
s or an empty string if s is None
- Return type:
- Example:
>>> xstr(None) ''
- zsl.utils.string_helper.xunicode(s)[source]¶
If
sis None return empty stringDeprecated since version Use:
xstr()instead.- Parameters:
s – string
- Returns:
s or an empty string if s in None
- Return type:
zsl.utils.task_helper¶
Helper module for task management.
- zsl.utils.task_helper.get_callable(task)[source]¶
Return the perform method on given task.
- Parameters:
task – task instance
- Returns:
bound perform method
- zsl.utils.task_helper.run_task(task_cls, task_data)[source]¶
Instantiate and run the perform method od given task data.
- Parameters:
task_cls – task class
task_data (TaskData) – task data
- Returns:
task’s result
- zsl.utils.task_helper.run_task_json(task_cls, task_data)[source]¶
Instantiate and run the perform method od given task data.
- Parameters:
task_cls – task class
task_data (TaskData) – task data
- Returns:
task’s result
zsl.utils.testing¶
- zsl.utils.testing.load_and_run_tests(config)[source]¶
Load application test package and run it using testing util.
- Parameters:
config (Config) – application configuration, injected
zsl.utils.type_helper¶
Helper module for handling types.
- zsl.utils.type_helper.not_empty_list(l)[source]¶
Check if given value is a non-empty list.
- Parameters:
l – value
- Returns:
True on non-empty list, False otherwise
- Return type:
- Example:
>>> not_empty_list([1, 2, 3]) True >>> not_empty_list(None) False
zsl.utils.url_helper¶
Helper module for URL handling.
- zsl.utils.url_helper.slugify(value, allow_unicode=False)[source]¶
Normalizes string, converts to lowercase, removes non-alpha characters, and converts spaces to hyphens.
- zsl.utils.warnings.deprecated(func)[source]¶
This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emitted when the function is used.
zsl.utils.xml_helper – xml helpers¶
Helper functions for working with XML and ElementTree.
- zsl.utils.xml_helper.attrib_to_dict(element, *args, **kwargs)[source]¶
For an ElementTree
elementextract specified attributes. If an attribute does not exists, its value will beNone.attrib_to_dict(element, ‘attr_a’, ‘attr_b’) -> {‘attr_a’: ‘value’, ‘attr_a’: ‘value’}
Mapping between xml attributes and dictionary keys is done with kwargs. attrib_to_dict(element, my_new_name = ‘xml_atribute_name’, ..)
- zsl.utils.xml_helper.create_el(name, text=None, attrib=None)[source]¶
Create element with given attributes and set element.text property to given text value (if text is not None)
- zsl.utils.xml_helper.element_to_int(element, attribute=None)[source]¶
Convert
elementobject to int. If attribute is not given, convertelement.text.
- zsl.utils.xml_helper.get_xml_root(xml_path)[source]¶
Load and parse an xml by given xml_path and return its root.
- Parameters:
xml_path (str) – URL to a xml file
- Returns:
xml root
- zsl.utils.xml_helper.msg_err_missing_attributes(tag, *attributes)[source]¶
Format message for missing attributes.
- Parameters:
tag – tag name
attributes – list of attributes
- Returns:
message
- Return type:
- zsl.utils.xml_helper.msg_err_missing_children(tag, *children)[source]¶
Format message for missing children.
- Parameters:
tag – tag name
children – list of children
- Returns:
message
- Return type:
- zsl.utils.xml_helper.required_attributes(element, *attributes)[source]¶
Check element for required attributes. Raise
NotValidXmlExceptionon error.- Parameters:
element – ElementTree element
attributes – list of attributes names to check
- Raises:
NotValidXmlException – if some argument is missing
- zsl.utils.xml_helper.required_elements(element, *children)[source]¶
Check element (
xml.etree.ElementTree.Element) for required children, defined as XPath. RaiseNotValidXmlExceptionon error.- Parameters:
element – ElementTree element
children – list of XPaths to check
- Raises:
NotValidXmlException – if some child is missing
- zsl.utils.xml_helper.required_items(element, children, attributes)[source]¶
Check an xml element to include given attributes and children.
- Parameters:
element – ElementTree element
children – list of XPaths to check
attributes – list of attributes names to check
- Raises:
NotValidXmlException – if some argument is missing
NotValidXmlException – if some child is missing
zsl.utils.xml_to_json – xml helpers¶
Helper functions for simpler parsing xml into object with schemas.
- exception zsl.utils.xml_to_json.NotCompleteXmlException[source]¶
Exception raised during parsing an invalid XML.
- exception zsl.utils.xml_to_json.XmlToJsonException[source]¶
Exception raised during converting xml to json.
- zsl.utils.xml_to_json.xml_to_json(element, definition, required=False)[source]¶
Convert XML (ElementTree) to dictionary from a definition schema.
Definition schema can be a simple string - XPath or @attribute for direct extraction or a complex one described by
dictionary
{key: 'xpath or @attribute', second: 'complex definition'}required parameters can be marked with * at the endlist
[xpath, [definition]]- create a list of all elements found by xpath, parse the parts with given definition if provided as second argumentCallable - parse the element by given function, can be handy as a part of complex definition