devtools/fill.py
branchtls-sprint
changeset 1802 d628defebc17
parent 1398 5fe84a5f7035
child 1977 606923dff11b
equal deleted inserted replaced
1801:672acc730ce5 1802:d628defebc17
    31     """
    31     """
    32     for cst in eschema.constraints(attrname):
    32     for cst in eschema.constraints(attrname):
    33         if isinstance(cst, StaticVocabularyConstraint):
    33         if isinstance(cst, StaticVocabularyConstraint):
    34             return cst.vocabulary()
    34             return cst.vocabulary()
    35     return None
    35     return None
    36     
    36 
    37 
    37 
    38 def get_max_length(eschema, attrname):
    38 def get_max_length(eschema, attrname):
    39     """returns the maximum length allowed for 'attrname'"""
    39     """returns the maximum length allowed for 'attrname'"""
    40     for cst in eschema.constraints(attrname):
    40     for cst in eschema.constraints(attrname):
    41         if isinstance(cst, SizeConstraint) and cst.max:
    41         if isinstance(cst, SizeConstraint) and cst.max:
    73         while value in _GENERATED_VALUES.get((self.e_schema.type, attrname), ()):
    73         while value in _GENERATED_VALUES.get((self.e_schema.type, attrname), ()):
    74             index += 1
    74             index += 1
    75             value = self.__generate_value(attrname, index, **kwargs)
    75             value = self.__generate_value(attrname, index, **kwargs)
    76         _GENERATED_VALUES.setdefault((self.e_schema.type, attrname), set()).add(value)
    76         _GENERATED_VALUES.setdefault((self.e_schema.type, attrname), set()).add(value)
    77         return value
    77         return value
    78         
    78 
    79     def __generate_value(self, attrname, index, **kwargs):
    79     def __generate_value(self, attrname, index, **kwargs):
    80         """generates a consistent value for 'attrname'"""
    80         """generates a consistent value for 'attrname'"""
    81         attrtype = str(self.e_schema.destination(attrname)).lower()
    81         attrtype = str(self.e_schema.destination(attrname)).lower()
    82         # Before calling generate_%s functions, try to find values domain
    82         # Before calling generate_%s functions, try to find values domain
    83         etype = self.e_schema.type
    83         etype = self.e_schema.type
    98         """generates a consistent value for 'attrname' if it's a choice"""
    98         """generates a consistent value for 'attrname' if it's a choice"""
    99         choices = get_choices(self.e_schema, attrname)
    99         choices = get_choices(self.e_schema, attrname)
   100         if choices is None:
   100         if choices is None:
   101             return None
   101             return None
   102         return unicode(choice(choices)) # FIXME
   102         return unicode(choice(choices)) # FIXME
   103         
   103 
   104     def generate_string(self, attrname, index, format=None):
   104     def generate_string(self, attrname, index, format=None):
   105         """generates a consistent value for 'attrname' if it's a string"""
   105         """generates a consistent value for 'attrname' if it's a string"""
   106         # First try to get choices
   106         # First try to get choices
   107         choosed = self.generate_choice(attrname, index)
   107         choosed = self.generate_choice(attrname, index)
   108         if choosed is not None:
   108         if choosed is not None:
   131         return value[:attrlength]
   131         return value[:attrlength]
   132 
   132 
   133     def generate_password(self, attrname, index):
   133     def generate_password(self, attrname, index):
   134         """generates a consistent value for 'attrname' if it's a password"""
   134         """generates a consistent value for 'attrname' if it's a password"""
   135         return u'toto'
   135         return u'toto'
   136         
   136 
   137     def generate_integer(self, attrname, index):
   137     def generate_integer(self, attrname, index):
   138         """generates a consistent value for 'attrname' if it's an integer"""
   138         """generates a consistent value for 'attrname' if it's an integer"""
   139         choosed = self.generate_choice(attrname, index)
   139         choosed = self.generate_choice(attrname, index)
   140         if choosed is not None:
   140         if choosed is not None:
   141             return choosed
   141             return choosed
   143         if maxvalue is not None and maxvalue <= 0 and minvalue is None:
   143         if maxvalue is not None and maxvalue <= 0 and minvalue is None:
   144             minvalue = maxvalue - index # i.e. randint(-index, 0)
   144             minvalue = maxvalue - index # i.e. randint(-index, 0)
   145         else:
   145         else:
   146             maxvalue = maxvalue or index
   146             maxvalue = maxvalue or index
   147         return randint(minvalue or 0, maxvalue)
   147         return randint(minvalue or 0, maxvalue)
   148     
   148 
   149     generate_int = generate_integer
   149     generate_int = generate_integer
   150     
   150 
   151     def generate_float(self, attrname, index):
   151     def generate_float(self, attrname, index):
   152         """generates a consistent value for 'attrname' if it's a float"""
   152         """generates a consistent value for 'attrname' if it's a float"""
   153         return float(randint(-index, index))
   153         return float(randint(-index, index))
   154     
   154 
   155     def generate_decimal(self, attrname, index):
   155     def generate_decimal(self, attrname, index):
   156         """generates a consistent value for 'attrname' if it's a float"""
   156         """generates a consistent value for 'attrname' if it's a float"""
   157         return Decimal(str(self.generate_float(attrname, index)))
   157         return Decimal(str(self.generate_float(attrname, index)))
   158     
   158 
   159     def generate_date(self, attrname, index):
   159     def generate_date(self, attrname, index):
   160         """generates a random date (format is 'yyyy-mm-dd')"""
   160         """generates a random date (format is 'yyyy-mm-dd')"""
   161         return date(randint(2000, 2004), randint(1, 12), randint(1, 28))
   161         return date(randint(2000, 2004), randint(1, 12), randint(1, 28))
   162 
   162 
   163     def generate_time(self, attrname, index):
   163     def generate_time(self, attrname, index):
   164         """generates a random time (format is ' HH:MM')"""
   164         """generates a random time (format is ' HH:MM')"""
   165         return timedelta(0, 11, index%60) #'11:%02d' % (index % 60)
   165         return timedelta(0, 11, index%60) #'11:%02d' % (index % 60)
   166     
   166 
   167     def generate_datetime(self, attrname, index):
   167     def generate_datetime(self, attrname, index):
   168         """generates a random date (format is 'yyyy-mm-dd HH:MM')"""
   168         """generates a random date (format is 'yyyy-mm-dd HH:MM')"""
   169         return datetime(randint(2000, 2004), randint(1, 12), randint(1, 28), 11, index%60)
   169         return datetime(randint(2000, 2004), randint(1, 12), randint(1, 28), 11, index%60)
   170         
   170 
   171 
   171 
   172     def generate_bytes(self, attrname, index, format=None):
   172     def generate_bytes(self, attrname, index, format=None):
   173         # modpython way
   173         # modpython way
   174         fakefile = Binary("%s%s" % (attrname, index))
   174         fakefile = Binary("%s%s" % (attrname, index))
   175         fakefile.filename = "file_%s" % attrname
   175         fakefile.filename = "file_%s" % attrname
   176         fakefile.value = fakefile.getvalue()
   176         fakefile.value = fakefile.getvalue()
   177         return fakefile
   177         return fakefile
   178     
   178 
   179     def generate_boolean(self, attrname, index):
   179     def generate_boolean(self, attrname, index):
   180         """generates a consistent value for 'attrname' if it's a boolean"""
   180         """generates a consistent value for 'attrname' if it's a boolean"""
   181         return index % 2 == 0
   181         return index % 2 == 0
   182 
   182 
   183     def generate_Any_data_format(self, index, **kwargs):
   183     def generate_Any_data_format(self, index, **kwargs):
   184         # data_format attribute of Image/File has no vocabulary constraint, we
   184         # data_format attribute of Image/File has no vocabulary constraint, we
   185         # need this method else stupid values will be set which make mtconverter
   185         # need this method else stupid values will be set which make mtconverter
   186         # raise exception
   186         # raise exception
   187         return u'application/octet-stream'
   187         return u'application/octet-stream'
   188     
   188 
   189     def generate_Any_content_format(self, index, **kwargs):
   189     def generate_Any_content_format(self, index, **kwargs):
   190         # content_format attribute of EmailPart has no vocabulary constraint, we
   190         # content_format attribute of EmailPart has no vocabulary constraint, we
   191         # need this method else stupid values will be set which make mtconverter
   191         # need this method else stupid values will be set which make mtconverter
   192         # raise exception
   192         # raise exception
   193         return u'text/plain'
   193         return u'text/plain'
   248         if restrictions:
   248         if restrictions:
   249             queries.append(('INSERT %s X: %s' % (etype, ', '.join(restrictions)),
   249             queries.append(('INSERT %s X: %s' % (etype, ', '.join(restrictions)),
   250                             args))
   250                             args))
   251             assert not 'eid' in args, args
   251             assert not 'eid' in args, args
   252         else:
   252         else:
   253             queries.append(('INSERT %s X' % etype, {}))        
   253             queries.append(('INSERT %s X' % etype, {}))
   254     return queries
   254     return queries
   255 
   255 
   256 
   256 
   257 def make_entity(etype, schema, vreg, index=0, choice_func=_default_choice_func,
   257 def make_entity(etype, schema, vreg, index=0, choice_func=_default_choice_func,
   258                 form=False):
   258                 form=False):
   363                 sym.add( (subj, obj) )
   363                 sym.add( (subj, obj) )
   364                 if rschema.symetric and (obj, subj) in sym:
   364                 if rschema.symetric and (obj, subj) in sym:
   365                     continue
   365                     continue
   366                 subjcard, objcard = rschema.rproperty(subj, obj, 'cardinality')
   366                 subjcard, objcard = rschema.rproperty(subj, obj, 'cardinality')
   367                 # process mandatory relations first
   367                 # process mandatory relations first
   368                 if subjcard in '1+' or objcard in '1+': 
   368                 if subjcard in '1+' or objcard in '1+':
   369                     queries += self.make_relation_queries(sedict, oedict,
   369                     queries += self.make_relation_queries(sedict, oedict,
   370                                                           rschema, subj, obj)
   370                                                           rschema, subj, obj)
   371                 else:
   371                 else:
   372                     delayed.append( (subj, obj) )
   372                     delayed.append( (subj, obj) )
   373             for subj, obj in delayed:
   373             for subj, obj in delayed:
   374                 queries += self.make_relation_queries(sedict, oedict, rschema,
   374                 queries += self.make_relation_queries(sedict, oedict, rschema,
   375                                                       subj, obj)
   375                                                       subj, obj)
   376         return queries
   376         return queries
   377         
   377 
   378     def qargs(self, subjeids, objeids, subjcard, objcard, subjeid, objeid):
   378     def qargs(self, subjeids, objeids, subjcard, objcard, subjeid, objeid):
   379         if subjcard in '?1':
   379         if subjcard in '?1':
   380             subjeids.remove(subjeid)
   380             subjeids.remove(subjeid)
   381         if objcard in '?1':
   381         if objcard in '?1':
   382             objeids.remove(objeid)
   382             objeids.remove(objeid)
   409                     objeids.remove(objeid)
   409                     objeids.remove(objeid)
   410                     if subjeid in subjeids:
   410                     if subjeid in subjeids:
   411                         subjeids.remove(subjeid)
   411                         subjeids.remove(subjeid)
   412         if not subjeids:
   412         if not subjeids:
   413             check_card_satisfied(objcard, objeids, subj, rschema, obj)
   413             check_card_satisfied(objcard, objeids, subj, rschema, obj)
   414             return 
   414             return
   415         if not objeids:
   415         if not objeids:
   416             check_card_satisfied(subjcard, subjeids, subj, rschema, obj)
   416             check_card_satisfied(subjcard, subjeids, subj, rschema, obj)
   417             return
   417             return
   418         if subjcard in '?1+':
   418         if subjcard in '?1+':
   419             for subjeid in tuple(subjeids):
   419             for subjeid in tuple(subjeids):
   450             for subjeid, objeid in zip(subjeidsiter, objeidsiter):
   450             for subjeid, objeid in zip(subjeidsiter, objeidsiter):
   451                 if subjeid != objeid and not (subjeid, objeid) in used:
   451                 if subjeid != objeid and not (subjeid, objeid) in used:
   452                     used.add( (subjeid, objeid) )
   452                     used.add( (subjeid, objeid) )
   453                     yield q, self.qargs(subjeids, objeids, subjcard, objcard,
   453                     yield q, self.qargs(subjeids, objeids, subjcard, objcard,
   454                                         subjeid, objeid)
   454                                         subjeid, objeid)
   455                     
   455 
   456 def check_card_satisfied(card, remaining, subj, rschema, obj):
   456 def check_card_satisfied(card, remaining, subj, rschema, obj):
   457     if card in '1+' and remaining:
   457     if card in '1+' and remaining:
   458         raise Exception("can't satisfy cardinality %s for relation %s %s %s"
   458         raise Exception("can't satisfy cardinality %s for relation %s %s %s"
   459                         % (card, subj, rschema, obj))
   459                         % (card, subj, rschema, obj))
   460 
   460 
   464         return None
   464         return None
   465     objeid = choice(values)
   465     objeid = choice(values)
   466     while objeid == avoid: # avoid infinite recursion like in X comment X
   466     while objeid == avoid: # avoid infinite recursion like in X comment X
   467         objeid = choice(values)
   467         objeid = choice(values)
   468     return objeid
   468     return objeid
   469                     
   469 
   470                 
   470 
   471 
   471 
   472 # UTILITIES FUNCS ##############################################################
   472 # UTILITIES FUNCS ##############################################################
   473 def make_tel(num_tel):
   473 def make_tel(num_tel):
   474     """takes an integer, converts is as a string and inserts
   474     """takes an integer, converts is as a string and inserts
   475     white spaces each 2 chars (french notation)
   475     white spaces each 2 chars (french notation)