[Python-modules-commits] [python-odf] 29/118: Can now generate allows_text list

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 d6e672fc5dc2da5b6c191b89a80d7428bcad6d68
Author: Søren Roug <soren.roug at eea.europa.eu>
Date:   Fri Jan 2 22:56:56 2009 +0000

    Can now generate allows_text list
---
 grammar/Makefile                                   | 14 ++--
 grammar/gen_allowed_attrs.py                       | 25 ++++---
 grammar/gen_allowed_children.py                    | 80 ++++++++++++++--------
 ...{gen_allowed_children.py => gen_allows_text.py} | 59 +++++++---------
 odf/namespaces.py                                  |  4 ++
 5 files changed, 104 insertions(+), 78 deletions(-)

diff --git a/grammar/Makefile b/grammar/Makefile
index 3740f18..b2c57c5 100644
--- a/grammar/Makefile
+++ b/grammar/Makefile
@@ -1,17 +1,23 @@
-all: odf required_attrs.py allowed_attrs.py allowed_children.py
+all: odf grammarnew.py
 
 simplified-7-22.rng: OpenDocument-strict-schema-v1.2-draft7.rng
 	xsltproc simplification.xsl $<
 
+grammarnew.py: allowed_children.py allows_text.py required_attrs.py allowed_attrs.py
+	cat allowed_children.py allows_text.py required_attrs.py allowed_attrs.py >grammarnew.py
+
+allowed_children.py: simplified-7-22.rng gen_allowed_children.py
+	python gen_allowed_children.py > allowed_children.py
+
+allows_text.py: simplified-7-22.rng gen_allows_text.py
+	python gen_allows_text.py > allows_text.py
+
 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 odf simplified-7-*.rng required_attrs.py allowed_attrs.py allowed_children.py
 
diff --git a/grammar/gen_allowed_attrs.py b/grammar/gen_allowed_attrs.py
index 869f328..9f44170 100755
--- a/grammar/gen_allowed_attrs.py
+++ b/grammar/gen_allowed_attrs.py
@@ -44,10 +44,9 @@ class Attribute(Node):
     " Attribute "
 
 #
-# Extract headings from content.xml
 #
-class ODTHeadingHandler(handler.ContentHandler):
-    """ Extract headings from content.xml of an ODT file """
+class S22RelaxParser(handler.ContentHandler):
+
     def __init__(self):
         self.data = []
         self.level = 0
@@ -82,11 +81,11 @@ class ODTHeadingHandler(handler.ContentHandler):
             currnode.name = "__ANYNAME__"
         self.data = []
 
-def odtheadings(relaxfile):
+def parse_rng(relaxfile):
     content = file(relaxfile)
     parser = make_parser()
     parser.setFeature(handler.feature_namespaces, 1)
-    parser.setContentHandler(ODTHeadingHandler())
+    parser.setContentHandler(S22RelaxParser())
     parser.setErrorHandler(handler.ErrorHandler())
 
     inpsrc = InputSource()
@@ -95,8 +94,7 @@ def odtheadings(relaxfile):
 
 
 if __name__ == "__main__":
-    filler = "          "
-    odtheadings("simplified-7-22.rng")
+    parse_rng("simplified-7-22.rng")
 
     print "allowed_attributes = {"
 
@@ -104,8 +102,13 @@ if __name__ == "__main__":
     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),"
+        if e.name == u'__ANYNAME__':
+            continue
+        if len(e.attrs.values()) == 1 and e.attrs.values()[0].name == u'__ANYNAME__':
+            print "\t(%sNS,u'%s'): None," % (nsdict.get(e.ns,'unknown').upper(), e.name)
+        else:
+            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
index 1055464..7aeaccb 100755
--- a/grammar/gen_allowed_children.py
+++ b/grammar/gen_allowed_children.py
@@ -25,7 +25,7 @@ from odf.namespaces import *
 
 RELAXNS=u"http://relaxng.org/ns/structure/1.0"
 
-elements = {}
+#elements = {}
 
 class Node:
     ns = None
@@ -40,7 +40,6 @@ class Attribute(Node):
     " Attribute "
 
 #
-# Extract headings from content.xml
 #
 class S22RelaxParser(handler.ContentHandler):
     """ Extract headings from content.xml of an ODT file """
@@ -98,7 +97,7 @@ class S22RelaxParser(handler.ContentHandler):
             self.currdef['element'] = "__ANYNAME__"
         self.data = []
 
-def odtheadings(relaxfile):
+def parse_rng(relaxfile):
     content = file(relaxfile)
     parser = make_parser()
     parser.setFeature(handler.feature_namespaces, 1)
@@ -113,29 +112,52 @@ def odtheadings(relaxfile):
 
 
 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 "}"
