Пример #1
0
    def __new__(mcls, name, bases, ns):
        """
        Creates an ArsdkEnum type and its associated bitfield type.
        All equivalent enum types are derived from a common based enum class.
        Two enum types are considered equal if they define the same labels.
        """
        if ArsdkEnumMeta._base is None:
            cls = _EnumBase.__class__.__new__(mcls, builtin_str(name),
                                              (_EnumBase, ), ns)
            ArsdkEnumMeta._base = cls
        else:
            # Arsdk enums may have aliases.
            # For example the following enums found in ardrone3.xml and rth.xml
            # should be comparable
            #
            # <arg name="state" type="enum">
            #     <enum name="available"></enum>
            #     <enum name="inProgress"></enum>
            #     <enum name="unavailable"></enum>
            #     <enum name="pending"></enum>
            # </arg>
            # <enum name="state">
            #     <value name="available"></value>
            #     <value name="in_progress"></value>
            #     <value name="unavailable"></value>
            #     <value name="pending"></value>
            # </enum>
            #
            # Notice the subtle difference between the two labels
            # "state.inProgress" vs "state.in_progress"
            #
            # This modules defines one enum type for each xml definition and
            # automatically define enum aliases for equivalent definitions.
            # The following code handles these aliases so that the user should
            # not be bothered with this. Enum types that have the same
            # ArsdkEnumAlias_* base class are comparable with each others.

            class_key = (name, ) + tuple(
                (starmap(lambda k, v: k + "_" + str(v), ns.items())))
            cls = mcls._classes.get(class_key)
            if cls is not None:
                return cls

            alias_key = tuple(
                starmap(lambda k, v: (k.replace('_', '').lower(), v),
                        ns.items()))
            alias_name = (label + "_" + str(value)
                          for label, value in alias_key)
            alias_name = str("ArsdkEnumAlias_" + '_'.join(alias_name))

            if alias_key not in mcls._aliases:
                alias_base = _EnumBase.__class__.__new__(
                    mcls, builtin_str(alias_name), (ArsdkEnumMeta._base, ), {})
                mcls._aliases[alias_key] = alias_base
            else:
                alias_base = mcls._aliases[alias_key]
            cls = _EnumBase.__class__.__new__(mcls, builtin_str(name),
                                              (alias_base, ), ns)
            mcls._classes[class_key] = cls
        return cls
Пример #2
0
 def find_number_in_words(self, word):
     match = False
     try:
         match = w2n.word_to_num(builtin_str(word))
     except ValueError:
         pass
     return match
Пример #3
0
def getAppForm(application_id, user):
    fields = {}
    application = Application.objects.get(id=application_id)
    if application.input_type == 'upload':
        fields['File Upload'] = FileField()
    for option in \
        ApplicationOptions.objects.filter(application=application).order_by('id'
            ):
        try: # check for a special fileInput option
            value = ApplicationOptionsList.objects.get(name='fileInput'
                    , category=option)
            getJobList(user)
            fields[option.name] = \
                ModelChoiceField(queryset=Job.objects.filter(application__output_type=value.realName,
                                 user=user,
                                 status=Job.FINISHED).order_by('-id'),
                                 empty_label='None', required=False)
            continue
        except:
            pass
        try: # check for a special stringInput option
            value = ApplicationOptionsList.objects.get(name='stringInput', category=option)
            fields[option.name] = CharField(max_length=2056, widget=forms.Textarea(attrs={'cols': 200, 'rows': 1,}))
            continue
        except:
            pass
        fields[option.name] = \
            ModelChoiceField(queryset=ApplicationOptionsList.objects.filter(category=option).order_by('id'
                             ), empty_label=None)
    fields['application'] = IntegerField(initial=application.id,
            widget=HiddenInput())

    return type(builtin_str(application.name+"Form"), (Form, ), fields)
