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

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