Пример #1
0
    def testPostValidatingEvent(self):

        # This test is similar to the test for pre-validation above. The
        # difference is that the post validation works after main schema
        # validation, whilst the pre-validation works before, and may
        # short-circuit schema validation.

        component.provideSubscriptionAdapter(PostValidation, adapts=(IObject2,))
        component.provideSubscriptionAdapter(PostValidation, adapts=(IObject3,))

        ob = Dummy('dummy')
        directlyProvides(ob, IObject1)
        errors = ob.validate()
        self.assertFalse('bar' in errors)
        del ob

        ob = Dummy('dummy')
        directlyProvides(ob, IObject2)
        errors = ob.validate()
        self.assertTrue(errors['bar'])
        del ob

        ob = Dummy('dummy')
        directlyProvides(ob, IObject3)
        errors = ob.validate()
        self.assertTrue('bar' in errors)
        del ob

        sm = component.getSiteManager()
        sm.unregisterSubscriptionAdapter(PostValidation, required=(IObject2,))
        sm.unregisterSubscriptionAdapter(PostValidation, required=(IObject3,))
Пример #2
0
def GenerateCollectionTraversal(ctx):
    
    collection_names = []
    for k, v in ctx.domain_model.__dict__.items():
        if IManagedContainer.providedBy(v):
            collection_names.append(k)

    if not collection_names:
        return
    
    # Note: the templated CollectionTraverser TYPE returned by this is
    # instantiated multiple times in case of inheritance of catalyzed 
    # descriptors e.g. for Motion, it is instantiated once for 
    # ParliamentaryItemDescriptor and once for MotionDescriptor.
    traverser = CollectionTraverser(*collection_names)
    
    # provideSubscriptionAdapter(factory, adapts=None, provides=None)
    component.provideSubscriptionAdapter(traverser, 
        adapts=(ctx.domain_interface, IPublisherRequest), 
        provides=ITraverserPlugin)
    # provideAdapter(factory, adapts=None, provides=None, name="")
    component.provideAdapter(PluggableTraverser,
        adapts=(ctx.domain_interface, IPublisherRequest),
        provides=IPublishTraverse
    )
Пример #3
0
    def test_register_deserializer(self):
        @implementer(IBlockFieldDeserializationTransformer)
        @adapter(IBlocks, IBrowserRequest)
        class TestAdapter(object):
            order = 10
            block_type = "test"

            def __init__(self, context, request):
                self.context = context
                self.request = request

            def __call__(self, value):
                self.context._handler_called = True

                value["value"] = u"changed: {}".format(value["value"])

                return value

        provideSubscriptionAdapter(
            TestAdapter,
            (IDexterityItem, IBrowserRequest),
        )

        self.deserialize(blocks={"123": {"@type": "test", "value": u"text"}})

        assert self.portal.doc1._handler_called is True
        assert self.portal.doc1.blocks["123"]["value"] == u"changed: text"
Пример #4
0
def generate_collection_traversal(ti):

    # !+ merge with utils.get_managed_containers?
    def get_collection_names(domain_model):
        return [
            k for k, v in domain_model.__dict__.items()
            if IManagedContainer.providedBy(v)
        ]

    collection_names = get_collection_names(ti.domain_model)

    if not collection_names:
        return
    # Note: the templated CollectionTraverser TYPE returned by this is
    # instantiated per "inherited and catalysed" descriptor e.g. for Motion,
    # it is instantiated once for DocDescriptor and once for MotionDescriptor.
    traverser = CollectionTraverser(*collection_names)

    # provideSubscriptionAdapter(factory, adapts=None, provides=None)
    component.provideSubscriptionAdapter(traverser,
                                         adapts=(ti.derived_table_schema,
                                                 IPublisherRequest),
                                         provides=ITraverserPlugin)
    # provideAdapter(factory, adapts=None, provides=None, name="")
    component.provideAdapter(PluggableTraverser,
                             adapts=(ti.derived_table_schema,
                                     IPublisherRequest),
                             provides=IPublishTraverse)
Пример #5
0
def decorate_at_field(_context=None, fieldclass=None, zcml=True):
    if not fieldclass in _fieldclass_monkies:

        # register adapters to call original methods

        _get=fieldclass.get
        _set=fieldclass.set    
        
        @implementer(IFieldValue)
        @adapter(fieldclass, wicked.fieldevent.IFieldRenderEvent)
        def field_value(field, event):
            return _get(field, event.instance, **event.kwargs)

        @implementer(IFieldValueSetter)
        @adapter(fieldclass, wicked.fieldevent.IFieldStorageEvent)
        def field_value_set(field, event):
            _set(field, event.instance, event.value, **event.kwargs)

        if zcml:
            # do proper zopeish config
            zope.component.zcml.adapter(_context, (field_value,))
            zope.component.zcml.subscriber(_context,
                                           factory=field_value_set,
                                           provides=IFieldValueSetter)
            _context.action(
                discriminator = (fieldclass, monkey_getset),
                callable = monkey_getset,
                args = (fieldclass,)
                )
        else:
            # hook it up no delay
            monkey_getset(fieldclass)
            provideAdapter(field_value)
            provideSubscriptionAdapter(field_value_set)
Пример #6
0
    def testPreValidatingEvent(self):

        # Register some subscription adapters for different types of objects
        # These will be called during validation

        component.provideSubscriptionAdapter(PreValidation, adapts=(IObject1,))
        component.provideSubscriptionAdapter(PreValidation, adapts=(IObject3,))

        # Verify that they are called only on the right type of object,
        # and that their return values are included in the error output

        ob = Dummy('dummy')
        directlyProvides(ob, IObject1)
        errors = ob.validate()
        self.assertTrue(errors['foo'])
        del ob

        ob = Dummy('dummy')
        directlyProvides(ob, IObject2)
        errors = ob.validate()
        self.assertFalse('foo' in errors)
        del ob

        ob = Dummy('dummy')
        directlyProvides(ob, IObject3)
        errors = ob.validate()
        self.assertTrue(errors['foo'])
        del ob

        sm = component.getSiteManager()
        sm.unregisterSubscriptionAdapter(PreValidation, required=(IObject1,))
        sm.unregisterSubscriptionAdapter(PreValidation, required=(IObject3,))
