1 | ''' Generate vocabulary definition document and sample vocabulary document. |
---|
2 | USAGE |
---|
3 | ..... |
---|
4 | ptxt.py [-f srcFile] [samp|defn] |
---|
5 | ''' |
---|
6 | import string, re, collections, sys, uuid |
---|
7 | |
---|
8 | if len(sys.argv) == 1: |
---|
9 | print __doc__ |
---|
10 | exit() |
---|
11 | |
---|
12 | args = sys.argv[1:] |
---|
13 | if args[0] == '-f': |
---|
14 | srcFn = args[1] |
---|
15 | args = args[2:] |
---|
16 | else: |
---|
17 | srcFn = 'parVocabDefn.txt' |
---|
18 | srcFn = 'dreqDefn.txt' |
---|
19 | |
---|
20 | mode = args[0] |
---|
21 | |
---|
22 | assert mode in ["defn", "samp"] |
---|
23 | |
---|
24 | re_it = re.compile( '{(.*)}' ) |
---|
25 | re_is = re.compile( '\[(.*)\]' ) |
---|
26 | re_atdef = re.compile( '^(?P<n>\S+)\s*(((?P<b>\[.*?])|(?P<a>\{.*?\})|(?P<c>\<.*?\>))\s*){0,3}$' ) |
---|
27 | re_atdef = re.compile( '^(?P<n>\S+)\s*((\[(?P<b>.*?)\]|\{(?P<a>.*?)\}|\<(?P<c>.*?)\>)\s*){0,3}$' ) |
---|
28 | |
---|
29 | ### tuble to define attributes of an item |
---|
30 | nt__itematt = collections.namedtuple( 'itematt', ['name','type','title','clss','techn'] ) |
---|
31 | |
---|
32 | vocab_elTmpl = '''<table label="%(label)s" title="%(title)s" id="%(id)s" itemLabelMode="%(ilm)s"> |
---|
33 | %(itemAttrList)s |
---|
34 | </table> |
---|
35 | ''' |
---|
36 | ial_elTmpl = ' <rowAttribute label="%(label)s"%(wrappedType)s%(wrappedTitle)s%(wrappedClass)s%(wrappedTechn)s/>' |
---|
37 | |
---|
38 | expl_Tmpl = '''<%(label)s class="vocab" title="%(title)s" id="%(id)s"> |
---|
39 | <!-- <info srcType="dummy" srcRef="ptxt.py">Dummy entries</info> --> |
---|
40 | %(exampleItem)s |
---|
41 | </%(label)s> |
---|
42 | ''' |
---|
43 | item_Tmpl = '<item id="%(id)s" label="%(label)s" title="%(title)s"%(extras)s/>' |
---|
44 | |
---|
45 | class vocab(object): |
---|
46 | |
---|
47 | def __init__( self, line, kk=0 ): |
---|
48 | bits = map( string.strip, string.split(line, ';' ) ) |
---|
49 | assert len (bits) == 4, '[1] Cannot parse %s' % line |
---|
50 | assert bits[0][:5] == 'vocab', '[2] Cannot parse %s' % line |
---|
51 | self.label = string.split(bits[0], ' ')[1] |
---|
52 | self.title = bits[1] |
---|
53 | self.id = 'cmip.drv.%3.3i' % kk |
---|
54 | self.ilt = bits[3] |
---|
55 | self.kk = kk |
---|
56 | self.msg( '[%s] %s {%s:%s}' % (self.label, self.title, self.id, self.ilt) ) |
---|
57 | self.itematts = [ nt__itematt( 'label','xs:string',None,None,None ), |
---|
58 | nt__itematt( 'title','xs:string',None,None,None ) ] |
---|
59 | |
---|
60 | def tmpl(self,oo=None,mode="defn"): |
---|
61 | if mode == "defn": |
---|
62 | ss = [] |
---|
63 | for i in self.itematts: |
---|
64 | if i.clss != None: |
---|
65 | wrappedClass = ' class="%s"' % i.clss |
---|
66 | else: |
---|
67 | wrappedClass = '' |
---|
68 | |
---|
69 | if i.techn != None: |
---|
70 | wrappedTechn = ' techNote="%s"' % i.techn |
---|
71 | else: |
---|
72 | wrappedTechn = '' |
---|
73 | |
---|
74 | if i.title != None: |
---|
75 | wrappedTitle = ' title="%s"' % i.title |
---|
76 | else: |
---|
77 | wrappedTitle = '' |
---|
78 | |
---|
79 | if i.type != "xs:string": |
---|
80 | wrappedType = ' type="%s"' % i.type |
---|
81 | else: |
---|
82 | wrappedType = '' |
---|
83 | |
---|
84 | label = i.name |
---|
85 | ss.append(ial_elTmpl % locals()) |
---|
86 | sss = string.join( ss, '\n' ) |
---|
87 | |
---|
88 | label = self.label |
---|
89 | title = self.title |
---|
90 | id = self.id |
---|
91 | ilm = self.ilt |
---|
92 | itemAttrList = sss |
---|
93 | self.vocab = vocab_elTmpl % locals() |
---|
94 | else: |
---|
95 | ## construct sample item ## |
---|
96 | if self.ilt == 'an': |
---|
97 | label = 'example01' |
---|
98 | elif self.ilt == 'def': |
---|
99 | label = 'example-01' |
---|
100 | elif self.ilt == 'int': |
---|
101 | label = '1' |
---|
102 | title = 'dummy title string' |
---|
103 | |
---|
104 | extras = '' |
---|
105 | for i in self.itematts: |
---|
106 | if i.name not in ['label','title']: |
---|
107 | value = "noType" |
---|
108 | if i.type == "xs:string": |
---|
109 | if i.name == 'uuid': |
---|
110 | value = str( uuid.uuid1() ) |
---|
111 | else: |
---|
112 | value = 'dummyAt' |
---|
113 | elif i.type == "xs:integer": |
---|
114 | value = '25' |
---|
115 | elif i.type == "xs:duration": |
---|
116 | value = 'P1Y' |
---|
117 | |
---|
118 | extras += ' %s="%s"' % (i.name,value) |
---|
119 | id = '001.%3.3i.%3.3i' % (self.kk,1) |
---|
120 | exampleItem = item_Tmpl % locals() |
---|
121 | label = self.label |
---|
122 | title = self.title |
---|
123 | id = self.id |
---|
124 | ilm = self.ilt |
---|
125 | self.vocab = expl_Tmpl % locals() |
---|
126 | ## |
---|
127 | ## print and write if needed |
---|
128 | ## |
---|
129 | if oo != None: |
---|
130 | oo.write( self.vocab ) |
---|
131 | self.msg( vocab ) |
---|
132 | |
---|
133 | def msg(self,txt): |
---|
134 | swallow=True |
---|
135 | if swallow: |
---|
136 | return |
---|
137 | print text |
---|
138 | |
---|
139 | def attr(self,line): |
---|
140 | bits = map( string.strip, string.split(line[3:], ';' ) ) |
---|
141 | for b in bits: |
---|
142 | if string.strip(b) != '': |
---|
143 | bn = string.split(b, ' ')[0] |
---|
144 | x = self.pb(b) |
---|
145 | self.itematts.append( nt__itematt( bn, x[0], x[1], x[2], x[3] ) ) |
---|
146 | self.msg( '%s, %s, %s, %s, %s' % (bn, x[0], x[1], x[2], x[3]) ) |
---|
147 | |
---|
148 | def pb(self, b): |
---|
149 | x = re_it.findall(b) |
---|
150 | m = re_atdef.match( b ) |
---|
151 | if m == None: |
---|
152 | print 'COULD NOT SCAN: ',b |
---|
153 | assert False |
---|
154 | else: |
---|
155 | ee = m.groupdict() |
---|
156 | for k in ['a','b','c']: |
---|
157 | if type( ee[k] ) == type( 'x' ): |
---|
158 | assert string.find( ee[k], '"' ) == -1, 'unable to deal with quote in string: %s' % ee[k] |
---|
159 | itemTitle = ee['a'] |
---|
160 | itemType = ee['b'] |
---|
161 | itemClass = ee['c'] |
---|
162 | itemTechNote = None |
---|
163 | if itemType == None: |
---|
164 | itemType = 'xs:string' |
---|
165 | if type(itemClass) == type('x'): |
---|
166 | if string.find(itemClass, '|' ) != -1: |
---|
167 | bits = string.split( itemClass, '|' ) |
---|
168 | assert len(bits) == 2, 'ERROR.001.0001: failed to parse attribute definition: %s' % b |
---|
169 | itemClass = bits[0] |
---|
170 | itemTechNote = bits[1] |
---|
171 | |
---|
172 | #if len(x) > 0: |
---|
173 | #itemTitle = x[0] |
---|
174 | #else: |
---|
175 | #itemTitle = None |
---|
176 | #x = re_is.findall(b) |
---|
177 | #if len(x) > 0: |
---|
178 | #itemType = x[0] |
---|
179 | #else: |
---|
180 | #itemType = 'xs:string' |
---|
181 | return [itemType, itemTitle,itemClass,itemTechNote] |
---|
182 | |
---|
183 | class main(object): |
---|
184 | |
---|
185 | def __init__(self,fn): |
---|
186 | ii = [] |
---|
187 | for l in open('../../docs/%s' % fn).readlines(): |
---|
188 | if l[0] != '#': |
---|
189 | ii.append(l) |
---|
190 | kk= 0 |
---|
191 | this = None |
---|
192 | fstem = string.split( fn, '.' )[0] |
---|
193 | fns = {'defn':'%sDefn' % fstem, 'samp':'%sSample' % fstem } |
---|
194 | fn = fns[mode] |
---|
195 | oo = open( 'out/%s.xml' % fn, 'w' ) |
---|
196 | if mode == 'samp': |
---|
197 | mainEl = "main" |
---|
198 | else: |
---|
199 | mainEl = "defDoc" |
---|
200 | |
---|
201 | oo.write( '''<?xml version="1.0" ?> |
---|
202 | <%s>\n''' % mainEl ) |
---|
203 | |
---|
204 | for l in ii: |
---|
205 | if l[:5] == 'vocab': |
---|
206 | if this != None: |
---|
207 | this.tmpl(oo=oo,mode=mode) |
---|
208 | kk+=1 |
---|
209 | this = vocab(l,kk=kk) |
---|
210 | else: |
---|
211 | this.attr( l ) |
---|
212 | |
---|
213 | this.tmpl(oo=oo,mode=mode) |
---|
214 | oo.write( '</%s>\n' % mainEl ) |
---|
215 | oo.close() |
---|
216 | |
---|
217 | |
---|
218 | m = main(srcFn) |
---|