[Python-modules-commits] [python-odf] 28/118: More extractions

Wolfgang Borgert debacle at moszumanska.debian.org
Fri Oct 3 21:27:19 UTC 2014


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to reference refs/remotes/upstream/master
in repository python-odf.

commit a55489935345e4de355add39c75667efa7df44a0
Author: Søren Roug <soren.roug at eea.europa.eu>
Date:   Fri Jan 2 17:18:10 2009 +0000

    More extractions
---
 grammar/Makefile                |  11 +++-
 grammar/gen_allowed_attrs.py    | 111 +++++++++++++++++++++++++++++++
 grammar/gen_allowed_children.py | 141 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 261 insertions(+), 2 deletions(-)

diff --git a/grammar/Makefile b/grammar/Makefile
index b66d340..3740f18 100644
--- a/grammar/Makefile
+++ b/grammar/Makefile
@@ -1,12 +1,19 @@
-all: odf required_attrs.py
+all: odf required_attrs.py allowed_attrs.py allowed_children.py
 
 simplified-7-22.rng: OpenDocument-strict-schema-v1.2-draft7.rng
 	xsltproc simplification.xsl $<
 
 required_attrs.py: simplified-7-22.rng
 	python gen_required_attrs.py > required_attrs.py
+
+allowed_attrs.py: simplified-7-22.rng gen_allowed_attrs.py
+	python gen_allowed_attrs.py > allowed_attrs.py
+
+allowed_children.py: simplified-7-22.rng gen_allowed_children.py
+	python gen_allowed_children.py > allowed_children.py
+
 clean:
-	rm -f simplified-7-*.rng required_attrs.py odf
+	rm -f odf simplified-7-*.rng required_attrs.py allowed_attrs.py allowed_children.py
 
 odf:
 	ln -s ../odf
