|
@@ -3,6 +3,9 @@
|
3
|
3
|
## @file editorialmodel.py
|
4
|
4
|
# Manage instance of an editorial model
|
5
|
5
|
|
|
6
|
+import random
|
|
7
|
+import time
|
|
8
|
+
|
6
|
9
|
import EditorialModel
|
7
|
10
|
from EditorialModel.migrationhandler.dummy import DummyMigrationHandler
|
8
|
11
|
from EditorialModel.backend.dummy_backend import EmBackendDummy
|
|
@@ -10,6 +13,8 @@ from EditorialModel.classes import EmClass
|
10
|
13
|
from EditorialModel.fieldgroups import EmFieldGroup
|
11
|
14
|
from EditorialModel.fields import EmField
|
12
|
15
|
from EditorialModel.types import EmType
|
|
16
|
+from EditorialModel.classtypes import EmClassType
|
|
17
|
+from Lodel.utils.mlstring import MlString
|
13
|
18
|
from EditorialModel.exceptions import EmComponentCheckError, EmComponentNotExistError, MigrationHandlerChangeError
|
14
|
19
|
import hashlib
|
15
|
20
|
|
|
@@ -259,3 +264,127 @@ class Model(object):
|
259
|
264
|
del new_me
|
260
|
265
|
|
261
|
266
|
self.migration_handler = new_mh
|
|
267
|
+
|
|
268
|
+ @classmethod
|
|
269
|
+ ## @brief Generate a random editorial model
|
|
270
|
+ def random(cls, backend):
|
|
271
|
+ em = Model(backend)
|
|
272
|
+
|
|
273
|
+ chances = {
|
|
274
|
+ 'classtype' : 0, # a class in classtype
|
|
275
|
+ 'nclass': 5, #max number of classes per classtype
|
|
276
|
+ 'nofg': 10, #no fieldgroup in a class
|
|
277
|
+ 'nfg': 5, #max number of fieldgroups per classes
|
|
278
|
+ 'notype': 5, # no types in a class
|
|
279
|
+ 'ntype': 3, # max number of types in a class
|
|
280
|
+ 'seltype': 2, #chances to select an optional field
|
|
281
|
+ 'ntypesuperiors': 3, #chances to link with a superior
|
|
282
|
+ 'nofields': 10, # no fields in a fieldgroup
|
|
283
|
+ 'nfields' : 8, #max number of fields per fieldgroups
|
|
284
|
+ 'rfields': 5,#max number of attributes relation fields
|
|
285
|
+ 'optfield': 2, #chances to be optionnal
|
|
286
|
+ }
|
|
287
|
+
|
|
288
|
+ #classes creation
|
|
289
|
+ for classtype in EmClassType.getall():
|
|
290
|
+ if random.randint(0,chances['classtype']) == 0:
|
|
291
|
+ for _ in range(random.randint(1,chances['nclass'])):
|
|
292
|
+ cdats = cls._rnd_component_datas()
|
|
293
|
+ cdats['classtype'] = classtype['name']
|
|
294
|
+ em.create_component('EmClass', cdats)
|
|
295
|
+
|
|
296
|
+ for emclass in em.classes():
|
|
297
|
+ #fieldgroups creation
|
|
298
|
+ if random.randint(0, chances['nofg']) != 0:
|
|
299
|
+ for _ in range(random.randint(1, chances['nfg'])):
|
|
300
|
+ fgdats = cls._rnd_component_datas()
|
|
301
|
+ fgdats['class_id'] = emclass.uid
|
|
302
|
+ em.create_component('EmFieldGroup', fgdats)
|
|
303
|
+
|
|
304
|
+ #types creation
|
|
305
|
+ if random.randint(0, chances['notype']) != 0:
|
|
306
|
+ for _ in range(random.randint(1, chances['ntype'])):
|
|
307
|
+ tdats = cls._rnd_component_datas()
|
|
308
|
+ tdats['class_id'] = emclass.uid
|
|
309
|
+ em.create_component('EmType', tdats)
|
|
310
|
+
|
|
311
|
+ #random type hierarchy
|
|
312
|
+ for emtype in em.components(EmType):
|
|
313
|
+ possible = emtype.possible_superiors()
|
|
314
|
+ for nat in possible:
|
|
315
|
+ while random.randint(0, chances['ntypesuperiors']) == 0 and len(possible[nat]) > 0:
|
|
316
|
+ i = random.randint(0,len(possible[nat])-1)
|
|
317
|
+ emtype.add_superior(possible[nat][i], nat)
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+ #fields creation
|
|
321
|
+ ft_l = EmField.fieldtypes_list()
|
|
322
|
+ for emfg in em.components(EmFieldGroup):
|
|
323
|
+ if random.randint(0, chances['nofields']) != 0:
|
|
324
|
+ for _ in range(random.randint(1, chances['nfields'])):
|
|
325
|
+ ft = ft_l[random.randint(0,len(ft_l)-1)]
|
|
326
|
+ fdats = cls._rnd_component_datas()
|
|
327
|
+ fdats['fieldtype']=ft
|
|
328
|
+ fdats['fieldgroup_id'] = emfg.uid
|
|
329
|
+ if ft == 'rel2type':
|
|
330
|
+ emtypes = em.components(EmType)
|
|
331
|
+ fdats['rel_to_type_id'] = emtypes[random.randint(0,len(emtypes)-1)].uid
|
|
332
|
+ if random.randint(0,chances['optfield']) == 0:
|
|
333
|
+ fdats['optional'] = True
|
|
334
|
+ em.create_component('EmField', fdats)
|
|
335
|
+
|
|
336
|
+ #relationnal fiels creation
|
|
337
|
+ ft_l = [ ft for ft in EmField.fieldtypes_list() if ft != 'rel2type' ]
|
|
338
|
+ for emrelf in [ f for f in em.components(EmField) if f.ftype == 'rel2type' ]:
|
|
339
|
+ for _ in range(0,chances['rfields']):
|
|
340
|
+ ft = ft_l[random.randint(0, len(ft_l)-1)]
|
|
341
|
+ fdats = cls._rnd_component_datas()
|
|
342
|
+ fdats['fieldtype'] = ft
|
|
343
|
+ fdats['fieldgroup_id'] = emrelf.fieldgroup_id
|
|
344
|
+ if random.randint(0, chances['optfield']) == 0:
|
|
345
|
+ fdats['optional'] = True
|
|
346
|
+ em.create_component('EmField', fdats)
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+ #selection optionnal fields
|
|
350
|
+ for emtype in em.components(EmType):
|
|
351
|
+ selectable = [field for fieldgroup in emtype.fieldgroups() for field in fieldgroup.fields() if field.optional ]
|
|
352
|
+ for field in selectable:
|
|
353
|
+ if random.randint(0,chances['seltype']) == 0:
|
|
354
|
+ emtype.select_field(field)
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+ return em
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+ @staticmethod
|
|
362
|
+ ## @brief Generate a random string
|
|
363
|
+ # @warning dirty cache trick with globals()
|
|
364
|
+ def _rnd_str():
|
|
365
|
+ if '_words' not in globals():
|
|
366
|
+ with open('/usr/share/dict/words', 'r') as fpw:
|
|
367
|
+ globals()['_words'] = [ l for l in fpw ]
|
|
368
|
+ words = globals()['_words']
|
|
369
|
+ return words[random.randint(0,len(words)-1)]
|
|
370
|
+
|
|
371
|
+ @classmethod
|
|
372
|
+ def _rnd_mlstr(cls, nlng):
|
|
373
|
+ ret = MlString()
|
|
374
|
+ for _ in range(nlng):
|
|
375
|
+ ret.set(cls._rnd_str(), cls._rnd_str())
|
|
376
|
+ return ret
|
|
377
|
+
|
|
378
|
+ @classmethod
|
|
379
|
+ ## @brief returns randomly generated datas for an EmComponent
|
|
380
|
+ def _rnd_component_datas(cls):
|
|
381
|
+ mlstr_nlang = 5;
|
|
382
|
+ ret = {}
|
|
383
|
+ ret['name'] = cls._rnd_str()
|
|
384
|
+ ret['string'] = cls._rnd_mlstr(mlstr_nlang)
|
|
385
|
+ ret['help_text'] = cls._rnd_mlstr(mlstr_nlang)
|
|
386
|
+
|
|
387
|
+ return ret
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|