Skip to content
This repository was archived by the owner on Sep 7, 2018. It is now read-only.

Documentation

Daniel Däschle edited this page Feb 22, 2018 · 26 revisions

Version: alpha-0.2.2

Module swapy

app() -> function

Returns the built app


config(cfg) -> None

A short function for all other setting functions like: include, ssl, error, favicon etc. It also handles the entry 'environment' key which can contains 'production' and 'development' or just global variables. The variables can used by extensions. 'production' and 'development' are reserved variables in this case which you can't use if you don't want to separate that.

Parameter Type Desctiption
cfg dict The config dict

environment() -> None

Sets the given data as environment

The keys "production" and "development" are reserved. All keys in "production" are used if the app runs without the debug=True. I the other case "development" is used.

Example:

'production': {
    'host': '0.0.0.0'
},
'development': {
    'host': '127.0.0.1'
}

If the app starts without debug mode the "host" key with the value "0.0.0.0" is the priority value.

Parameter Type Desctiption
data dict

error(f) -> None

Registers a function as error handler.

Parameter Type Desctiption
f function Function should receive an Exception object as parameter

favicon(path) -> None

Registers a route to the favicon.

Parameter Type Desctiption
path str Path to the file

file(path, name=None) -> FileWrapper Ref

Returns a file response.

Parameter Type Desctiption
path str Path to the file
name (optional) str Name of the file which will be returned. If it is None the name is like the real file name.

get_env(key) -> object

Returns the value of the give key in environment variables.

Parameter Type Desctiption
key str

include(module, prefix='') -> None

Includes a source module into the target module.

Parameter Type Desctiption
module module The source module
prefix (optional) str Routes from the source module will get the prefix in front of their route

not_found(f) -> None

Registers a function as 404 error handler.

Parameter Type Desctiption
f function Functionshould receive an Exception object as parameter

on(url='/', methods=('GET', 'POST', 'PUT', 'DELETE')) -> function

Route registerer for all http methods.

Parameter Type Desctiption
url (optional) str
methods (optional) list | tuple HTTP method

on_delete(url='/') -> function

Route registerer for DELETE http method.

Parameter Type Desctiption
url (optional) str

on_get(url='/') -> function

Route registerer for GET http method.

Parameter Type Desctiption
url (optional) str

on_post(url='/') -> function

Route registerer for POST http method.

Parameter Type Desctiption
url (optional) str

on_put(url='/') -> function

Route registerer for PUT http method.

Parameter Type Desctiption
url (optional) str

redirect(location, code=301) -> Response Ref

Returns a redirect response.

Parameter Type Desctiption
location str Url where the user should be redirect
code int HTTP status code which the server returns to the client. It should be any 3xx code. See more at Wikipedia - Http Status Codes.

render(file_path, **kwargs) -> str

Returns a rendered HTML file.

Parameter Type Desctiption
file_apth str Path to file including filename and extension
kwargs object Additional arguments which will forward to the template

routes() -> list

Returns a list of all registered routes


run(host='127.0.0.1', port=5000, debug=False, module_name=None) -> None

Runs the app.

Parameter Type Desctiption
host (optional) str IP Address where the server serves. '0.0.0.0' for public address.
port (optional) int Additional arguments which will forward to the template
debug (optional) bool Enables debug output and hot reload
module_name (optional) str Starts the app from the specific module if given

set_env(key, value) -> None

Sets a value for a key in the global environment

Parameter Type Desctiption
key str
value object

shared(directory) -> None

Registers a directory as shared.

Parameter Type Desctiption
directory str Path to the directory

ssl(host='127.0.0.1', path=None) -> None

Registers SSL.

Parameter Type Desctiption
host str IP address of the server
path str Path to the certificates. If it is None swapy generates certificates for development, but then you should have python OpenSSL installed.

use(*middlewares_) -> None

Registers middlewares for global use.

Parameter Type Desctiption
middlewares_ function[] Arguments of middlewares (decorators)

Module swapy.wrappers

class Request(BaseRequest) Ref

Request class which inherits from werkzeug's request class. It adds the json function.

Attribute Type Desctiption
session Session
state State
url_args dict
json dict Returns dict from json string if available
secure_cookie SecureCookie Returns the secure cookie object from werkzeug

get_secure_cookie() -> SecureCookie Ref

Returns the secure cookie object from werkzeug.


class Response

Example: res = Response('content', 200, {'my_header': 'value'})

Attribute Type Desctiption
content str | FileWrapper
code int
headers dict
cookies dict Setter and getter property for cookies

__init__(content, code=200, headers=None) -> None

Parameter Type Desctiption
content str
code int (optional)
headers dict (optional)

set_cookies(data) -> None

Adds cookies to the current

Parameter Type Desctiption
data dict

get_cookies() -> dict

Returns all set cookies.


response_from(args) -> Response Ref

Casts a result from a route function into a response object

Parameter Type Desctiption
args list, tuple | Response

Module swapy.middelwares

