コード例 #1
0
ファイル: test_labels.py プロジェクト: clchiou/garage
    def test_label_format(self):

        for module_path, object_path in (
            ('foo', 'bar'),
            ('_f_9_o_.x.y.z', 'b.a.r'),
            ('foo.bar', 'a[1].b[22].c[333]'),
        ):
            with self.subTest((module_path, object_path)):
                labels.Label(module_path, object_path)

        error = r'expect.*fullmatch'
        for module_path, object_path in (
                # Empty path.
            ('', ''),
            ('foo', ''),
            ('', 'bar'),
                # Empty path part.
            ('foo.', 'bar'),
            ('foo', 'bar.'),
            ('.foo', 'bar'),
            ('foo', '.bar'),
                # Illegal identifier characters.
            ('0foo', 'bar'),
            ('foo', '0bar'),
            ('foo/', 'bar'),
            ('foo', 'bar/'),
                # Incorrect element index.
            ('foo', 'bar[]'),
            ('foo', 'bar[x]'),
            ('foo', 'bar.[0]'),
        ):
            with self.subTest((module_path, object_path)):
                with self.assertRaisesRegex(AssertionError, error):
                    labels.Label(module_path, object_path)
コード例 #2
0
ファイル: parameters.py プロジェクト: clchiou/garage
 def do_iter(namespace):
     for name, value in namespace._entries.items():
         parts.append(name)
         if isinstance(value, Namespace):
             yield from do_iter(value)
         else:
             ASSERT.isinstance(value, ParameterBase)
             label = labels.Label(module_path, '.'.join(parts))
             yield label, value
         parts.pop()
コード例 #3
0
ファイル: test_parameters.py プロジェクト: clchiou/garage
 def test_iter_parameters(self):
     n = parameters.Namespace(
         a=parameters.Parameter(1),
         m=parameters.Namespace(
             p=parameters.Parameter(4),
             q=parameters.Parameter(5),
         ),
         b=parameters.Parameter(2),
         c=parameters.Parameter(3),
     )
     self.assertEqual(
         list(parameters.iter_parameters('X', n)),
         [
             (labels.Label('X', 'a'), n.a),
             (labels.Label('X', 'm.p'), n.m.p),
             (labels.Label('X', 'm.q'), n.m.q),
             (labels.Label('X', 'b'), n.b),
             (labels.Label('X', 'c'), n.c),
         ],
     )
コード例 #4
0
ファイル: test_labels.py プロジェクト: clchiou/garage
    def test_label(self):
        l = labels.Label('foo.bar', 'spam.egg')
        self.assertEqual(l, 'foo.bar:spam.egg')
        self.assertEqual(hash(l), hash('foo.bar:spam.egg'))
        self.assertEqual(str(l), 'foo.bar:spam.egg')
        self.assertEqual(repr(l), repr('foo.bar:spam.egg'))
        self.assertEqual(l.module_path, 'foo.bar')
        self.assertEqual(l.object_path, 'spam.egg')

        self.assertEqual(labels.Label.parse('foo.bar:spam.egg'), l)

        l2 = labels.Label('foo.bar', 'spam.egg')
        self.assertIsNot(l, l2)
        self.assertEqual(l, l2)
        self.assertEqual(hash(l), hash(l2))

        l3 = labels.Label('foo.bar', 'spam.egg_')
        self.assertIsNot(l, l3)
        self.assertNotEqual(l, l3)
        self.assertNotEqual(hash(l), hash(l3))