Пример #7
0
    def solve_dependencies(self, interface, library, resources):
        """This solve dependencies for resources mapped to an
        interface in the given library. When solved, a fanstatic
        resource is created and registered.
        """
        dependencies = []
        try:
            need_parent = None
            parents = list_base_layers(interface)
            while True:
                dependency = parents.send(need_parent)
                identifier = dependency.__identifier__
                if identifier in self.resources:
                    if identifier in INTERFACES_RESOURCES:
                        fanstatic_dependency = INTERFACES_RESOURCES[identifier]
                    else:
                        fanstatic_dependency = self.solve_dependencies(*self.resources[identifier])
                    if fanstatic_dependency is not None:
                        dependencies.insert(0, fanstatic_dependency)
                    need_parent = False
                else:
                    need_parent = True
        except StopIteration:
            fanstatic_resource = get_fanstatic_resource(library, resources, dependencies)
            context = silvaconf.only_for.bind().get(interface)
            factory = create_resource_subscriber(fanstatic_resource)

            provideSubscriptionAdapter(factory, (interface, context), ISubscribedResource)
            provideInterface('', interface)
            INTERFACES_RESOURCES[interface.__identifier__] = fanstatic_resource
            return fanstatic_resource
        return None
Пример #8
0
def GenerateCollectionTraversal(ctx):

    collection_names = []
    for k, v in ctx.domain_model.__dict__.items():
        if IManagedContainer.providedBy(v):
            collection_names.append(k)

    if not collection_names:
        return

    # Note: the templated CollectionTraverser TYPE returned by this is
    # instantiated multiple times in case of inheritance of catalyzed
    # descriptors e.g. for Motion, it is instantiated once for
    # ParliamentaryItemDescriptor and once for MotionDescriptor.
    traverser = CollectionTraverser(*collection_names)

    # provideSubscriptionAdapter(factory, adapts=None, provides=None)
    component.provideSubscriptionAdapter(traverser,
                                         adapts=(ctx.domain_interface,
                                                 IPublisherRequest),
                                         provides=ITraverserPlugin)
    # provideAdapter(factory, adapts=None, provides=None, name="")
    component.provideAdapter(PluggableTraverser,
                             adapts=(ctx.domain_interface, IPublisherRequest),
                             provides=IPublishTraverse)
Пример #9
0
def handle_setting(key, text, alt_text, default):
    def accessControlSettingFactory(context=None):
        return AccessControlSetting(key, text, alt_text, default)

    provideSubscriptionAdapter(accessControlSettingFactory,
                               adapts=[None],
                               provides=IAccessControlSetting)
Пример #10
0
    def test_disabled_deserializer(self):
        @implementer(IBlockFieldDeserializationTransformer)
        @adapter(IBlocks, IBrowserRequest)
        class TestAdapter:
            order = 10
            block_type = "test"
            disabled = True

            def __init__(self, context, request):
                self.context = context
                self.request = request

            def __call__(self, value):
                self.context._handler_called = True

                value["value"] = "changed: {}".format(value["value"])

                return value

        provideSubscriptionAdapter(
            TestAdapter,
            (IDexterityItem, IBrowserRequest),
        )

        self.deserialize(blocks={"123": {"@type": "test", "value": "text"}})

        assert not getattr(self.portal.doc1, "_handler_called", False)
        assert self.portal.doc1.blocks["123"]["value"] == "text"

        sm = getGlobalSiteManager()
        sm.adapters.unsubscribe(
            (IDexterityItem, IBrowserRequest),
            IBlockFieldDeserializationTransformer,
            TestAdapter,
        )
Пример #11
0
    def setUp(tc):
        # clean slate!
        cleanUp()

        # init event system
        from zope.component import event

        # register components
        provideHandler(notifyFieldEvent)
        provideAdapter(at_field_retrieval)
        provideSubscriptionAdapter(at_field_storage)
Пример #12
0
    def test_register_serializer(self):
        @adapter(IBlocks, IBrowserRequest)
        @implementer(IBlockFieldSerializationTransformer)
        class TestAdapterA:
            order = 10
            block_type = "test_multi"

            def __init__(self, context, request):
                self.context = context
                self.request = request

            def __call__(self, value):
                self.context._handler_called_a = True

                value["value"] = value["value"].replace("a", "b")

                return value

        @adapter(IBlocks, IBrowserRequest)
        @implementer(IBlockFieldSerializationTransformer)
        class TestAdapterB:
            order = 11
            block_type = "test_multi"

            def __init__(self, context, request):
                self.context = context
                self.request = request

            def __call__(self, value):
                self.context._handler_called_b = True

                value["value"] = value["value"].replace("b", "c")

                return value

        provideSubscriptionAdapter(TestAdapterA, (IDexterityItem, IBrowserRequest))
        provideSubscriptionAdapter(TestAdapterB, (IDexterityItem, IBrowserRequest))
        value = self.serialize(
            context=self.portal.doc1,
            blocks={"123": {"@type": "test_multi", "value": "a"}},
        )
        self.assertEqual(value["123"]["value"], "c")

        sm = getGlobalSiteManager()
        sm.adapters.unsubscribe(
            (IDexterityItem, IBrowserRequest),
            IBlockFieldSerializationTransformer,
            TestAdapterA,
        )
        sm.adapters.unsubscribe(
            (IDexterityItem, IBrowserRequest),
            IBlockFieldSerializationTransformer,
            TestAdapterB,
        )
