Update
This commit is contained in:
@@ -3,8 +3,8 @@
|
||||
You can adapt this file completely to your liking, but it should at least
|
||||
contain the root `toctree` directive.
|
||||
|
||||
Документация маршрутизатора
|
||||
===========================
|
||||
Маршрутизатор
|
||||
=============
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
|
||||
+5
-4
@@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "router"
|
||||
version ="0.2"
|
||||
version ="0.3"
|
||||
authors = [
|
||||
{ name="RemiZOffAlex", email="remizoffalex@gmail.com" },
|
||||
]
|
||||
@@ -9,9 +9,10 @@ requires-python = ">=3.10"
|
||||
classifiers = [
|
||||
"Programming Language :: Python",
|
||||
"Programming Language :: Python :: 3",
|
||||
"Programming Language :: Python :: 3.10",
|
||||
"Programming Language :: Python :: 3.11",
|
||||
"Programming Language :: Python :: 3.12"
|
||||
"Programming Language :: Python :: 3.12",
|
||||
"Programming Language :: Python :: 3.13",
|
||||
"Programming Language :: Python :: 3.14",
|
||||
"Programming Language :: Python :: 3.15"
|
||||
]
|
||||
keywords = ["broker", "router"]
|
||||
dependencies = []
|
||||
|
||||
+27
-24
@@ -4,64 +4,67 @@ __email__ = 'remizoffalex@mail.ru'
|
||||
|
||||
import logging
|
||||
|
||||
from collections.abc import Callable
|
||||
|
||||
from .route import Route
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Broker:
|
||||
def __init__(self, debug: bool = False):
|
||||
self.routes = {}
|
||||
self.__routes = {}
|
||||
self.debug = debug
|
||||
|
||||
def __call__(self, query):
|
||||
"""Вызов метода
|
||||
"""
|
||||
for key in self.routes:
|
||||
_route = self.routes[key]
|
||||
for key in self.__routes:
|
||||
_route = self.__routes[key]
|
||||
if self.debug:
|
||||
log.debug('{key}: {route}'.format(
|
||||
key=key,
|
||||
route=_route
|
||||
))
|
||||
print(key)
|
||||
print(_route)
|
||||
print(_route.comparator)
|
||||
print(query)
|
||||
output = _route.comparator(query)
|
||||
if output:
|
||||
return (_route.endpoint, output)
|
||||
return _route.method
|
||||
|
||||
def __getitem__(self, key):
|
||||
method = self.routes[key]
|
||||
return method.function
|
||||
method = self.__routes[key]
|
||||
return method.method
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self.routes)
|
||||
return iter(self.__routes)
|
||||
|
||||
def __len__(self):
|
||||
return len(self.routes)
|
||||
return len(self.__routes)
|
||||
|
||||
def __setitem__(self, key, function, pre=None):
|
||||
method = Method(function=function, pre=pre)
|
||||
self.routes[key] = method
|
||||
def __setitem__(self, key, method):
|
||||
self.__routes[key] = method
|
||||
|
||||
def __delitem__(self, key):
|
||||
del self.routes[key]
|
||||
del self.__routes[key]
|
||||
|
||||
def __repr__(self):
|
||||
return repr(self.routes)
|
||||
return repr(self.__routes)
|
||||
|
||||
def route(self, name: str, comparator: Callable, *args):
|
||||
def register(self, name: str, comparator, method):
|
||||
"""регистратор
|
||||
"""
|
||||
route = Route(
|
||||
comparator=comparator,
|
||||
method=method
|
||||
)
|
||||
self.__routes[name] = route
|
||||
|
||||
def route(self, name: str, comparator, *args):
|
||||
"""Декоратор
|
||||
"""
|
||||
def wrap(function):
|
||||
def wrap(method):
|
||||
_route = Route(
|
||||
comparator=comparator,
|
||||
endpoint=function
|
||||
method=method
|
||||
)
|
||||
self.routes[name] = _route
|
||||
return function
|
||||
self.__routes[name] = _route
|
||||
return method
|
||||
return wrap
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
__author__ = 'RemiZOffAlex'
|
||||
__email__ = 'remizoffalex@mail.ru'
|
||||
@@ -1,68 +0,0 @@
|
||||
__author__ = 'RemiZOffAlex'
|
||||
__email__ = 'remizoffalex@mail.ru'
|
||||
|
||||
import re
|
||||
import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Node:
|
||||
__params__ = {}
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
log.debug('{}.__init__'.format(self.__class__.__name__))
|
||||
for item in self.__params__:
|
||||
setattr(self, item, kwargs[item])
|
||||
|
||||
def __call__(self, state):
|
||||
print(self.__class__.__name__)
|
||||
raise ValueError('Метод не реализован')
|
||||
|
||||
def __repr__(self):
|
||||
log.debug('{}.__repr__'.format(self.__class__.__name__))
|
||||
result = self.__class__.__name__ + '(' + ')'
|
||||
params = []
|
||||
for item in self.__params__:
|
||||
param = getattr(self, item)
|
||||
params.append('{} = {}'.format(item, getattr(self, item)))
|
||||
result = self.__class__.__name__ + '(' + ', '.join(params) + ')'
|
||||
|
||||
log.debug('__repr__')
|
||||
return result
|
||||
|
||||
|
||||
class Regex(Node):
|
||||
__params__ = {
|
||||
'key': '',
|
||||
'value': ''
|
||||
}
|
||||
|
||||
def __init__(self, key, value):
|
||||
self.key = key
|
||||
self.value = re.compile(value)
|
||||
|
||||
def __json__(self):
|
||||
return {
|
||||
'operator': 'regex',
|
||||
'key': self.key,
|
||||
'value': self.value.pattern
|
||||
}
|
||||
|
||||
def __call__(self, query):
|
||||
print('Regex.__call__')
|
||||
if isinstance(query, dict):
|
||||
result = self.value.match(query[self.key])
|
||||
else:
|
||||
if '.' in self.key:
|
||||
keys = self.key.split('.')
|
||||
value = query
|
||||
for key in keys:
|
||||
value = getattr(value, key)
|
||||
else:
|
||||
value = getattr(query, self.key)
|
||||
print(value)
|
||||
print(type(value).__name__)
|
||||
result = self.value.match(value)
|
||||
print(result)
|
||||
return result
|
||||
@@ -1,70 +0,0 @@
|
||||
__author__ = 'RemiZOffAlex'
|
||||
__email__ = 'remizoffalex@mail.ru'
|
||||
|
||||
import re
|
||||
import logging
|
||||
|
||||
from .common import Node
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class And(Node):
|
||||
__params__ = {
|
||||
'rules': ''
|
||||
}
|
||||
|
||||
def __json__(self):
|
||||
result = []
|
||||
for item in self.rules:
|
||||
result.append(item.__json__())
|
||||
return {
|
||||
'operator': 'and',
|
||||
'rules': result
|
||||
}
|
||||
|
||||
def __call__(self, query):
|
||||
log.debug('And.__call__')
|
||||
return all(map(lambda x: x(query), self.rules))
|
||||
|
||||
|
||||
class Equal:
|
||||
__params__ = {
|
||||
'key': '',
|
||||
'value': ''
|
||||
}
|
||||
def __init__(self, key, value):
|
||||
self.key = key
|
||||
self.value = value
|
||||
|
||||
def __json__(self):
|
||||
return {
|
||||
'operator': 'equal',
|
||||
'key': self.key,
|
||||
'value': self.value
|
||||
}
|
||||
|
||||
def __call__(self, query):
|
||||
log.debug('Equal.__call__')
|
||||
return query[self.key] == self.value
|
||||
|
||||
|
||||
class Or:
|
||||
__params__ = {
|
||||
'rules': ''
|
||||
}
|
||||
def __init__(self, rules):
|
||||
self.rules = rules
|
||||
|
||||
def __json__(self):
|
||||
result = []
|
||||
for item in self.rules:
|
||||
result.append(item.__json__())
|
||||
return {
|
||||
'operator': 'or',
|
||||
'rules': result
|
||||
}
|
||||
|
||||
def __call__(self, query):
|
||||
print('Or.__call__')
|
||||
return any(map(lambda x: x(query), self.rules))
|
||||
@@ -1,10 +0,0 @@
|
||||
__author__ = 'RemiZOffAlex'
|
||||
__email__ = 'remizoffalex@mail.ru'
|
||||
|
||||
|
||||
class Endpoint:
|
||||
def __init__(self, function):
|
||||
self.function = function
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
result = self.function()
|
||||
+5
-6
@@ -3,17 +3,16 @@ __email__ = 'remizoffalex@mail.ru'
|
||||
|
||||
|
||||
class Route:
|
||||
def __init__(self, comparator, endpoint):
|
||||
def __init__(self, comparator, method):
|
||||
"""
|
||||
:param comparator: Как и с чем сравнивать
|
||||
:param endpoint: Куда отправить
|
||||
:param method: Куда отправить
|
||||
"""
|
||||
print('Route.__init__')
|
||||
self.comparator = comparator
|
||||
self.endpoint = endpoint
|
||||
self.method = method
|
||||
|
||||
def __repr__(self):
|
||||
return 'Route(comparator={comparator}, endpoint={endpoint})'.format(
|
||||
return 'Route(comparator={comparator}, method={method})'.format(
|
||||
comparator=self.comparator,
|
||||
endpoint=self.endpoint
|
||||
method=self.method
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user