Example #1
0
 def test_methodAttr(self):
     class Child(rend.Page):
         pass
     class Parent(rend.Page):
         def child_now(self, request):
             return Child()
         def child_defer(self, request):
             return defer.succeed(None).addCallback(lambda x: Child())
     p = Parent()
     r = getResource(p, '/now')
     self.failUnless(compy.implements(r.tag, inevow.IResource))
     r = getResource(p, '/defer')
     self.failUnless(compy.implements(r.tag, inevow.IResource))
Example #2
0
 def test_getDynamicChild(self):
     class Child(rend.Page):
         pass
     class Parent(rend.Page):
         def getDynamicChild(self, name, request):
             if name == 'now':
                 return Child()
             if name == 'defer':
                 return defer.succeed(None).addCallback(lambda x: Child())
     p = Parent()
     r = getResource(p, '/now')
     self.failUnless(compy.implements(r.tag, inevow.IResource))
     r = getResource(p, '/defer')
     self.failUnless(compy.implements(r.tag, inevow.IResource))
Example #3
0
 def test_oldResource(self):
     from twisted.web import twcgi
     class Parent(rend.Page):
         child_child = twcgi.CGIScript('abc.cgi')
     p = Parent()
     r = getResource(p, '/child')
     self.failUnless(compy.implements(r.tag, inevow.IResource))
Example #4
0
 def test_resourceAttr(self):
     class Child(rend.Page):
         pass
     class Parent(rend.Page):
         child_child = Child()
     p = Parent()
     r = getResource(p, '/child')
     self.failUnless(compy.implements(r.tag, inevow.IResource))
Example #5
0
    def test_getDynamicChild(self):
        class Child(rend.Page):
            pass

        class Parent(rend.Page):
            def getDynamicChild(self, name, request):
                if name == 'now':
                    return Child()
                if name == 'defer':
                    return defer.succeed(None).addCallback(lambda x: Child())

        p = Parent()
        r = getResource(p, '/now')
        self.failUnless(compy.implements(r.tag, inevow.IResource))

        r = getResource(p, '/defer')
        self.failUnless(compy.implements(r.tag, inevow.IResource))
Example #6
0
 def __adapt__(other, default=None):
     from formless.annotate import TypedInterface
     if implements(other, TypedInterface):
         from formless.configurable import TypedInterfaceConfigurable
         return TypedInterfaceConfigurable(other)
     if hasattr(Interface, '__adapt__'):
         return Interface.__class__.__adapt__(IConfigurable, other)
     return default
Example #7
0
    def test_oldResource(self):
        from twisted.web import twcgi

        class Parent(rend.Page):
            child_child = twcgi.CGIScript('abc.cgi')

        p = Parent()
        r = getResource(p, '/child')
        self.failUnless(compy.implements(r.tag, inevow.IResource))
Example #8
0
 def test_inDict(self):
     class Child(rend.Page):
         pass
     class Parent(rend.Page):
         pass
     p = Parent()
     p.putChild('child', Child())
     r = getResource(p, '/child')
     self.failUnless(compy.implements(r.tag, inevow.IResource))
Example #9
0
    def test_resourceAttr(self):
        class Child(rend.Page):
            pass

        class Parent(rend.Page):
            child_child = Child()

        p = Parent()
        r = getResource(p, '/child')
        self.failUnless(compy.implements(r.tag, inevow.IResource))
Example #10
0
    def test_methodAttr(self):
        class Child(rend.Page):
            pass

        class Parent(rend.Page):
            def child_now(self, request):
                return Child()

            def child_defer(self, request):
                return defer.succeed(None).addCallback(lambda x: Child())

        p = Parent()
        r = getResource(p, '/now')

        self.failUnless(compy.implements(r.tag, inevow.IResource))

        r = getResource(p, '/defer')

        self.failUnless(compy.implements(r.tag, inevow.IResource))
Example #11
0
    def test_inDict(self):
        class Child(rend.Page):
            pass

        class Parent(rend.Page):
            pass

        p = Parent()
        p.putChild('child', Child())
        r = getResource(p, '/child')
        self.failUnless(compy.implements(r.tag, inevow.IResource))
Example #12
0
 def test_missingRemembrances(self):
     class IThing(compy.Interface):
         pass
     class Page(rend.Page):
         def render_it(self, ctx, data):
             return ctx.locate(IThing)
         def child_child(self, ctx):
             ctx.remember("Thing", IThing)
             return defer.succeed(Page())
         docFactory = loaders.stan(html[render_it])
     page = Page()
     r = getResource(page, '/child')
     self.failUnless(compy.implements(r.tag, inevow.IResource))
