class TestList(unittest.TestCase):

    TRAFARET = T.Dict({
        T.Key("strs", optional=True): T.List(T.String()),
        T.Key("ints", optional=True): T.List(T.Int()),
    })

    def test_strings(self):
        self.assertEqual(
            get_err(self.TRAFARET, u"""
            strs: [1, 2]
        """),
            dedent(u"""\
            config.yaml:2: strs[0]: value is not a string
            config.yaml:2: strs[1]: value is not a string
        """))

    def test_ints(self):
        self.assertEqual(
            get_err(self.TRAFARET, u"""
            ints: ["hello", "world"]
        """),
            dedent(u"""\
            config.yaml:2: ints[0]: value can't be converted to int
            config.yaml:2: ints[1]: value can't be converted to int
        """))
Beispiel #2
0
 def test_list_repr(self):
     res = t.List(t.Int)
     self.assertEqual(repr(res), '<List(<Int>)>')
     res = t.List(t.Int, min_length=1)
     self.assertEqual(repr(res), '<List(min_length=1 | <Int>)>')
     res = t.List(t.Int, min_length=1, max_length=10)
     self.assertEqual(repr(res), '<List(min_length=1, max_length=10 | <Int>)>')
Beispiel #3
0
def setup_app(app: FastAPI):
    """
    Настройка приложения
    """

    # Шаблон для задания структуры базы данных
    TRAFARET = T.Dict({
        T.Key('databases'):
        T.List(
            T.Dict({
                'name':
                T.String(),
                'address':
                T.String(),
                'collections':
                T.List(
                    T.Dict({
                        'name':
                        T.String(),
                        'fields':
                        T.List(T.String),
                        T.Key('index_fields', optional=True):
                        T.List(T.String),
                    }))
            }))
    })

    config = read_and_validate('config.yaml', TRAFARET)
    app.config = config

    app.add_middleware(CheckUserAuthMiddleware)
Beispiel #4
0
class SubAbstractModel(BaseModel):
    __abstract__ = True
    structure = t.Dict({
        'list_attrs': t.List(t.String),
        'salers': t.List(t.Dict({
            'name': t.String,
            'address': t.String
        }))
    }).allow_extra('wrong_attr')
    indexes = [('quantity', DESCENDING), 'name']
    required_fields = ['list_attrs']
Beispiel #5
0
 def test_list_repr(self):
     res = t.List(t.ToInt)
     assert repr(res) == '<List(<ToInt>)>'
     res = t.List(t.ToInt, min_length=1)
     assert repr(res) == '<List(min_length=1 | <ToInt>)>'
     res = t.List(t.ToInt, min_length=1, max_length=10)
     assert repr(res) == '<List(min_length=1, max_length=10 | <ToInt>)>'
     res = t.List[t.ToInt]
     assert repr(res) == '<List(<ToInt>)>'
     res = t.List[t.ToInt, 1:]
     assert repr(res) == '<List(min_length=1 | <ToInt>)>'
     res = t.List[:10, t.ToInt]
     assert repr(res) == '<List(max_length=10 | <ToInt>)>'
 def from_row(cls, row, books: List['Book'], photos: List['Photo'],
              alias=None):
     table = cls.Options.db_table if alias is None else alias
     return cls(
         id=t.Int().check(row[table.c.id]),
         name=t.String().check(row[table.c.name]),
         date_of_birth=Date().check(row[table.c.date_of_birth]),
         date_of_death=t.Or(Date, t.Null).check(row[table.c.date_of_death]),
         books=t.List(t.Type(Book)).check(books),
         photos=t.List(t.Type(Photo)).check(photos),
         created_at=DateTime().check(row[table.c.created_at]),
         updated_at=t.Or(DateTime, t.Null).check(row[table.c.updated_at]),
         is_populated=True
     )
Beispiel #7
0
    def test_simple_validator(self):
        # Create contract
        trafaret = t.Trafaret()
        template = Template(trafaret)
        contract = Contract(template)
        self.assertEqual(contract.template, template)
        new_template = Template(trafaret)
        contract.template = new_template
        self.assertEqual(contract.template, new_template)

        # Int
        int_t = Template(t.Int())
        contract = Contract(int_t)

        # String
        string_t = Template(t.String())
        contract = Contract(string_t)

        # Dict
        dict_t = Template(t.Dict({'id': t.Int, 'email': t.Email}))
        contract.template = dict_t

        # List
        list_t = Template(t.List(t.Int))
        contract.template = list_t
Beispiel #8
0
    def __init__(self, allow_extra):
        self.schema = t.Dict({
            'id': t.Int(),
            'client_name': t.String(max_length=255),
            'sort_index': t.Float,
            # t.Key('client_email', optional=True): t.Or(t.Null | t.Email()),
            t.Key('client_phone', optional=True): t.Or(t.Null | t.String(max_length=255)),

            t.Key('location', optional=True): t.Or(t.Null | t.Dict({
                'latitude': t.Or(t.Float | t.Null),
                'longitude': t.Or(t.Float | t.Null),
            })),

            t.Key('contractor', optional=True): t.Or(t.Null | t.Int(gt=0)),
            t.Key('upstream_http_referrer', optional=True): t.Or(t.Null | t.String(max_length=1023)),
            t.Key('grecaptcha_response'): t.String(min_length=20, max_length=1000),

            t.Key('last_updated', optional=True): t.Or(t.Null | t.String >> parse),

            t.Key('skills', default=[]): t.List(t.Dict({
                'subject': t.String,
                'subject_id': t.Int,
                'category': t.String,
                'qual_level': t.String,
                'qual_level_id': t.Int,
                t.Key('qual_level_ranking', default=0): t.Float,
            })),
        })
        if allow_extra:
            self.schema.allow_extra('*')
 def from_row(cls, row, author: 'Author', photos: List['Photo'],
              chapters: List['Chapter'], series: 'Series' = None,
              alias=None):
     table = cls.Options.db_table if alias is None else alias
     return cls(
         id=t.Int().check(row[table.c.id]),
         title=t.String().check(row[table.c.title]),
         date_published=Date().check(row[table.c.date_published]),
         author=t.Type(Author).check(author),
         photos=t.List(t.Type(Photo)).check(photos),
         chapters=t.List(t.Type(Chapter)).check(chapters),
         series=t.Or(t.Type(Series), t.Null).check(series),
         created_at=DateTime().check(row[table.c.created_at]),
         updated_at=t.Or(DateTime, t.Null).check(row[table.c.updated_at]),
         is_populated=True
     )
Beispiel #10
0
class i18nModel(BaseModel):
    __collection__ = "i18ntests"
    inc_id = True
    structure = t.Dict({
        'list_attrs':
        t.List(t.String),
        'salers':
        t.List(t.Dict({
            'name': t.String,
            'address': t.String
        })),
        t.Key('list_sku', default=[]):
        t.List(t.Int)
    }).allow_extra('*')
    i18n = ['list_attrs', 'salers']
    indexes = [('quantity', DESCENDING), 'name']
Beispiel #11
0
async def test_list():
    trafaret = t.List(t.ToInt & check_int)
    res = await (trafaret.async_check(['5']))
    assert res == [5]
    with pytest.raises(t.DataError) as res:
        await trafaret.async_check(['5qwe'])
    assert res.value.as_dict() == {0: "value can't be converted to int"}
Beispiel #12
0
def property_names(trafaret):
    checker = t.List(trafaret)

    def check(data):
        return checker(list(data.keys()))

    return check
Beispiel #13
0
def check_array(items=[], additionalItems=None):
    if len(items) == 1:
        return t.List(items[0])

    def inner(data):
        errors = {}
        values = []
        for index, schema in enumerate(items):
            try:
                value = schema(data[index])
                values.append(value)
            except t.DataError as de:
                errors[index] = de
            except IndexError:
                errors[index] = t.DataError('value with this index is required')
        if len(items) < len(data):
            if additionalItems:
                for index in range(len(items), len(data)):
                    try:
                        value = additionalItems(data[index])
                        values.append(value)
                    except t.DataError as de:
                        errors[index] = de
            else:
                raise t.DataError('Too many items in array')
        if errors:
            raise t.DataError(errors)
        return values
    return inner
Beispiel #14
0
 def __init__(self, field, min_length=0, max_length=None, **kwargs):
     super(List, self).__init__(**kwargs)
     self.field = field
     self._trafaret = t.List(field._trafaret,
                             min_length=min_length, max_length=max_length)
     if self.allow_none:
         self._trafaret |= t.Null
Beispiel #15
0
def shortest_paths_tree():
    '''
    Function for implementation API endpoint 'api/shortest_paths_tree'.

    > Request:
        - body:
            :param nodes: list<str>,
                Ids nodes
            :param object: str
                Id node for object

    > Response:
        (Success)
            - body:
                :param tree_weight: double
                :param paths_weight: double
                    * Sum of the shortest paths
                :param shortest_paths_tree: [(str, str), ...]
                    * Array edges
        (Failed)
            - body:
                :param detail: str

        status: int
    '''
    logger.setLevel(logging.INFO)
    logger.info("Request on API Gateway 'api/shortest_paths_tree'")

    # Validation of body request
    validator = trafaret.Dict({
        trafaret.Key('nodes'): trafaret.List(trafaret.String),
        trafaret.Key('object'): trafaret.String
    })
    try:
        validated_data = validator.check(request.json)
    except trafaret.DataError:
        return jsonify({'details': f"Error of body request: {trafaret.extract_error(validator, request.json)}"}), 400

    # Getting info for graph
    id_nodes = _graph__get_id_nodes(validated_data['nodes'])
    id_object = _graph__get_id_nodes([validated_data['object']])[0]

    # Result: float (tree_weight), list<(int, int)> (array edges)
    result = algorithmsWrapper.task_2_1(id_object, id_nodes)
    tree_weight, paths_weight, edges = result

    # Converting results from graph to real
    with open(os.path.join(PATH_DATA, 'matching_from_graph.json'), 'r') as file:
        matching_from_graph = ast.literal_eval(file.read())

    shortest_paths_tree = list(map(lambda edge: (matching_from_graph[edge[0]], matching_from_graph[edge[1]]), edges))

    response_data = {
        "tree_weight": tree_weight,
        "paths_weight": paths_weight,
        "shortest_paths_tree": shortest_paths_tree
    }

    return jsonify(response_data), 200
Beispiel #16
0
    async def post(self, citizens: t.List(Citizen)) -> Response:
        import_id = str(uuid4())

        for citizen in citizens:
            citizen["import_id"] = import_id

        await self.dao.insert_many(citizens)
        return get_json_response({"import_id": import_id}, status=201)
Beispiel #17
0
def build_trafaret(sa_type, **kwargs):

    if isinstance(sa_type, sa.sql.sqltypes.Enum):
        trafaret = t.Enum(*sa_type.enums, **kwargs)

    # check for Text should be before String
    elif isinstance(sa_type, sa.sql.sqltypes.Text):
        trafaret = t.String(**kwargs)

    elif isinstance(sa_type, sa.sql.sqltypes.String):
        trafaret = t.String(max_length=sa_type.length, **kwargs)

    elif isinstance(sa_type, sa.sql.sqltypes.Integer):
        trafaret = t.Int(**kwargs)

    elif isinstance(sa_type, sa.sql.sqltypes.Float):
        trafaret = t.Float(**kwargs)

    elif isinstance(sa_type, sa.sql.sqltypes.DateTime):
        trafaret = DateTime(**kwargs)  # RFC3339

    elif isinstance(sa_type, sa.sql.sqltypes.Date):
        trafaret = DateTime(**kwargs)  # RFC3339

    elif isinstance(sa_type, sa.sql.sqltypes.Boolean):
        trafaret = t.StrBool(**kwargs)

    # Add PG related JSON and ARRAY
    elif isinstance(sa_type, postgresql.JSON):
        trafaret = AnyDict | t.List(AnyDict)

    # Add PG related JSON and ARRAY
    elif isinstance(sa_type, postgresql.ARRAY):
        item_trafaret = build_trafaret(sa_type.item_type)
        trafaret = t.List(item_trafaret)

    elif isinstance(sa_type, postgresql.INET):
        # TODO: depend on "ipaddress" module?
        trafaret = t.String(**kwargs)

    else:
        type_ = str(sa_type)
        msg = 'Validator for type {} not implemented'.format(type_)
        raise NotImplementedError(msg)
    return trafaret
Beispiel #18
0
def find_objects_in_radius():
    '''
    Function for implementation API endpoint 'api/objects/find/in_radius'.

    > Request:
        - body:
            :param nodes: list<str>,
                Ids nodes
            :param metrics: str
                Type direction: 'to', 'from', 'to-from'
            :param max_dist: float

    > Response:
        (Success)
            - body:
                <id_node>: list<<id_object>>
                ...
        (Failed)
            - body:
                :param detail: str

        status: int
    '''
    logger.setLevel(logging.INFO)
    logger.info("Request on API Gateway 'api/objects/find/in_radius'")

    # Validation of body request
    validator = trafaret.Dict({
        trafaret.Key('nodes'): trafaret.List(trafaret.String),
        trafaret.Key('metrics'): trafaret.Enum("to", "from", "to-from"),
        trafaret.Key('max_dist'): trafaret.Float
    })
    try:
        validated_data = validator.check(request.json)
    except trafaret.DataError:
        return jsonify({'details': f"Error of body request: {trafaret.extract_error(validator, request.json)}"}), 400

    # Getting info for graph
    id_objects = _graph__get_all_id_objects()
    id_nodes = _graph__get_id_nodes(validated_data['nodes'])

    type_dir = {"to": 1, "from": 2, "to-from": 3}[validated_data['metrics']]
    max_dist = validated_data['max_dist']

    # Results: list<(int, list<int>)>. Array of pair (id_node, array id_object)
    results = algorithmsWrapper.task_1_1_b(id_objects, id_nodes, type_dir, max_dist)

    # Converting results from graph to real
    with open(os.path.join(PATH_DATA, 'matching_from_graph.json'), 'r') as file:
        matching_from_graph = ast.literal_eval(file.read())
    response_data = {}
    for result in results:
        response_data[matching_from_graph[result[0]]] = list(matching_from_graph[id_object] for id_object in result[1])

    return jsonify(response_data), 200