Пример #13
0
    def setUp(self):
        from collective.jekyll.interfaces import ISymptom
        from collective.jekyll.interfaces import IIgnoredSymptomNames
        from collective.jekyll.ignored import IgnoredNames
        from collective.jekyll.symptoms import IdFormatSymptom
        from collective.jekyll.symptoms import TitleLengthSymptom

        zope.component.testing.setUp()
        provideAdapter(AttributeAnnotations, (ToDiagnose,), IAnnotations)
        provideAdapter(IgnoredNames, (ToDiagnose,), IIgnoredSymptomNames)
        provideSubscriptionAdapter(IdFormatSymptom, (ToDiagnose,), ISymptom)
        provideSubscriptionAdapter(TitleLengthSymptom, (ToDiagnose,), ISymptom)
Пример #14
0
    def setUp(self):
        from collective.jekyll.interfaces import ISymptom
        from collective.jekyll.interfaces import IIgnoredSymptomNames
        from collective.jekyll.ignored import IgnoredNames
        from collective.jekyll.symptoms import IdFormatSymptom
        from collective.jekyll.symptoms import TitleLengthSymptom

        zope.component.testing.setUp()
        provideAdapter(AttributeAnnotations, (ToDiagnose, ), IAnnotations)
        provideAdapter(IgnoredNames, (ToDiagnose, ), IIgnoredSymptomNames)
        provideSubscriptionAdapter(IdFormatSymptom, (ToDiagnose, ), ISymptom)
        provideSubscriptionAdapter(TitleLengthSymptom, (ToDiagnose, ),
                                   ISymptom)
    def test_add_map_layer(self):

        class BeautifulMap(MapLayer):
            name = "beautifulmap"
            title = "Beautiful Map"
            index = ViewPageTemplateFile('../browser/layers/osm.pt')

        provideSubscriptionAdapter(
            BeautifulMap, [int], IMapLayer
        )

        bm = BeautifulMap(self.portal, self.portal.REQUEST)
        self.assertTrue(bm.name, "beautifulmap")
        self.assertTrue('<script type="text/javascript"' in bm.index())
Пример #16
0
    def test_register_multiple_transform(self):
        @implementer(IBlockFieldDeserializationTransformer)
        @adapter(IBlocks, IBrowserRequest)
        class TestAdapterA(object):
            order = 10
            block_type = "test_multi"

            def __init__(self, context, request):
                self.context = context
                self.request = request

            def __call__(self, value):
                self.context._handler_called_a = True

                value["value"] = value["value"].replace(u"a", u"b")

                return value

        @implementer(IBlockFieldDeserializationTransformer)
        @adapter(IBlocks, IBrowserRequest)
        class TestAdapterB(object):
            order = 11
            block_type = "test_multi"

            def __init__(self, context, request):
                self.context = context
                self.request = request

            def __call__(self, value):
                self.context._handler_called_b = True

                value["value"] = value["value"].replace(u"b", u"c")

                return value

        provideSubscriptionAdapter(
            TestAdapterB,
            (IDexterityItem, IBrowserRequest),
        )

        provideSubscriptionAdapter(
            TestAdapterA,
            (IDexterityItem, IBrowserRequest),
        )

        self.deserialize(blocks={"123": {"@type": "test_multi", "value": u"a"}})

        self.assertTrue(self.portal.doc1._handler_called_a)
        self.assertTrue(self.portal.doc1._handler_called_b)
        self.assertEqual(self.portal.doc1.blocks["123"]["value"], u"c")
Пример #17
0
    def setUp(self):
        from collective.jekyll.interfaces import IDiagnosis
        from collective.jekyll.interfaces import ISymptom
        from collective.jekyll.diagnosis import Diagnosis
        from collective.jekyll.symptoms import SymptomBase

        testing.setUp(self)
        provideAdapter(Diagnosis, [int], IDiagnosis)

        class TestSymptom(SymptomBase):

            @property
            def isIgnored(self):
                return False

            @property
            def isActive(self):
                return True

        class PositiveSymptom(TestSymptom):
            title = "Positive"
            help = "Is positive."

            def _update(self):
                context = self.context
                counter.inc()
                self.status = context > 0
                if not self.status:
                    self.description = u"Is zero or negative."

        provideSubscriptionAdapter(
            PositiveSymptom, [int], ISymptom
        )

        class GreaterThanOneSymptom(TestSymptom):
            title = "Greater than one"
            help = title

            def _update(self):
                context = self.context
                self.status = context > 1
                if not self.status:
                    self.description = u"Is smaller than one."

        provideSubscriptionAdapter(
            GreaterThanOneSymptom, [int], ISymptom
        )
        counter.clear()
    def test_post_validation(self):
        @implementer(IObjectPostValidation)
        @adapter(IBaseObject)
        class PostValidator(object):
            def __init__(self, context):
                self.context = context

            def __call__(self, request):
                return {'post': 'post_validation_error'}

        provideSubscriptionAdapter(PostValidator)

        with self.assertRaises(BadRequest) as cm:
            self.deserialize(body='{"testRequiredField": "My Value"}',
                             validate_all=True)

        self.assertEquals('post_validation_error',
                          cm.exception.message[0]['message'])
Пример #19
0
    def test_pre_validation(self):
        @implementer(IObjectPreValidation)
        @adapter(IBaseObject)
        class PreValidator(object):
            def __init__(self, context):
                self.context = context

            def __call__(self, request):
                return {"pre": "pre_validation_error"}

        provideSubscriptionAdapter(PreValidator)

        with self.assertRaises(BadRequest) as cm:
            self.deserialize(
                body='{"testRequiredField": "My Value"}', validate_all=True
            )

        self.assertEqual("pre_validation_error", cm.exception.args[0][0]["message"])
