~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Tools/generateBase/generateTools.py

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
Import upstream version 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! python
 
2
# -*- coding: utf-8 -*-
 
3
# (c) 2007 Jürgen Riegel 
 
4
 
 
5
import os, errno
 
6
 
 
7
 
 
8
def ensureDir(path,mode=0777):
 
9
        try: 
 
10
                os.makedirs(path,mode)
 
11
        except OSError, err:
 
12
                #  raise an error unless it's about a alredy existing directory
 
13
                print "Dir Exist"
 
14
                #if errno != 17 or not os.path.isdir(path):
 
15
                #       raise
 
16
                        
 
17
def convertMultilineString(str):
 
18
        str = str.replace('\n','\\n')
 
19
        str = str.replace('"','\\"')
 
20
        return str      
 
21
        
 
22
"Yet Another Python Templating Utility, Version 1.2"
 
23
 
 
24
import sys
 
25
 
 
26
# utility stuff to avoid tests in the mainline code
 
27
class _nevermatch:
 
28
    "Polymorphic with a regex that never matches"
 
29
    def match(self, line):
 
30
        return None
 
31
_never = _nevermatch()     # one reusable instance of it suffices
 
32
def identity(string, why):
 
33
    "A do-nothing-special-to-the-input, just-return-it function"
 
34
    return string
 
35
def nohandle(string):
 
36
    "A do-nothing handler that just re-raises the exception"
 
37
    raise
 
38
 
 
39
# and now the real thing
 
40
class copier:
 
41
    "Smart-copier (YAPTU) class"
 
42
    def copyblock(self, i=0, last=None):
 
43
        "Main copy method: process lines [i,last) of block"
 
44
        def repl(match, self=self):
 
45
            "return the eval of a found expression, for replacement"
 
46
            # uncomment for debug: print '!!! replacing',match.group(1)
 
47
            expr = self.preproc(match.group(1), 'eval')
 
48
            try: return str(eval(expr, self.globals, self.locals))
 
49
            except: return str(self.handle(expr))
 
50
        block = self.locals['_bl']
 
51
        if last is None: last = len(block)
 
52
        while i<last:
 
53
            line = block[i]
 
54
            match = self.restat.match(line)
 
55
            if match:   # a statement starts "here" (at line block[i])
 
56
                # i is the last line to _not_ process
 
57
                stat = match.string[match.end(0):].strip()
 
58
                j=i+1   # look for 'finish' from here onwards
 
59
                nest=1  # count nesting levels of statements
 
60
                while j<last:
 
61
                    line = block[j]
 
62
                    # first look for nested statements or 'finish' lines
 
63
                    if self.restend.match(line):    # found a statement-end
 
64
                        nest = nest - 1     # update (decrease) nesting
 
65
                        if nest==0: break   # j is first line to _not_ process
 
66
                    elif self.restat.match(line):   # found a nested statement
 
67
                        nest = nest + 1     # update (increase) nesting
 
68
                    elif nest==1:   # look for continuation only at this nesting
 
69
                        match = self.recont.match(line)
 
70
                        if match:                   # found a contin.-statement
 
71
                            nestat = match.string[match.end(0):].strip()
 
72
                            stat = '%s _cb(%s,%s)\n%s' % (stat,i+1,j,nestat)
 
73
                            i=j     # again, i is the last line to _not_ process
 
74
                    j=j+1
 
75
                stat = self.preproc(stat, 'exec')
 
76
                stat = '%s _cb(%s,%s)' % (stat,i+1,j)
 
77
                # for debugging, uncomment...: print "-> Executing: {"+stat+"}"
 
78
                exec stat in self.globals,self.locals
 
79
                i=j+1
 
80
            else:       # normal line, just copy with substitution
 
81
                self.ouf.write(self.regex.sub(repl,line))
 
82
                i=i+1
 
83
    def __init__(self, regex=_never, dict={},
 
84
            restat=_never, restend=_never, recont=_never, 
 
85
            preproc=identity, handle=nohandle, ouf=sys.stdout):
 
86
        "Initialize self's attributes"
 
87
        self.regex   = regex
 
88
        self.globals = dict
 
89
        self.locals  = { '_cb':self.copyblock }
 
90
        self.restat  = restat
 
91
        self.restend = restend
 
92
        self.recont  = recont
 
93
        self.preproc = preproc
 
94
        self.handle  = handle
 
95
        self.ouf     = ouf
 
96
    def copy(self, block=None, inf=sys.stdin):
 
97
        "Entry point: copy-with-processing a file, or a block of lines"
 
98
        if block is None: block = inf.readlines()
 
99
        self.locals['_bl'] = block
 
100
        self.copyblock()
 
101
 
 
102
def replace(template,dict,file):
 
103
  "Test: copy a block of lines, with full processing"
 
104
  import re
 
105
  rex=re.compile('@([^@]+)@')
 
106
  rbe=re.compile('\+')
 
107
  ren=re.compile('-')
 
108
  rco=re.compile('= ')
 
109
  x=23 # just a variable to try substitution
 
110
  cop = copier(rex, dict, rbe, ren, rco)
 
111
  lines_block = [line+'\n' for line in template.split('\n')]
 
112
  cop.ouf = file
 
113
  cop.copy(lines_block)
 
114
 
 
115
if __name__=='__main__':
 
116
    "Test: copy a block of lines, with full processing"
 
117
    import re
 
118
    rex=re.compile('@([^@]+)@')
 
119
    rbe=re.compile('\+')
 
120
    ren=re.compile('-')
 
121
    rco=re.compile('= ')
 
122
    x=23 # just a variable to try substitution
 
123
    cop = copier(rex, globals(), rbe, ren, rco)
 
124
    lines_block = [line+'\n' for line in """
 
125
A first, plain line -- it just gets copied.
 
126
A second line, with @x@ substitutions.
 
127
+ x+=1   # non-block statements MUST end with comments
 
128
-
 
129
Now the substitutions are @x@.
 
130
+ if x>23:
 
131
After all, @x@ is rather large!
 
132
= else:
 
133
After all, @x@ is rather small!
 
134
-
 
135
+ for i in range(3):
 
136
  Also, @i@ times @x@ is @i*x@.
 
137
-
 
138
One last, plain line at the end.""".split('\n')]
 
139
    print "*** input:"
 
140
    print ''.join(lines_block)
 
141
    print "*** output:"
 
142
    cop.copy(lines_block)
 
143