Beispiel #19
0
    def actions_validate(self, **kwargs):

        actions_schema = t.Dict({
            t.Key('actions'):
            t.List(t.Dict({
                t.Key('type'):
                t.String,
                t.Key('action'):
                t.String,
                t.Key('description', optional=True):
                t.String(max_length=200),
                t.Key('parameters'):
                t.Dict().allow_extra("*"),
                t.Key('filters', optional=True):
                t.List(t.Dict().allow_extra("*"))
            }),
                   min_length=1),
        })

        actions_schema(kwargs)
Beispiel #20
0
 def test_2_0_regression(self):
     t_request = t.Dict({
         t.Key('params', optional=True):
         t.Or(t.List(t.Any()), t.Mapping(t.AnyString(), t.Any())),
     })
     assert t_request.check({'params': {
         'aaa': 123
     }}) == {
         'params': {
             'aaa': 123
         }
     }
Beispiel #21
0
async def delete_files(request: web.Request) -> web.Response:
    async with check_params(
            request,
            t.Dict({
                t.Key('volume'): t.String(),
                t.Key('vfid'): tx.UUID(),
                t.Key('relpaths'): t.List(tx.PurePath(relative_only=True)),
            })) as params:
        await log_manager_api_entry(log, 'delete_files', params)
        return web.json_response({
            'status': 'ok',
        })
Beispiel #22
0
        def wrapper(value):
            trafaret = t.List(t.Int)
            value = trafaret.check(value)
            roles = Role.query.filter(Role.id.in_(value)).all()

            if len(value) != len(roles):
                return t.DataError(_("Roles are invalid"))

            if current_user.is_superuser():
                return roles
            else:
                return self.get_object(id).roles
Beispiel #23
0
 def test_list(self):
     res = extract_error(t.List(t.ToInt), 1)
     assert res == 'value is not a list'
     res = t.List(t.ToInt).check([1, 2, 3])
     assert res == [1, 2, 3]
     res = t.List(t.String).check([u"foo", u"bar", u"spam"])
     assert res == [u'foo', u'bar', u'spam']
     res = extract_error(t.List(t.ToInt), [1, 2, 1 + 3j])
     assert res == {2: 'value is not int'}
     res = t.List(t.ToInt, min_length=1).check([1, 2, 3])
     assert res == [1, 2, 3]
     res = extract_error(t.List(t.ToInt, min_length=1), [])
     assert res == 'list length is less than 1'
     res = t.List(t.ToInt, max_length=2).check([1, 2])
     assert res == [1, 2]
     res = extract_error(t.List(t.ToInt, max_length=2), [1, 2, 3])
     assert res == 'list length is greater than 2'
     res = extract_error(t.List(t.ToInt), ["a"])
     assert res == {0: "value can't be converted to int"}
Beispiel #24
0
 def test_list(self):
     res = extract_error(t.List(t.Int), 1)
     self.assertEqual(res, 'value is not a list')
     res = t.List(t.Int).check([1, 2, 3])
     self.assertEqual(res, [1, 2, 3])
     res = t.List(t.String).check(["foo", "bar", "spam"])
     self.assertEqual(res, ['foo', 'bar', 'spam'])
     res = extract_error(t.List(t.Int), [1, 2, 1 + 3j])
     self.assertEqual(res, {2: 'value is not int'})
     res = t.List(t.Int, min_length=1).check([1, 2, 3])
     self.assertEqual(res, [1, 2, 3])
     res = extract_error(t.List(t.Int, min_length=1), [])
     self.assertEqual(res, 'list length is less than 1')
     res = t.List(t.Int, max_length=2).check([1, 2])
     self.assertEqual(res, [1, 2])
     res = extract_error(t.List(t.Int, max_length=2), [1, 2, 3])
     self.assertEqual(res, 'list length is greater than 2')
     res = extract_error(t.List(t.Int), ["a"])
     self.assertEqual(res, {0: "value can't be converted to int"})
