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

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