def test_5027_2(self): # The xml prefix (as in xml:lang below) is reserved and bound by # definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had # a bug whereby a KeyError is raised because this namespace is missing # from a dictionary. # # This test demonstrates the bug by direct manipulation of the # XMLGenerator. result = StringIO() gen = XMLGenerator(result) gen.startDocument() gen.startPrefixMapping("a", "http://example.com/ns") gen.startElementNS(("http://example.com/ns", "g1"), "g1", {}) lang_attr = {("http://www.w3.org/XML/1998/namespace", "lang"): "en"} gen.startElementNS(("http://example.com/ns", "g2"), "g2", lang_attr) gen.characters("Hello") gen.endElementNS(("http://example.com/ns", "g2"), "g2") gen.endElementNS(("http://example.com/ns", "g1"), "g1") gen.endPrefixMapping("a") gen.endDocument() self.assertEqual( result.getvalue(), start + ('<a:g1 xmlns:a="http://example.com/ns">' '<a:g2 xml:lang="en">Hello</a:g2>' "</a:g1>"), )
def test_5027_2(self): # The xml prefix (as in xml:lang below) is reserved and bound by # definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had # a bug whereby a KeyError is thrown because this namespace is missing # from a dictionary. # # This test demonstrates the bug by direct manipulation of the # XMLGenerator. result = StringIO() gen = XMLGenerator(result) gen.startDocument() gen.startPrefixMapping('a', 'http://example.com/ns') gen.startElementNS(('http://example.com/ns', 'g1'), 'g1', {}) lang_attr = {('http://www.w3.org/XML/1998/namespace', 'lang'): 'en'} gen.startElementNS(('http://example.com/ns', 'g2'), 'g2', lang_attr) gen.characters('Hello') gen.endElementNS(('http://example.com/ns', 'g2'), 'g2') gen.endElementNS(('http://example.com/ns', 'g1'), 'g1') gen.endPrefixMapping('a') gen.endDocument() self.assertEqual(result.getvalue(), start + ( '<a:g1 xmlns:a="http://example.com/ns">' '<a:g2 xml:lang="en">Hello</a:g2>' '</a:g1>'))
class XMLTweets(): ns = "http://www.milowski.com/twitter" out = None xml = None def open(self,filename): self.out = open(filename,"w") self.xml = XMLGenerator(self.out,"utf-8") self.xml.startDocument() self.xml.startPrefixMapping("",self.ns) self.xml.startElementNS((self.ns,"tweets"),"tweets",{}) self.xml.characters("\n") def tweet(self,tweet): self.xml.startElementNS((self.ns,"tweet"),"tweet",{(None,"alias"):tweet.user.screen_name, (None,"created"): tweet.created_at.strftime("%Y-%m-%dT%H-%M-%SZ")}) self.xml.characters(tweet.text) self.xml.endElementNS((self.ns,"tweet"),"tweet") self.xml.characters("\n") def close(self): if self.out != None: self.xml.endElementNS((self.ns,"tweets"),"tweets") self.xml.endDocument() self.out.close() self.xml = None self.out = None
class xmlreport: def __init__(self, filename, xslfile): self.fp = open(filename, "wb") self.xmlgen = XMLGenerator(self.fp, "utf-8") self.xmlgen.startDocument() self.xmlgen.processingInstruction("xml-stylesheet", 'type="text/xsl" href="{}"'.format(xslfile)) attrs = AttributesNSImpl({}, {}) self.xmlgen.startElementNS((None, "report"), "report", attrs) def write(self, path, code, message): attr_vals = { (None, "code"): code, (None, "path"): path } attr_qnames = { (None, "code"): "code", (None, "path"): "path" } attrs = AttributesNSImpl(attr_vals, attr_qnames) self.xmlgen.startElementNS((None, "file"), "file", attrs) self.xmlgen.characters(message) self.xmlgen.endElementNS((None, "file"), "file") def newline(self): #self.fp.write("\n") self.xmlgen.characters("\n") def close(self): self.xmlgen.endElementNS((None, "report"), "report") self.xmlgen.endDocument() self.fp.close()
def test_1463026_1_empty(self): result = self.ioclass() gen = XMLGenerator(result, short_empty_elements=True) gen.startDocument() gen.startElementNS((None, 'a'), 'a', {(None, 'b'):'c'}) gen.endElementNS((None, 'a'), 'a') gen.endDocument() self.assertEqual(result.getvalue(), self.xml('<a b="c"/>'))
def test_1463026_1_empty(self): result = StringIO() gen = XMLGenerator(result, short_empty_elements=True) gen.startDocument() gen.startElementNS((None, "a"), "a", {(None, "b"): "c"}) gen.endElementNS((None, "a"), "a") gen.endDocument() self.assertEqual(result.getvalue(), start + '<a b="c"/>')
def test_1463026_1(self): result = StringIO() gen = XMLGenerator(result) gen.startDocument() gen.startElementNS((None, 'a'), 'a', {(None, 'b'):'c'}) gen.endElementNS((None, 'a'), 'a') gen.endDocument() self.assertEqual(result.getvalue(), start+'<a b="c"></a>')
def test_1463026_1(): result = StringIO() gen = XMLGenerator(result) gen.startDocument() gen.startElementNS((None, "a"), "a", {(None, "b"): "c"}) gen.endElementNS((None, "a"), "a") gen.endDocument() return result.getvalue() == start + '<a b="c"></a>'
def test_1463026_1(): result = StringIO() gen = XMLGenerator(result) gen.startDocument() gen.startElementNS((None, 'a'), 'a', {(None, 'b'):'c'}) gen.endElementNS((None, 'a'), 'a') gen.endDocument() return result.getvalue() == start+'<a b="c"></a>'
def test_1463026_1(self): result = StringIO() gen = XMLGenerator(result) gen.startDocument() gen.startElementNS((None, "a"), "a", {(None, "b"): "c"}) gen.endElementNS((None, "a"), "a") gen.endDocument() self.assertEquals(result.getvalue(), start + '<a b="c"></a>')
class XMLStreamWriter(object): """ class for writing an xml file iteratively """ def __init__(self, file_handle=None, header='<?xml version="1.0" ?>'): """ initializes the writer with a stream to write to. If `filehandle=None`, the output is written to sys.stdout """ if file_handle is None: file_handle = sys.stdout self.file_handle = file_handle if header: self.file_handle.write(header) self._generator = XMLGenerator(file_handle, 'utf-8') self._tags = [] def start_tag(self, name, attr=None, body=None, namespace=None): """ start tag `name` with attributes `attr` and body `body` """ attr_vals = {} attr_keys = {} if attr is not None: for key, val in attr.items(): key_tuple = (namespace, key) attr_vals[key_tuple] = str(val) attr_keys[key_tuple] = key attr_obj = AttributesNSImpl(attr_vals, attr_keys) self._generator.startElementNS((namespace, name), name, attr_obj) if body: self._generator.characters(str(body)) self._tags.append(name) def end_tag(self, name=None, namespace=None): """ and tag `name` """ close_tag = self._tags.pop() if name is not None: if name != close_tag: raise ValueError('Cannot close tag `%s`, since the last opened ' 'tag was `%s`' % (name, close_tag)) self._generator.endElementNS((namespace, name), name) def tag(self, name, attr=None, body=None, namespace=None): """ write tag `name` """ self.start_tag(name, attr, body, namespace) self.end_tag(name, namespace)
def test_1463026_3(): result = StringIO() gen = XMLGenerator(result) gen.startDocument() gen.startPrefixMapping('my', 'qux') gen.startElementNS(('qux', 'a'), 'a', {(None, 'b'):'c'}) gen.endElementNS(('qux', 'a'), 'a') gen.endPrefixMapping('my') gen.endDocument() return result.getvalue() == start+'<my:a xmlns:my="qux" b="c"></my:a>'
def test_1463026_2_empty(self): result = self.ioclass() gen = XMLGenerator(result, short_empty_elements=True) gen.startDocument() gen.startPrefixMapping(None, 'qux') gen.startElementNS(('qux', 'a'), 'a', {}) gen.endElementNS(('qux', 'a'), 'a') gen.endPrefixMapping(None) gen.endDocument() self.assertEqual(result.getvalue(), self.xml('<a xmlns="qux"/>'))
def test_1463026_3(): result = StringIO() gen = XMLGenerator(result) gen.startDocument() gen.startPrefixMapping("my", "qux") gen.startElementNS(("qux", "a"), "a", {(None, "b"): "c"}) gen.endElementNS(("qux", "a"), "a") gen.endPrefixMapping("my") gen.endDocument() return result.getvalue() == start + '<my:a xmlns:my="qux" b="c"></my:a>'
def test_1463026_2(self): result = StringIO() gen = XMLGenerator(result) gen.startDocument() gen.startPrefixMapping(None, 'qux') gen.startElementNS(('qux', 'a'), 'a', {}) gen.endElementNS(('qux', 'a'), 'a') gen.endPrefixMapping(None) gen.endDocument() self.assertEqual(result.getvalue(), start+'<a xmlns="qux"></a>')
def endElementNS(self, ns, el) : self.__level = self.__level - 1 if self.__last == "end" : self.ignorableWhitespace("\n") if self.__last == 'end' : self.ignorableWhitespace(" " * (self.__level * self.__indent)) XMLGenerator.endElementNS(self, ns, el) self.__last = 'end'
def test_1463026_2(): result = StringIO() gen = XMLGenerator(result) gen.startDocument() gen.startPrefixMapping(None, "qux") gen.startElementNS(("qux", "a"), "a", {}) gen.endElementNS(("qux", "a"), "a") gen.endPrefixMapping(None) gen.endDocument() return result.getvalue() == start + '<a xmlns="qux"></a>'
def test_1463026_3_empty(self): result = StringIO() gen = XMLGenerator(result, short_empty_elements=True) gen.startDocument() gen.startPrefixMapping("my", "qux") gen.startElementNS(("qux", "a"), "a", {(None, "b"): "c"}) gen.endElementNS(("qux", "a"), "a") gen.endPrefixMapping("my") gen.endDocument() self.assertEqual(result.getvalue(), start + '<my:a xmlns:my="qux" b="c"/>')
def test_1463026_3(self): result = self.ioclass() gen = XMLGenerator(result) gen.startDocument() gen.startPrefixMapping('my', 'qux') gen.startElementNS(('qux', 'a'), 'a', {(None, 'b'): 'c'}) gen.endElementNS(('qux', 'a'), 'a') gen.endPrefixMapping('my') gen.endDocument() self.assertEqual(result.getvalue(), start + '<my:a xmlns:my="qux" b="c"></my:a>')
def test_1463026_3(self): result = self.ioclass() gen = XMLGenerator(result) gen.startDocument() gen.startPrefixMapping('my', 'qux') gen.startElementNS(('qux', 'a'), 'a', {(None, 'b'):'c'}) gen.endElementNS(('qux', 'a'), 'a') gen.endPrefixMapping('my') gen.endDocument() self.assertEqual(result.getvalue(), self.xml('<my:a xmlns:my="qux" b="c"></my:a>'))
def test_1463026_3_empty(self): result = StringIO() gen = XMLGenerator(result, short_empty_elements=True) gen.startDocument() gen.startPrefixMapping('my', 'qux') gen.startElementNS(('qux', 'a'), 'a', {(None, 'b'): 'c'}) gen.endElementNS(('qux', 'a'), 'a') gen.endPrefixMapping('my') gen.endDocument() self.assertEqual(result.getvalue(), start + '<my:a xmlns:my="qux" b="c"/>')
def test_1463026_3_empty(self): result = StringIO() gen = XMLGenerator(result, short_empty_elements=True) gen.startDocument() gen.startPrefixMapping('my', 'qux') gen.startElementNS(('qux', 'a'), 'a', {(None, 'b'):'c'}) gen.endElementNS(('qux', 'a'), 'a') gen.endPrefixMapping('my') gen.endDocument() self.assertEqual(result.getvalue(), start+'<my:a xmlns:my="qux" b="c"/>')
class XMLWriter(object): def __init__(self, output): self.output = XMLGenerator(output, "utf-8") self.level = 0 def start_element(self, name, attrs, nochar=False): self.output.characters(' ' * self.level) self.output.startElementNS((None, name), name, self.attrs(attrs)) if not nochar: self.output.characters('\n') self.level += 1 def end_element(self, name, nochar=False): self.level -= 1 if not nochar: self.output.characters(' ' * self.level) self.output.endElementNS((None, name), name) self.output.characters('\n') def start_document(self, keys): self.start_element('graphml', { "xmlns": u"http://graphml.graphdrawing.org/xmlns", "xmlns:xsi": u"http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation": u"http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd", }) for kid, kfor, kname, ktype in keys: self.start_element('key', { "id": kname, "for": kfor, "attr.name": kname, "attr.type": ktype, }) self.end_element('key') self.start_element('graph', { 'id': 'G', 'edgedefault': 'undirected', }) def end_document(self): self.end_element('graph') self.output.endElementNS((None, u'graphml'), u'graphml') self.output.endDocument() def attrs(self, attributes): return AttributesNSImpl(dict(((None, k), v) for (k,v) in attributes.iteritems()), {})
def test_xmlgen_ns_empty(self): result = self.ioclass() gen = XMLGenerator(result, short_empty_elements=True) gen.startDocument() gen.startPrefixMapping('ns1', ns_uri) gen.startElementNS((ns_uri, 'doc'), 'ns1:doc', {}) gen.startElementNS((None, 'udoc'), None, {}) gen.endElementNS((None, 'udoc'), None) gen.endElementNS((ns_uri, 'doc'), 'ns1:doc') gen.endPrefixMapping('ns1') gen.endDocument() self.assertEqual( result.getvalue(), self.xml('<ns1:doc xmlns:ns1="%s"><udoc/></ns1:doc>' % ns_uri))
def test_xmlgen_ns(): result = StringIO() gen = XMLGenerator(result) gen.startDocument() gen.startPrefixMapping("ns1", ns_uri) gen.startElementNS((ns_uri, "doc"), "ns1:doc", {}) # add an unqualified name gen.startElementNS((None, "udoc"), None, {}) gen.endElementNS((None, "udoc"), None) gen.endElementNS((ns_uri, "doc"), "ns1:doc") gen.endPrefixMapping("ns1") gen.endDocument() return result.getvalue() == start + ('<ns1:doc xmlns:ns1="%s"><udoc></udoc></ns1:doc>' % ns_uri)
def test_xmlgen_ns_empty(self): result = StringIO() gen = XMLGenerator(result, short_empty_elements=True) gen.startDocument() gen.startPrefixMapping("ns1", ns_uri) gen.startElementNS((ns_uri, "doc"), "ns1:doc", {}) # add an unqualified name gen.startElementNS((None, "udoc"), None, {}) gen.endElementNS((None, "udoc"), None) gen.endElementNS((ns_uri, "doc"), "ns1:doc") gen.endPrefixMapping("ns1") gen.endDocument() self.assertEqual(result.getvalue(), start + ('<ns1:doc xmlns:ns1="%s"><udoc/></ns1:doc>' % ns_uri))
class _PrettyXMLWriter(object): def __init__(self, output, encoding='UTF-8', namespace=('bibtex', 'http://bibtexml.sf.net/'), header=True): self.prefix, self.uri = namespace self.generator = XMLGenerator(output, encoding=encoding) if header: self.generator.startDocument() self.generator.startPrefixMapping(self.prefix, self.uri) self.stack = [] def write(self, data): self.generator.characters(data) def newline(self): self.write('\n') def indent_line(self): self.write(' ' * (len(self.stack) * 4)) def start(self, tag, attrs=None, newline=True): if attrs is None: attrs = {} else: attrs = {(None, key): value for key, value in attrs.items()} self.indent_line() self.stack.append(tag) self.generator.startElementNS((self.uri, tag), tag, AttributesImpl(attrs)) if newline: self.newline() def end(self, indent=True): tag = self.stack.pop() if indent: self.indent_line() self.generator.endElementNS((self.uri, tag), tag) self.newline() def element(self, tag, data): self.start(tag, newline=False) self.write(data) self.end(indent=False) def close(self): self.generator.endDocument()
def test_xmlgen_ns(self): result = self.ioclass() gen = XMLGenerator(result) gen.startDocument() gen.startPrefixMapping("ns1", ns_uri) gen.startElementNS((ns_uri, "doc"), "ns1:doc", {}) # add an unqualified name gen.startElementNS((None, "udoc"), None, {}) gen.endElementNS((None, "udoc"), None) gen.endElementNS((ns_uri, "doc"), "ns1:doc") gen.endPrefixMapping("ns1") gen.endDocument() self.assertEqual(result.getvalue(), self.xml( '<ns1:doc xmlns:ns1="%s"><udoc></udoc></ns1:doc>' % ns_uri))
def test_xmlgen_ns_empty(self): result = self.ioclass() gen = XMLGenerator(result, short_empty_elements=True) gen.startDocument() gen.startPrefixMapping("ns1", ns_uri) gen.startElementNS((ns_uri, "doc"), "ns1:doc", {}) # add an unqualified name gen.startElementNS((None, "udoc"), None, {}) gen.endElementNS((None, "udoc"), None) gen.endElementNS((ns_uri, "doc"), "ns1:doc") gen.endPrefixMapping("ns1") gen.endDocument() self.assertEqual( result.getvalue(), self.xml('<ns1:doc xmlns:ns1="%s"><udoc/></ns1:doc>' % ns_uri))
def test_xmlgen_ns(): result = StringIO() gen = XMLGenerator(result) gen.startDocument() gen.startPrefixMapping("ns1", ns_uri) gen.startElementNS((ns_uri, "doc"), "ns1:doc", {}) # add an unqualified name gen.startElementNS((None, "udoc"), None, {}) gen.endElementNS((None, "udoc"), None) gen.endElementNS((ns_uri, "doc"), "ns1:doc") gen.endPrefixMapping("ns1") gen.endDocument() return result.getvalue() == start + \ ('<ns1:doc xmlns:ns1="%s"><udoc></udoc></ns1:doc>' % ns_uri)
def test_5027_2(self): result = self.ioclass() gen = XMLGenerator(result) gen.startDocument() gen.startPrefixMapping('a', 'http://example.com/ns') gen.startElementNS(('http://example.com/ns', 'g1'), 'g1', {}) lang_attr = {('http://www.w3.org/XML/1998/namespace', 'lang'): 'en'} gen.startElementNS(('http://example.com/ns', 'g2'), 'g2', lang_attr) gen.characters('Hello') gen.endElementNS(('http://example.com/ns', 'g2'), 'g2') gen.endElementNS(('http://example.com/ns', 'g1'), 'g1') gen.endPrefixMapping('a') gen.endDocument() self.assertEqual( result.getvalue(), self.xml( '<a:g1 xmlns:a="http://example.com/ns"><a:g2 xml:lang="en">Hello</a:g2></a:g1>' ))
def codebook_xml(tags, file): """Export a codebook in REFI-QDA format for the given tags. """ with contextlib.ExitStack() as stack: if not hasattr(file, 'write'): file = stack.enter_context(open(file, 'wb')) # http://schema.qdasoftware.org/versions/Codebook/v1.0/Codebook.xsd output = XMLGenerator( file, encoding='utf-8', short_empty_elements=True, ) output.startDocument() output.startPrefixMapping(None, 'urn:QDA-XML:codebook:1.0') output.startElementNS( (None, 'CodeBook'), 'CodeBook', AttributesNSImpl({(None, 'origin'): 'Taguette %s' % version}, {(None, 'origin'): 'origin'}), ) output.startElementNS( (None, 'Codes'), 'Codes', AttributesNSImpl({}, {}), ) for tag in tags: guid = uuid.uuid5(TAGUETTE_NAMESPACE, tag.path) guid = str(guid).upper() output.startElementNS( (None, 'Code'), 'Code', AttributesNSImpl( { (None, 'guid'): guid, (None, 'name'): tag.path, (None, 'isCodable'): 'true' }, { (None, 'guid'): 'guid', (None, 'name'): 'name', (None, 'isCodable'): 'isCodable' }), ) output.endElementNS((None, 'Code'), 'Code') output.endElementNS((None, 'Codes'), 'Codes') output.startElementNS( (None, 'Sets'), 'Sets', AttributesNSImpl({}, {}), ) output.endElementNS((None, 'Sets'), 'Sets') output.endElementNS((None, 'CodeBook'), 'CodeBook') output.endPrefixMapping(None) output.endDocument()
class XMLWriter: def __init__(self, fileName): self.out = open(fileName, 'w') self.g = XMLGenerator(self.out, 'utf-8') return def writeHeader(self, header): #self.out.write("""<?xml version="1.0" encoding="UTF-8"?>""") #self.out.write("""<gexf version="1.2" xmlns="http://www.gexf.net/1.2draft" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd">""") self.out.write(header) return def startTag(self, name, attr={}, body=None, namespace=None): attr_vals = {} attr_keys = {} for key, val in attr.iteritems(): key_tuple = (namespace, key) attr_vals[key_tuple] = val attr_keys[key_tuple] = key attr2 = AttributesNSImpl(attr_vals, attr_keys) self.g.startElementNS((namespace, name), name, attr2) if body: self.g.characters(body) return def endTag(self, name, namespace=None): self.g.endElementNS((namespace, name), name) return def tag(self, name, attr={}, body=None, namespace=None): self.startTag(name, attr, body, namespace) self.endTag(name, namespace) return def endDocument(self): self.g.endDocument() self.out.close() return
def run(self): xml = XMLGenerator(self.out, 'UTF-8', True) xml.startDocument() xml.startPrefixMapping('', self.SVG_NAMESPACE) xml.startPrefixMapping(self.XLINK_PREFIX, self.XLINK_NAMESPACE) canvasWidth = int(self.marginWidth + (self.count - 1) * self.offset) attrs = self._defaultNSAttrs({ self._svgName('version'): self.SVG_VERSION, self._svgName('width'): str(canvasWidth), self._svgName('height'): str(self.canvasHeight), self._svgName('viewBox'): ('%d %d %d %g' % (0, 0, canvasWidth, self.canvasHeight)) }) xml.startElementNS(self.SVG_ELEMENT, None, attrs) self._defs(xml) self._contentGroup(xml) xml.ignorableWhitespace('\n') xml.endElementNS(self.SVG_ELEMENT, None) xml.endPrefixMapping('') xml.endPrefixMapping(self.XLINK_PREFIX) xml.endDocument()
class SocialGraphExport(object): def __init__(self, userinfo, dataset, output): self.gzfd = gzip.open(output, "w") self.output = XMLGenerator(self.gzfd, "utf-8") self.level = 0 self.start_document() self.userinfo = self.load_userinfo(userinfo) for user_id in self.userinfo: self.start_element('node', {'id': str(user_id)}, False) self.start_element('data', {'key': 'screen_name'}, True) self.output.characters(self.get_screen_name(user_id)) self.end_element('data', True) self.end_element('node', False) self.dataset = os.path.abspath(dataset) self.extract_graph() self.end_document() def load_userinfo(self, inputfile): info = {} with gzip.open(inputfile, 'r') as input: for line in input: user = json.loads(line.strip()) info[int(user['id_str'])] = user return info def get_screen_name(self, user_id): return self.userinfo[user_id]['screen_name'] def attrs(self, attributes): return AttributesNSImpl(dict(((None, k), v) for (k,v) in attributes.iteritems()), {}) def start_element(self, name, attrs, nochar=False): self.output.characters(' ' * self.level) self.output.startElementNS((None, name), name, self.attrs(attrs)) if not nochar: self.output.characters('\n') self.level += 1 def end_element(self, name, nochar=False): self.level -= 1 if not nochar: self.output.characters(' ' * self.level) self.output.endElementNS((None, name), name) self.output.characters('\n') def start_document(self): self.start_element('graphml', { "xmlns": u"http://graphml.graphdrawing.org/xmlns", "xmlns:xsi": u"http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation": u"http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd", }) self.start_element('key', { "id": "screen_name", "for": "node", "attr.name": "screen_name", "attr.type": "string", }) self.end_element('key') self.start_element('graph', { 'id': 'G', 'edgedefault': 'directed', }) def end_document(self): self.end_element('graph') self.output.endElementNS((None, u'graphml'), u'graphml') self.output.endDocument() self.gzfd.close() def extract_graph(self): lastid = 1 lastuser = 0 users = set(self.userinfo.keys()) for user_id in users: opener = open filename = os.path.join(self.dataset, str(user_id)[:2], str(user_id) + '.fws') if not os.path.exists(filename): filename += '.gz' opener = gzip.open if not os.path.exists(filename): continue followers = set() datasize = struct.calcsize('!Q') with opener(filename, 'r') as ffile: while True: data = ffile.read(datasize) if not data: break following = struct.unpack('!Q', data)[0] followers.add(following) for following in followers.intersection(users): self.start_element('edge', { "id": str(lastid), "source": str(user_id), "target": str(following), "label": "follows", }, True) self.end_element('edge', True) lastid += 1 lastuser += 1 sys.stderr.write('\rUser: %d of %d [%02d%%]' % (lastuser, len(users), lastuser * 100.0 / len(users))) sys.stderr.flush() sys.stderr.write('\nCompleted') sys.stderr.flush()
class CombineSVG(object): """ CombineSVG combines multiple SVGa into a single SVG `output` stream. Different `layer_name`s are added into a separate SVG layers (`<g>`). Empty layers are stripped. Note: Only the simplest SVGs with <path> elements are supported (e.g. MapServer Cairo SVG output format). """ def __init__(self, output, root_id=None): self.first = True self.root_id = root_id self.out = XMLGenerator(output, "utf-8") self.out.startPrefixMapping(None, SVG_NS) self.out.startDocument() self.groups = [] self.actual_groups = [] def push_group(self, name): """ push_group adds a new SVG layer name to be used for the next SVG sub-document with add. """ self.groups.append(name) def pop_group(self): """ pop_group removes the last added SVG layer. """ self.groups = self.groups[:-1] def balance_group(self): """ balance_group opens/closes <g> tags as necessary. We can't create the <g> tags in push/pop_group as we do not want them for empty layers. """ for want, got in zip_longest(self.groups, self.actual_groups): if want == got: continue if got is None: self.out.startElement("g", {"id": want}) self.out.characters("\n") self.actual_groups.append(want) self.balance_group() return if want is None or want != got: self.out.endElement("g") self.out.characters("\n") self.actual_groups.pop() self.balance_group() return def __enter__(self): return self def __exit__(self, *exc): self.close() def add(self, r): """ Add an SVG layer with `layer_name` from file object `r`. """ for evt, elem in iterparse(r, events=("start", "end")): if evt == "start" and self.first and elem.tag == SVG_TAG: # copy <svg> from first doc, otherwise ignore svg_attrib = {(None, k): v for k, v in elem.attrib.items()} if self.root_id: svg_attrib[(None, "id")] = self.root_id self.out.startElementNS((SVG_NS, "svg"), "svg", svg_attrib) self.out.characters("\n") continue if evt == "end" and self.first and elem.tag == RECT_TAG: # copy only first <rect>, used by Mapserver to draw blank canvas, but we # need all additional to be transparent. self.balance_group() self.out.startElement("rect", elem.attrib) self.out.endElement("rect") self.out.characters("\n") elif evt == "end" and elem.tag == PATH_TAG: # all drawings are included in <path> tags self.balance_group() self.out.startElement("path", elem.attrib) self.out.endElement("path") self.out.characters("\n") self.first = False def close(self): """ Close all open SVG XML elements. """ self.groups = [] self.balance_group() self.out.endElementNS((SVG_NS, "svg"), "svg") self.out.endDocument()
class ModelSaver: def __init__(self, filepath, engine, state = 0, datastate = 0): self.SModel, self.SMap, self.SLayer, self.SInstances, self.SObject, self.SAction = range(6) self.engine = engine self.model = self.engine.getModel() self.pool = self.engine.getImagePool() self.anim_pool = self.engine.getAnimationPool() if (state): self.state = state if (state == self.SMap): self.map = datastate else: assert 0, "Invalid initialization state." else: self.state = self.SModel self.stack = [ self.SModel ] self.datastack = [ ] self.file = open(filepath, 'w') self.xmlout = XMLGenerator(self.file, 'ascii') self.xmlout.startDocument() self.indent_level = '' self.nspace = None def startElement(self, name, attrs): self.file.write(self.indent_level) self.xmlout.startElementNS((None, name), name, attrs) self.file.write('\n') self.indent_level = self.indent_level + '\t' def endElement(self, name): self.indent_level = self.indent_level[0:(len(self.indent_level) - 1)] self.file.write(self.indent_level) self.xmlout.endElementNS((None, name), name) self.file.write('\n') def write_map(self, map, importList): assert self.state == self.SModel, "Declaration of <map> not at the top level." attr_vals = { (None, 'id'): map.getId(), (None, 'format'): MAPFORMAT, } attr_names = { (None, 'id'): 'id', (None, 'format'): 'format', } attrs = AttributesNSImpl(attr_vals, attr_names) self.startElement('map', attrs) self.state = self.SMap self.write_imports(map, importList) self.write_layers(map) self.write_camera(map) self.endElement('map') def write_imports(self, map, importList): for importdir in importList: self.write_importdir(root_subfile(map.getResourceFile(), importdir)) imports = [] for layer in map.getLayers(): for instance in layer.getInstances(): file = instance.getObject().getResourceFile() if not (file in imports): if not self.have_superdir(file, importList): imports.append(file) self.write_import(root_subfile(map.getResourceFile(), file)) def have_superdir(self, file, importList): '''returns true, if file is in directories given in importList''' for dir in importList: have = True for test in zip(dir.split(os.path.sep), file.split(os.path.sep)): if test[0] != test[1]: have = False if have: return True return False def write_import(self, file): attr_vals = { (None, 'file'): file, } attr_names = { (None, 'file'): 'file', } attrs = AttributesNSImpl(attr_vals, attr_names) self.file.write(self.indent_level) self.xmlout.startElementNS((None, 'import'), 'import', attrs) self.xmlout.endElementNS((None, 'import'), 'import') self.file.write('\n') def write_importdir(self, dir): attr_vals = { (None, 'dir'): dir, } attr_names = { (None, 'dir'): 'dir', } attrs = AttributesNSImpl(attr_vals, attr_names) self.file.write(self.indent_level) self.xmlout.startElementNS((None, 'import'), 'import', attrs) self.xmlout.endElementNS((None, 'import'), 'import') self.file.write('\n') def pathing_val_to_str(self, val): if val == fife.CELL_EDGES_AND_DIAGONALS: return "cell_edges_and_diagonals" if val == fife.FREEFORM: return "freeform" return "cell_edges_only" def write_layers(self, map): for layer in map.getLayers(): cellgrid = layer.getCellGrid() attr_vals = { (None, 'id'): layer.getId(), (None, 'grid_type'): cellgrid.getType(), (None, 'x_scale'): str(cellgrid.getXScale()), (None, 'y_scale'): str(cellgrid.getYScale()), (None, 'rotation'): str(cellgrid.getRotation()), (None, 'x_offset'): str(cellgrid.getXShift()), (None, 'y_offset'): str(cellgrid.getYShift()), (None, 'pathing'): self.pathing_val_to_str(layer.getPathingStrategy()), (None, 'transparency'): str(layer.getLayerTransparency()), } attr_names = { (None, 'id'): 'id', (None, 'grid_type'): 'grid_type', (None, 'scaling'): 'scaling', (None, 'rotation'): 'rotation', (None, 'x_offset'): 'x_offset', (None, 'y_offset'): 'y_offset', (None, 'pathing'): 'pathing', } attrs = AttributesNSImpl(attr_vals, attr_names) self.startElement('layer', attrs) self.write_instances(layer) self.endElement('layer') def write_instances(self, layer): attrs = AttributesNSImpl({}, {}) self.startElement('instances', attrs) for inst in layer.getInstances(): position = inst.getLocationRef().getExactLayerCoordinates() attr_vals = { (None, 'o'): inst.getObject().getId(), (None, 'x'): str(position.x), (None, 'y'): str(position.y), (None, 'z'): str(position.z), (None, 'r'): str(inst.getRotation()), } attr_names = { (None, 'o'): 'o', (None, 'x'): 'x', (None, 'y'): 'y', (None, 'z'): 'z', (None, 'r'): 'r', } nspace = inst.getObject().getNamespace() if nspace != self.nspace: attr_vals[(None, 'ns')] = inst.getObject().getNamespace() attr_names[(None, 'ns')] = 'ns' self.nspace = nspace instId = inst.getId() if instId: attr_vals[(None, 'id')] = inst.getId() attr_names[(None, 'id')] = 'id' attrs = AttributesNSImpl(attr_vals, attr_names) self.file.write(self.indent_level) self.xmlout.startElementNS((None, 'i'), 'i', attrs) self.xmlout.endElementNS((None, 'i'), 'i') self.file.write('\n') self.endElement('instances') # Save the linked camera of a map. def write_camera( self, map ): cameralist = self.engine.getView().getCameras() for cam in cameralist: if cam.getLocationRef().getMap().getId() == map.getId(): celldimensions = cam.getCellImageDimensions() viewport = cam.getViewPort(); attr_names = { (None, 'id'): 'id', (None, 'zoom'): 'zoom', (None, 'tilt'): 'tile', (None, 'rotation'): 'rotation', (None, 'ref_layer_id'): 'ref_layer_id', (None, 'ref_cell_width'): 'ref_cell_width', (None, 'ref_cell_height'): 'ref_cell_height', } attr_vals = { (None, 'id'): cam.getId(), (None, 'zoom'): str( cam.getZoom()), (None, 'tilt'): str( cam.getTilt()), (None, 'rotation'): str( cam.getRotation()), (None, 'ref_layer_id'): cam.getLocation().getLayer().getId(), (None, 'ref_cell_width'): str( celldimensions.x ), (None, 'ref_cell_height'): str( celldimensions.y ), } # add a viewport entry if the cam isn't full sized if not (viewport == self.engine.getRenderBackend().getArea()): attr_names[(None,'viewport')] = 'viewport' attr_vals[(None,'viewport')] = '%d,%d,%d,%d' % (viewport.x, viewport.y, viewport.w, viewport.h) attrs = AttributesNSImpl( attr_vals, attr_names ) self.startElement( 'camera', attrs ); self.endElement( 'camera' ); def flush(self): self.xmlout.endDocument() self.file.close()
def endElementNS(self, name, qname): self._out.pop() XMLGenerator.endElementNS(self, name, qname)
class XMLMapSaver(object): def __init__(self, filepath, engine, map, importList, state=0, datastate=0): self.SModel, self.SMap, self.SLayer, self.SInstances, self.SObject, self.SAction = list( range(6)) self.engine = engine self.model = self.engine.getModel() self.image_manager = self.engine.getImageManager() if (state): self.state = state if (state == self.SMap): self.map = datastate else: assert 0, "Invalid initialization state." else: self.state = self.SModel self.stack = [self.SModel] self.datastack = [] self.file = open(filepath, 'w') self.xmlout = XMLGenerator(self.file, 'ascii') self.xmlout.startDocument() self.indent_level = '' self.nspace = None self.map = map self.importList = importList def startElement(self, name, attrs): self.file.write(self.indent_level) self.xmlout.startElementNS((None, name), name, attrs) self.file.write('\n') self.indent_level = self.indent_level + '\t' def endElement(self, name): self.indent_level = self.indent_level[0:(len(self.indent_level) - 1)] self.file.write(self.indent_level) self.xmlout.endElementNS((None, name), name) self.file.write('\n') def write_map(self): assert self.state == self.SModel, "Declaration of <map> not at the top level." attr_vals = { (None, 'id'): self.map.getId(), (None, 'format'): MAPFORMAT, } attr_names = { (None, 'id'): 'id', (None, 'format'): 'format', } attrs = AttributesNSImpl(attr_vals, attr_names) self.startElement('map', attrs) self.state = self.SMap self.write_imports(self.map, self.importList) self.write_layers(self.map) self.write_camera(self.map) self.endElement('map') def write_imports(self, map, importList): for importdir in importList: self.write_importdir(root_subfile(map.getFilename(), importdir)) imports = [] for layer in map.getLayers(): for instance in layer.getInstances(): file = instance.getObject().getFilename() if not (file in imports): if not self.have_superdir(file, importList): imports.append(file) self.write_import(root_subfile(map.getFilename(), file)) def have_superdir(self, file, importList): '''returns true, if file is in directories given in importList''' for dir in importList: have = True for test in zip(dir.split(os.path.sep), file.split(os.path.sep)): if test[0] != test[1]: have = False if have: return True return False def write_import(self, file): attr_vals = { (None, 'file'): file, } attr_names = { (None, 'file'): 'file', } attrs = AttributesNSImpl(attr_vals, attr_names) self.file.write(self.indent_level) self.xmlout.startElementNS((None, 'import'), 'import', attrs) self.xmlout.endElementNS((None, 'import'), 'import') self.file.write('\n') def write_importdir(self, dir): attr_vals = { (None, 'dir'): dir, } attr_names = { (None, 'dir'): 'dir', } attrs = AttributesNSImpl(attr_vals, attr_names) self.file.write(self.indent_level) self.xmlout.startElementNS((None, 'import'), 'import', attrs) self.xmlout.endElementNS((None, 'import'), 'import') self.file.write('\n') def pathing_val_to_str(self, val): if val == fife.CELL_EDGES_AND_DIAGONALS: return "cell_edges_and_diagonals" if val == fife.FREEFORM: return "freeform" return "cell_edges_only" def layer_type_to_str(self, layer): if layer.isWalkable(): return "walkable" elif layer.isInteract(): return "interact" return "" def write_layers(self, map): for layer in map.getLayers(): cellgrid = layer.getCellGrid() attr_vals = { (None, 'id'): layer.getId(), (None, 'grid_type'): cellgrid.getType(), (None, 'x_scale'): str(cellgrid.getXScale()), (None, 'y_scale'): str(cellgrid.getYScale()), (None, 'rotation'): str(cellgrid.getRotation()), (None, 'x_offset'): str(cellgrid.getXShift()), (None, 'y_offset'): str(cellgrid.getYShift()), (None, 'z_offset'): str(cellgrid.getZShift()), (None, 'pathing'): self.pathing_val_to_str(layer.getPathingStrategy()), (None, 'transparency'): str(layer.getLayerTransparency()), (None, 'layer_type'): str(self.layer_type_to_str(layer)), (None, 'layer_type_id'): str(layer.getWalkableId()), } attr_names = { (None, 'id'): 'id', (None, 'grid_type'): 'grid_type', (None, 'scaling'): 'scaling', (None, 'rotation'): 'rotation', (None, 'x_offset'): 'x_offset', (None, 'y_offset'): 'y_offset', (None, 'z_offset'): 'z_offset', (None, 'pathing'): 'pathing', (None, 'layer_type'): 'layer_type', (None, 'layer_type_id'): 'layer_type_id', } attrs = AttributesNSImpl(attr_vals, attr_names) self.startElement('layer', attrs) self.write_instances(layer) self.write_lights(layer) self.endElement('layer') def write_instances(self, layer): attrs = AttributesNSImpl({}, {}) self.startElement('instances', attrs) for inst in layer.getInstances(): position = inst.getLocationRef().getExactLayerCoordinates() attr_vals = { (None, 'o'): inst.getObject().getId(), (None, 'x'): str(position.x), (None, 'y'): str(position.y), (None, 'z'): str(position.z), (None, 'r'): str(inst.getRotation()), } attr_names = { (None, 'o'): 'o', (None, 'x'): 'x', (None, 'y'): 'y', (None, 'z'): 'z', (None, 'r'): 'r', } visual = inst.get2dGfxVisual() if visual: attr_vals[(None, 'stackpos')] = str(visual.getStackPosition()) attr_names[(None, 'stackpos')] = 'stackpos' nspace = inst.getObject().getNamespace() if nspace != self.nspace: attr_vals[(None, 'ns')] = inst.getObject().getNamespace() attr_names[(None, 'ns')] = 'ns' self.nspace = nspace instId = inst.getId() if instId: attr_vals[(None, 'id')] = inst.getId() attr_names[(None, 'id')] = 'id' if inst.isOverrideBlocking(): attr_vals[(None, 'override_blocking')] = str( int(inst.isOverrideBlocking())) attr_names[(None, 'override_blocking')] = 'override_blocking' if inst.getObject().isBlocking() is not inst.isBlocking(): attr_vals[(None, 'blocking')] = str(int(inst.isBlocking())) attr_names[(None, 'blocking')] = 'blocking' attrs = AttributesNSImpl(attr_vals, attr_names) self.file.write(self.indent_level) self.xmlout.startElementNS((None, 'i'), 'i', attrs) self.xmlout.endElementNS((None, 'i'), 'i') self.file.write('\n') self.endElement('instances') def write_lights(self, layer): attrs = AttributesNSImpl({}, {}) self.startElement('lights', attrs) cameras = layer.getMap().getCameras() for cam in cameras: hit = False layers = cam.getRenderer("LightRenderer").getActiveLayers() for lay in layers: if lay.getId() == layer.getId(): hit = True if hit == False: continue renderer = fife.LightRenderer.getInstance(cam) groups = renderer.getGroups() for group in groups: infos = renderer.getLightInfo(group) for info in infos: attr_vals = {} attr_names = {} type = info.getName() attr_vals[(None, 'group')] = str(group) attr_names[(None, 'group')] = 'group' attr_vals[(None, 'type')] = str(type) attr_names[(None, 'type')] = 'type' attr_vals[(None, 'instance')] = str( info.getNode().getInstance().getId()) attr_names[(None, 'instance')] = 'instance' if info.getSrcBlend() > -1: attr_vals[(None, 'src')] = str(info.getSrcBlend()) attr_names[(None, 'src')] = 'src' if info.getDstBlend() > -1: attr_vals[(None, 'dst')] = str(info.getDstBlend()) attr_names[(None, 'dst')] = 'dst' if info.getStencil() > -1: attr_vals[(None, 's_ref')] = str(info.getStencil()) attr_names[(None, 's_ref')] = 's_ref' attr_vals[(None, 'a_ref')] = str(info.getAlpha()) attr_names[(None, 'a_ref')] = 'a_ref' if type == 'simple': if info.getRadius() > 0: attr_vals[(None, 'radius')] = str(info.getRadius()) attr_names[(None, 'radius')] = 'radius' if info.getColor(): color = info.getColor() attr_vals[(None, 'color')] = '%d,%d,%d' % ( color[0], color[1], color[2]) attr_vals[(None, 'intensity')] = str(color[3]) attr_names[(None, 'color')] = 'color' attr_names[(None, 'intensity')] = 'intensity' if info.getSubdivisions() is not 32: attr_vals[(None, 'subdivisions')] = str( info.getSubdivisions()) attr_names[(None, 'subdivisions')] = 'subdivisions' if info.getXStretch() > 1.001 or info.getXStretch( ) < 0.999: attr_vals[(None, 'xstretch')] = str(info.getXStretch()) attr_names[(None, 'xstretch')] = 'xstretch' if info.getYStretch() > 1.001 or info.getYStretch( ) < 0.999: attr_vals[(None, 'ystretch')] = str(info.getYStretch()) attr_names[(None, 'ystretch')] = 'ystretch' elif type == 'image': if info.getImage() == 0: continue img = info.getImage() name = img.getName() attr_vals[(None, 'image')] = str('../' + name) attr_names[(None, 'image')] = 'image' elif type == 'animation': if info.getAnimation() == 0: continue ani = info.getAnimation() count = 0 newstr = '' image = ani.getFrame(ani.getActionFrame()) fname = image.getName() strings = ([str(s) for s in fname.split('/')]) leng = len(strings) - 1 while count < leng: newstr = str(newstr + strings[count] + '/') count += 1 name = str('../' + newstr + 'animation.' + fileExtensions[0]) attr_vals[(None, 'animation')] = str(name) attr_names[(None, 'animation')] = 'animation' else: continue attrs = AttributesNSImpl(attr_vals, attr_names) self.file.write(self.indent_level) self.xmlout.startElementNS((None, 'l'), 'l', attrs) self.xmlout.endElementNS((None, 'l'), 'l') self.file.write('\n') self.endElement('lights') # Save the linked camera of a map. def write_camera(self, map): cameralist = map.getCameras() for cam in cameralist: if cam.getMap().getId() == map.getId(): celldimensions = cam.getCellImageDimensions() viewport = cam.getViewPort() attr_names = { (None, 'id'): 'id', (None, 'zoom'): 'zoom', (None, 'tilt'): 'tile', (None, 'rotation'): 'rotation', (None, 'ref_cell_width'): 'ref_cell_width', (None, 'ref_cell_height'): 'ref_cell_height', } attr_vals = { (None, 'id'): cam.getId(), (None, 'zoom'): str(cam.getZoom()), (None, 'tilt'): str(cam.getTilt()), (None, 'rotation'): str(cam.getRotation()), (None, 'ref_cell_width'): str(celldimensions.x), (None, 'ref_cell_height'): str(celldimensions.y), } # add a viewport entry if the cam isn't full sized if not (viewport == self.engine.getRenderBackend().getArea()): attr_names[(None, 'viewport')] = 'viewport' attr_vals[(None, 'viewport')] = '%d,%d,%d,%d' % ( viewport.x, viewport.y, viewport.w, viewport.h) colors = cam.getLightingColor() if colors[0] < 1.0 or colors[1] < 1.0 or colors[2] < 1.0: attr_names[(None, 'light_color')] = 'light_color' attr_vals[(None, 'light_color')] = '%f,%f,%f' % ( colors[0], colors[1], colors[2]) attrs = AttributesNSImpl(attr_vals, attr_names) self.startElement('camera', attrs) self.endElement('camera') def flush(self): self.xmlout.endDocument() self.file.close() def saveResource(self): self.write_map()
class XMLMapSaver: def __init__(self, filepath, engine, map, importList, state = 0, datastate = 0): self.SModel, self.SMap, self.SLayer, self.SInstances, self.SObject, self.SAction = range(6) self.engine = engine self.model = self.engine.getModel() self.image_manager = self.engine.getImageManager() if (state): self.state = state if (state == self.SMap): self.map = datastate else: assert 0, "Invalid initialization state." else: self.state = self.SModel self.stack = [ self.SModel ] self.datastack = [ ] self.file = open(filepath, 'w') self.xmlout = XMLGenerator(self.file, 'ascii') self.xmlout.startDocument() self.indent_level = '' self.nspace = None self.map = map self.importList = importList def startElement(self, name, attrs): self.file.write(self.indent_level) self.xmlout.startElementNS((None, name), name, attrs) self.file.write('\n') self.indent_level = self.indent_level + '\t' def endElement(self, name): self.indent_level = self.indent_level[0:(len(self.indent_level) - 1)] self.file.write(self.indent_level) self.xmlout.endElementNS((None, name), name) self.file.write('\n') def write_map(self): assert self.state == self.SModel, "Declaration of <map> not at the top level." attr_vals = { (None, 'id'): self.map.getId(), (None, 'format'): MAPFORMAT, } attr_names = { (None, 'id'): 'id', (None, 'format'): 'format', } attrs = AttributesNSImpl(attr_vals, attr_names) self.startElement('map', attrs) self.state = self.SMap self.write_imports(self.map, self.importList) self.write_layers(self.map) self.write_camera(self.map) self.endElement('map') def write_imports(self, map, importList): for importdir in importList: self.write_importdir(root_subfile(map.getFilename(), importdir)) imports = [] for layer in map.getLayers(): for instance in layer.getInstances(): file = instance.getObject().getFilename() if not (file in imports): if not self.have_superdir(file, importList): imports.append(file) self.write_import(root_subfile(map.getFilename(), file)) def have_superdir(self, file, importList): '''returns true, if file is in directories given in importList''' for dir in importList: have = True for test in zip(dir.split(os.path.sep), file.split(os.path.sep)): if test[0] != test[1]: have = False if have: return True return False def write_import(self, file): attr_vals = { (None, 'file'): file, } attr_names = { (None, 'file'): 'file', } attrs = AttributesNSImpl(attr_vals, attr_names) self.file.write(self.indent_level) self.xmlout.startElementNS((None, 'import'), 'import', attrs) self.xmlout.endElementNS((None, 'import'), 'import') self.file.write('\n') def write_importdir(self, dir): attr_vals = { (None, 'dir'): dir, } attr_names = { (None, 'dir'): 'dir', } attrs = AttributesNSImpl(attr_vals, attr_names) self.file.write(self.indent_level) self.xmlout.startElementNS((None, 'import'), 'import', attrs) self.xmlout.endElementNS((None, 'import'), 'import') self.file.write('\n') def pathing_val_to_str(self, val): if val == fife.CELL_EDGES_AND_DIAGONALS: return "cell_edges_and_diagonals" if val == fife.FREEFORM: return "freeform" return "cell_edges_only" def layer_type_to_str(self, layer): if layer.isWalkable(): return "walkable" elif layer.isInteract(): return "interact" return "" def write_layers(self, map): for layer in map.getLayers(): cellgrid = layer.getCellGrid() attr_vals = { (None, 'id'): layer.getId(), (None, 'grid_type'): cellgrid.getType(), (None, 'x_scale'): str(cellgrid.getXScale()), (None, 'y_scale'): str(cellgrid.getYScale()), (None, 'rotation'): str(cellgrid.getRotation()), (None, 'x_offset'): str(cellgrid.getXShift()), (None, 'y_offset'): str(cellgrid.getYShift()), (None, 'z_offset'): str(cellgrid.getZShift()), (None, 'pathing'): self.pathing_val_to_str(layer.getPathingStrategy()), (None, 'transparency'): str(layer.getLayerTransparency()), (None, 'layer_type'): str(self.layer_type_to_str(layer)), (None, 'layer_type_id'): str(layer.getWalkableId()), } attr_names = { (None, 'id'): 'id', (None, 'grid_type'): 'grid_type', (None, 'scaling'): 'scaling', (None, 'rotation'): 'rotation', (None, 'x_offset'): 'x_offset', (None, 'y_offset'): 'y_offset', (None, 'z_offset'): 'z_offset', (None, 'pathing'): 'pathing', (None, 'layer_type'): 'layer_type', (None, 'layer_type_id'): 'layer_type_id', } attrs = AttributesNSImpl(attr_vals, attr_names) self.startElement('layer', attrs) self.write_instances(layer) self.write_lights(layer) self.endElement('layer') def write_instances(self, layer): attrs = AttributesNSImpl({}, {}) self.startElement('instances', attrs) for inst in layer.getInstances(): position = inst.getLocationRef().getExactLayerCoordinates() attr_vals = { (None, 'o'): inst.getObject().getId(), (None, 'x'): str(position.x), (None, 'y'): str(position.y), (None, 'z'): str(position.z), (None, 'r'): str(inst.getRotation()), } attr_names = { (None, 'o'): 'o', (None, 'x'): 'x', (None, 'y'): 'y', (None, 'z'): 'z', (None, 'r'): 'r', } visual = inst.get2dGfxVisual(); if visual: attr_vals[(None, 'stackpos')] = str(visual.getStackPosition()) attr_names[(None, 'stackpos')] = 'stackpos' nspace = inst.getObject().getNamespace() if nspace != self.nspace: attr_vals[(None, 'ns')] = inst.getObject().getNamespace() attr_names[(None, 'ns')] = 'ns' self.nspace = nspace instId = inst.getId() if instId: attr_vals[(None, 'id')] = inst.getId() attr_names[(None, 'id')] = 'id' if inst.isOverrideBlocking(): attr_vals[(None, 'override_blocking')] = str(int(inst.isOverrideBlocking())) attr_names[(None, 'override_blocking')] = 'override_blocking' if inst.getObject().isBlocking() is not inst.isBlocking(): attr_vals[(None, 'blocking')] = str(int(inst.isBlocking())) attr_names[(None, 'blocking')] = 'blocking' attrs = AttributesNSImpl(attr_vals, attr_names) self.file.write(self.indent_level) self.xmlout.startElementNS((None, 'i'), 'i', attrs) self.xmlout.endElementNS((None, 'i'), 'i') self.file.write('\n') self.endElement('instances') def write_lights(self, layer): attrs = AttributesNSImpl({}, {}) self.startElement('lights', attrs) cameras = layer.getMap().getCameras() for cam in cameras: hit = False layers = cam.getRenderer("LightRenderer").getActiveLayers(); for lay in layers: if lay.getId() == layer.getId(): hit = True if hit == False: continue renderer = fife.LightRenderer.getInstance(cam) groups = renderer.getGroups() for group in groups: infos = renderer.getLightInfo(group) for info in infos: attr_vals = {} attr_names = {} type = info.getName() attr_vals[(None, 'group')] = str(group) attr_names[(None, 'group')] = 'group' attr_vals[(None, 'type')] = str(type) attr_names[(None, 'type')] = 'type' attr_vals[(None, 'instance')] = str(info.getNode().getInstance().getId()) attr_names[(None, 'instance')] = 'instance' if info.getSrcBlend() > -1: attr_vals[(None, 'src')] = str(info.getSrcBlend()) attr_names[(None, 'src')] = 'src' if info.getDstBlend() > -1: attr_vals[(None, 'dst')] = str(info.getDstBlend()) attr_names[(None, 'dst')] = 'dst' if info.getStencil() > -1: attr_vals[(None, 's_ref')] = str(info.getStencil()) attr_names[(None, 's_ref')] = 's_ref' attr_vals[(None, 'a_ref')] = str(info.getAlpha()) attr_names[(None, 'a_ref')] = 'a_ref' if type == 'simple': if info.getRadius() > 0: attr_vals[(None, 'radius')] = str(info.getRadius()) attr_names[(None, 'radius')] = 'radius' if info.getColor(): color = info.getColor() attr_vals[(None, 'color')] = '%d,%d,%d' % (color[0], color[1], color[2]) attr_vals[(None, 'intensity')] = str(color[3]) attr_names[(None, 'color')] = 'color' attr_names[(None, 'intensity')] = 'intensity' if info.getSubdivisions() is not 32: attr_vals[(None, 'subdivisions')] = str(info.getSubdivisions()) attr_names[(None, 'subdivisions')] = 'subdivisions' if info.getXStretch() > 1.001 or info.getXStretch() < 0.999: attr_vals[(None, 'xstretch')] = str(info.getXStretch()) attr_names[(None, 'xstretch')] = 'xstretch' if info.getYStretch() > 1.001 or info.getYStretch() < 0.999: attr_vals[(None, 'ystretch')] = str(info.getYStretch()) attr_names[(None, 'ystretch')] = 'ystretch' elif type == 'image': if info.getImage() == 0: continue img = info.getImage() name = img.getName() attr_vals[(None, 'image')] = str('../' + name) attr_names[(None, 'image')] = 'image' elif type == 'animation': if info.getAnimation() == 0: continue ani = info.getAnimation(); count = 0 newstr = '' image = ani.getFrame(ani.getActionFrame()) fname = image.getName() strings = ([str(s) for s in fname.split('/')]) leng = len(strings) -1 while count < leng: newstr = str(newstr + strings[count] + '/') count += 1 name = str('../' + newstr + 'animation.' + fileExtensions[0]) attr_vals[(None, 'animation')] = str(name) attr_names[(None, 'animation')] = 'animation' else: continue attrs = AttributesNSImpl(attr_vals, attr_names) self.file.write(self.indent_level) self.xmlout.startElementNS((None, 'l'), 'l', attrs) self.xmlout.endElementNS((None, 'l'), 'l') self.file.write('\n') self.endElement('lights') # Save the linked camera of a map. def write_camera( self, map ): cameralist = map.getCameras() for cam in cameralist: if cam.getLocationRef().getMap().getId() == map.getId(): celldimensions = cam.getCellImageDimensions() viewport = cam.getViewPort(); attr_names = { (None, 'id'): 'id', (None, 'zoom'): 'zoom', (None, 'tilt'): 'tile', (None, 'rotation'): 'rotation', (None, 'ref_layer_id'): 'ref_layer_id', (None, 'ref_cell_width'): 'ref_cell_width', (None, 'ref_cell_height'): 'ref_cell_height', } attr_vals = { (None, 'id'): cam.getId(), (None, 'zoom'): str( cam.getZoom()), (None, 'tilt'): str( cam.getTilt()), (None, 'rotation'): str( cam.getRotation()), (None, 'ref_layer_id'): cam.getLocation().getLayer().getId(), (None, 'ref_cell_width'): str( celldimensions.x ), (None, 'ref_cell_height'): str( celldimensions.y ), } # add a viewport entry if the cam isn't full sized if not (viewport == self.engine.getRenderBackend().getArea()): attr_names[(None,'viewport')] = 'viewport' attr_vals[(None,'viewport')] = '%d,%d,%d,%d' % (viewport.x, viewport.y, viewport.w, viewport.h) colors = cam.getLightingColor() if colors[0] < 1.0 or colors[1] < 1.0 or colors[2] < 1.0: attr_names[(None,'light_color')] = 'light_color' attr_vals[(None,'light_color')] = '%f,%f,%f' % (colors[0], colors[1], colors[2]) attrs = AttributesNSImpl( attr_vals, attr_names ) self.startElement( 'camera', attrs ); self.endElement( 'camera' ); def flush(self): self.xmlout.endDocument() self.file.close() def saveResource(self): self.write_map()
# regression test for SAX 2.0
class SocialGraphExport(object): def __init__(self, userinfo, dataset, output): self.gzfd = gzip.open(output, "w") self.output = XMLGenerator(self.gzfd, "utf-8") self.level = 0 self.start_document() self.userinfo = self.load_userinfo(userinfo) for user_id in self.userinfo: self.start_element('node', {'id': str(user_id)}, False) self.start_element('data', {'key': 'screen_name'}, True) self.output.characters(self.get_screen_name(user_id)) self.end_element('data', True) self.end_element('node', False) self.dataset = os.path.abspath(dataset) self.extract_graph() self.end_document() def load_userinfo(self, inputfile): info = {} with gzip.open(inputfile, 'r') as input: for line in input: user = json.loads(line.strip()) info[int(user['id_str'])] = user return info def get_screen_name(self, user_id): return self.userinfo[user_id]['screen_name'] def attrs(self, attributes): return AttributesNSImpl( dict(((None, k), v) for (k, v) in attributes.iteritems()), {}) def start_element(self, name, attrs, nochar=False): self.output.characters(' ' * self.level) self.output.startElementNS((None, name), name, self.attrs(attrs)) if not nochar: self.output.characters('\n') self.level += 1 def end_element(self, name, nochar=False): self.level -= 1 if not nochar: self.output.characters(' ' * self.level) self.output.endElementNS((None, name), name) self.output.characters('\n') def start_document(self): self.start_element( 'graphml', { "xmlns": u"http://graphml.graphdrawing.org/xmlns", "xmlns:xsi": u"http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation": u"http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd", }) self.start_element( 'key', { "id": "screen_name", "for": "node", "attr.name": "screen_name", "attr.type": "string", }) self.end_element('key') self.start_element('graph', { 'id': 'G', 'edgedefault': 'directed', }) def end_document(self): self.end_element('graph') self.output.endElementNS((None, u'graphml'), u'graphml') self.output.endDocument() self.gzfd.close() def extract_graph(self): lastid = 1 lastuser = 0 users = set(self.userinfo.keys()) for user_id in users: opener = open filename = os.path.join(self.dataset, str(user_id)[:2], str(user_id) + '.fws') if not os.path.exists(filename): filename += '.gz' opener = gzip.open if not os.path.exists(filename): continue followers = set() datasize = struct.calcsize('!Q') with opener(filename, 'r') as ffile: while True: data = ffile.read(datasize) if not data: break following = struct.unpack('!Q', data)[0] followers.add(following) for following in followers.intersection(users): self.start_element( 'edge', { "id": str(lastid), "source": str(user_id), "target": str(following), "label": "follows", }, True) self.end_element('edge', True) lastid += 1 lastuser += 1 sys.stderr.write( '\rUser: %d of %d [%02d%%]' % (lastuser, len(users), lastuser * 100.0 / len(users))) sys.stderr.flush() sys.stderr.write('\nCompleted') sys.stderr.flush()
class SchemaGenerator(object): """ This class generates XSD schema based on class description from qooxdoo API. Constructor takes dictinary with class description, generated by qxt.xsd.utils.ApiDataParser. """ def __init__(self, classInfoDict): self.__classInfoDict = classInfoDict def generate(self, fileName): """ Generates XSD based on given classes definitions. Writes it to specified fileName. """ #TODO make proper error handling if something wrong with file. self.__output = open(fileName, "w") self.__doc = XMLGenerator(self.__output, "utf-8") self.__doc.startDocument() self.__start_schema() # generate attibute groups self.__generate_attribute_groups() # generate group of references # generate allGroup element self.__generate_all_group() # generate elements self.__generate_elements() # generate top level elements self.__generate_top_elements() self.__end_schema() self.__doc.endDocument() self.__output.close() def __start_schema(self): """Generates schema header.""" self.__doc.startPrefixMapping(u'xsd', XSD_NS) self.__doc.startPrefixMapping(u'qx', QX_NS) self.__doc.startPrefixMapping(u'qxt', QXT_NS) shemaattr = AttributesNSImpl( { (None, 'targetNamespace'): "http://www.qxtransformer.org/qooxdoo/0.8", (None, 'elementFormDefault'): 'qualified', (None, 'attributeFormDefault'): 'unqualified' }, {}) self.__doc.startElementNS((XSD_NS, u'schema'), u'xsd:schema', shemaattr) def __end_schema(self): """Generated closing tags for schema.""" self.__doc.endElementNS((XSD_NS, u'schema'), u'xsd:schema') self.__doc.endPrefixMapping(u'xsd') def __generate_attribute_groups(self): """Geneartes all groups for API data. Iterates over al classes definitions. """ for className, classInfo in self.__classInfoDict.iteritems(): self.__generate_attribute_group(className, classInfo) def __generate_attribute_group(self, className, classInfo): """Generate an attribute group based on given classInfo for each class. Includes class property in group if it's not overriden from base class (in other case it will be added for base class). """ self.__doc.startElementNS( (XSD_NS, u'attributeGroup'), u'xsd:attributeGroup', AttributesNSImpl({(None, 'name'): className}, {})) #generate attrubute group only if class has any properties if classInfo.has_key('properties'): properties = classInfo['properties'] for propName, prop in properties.iteritems(): #include property only if it's not overriden from base class if not prop.get('overriddenFrom'): self.__doc.startElementNS( (XSD_NS, u'attribute'), u'xsd:attribute', AttributesNSImpl({(None, 'name'): propName}, {})) self.__doc.startElementNS((XSD_NS, u'annotation'), u'xsd:annotation', AttributesNSImpl({}, {})) self.__doc.startElementNS((XSD_NS, u'documentation'), u'xsd:documentation', AttributesNSImpl({}, {})) self.__doc.characters(prop.get('info', '')) self.__doc.endElementNS((XSD_NS, u'documentation'), u'xsd:documentation') self.__doc.endElementNS((XSD_NS, u'annotation'), u'xsd:annotation') self.__doc.endElementNS((XSD_NS, u'attribute'), u'xsd:attribute') self.__doc.endElementNS((XSD_NS, u'attributeGroup'), u'xsd:attributeGroup') def __generate_elements(self): """Generated all elements for given API data. Iterates over all definitions.""" for className, classInfo in self.__classInfoDict.iteritems(): tagName = self.__parse_tag_name(classInfo.get('tagName')) # generate tag only if tag name has namespace (tags without namespaces in config # are base tags only for inheritance inside config file) if tagName and tagName[0]: inheritance_chain = [] self.__fill_inheritance_chain(inheritance_chain, className) self.__generate_element(tagName, className, classInfo, inheritance_chain, [('qx', 'allGroup')]) def __generate_element(self, tagName, className, classInfo, attributeGroups, elementGroup): """Generates an definition of XSD element. @param tagName tuple produced by __parse_tag_name @param className name of widget class @param classInfo dictionary with class information @param attributeGroups array with all attribute groups which this class includes (simply all inheritance chain) @param elementGroup array with all allowed child elements """ self.__doc.startElementNS( (XSD_NS, u'element'), u'xsd:element', AttributesNSImpl({(None, 'name'): tagName[1]}, {})) self.__doc.startElementNS((XSD_NS, u'annotation'), u'xsd:annotation', AttributesNSImpl({}, {})) self.__doc.startElementNS( (XSD_NS, u'documentation'), u'xsd:documentation', AttributesNSImpl({}, {})) self.__doc.characters(classInfo.get('info', '')) self.__doc.endElementNS((XSD_NS, u'documentation'), u'xsd:documentation') self.__doc.endElementNS((XSD_NS, u'annotation'), u'xsd:annotation') self.__doc.startElementNS((XSD_NS, u'complexType'), u'xsd:complexType', AttributesNSImpl({}, {})) #generate references for allowed child elements self.__doc.startElementNS((XSD_NS, u'sequence'), u'xsd:sequence', AttributesNSImpl({}, {})) for group in elementGroup: self.__doc.startElementNS( (XSD_NS, u'group'), u'xsd:group', AttributesNSImpl({(None, 'ref'): ":".join(group)}, {})) self.__doc.endElementNS((XSD_NS, u'group'), u'xsd:group') self.__doc.endElementNS((XSD_NS, u'sequence'), u'xsd:sequence') #generate attribute group for attrGroupName in attributeGroups: #TODO fix namespace of the generated group self.__doc.startElementNS( (XSD_NS, u'attributeGroup'), u'xsd:attributeGroup', AttributesNSImpl({(None, 'ref'): "qx:%s" % attrGroupName}, {})) self.__doc.endElementNS((XSD_NS, u'attributeGroup'), u'xsd:attributeGroup') self.__doc.endElementNS((XSD_NS, u'complexType'), u'xsd:complexType') self.__doc.endElementNS((XSD_NS, u'element'), u'xsd:element') def __generate_group(self, name, tagNames): """Generates group with references to allowed element.""" self.__doc.startElementNS((XSD_NS, u'group'), u'xsd:group', AttributesNSImpl({(None, 'name'): name}, {})) self.__doc.startElementNS((XSD_NS, u'choice'), u'xsd:choice', AttributesNSImpl({}, {})) for tagName in tagNames: self.__doc.startElementNS((XSD_NS, u'element'), u'xsd:element', AttributesNSImpl( { (None, 'ref'): ':'.join(tagName), (None, 'minOccurs'): '0', (None, 'maxOccurs'): 'unbounded' }, {})) self.__doc.endElementNS((XSD_NS, u'element'), u'xsd:element') self.__doc.endElementNS((XSD_NS, u'choice'), u'xsd:choice') self.__doc.endElementNS((XSD_NS, u'group'), u'xsd:group') def __generate_all_group(self): """Genrates group with name 'allGroup' which includes all tags from XSD.""" # getting all tags from class info (only with defined namespaces) groupTags = [] for className, classInfo in self.__classInfoDict.iteritems(): tagName = self.__parse_tag_name(classInfo.get('tagName')) # tag with defined namespace if tagName and tagName[0]: groupTags.append(tagName) # generate group self.__generate_group('allGroup', groupTags) def __generate_top_elements(self): """Generates a set of predefined top level elements.""" self.__generate_element((None, 'application'), None, {}, [], [('qx', 'allGroup')]) def __fill_inheritance_chain(self, chain, className): """Fills inheritance chain for given className. Chain will contain base class, base class of base class, ... Uses recursion. """ chain.append(className) superClass = self.__classInfoDict[className].get('superClass') if superClass: self.__fill_inheritance_chain(chain, superClass) def __parse_tag_name(self, tagName): """Parses tag name and returns it as tuple.""" if not tagName: return None parts = tagName.split(':') if len(parts) == 2: return (parts[0], parts[1]) elif len(parts) == 1: return (None, tagName) else: return None
class SkosCoreWriter: xmlns = { 'rdf': u'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdfs': u'http://www.w3.org/2000/01/rdf-schema#', 'skos': u'http://www.w3.org/2004/02/skos/core#', 'xml': u'http://www.w3.org/XML/1998/namespace', 'foaf': u'http://xmlns.com/foaf/0.1/', 'dc': u'http://purl.org/dc/elements/1.1/', 'dcterms': u'http://purl.org/dc/terms/', } def __init__(self, output, encoding='utf-8', source='http://platecom.inter-cultura.com/concepts', lang=['en','es','fr','de','nl']): self._l = lang self._term_url = lambda t: "%s#%s" % (source, t) self._concept_url = lambda c: self._term_url(c.get_prefered(self._l)[0]) self._xml = XMLGenerator(output, encoding) self._xml.startDocument() for prefix, uri in self.xmlns.items(): self._xml.startPrefixMapping(prefix or None, uri) pass def close(self): self._xml.endDocument() return def _build_tag(self, tag): if type(tag) == type(""): qname = tag tag = (None, tag) else: qname = "%s:%s" % tag tag = (self.xmlns[tag[0]], tag[1]) return tag, qname def startNode(self, tag, attr={}): tag, qname = self._build_tag(tag) self._xml.startElementNS(tag, qname, attr) def endNode(self, tag): tag, qname = self._build_tag(tag) self._xml.endElementNS(tag, qname) def simpleNode(self, tag, value, attr={}): self.startNode(tag, attr) if value: self._xml.characters(value) self.endNode(tag) def writeThesaurus(self, t): self.startNode(('rdf', 'RDF'), {}) for c in t.concepts(): self.writeConcept(t[c]) self.endNode(('rdf', 'RDF')) def writeConcept(self, c): if c.get_prefered(self._l) == []: return # No escribo conceptos sin términos attr={ (self.xmlns['rdf'],'about'): self._concept_url(c) } self.startNode(('skos', 'Concept'), attr) pt = c.get_prefered(self._l[0]) for tl in c['=']: t, l = tl.split("@") attr={ (self.xmlns['xml'],'lang'): l } if tl in pt: self.simpleNode(('skos', 'prefLabel'), t, attr) else: self.simpleNode(('skos', 'altLabel'), t, attr) for tl in c['#']: t, l = tl.split("@") attr={ (self.xmlns['xml'],'lang'): l } self.simpleNode(('skos', 'hiddenLabel'), t, attr) for tl in c['<']: resource = self._term_url(tl) attr={ (self.xmlns['rdf'],'resource'): resource } self.simpleNode(('skos', 'broader'), t, attr) for tl in c['>']: resource = self._term_url(tl) attr={ (self.xmlns['rdf'],'resource'): resource } self.simpleNode(('skos', 'narrower'), t, attr) for tl in c['-']: resource = self._term_url(tl) attr={ (self.xmlns['rdf'],'resource'): resource } self.simpleNode(('skos', 'related'), t, attr) for tl in c['~']: self.warning('SKOS not support similar concepts') self.writePublicNotes(c._pubn.items()) self.endNode(('skos', 'Concept')) pass def writePublicNotes(self, n): for k,v in n: if k == 'definition': for i in v: value, lang = i.split('@') attr={ (self.xmlns['xml'],'lang'): lang } self.simpleNode(('skos', 'definition'), value, attr) if k == 'changenote': for i in v: attr={ (self.xmlns['rdf'],'parseType'): 'Resource' } self.startNode(('skos', 'changenote'), attr) if 'value' in i: self.simpleNode(('rdf', 'value'), i['value'], {}) if 'creator' in i: self.writeCreator(i['creator']) self.endNode(('skos', 'changenote')) if k == 'scopenote': for i in v: attr = {} if 'resource' in i and i['resource'] != None: attr[(self.xmlns['rdf'],'resource')] = i['resource'] self.startNode(('skos', 'scopenote'), attr) if 'document' in i: self.writeDocument(i['document']) self.endNode(('skos', 'scopenote')) def writeDocument(self, d): attr = {} if 'resource' in d and d['resource'] != None: attr[(self.xmlns['rdf'],'resource')] = d['resource'] self.startNode(('foaf', 'document'), attr) if 'creator' in d: self.writeCreator(d['creator']) if 'language' in d: self.writeLanguage(d['language']) self.endNode(('foaf', 'document')) def writeLanguage(self, l): self.startNode(('dc', 'language'), {}) self.startNode(('dcterms', 'RFC1766'), {}) self.simpleNode(('rdf', 'value'), l['value'] , {}) self.simpleNode(('rdfs', 'label'), l['label'] , {}) self.endNode(('dcterms', 'RFC1766')) self.endNode(('dc', 'language')) def writeCreator(self, c): self.startNode(('dc', 'creator'), {}) if 'person' in c: self.startNode(('foaf', 'person'), {}) if 'name' in c['person']: self.simpleNode(('foaf', 'name'), c['person']['name'], {}) if 'mbox' in c['person']: attr={ (self.xmlns['rdf'],'resource'): c['person']['mbox'] } self.simpleNode(('foaf', 'mbox'), None, attr) self.endNode(('foaf', 'person')) self.endNode(('dc', 'creator'))
def handle(self, *args, **options): if len(args) != 1: raise CommandError("This command takes exactly one argument") filepath = args[0] bzip2 = options.get('bzip2', False) gzip_opt = options.get('gzip', False) if bzip2 and not filepath.endswith(".bz2"): filepath += ".bz2" elif gzip_opt and not filepath.endswith(".gz"): filepath += ".gz" limit = options.get("limit", -1) skip = options.get("skip", 0) batch = options.get("batch", 100) newline = options.get("newline", False) qs = Record.objects.all().select_related(*[field.name for field in Record._meta.fields if isinstance(field, ForeignKey)]).prefetch_related(*([field.name for field in Record._meta.many_to_many] + [obj.get_accessor_name() for obj in Record._meta.get_all_related_objects()])).order_by('identifier') # @UndefinedVariable if limit>=0: qs = qs[skip:skip+limit] else: qs = qs[skip:] open_method = None open_args = [] decode_method = lambda s: s if bzip2: open_method = bz2.BZ2File open_args = [filepath, 'wb', 9] elif gzip_opt: open_method = gzip.GzipFile open_args = [filepath, 'wb', 9] else: open_method = codecs.open open_args = [filepath, 'wb', "utf-8"] decode_method = lambda s: s.decode("utf-8") total_records = qs.count() print("Total record to export : %d" % total_records) progress_writer = None with open_method(*open_args) as dest_file: writer = XMLGenerator(dest_file, "UTF-8") writer.startDocument() for prefix,uri in GRAPH_NAMESPACES.items(): writer.startPrefixMapping(prefix, uri) writer.startElementNS((RDF, 'RDF'), 'RDF', AttributesNSImpl({}, {})) writer.characters("\n") for n in range((total_records/batch)+1): for i,r in enumerate(qs[n*batch:((n+1)*batch)]): progress_writer = show_progress(i+(n*batch)+1, total_records, "Exporting record %s" % r.identifier, 40, writer=progress_writer, newline=newline) graph = self.get_graph_from_object(r) do_write = False for line in graph.serialize(format="pretty-xml", encoding="utf-8").splitlines(True): if "<iiep:Record" in line: do_write = True if do_write: dest_file.write(decode_method(line)) if "</iiep:Record>" in line: break writer.endElementNS((RDF, 'RDF'), 'RDF') writer.endDocument() dest_file.write("\n")
class ClipExtractor(ContentHandler): def __init__(self, output, clipId): self._sink = XMLGenerator(output, 'UTF-8', True) self._locator = None self._id = clipId def setDocumentLocator(self, locator): self._locator = locator self._skip = None self._stopAt = None return self._sink.setDocumentLocator(locator) def startDocument(self): # self._prefixes = {} self.scale = self.units = None self._context = [] self._outerSvgRendered = False return self._sink.startDocument() def endDocument(self): # self._prefixes.clear() return self._sink.endDocument() def startPrefixMapping(self, prefix, uri): self._context.append(('xmlns', None, prefix, uri)) # mappings = self._prefixes.get(prefix) # if mappings is None: # self._prefixes[prefix] = mappings = [] # mappings.append(uri) return self._sink.startPrefixMapping(prefix, uri) def endPrefixMapping(self, prefix): context = self._context.pop() assert ('xmlns', None, prefix) == context[:-1] # mappings = self._prefixes.get(prefix) # assert mappings is not None # mappings.pop() return self._sink.endPrefixMapping(prefix) def startElement(self, qname, attrs): raise xml.sax.SAXNotSupportedException( 'This handler must be used with feature "%s"' ' turned on' % xml.sax.handler.feature_namespaces) # if 'xmlns' in attrs: # if self._context: # raise xml.sax.SAXNotSupportedException( # 'This document must be parsed with feature "%s"' # ' turned on' # % xml.sax.handler.feature_namespaces # ) # else: # self.startPrefixMapping('', attrs.get('xmlns')) # ns, name = self._splitNS(qname) # handler = self.ELEMENTS.get((ns, name)) # if handler and handler[0]: # update = handler[0](self, attrs, ns, name) # if not update: # if self._skip is None: # self._skip = len(self._context) + 1 # elif isinstance(update, collections.Sequence): # attrs = update[0] # if len(update) > 1: # ns, name, qname = update[1:] # self._context.append((ns, name, qname, attrs.copy())) # if self._skip is None: # return self._sink.startElement(qname, attrs) def endElement(self, qname): raise xml.sax.SAXNotSupportedException( 'This handler must be used with feature "%s"' ' turned on' % xml.sax.handler.feature_namespaces) # ns, name = self._splitNS(qname) # handler = self.ELEMENTS.get((ns, name)) # if handler and handler[1]: # handler[1](self, ns, name) # context = self._context.pop() # assert (ns, name) == context[:2] # if self._skip is None: # self._sink.endElement(qname) # elif len(self._context) < self._skip: # self._skip = None # if len(self._context) == self._stopAt: # raise self.Done('extraction complete', # self._locator.getLineNumber(), # self._locator.getColumnNumber() # ) def startElementNS(self, name, qname, attrs): if (None, 'xmlns') in attrs: self.startPrefixMapping(None, attrs.get('xmlns')) handler = self.ELEMENTS.get(name) if handler and handler[0]: update = handler[0](self, attrs, *name, qname) if not update: if self._skip is None: self._skip = len(self._context) + 1 elif isinstance(update, collections.Sequence): attrs = update[0] if len(update) > 1: name = tuple(update[1:3]) if len(update) > 3: qname = update[3] self._context.append(name + (qname, attrs.copy())) if self._skip is None: return self._sink.startElementNS(name, qname, attrs) def endElementNS(self, name, qname): handler = self.ELEMENTS.get(name) if handler and handler[1]: handler[1](self, *name, qname) context = self._context.pop() assert name == context[:2] if self._skip is None: self._sink.endElementNS(name, qname) elif len(self._context) < self._skip: self._skip = None if len(self._context) == self._stopAt: while self._context: toClose = self._context.pop() if 'xmlns' == toClose[0]: self._sink.endPrefixMapping(toClose[2]) else: self._sink.ignorableWhitespace('\n') self._sink.endElementNS(toClose[:2], toClose[2]) self._sink.endDocument() raise self.Done('extraction complete', (self._locator.getLineNumber(), self._locator.getColumnNumber())) def elementSvgEnd(self, *args): attrs = self._context[-1][3] if self.ATTR_ID in attrs and \ self._id == attrs.get(self.ATTR_ID): self._stopAt = len(self._context) - 1 def elementSvgStart(self, attrs, *args): if any((CardBackView.SVG_NAMESPACE, 'svg') == e[:2] for e in self._context): return self.ATTR_ID in attrs and \ self._id == attrs.get(self.ATTR_ID) elif not 'viewBox' in attrs and not (None, 'viewBox') in attrs: self._error(None, 'Main <svg> element has no viewBox attribute') else: try: internalSize = attrs.get((None, 'viewBox')) if internalSize is None: internalSize = attrs.get('viewBox') internalSize = self.parseBox(internalSize)[2:] externalSize = list(internalSize) units = [''] * 2 i = -1 for attr in CardBackView.ATTRS_SVG_DIMENSION: i += 1 value, unit = self._distanceAttr(attrs, attr) if unit.strip() == '%': value = None if value is not None: externalSize[i] = value units[i] = unit self.units = tuple(units) try: self.scale = tuple( e / i for e, i in zip(externalSize, internalSize)) except ZeroDivisionError as error: self._error( None, 'Found <svg> with zero dimension(s), viewBox="%s"' % attrs.get((None, 'viewBox'))) return False except: self._error() def elementUseStart(self, attrs, ns, name, qname): target = attrs.get(CardBackView.XLINK_HREF_ATTR) if not target: return False target = target.strip() if '#' == target[0] and self._id == target[1:] \ and not self._outerSvgRendered: try: outerSvg = next(e for e in self._context if (CardBackView.SVG_NAMESPACE, 'svg') == e[:2]) except StopIteration: self._error(None, 'Found <use> element outside of an <svg>') size = [] for attr in CardBackView.ATTRS_SVG_DIMENSION: value, unit = self._distanceAttr(attrs, attr) if unit.strip(): self._error( None, 'Attribute <use %s="%s" ...> is not valid' ' in this context, expected a unit-free number.' % (attr, attrs.get(None, attr))) if value is None: self._error( None, 'Attribute `%s` is missing from <use> element,' ' but is expected in this context.' % attr) size.append(value) sattrs = ('%.6f%s' % (s * v, u) for s, v, u in zip(self.scale, size, self.units)) qnames = { name: outerSvg[3].getQNameByName(name) for name in outerSvg[3].getNames() } attrMap = dict(outerSvg[3].items()) attrMap[(None, 'viewBox')] = self.boxValue((0, 0) + tuple(size)) for attr in zip(CardBackView.ATTRS_SVG_DIMENSION, sattrs): attrMap[(None, attr[0])] = attr[1] self._sink.startElementNS(outerSvg[:2], outerSvg[2], AttributesNSImpl(attrMap, qnames)) self._sink.ignorableWhitespace('\n') self._outerSvgRendered = True qnames = { name: attrs.getQNameByName(name) for name in attrs.getNames() } attrMap = dict(attrs.items()) for attr in ('x', 'y') + CardBackView.ATTRS_SVG_DIMENSION: qnames.pop((None, attr), None) attrMap.pop((None, attr), None) self._sink.startElementNS((ns, name), qname, AttributesNSImpl(attrMap, qnames)) self._sink.endElementNS((ns, name), qname) self._sink.ignorableWhitespace('\n') return False def elementDefsStart(self, attrs, *args): if not self._outerSvgRendered: self._error( None, 'Element <use xlink:href="#%s" ...> must precede' ' the <defs> element in this context.' % self._id) self._skip = None return True ATTR_ID = (None, 'id') ELEMENTS = { (CardBackView.SVG_NAMESPACE, 'svg'): (elementSvgStart, elementSvgEnd), (CardBackView.SVG_NAMESPACE, 'use'): (elementUseStart, None), (CardBackView.SVG_NAMESPACE, 'defs'): (elementDefsStart, None), } def characters(self, content): if self._skip is None: return self._sink.characters(content) def ignorableWhitespace(self, whitespace): if self._skip is None: return self._sink.ignorableWhitespace(whitespace) def processingInstruction(self, target, data): if self._skip is None: return self._sink.processingInstruction(target, data) def skippedEntity(self, name): if self._skip is None: return self._sink.skippedEntity(name) def _error(self, error=True, message=''): if error == True: error = sys.exc_info()[1] if error is None: assert message error = SAXParseException(message, None, self._locator) elif not isinstance(error, SAXParseException): msg = error.args[0] if error.args else '' if msg: msg += ' ' if message: msg += message + ' ' error.args = (msg + 'at line %d, column %d.' % (self._locator.getLineNumber(), self._locator.getColumnNumber()), ) + error.args[1:] raise error class Done(xml.sax.SAXException): pass