Пример #20
0
    def setUp(self):
        from collective.jekyll.interfaces import IDiagnosis
        from collective.jekyll.interfaces import ISymptom
        from collective.jekyll.interfaces import IIsActive
        from collective.jekyll.diagnosis import Diagnosis
        from collective.jekyll.symptoms import SymptomBase

        testing.setUp(self)
        provideAdapter(Diagnosis, [int], IDiagnosis)

        class AlwaysActive(object):
            def __init__(self, context):
                self.context = context

            isActive = True

        provideAdapter(AlwaysActive, [ISymptom], IIsActive)

        class PositiveSymptom(SymptomBase):
            title = "Positive"
            help = "Is positive."

            def _update(self):
                context = self.context
                counter.inc()
                self.status = context > 0
                if not self.status:
                    self.description = u"Is zero or negative."

        provideSubscriptionAdapter(PositiveSymptom, [int], ISymptom)

        class GreaterThanOneSymptom(SymptomBase):
            title = "Greater than one"
            help = title

            def _update(self):
                context = self.context
                self.status = context > 1
                if not self.status:
                    self.description = u"Is smaller than one."

        provideSubscriptionAdapter(GreaterThanOneSymptom, [int], ISymptom)
        counter.clear()
    def test_post_validation(self):

        @implementer(IObjectPostValidation)
        @adapter(IBaseObject)
        class PostValidator(object):

            def __init__(self, context):
                self.context = context

            def __call__(self, request):
                return {'post': 'post_validation_error'}

        provideSubscriptionAdapter(PostValidator)

        with self.assertRaises(BadRequest) as cm:
            self.deserialize(body='{"testRequiredField": "My Value"}',
                             validate_all=True)

        self.assertEquals(
            'post_validation_error', cm.exception.message[0]['message'])
Пример #22
0
def GenerateCollectionTraversal(ctx):

    collection_names = []
    for k, v in ctx.domain_model.__dict__.items():
        if IManagedContainer.providedBy(v):
            collection_names.append(k)

    if not collection_names:
        return

    traverser = CollectionTraverser(*collection_names)

    # provideSubscriptionAdapter(factory, adapts=None, provides=None)
    component.provideSubscriptionAdapter(traverser,
                                         adapts=(ctx.domain_interface,
                                                 IPublisherRequest),
                                         provides=ITraverserPlugin)
    # provideAdapter(factory, adapts=None, provides=None, name="")
    component.provideAdapter(PluggableTraverser,
                             adapts=(ctx.domain_interface, IPublisherRequest),
                             provides=IPublishTraverse)
Пример #23
0
def GenerateCollectionTraversal(ctx):
    
    collection_names = []
    for k,v in ctx.domain_model.__dict__.items():
        if IManagedContainer.providedBy(v):
            collection_names.append(k)

    if not collection_names:
        return
    
    traverser = CollectionTraverser(*collection_names)
    
    # provideSubscriptionAdapter(factory, adapts=None, provides=None)
    component.provideSubscriptionAdapter(traverser, 
        adapts=(ctx.domain_interface, IPublisherRequest), 
        provides=ITraverserPlugin)
    # provideAdapter(factory, adapts=None, provides=None, name="")
    component.provideAdapter(PluggableTraverser,
        adapts=(ctx.domain_interface, IPublisherRequest),
        provides=IPublishTraverse
    )
    def test_decorator(self):
        class ITest(interface.Interface):  # pylint:disable=inherit-non-class
            pass

        @interface.implementer(ITest, IExternalObject)
        class Test(object):
            def toExternalObject(self, **unused_kwargs):
                return {}

        test = Test()
        assert_that(toExternalObject(test), is_({}))

        @interface.implementer(IExternalObjectDecorator)
        class Decorator(object):
            def __init__(self, o):
                pass

            def decorateExternalObject(self, obj, result):
                result['test'] = obj

        component.provideSubscriptionAdapter(Decorator, adapts=(ITest, ))

        assert_that(toExternalObject(test), is_({'test': test}))
Пример #25
0
def generate_collection_traversal(ti):
    
    # !+ merge with utils.get_managed_containers?
    def get_collection_names(domain_model):
        return [ k for k, v in domain_model.__dict__.items()
            if IManagedContainer.providedBy(v) ]
    
    collection_names = get_collection_names(ti.domain_model)
    
    if not collection_names:
        return
    # Note: the templated CollectionTraverser TYPE returned by this is
    # instantiated per "inherited and catalysed" descriptor e.g. for Motion, 
    # it is instantiated once for DocDescriptor and once for MotionDescriptor.
    traverser = CollectionTraverser(*collection_names)
    
    # provideSubscriptionAdapter(factory, adapts=None, provides=None)
    component.provideSubscriptionAdapter(traverser, 
        adapts=(ti.derived_table_schema, IPublisherRequest), 
        provides=ITraverserPlugin)
    # provideAdapter(factory, adapts=None, provides=None, name="")
    component.provideAdapter(PluggableTraverser,
        adapts=(ti.derived_table_schema, IPublisherRequest),
        provides=IPublishTraverse)
Пример #26
0
            res.append(u'virt:no')

        config = get_config()
        if config.has_section('netenv-tags'):
            for tag, nets in config.items('netenv-tags'):
                try:
                    if (self.context.ipv4_address is not None and
                        len(netaddr.all_matching_cidrs(self.context.ipv4_address.split('/')[0],
                                                       nets.split(','))) > 0):
                        res.append(u'env:' + tag)
                except ValueError:
                    # graceful ignoring of incorrect ips
                    pass
        return res


class VirtualComputeLocation(Adapter):
    implements(ILocation)
    context(IVirtualCompute)

    def get_url(self):
        return '/computes/%s/' % (self.context.__name__)


provideAdapter(adapter_value(['cpu_usage', 'memory_usage', 'network_usage', 'diskspace_usage']),
               adapts=(Compute, ), provides=IMetrics)


provideSubscriptionAdapter(ActionsContainerExtension, adapts=(Compute, ))
provideSubscriptionAdapter(MetricsContainerExtension, adapts=(Compute, ))
Пример #27
0
            self.write(line)

        if recursive and IContainer.providedBy(obj) and not self.opts_dir:
            for ch in container:
                child_obj = obj[ch.__name__]
                if (IContainer.providedBy(child_obj)
                        and not isinstance(child_obj, Symlink)
                        and child_obj not in self.visited):
                    self.write("\n%s:\n" %
                               os.path.join(path, ch.__name__.encode('utf8')))
                    self._do_ls(child_obj,
                                os.path.join(path, ch.__name__),
                                recursive=True)


provideSubscriptionAdapter(CommonArgs, adapts=(ListDirContentsCmd, ))


class PrintWorkDirCmd(Cmd):
    command('pwd')

    def execute(self, args):
        self.write('%s\n' % self.protocol._cwd())


class CatObjectCmd(Cmd):
    implements(ICmdArgumentsSyntax)

    command('cat')

    def arguments(self):
Пример #28
0
def handle_setting(key, text, alt_text, default):
    def accessControlSettingFactory(context=None):
        return AccessControlSetting(key, text, alt_text, default)
    provideSubscriptionAdapter(accessControlSettingFactory,
                               adapts=[None],
                               provides=IAccessControlSetting)
Пример #29
0
                if iterable:
                    log.msg('\t CMD: %s %s' % (item.__name__, item.cmdline),
                            system='proc',
                            logLevel=logging.DEBUG)
                    for s in item.subject:
                        log.msg('\t\tSUBJ: %s, %s' %
                                (s.__name__
                                 == self.__parent__.__name__, self.__parent__),
                                system='proc',
                                logLevel=logging.DEBUG)

                if iterable and self.__parent__.__name__ in map(
                        lambda s: s.__name__, item.subject):
                    tasks[name] = Symlink(name, item)

                if name not in seen:
                    seen.add(name)
                    collect(item)

        collect(processes)
        return tasks


class ComputeTasksInjector(ContainerInjector):
    context(Compute)
    __class__ = ComputeTasks


provideSubscriptionAdapter(ByNameContainerExtension, adapts=(Computes, ))
Пример #30
0
    def complete(self, token, parsed, parser, **kwargs):
        action = self.expected_action(parsed, parser)
        if action and action.choices:
            return [value for value in action.choices if value.startswith(token)]


class EnvironmentCompleter(Completer):
    baseclass()

    def complete(self, token, parsed, parser, protocol=None, **kwargs):
        return [value for value in protocol.environment.keys() if value.startswith(token)]


# TODO: move to handler
for command in [commands.ListDirContentsCmd, commands.ChangeDirCmd, commands.CatObjectCmd, commands.SetAttrCmd, commands.RemoveCmd, commands.MoveCmd, commands.FileCmd, commands.EchoCmd, commands.LinkCmd, commands.EditCmd, security.GetAclCmd, security.SetAclCmd, security.PermCheckCmd]:
    provideSubscriptionAdapter(PathCompleter, adapts=(command, ))

for command in [commands.ListDirContentsCmd, commands.ChangeDirCmd, commands.CatObjectCmd, commands.SetAttrCmd, commands.RemoveCmd, commands.QuitCmd, commands.FileCmd, commands.LinkCmd, commands.KillTaskCmd, security.GetAclCmd, security.SetAclCmd, security.PermCheckCmd]:
    provideSubscriptionAdapter(ArgSwitchCompleter, adapts=(command, ))

for command in [commands.SetAttrCmd, commands.CreateObjCmd]:
    provideSubscriptionAdapter(KeywordSwitchCompleter, adapts=(command, ))

for command in [commands.SetAttrCmd, commands.CreateObjCmd]:
    provideSubscriptionAdapter(KeywordValueCompleter, adapts=(command, ))

for command in [commands.HelpCmd, commands.CreateObjCmd]:
    provideSubscriptionAdapter(PositionalChoiceCompleter, adapts=(command, ))

for command in [commands.SetEnvCmd]:
    provideSubscriptionAdapter(EnvironmentCompleter, adapts=(command, ))
Пример #31
0
    def display_name(self):
        return self.name

    @property
    def nicknames(self):
        return [self.name]

    def __str__(self):
        return '<RootStorageContainer ObjectID=%s name=%s>' % (self.oid, self.name)

    @property
    def type(self):
        return RootStorageContainer


class DataObjectsRootInjector(ContainerInjector):
    context(OmsRoot)
    __class__ = RootStorageContainer


class ObjectIDContainerInjector(ContainerInjector):
    context(RootStorageContainer)
    __class__ = ObjectIdContainer


provideSubscriptionAdapter(ActionsContainerExtension, adapts=(RootStorageContainer, ))
provideSubscriptionAdapter(ByNameContainerExtension, adapts=(RootStorageContainer, ))

provideSubscriptionAdapter(ActionsContainerExtension, adapts=(StorageContainer, ))
provideSubscriptionAdapter(ByNameContainerExtension, adapts=(StorageContainer, ))
Пример #32
0
    def get(self, ip):
        return self._items.get(int(ip))

    def use(self, ip):
        self._items[int(ip)] = IPAddressStorable(self, int(ip))

    def free(self, ip):
        log.msg('Deallocating IP %s from the pool %s' % (ip, self), system='ippool')
        del self._items[int(ip)]

    def validate(self):
        assert int(self.minimum) <= int(self.maximum),\
                'Minimum IP value must be smaller or equal to max IP value'

