comps-wrapper: Reduce duplication in code

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2017-04-12 14:57:28 +02:00
parent 3861be3e08
commit 180a5b94a9

View File

@ -77,45 +77,12 @@ class CompsWrapper(object):
group_node = doc.createElement("group") group_node = doc.createElement("group")
msg_elem.appendChild(group_node) msg_elem.appendChild(group_node)
id_node = doc.createElement("id") append_common_info(doc, group_node, group, force_description=True)
id_node.appendChild(doc.createTextNode(group.id)) append_bool(doc, group_node, "default", group.default)
group_node.appendChild(id_node) append_bool(doc, group_node, "uservisible", group.uservisible)
name_node = doc.createElement("name")
name_node.appendChild(doc.createTextNode(group.name))
group_node.appendChild(name_node)
for lang in sorted(group.name_by_lang):
text = group.name_by_lang[lang]
node = doc.createElement("name")
node.setAttribute("xml:lang", lang)
node.appendChild(doc.createTextNode(text))
group_node.appendChild(node)
node = doc.createElement("description")
group_node.appendChild(node)
if group.desc and group.desc != "":
node.appendChild(doc.createTextNode(group.desc))
for lang in sorted(group.desc_by_lang):
text = group.desc_by_lang[lang]
node = doc.createElement("description")
node.setAttribute("xml:lang", lang)
node.appendChild(doc.createTextNode(text))
group_node.appendChild(node)
node = doc.createElement("default")
node.appendChild(doc.createTextNode("true" if group.default else "false"))
group_node.appendChild(node)
node = doc.createElement("uservisible")
node.appendChild(doc.createTextNode("true" if group.uservisible else "false"))
group_node.appendChild(node)
if group.lang_only: if group.lang_only:
node = doc.createElement("langonly") append(doc, group_node, "langonly", group.lang_only)
node.appendChild(doc.createTextNode(group.lang_only))
group_node.appendChild(node)
packagelist = doc.createElement("packagelist") packagelist = doc.createElement("packagelist")
@ -125,10 +92,7 @@ class CompsWrapper(object):
for type_name in TYPE_MAPPING.values(): for type_name in TYPE_MAPPING.values():
for package in sorted(packages_by_type[type_name], key=attrgetter('name')): for package in sorted(packages_by_type[type_name], key=attrgetter('name')):
node = doc.createElement("packagereq") node = append(doc, packagelist, "packagereq", package.name, type=type_name)
node.appendChild(doc.createTextNode(package.name))
node.setAttribute("type", type_name)
packagelist.appendChild(node)
if type_name == "conditional": if type_name == "conditional":
node.setAttribute("requires", pkg.requires) node.setAttribute("requires", pkg.requires)
@ -141,114 +105,36 @@ class CompsWrapper(object):
cat_node = doc.createElement("category") cat_node = doc.createElement("category")
msg_elem.appendChild(cat_node) msg_elem.appendChild(cat_node)
id_node = doc.createElement("id") append_common_info(doc, cat_node, category)
id_node.appendChild(doc.createTextNode(category.id))
cat_node.appendChild(id_node)
name_node = doc.createElement("name")
name_node.appendChild(doc.createTextNode(category.name))
cat_node.appendChild(name_node)
for lang in sorted(category.name_by_lang):
text = category.name_by_lang[lang]
node = doc.createElement("name")
node.setAttribute("xml:lang", lang)
node.appendChild(doc.createTextNode(text))
cat_node.appendChild(node)
if category.desc and category.desc != "":
node = doc.createElement("description")
node.appendChild(doc.createTextNode(category.desc))
cat_node.appendChild(node)
for lang in sorted(category.desc_by_lang):
text = category.desc_by_lang[lang]
node = doc.createElement("description")
node.setAttribute("xml:lang", lang)
node.appendChild(doc.createTextNode(text))
cat_node.appendChild(node)
if category.display_order is not None: if category.display_order is not None:
display_node = doc.createElement("display_order") append(doc, cat_node, "display_order", str(category.display_order))
display_node.appendChild(doc.createTextNode(str(category.display_order)))
cat_node.appendChild(display_node)
grouplist_node = doc.createElement("grouplist") append_grouplist(doc, cat_node, groups)
groupids = sorted(groups)
for groupid in groupids: for environment in sorted(self.comps.environments, key=attrgetter('id')):
node = doc.createElement("groupid") groups = set(x.name for x in environment.group_ids) & set(self.get_comps_groups())
node.appendChild(doc.createTextNode(groupid)) if not groups:
grouplist_node.appendChild(node) continue
env_node = doc.createElement("environment")
msg_elem.appendChild(env_node)
cat_node.appendChild(grouplist_node) append_common_info(doc, env_node, environment)
environments = sorted(self.comps.environments, key=attrgetter('id')) if environment.display_order is not None:
if environments: append(doc, env_node, "display_order", str(environment.display_order))
for environment in environments:
groups = set(x.name for x in environment.group_ids) & set(self.get_comps_groups())
if not groups:
continue
env_node = doc.createElement("environment")
msg_elem.appendChild(env_node)
id_node = doc.createElement("id") append_grouplist(doc, env_node, groups)
id_node.appendChild(doc.createTextNode(environment.id))
env_node.appendChild(id_node)
name_node = doc.createElement("name") if environment.option_ids:
name_node.appendChild(doc.createTextNode(environment.name)) append_grouplist(doc, env_node, (x.name for x in environment.option_ids), "optionlist")
env_node.appendChild(name_node)
for lang in sorted(environment.name_by_lang):
text = environment.name_by_lang[lang]
node = doc.createElement("name")
node.setAttribute("xml:lang", lang)
node.appendChild(doc.createTextNode(text))
env_node.appendChild(node)
if environment.desc:
node = doc.createElement("description")
node.appendChild(doc.createTextNode(environment.desc))
env_node.appendChild(node)
for lang in sorted(environment.desc_by_lang):
text = environment.desc_by_lang[lang]
node = doc.createElement("description")
node.setAttribute("xml:lang", lang)
node.appendChild(doc.createTextNode(text))
env_node.appendChild(node)
if environment.display_order is not None:
display_node = doc.createElement("display_order")
display_node.appendChild(doc.createTextNode("%s" % environment.display_order))
env_node.appendChild(display_node)
grouplist_node = doc.createElement("grouplist")
groupids = sorted(groups)
for groupid in groupids:
node = doc.createElement("groupid")
node.appendChild(doc.createTextNode(groupid))
grouplist_node.appendChild(node)
env_node.appendChild(grouplist_node)
if environment.option_ids:
optionlist_node = doc.createElement("optionlist")
for optionid in sorted(x.name for x in environment.option_ids):
node = doc.createElement("groupid")
node.appendChild(doc.createTextNode(optionid))
optionlist_node.appendChild(node)
env_node.appendChild(optionlist_node)
if self.comps.langpacks: if self.comps.langpacks:
lang_node = doc.createElement("langpacks") lang_node = doc.createElement("langpacks")
msg_elem.appendChild(lang_node) msg_elem.appendChild(lang_node)
for name in sorted(self.comps.langpacks): for name in sorted(self.comps.langpacks):
match_node = doc.createElement("match") append(doc, lang_node, "match", name=name, install=self.comps.langpacks[name])
match_node.setAttribute("name", name)
match_node.setAttribute("install", self.comps.langpacks[name])
lang_node.appendChild(match_node)
return doc return doc
@ -279,14 +165,10 @@ class CompsWrapper(object):
to_remove = [] to_remove = []
for group_obj in self.comps.groups: for group_obj in self.comps.groups:
for group_dict in group_dicts: for group_dict in group_dicts:
if group_dict["glob"]: matcher = fnmatch.fnmatch if group_dict["glob"] else lambda x, y: x == y
if fnmatch.fnmatch(group_obj.id, group_dict["name"]): if matcher(group_obj.id, group_dict["name"]):
self._tweak_group(group_obj, group_dict) self._tweak_group(group_obj, group_dict)
break break
else:
if group_obj.id == group_dict["name"]:
self._tweak_group(group_obj, group_dict)
break
else: else:
to_remove.append(group_obj) to_remove.append(group_obj)
@ -322,3 +204,46 @@ class CompsWrapper(object):
for env in to_remove: for env in to_remove:
self.comps.environments.remove(env) self.comps.environments.remove(env)
def append(doc, parent, elem, content=None, lang=None, **kwargs):
"""Create a new DOM element and append it to parent."""
node = doc.createElement(elem)
if content:
node.appendChild(doc.createTextNode(content))
if lang:
node.setAttribute("xml:lang", lang)
for attr, value in kwargs.iteritems():
node.setAttribute(attr, value)
parent.appendChild(node)
return node
def append_grouplist(doc, parent, groups, elem="grouplist"):
grouplist_node = doc.createElement(elem)
for groupid in sorted(groups):
append(doc, grouplist_node, "groupid", groupid)
parent.appendChild(grouplist_node)
def append_common_info(doc, parent, obj, force_description=False):
"""Add id, name and description (with translations)."""
append(doc, parent, "id", obj.id)
append(doc, parent, "name", obj.name)
for lang in sorted(obj.name_by_lang):
text = obj.name_by_lang[lang]
append(doc, parent, "name", text, lang=lang)
if obj.desc or force_description:
append(doc, parent, "description", obj.desc or '')
for lang in sorted(obj.desc_by_lang):
text = obj.desc_by_lang[lang]
append(doc, parent, "description", text, lang=lang)
def append_bool(doc, parent, elem, value):
node = doc.createElement(elem)
node.appendChild(doc.createTextNode("true" if value else "false"))
parent.appendChild(node)