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
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
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