Example #13
0
 def getBindingNames(self, context):
     bindingNames = []
     self.bindingDict = bindingDict = {}
     interface = self.groupInterface
     for binding in getattr(interface, '__spec__', []):
         bindingDict[binding.name] = binding
         if binding.name not in bindingNames:
             bindingNames.append(binding.name)
         if compy.implements(binding.typedValue, IActionableType):
             acts = binding.typedValue.actions
             if acts is None:
                 acts = []
             for action in acts:
                 bindingDict[action.name] = action
     return bindingNames
Example #14
0
    def test_missingRemembrances(self):
        class IThing(compy.Interface):
            pass

        class Page(rend.Page):
            def render_it(self, ctx, data):
                return ctx.locate(IThing)

            def child_child(self, ctx):
                ctx.remember("Thing", IThing)
                return defer.succeed(Page())

            docFactory = loaders.stan(html[render_it])

        page = Page()
        r = getResource(page, '/child')
        self.failUnless(compy.implements(r.tag, inevow.IResource))
Example #15
0
 def getBindingNames(self, context):
     ## Todo: remove this getattr
     ifs = compy.getInterfaces(getattr(self, 'boundTo', self))
     ifs = [
         x for x in ifs if x is not IConfigurable and x is not TypedInterface
     ]
     bindingNames = []
     self.bindingDict = bindingDict = {}
     for interface in ifs:
         ## TypedInterfaces have a __spec__ attribute which is a list of all Typed properties and
         ## autocallable methods
         for binding in getattr(interface, '__spec__', []):
             bindingDict[binding.name] = binding
             if binding.name not in bindingNames:
                 bindingNames.append(binding.name)
             if compy.implements(binding.typedValue, IActionableType):
                 acts = binding.typedValue.actions
                 if acts is None:
                     acts = []
                 for action in acts:
                     bindingDict[action.name] = action
     return bindingNames
Example #16
0
    def __new__(cls, name, bases, dct):
        dct['__id__'] = nextId()
        dct['__methods__'] = methods = []
        dct['__properties__'] = properties = []
        possibleActions = []
        actionAttachers = []
        for key, value in dct.items():
            if key[0] == '_': continue

            if isinstance(value, MetaTypedInterface):
                ## A Nested TypedInterface indicates a GroupBinding
                properties.append(GroupBinding(key, value, value.__id__))
            elif callable(value):
                try:
                    result = value(_Marker)
                except:
                    ## Allow non-autocallable methods in the interface; ignore them
                    continue
                names, _, _, typeList = inspect.getargspec(value)

                if typeList is None:
                    argumentTypes = []
                else:
                    argumentTypes = [
                        Argument(n, argtype, argtype.id) for n, argtype in zip(names[1:], typeList)
                    ]
                label = None
                description = None
                if getattr(value, 'autocallable', None):
                    # autocallables have attributes that can set label and description
                    label = value.attributes.get('label', None)
                    description = value.attributes.get('description', None)

                adapted = iformless.ITyped(result, None)
                if adapted is None:
                    adapted = Object(result)

                # ITyped has label and description we can use
                if label is None:
                    label = adapted.label
                if description is None:
                    description = adapted.description

                defaultLabel, defaultDescription = labelAndDescriptionFromDocstring(value.__doc__)
                if defaultLabel is None:
                    # docstring had no label, try the action if it is an autocallable
                    if getattr(value, 'autocallable', None):
                        if label is None and value.action is not None:
                            # no explicit label, but autocallable has action we can use
                            defaultLabel = value.action

                if defaultLabel is None:
                    # final fallback: use the function name as label
                    defaultLabel = nameToLabel(key)
                    
                if label is None:
                    label = defaultLabel
                if description is None:
                    description = defaultDescription

                theMethod = Method(
                    adapted, argumentTypes, label=label, description=description
                )

                if getattr(value, 'autocallable', None):
                    methods.append(
                        MethodBinding(
                            key, theMethod, value.id, value.action, value.attributes))
                else:
                    possibleActions.append((value, MethodBinding(key, theMethod)))
            else:
                if not value.label:
                    value.label = nameToLabel(key)
                if implements(value, iformless.IActionableType):
                    actionAttachers.append(value)
                properties.append(
                    Property(key, value, value.id)
                )
        for attacher in actionAttachers:
            attacher.attachActionBindings(possibleActions)
        methods.sort(_sorter)
        properties.sort(_sorter)
        dct['__spec__'] = spec = methods + properties
        spec.sort(_sorter)
        dct['name'] = name

        # because attributes "label" and "description" would become Properties,
        # check for ones with an underscore prefix.
        dct['label'] = dct.get('_label', None)
        dct['description'] = dct.get('_description', None)
        defaultLabel, defaultDescription = labelAndDescriptionFromDocstring(dct.get('__doc__'))
        if defaultLabel is None:
            defaultLabel = nameToLabel(name)
        if dct['label'] is None:
            dct['label'] = defaultLabel
        if dct['description'] is None:
            dct['description'] = defaultDescription

        # What the heck...work around strange bug in Zope C Optimizations
        # whose cause I cannot figure out.
        try:
            return MetaInterface.__new__(cls, name, bases, dct)
        except TypeError, e:
            if len(e.args) == 1 and e.args[0] == '_interface_coptimizations.SpecificationBase.__new__(MetaTypedInterface) is not safe, use object.__new__()':
                return object.__new__(cls, name, bases, dct)
            raise