provideSubscriptionAdapter(ActionsContainerExtension, adapts=(IPv4Pool, ))

class IPv4Pools(Container):
    __contains__ = IPv4Pool
    __name__ = 'ippools'

    def __init__(self):
        super(IPv4Pools, self).__init__()

    def find_pool(self, ip):
        ip = netaddr.IPAddress(ip)
        for n, pool in self._items.iteritems():
            if int(pool.minimum) <= int(ip) and int(pool.maximum) >= int(ip):
                return pool

    def find_intersections(self, pool):
        def collect(container):
            from opennode.knot.model.machines import Machines

            seen = set()
            for item in container.listcontent():
                if ICompute.providedBy(item) and item.ctid is not None:
                    computes[str(item.ctid)] = Symlink(str(item.ctid), item)

                if (isinstance(item, Machines) or isinstance(item, Computes) or
                        ICompute.providedBy(item) or IVirtualizationContainer.providedBy(item)):
                    if item.__name__ not in seen:
                        seen.add(item.__name__)
                        collect(item)

        collect(machines)
        return computes


class OpenVZContainerExtension(Subscription):
    implements(IContainerExtender)
    baseclass()

    def extend(self):
        return {'openvz': OpenVZContainer(self.context)}


provideSubscriptionAdapter(OpenVZContainerExtension, adapts=(Computes, ))
provideSubscriptionAdapter(ActionsContainerExtension, adapts=(VirtualizationContainer, ))
provideSubscriptionAdapter(ByNameContainerExtension, adapts=(VirtualizationContainer, ))
Пример #34
0
    def display_name(self):
        return self.name

    def __repr__(self):
        return "UserProfile('%s', %s, %s, %s, '%s', %s)" % (self.__name__, self.groups,
                                                            self.credit, self.balance_limit,
                                                            self.credit_timestamp, self.uid)


class IHome(Interface):
    """ User profile container """
    pass


class Home(Container):
    implements(IHome)
    __contains__ = UserProfile
    __name__ = 'home'

    def _new_id(self):
        raise TypeError('This container does not support generated IDs')


class HomeRootInjector(ContainerInjector):
    context(OmsRoot)
    __class__ = Home


provideSubscriptionAdapter(ActionsContainerExtension, adapts=(UserProfile, ))
Пример #35
0
from opennode.oms.endpoint.ssh.cmd.base import Cmd
from opennode.oms.endpoint.ssh.cmd.completers import PathCompleter
from opennode.oms.endpoint.ssh.cmd.directives import command
from opennode.oms.endpoint.ssh.cmdline import ICmdArgumentsSyntax, VirtualConsoleArgumentParser
from opennode.oms.zodb import db


class PingCmd(Cmd):
    implements(ICmdArgumentsSyntax)
    command('ping')

    def arguments(self):
        parser = VirtualConsoleArgumentParser()
        parser.add_argument('host', nargs='+', help="Host or compute object")
        return parser

    @db.transact
    def execute(self, args):
        for arg in args.host:
            obj = self.traverse(arg)
            if ICompute.providedBy(obj):
                address = obj.hostname.encode('utf-8')
            else:
                address = arg
            res = ping(address)
            self.write("%s is %s\n" % (address, ["unreachable", "alive"][res]))


for cmd in [PingCmd]:
    provideSubscriptionAdapter(PathCompleter, adapts=(cmd, ))
        for line in format_(summary_):
            logger.info(line)
        log.msg('Profiling memory done', system=self.__name__)
        return defer.succeed(None)

    def track_changes(self):
        log.msg('Profiling memory (tracking changes)...', system=self.__name__)
        logger.info('Change summary follows')
        summary_ = self.summary_tracker.diff()
        for line in format_(summary_):
            logger.info(line)
        log.msg('Profiling memory (tracking changes) done', system=self.__name__)
        return defer.succeed(None)


provideSubscriptionAdapter(subscription_factory(MemoryProfilerDaemonProcess), adapts=(Proc,))


def format_(rows, limit=15, sort='size', order='descending'):
    """Format the rows as a summary.

    Keyword arguments:
    limit -- the maximum number of elements to be listed
    sort  -- sort elements by 'size', 'type', or '#'
    order -- sort 'ascending' or 'descending'

    Heavily based on pympler.summary.print_
    """
    localrows = []
    for row in rows:
        localrows.append(list(row))
Пример #37
0
    def display_name(self):
        return self.name

    def __repr__(self):
        return "UserProfile('%s', %s, %s, %s, '%s', %s)" % (
            self.__name__, self.groups, self.credit, self.balance_limit,
            self.credit_timestamp, self.uid)


class IHome(Interface):
    """ User profile container """
    pass


class Home(Container):
    implements(IHome)
    __contains__ = UserProfile
    __name__ = 'home'

    def _new_id(self):
        raise TypeError('This container does not support generated IDs')


class HomeRootInjector(ContainerInjector):
    context(OmsRoot)
    __class__ = Home


provideSubscriptionAdapter(ActionsContainerExtension, adapts=(UserProfile, ))
Пример #38
0
        if not self.try_index(searcher, model, event):
            log.msg("cannot (un)index %s %s" % (model, type(event).__name__), system="indexer")

    def try_index(self, searcher, model, event):
        path = canonical_path(model)
        op = 'un' if IModelDeletedEvent.providedBy(event) else ''

        log.msg("%sindexing %s %s" % (op, path, type(event).__name__), system="indexer")

        objs, unresolved_path = traverse_path(db.get_root()['oms_root'], path)
        if unresolved_path and not IModelDeletedEvent.providedBy(event):
            return False

        obj = objs[-1]

        try:
            if IModelDeletedEvent.providedBy(event):
                searcher.unindex_object(obj)
            else:
                searcher._index_object(obj)
        except NotYet:
            return False

        log.msg("%sindexed %s %s" % (op, path, type(event).__name__), system="indexer")
        return True

    def reindex(self):
        ReindexAction(None).execute(DetachedProtocol(), object())