+    p = parse_rng("simplified-7-22.rng")
+
+    defs = p.definitions
+    keys= defs.keys()
+    keys.sort()
+    print '''# -*- coding: utf-8 -*-
+# Copyright (C) 2006-2008 Søren Roug, European Environment Agency
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library 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
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+#
+# Contributor(s):
+#
+
+__doc__=""" In principle the OpenDocument schema converted to python structures.
+Currently it contains the legal child elements of a given element.
+To be used for validation check in the API
+"""
+
+from odf.namespaces import *'''
+
+    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 "}"
diff --git a/grammar/gen_allowed_children.py b/grammar/gen_allows_text.py
similarity index 72%
copy from grammar/gen_allowed_children.py
copy to grammar/gen_allows_text.py
index 1055464..cb68f70 100755
--- a/grammar/gen_allowed_children.py
+++ b/grammar/gen_allows_text.py
@@ -25,7 +25,7 @@ from odf.namespaces import *
 
 RELAXNS=u"http://relaxng.org/ns/structure/1.0"
 
-elements = {}
+#elements = {}
 
 class Node:
     ns = None
@@ -40,7 +40,6 @@ class Attribute(Node):
     " Attribute "
 
 #
-# Extract headings from content.xml
 #
 class S22RelaxParser(handler.ContentHandler):
     """ Extract headings from content.xml of an ODT file """
@@ -69,17 +68,19 @@ class S22RelaxParser(handler.ContentHandler):
         #print "START ",tag
         if tag == (RELAXNS, 'define'):
             self.currdef = {}
-            self.currdef['refs'] = []
             self.currdef['name'] = attrs.get( (None, 'name'))
+            self.currdef['type'] = None
+            self.currdef['datatypeLibrary'] = None
         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 = []
+        elif tag == (RELAXNS, 'data'):
+            self.currdef['type'] = attrs.get( (None, 'type'))
+            self.currdef['datatypeLibrary'] = attrs.get( (None, 'datatypeLibrary'))
+        elif tag == (RELAXNS, 'text'):
+            self.currdef['type'] = "text"
 
     def endElementNS(self, tag, qname):
         if tag in ((RELAXNS, 'attribute'), (RELAXNS, 'start')):
@@ -98,7 +99,7 @@ class S22RelaxParser(handler.ContentHandler):
             self.currdef['element'] = "__ANYNAME__"
         self.data = []
 
-def odtheadings(relaxfile):
+def parse_rng(relaxfile):
     content = file(relaxfile)
     parser = make_parser()
     parser.setFeature(handler.feature_namespaces, 1)
@@ -113,29 +114,19 @@ def odtheadings(relaxfile):
 
 
 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 "}"
+    p = parse_rng("simplified-7-22.rng")
+
+    defs = p.definitions
+    keys= defs.keys()
+    keys.sort()
+
+    print "allows_text = ("
+    for key in keys:
+        val = defs[key]
+        if val['element'] == u'__ANYNAME__':
+            continue
+        if val.get('type') is None:
+            continue
+        ns = val.get('ns','UNKNOWN')
+        print "\t(%sNS,u'%s')," % (nsdict.get(ns,'unknown').upper(), val['element'])
+    print ")"
diff --git a/odf/namespaces.py b/odf/namespaces.py
index 5dc9112..3fb42af 100644
--- a/odf/namespaces.py
+++ b/odf/namespaces.py
@@ -40,6 +40,7 @@ OOONS          = u"http://openoffice.org/2004/office"
 OOOWNS         = u"http://openoffice.org/2004/writer"
 OOOCNS         = u"http://openoffice.org/2004/calc"
 PRESENTATIONNS = u"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0"
+RDFANS         = u"http://docs.oasis-open.org/opendocument/meta/rdfa#"
 SCRIPTNS       = u"urn:oasis:names:tc:opendocument:xmlns:script:1.0"
 SMILNS         = u"urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0"
 STYLENS        = u"urn:oasis:names:tc:opendocument:xmlns:style:1.0"
@@ -48,6 +49,7 @@ TABLENS        = u"urn:oasis:names:tc:opendocument:xmlns:table:1.0"
 TEXTNS         = u"urn:oasis:names:tc:opendocument:xmlns:text:1.0"
 XFORMSNS       = u"http://www.w3.org/2002/xforms"
 XLINKNS        = u"http://www.w3.org/1999/xlink"
+XMLNS          = "http://www.w3.org/XML/1998/namespace"
 
 
 nsdict = {
@@ -71,6 +73,7 @@ nsdict = {
    OOOWNS: u'ooow',
    OOOCNS: u'ooc',
    PRESENTATIONNS: u'presentation',
+   RDFANS: u'rdfa',
    SCRIPTNS: u'script',
    SMILNS: u'smil',
    STYLENS: u'style',
@@ -79,4 +82,5 @@ nsdict = {
    TEXTNS: u'text',
    XFORMSNS: u'xforms',
    XLINKNS: u'xlink',
+   XMLNS: u'xml',
 }

-- 
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