diff --git a/grammar/gen_allowed_attrs.py b/grammar/gen_allowed_attrs.py
new file mode 100755
index 0000000..869f328
--- /dev/null
+++ b/grammar/gen_allowed_attrs.py
@@ -0,0 +1,111 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+# Copyright (C) 2006 Søren Roug, European Environment Agency
+#
+# This is free software.  You may redistribute it under the terms
+# of the Apache license and the GNU General Public License Version
+# 2 or at your option any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+#
+# Contributor(s):
+#
+from xml.sax import make_parser,handler
+from xml.sax.xmlreader import InputSource
+import xml.sax.saxutils
+import sys
+
+RELAXNS=u"http://relaxng.org/ns/structure/1.0"
+
+elements = {}
+currdef = None
+currelement = None
+currnode = None
+
+from odf.namespaces import *
+
+class Node:
+    ns = None
+    name = None
+
+class Element(Node):
+    " Element "
+    def __init__(self):
+        self.attrs = {}
+
+class Attribute(Node):
+    " Attribute "
+
+#
+# Extract headings from content.xml
+#
+class ODTHeadingHandler(handler.ContentHandler):
+    """ Extract headings from content.xml of an ODT file """
+    def __init__(self):
+        self.data = []
+        self.level = 0
+
+    def text(self):
+        return ''.join(self.data)
+
+    def characters(self, data):
+        self.data.append(data)
+
+    def startElementNS(self, tag, qname, attrs):
+        global currnode, currelement
+        if tag == (RELAXNS, 'element'):
+            currelement = currnode = Element()
+        elif tag == (RELAXNS, 'attribute'):
+            currnode = Attribute()
+        elif tag == (RELAXNS, 'name'):
+            currnode.ns = attrs.get((None,'ns'),'')
+            self.data = []
+
+    def endElementNS(self, tag, qname):
+        global currnode, currelement
+        if tag == (RELAXNS, 'element'):
+            elements[(currelement.ns, currelement.name)] = currelement
+            currelement = None
+        elif tag == (RELAXNS, 'attribute'):
+            currelement.attrs[currnode.name] = currnode
+            currnode = None
+        elif tag == (RELAXNS, 'name'):
+            currnode.name = self.text()
+        elif tag == (RELAXNS, 'anyName'):
+            currnode.name = "__ANYNAME__"
+        self.data = []
+
+def odtheadings(relaxfile):
+    content = file(relaxfile)
+    parser = make_parser()
+    parser.setFeature(handler.feature_namespaces, 1)
+    parser.setContentHandler(ODTHeadingHandler())
+    parser.setErrorHandler(handler.ErrorHandler())
+
+    inpsrc = InputSource()
+    inpsrc.setByteStream(content)
+    parser.parse(inpsrc)
+
+
+if __name__ == "__main__":
+    filler = "          "
+    odtheadings("simplified-7-22.rng")
+
+    print "allowed_attributes = {"
+
+    slist = elements.keys()
+    slist.sort()
+    for s in slist:
+        e = elements[s]
+        print "\t(%sNS,u'%s'):(" % (nsdict.get(e.ns,'unknown').upper(), e.name)
+        for a in e.attrs.values():
+            print "\t\t(%sNS,u'%s')," % (nsdict.get(a.ns,'unknown').upper(), a.name)
+        print "\t),"
+    print "}"
diff --git a/grammar/gen_allowed_children.py b/grammar/gen_allowed_children.py
new file mode 100755
index 0000000..1055464
--- /dev/null
+++ b/grammar/gen_allowed_children.py
@@ -0,0 +1,141 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+# Copyright (C) 2006 Søren Roug, European Environment Agency
+#
+# This is free software.  You may redistribute it under the terms
+# of the Apache license and the GNU General Public License Version
+# 2 or at your option any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+#
+# Contributor(s):
+#
+from xml.sax import make_parser,handler
+from xml.sax.xmlreader import InputSource
+import xml.sax.saxutils
+import sys
+from odf.namespaces import *
+
+RELAXNS=u"http://relaxng.org/ns/structure/1.0"
+
+elements = {}
+
+class Node:
+    ns = None
+    name = None
+
+class Element(Node):
+    " Element "
+    def __init__(self):
+        self.attrs = {}
+
+class Attribute(Node):
+    " Attribute "
+
+#
+# Extract headings from content.xml
+#
+class S22RelaxParser(handler.ContentHandler):
+    """ Extract headings from content.xml of an ODT file """
+
+    optional = 0
+    currattr = None
+    currelem = None
+    currnode = None
+    currdef = None
+    definitions = {}
+    ignore = 0
+
+    def __init__(self):
+        self.data = []
+        self.level = 0
+
+    def text(self):
+        return ''.join(self.data).strip()
+
+    def characters(self, data):
+        self.data.append(data)
+
+    def startElementNS(self, tag, qname, attrs):
+        if self.ignore == 1:
+            return
+        #print "START ",tag
+        if tag == (RELAXNS, 'define'):
+            self.currdef = {}
+            self.currdef['refs'] = []
+            self.currdef['name'] = attrs.get( (None, 'name'))
+        elif tag in ((RELAXNS, 'attribute'), (RELAXNS, 'start')):
+            self.ignore = 1
+        elif tag == (RELAXNS, 'ref'):
+            ref = attrs.get( (None, 'name'))
+            if ref not in self.currdef['refs']:
+                self.currdef['refs'].append(ref)
+        elif tag == (RELAXNS, 'name'):
+            self.currdef['ns'] = attrs.get( (None, 'ns'))
+            self.data = []
+
+    def endElementNS(self, tag, qname):
+        if tag in ((RELAXNS, 'attribute'), (RELAXNS, 'start')):
+            self.ignore = 0
+            return
+        if self.ignore == 1:
+            return
+        #print "END   ",tag
+        if tag == (RELAXNS, 'define'):
+            if self.currdef.has_key('element'):
+                self.definitions[self.currdef['name']] = self.currdef
+        elif tag == (RELAXNS, 'name'):
+            #print "ELEMENT NAME:", self.text()
+            self.currdef['element'] = self.text()
+        elif tag == (RELAXNS, 'anyName'):
+            self.currdef['element'] = "__ANYNAME__"
+        self.data = []
+
+def odtheadings(relaxfile):
+    content = file(relaxfile)
+    parser = make_parser()
+    parser.setFeature(handler.feature_namespaces, 1)
+    p = S22RelaxParser()
+    parser.setContentHandler(p)
+    parser.setErrorHandler(handler.ErrorHandler())
+
+    inpsrc = InputSource()
+    inpsrc.setByteStream(content)
+    parser.parse(inpsrc)
+    return p
+
+
+if __name__ == "__main__":
+    filler = "          "
+    p = odtheadings("simplified-7-22.rng")
+
+defs = p.definitions
+#for key,val in defs.items():
+#   print key, val['element']
+#sys.exit()
+keys= defs.keys()
+keys.sort()
+print "allowed_children = {"
+for key in keys:
+    val = defs[key]
+    if val['element'] == u'__ANYNAME__':
+        continue
+    ns = val.get('ns','UNKNOWN')
+    refs = val['refs']
+    if len(refs) == 1 and defs[refs[0]]['element'] == u'__ANYNAME__':
+        print "\t(%sNS,u'%s') : " % (nsdict.get(ns,'unknown').upper(), val['element'])
+        print "\t\tNone,"
+    else:
+        print "\t(%sNS,u'%s') : (" % (nsdict.get(ns,'unknown').upper(), val['element'])
+        for r in refs:
+            ns = defs[r].get('ns','UNKNOWN')
+            print "\t\t(%sNS,u'%s'), " % (nsdict.get(ns,'unknown').upper(), defs[r]['element'])
+        print "\t),"
+print "}"

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-odf.git



More information about the Python-modules-commits mailing list