provideSubscriptionAdapter(subscription_factory(IndexerDaemonProcess), adapts=(Proc,))
Пример #39
0
 def _subscription_adapter(factory):
     component.provideSubscriptionAdapter(factory, adapts, provides)
     return factory
Пример #40
0
        config = get_config()
        self.interval = config.getint('db', 'pack_interval')

    @defer.inlineCallbacks
    def run(self):
        while True:
            try:
                if not self.paused:
                    yield self.pack()
            except Exception:
                import traceback
                traceback.print_exc()
                pass

            yield async_sleep(self.interval)

    @db.ro_transact
    def pack(self):
        storage_type = get_config().get('db', 'storage_type')

        if storage_type == 'zeo':
            print "[db_pack] zeo pack not implemented yet, please setup cron to run bin/zeopack -u db/socket"
        elif storage_type == 'embedded':
            d = db.get_db()
            d.pack(time.time())


provideSubscriptionAdapter(subscription_factory(PackDaemonProcess),
                           adapts=(Proc, ))
Пример #41
0
            res[name] = Symlink(name, item)
        return res


class SearchByTagContainer(AddingContainer):
    __name__ = 'by-tag'
    __contains__ = ITag

    def __init__(self, parent):
        self.__parent__ = parent

    @property
    def tags(self):
        return list(self.__parent__.catalog['tags']._fwd_index.keys())

    @property
    def _items(self):
        res = {}
        tag_set = OOTreeSet(self.tags)
        for i in self.tags:
            res[i] = Tag(i, self.__parent__, self, tag_set, [])
        return res

    def _add(self, item):
        self.tags.add(item.__name__)
        return item.__name__


provideAdapter(KeyReferenceToPersistent, adapts=(Model,))
provideSubscriptionAdapter(ActionsContainerExtension, adapts=(SearchContainer, ))
Пример #42
0
        ssh_connect_interactive_shell('root', phy.hostname, 22, self.transport, self._set_channel,
                                      size, self.command)


class TtyAttachAction(HypervisorSshAttachAction):
    context(ITtyConsole)

    @property
    def command(self):
        return 'screen -xRR %s %s' % (self.context.pty.replace('/', ''), self.context.pty)

    @property
    def name(self):
        return self.context.pty.encode('utf-8')


class OpenvzAttachAction(HypervisorSshAttachAction):
    context(IOpenVzConsole)

    @property
    def command(self):
        return 'vzctl enter %s' % (self.context.cid)

    @property
    def name(self):
        return self.context.cid


provideSubscriptionAdapter(ActionsContainerExtension, adapts=(IConsole, ))
Пример #43
0
            yield IVirtualizationContainer.providedBy(item)
            yield ICompute.providedBy(item)
            yield IVirtualCompute.providedBy(item)

        def collect(container):
            seen = set()
            for item in container.listcontent():
                if ITemplate.providedBy(
                        item) and item.__name__ not in templates:
                    templates[item.__name__] = Symlink(item.__name__, item)

                if any(allowed_classes_gen(item)):
                    if item.__name__ not in seen:
                        seen.add(item.__name__)
                        collect(item)

        collect(machines)
        return templates


class TemplatesRootInjector(ContainerInjector):
    context(OmsRoot)
    __class__ = GlobalTemplates


provideSubscriptionAdapter(ActionsContainerExtension,
                           adapts=(GlobalTemplates, ))
provideSubscriptionAdapter(ByNameContainerExtension, adapts=(Templates, ))
provideSubscriptionAdapter(ByNameContainerExtension,
                           adapts=(GlobalTemplates, ))
Пример #44
0
class ContinueTaskAction(SignalAction):
    """Send CONT signal"""

    context(ISuspendableTask)
    action('continue')

    __signal__ = 'CONT'


class TerminateTaskAction(SignalAction):
    """Send TERM signal"""

    context(ITask)
    action('terminate')

    __signal__ = 'TERM'


provideSubscriptionAdapter(ActionsContainerExtension, adapts=(Task, ))


@subscribe(IAfterApplicationInitializedEvent)
def start_daemons(event):
    try:
        Proc().start_daemons()
    except Exception as e:
        log.msg("Got exception while starting daemons", system='proc')
        if get_config().getboolean('debug', 'print_exceptions'):
            log.err(e, system='proc')
Пример #45
0
                                                           '(after timeout!)' if targetkey in
                                                           self.outstanding_requests else ''),
                                                           logLevel=logging.DEBUG)
                d = g.gather()
                curtime = datetime.datetime.now().isoformat()
                self.outstanding_requests[targetkey] = [d, curtime, 0, g]
                d.addCallback(handle_success, g.context)
                d.addErrback(handle_errors, g.context)
            else:
                self.outstanding_requests[targetkey][2] += 1
                self.log_msg('Skipping: another outstanding request to "%s" (%s) is found from %s.' %
                             (g.context, hostname, self.outstanding_requests[targetkey][1]),
                             logLevel=logging.DEBUG)


provideSubscriptionAdapter(subscription_factory(MetricsDaemonProcess), adapts=(Proc,))


class VirtualComputeMetricGatherer(Adapter):
    """Gathers VM metrics using IVirtualizationContainerSubmitter"""

    implements(IMetricsGatherer)
    context(IManageable)

    @defer.inlineCallbacks
    def gather(self):
        self._killhook = defer.Deferred()
        yield self.gather_vms()
        yield self.gather_phy()

    def kill(self):
Пример #46
0
            yield async_sleep(self.interval)

    @defer.inlineCallbacks
    def ping_check(self):

        @db.ro_transact
        def get_computes():
            oms_root = db.get_root()['oms_root']
            res = [(i, i.hostname)
                   for i in map(follow_symlinks, oms_root['computes'].listcontent())
                   if ICompute.providedBy(i)]

            return res

        ping_actions = []
        for i, hostname in (yield get_computes()):
            action = PingCheckAction(i)
            d = action.execute(DetachedProtocol(), object())
            ping_actions.append((hostname, d))

        def handle_errors(e, c):
            e.trap(Exception)
            log.msg("Got exception when pinging compute '%s': %s" % (c, e), system='ping-check')
            if get_config().getboolean('debug', 'print_exceptions'):
                log.err(system='ping-check')

        for c, deferred in ping_actions:
            deferred.addErrback(handle_errors, c)

provideSubscriptionAdapter(subscription_factory(PingCheckDaemonProcess), adapts=(Proc,))
            seen = set()
            for item in container.listcontent():
                if ICompute.providedBy(item) and item.ctid is not None:
                    computes[str(item.ctid)] = Symlink(str(item.ctid), item)

                if (isinstance(item, Machines) or isinstance(item, Computes)
                        or ICompute.providedBy(item)
                        or IVirtualizationContainer.providedBy(item)):
                    if item.__name__ not in seen:
                        seen.add(item.__name__)
                        collect(item)

        collect(machines)
        return computes


class OpenVZContainerExtension(Subscription):
    implements(IContainerExtender)
    baseclass()

    def extend(self):
        return {'openvz': OpenVZContainer(self.context)}


provideSubscriptionAdapter(OpenVZContainerExtension, adapts=(Computes, ))
provideSubscriptionAdapter(ActionsContainerExtension,
                           adapts=(VirtualizationContainer, ))
provideSubscriptionAdapter(ByNameContainerExtension,
                           adapts=(VirtualizationContainer, ))
Пример #48
0
            res.append(u'virt:no')

        config = get_config()
        if config.has_section('netenv-tags'):
            for tag, nets in config.items('netenv-tags'):
                try:
                    if (self.context.ipv4_address is not None and
                        len(netaddr.all_matching_cidrs(self.context.ipv4_address.split('/')[0],
                                                       nets.split(','))) > 0):
                        res.append(u'env:' + tag)
                except ValueError:
                    # graceful ignoring of incorrect ips
                    pass
        return res


class VirtualComputeLocation(Adapter):
    implements(ILocation)
    context(IVirtualCompute)

    def get_url(self):
        return '/computes/%s/' % (self.context.__name__)


provideAdapter(adapter_value(['cpu_usage', 'memory_usage', 'network_usage', 'diskspace_usage']),
               adapts=(Compute, ), provides=IMetrics)


provideSubscriptionAdapter(ActionsContainerExtension, adapts=(Compute, ))
provideSubscriptionAdapter(MetricsContainerExtension, adapts=(Compute, ))
Пример #49
0
        collect(machines)
        return computes

    def _add(self, item):
        # break an import cycle
        from opennode.oms.zodb import db
        machines = db.get_root()['oms_root']['machines']
        return (machines.hangar if IVirtualCompute.providedBy(item) else machines).add(item)

    def __delitem__(self, key):
        item = self._items[key]
        if isinstance(item, Symlink):
            del item.target.__parent__[item.target.__name__]


provideSubscriptionAdapter(ActionsContainerExtension, adapts=(Compute, ))
provideSubscriptionAdapter(ByNameContainerExtension, adapts=(Computes, ))


class ComputesRootInjector(ContainerInjector):
    context(OmsRoot)
    __class__ = Computes


class Machines(Container):
    __contains__ = Compute
    __name__ = 'machines'

    def __init__(self):
        super(Machines, self).__init__()
Пример #50
0
 def _subscription_adapter(factory):
     component.provideSubscriptionAdapter(factory, adapts, provides)
     return factory
Пример #51
0
            logger.info(line)
        log.msg('Profiling memory done', system=self.__name__)
        return defer.succeed(None)

    def track_changes(self):
        log.msg('Profiling memory (tracking changes)...', system=self.__name__)
        logger.info('Change summary follows')
        summary_ = self.summary_tracker.diff()
        for line in format_(summary_):
            logger.info(line)
        log.msg('Profiling memory (tracking changes) done',
                system=self.__name__)
        return defer.succeed(None)


provideSubscriptionAdapter(subscription_factory(MemoryProfilerDaemonProcess),
                           adapts=(Proc, ))


def format_(rows, limit=15, sort='size', order='descending'):
    """Format the rows as a summary.

    Keyword arguments:
    limit -- the maximum number of elements to be listed
    sort  -- sort elements by 'size', 'type', or '#'
    order -- sort 'ascending' or 'descending'

    Heavily based on pympler.summary.print_
    """
    localrows = []
    for row in rows:
        localrows.append(list(row))
Пример #52
0
class IncomingMachines(ReadonlyContainer):
    __name__ = 'incoming'


class IncomingMachinesInjector(ContainerInjector):
    context(Machines)
    __class__ = IncomingMachines


class BaseIncomingMachines(ReadonlyContainer):
    """Template method abstract class for stack-specific incoming machines list implementations"""

    def _get(self):
        """ Provide list of incoming host names """
        raise NotImplemented

    @property
    def _items(self):
        items = self._get()
        pending = dict((h, IncomingMachineRequest(h)) for h in items)
        return pending


class HangarMachinesInjector(ContainerInjector):
    context(Machines)
    __class__ = Hangar


provideSubscriptionAdapter(ByNameContainerExtension, adapts=(Machines, ))
provideSubscriptionAdapter(ActionsContainerExtension, adapts=(IncomingMachineRequest, ))