Example #17
0
    def __new__(cls, name, bases, dct):
        dct['__id__'] = nextId()
        dct['__methods__'] = methods = []
        dct['__properties__'] = properties = []
        possibleActions = []
        actionAttachers = []
        for key, value in dct.items():
            if key[0] == '_': continue

            if isinstance(value, MetaTypedInterface):
                ## A Nested TypedInterface indicates a GroupBinding
                properties.append(GroupBinding(key, value, value.__id__))
            elif callable(value):
                try:
                    result = value(_Marker)
                except:
                    ## Allow non-autocallable methods in the interface; ignore them
                    continue
                names, _, _, typeList = inspect.getargspec(value)

                if typeList is None:
                    argumentTypes = []
                else:
                    argumentTypes = [
                        Argument(n, argtype, argtype.id)
                        for n, argtype in zip(names[1:], typeList)
                    ]
                label = None
                description = None
                if getattr(value, 'autocallable', None):
                    # autocallables have attributes that can set label and description
                    label = value.attributes.get('label', None)
                    description = value.attributes.get('description', None)

                adapted = iformless.ITyped(result, None)
                if adapted is None:
                    adapted = Object(result)

                # ITyped has label and description we can use
                if label is None:
                    label = adapted.label
                if description is None:
                    description = adapted.description

                defaultLabel, defaultDescription = labelAndDescriptionFromDocstring(
                    value.__doc__)
                if defaultLabel is None:
                    # docstring had no label, try the action if it is an autocallable
                    if getattr(value, 'autocallable', None):
                        if label is None and value.action is not None:
                            # no explicit label, but autocallable has action we can use
                            defaultLabel = value.action

                if defaultLabel is None:
                    # final fallback: use the function name as label
                    defaultLabel = nameToLabel(key)

                if label is None:
                    label = defaultLabel
                if description is None:
                    description = defaultDescription

                theMethod = Method(adapted,
                                   argumentTypes,
                                   label=label,
                                   description=description)

                if getattr(value, 'autocallable', None):
                    methods.append(
                        MethodBinding(key, theMethod, value.id, value.action,
                                      value.attributes))
                else:
                    possibleActions.append(
                        (value, MethodBinding(key, theMethod)))
            else:
                if not value.label:
                    value.label = nameToLabel(key)
                if implements(value, iformless.IActionableType):
                    actionAttachers.append(value)
                properties.append(Property(key, value, value.id))
        for attacher in actionAttachers:
            attacher.attachActionBindings(possibleActions)
        methods.sort(_sorter)
        properties.sort(_sorter)
        dct['__spec__'] = spec = methods + properties
        spec.sort(_sorter)
        dct['name'] = name

        # because attributes "label" and "description" would become Properties,
        # check for ones with an underscore prefix.
        dct['label'] = dct.get('_label', None)
        dct['description'] = dct.get('_description', None)
        defaultLabel, defaultDescription = labelAndDescriptionFromDocstring(
            dct.get('__doc__'))
        if defaultLabel is None:
            defaultLabel = nameToLabel(name)
        if dct['label'] is None:
            dct['label'] = defaultLabel
        if dct['description'] is None:
            dct['description'] = defaultDescription

        # What the heck...work around strange bug in Zope C Optimizations
        # whose cause I cannot figure out.
        try:
            return MetaInterface.__new__(cls, name, bases, dct)
        except TypeError, e:
            if len(e.args) == 1 and e.args[
                    0] == '_interface_coptimizations.SpecificationBase.__new__(MetaTypedInterface) is not safe, use object.__new__()':
                return object.__new__(cls, name, bases, dct)
            raise