コード例 #5
0
ファイル: schemas.py プロジェクト: clchiou/garage
    def _update(self):
        ASSERT.not_none(self._loader)
        LOG.debug('update schema tables')

        id_to_schema = {
            schema.proto.id: schema
            for schema in map(Schema, self._loader.getAllLoaded())
        }

        for schema in id_to_schema.values():

            if schema.proto.is_file():
                path = schema.proto.display_name
                if path not in self.files:
                    LOG.debug('add file node: %s', path)
                    self.files[path] = schema
                continue

            if schema.proto.is_struct():
                table = self.struct_schemas
            elif schema.proto.is_enum():
                table = self.enum_schemas
            elif schema.proto.is_interface():
                table = self.interface_schemas
            elif schema.proto.is_const():
                table = self.const_schemas
            elif schema.proto.is_annotation():
                table = self.annotations
            else:
                ASSERT.unreachable('unexpected schema kind: {}', schema)

            label = labels.Label(
                self._get_module_path(schema, id_to_schema),
                self._get_object_path(schema, id_to_schema),
            )
            if label in table:
                continue

            LOG.debug('add schema: %s', label)
            if schema.proto.is_struct():
                table[label] = schema.as_struct()
            elif schema.proto.is_enum():
                table[label] = schema.as_enum()
            elif schema.proto.is_interface():
                table[label] = schema.as_interface()
            elif schema.proto.is_const():
                table[label] = schema.as_const()
            elif schema.proto.is_annotation():
                table[label] = schema
            else:
                ASSERT.unreachable('unexpected schema kind: {}', schema)
コード例 #6
0
ファイル: test_labels.py プロジェクト: clchiou/garage
    def test_make_labels(self):
        names = labels.make_labels('foo.bar', 'x', 'y', z='p.q')
        self.assertEqual(
            names._asdict(),
            {
                'x': labels.Label('foo.bar', 'x'),
                'y': labels.Label('foo.bar', 'y'),
                'z': labels.Label('foo.bar', 'p.q'),
            },
        )
        self.assertEqual(names.x, labels.Label('foo.bar', 'x'))
        self.assertEqual(names.y, labels.Label('foo.bar', 'y'))
        self.assertEqual(names.z, labels.Label('foo.bar', 'p.q'))

        n2 = labels.make_labels('spam.egg', 'p', **names._asdict())
        self.assertEqual(
            n2._asdict(),
            {
                'p': labels.Label('spam.egg', 'p'),
                'x': labels.Label('foo.bar', 'x'),
                'y': labels.Label('foo.bar', 'y'),
                'z': labels.Label('foo.bar', 'p.q'),
            },
        )
コード例 #7
0
ファイル: monitors.py プロジェクト: clchiou/garage
            unit='seconds',
        ),
        executor_queue_threshold=parameters.Parameter(
            # If you have enough executor threads, this queue should
            # never accumulate backlog.
            1,
            validate=(0).__le__,
        ),
        num_tasks_threshold=parameters.Parameter(
            200,
            validate=(0).__le__,
        ),
    ),
)

PARAMS_LABEL = labels.Label(__name__, 'params')


async def monitor(
    period,
    executor_queue_threshold,
    num_tasks_threshold,
    executor_queue,
):
    while True:
        queue_length = len(executor_queue)
        stats = kernels.get_kernel().get_stats()
        if (
            queue_length >= executor_queue_threshold
            or stats.num_tasks >= num_tasks_threshold
        ):
コード例 #8
0
ファイル: composers.py プロジェクト: clchiou/garage
        del request  # Unused.
        response.status = consts.Statuses.NO_CONTENT
        response.headers[consts.HEADER_ALLOW] = self._allow

    async def __call__(self, request, response):
        handler = self._handlers.get(request.method)
        if not handler:
            raise wsgi_apps.HttpError(
                consts.Statuses.METHOD_NOT_ALLOWED,
                'unsupported request method: %s' % request.method,
                {consts.HEADER_ALLOW: self._allow},
            )
        return await handler(request, response)


PATH_MATCH = labels.Label(__name__, 'path_match')


def group(request, *groups, default=None):
    match = request.context.get(PATH_MATCH)
    if match is None:
        return default
    return match.group(*groups)


def get_path_str(request):
    path_str = request.path_str
    match = request.context.get(PATH_MATCH)
    if match is not None:
        path_str = path_str[match.end():]
    return path_str