"""Function to import and export database from and into XML files."""
from lxml import etree
from sqlalchemy import desc
from chrysalio.models.dbuser import DBUser
from chrysalio.models.dbgroup import DBGroup
from chrysalio.models.populate import element2db
from ..relaxng import RELAXNG_CIOSERVICE
from .dbjob import DBJob
# =============================================================================
[docs]def xml2db(dbsession, root_elt, only=None, error_if_exists=True, modules=None):
"""Load an XML configuration file for an included module.
:type dbsession: sqlalchemy.orm.session.Session
:param dbsession:
SQLAlchemy session.
:type root_elt: lxml.etree.Element
:param root_elt:
XML element with the namespace of the module.
:param str only: (optional)
If not ``None``, only the items of type ``only`` are loaded.
:param bool error_if_exists: (default=True)
It returns an error if an item already exists.
:type modules: collections.OrderedDict
:param modules: (optional)
Dictionary of modules to use to complete the loading.
:rtype: list
:return:
A list of error messages.
"""
# pylint: disable = unused-argument
users = dict(dbsession.query(DBUser.login, DBUser.user_id))
groups = [k[0] for k in dbsession.query(DBGroup.group_id)]
errors = element2db(
dbsession, root_elt, only, error_if_exists, {
'tag': 'job', 'class': DBJob, 'relaxng': RELAXNG_CIOSERVICE,
'kwargs': {'users': users, 'groups': groups}})
return [k for k in errors if k is not None]
# =============================================================================
[docs]def db2xml(dbsession, root_elt):
"""Fill ``root_elt`` with the XML configuration of the module.
:type dbsession: sqlalchemy.orm.session.Session
:param dbsession:
SQLAlchemy session.
:type root_elt: lxml.etree.Element
:param root_elt:
XML element with the namespace of the module.
"""
dbjobs = dbsession.query(DBJob).order_by(desc('priority')).all()
if dbjobs:
root_elt.append(etree.Comment('{0:.^64}'.format('jobs')))
group_elt = etree.SubElement(root_elt, 'jobs')
for dbitem in dbjobs:
group_elt.append(dbitem.db2xml(dbsession))