Kaydet (Commit) e6179182 authored tarafından Suleyman Poyraz's avatar Suleyman Poyraz

Inary system running with two different xml parser

üst 9556b46a
2018-03-18 Suleyman Poyraz <nipalensisaquila@gmail.com>
* minidom vs ciksemek:
minidom ile tüm inary sistemi yeniden yazıldı. Ancak ciddi manada
inar yavaşlıyor. Db initializing süresi 8 sn. Yuh yani. Ama minidom
uğraşımı öylece silmek istemediğim için minidom ve ciksemeli beraber
kullanma kararı aldım.
2018-03-11 Suleyman Poyraz <nipalensisaquila@gmail.com>
* colorized help output:
Colorized command names and now help screen look well.
......
......@@ -17,6 +17,12 @@ import signal
import inary
import inary.context as ctx
import inary.cli.inarycli as inarycli
try:
import ciksemel
except:
#FIXME: Görünüşü güzel olsa bile kodda anlamsızlık yaratıyor
sys.stdout.write("\033[05;31mWARNING:\033[0m\n\t\033[02;37mCiksemel XML Parser not found!!!\n\tFalling back with minidom!!! :(\033[0m\n\n")
import gettext
gettext.bindtextdomain('inary', "/usr/share/locale")
gettext.textdomain('inary')
......
......@@ -254,7 +254,7 @@ class ArchiveLzma(ArchiveBase):
from backports import lzma
lzma_file = lzma.LZMAFile(self.file_path, "rb")
output = open(output_path, "w")
output.write(str(lzma_file.read()))
output.write(lzma_file.read().decode('utf-8'))
output.close()
lzma_file.close()
......
......@@ -118,6 +118,7 @@ class InaryCLI(object):
def __init__(self, orig_args=None):
# first construct a parser for common options
# this is really dummy
self.parser = PreParser(version="%prog " + inary.__version__)
try:
opts, args = self.parser.parse_args(args=orig_args)
......
......@@ -64,7 +64,7 @@ all repositories.
else:
# print for all repos
for repo in op_wrappers.list_repos():
ctx.ui.info(_("Repository : {}\n").format(str(repo)))
ctx.ui.info(util.colorize(_("\n Repository : {}\n"), "blue").format(str(repo)))
self.print_packages(repo)
def print_packages(self, repo):
......
......@@ -54,6 +54,7 @@ Finds the installed package which contains the specified file.
def run(self):
self.init(database = True, write = False)
print(34623456)
if not self.args:
self.help()
......
......@@ -19,6 +19,7 @@ _ = __trans.gettext
import inary.cli.command as command
import inary.context as ctx
import inary.atomicoperations
import inary.operations.op_wrappers as op_wrappers
class UpdateRepo(command.Command, metaclass=command.autocommand):
__doc__ = _("""Update repository databases
......
......@@ -23,10 +23,6 @@ _ = __trans.gettext
# standard python modules
import os.path
#for compability cross platform package managing
import xml.dom.minidom as minidom
from xml.parsers.expat import ExpatError
# inary modules
import inary.sxml.xmlfile as xmlfile
import inary.sxml.autoxml as autoxml
......@@ -39,6 +35,13 @@ import inary.data.group as group
import inary.util as util
import inary.db
try:
import ciksemel
parser = "ciksemel"
except:
import xml.dom.minidom as minidom
parser = "minidom"
class Error(inary.Error):
pass
......@@ -423,7 +426,17 @@ class SpecFile(xmlfile.XmlFile, metaclass=autoxml.autoxml):
def getSourceRelease(self):
return self.history[0].release
def _set_i18n(self, tag, inst):
def _set_i18n(self, tag, inst, parser="ciksemel"):
if parser=="ciksemel":
try:
for summary in tag.tags("Summary"):
inst.summary[summary.getAttribute("xml:lang")] = summary.firstChild().data()
for desc in tag.tags("Description"):
inst.description[desc.getAttribute("xml:lang")] = desc.firstChild().data()
except AttributeError:
raise Error(_("translations.xml file is badly formed."))
else:
try:
for summary in tag.getElementsByTagName("Summary"):
inst.summary[summary.getAttribute("xml:lang")] = summary.childNodes[0].data
......@@ -436,6 +449,21 @@ class SpecFile(xmlfile.XmlFile, metaclass=autoxml.autoxml):
def read_translations(self, path):
if not os.path.exists(path):
return
if parser=="ciksemel":
doc = ciksemel.parse(path)
if doc.getTag("Source").getTagData("Name") == self.source.name:
# Set source package translations
self._set_i18n(doc.getTag("Source"), self.source)
for pak in doc.tags("Package"):
for inst in self.packages:
if inst.name == pak.getTagData("Name"):
self._set_i18n(pak, inst)
break
else:
try:
doc = minidom.parse(path).documentElement
except ExpatError as err:
......@@ -443,14 +471,14 @@ class SpecFile(xmlfile.XmlFile, metaclass=autoxml.autoxml):
if doc.getElementsByTagName("Source")[0].getElementsByTagName("Name")[0].firstChild.data == self.source.name:
# Set source package translations
self._set_i18n(doc.getElementsByTagName("Source")[0], self.source)
self._set_i18n(doc.getElementsByTagName("Source")[0], self.source, parser="minidom")
#FIXME: How can we fix it
for pak in doc.childNodes:
if pak.nodeType == pak.ELEMENT_NODE and pak.tagName == "Package":
for inst in self.packages:
if inst.name == pak.getElementsByTagName("Name")[0].firstChild.data:
self._set_i18n(pak, inst)
self._set_i18n(pak, inst, parser="minidom")
break
def __str__(self):
......
......@@ -16,6 +16,7 @@ __trans = gettext.translation('inary', fallback=True)
_ = __trans.gettext
import inary
import inary.context as ctx
import inary.db.repodb
import inary.db.itembyrepo
import inary.data.component as Component
......@@ -25,6 +26,11 @@ class ComponentDB(lazydb.LazyDB):
def __init__(self):
lazydb.LazyDB.__init__(self, cacheable=True)
try:
import ciksemel
self.parser = "ciksemel"
except:
self.parser = "minidom"
def init(self):
component_nodes = {}
......@@ -43,27 +49,41 @@ class ComponentDB(lazydb.LazyDB):
self.cpdb = inary.db.itembyrepo.ItemByRepo(component_packages)
self.csdb = inary.db.itembyrepo.ItemByRepo(component_sources)
def __generate_packages(self, doc):
def __generate_packages(self, doc, ):
components = {}
if self.parser == "ciksemel":
for pkg in doc.tags("Package"):
components.setdefault(pkg.getTagData("PartOf"), []).append(pkg.getTagData("Name"))
else:
for pkg in doc.childNodes:
if pkg.nodeType == pkg.ELEMENT_NODE and pkg.tagName == "Package":
partOf = pkg.getElementsByTagName("PartOf")[0].firstChild.data
pkgName = pkg.getElementsByTagName("Name")[0].firstChild.data
components.setdefault(partOf, []).append(pkgName)
return components
def __generate_sources(self, doc):
components = {}
if self.parser == "ciksemel":
for spec in doc.tags("SpecFile"):
src = spec.getTag("Source")
components.setdefault(src.getTagData("PartOf"), []).append(src.getTagData("Name"))
else:
for spec in doc.childNodes:
if spec.nodeType == spec.ELEMENT_NODE and spec.tagName == "SpecFile":
src = spec.getElementsByTagName("Source")[0]
partOf = src.getElementsByTagName("PartOf")[0].firstChild.data
pkgName = src.getElementsByTagName("Name")[0].firstChild.data
components.setdefault(partOf, []).append(pkgName)
return components
def __generate_components(self, doc):
#return dict([(x.getTagData("Name"), x.toString()) for x in doc.tags("Component")])
if self.parser == "ciksemel":
return dict([(x.getTagData("Name"), x.toString()) for x in doc.tags("Component")])
else:
return dict([(x.getElementsByTagName("Name")[0].firstChild.data, x.toxml('utf-8')) for x in doc.getElementsByTagName("Component")])
def has_component(self, name, repo = None):
......
......@@ -15,6 +15,7 @@ __trans = gettext.translation('inary', fallback=True)
_ = __trans.gettext
import inary
import inary.context as ctx
import inary.db.repodb
import inary.db.itembyrepo
import inary.data.group as Group
......@@ -32,6 +33,12 @@ class GroupDB(lazydb.LazyDB):
group_nodes = {}
group_components = {}
try:
import ciksemel
self.parser = "ciksemel"
except:
self.parser = "minidom"
repodb = inary.db.repodb.RepoDB()
for repo in repodb.list_repos():
......@@ -44,6 +51,15 @@ class GroupDB(lazydb.LazyDB):
def __generate_components(self, doc):
groups = {}
if self.parser=="ciksemel":
for c in doc.tags("Component"):
group = c.getTagData("Group")
if not group:
group = "unknown"
groups.setdefault(group, []).append(c.getTagData("Name"))
else:
for c in doc.childNodes:
if c.nodeType == c.ELEMENT_NODE and c.tagName == "Component":
group = c.getElementsByTagName("Group")[0]
......@@ -53,6 +69,11 @@ class GroupDB(lazydb.LazyDB):
return groups
def __generate_groups(self, doc):
if self.parser=="ciksemel":
return dict([(x.getTagData("Name"), x.toString()) for x in doc.tags("Group")])
else:
return dict([(x.getElementsByTagName("Name")[0].firstChild.data, x.toxml()) \
for x in doc.childNodes if x.nodeType == x.ELEMENT_NODE and x.tagName == "Group"])
......
......@@ -19,15 +19,21 @@ import gettext
__trans = gettext.translation('inary', fallback=True)
_ = __trans.gettext
import xml.dom.minidom as minidom
# INARY
import inary
import inary.context as ctx
import inary.analyzer.dependency
import inary.data.files as Files
import inary.util
import inary.db.lazydb as lazydb
import inary.util
import inary.context as ctx
try:
import ciksemel
parser = "ciksemel"
except:
import xml.dom.minidom as minidom
parser = "minidom"
class InstallDBError(inary.Error):
pass
......@@ -92,6 +98,32 @@ class InstallDB(lazydb.LazyDB):
def __add_to_revdeps(self, package, revdeps):
metadata_xml = os.path.join(self.package_path(package), ctx.const.metadata_xml)
if parser=="ciksemel":
try:
meta_doc = ciksemel.parse(metadata_xml)
pkg = meta_doc.getTag("Package")
except:
pkg = None
if pkg is None:
# If package info is broken or not available, skip it.
ctx.ui.warning(_("Installation info for package '{}' is broken. "
"Reinstall it to fix this problem.").format(package))
del self.installed_db[package]
return
deps = pkg.getTag('RuntimeDependencies')
if deps:
for dep in deps.tags("Dependency"):
revdep = revdeps.setdefault(dep.firstChild().data(), {})
revdep[package] = dep.toString()
for anydep in deps.tags("AnyDependency"):
for dep in anydep.tags("Dependency"):
revdep = revdeps.setdefault(dep.firstChild().data(), {})
revdep[package] = anydep.toString()
else:
try:
meta_doc = minidom.parse(metadata_xml).documentElement
pkg = meta_doc.getElementsByTagName("Package")[0]
......@@ -147,6 +179,12 @@ class InstallDB(lazydb.LazyDB):
return found
def __get_version(self, meta_doc):
if parser== "ciksemel":
history = meta_doc.getTag("Package").getTag("History")
version = history.getTag("Update").getTagData("Version")
release = history.getTag("Update").getAttribute("release")
else:
history = meta_doc.getElementsByTagName("Package")[0].getElementsByTagName("History")[0]
version = history.getElementsByTagName("Update")[0].getElementsByTagName("Version")[0].firstChild.data
release = history.getElementsByTagName("Update")[0].getAttribute("release")[0]
......@@ -154,26 +192,42 @@ class InstallDB(lazydb.LazyDB):
return version, release, None
def __get_distro_release(self, meta_doc):
if parser== "ciksemel":
distro = meta_doc.getTag("Package").getTagData("Distribution")
release = meta_doc.getTag("Package").getTagData("DistributionRelease")
else:
distro = meta_doc.getElementsByTagName("Package")[0].getElementsByTagName("Distribution")[0].firstChild.data
release = meta_doc.getElementsByTagName("Package")[0].getElementsByTagName("DistributionRelease")[0].firstChild.data
return distro, release
def __get_install_tar_hash(self, meta_doc):
if parser== "ciksemel":
hash = meta_doc.getTag("Package").getTagData("InstallTarHash")
else:
hash = meta_doc.getElementsByTagName("Package")[0].getElementsByTagName("InstallTarHash")[0].firstChild.data
return hash
def get_install_tar_hash(self, package):
metadata_xml = os.path.join(self.package_path(package), ctx.const.metadata_xml)
if parser== "ciksemel":
meta_doc = ciksemel.parse(metadata_xml)
else:
meta_doc = minidom.parse(metadata_xml).documentElement
return self.__get_install_tar_hash(meta_doc)
def get_version_and_distro_release(self, package):
metadata_xml = os.path.join(self.package_path(package), ctx.const.metadata_xml)
if parser== "ciksemel":
meta_doc = ciksemel.parse(metadata_xml)
else:
meta_doc = minidom.parse(metadata_xml).documentElement
return self.__get_version(meta_doc) + self.__get_distro_release(meta_doc)
def get_version(self, package):
metadata_xml = os.path.join(self.package_path(package), ctx.const.metadata_xml)
if parser== "ciksemel":
meta_doc = ciksemel.parse(metadata_xml)
else:
meta_doc = minidom.parse(metadata_xml).documentElement
return self.__get_version(meta_doc)
......@@ -240,13 +294,22 @@ class InstallDB(lazydb.LazyDB):
return info
def __make_dependency(self, depStr):
if parser=="ciksemel":
node = ciksemel.parseString(depStr)
dependency = inary.analyzer.dependency.Dependency()
dependency.package = node.firstChild().data()
if node.attributes():
attr = node.attributes()[0]
dependency.__dict__[str(attr)] = node.getAttribute(str(attr))
else:
node = minidom.parseString(depStr).documentElement
dependency = inary.analyzer.dependency.Dependency()
dependency.package = node.childNodes[0].data
#FIXME: Bir sakatlık çıkacak
if node.attributes:
attr = node.attributes[0]
dependency.__dict__[str(attr)] = node.getAttribute(str(attr))
return dependency
def __create_dependency(self, depStr):
......
......@@ -43,7 +43,7 @@ class ItemByRepo:
for r in self.item_repos(repo):
if r in self.dbobj and item in self.dbobj[r]:
if self.compressed:
return gzip.zlib.decompress(self.dbobj[r][item]), r
return gzip.zlib.decompress(self.dbobj[r][item]).decode('utf-8') , r
else:
return self.dbobj[r][item], r
......@@ -82,7 +82,7 @@ class ItemByRepo:
if self.compressed:
for item in list(self.dbobj[r].keys()):
yield str(item), str(gzip.zlib.decompress(self.dbobj[r][item]))
yield str(item), gzip.zlib.decompress(self.dbobj[r][item]).decode('utf-8')
else:
for item in list(self.dbobj[r].keys()):
yield str(item), str(self.dbobj[r][item])
......
......@@ -15,16 +15,25 @@ import time
import gzip
import gettext
import datetime
__trans = gettext.translation('inary', fallback=True)
_ = __trans.gettext
import xml.dom.minidom as minidom
import inary.db
import inary.data.metadata as Metadata
import inary.analyzer.dependency
import inary.db.itembyrepo
import inary.db.lazydb as lazydb
import inary.context as ctx
import gettext
__trans = gettext.translation('inary', fallback=True)
_ = __trans.gettext
try:
import ciksemel
parser = "ciksemel"
except:
import xml.dom.minidom as minidom
parser = "minidom"
class PackageDB(lazydb.LazyDB):
......@@ -55,6 +64,10 @@ class PackageDB(lazydb.LazyDB):
## Generate functions look sooo ugly
def __generate_replaces(self, doc):
if parser== "ciksemel":
return [x.getTagData("Name") for x in doc.tags("Package") if x.getTagData("Replaces")]
else:
return [node.getElementsByTagName("Name")[0].firstChild.data \
for node in doc.childNodes \
if node.nodeType == node.ELEMENT_NODE and \
......@@ -63,6 +76,16 @@ class PackageDB(lazydb.LazyDB):
def __generate_obsoletes(self, doc):
if parser=="ciksemel":
distribution = doc.getTag("Distribution")
obsoletes = distribution and distribution.getTag("Obsoletes")
src_repo = doc.getTag("SpecFile") is not None
if not obsoletes or src_repo:
return []
return [x.firstChild().data() for x in obsoletes.tags("Package")]
else:
distribution = doc.getElementsByTagName("Distribution")[0]
obsoletes = distribution and distribution.getElementsByTagName("Obsoletes")[0]
try:
......@@ -77,6 +100,11 @@ class PackageDB(lazydb.LazyDB):
def __generate_packages(self, doc):
pdict={}
if parser=="ciksemel":
for x in doc.tags("Package"):
pdict[x.getTagData("Name")]= gzip.zlib.compress(x.toString().encode('utf-8'))
else:
for node in doc.childNodes:
if node.nodeType == node.ELEMENT_NODE and node.tagName == "Package":
name = node.getElementsByTagName('Name')[0].firstChild.data
......@@ -85,6 +113,15 @@ class PackageDB(lazydb.LazyDB):
def __generate_revdeps(self, doc):
revdeps = {}
if parser=="ciksemel":
for node in doc.tags("Package"):
name = node.getTagData('Name')
deps = node.getTag('RuntimeDependencies')
if deps:
for dep in deps.tags("Dependency"):
revdeps.setdefault(dep.firstChild().data(), set()).add((name, dep.toString()))
else:
for node in doc.childNodes:
if node.nodeType == node.ELEMENT_NODE and node.tagName == "Package":
name = node.getElementsByTagName('Name')[0].firstChild.data
......@@ -145,6 +182,11 @@ class PackageDB(lazydb.LazyDB):
return found
def __get_version(self, meta_doc):
if parser=="ciksemel":
history = meta_doc.getTag("History")
version = history.getTag("Update").getTagData("Version")
release = history.getTag("Update").getAttribute("release")
else:
history = meta_doc.getElementsByTagName("History")[0]
version = history.getElementsByTagName("Update")[0].getElementsByTagName("Version")[0].firstChild.data
release = history.getElementsByTagName("Update")[0].getAttribute["release"]
......@@ -153,6 +195,11 @@ class PackageDB(lazydb.LazyDB):
return version, release, None
def __get_distro_release(self, meta_doc):
if parser=="ciksemel":
distro = meta_doc.getTagData("Distribution")
release = meta_doc.getTagData("DistributionRelease")
else:
distro = meta_doc.getElementsByTagName("Distribution")[0].firstChild.data
release = meta_doc.getElementsByTagName("DistributionRelease")[0].firstChild.data
......@@ -162,6 +209,9 @@ class PackageDB(lazydb.LazyDB):
if not self.has_package(name, repo):
raise Exception(_('Package {} not found.').format(name))
if parser=="ciksemel":
pkg_doc = ciksemel.parseString(self.pdb.get_item(name, repo))
else:
pkg_doc = minidom.parseString(self.pdb.get_item(name, repo)).documentElement
return self.__get_version(pkg_doc) + self.__get_distro_release(pkg_doc)
......@@ -169,6 +219,9 @@ class PackageDB(lazydb.LazyDB):
if not self.has_package(name, repo):
raise Exception(_('Package {} not found.').format(name))
if parser=="ciksemel":
pkg_doc = ciksemel.parseString(self.pdb.get_item(name, repo))
else:
pkg_doc = minidom.parseString(self.pdb.get_item(name, repo)).documentElement
return self.__get_version(pkg_doc)
......@@ -188,6 +241,16 @@ class PackageDB(lazydb.LazyDB):
repodb = inary.db.repodb.RepoDB()
packages = set()
if parser=="ciksemel":
for repo in repodb.list_repos():
doc = repodb.get_repo_doc(repo)
for package in doc.tags("Package"):
if package.getTagData("IsA"):
for node in package.tags("IsA"):
if node.firstChild().data() == isa:
packages.add(package.getTagData("Name"))
else:
for repo in repodb.list_repos():
doc = repodb.get_repo_doc(repo)
for package in doc.getElementsByTagName("Package"):
......@@ -204,6 +267,18 @@ class PackageDB(lazydb.LazyDB):
return []
rev_deps = []
if parser=="ciksemel":
for pkg, dep in rvdb:
node = ciksemel.parseString(dep)
dependency = inary.analyzer.dependency.Dependency()
dependency.package = node.firstChild().data()
if node.attributes():
attr = node.attributes()[0]
dependency.__dict__[attr] = node.getAttribute(attr)
rev_deps.append((pkg, dependency))
else:
for pkg, dep in rvdb:
node = minidom.parseString(dep).documentElement
dependency = inary.analyzer.dependency.Dependency()
......@@ -213,12 +288,27 @@ class PackageDB(lazydb.LazyDB):
attr = node.attributes[0]
dependency.__dict__[attr] = node.getAttribute(attr)
rev_deps.append((pkg, dependency))
return rev_deps
# replacesdb holds the info about the replaced packages (ex. gaim -> pidgin)
def get_replaces(self, repo=None):
pairs = {}
if parser=="ciksemel":
for pkg_name in self.rpdb.get_list_item():
xml = self.pdb.get_item(pkg_name, repo)
package = ciksemel.parseString(str(xml))
replaces_tag = package.getTag("Replaces")
if replaces_tag:
for node in replaces_tag.tags("Package"):
r = inary.data.relation.Relation()
# XXX Is there a better way to do this?
r.decode(node, [])
if inary.data.replace.installed_package_replaced(r):
pairs.setdefault(r.package, []).append(pkg_name)
else:
for pkg_name in self.rpdb.get_list_item():
xml = self.pdb.get_item(pkg_name, repo)
package = minidom.parseString(xml)
......
......@@ -19,9 +19,6 @@ _ = __trans.gettext
import os
import xml.dom.minidom as minidom
from xml.parsers.expat import ExpatError
import inary
import inary.uri
import inary.util
......@@ -30,6 +27,13 @@ import inary.db.lazydb as lazydb
from inary.file import File
import inary.sxml.xmlext as xmlext
try:
import ciksemel
parser = "ciksemel"
except:
import xml.dom.minidom as minidom
parser = "minidom"
class RepoError(inary.Error):
pass
......@@ -48,7 +52,29 @@ class RepoOrder:
self._doc = None
self.repos = self._get_repos()
def add(self, repo_name, repo_url, repo_type="remote"):
if parser == "ciksemel":
repo_doc = self._get_doc()
try:
node = [x for x in repo_doc.tags("Repo")][-1]
repo_node = node.appendTag("Repo")
except IndexError:
repo_node = repo_doc.insertTag("Repo")
name_node = repo_node.insertTag("Name")
name_node.insertData(repo_name)
url_node = repo_node.insertTag("Url")
url_node.insertData(repo_url)
name_node = repo_node.insertTag("Status")
name_node.insertData("active")
media_node = repo_node.insertTag("Media")
media_node.insertData(repo_type)
else:
repo_doc = self._get_doc().ownerDocument
repo_node = repo_doc.createElement("Repo")
......@@ -75,6 +101,19 @@ class RepoOrder:
def set_status(self, repo_name, status):
repo_doc = self._get_doc()
if parser=="ciksemel":
for r in repo_doc.tags("Repo"):
if r.getTagData("Name") == repo_name:
status_node = r.getTag("Status")
if status_node:
status_node.firstChild().hide()
status_node.insertData(status)
else:
status_node = r.insertTag("Status")
status_node.insertData(status)
else:
for r in repo_doc.childNodes:
if r.nodeType == r.ELEMENT_NODE and r.tagName == "Repo":
if r.getElementsByTagName("Name")[0].firstChild.data == repo_name:
......@@ -86,10 +125,21 @@ class RepoOrder:
status_node.appendChild(repo_doc.createTextNode("active"))
r.appendChild(status_node)
self._update(repo_doc)
def get_status(self, repo_name):
repo_doc = self._get_doc()
if parser=="ciksemel":
for r in repo_doc.tags("Repo"):
if r.getTagData("Name") == repo_name:
status_node = r.getTag("Status")
if status_node:
status = status_node.firstChild().data()
if status in ["active", "inactive"]:
return status
else:
for r in repo_doc.childNodes:
if r.nodeType == r.ELEMENT_NODE and r.tagName == "Repo":
if r.getElementsByTagName("Name")[0].firstChild.data == repo_name:
......@@ -98,11 +148,17 @@ class RepoOrder:
status = status_node[0].childNodes[0].data
if status in ["active", "inactive"]:
return status
return "inactive"
def remove(self, repo_name):
repo_doc = self._get_doc()
if parser=="ciksemel":
for r in repo_doc.tags("Repo"):
if r.getTagData("Name") == repo_name:
r.hide()
else:
for r in repo_doc.getElementsByTagName("Repo"):
if r.getElementsByTagName("Name")[0].firstChild.data == repo_name:
repo_doc.removeChild(r)
......@@ -121,7 +177,12 @@ class RepoOrder:
def _update(self, doc):
repos_file = os.path.join(ctx.config.info_dir(), ctx.const.repos)
if parser=="ciksemel":
open(repos_file, "w").write("{}\n".format(doc.toPrettyString()))
else:
open(repos_file, "w").write("{}\n".format(doc.toprettyxml().strip()))
self._doc = None
self.repos = self._get_repos()
......@@ -129,7 +190,13 @@ class RepoOrder:
if self._doc is None:
repos_file = os.path.join(ctx.config.info_dir(), ctx.const.repos)
if os.path.exists(repos_file):
if parser=="ciksemel":
self._doc = ciksemel.parse(repos_file)
else:
self._doc = minidom.parse(repos_file).documentElement
else:
if parser=="ciksemel":
self._doc = ciksemel.newDocument("REPOS")
else:
impl = minidom.getDOMImplementation()
dom = impl.createDocument(None, "REPOS", None)
......@@ -141,6 +208,14 @@ class RepoOrder:
repo_doc = self._get_doc()
order = {}
if parser=="ciksemel":
for r in repo_doc.tags("Repo"):
media = r.getTagData("Media")
name = r.getTagData("Name")
status = r.getTagData("Status")
order.setdefault(media, []).append(name)
else:
for r in repo_doc.childNodes:
if r.nodeType == r.ELEMENT_NODE and r.tagName == "Repo":
media = r.getElementsByTagName("Media")[0].firstChild.data
......@@ -177,14 +252,20 @@ class RepoDB(lazydb.LazyDB):
if not os.path.exists(index_path):
ctx.ui.warning(_("{} repository needs to be updated").format(repo_name))
if parser=="ciksemel":
return ciksemel.newDocument("INARY")
else:
impl = minidom.getDOMImplementation()
dom = impl.createDocument(None, "INARY", None)
return dom.documentElement
try:
if parser=="ciksemel":
return ciksemel.parse(index_path)
else:
return minidom.parse(index_path).documentElement
except ExpatError as e:
raise RepoError(_("Error parsing repository index information. Index file does not exist or is malformed."))
except Exception as e:
raise RepoError(_("Error parsing repository index information: {} \n Index file does not exist or is malformed.").format(e))
def get_repo(self, repo):
return Repo(inary.uri.URI(self.get_repo_url(repo)))
......@@ -214,6 +295,13 @@ class RepoDB(lazydb.LazyDB):
def get_source_repos(self, only_active=True):
repos = []
if parser=="ciksemel":
for r in self.list_repos(only_active):
if self.get_repo_doc(r).getTag("SpecFile"):
repos.append(r)
else:
for r in self.list_repos(only_active):
if self.get_repo_doc(r).getElementsByTagName("SpecFile")[0]:
repos.append(r)
......@@ -221,6 +309,13 @@ class RepoDB(lazydb.LazyDB):
def get_binary_repos(self, only_active=True):
repos = []
if parser=="ciksemel":
for r in self.list_repos(only_active):
if self.get_repo_doc(r).getTag("SpecFile"):
repos.append(r)
else:
for r in self.list_repos(only_active):
if not self.get_repo_doc(r).getElementsByTagName("SpecFile")[0]:
repos.append(r)
......@@ -260,14 +355,27 @@ class RepoDB(lazydb.LazyDB):
def get_distribution(self, name):
doc = self.get_repo_doc(name)
if parser=="ciksemel":
distro = doc.getTag("Distribution")
return distro and distro.getTagData("SourceName")
else:
distro = doc.getElementsByTagName("Distribution")[0]
return distro.firstChild.data and distro.getElementsByTagName("SourceName")[0].firstChild.data
def get_distribution_release(self, name):
doc = self.get_repo_doc(name)
if parser=="ciksemel":
distro = doc.getTag("Distribution")
return distro and distro.getTagData("Version")
else:
distro = doc.getElementsByTagName("Distribution")[0]
return distro.firstChild.data and distro.getElementsByTagName("Version")[0].firstChild.data
def check_distribution(self, name):
if ctx.get_option('ignore_check'):
return
......
......@@ -13,12 +13,22 @@
import re
import gzip
import xml.dom.minidom as minidom
import inary
import inary.context as ctx
import inary.data.specfile as Specfile
import inary.db.lazydb as lazydb
import gettext
__trans = gettext.translation('inary', fallback=True)
_ = __trans.gettext
try:
import ciksemel
parser = "ciksemel"
except:
import xml.dom.minidom as minidom
parser = "minidom"
class SourceDB(lazydb.LazyDB):
def __init__(self):
......@@ -44,6 +54,14 @@ class SourceDB(lazydb.LazyDB):
sources = {}
pkgstosrc = {}
if parser=="ciksemel":
for spec in doc.tags("SpecFile"):
src_name = spec.getTag("Source").getTagData("Name")
sources[src_name] = gzip.zlib.compress(spec.toString().encode('utf-8'))
for package in spec.tags("Package"):
pkgstosrc[package.getTagData("Name")] = src_name
else:
for spec in doc.childNodes:
if spec.nodeType == spec.ELEMENT_NODE and spec.tagName == "SpecFile":
src_name = spec.getElementsByTagName("Source")[0].getElementsByTagName("Name")[0].firstChild.data
......@@ -56,6 +74,15 @@ class SourceDB(lazydb.LazyDB):
def __generate_revdeps(self, doc):
revdeps = {}
if parser=="ciksemel":
for spec in doc.tags("SpecFile"):
name = spec.getTag("Source").getTagData("Name")
deps = spec.getTag("Source").getTag("BuildDependencies")
if deps:
for dep in deps.tags("Dependency"):
revdeps.setdefault(dep.firstChild().data(), set()).add((name, dep.toString()))
else:
for spec in doc.childNodes:
if spec.nodeType == spec.ELEMENT_NODE and spec.tagName == "SpecFile":
source = spec.getElementsByTagName("Source")[0]
......@@ -128,6 +155,18 @@ class SourceDB(lazydb.LazyDB):
return []
rev_deps = []
if parser=="ciksemel":
for pkg, dep in rvdb:
node = ciksemel.parseString(dep)
dependency = inary.analyzer.dependency.Dependency()
dependency.package = node.firstChild().data()
if node.attributes():
attr = node.attributes()[0]
dependency.__dict__[attr] = node.getAttribute(attr)
rev_deps.append((pkg, dependency))
else:
for pkg, dep in rvdb:
node = minidom.parseString(dep).documentElement
dependency = inary.analyzer.dependency.Dependency()
......
......@@ -10,6 +10,7 @@
# Please read the COPYING file.
#
import inary
import inary.context as ctx
import inary.db
def search_package(terms, lang=None, repo=None):
......
......@@ -21,13 +21,11 @@
# System
import locale
import types
import formatter
import sys
import io
import inspect
import re
import codecs
import gettext
__trans = gettext.translation('inary', fallback=True)
......@@ -528,7 +526,10 @@ class autoxml(oo.autosuper, oo.autoprop):
tag_type = spec[0]
assert type(tag_type) == type(type)
def readtext(node, blah):
node.normalize() # minidom have this :D
try:
node.normalize() # ciksemel doesn't have this
except:
pass
return xmlext.getNodeText(node)
def writetext(node, blah, text):
xmlext.addText(node, "", text)
......@@ -704,6 +705,7 @@ class autoxml(oo.autosuper, oo.autoprop):
def encode(node, obj, errs):
if node and obj:
try:
#FIXME: this doesn't look pretty
classnode = xmlext.newNode(node, tag)
obj.encode(classnode, errs)
xmlext.addNode(node, '', classnode)
......@@ -823,6 +825,7 @@ class autoxml(oo.autosuper, oo.autoprop):
def encode(node, obj, errs):
if node and obj:
try:
#FIXME: this doesn't look pretty
obj.encode(node, errs)
except Error:
if req == mandatory:
......
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 - 2018, Suleyman POYRAZ (Zaryob)
# Copyright (C) 2018, Suleyman POYRAZ (Zaryob)
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
......@@ -10,176 +10,8 @@
# Please read the COPYING file.
#
"""
xmlext is a helper module for accessing XML files using
xml.dom.minidom . It is a convenient wrapper for some
DOM functions, and provides path based get/add functions
as in KDE API.
try:
from .xmlext_iks import *
function names are mixedCase for compatibility with minidom,
an 'old library'
this implementation uses pciksemel
"""
import gettext
__trans = gettext.translation('inary', fallback=True)
_ = __trans.gettext
import inary
import xml.dom.minidom as minidom
from xml.parsers.expat import ExpatError
def newDocument(tag):
impl = minidom.getDOMImplementation()
dom = impl.createDocument(None, tag, None)
return dom.documentElement
def parse(fileName):
try:
dom = minidom.parse(fileName)
return dom.documentElement
except ExpatError as inst:
raise Error(_("File '{}' has invalid XML: {}\n").format(fileName,
str(inst)))
def getAllNodes(node, tagPath):
"""retrieve all nodes that match a given tag path."""
tags = tagPath.split('/')
if len(tags) == 0:
return []
nodeList = [node] # basis case
for tag in tags:
results = [getTagByName(x, tag) for x in nodeList]
nodeList = []
for x in results:
nodeList.extend(x)
pass # emacs indentation error, keep it here
if len(nodeList) == 0:
return []
return nodeList
def getNodeAttribute(node, attrname):
"""get named attribute from DOM node"""
if not node.hasAttribute(attrname):
return None
return node.getAttribute(attrname)
def setNodeAttribute(node, attrname, value):
"""get named attribute from DOM node"""
return node.setAttribute(attrname, value)
def getChildElts(parent):
"""get only child elements"""
return [x for x in parent.childNodes if x.nodeType == x.ELEMENT_NODE]
def getTagByName(parent, childName):
return [x for x in parent.childNodes
if x.nodeType == x.ELEMENT_NODE and x.tagName == childName]
def getNodeText(node, tagpath = ""):
"""get the first child and expect it to be text!"""
if tagpath!="":
node = getNode(node, tagpath)
try:
child = node.childNodes[0]
except IndexError:
return None
except AttributeError: # no node by that name
return None
if child.nodeType == child.TEXT_NODE:
#print('child_data=', child_data.strip())
return child.data.strip() # in any case, strip whitespaces...
else:
raise XmlError(_("getNodeText: Expected text node, got something else!"))
def getChildText(node_s, tagpath):
"""get the text of a child at the end of a tag path"""
node = getNode(node_s, tagpath)
if not node:
return None
return getNodeText(node)
def getNode(node, tagpath):
"""returns the *first* matching node for given tag path."""
if tagpath == "":
return node
assert type(tagpath)==str
tags = tagpath.split('/')
assert len(tags)>0
# iterative code to search for the path
for tag in tags:
currentNode = None
for child in node.childNodes:
if child.nodeType == node.ELEMENT_NODE and child.tagName == tag:
currentNode = child
break
if not currentNode:
return None
else:
node = currentNode
return currentNode
def createTagPath(node, tags):
"""create new child at the end of a tag chain starting from node
no matter what"""
if len(tags)==0:
return node
dom = node.ownerDocument
for tag in tags:
node = node.appendChild(dom.createElement(tag))
return node
def addTagPath(node, tags, newnode=None):
"""add newnode at the end of a tag chain, smart one"""
node = createTagPath(node, tags)
if newnode: # node to add specified
node.appendChild(newnode)
return node
def addNode(node, tagpath, newnode = None, branch=True):
"""add a new node at the end of the tree and returns it
if newnode is given adds that node, too."""
assert type(tagpath)==str
tags = []
if tagpath != "":
tags = tagpath.split('/') # tag chain
else:
addTagPath(node, [], newnode)
return node
assert len(tags)>0 # we want a chain
# iterative code to search for the path
if branch:
rem = 1
else:
rem = 0
while len(tags) > rem:
tag = tags.pop(0)
nodeList = getTagByName(node, tag)
if len(nodeList) == 0: # couldn't find
tags.insert(0, tag) # put it back in
return addTagPath(node, tags, newnode)
else:
node = nodeList[len(nodeList)-1] # discard other matches
else:
# had only one tag..
return addTagPath(node, tags, newnode)
return node
def addText(node, tagPath, text, branch = True):
newnode = node.ownerDocument.createTextNode(text)
return addNode(node, tagPath, newnode, branch = branch)
def newNode(node, tag):
return node.ownerDocument.createElement(tag)
except:
from .xmlext_minidom import *
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 - 2018, Suleyman POYRAZ (Zaryob)
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.
#
"""
xmlext is a helper module for accessing XML files using
xml.dom.minidom . It is a convenient wrapper for some
DOM functions, and provides path based get/add functions
as in KDE API.
function names are mixedCase for compatibility with minidom,
an 'old library'
this implementation uses ciksemel
"""
import gettext
__trans = gettext.translation('inary', fallback=True)
_ = __trans.gettext
import inary
import ciksemel as iks
parse = iks.parse
newDocument = iks.newDocument
def getAllNodes(node, tagPath):
"""retrieve all nodes that match a given tag path."""
tags = tagPath.split('/')
if len(tags) == 0:
return []
nodeList = [node] # basis case
for tag in tags:
results = [getTagByName(x, tag) for x in nodeList]
nodeList = []
for x in results:
nodeList.extend(x)
pass # emacs indentation error, keep it here
if len(nodeList) == 0:
return []
return nodeList
def getNodeAttribute(node, attrname):
"""get named attribute from DOM node"""
return node.getAttribute(attrname)
def setNodeAttribute(node, attrname, value):
"""get named attribute from DOM node"""
return node.setAttribute(attrname, value)
def getChildElts(parent):
"""get only child elements"""
return [x for x in parent.tags()]
def getTagByName(parent, childName):
return [x for x in parent.tags(childName)]
def getNodeText(node, tagpath = ""):
"""get the first child and expect it to be text!"""
if tagpath!="":
node = getNode(node, tagpath)
if not node:
return None
child = node.firstChild()
if not child:
return None
if child.type() == iks.DATA:
# KLUDGE: FIXME: python 2.x bug: force decoding as UTF-8
child_data = child.data()
#print('child_data=', child_data.strip())
return child_data.strip() # in any case, strip whitespaces...
else:
raise XmlError(_("getNodeText: Expected text node, got something else!"))
def getChildText(node_s, tagpath):
"""get the text of a child at the end of a tag path"""
node = getNode(node_s, tagpath)
if not node:
return None
return getNodeText(node)
def getNode(node, tagpath):
"""returns the *first* matching node for given tag path."""
if tagpath == "":
return node
assert type(tagpath)==str
tags = tagpath.split('/')
assert len(tags)>0
# iterative code to search for the path
for tag in tags:
currentNode = None
for child in node.tags():
if child.name() == tag:
currentNode = child
break
if not currentNode:
return None
else:
node = currentNode
return currentNode
def createTagPath(node, tags):
"""create new child at the end of a tag chain starting from node
no matter what"""
if len(tags)==0:
return node
for tag in tags:
node = node.insertTag(tag)
return node
def addTagPath(node, tags, newnode=None):
"""add newnode at the end of a tag chain, smart one"""
node = createTagPath(node, tags)
if newnode: # node to add specified
node.insertNode(newnode)
return node
def addNode(node, tagpath, newnode = None, branch=True):
"""add a new node at the end of the tree and returns it
if newnode is given adds that node, too."""
assert type(tagpath)==str
tags = []
if tagpath != "":
tags = tagpath.split('/') # tag chain
else:
addTagPath(node, [], newnode)
return node #FIXME: is this correct!?!?
assert len(tags)>0 # we want a chain
# iterative code to search for the path
if branch:
rem = 1
else:
rem = 0
while len(tags) > rem:
tag = tags.pop(0)
nodeList = getTagByName(node, tag)
if len(nodeList) == 0: # couldn't find
tags.insert(0, tag) # put it back in
return addTagPath(node, tags, newnode)
else:
node = nodeList[len(nodeList)-1] # discard other matches
else:
# had only one tag..
return addTagPath(node, tags, newnode)
return node
def addText(node, tagpath, text):
node = addNode(node, tagpath)
node.insertData(text)
def newNode(node, tag):
return iks.newDocument(tag)
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 - 2018, Suleyman POYRAZ (Zaryob)
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.
#
"""
xmlext is a helper module for accessing XML files using
xml.dom.minidom . It is a convenient wrapper for some
DOM functions, and provides path based get/add functions
as in KDE API.
function names are mixedCase for compatibility with minidom,
an 'old library'
this implementation uses pciksemel
"""
import gettext
__trans = gettext.translation('inary', fallback=True)
_ = __trans.gettext
import inary
import xml.dom.minidom as minidom
from xml.parsers.expat import ExpatError
def newDocument(tag):
impl = minidom.getDOMImplementation()
dom = impl.createDocument(None, tag, None)
return dom.documentElement
def parse(fileName):
try:
dom = minidom.parse(fileName)
return dom.documentElement
except ExpatError as inst:
raise Error(_("File '{}' has invalid XML: {}\n").format(fileName,
str(inst)))
def getAllNodes(node, tagPath):
"""retrieve all nodes that match a given tag path."""
tags = tagPath.split('/')
if len(tags) == 0:
return []
nodeList = [node] # basis case
for tag in tags:
results = [getTagByName(x, tag) for x in nodeList]
nodeList = []
for x in results:
nodeList.extend(x)
pass # emacs indentation error, keep it here
if len(nodeList) == 0:
return []
return nodeList
def getNodeAttribute(node, attrname):
"""get named attribute from DOM node"""
if not node.hasAttribute(attrname):
return None
return node.getAttribute(attrname)
def setNodeAttribute(node, attrname, value):
"""get named attribute from DOM node"""
return node.setAttribute(attrname, value)
def getChildElts(parent):
"""get only child elements"""
return [x for x in parent.childNodes if x.nodeType == x.ELEMENT_NODE]
def getTagByName(parent, childName):
return [x for x in parent.childNodes
if x.nodeType == x.ELEMENT_NODE and x.tagName == childName]
def getNodeText(node, tagpath = ""):
"""get the first child and expect it to be text!"""
if tagpath!="":
node = getNode(node, tagpath)
try:
child = node.childNodes[0]
except IndexError:
return None
except AttributeError: # no node by that name
return None
if child.nodeType == child.TEXT_NODE:
#print('child_data=', child_data.strip())
return child.data.strip() # in any case, strip whitespaces...
else:
raise XmlError(_("getNodeText: Expected text node, got something else!"))
def getChildText(node_s, tagpath):
"""get the text of a child at the end of a tag path"""
node = getNode(node_s, tagpath)
if not node:
return None
return getNodeText(node)
def getNode(node, tagpath):
"""returns the *first* matching node for given tag path."""
if tagpath == "":
return node
assert type(tagpath)==str
tags = tagpath.split('/')
assert len(tags)>0
# iterative code to search for the path
for tag in tags:
currentNode = None
for child in node.childNodes:
if child.nodeType == node.ELEMENT_NODE and child.tagName == tag:
currentNode = child
break
if not currentNode:
return None
else:
node = currentNode
return currentNode
def createTagPath(node, tags):
"""create new child at the end of a tag chain starting from node
no matter what"""
if len(tags)==0:
return node
dom = node.ownerDocument
for tag in tags:
node = node.appendChild(dom.createElement(tag))
return node
def addTagPath(node, tags, newnode=None):
"""add newnode at the end of a tag chain, smart one"""
node = createTagPath(node, tags)
if newnode: # node to add specified
node.appendChild(newnode)
return node
def addNode(node, tagpath, newnode = None, branch=True):
"""add a new node at the end of the tree and returns it
if newnode is given adds that node, too."""
assert type(tagpath)==str
tags = []
if tagpath != "":
tags = tagpath.split('/') # tag chain
else:
addTagPath(node, [], newnode)
return node
assert len(tags)>0 # we want a chain
# iterative code to search for the path
if branch:
rem = 1
else:
rem = 0
while len(tags) > rem:
tag = tags.pop(0)
nodeList = getTagByName(node, tag)
if len(nodeList) == 0: # couldn't find
tags.insert(0, tag) # put it back in
return addTagPath(node, tags, newnode)
else:
node = nodeList[len(nodeList)-1] # discard other matches
else:
# had only one tag..
return addTagPath(node, tags, newnode)
return node
def addText(node, tagPath, text, branch = True):
newnode = node.ownerDocument.createTextNode(text)
return addNode(node, tagPath, newnode, branch = branch)
def newNode(node, tag):
return node.ownerDocument.createElement(tag)
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 - 2018, Suleyman POYRAZ (Zaryob)
# Copyright (C) 2018, Suleyman POYRAZ (Zaryob)
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
......@@ -10,88 +10,8 @@
# Please read the COPYING file.
#
try:
from .xmlfile_iks import *
"""
XmlFile class further abstracts a dom object using the
high-level dom functions provided in xmlext module (and sorely lacking
in xml.dom :( )
function names are mixedCase for compatibility with minidom,
an 'old library'
"""
import gettext
__trans = gettext.translation('inary', fallback=True)
_ = __trans.gettext
import io
import xml.dom.minidom as minidom
from xml.parsers.expat import ExpatError
import inary
from inary.file import File
from inary.util import join_path as join
class Error(inary.Error):
pass
class XmlFile(object):
"""A class to help reading and writing an XML file"""
def __init__(self, tag):
self.rootTag = tag
def newDocument(self):
"""clear DOM"""
impl = minidom.getDOMImplementation()
self.doc = impl.createDocument(None, self.rootTag, None)
def unlink(self):
"""deallocate DOM structure"""
self.doc.unlink()
del self.doc
def rootNode(self):
"""returns root document element"""
return self.doc.documentElement
def parsexml(self, file):
try:
self.doc = minidom.parseString(file)
return self.doc.documentElement
except Exception as e:
raise Error(_("File '{}' has invalid XML").format(file) )
def readxml(self, uri, tmpDir='/tmp', sha1sum=False,
compress=None, sign=None, copylocal = False):
uri = File.make_uri(uri)
try:
localpath = File.download(uri, tmpDir, sha1sum=sha1sum,
compress=compress,sign=sign, copylocal=copylocal)
except IOError as e:
raise Error(_("Cannot read URI {0}: {1}").format(uri, str(e)) )
st = io.StringIO()
try:
from preprocess import preprocess, PreprocessError
preprocess(infile=localpath,outfile=st,defines=inary.config.Config().values.directives)
st.seek(0)
except:
st = open(localpath,'r')
try:
self.doc = minidom.parse(localpath)
return self.doc.documentElement
except ExpatError as err:
raise Error(_("File '{}' has invalid XML: {}\n").format(localpath,
str(err)))
def writexml(self, uri, tmpDir = '/tmp', sha1sum=False, compress=None, sign=None):
f = inary.file.File(uri, inary.file.File.write, sha1sum=sha1sum, compress=compress, sign=sign)
f.write(self.doc.toprettyxml())
f.close()
def writexmlfile(self, f):
f.write(self.doc.toprettyxml())
except:
from .xmlfile_minidom import *
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 - 2018, Suleyman POYRAZ (Zaryob)
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.
#
"""
XmlFile class further abstracts a dom object using the
high-level dom functions provided in xmlext module (and sorely lacking
in xml.dom :( )
function names are mixedCase for compatibility with minidom,
an 'old library'
this implementation uses ciksemel
"""
import gettext
__trans = gettext.translation('inary', fallback=True)
_ = __trans.gettext
import io
import ciksemel as iks
import inary
from inary.file import File
from inary.util import join_path as join
class Error(inary.Error):
pass
class XmlFile(object):
"""A class to help reading and writing an XML file"""
def __init__(self, tag):
self.rootTag = tag
def newDocument(self):
"""clear DOM"""
self.doc = iks.newDocument(self.rootTag)
def unlink(self):
"""deallocate DOM structure"""
del self.doc
def rootNode(self):
"""returns root document element"""
return self.doc
def parsexml(self, file):
#try:
self.doc = iks.parseString(str(file))
return self.doc
#except Exception as e:
#raise Error(_("File '{}' has invalid XML").format(file) )
def readxml(self, uri, tmpDir='/tmp', sha1sum=False,
compress=None, sign=None, copylocal = False):
uri = File.make_uri(uri)
try:
localpath = File.download(uri, tmpDir, sha1sum=sha1sum,
compress=compress,sign=sign, copylocal=copylocal)
except IOError as e:
raise Error(_("Cannot read URI {0}: {1}").format(uri, str(e)) )
st = io.StringIO()
try:
from preprocess import preprocess, PreprocessError
preprocess(infile=localpath,outfile=st,defines=inary.config.Config().values.directives)
st.seek(0)
except:
st = open(localpath,'r')
try:
self.doc = iks.parse(localpath)
return self.doc
except Exception as e:
raise Error(_("File '{}' has invalid XML").format(localpath) )
def writexml(self, uri, tmpDir = '/tmp', sha1sum=False, compress=None, sign=None):
f = inary.file.File(uri, inary.file.File.write, sha1sum=sha1sum, compress=compress, sign=sign)
f.write(self.doc.toPrettyString())
f.close()
def writexmlfile(self, f):
f.write(self.doc.toPrettyString())
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 - 2018, Suleyman POYRAZ (Zaryob)
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.
#
"""
XmlFile class further abstracts a dom object using the
high-level dom functions provided in xmlext module (and sorely lacking
in xml.dom :( )
function names are mixedCase for compatibility with minidom,
an 'old library'
"""
import gettext
__trans = gettext.translation('inary', fallback=True)
_ = __trans.gettext
import io
import xml.dom.minidom as minidom
from xml.parsers.expat import ExpatError
import inary
from inary.file import File
from inary.util import join_path as join
class Error(inary.Error):
pass
class XmlFile(object):
"""A class to help reading and writing an XML file"""
def __init__(self, tag):
self.rootTag = tag
def newDocument(self):
"""clear DOM"""
impl = minidom.getDOMImplementation()
self.doc = impl.createDocument(None, self.rootTag, None)
def unlink(self):
"""deallocate DOM structure"""
self.doc.unlink()
del self.doc
def rootNode(self):
"""returns root document element"""
return self.doc.documentElement
def parsexml(self, file):
try:
self.doc = minidom.parseString(file)
return self.doc.documentElement
except Exception as e:
raise Error(_("File '{}' has invalid XML").format(file) )
def readxml(self, uri, tmpDir='/tmp', sha1sum=False,
compress=None, sign=None, copylocal = False):
uri = File.make_uri(uri)
try:
localpath = File.download(uri, tmpDir, sha1sum=sha1sum,
compress=compress,sign=sign, copylocal=copylocal)
except IOError as e:
raise Error(_("Cannot read URI {0}: {1}").format(uri, str(e)) )
st = io.StringIO()
try:
from preprocess import preprocess, PreprocessError
preprocess(infile=localpath,outfile=st,defines=inary.config.Config().values.directives)
st.seek(0)
except:
st = open(localpath,'r')
try:
self.doc = minidom.parse(localpath)
return self.doc.documentElement
except ExpatError as err:
raise Error(_("File '{}' has invalid XML: {}\n").format(localpath,
str(err)))
def writexml(self, uri, tmpDir = '/tmp', sha1sum=False, compress=None, sign=None):
f = inary.file.File(uri, inary.file.File.write, sha1sum=sha1sum, compress=compress, sign=sign)
f.write(self.doc.toprettyxml())
f.close()
def writexmlfile(self, f):
f.write(self.doc.toprettyxml())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment