Ejemplo n.º 1
0
    def __new__(cls, name, bases, attrs):
        super_new = super(LazyVariableMeta, cls).__new__

        # Also ensure initialization is only performed for subclasses of Model
        # (excluding Model class itself).
        parents = [b for b in bases if isinstance(b, LazyVariableMeta)]
        if not parents:
            return super_new(cls, name, bases, attrs)

        # Create the class
        new_class = super_new(cls, name, bases, attrs)

        if not new_class.code:
            raise exceptions.ConstantReferenceException(
                "LazyVariable %s: code can't be empty." % new_class.__name__)

        try:
            obj, created = VariableModel.objects.get_or_create(
                code=new_class.code, defaults={
                    'status': __debug__,
                })
            if not created and not obj.status:
                obj.status = True
                obj.save()
        except ProgrammingError:
            # first migrate
            pass

        library.VariableLibrary.variables[new_class.code] = new_class

        return new_class
Ejemplo n.º 2
0
def resolve_string(string, maps):
    for key, value in maps.iteritems():
        # exit loop when the value is no more a string
        if not isinstance(string, basestring):
            break
        if isinstance(value, (basestring, int, float, long)):
            string = string.replace(key, str(value))
        # value is not a string, check whether the ref is a direct reference
        elif string == key:
            string = value
        # can not reference a object in a string
        elif key in string:
            raise exceptions.ConstantReferenceException(
                'Object Variable:%s cannot referred to %s' % (key, string))
    return string
Ejemplo n.º 3
0
    def __new__(cls, name, bases, attrs):
        super_new = super(RegisterVariableMeta, cls).__new__

        # Also ensure initialization is only performed for subclasses of Model
        # (excluding Model class itself).
        parents = [b for b in bases if isinstance(b, RegisterVariableMeta)]
        if not parents:
            return super_new(cls, name, bases, attrs)

        # Create the class
        new_class = super_new(cls, name, bases, attrs)

        if not new_class.code:
            raise exceptions.ConstantReferenceException(
                "LazyVariable %s: code can't be empty." % new_class.__name__)

        pre_variable_register.send(sender=LazyVariable, variable_cls=new_class)

        library.VariableLibrary.variables[new_class.code] = new_class

        return new_class