Attribute Type Desctiption
JsonException function Alias for json_exception
DefaultException function Alias for default_exception
JsonMiddleware function Alias for json_middleware
HtmlMiddleware function Alias for html_middleware
CorsMiddleware function Alias for cors_middleware
ExpectKeysMiddleware function Alias for expect_keys_middleware

json_exception(error) -> str

Exception middleware which returns the error as JSON string. -> {"message": error, "status_code": code}

Parameter Type Desctiption
error Exception The exception object

default_exception(error) -> str

Default exception middleware.

Parameter Type Desctiption
error Exception The exception object

json_middleware(f) -> function

Returns every output from an route which has the JSON middleware to a JSON string.

Parameter Type Desctiption
f function

html_middleware(f) -> function

Appends a 'text/html' Content-Type header to each response from a route.

Parameter Type Desctiption
f function

cors_middleware(f) -> function

Appends CORS headers to each response from a route.

Parameter Type Desctiption
f function

expect_keys_middleware(f) -> function

Returns a 400 error to the client if the route function tries to get a key from an object and cause a KeyError.

Parameter Type Desctiption
f function

Module swapy.testing

client(app) -> Client Ref

Returns the werkzeug Client class with the Response class

Parameter Type Desctiption
app function The application function

Module swapy._utils

class Environment

Environment class. I need it that it can be referenced.

Attribute Type Desctiption
data dict
development dict
production dict
runtime_data dict Returns the whole data

__init__(_state, data=None, development=None, production=None) -> None

Parameter Type Desctiption
_state State
data dict
development (optional) dict
production (optional) dict

__repr__() -> dict

Returns the whole data.


__setitem__(key, value) -> object

Sets a value for the given key in this environment.

Parameter Type Desctiption
key str
value object

get(key) -> object

Returns the value by the given key.

Example: environment['my_item']

Parameter Type Desctiption
key str

parse(data) -> None

Parses the environment dict data into this class.

Parameter Type Desctiption
data dict

set(key, value, runtime=None) -> None

Sets a value for the given key in this environment.

Parameter Type Desctiption
key str
value object
runtime str (optional) Should be 'development' or 'production'

class State

State for every module.

Attribute Type Desctiption
url_map Map
middlewares list
on_error function
on_not_found function
routes dict Contains all registered routes.
Example: {'uuid': {'function': function, 'on_error': function, 'url': str}}
ssl str | tuple(str, str)
shared str
environment Environment
debug bool

build_app(module) -> function

Returns the built app.

Parameter Type Desctiption
module str Name of the module

caller() -> str

Returns the name of the module which calls swapy.


caller_frame() -> Frame Ref

Returns the frame of the module which calls swapy.


environment(module, data) -> None

Sets the environment data to the given module.

Parameter Type Desctiption
module str Name of the module
data dict

error(module, f) -> None

Sets the given function to the given module as error handler.

Parameter Type Desctiption
module str Name of the module
f function The function which will be set for the module as error handler

error_handler(e, module) -> None

Handles the errors at requests.

Parameter Type Desctiption
e Exception
module str Name of the module

favicon(module, path) -> None

Registers route for favicon.

Parameter Type Desctiption
module str Name of the module
path str Path to favicon file

find_route(name) -> Rule | None Ref

Returns the route by name (url).

Parameter Type Desctiption
name str URL of the route

include(module, target, prefix='') -> None

Includes all functions from source module into the target module.

Parameter Type Desctiption
module str Name of the source module
target str Name of the target module
prefix str (optional) Route prefix for functions of the source module

init(module) -> None

Adds a state object for every module into the modules list.

Parameter Type Desctiption
module str The name of the module which should be initialized

not_found(module, f) -> None

Registers "not found" function for the 404 error.

Parameter Type Desctiption
module str Name of the module
f function Function which will be registered

not_found_handler(e, module) -> Response | Exception

Error handler if the route is not found and returns a werkzeug Response object or an exception object.

Parameter Type Desctiption
e Exception
module str Name of the module

register_route(module, url='/', methods=('GET', 'POST', 'PUT', 'DELETE')) -> function

Adds a route to the module which calls this and returns the decorator which registers a function.

Parameter Type Desctiption
module str name of the module
url (optional) str
methods (optional) list | tuple HTTP methods as strings

shared(frame, directory) -> None

Adds a directory as shared for the given module.

Parameter Type Desctiption
frame Frame Frame of the module
directory str Absolute path or relative path

ssl(module, host='127.0.0.1', path=None) -> None

Adds SSL support for development.

Parameter Type Desctiption
module str Name of the module
host (optional) str Domain or IP of the server
path (optional) str Path to cert files (without .crt and .key ending). If empty you have to install python OpenSSL lib.

state(module) -> State Ref

Returns state of the given module.

Parameter Type Desctiption
module str Name of the module

use(*middlewares_) -> None

Registers middlewares.

Parameter Type Desctiption
module str Name of the module
middlewares_ list List of decorators / middlewares (functions)
Clone this wiki locally