Beispiel #25
0
class DesktopBrowserInfoTrafaret(trf.Trafaret):
    scheme = trf.List(
        trf.Dict(
            {
                "browser_type": UpperTextToEnumTrafaret(BrowserType),
                "width": trf.Int,
                "height": trf.Int,
            }
        )
    )

    def check_and_return(self, value, context=None):
        sanitized = self.scheme.check(value, context)
        return [DesktopBrowserInfo(**dct) for dct in sanitized]
Beispiel #26
0
class ChromeEmulationInfoTrafaret(trf.Trafaret):
    scheme = trf.List(
        trf.Dict(
            {
                "device_name": TextToEnumTrafaret(DeviceName),
                trf.Key("screen_orientation", optional=True): UpperTextToEnumTrafaret(
                    ScreenOrientation
                ),
            }
        )
    )

    def check_and_return(self, value, context=None):
        sanitized = self.scheme.check(value, context)
        return [ChromeEmulationInfo(**dct) for dct in sanitized]
Beispiel #27
0
def main():
    # wall_owner_id = -77868632  # Force
    # wall_owner_id = -101555444  # CopyPaste
    wall_owner_id = -58010960  # target
    mask = t.Dict({
        'from_id':
        t.Int,
        'date':
        lambda v: datetime.fromtimestamp(v),
        'text':
        t.String(allow_blank=True),
        t.Key('id') >> 'post_id':
        t.Int,
        t.Key('owner_id', default=wall_owner_id):
        t.Int(),
        t.Key('likes') >> 'likes_count':
        lambda v: int(v['count']),
        t.Key('reposts') >> 'reposts_count':
        lambda v: int(v['count']),
        t.Key('attachments', optional=True) >> 'attachments':
        t.List(t.Dict().allow_extra('*'))
    }).ignore_extra('*')
    api = vk.API(vk.Session())

    posts = resources.Posts(wall_owner_id, api)

    posts_list = []
    try:
        raise Exception
        posts_list = p.load(open("posts%s.pikle".format(wall_owner_id), "rb"))

        if len(posts_list) is 0:
            raise Exception
        print('~~~***!!!Data taken from pickle!!!***~~~')
    except:
        for post in tqdm(posts):
            data = mask.check(post)
            if data.get('attachments'):
                attachments = Attachments(data['post_id'], data['owner_id'])
                data['attachments'], data['photos'] = \
                    attachments.download(data['attachments'])
            post = Post(**data)
            posts_list.append(post)

        p.dump(posts_list, open("posts%s.pikle".format(wall_owner_id), "wb"))

    session.add_all(posts_list)
    session.commit()
Beispiel #28
0
class Product(Model):
    __collection__ = "products"
    inc_id = True
    structure = t.Dict({'list_attrs': t.List(t.String)}).allow_extra('*')
    i18n = ['list_attrs']
    indexes = [('quantity', -1), 'name']

    def as_dict(self, api_fields=None, exclude=None):
        """ Returns instance as dict in selected language
        """
        keys = api_fields or self.keys()
        if exclude:
            keys = list(set(keys) | set(exclude))
        result = dict(map(lambda key: (key, getattr(self, key)), keys))
        '_id' in result and result.__setitem__('_id', str(result['_id']))
        return result
Beispiel #29
0
    def action_validate(self, choices, **kwargs):

        action_schema = self.tr.Dict({
            self.tr.Key('type'):
            t.String,
            self.tr.Key('action'):
            t.Enum(*choices),
            self.tr.Key('description', optional=True):
            t.String(max_length=200),
            self.tr.Key('parameters'):
            t.Dict().allow_extra("*"),
            self.tr.Key('filters', optional=True):
            t.List(t.Dict().allow_extra("*"))
        })

        action_schema(kwargs)
def setup_app(app: Application):

    if isfile('config.yaml'):
        TRAFARET = T.Dict({
            T.Key('services'):
            T.List(
                T.Dict({
                    'name': T.String(),
                    'jwt_ttl_minutes': T.Int(),
                    'redirect_link': T.String(),
                    'algorithm': T.String(),
                    'secret_key': T.String()
                }))
        })

        config = read_and_validate('config.yaml', TRAFARET)
        app['services'] = {x['name']: x for x in config['services']}