No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xmlfile.py 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. #-*- coding: utf-8 -*-
  2. import lxml
  3. import os
  4. from lxml import etree
  5. from lodel.context import LodelContext
  6. LodelContext.expose_modules(globals(), {
  7. 'lodel.editorial_model.model': ['EditorialModel'],
  8. 'lodel.editorial_model.components': ['EmComponent', 'EmClass', 'EmField',
  9. 'EmGroup'],
  10. 'lodel.utils.mlstring': ['MlString']})
  11. ##@package lodel.editorial_model.translator.xmlfile Translator module designed
  12. #to load & save EM in XML
  13. #
  14. # Structure of a xml file which represents an editorial model:
  15. # <ul>
  16. # <li>\<name\>: name of the model, field <b><em>name</em></b> in class <b><em>EditorialModel</em></b>
  17. # <li>\<description\>: field <b><em>description</em></b> of a composed element, one for each language translation named
  18. # <ul><li>\<fre\> for french,
  19. # <li>\<eng\> for english,
  20. # <li>\<esp\> for spanish,
  21. # <li>\<ger\> for german</ul>
  22. # <li>\<classes\>: set of all <b><em>EmClass</em></b> in the model \n
  23. # for each classe: \n
  24. # \<class\><ul>
  25. # <li>\<uid\>the class's id
  26. # <li>\<display_name\> The name of the class, field <b><em>display_name</em></b> of the <b><em>EmClass</em></b> , in different languages if they're available :
  27. # <ul><li>\<fre\> for french,
  28. # <li>\<eng\> for english,
  29. # <li>\<esp\> for spanish,
  30. # <li>\<ger> for german</ul>
  31. # <li>\<help_text\> Short explanation of the class's purpose, in different languages, as above
  32. # <li>\<abstract\> True or False, field <b><em>abstract</em></b> of the <b><em>EmClass</em></b>
  33. # <li>\<pure_abstract\> True or False, field <b><em>pure_bastract</em></b> of the <b><em>EmClass</em></b>
  34. # <li>\<group\><b><em>uid</em></b> of the group of the field <b><em>group</em></b> of the <b><em>EmClass</em></b>
  35. # <li>\<fields\>: set of all the <b><em>EmField</em></b> of the <b><em>EmClass</em></b>\n
  36. # for each field: \n
  37. # \<field\>
  38. # <ul><li>\<uid\> uid of the <b><em>EmField</em></b>
  39. # <li>\<display_name\> field <b><em>display_name</em></b> of the <b><em>EmField</em></b>, in different languages, as above
  40. # <li>\<help_text\> Short explanation of the class's purpose, in different languages, as above
  41. # <li>\<group\><b><em>uid</em></b> of the group of the field <b><em>group</em></b> of the <b><em>EmClass</em></b>
  42. # <li>\<datahandler_name\> field <b><em>datahandler_name</em></b> of the Emfield, the name of a datahandler
  43. # <li>\<datahandler_options\>, a list of xml items, each of them named with an option name and contains its value</ul></ul>
  44. # <li>\<groups\>: set of all the groups <b><em>EmGroup</em></b> in the model\n
  45. # for each group:\n
  46. # <ul><li>\<uid\> uid of the <b><em>EmField</em></b>
  47. # <li>\<display_name\> field <b><em>display_name</em></b> of the <b><em>EmField</em></b>, in different languages, as above
  48. # <li>\<help_text\> Short explanation of the class's purpose, in different languages, as above
  49. # <li>\<requires\> all uids of the <b><em>EmGroups</em></b> required by this group and which are in the fields <b><em>require</em></b>
  50. # <li>\<components\> Set of all components of the <b><em>EmGroups</em></b>, representation of the field <b><em>__components</em></b> \n
  51. # this item is splitted in two parts :\
  52. # <ul><li>\<emfields\> all the emfields with, for each of them:\n
  53. # \<emfield\> \n
  54. # <ul><li> \<uid\> <b><em>uid</em></b> of the <b><em>EmField</em></b></ul>
  55. # <li>\<emclasses\> all the emclasses with, for each of them:\n
  56. # \<emclass\> \n
  57. # <ul><li> \<uid\> <b><em>uid</em></b> of the <b><em>EmClass</em></b></ul></ul></ul>
  58. ##@brief Saves a model in a xml file
  59. # @param model EditorialModel : the model to save
  60. # @param filename str|None : if None display on stdout else writes in the file filename
  61. def save(model, **kwargs):
  62. Em = etree.Element("editorial_model")
  63. em_name = etree.SubElement(Em, 'name')
  64. write_mlstring_xml(etree, em_name, model.name)
  65. em_description = etree.SubElement(Em, 'description')
  66. write_mlstring_xml(etree, em_description, model.description)
  67. em_classes = etree.SubElement(Em, 'classes')
  68. classes = model.all_classes()
  69. for emclass in classes:
  70. write_emclass_xml(etree, em_classes, classes[emclass].uid, classes[emclass].display_name,
  71. classes[emclass].help_text, classes[emclass].group,
  72. classes[emclass].fields(no_parents=True), classes[emclass].parents,
  73. classes[emclass].abstract, classes[emclass].pure_abstract)
  74. em_groups = etree.SubElement(Em, 'groups')
  75. groups = model.all_groups()
  76. for group in groups:
  77. requires = groups[group].dependencies()
  78. write_emgroup_xml(etree, em_groups, groups[group].uid, groups[group].display_name, groups[group].help_text,
  79. list(requires.keys()), groups[group].components())
  80. emodel = etree.tostring(Em, encoding='utf-8', xml_declaration=True, method='xml', pretty_print= True)
  81. if len(kwargs) == 0:
  82. print(emodel.decode())
  83. else:
  84. outfile = open(kwargs['filename'], "w")
  85. outfile.write(emodel.decode())
  86. outfile.close()
  87. ##@brief Writes a representation of a MlString in xml
  88. # @param etree : the xml object
  89. # @param elem : the element which represents a MlString
  90. # @param mlstr : the mlstr to write
  91. def write_mlstring_xml(etree, elem, mlstr):
  92. for lang in mlstr.values:
  93. ss_mlstr = etree.SubElement(elem,lang)
  94. ss_mlstr.text = mlstr.get(lang)
  95. ##@brief Writes the definition of a datahandler in xml
  96. # @param etree : the xml object
  97. # @param elem : the element which defines a datahandler
  98. # @param dhdl_name : the name of the datahandler
  99. # @param kwargs : the options of the datahandler
  100. def write_datahandler_xml(etree, elem, dhdl_name, **kwargs):
  101. dhdl = etree.SubElement(elem,'datahandler_name')
  102. dhdl.text = dhdl_name
  103. dhdl_opt = etree.SubElement(elem, 'datahandler_options')
  104. for argname, argval in kwargs.items():
  105. arg = etree.SubElement(dhdl_opt, argname)
  106. opt_val=''
  107. if (isinstance(argval, str)):
  108. opt_val=argval
  109. elif (isinstance(argval, bool)):
  110. opt_val = str(argval)
  111. elif (isinstance(argval, list) | isinstance(argval, tuple) | isinstance(argval, dict)):
  112. for argu in argval:
  113. if len(opt_val) > 0:
  114. opt_val = opt_val + ','
  115. if isinstance(argu, EmComponent):
  116. opt_val = opt_val + argu.uid
  117. elif isinstance(argu, str):
  118. opt_val = opt_val + argu
  119. else:
  120. opt_val = str(argu)
  121. arg.text = opt_val
  122. ##@brief Writes a representation in xml of a EmField
  123. # @param etree : the xml object
  124. # @param elem : the element for the EmField
  125. # @param uid : the uid of the EmField
  126. # @param name : the name of the field
  127. # @param help_text : explanations of the EmField
  128. # @param group_uid : the uid of a group, can be None
  129. # @datahandler_name
  130. # @**kwargs : options of the datahandler
  131. def write_emfield_xml(etree, elem, uid, name, help_text, group, datahandler_name, **kwargs):
  132. emfield = etree.SubElement(elem,'field')
  133. emfield_uid = etree.SubElement(emfield, 'uid')
  134. emfield_uid.text = uid
  135. emfield_name = etree.SubElement(emfield, 'display_name')
  136. if name is None:
  137. pass
  138. else:
  139. write_mlstring_xml(etree, emfield_name, name)
  140. emfield_help = etree.SubElement(emfield, 'help_text')
  141. if help_text is None:
  142. pass
  143. else:
  144. write_mlstring_xml(etree, emfield_help, help_text)
  145. emfield_group = etree.SubElement(emfield, 'group')
  146. if group is not None:
  147. emfield_group.text = group.uid #write_emgroup_xml(etree, emfield_group, group.uid, group.display_name, group.help_text, group.requires)
  148. write_datahandler_xml(etree,emfield,datahandler_name, **kwargs)
  149. ##@brief Writes a representation of a EmGroup in xml
  150. # @param etree : the xml object
  151. # @param elem : the element for the EmGroup
  152. # @param name : the name of the group
  153. # @param help_text : explanations of the EmGroup
  154. # @param requires : a list of the group's uids whose this group depends
  155. def write_emgroup_xml(etree, elem, uid, name, help_text, requires, components):
  156. emgroup = etree.SubElement(elem, 'group')
  157. emgroup_uid = etree.SubElement(emgroup, 'uid')
  158. emgroup_uid.text = uid
  159. emgroup_name = etree.SubElement(emgroup, 'display_name')
  160. if name is None:
  161. pass
  162. else:
  163. write_mlstring_xml(etree, emgroup_name, name)
  164. emgroup_help = etree.SubElement(emgroup, 'help_text')
  165. if help_text is None:
  166. pass
  167. else:
  168. write_mlstring_xml(etree, emgroup_help, help_text)
  169. emgroup_requires = etree.SubElement(emgroup, 'requires')
  170. emgroup_requires.text = ",".join(requires)
  171. emgroup_comp = etree.SubElement(emgroup, 'components')
  172. emgroup_comp_cls = etree.SubElement(emgroup_comp, 'emclasses')
  173. emgroup_comp_fld = etree.SubElement(emgroup_comp, 'emfields')
  174. for component in components:
  175. if isinstance(component, EmField):
  176. emgroup_comp_fld_ins = etree.SubElement(emgroup_comp_fld, 'emfield')
  177. em_group_comp_fld_ins_uid = etree.SubElement(emgroup_comp_fld_ins,'uid')
  178. em_group_comp_fld_ins_uid.text = component.uid
  179. em_group_comp_fld_ins_cls = etree.SubElement(emgroup_comp_fld_ins,'class')
  180. em_group_comp_fld_ins_cls.text = component.get_emclass_uid()
  181. elif isinstance(component, EmClass):
  182. em_group_comp_cls_ins = etree.SubElement(emgroup_comp_cls, 'emclass')
  183. em_group_comp_cls_ins.text = component.uid
  184. ##@brief Writes a representation of a EmClass in xml
  185. # @param etree : the xml object
  186. # @param elem : the element for the EmClass
  187. # @param name : the name of the group
  188. # @param help_text : explanations of the EmClass
  189. # @param fields : a dict
  190. # @param parents : a list of EmClass uids
  191. # @param abstract : a boolean
  192. # @param pure_abstract : a boolean
  193. def write_emclass_xml(etree, elem, uid, name, help_text, group, fields, parents, abstract = False, pure_abstract = False):
  194. emclass = etree.SubElement(elem, 'class')
  195. emclass_uid = etree.SubElement(emclass, 'uid')
  196. emclass_uid.text = uid
  197. emclass_name = etree.SubElement(emclass, 'display_name')
  198. if name is None:
  199. pass
  200. else:
  201. write_mlstring_xml(etree, emclass_name, name)
  202. emclass_help = etree.SubElement(emclass, 'help_text')
  203. if help_text is None:
  204. pass
  205. else:
  206. write_mlstring_xml(etree, emclass_help, help_text)
  207. emclass_abstract = etree.SubElement(emclass, 'abstract')
  208. emclass_abstract.text ="True" if abstract else "False"
  209. emclass_pure_abstract = etree.SubElement(emclass, 'pure_abstract')
  210. emclass_pure_abstract.text = "True" if pure_abstract else "False"
  211. emclass_group = etree.SubElement(emclass, 'group')
  212. if group is not None:
  213. emclass_group.text = group.uid
  214. emclass_fields = etree.SubElement(emclass, 'fields')
  215. for field in fields:
  216. write_emfield_xml(etree, emclass_fields, field.uid, field.display_name, field.help_text,
  217. field.group,field.data_handler_name, **field.data_handler_options)
  218. parents_list=list()
  219. for parent in parents:
  220. parents_list.append(parent.uid)
  221. emclass_parents = etree.SubElement(emclass, 'parents')
  222. emclass_parents.text = ",".join(parents_list)
  223. ##@brief Loads a model from a xml file
  224. # @param model EditorialModel : the model to load
  225. # @return a new EditorialModel object
  226. def load(filename):
  227. Em = etree.parse(filename)
  228. emodel = Em.getroot()
  229. name = emodel.find('name')
  230. description = emodel.find('description')
  231. model = EditorialModel(load_mlstring_xml(name), load_mlstring_xml(description))
  232. classes = emodel.find('classes')
  233. for emclass in classes:
  234. em_class = load_class_xml(model, emclass)
  235. if em_class.uid not in model.all_classes():
  236. model.add_class(em_class)
  237. groups = emodel.find('groups')
  238. i = 0
  239. for group in groups:
  240. grp = load_group_xml(model, group)
  241. if grp.uid not in model.all_groups():
  242. grp = model.add_group(grp)
  243. return model
  244. ##@brief Creates a EmClass from a xml description
  245. # @param elem : the element which represents the EmClass
  246. # @param model : the model which will contain the new class
  247. # @return a new EmClass object
  248. def load_class_xml(model, elem):
  249. uid = elem.find('uid').text
  250. if elem.find('display_name').text is None:
  251. name = None
  252. else:
  253. name = load_mlstring_xml(elem.find('display_name'))
  254. if elem.find('help_text').text is None:
  255. help_text = None
  256. else:
  257. help_text = load_mlstring_xml(elem.find('help_text'))
  258. abstract = (elem.find('abstract').text == 'True')
  259. pure_abstract = (elem.find('pure_abstract').text == 'True')
  260. requires = list()
  261. classes = model.all_classes()
  262. req = elem.find('parents')
  263. if req.text is not None:
  264. l_req = req.text.split(',')
  265. for r in l_req:
  266. if r in classes:
  267. requires.append(model.all_classes_ref(r))
  268. else:
  269. requires.append(model.add_class(EmClass(r)))
  270. group = elem.find('group')
  271. if group.text is not None:
  272. if group.text in model.all_groups():
  273. grp = model.all_groups_ref(group.text)
  274. else:
  275. grp = model.add_group(EmGroup(group.text))
  276. else:
  277. grp = None
  278. if uid in classes:
  279. emclass = model.all_classes_ref(uid)
  280. emclass.display_name = name
  281. emclass.help_text = help_text
  282. emclass.parents=requires
  283. emclass.group = grp
  284. emclass.abstract = abstract
  285. emclass.pure_abstract = pure_abstract
  286. else:
  287. emclass = EmClass(uid, name, help_text, abstract,requires, grp, pure_abstract)
  288. model.add_class(emclass)
  289. fields = elem.find('fields')
  290. for field in fields:
  291. emfield = load_field_xml(model, field, emclass)
  292. l_emfields = emclass.fields()
  293. incls = False
  294. for emf in l_emfields:
  295. if emfield.uid == emf.uid:
  296. incls = True
  297. break
  298. if not incls:
  299. emclass.add_field(emfield)
  300. return emclass
  301. ##@brief Creates a EmField from a xml description
  302. #@param elem : the element which represents the EmField
  303. #@param model : the model which will contain the new field
  304. #@param emclass EmClass : the EmClass of the field
  305. #@return a new EmField object
  306. def load_field_xml(model, elem, emclass):
  307. uid = elem.find('uid').text
  308. if elem.find('display_name').text is None:
  309. name = None
  310. else:
  311. name = load_mlstring_xml(elem.find('display_name'))
  312. if elem.find('help_text').text is None:
  313. help_text = None
  314. else:
  315. help_text = load_mlstring_xml(elem.find('help_text'))
  316. emgroup = elem.find('group')
  317. if emgroup.text is not None:
  318. if emgroup.text in model.all_groups():
  319. group = model.all_groups_ref(emgroup.text)
  320. else:
  321. group = model.add_group(EmGroup(emgroup.text))
  322. else:
  323. group = None
  324. dhdl = elem.find('datahandler_name')
  325. dhdl_opts = {}
  326. if dhdl.text is not None:
  327. dhdl_opts = elem.find('datahandler_options')
  328. if dhdl_opts is not None:
  329. dhdl_options = load_dhdl_options_xml(model, dhdl_opts)
  330. emfield = EmField(
  331. uid, dhdl.text, emclass, name, help_text, group, **dhdl_options)
  332. return emfield
  333. ##@brief Returns datahandler options from a xml description
  334. # @param elem : the element which represents the datahandler
  335. # @param model : the model which will contain the new field
  336. # @return datahandler options
  337. def load_dhdl_options_xml(model, elem):
  338. dhdl_options=dict()
  339. for opt in elem:
  340. if (opt.tag == 'allowed_classes'):
  341. classes = list()
  342. if opt.text is not None:
  343. clss = opt.text.split(',')
  344. for classe in clss:
  345. if classe in model.all_classes():
  346. classes.append(model.all_classes_ref(classe))
  347. else:
  348. new_cls = model.add_class(EmClass(classe))
  349. classes.append(new_cls)
  350. dhdl_options['allowed_classes'] = classes
  351. elif (opt.tag == 'back_reference'):
  352. dhdl_options['back_reference'] = tuple(opt.text.split(','))
  353. elif ((opt.text == 'True') | (opt.text == 'False')):
  354. dhdl_options[opt.tag] = (opt.text == 'True')
  355. else:
  356. dhdl_options[opt.tag] = opt.text
  357. return dhdl_options
  358. ##@brief Creates a EmGroup from a xml description
  359. # @param elem : the element which represents the EmGroup
  360. # @param model : the model which will contain the new group
  361. # @return a new EmGroup object
  362. def load_group_xml(model, elem):
  363. uid = elem.find('uid')
  364. if elem.find('display_name').text is None:
  365. name = None
  366. else:
  367. name = load_mlstring_xml(elem.find('display_name'))
  368. if elem.find('help_text').text is None:
  369. help_text = None
  370. else:
  371. help_text = load_mlstring_xml(elem.find('help_text'))
  372. requires = list()
  373. groups = model.all_groups()
  374. req = elem.find('requires')
  375. if req.text is not None:
  376. l_req = req.text.split(',')
  377. for r in l_req:
  378. if r in groups:
  379. requires.append(model.all_groups_ref(r))
  380. else:
  381. grp = model.new_group(r)
  382. requires.append(grp)
  383. comp= list()
  384. components = elem.find('components')
  385. fields = components.find('emfields')
  386. for field in fields:
  387. fld_uid = field.find('uid').text
  388. fld_class = field.find('class').text
  389. fld = model.all_classes_ref(fld_class).fields(fld_uid)
  390. comp.append(fld)
  391. classes = components.find('emclasses')
  392. for classe in classes:
  393. comp.append(model.all_classes_ref(classe.text))
  394. groups = model.all_groups()
  395. if uid.text in groups:
  396. group = model.all_groups_ref(uid.text)
  397. group.display_name = name
  398. group.help_text = help_text
  399. group.add_dependencie(requires)
  400. else:
  401. group = EmGroup(uid.text, requires, name, help_text)
  402. group.add_components(comp)
  403. return group
  404. ##@brief Constructs a MlString from a xml description
  405. # @param elem : the element which represents the MlString
  406. # @param model : the model which will contain the new group
  407. # @return a new MlString object
  408. def load_mlstring_xml(elem):
  409. mlstr = dict()
  410. for lang in elem:
  411. mlstr[lang.tag] = lang.text
  412. return MlString(mlstr)