Пример #4
0
    def build_operator_method(method, operator, docstring, cls):
        """Return a method definition for a numerical operator.

        Keyword arguments:
        method -- name of the operator method of a subclass of *cls*, will used for
                  <operator>.__name__ for clean output in the class documentation.
        operator -- function of two arguments that is applied to the original function
                    result and a given operand.
        docstring -- docstring for the new operator method.
        cls -- class of which the new dynamic class will be subclassed.
        """

        def patch_members(self, other):
            """Return new object of class *newclassname* with patched members.

               Creates a new virtual class with the members in *members* patched to apply
               the given *operator* to the original function definition.
            """
            newmembers = {member: decorate(decorator=operate,
                                           base=getattr(self, member),
                                           operator=operator,
                                           operand=other) for member in members}
            newclass = type(builtin_str(newclassname), (cls,), newmembers)
            return newclass(*[getattr(self, attr) for attr in attributes])
        patch_members.__name__ = builtin_str(method)
        patch_members.__doc__ = docstring
        return patch_members
Пример #5
0
    def map_type(self, uri, store=None, classes=None):
        """ Create and return a `class` based on the `uri` given.

        Also will add the `classes` to the inheritance list.

        """

        classes = classes if isinstance(classes, (tuple, set, list)) else []
        store = store if store else self.default_store_key

        uri = self._uri(uri)
        if not uri:
            return None
        name = uri_to_classname(uri)

        base_classes = [Resource]
        base_classes.extend(classes)

        # Also take classes from session.mapping
        session_classes = self.mapping.get(uri, [])
        if type(session_classes) not in [list, tuple, set]:
            session_classes = [session_classes]
        base_classes.extend(session_classes)

        return type(builtin_str(name), tuple(base_classes), {
            'uri': uri,
            'store_key': store,
            'session': self
        })
Пример #6
0
 def __new__(mcls, enum_type, *args, **kwds):
     """
     Creates an ArsdkBitfield type from its associated enum type
     """
     if ArsdkBitfieldMeta._base is None:
         cls = type.__new__(ArsdkBitfieldMeta, builtin_str("ArsdkBitfield"),
                            *args, **kwds)
         mcls._base = cls
     else:
         cls = mcls._classes.get(enum_type)
         if cls is not None:
             return cls
         cls = type.__new__(mcls,
                            builtin_str(enum_type.__name__ + "_Bitfield"),
                            (mcls._base, ), dict(_enum_type_=enum_type))
         mcls._classes[enum_type] = cls
     return cls
Пример #7
0
        def patch_members(self, other):
            """Return new object of class *newclassname* with patched members.

               Creates a new virtual class with the members in *members* patched to apply
               the given *operator* to the original function definition.
            """
            newmembers = {member: decorate(decorator=operate,
                                           base=getattr(self, member),
                                           operator=operator,
                                           operand=other) for member in members}
            newclass = type(builtin_str(newclassname), (cls,), newmembers)
            return newclass(*[getattr(self, attr) for attr in attributes])
Пример #8
0
def getAppForm(application_id, user):
    fields = {}
    application = Application.objects.get(id=application_id)
    if application.input_type == 'upload':
        fields['File Upload'] = FileField()
    for option in \
        ApplicationOptions.objects.filter(application=application).order_by('id'
            ):
        try:  # check for a special fileInput option
            value = ApplicationOptionsList.objects.get(name='fileInput',
                                                       category=option)
            getJobList(user)
            fields[option.name] = \
                ModelChoiceField(queryset=Job.objects.filter(application__output_type=value.realName,
                                 user=user,
                                 status=Job.FINISHED).order_by('-id'),
                                 empty_label='None', required=False)
            continue
        except:
            pass
        try:  # check for a special stringInput option
            value = ApplicationOptionsList.objects.get(name='stringInput',
                                                       category=option)
            fields[option.name] = CharField(max_length=2056,
                                            widget=forms.Textarea(attrs={
                                                'cols': 200,
                                                'rows': 1,
                                            }))
            continue
        except:
            pass
        fields[option.name] = \
            ModelChoiceField(queryset=ApplicationOptionsList.objects.filter(category=option).order_by('id'
                             ), empty_label=None)
    fields['application'] = IntegerField(initial=application.id,
                                         widget=HiddenInput())

    return type(builtin_str(application.name + "Form"), (Form, ), fields)