producti_gestio.filters package¶
producti_gestio.filters¶
That’s the filters part of the module. It is used to filter the requests.
producti_gestio.filters.filters module¶
producti_gestio.filters.filters¶
All the filters are defined here using the Filters and the Filter class.
You can use filters by passing them as
parameters in producti_gestio.server.server.Server.on_request()
:
from producti_gestio import Server, Filter
my_server = Server(allow_get=True) # Create a server instance
@my_server.on_request(Filter.get) # It will filter the GET requests
def my_function(**kwargs):
return {
'response_code': 200, # The response code
'response': { # The response as a dictionary, it will be encoded in JSON
'ok': True
}
}
my_server.start() # Start the server using threads
while True:
pass
You can also use operators (&
, |
, and
, or
, ~
)!
You can check our repository for other examples.
-
class
producti_gestio.filters.filters.
Filters
¶ Bases:
object
The Filters class is used to define all Filters that could be used.
You can also create your own filter, see
producti_gestio.filters.filters.build()
.-
any
= <producti_gestio.filters.filters.Any object>¶ It defines the Any Filters. It will get all the requests.
-
get
= <producti_gestio.filters.filters.Get object>¶ It defines the Filter for GET requests.
It checks if the
request-type
isGET
.
-
static
path
(given_path: str) → producti_gestio.filters.filter.Filter¶ It filters requests using a path.
The
Path
always starts with/
!Parameters: - given_path (str) – The path you’d like to check
- given_path – str:
Returns: A Filter-like object
Return type:
-
post
= <producti_gestio.filters.filters.Post object>¶ It defines the Filter for POST requests.
It checks if the
request-type
isPOST
.
-
static
regex
(pattern: str, flags: int = 0) → producti_gestio.filters.filter.Filter¶ It filters requests with a path that match a given RegEx pattern.
The
Path
always starts with/
!You can also give a flag (using
re
), here an example:import re from producti_gestio import Filters, Server my_server = Server(allow_get=True) # Create a server instance @my_server.on_request(Filters.regex('^/print', re.IGNORECASE)) # It filters all requests that start with print, case-insensitive def my_function(**kwargs): return { 'response_code': 200, # The response code 'response': { 'ok': True, 'message': repr(kwargs['parameters']) } } my_server.start() # Start the server using threads while True: pass
Parameters: - pattern (str) – The RegEx pattern
- flags (int) – The RegEx flags
- pattern – str:
- flags – int: (Default value = 0)
Returns: A Filter-like object
Return type:
-
-
producti_gestio.filters.filters.
build
(name: str, func: callable, **kwargs) → producti_gestio.filters.filter.Filter¶ It builds a Filter class using a dictionary.
It is used to create a Filter just giving a name and a function using
type()
.The provided function must have these parameters:
- _ - The object that is passed (useful when using a compiled function, see
producti_gestio.filters.filters.Filters.regex()
) - kwargs - A dictionary of all useful informations (the same is passed to the handler function)
Here an example:
from producti_gestio.filters.filters import build from producti_gestio import Server my_filter = build('My filter', lambda _, kwargs: bool(kwargs['path'] == '/my_path')) # If the path is '/my_path' my_server = Server(allow_get=True) @my_server.on_request(my_filter) # It uses our own filter def my_function(**kwargs): return { 'response_code': 200, # The response code 'response': { 'ok': True, 'path': kwargs['path'] } } my_server.start() # Start the server using threads while True: pass
Parameters: - name (str) – The name of the Filter.
- func (callable) – A callable function that checks the request
- kwargs – A dictionary for other methods
- name – str:
- func – callable:
- **kwargs –
Returns: A Filter-like object
Return type: - _ - The object that is passed (useful when using a compiled function, see
producti_gestio.filters.filter module¶
producti_gestio.filters.filter¶
The Filter class is defined here, it is used to check the request using the filters.
When producti_gestio.filters.filters.build()
is triggered, a new Filter
object
is created.
This object is used to create the special methods (or dunder methods because of their __
) for
Python operators (__invert__
for ~
, __and__
for and
and &
, __or__
for or
and |
).
It could be useful when using Filters
.
Here an example:
from producti_gestio import Server, Filters
my_server = Server(allow_get=True) # Create a server instance
@my_server.on_request(Filters.get & Filters.path('/something')) # It will filter the GET requests
def my_function(**kwargs):
return {
'response_code': 200, # The response code
'response': { # The response as a dictionary, it will be encoded in JSON
'ok': True
}
}
my_server.start() # Start the server using threads
while True:
pass
-
class
producti_gestio.filters.filter.
AndFilter
(base: object, other: object)¶ Bases:
producti_gestio.filters.filter.Filter
The AndFilter is used to check two Filters and return their result
-
__call__
(args: dict) → bool¶ It returns the result of two Filters.
Parameters: args (dict) – The dictionary that will be used by filters Returns: True if all the Filters returned True, False if not Return type: bool
-
__init__
(base: object, other: object)¶ It initializes the object.
Parameters: - base (object) – One of the Filters that you’d like to check
- other (object) – The other Filter
-
-
class
producti_gestio.filters.filter.
Filter
¶ Bases:
object
The Filter class is used to check the given requests using Filters.
-
__and__
(other: object) → object¶ It returns a AndFilter object.
Parameters: other (Filter) – The other filter you’d like to check Returns: The AndFilter object Return type: AndFilter
-
__call__
(args: dict)¶ It raises a NotImplementedError.
Parameters: args (dict) – The dictionary that will be used by filters Raises: NotImplementedError
-
__invert__
() → object¶ It returns a InvertFilter object.
Returns: The InvertFilter object Return type: InvertFilter
-
-
class
producti_gestio.filters.filter.
InvertFilter
(base: object)¶ Bases:
producti_gestio.filters.filter.Filter
The InvertFilter is used to return an inverted filter
-
__call__
(args: dict) → bool¶ It returns the inverted result of a Filter.
Parameters: args (dict) – The dictionary that will be used by filters Returns: The inverted result of the Filter Return type: bool
-
__init__
(base: object)¶ It initializes the object.
Parameters: base (object) – The Filter that you’d like to check
-
-
class
producti_gestio.filters.filter.
OrFilter
(base: object, other: object)¶ Bases:
producti_gestio.filters.filter.Filter
The OrFilter is used to check two Filters and see if one of them is True.
-
__call__
(args: dict) → bool¶ It returns the result of two Filters.
Parameters: args (dict) – The dictionary that will be used by filters Returns: True if one of the Filters returned True, False if not Return type: bool
-
__init__
(base: object, other: object)¶ It initializes the object.
Parameters: - base (object) – One of the Filters that you’d like to check
- other (object) – The other Filter
-