Back to index

python3.2  3.2.2
Functions | Variables
h2py Namespace Reference

Functions

def main
def pytify
def process

Variables

tuple p_define = re.compile('^[\t ]*#[\t ]*define[\t ]+([a-zA-Z0-9_]+)[\t ]+')
tuple p_macro
tuple p_include = re.compile('^[\t ]*#[\t ]*include[\t ]+<([a-zA-Z0-9_/\.]+)')
tuple p_comment = re.compile(r'/\*([^*]+|\*+[^/])*(\*+/)?')
tuple p_cpp_comment = re.compile('//.*')
list ignores = [p_comment, p_cpp_comment]
tuple p_char = re.compile(r"'(\\.[^\\]*|[^\\])'")
tuple p_hex = re.compile(r"0x([0-9a-fA-F]+)L?")
dictionary filedict = {}
dictionary importable = {}
list searchdirs = os.environ['include']

Function Documentation

def h2py.main ( void  )

Definition at line 54 of file h2py.py.

00054 
00055 def main():
00056     global filedict
00057     opts, args = getopt.getopt(sys.argv[1:], 'i:')
00058     for o, a in opts:
00059         if o == '-i':
00060             ignores.append(re.compile(a))
00061     if not args:
00062         args = ['-']
00063     for filename in args:
00064         if filename == '-':
00065             sys.stdout.write('# Generated by h2py from stdin\n')
00066             process(sys.stdin, sys.stdout)
00067         else:
00068             fp = open(filename, 'r')
00069             outfile = os.path.basename(filename)
00070             i = outfile.rfind('.')
00071             if i > 0: outfile = outfile[:i]
00072             modname = outfile.upper()
00073             outfile = modname + '.py'
00074             outfp = open(outfile, 'w')
00075             outfp.write('# Generated by h2py from %s\n' % filename)
00076             filedict = {}
00077             for dir in searchdirs:
00078                 if filename[:len(dir)] == dir:
00079                     filedict[filename[len(dir)+1:]] = None  # no '/' trailing
00080                     importable[filename[len(dir)+1:]] = modname
00081                     break
00082             process(fp, outfp)
00083             outfp.close()
00084             fp.close()

Here is the call graph for this function:

Here is the caller graph for this function:

def h2py.process (   fp,
  outfp,
  env = {} 
)

Definition at line 105 of file h2py.py.

00105 
00106 def process(fp, outfp, env = {}):
00107     lineno = 0
00108     while 1:
00109         line = fp.readline()
00110         if not line: break
00111         lineno = lineno + 1
00112         match = p_define.match(line)
00113         if match:
00114             # gobble up continuation lines
00115             while line[-2:] == '\\\n':
00116                 nextline = fp.readline()
00117                 if not nextline: break
00118                 lineno = lineno + 1
00119                 line = line + nextline
00120             name = match.group(1)
00121             body = line[match.end():]
00122             body = pytify(body)
00123             ok = 0
00124             stmt = '%s = %s\n' % (name, body.strip())
00125             try:
00126                 exec(stmt, env)
00127             except:
00128                 sys.stderr.write('Skipping: %s' % stmt)
00129             else:
00130                 outfp.write(stmt)
00131         match = p_macro.match(line)
00132         if match:
00133             macro, arg = match.group(1, 2)
00134             body = line[match.end():]
00135             body = pytify(body)
00136             stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
00137             try:
00138                 exec(stmt, env)
00139             except:
00140                 sys.stderr.write('Skipping: %s' % stmt)
00141             else:
00142                 outfp.write(stmt)
00143         match = p_include.match(line)
00144         if match:
00145             regs = match.regs
00146             a, b = regs[1]
00147             filename = line[a:b]
00148             if filename in importable:
00149                 outfp.write('from %s import *\n' % importable[filename])
00150             elif filename not in filedict:
00151                 filedict[filename] = None
00152                 inclfp = None
00153                 for dir in searchdirs:
00154                     try:
00155                         inclfp = open(dir + '/' + filename)
00156                         break
00157                     except IOError:
00158                         pass
00159                 if inclfp:
00160                     outfp.write(
00161                             '\n# Included from %s\n' % filename)
00162                     process(inclfp, outfp, env)
00163                 else:
00164                     sys.stderr.write('Warning - could not find file %s\n' %
00165                                      filename)

Here is the call graph for this function:

Here is the caller graph for this function:

def h2py.pytify (   body)

Definition at line 85 of file h2py.py.

00085 
00086 def pytify(body):
00087     # replace ignored patterns by spaces
00088     for p in ignores:
00089         body = p.sub(' ', body)
00090     # replace char literals by ord(...)
00091     body = p_char.sub("ord('\\1')", body)
00092     # Compute negative hexadecimal constants
00093     start = 0
00094     UMAX = 2*(sys.maxsize+1)
00095     while 1:
00096         m = p_hex.search(body, start)
00097         if not m: break
00098         s,e = m.span()
00099         val = int(body[slice(*m.span(1))], 16)
00100         if val > sys.maxsize:
00101             val -= UMAX
00102             body = body[:s] + "(" + str(val) + ")" + body[e:]
00103         start = s + 1
00104     return body

Here is the caller graph for this function:


Variable Documentation

Definition at line 43 of file h2py.py.

Definition at line 37 of file h2py.py.

Definition at line 44 of file h2py.py.

tuple h2py.p_char = re.compile(r"'(\\.[^\\]*|[^\\])'")

Definition at line 39 of file h2py.py.

tuple h2py.p_comment = re.compile(r'/\*([^*]+|\*+[^/])*(\*+/)?')

Definition at line 34 of file h2py.py.

tuple h2py.p_cpp_comment = re.compile('//.*')

Definition at line 35 of file h2py.py.

tuple h2py.p_define = re.compile('^[\t ]*#[\t ]*define[\t ]+([a-zA-Z0-9_]+)[\t ]+')

Definition at line 26 of file h2py.py.

tuple h2py.p_hex = re.compile(r"0x([0-9a-fA-F]+)L?")

Definition at line 41 of file h2py.py.

tuple h2py.p_include = re.compile('^[\t ]*#[\t ]*include[\t ]+<([a-zA-Z0-9_/\.]+)')

Definition at line 32 of file h2py.py.

tuple h2py.p_macro
Initial value:
00001 re.compile(
00002   '^[\t ]*#[\t ]*define[\t ]+'
00003   '([a-zA-Z0-9_]+)\(([_a-zA-Z][_a-zA-Z0-9]*)\)[\t ]+')

Definition at line 28 of file h2py.py.

list h2py.searchdirs = os.environ['include']

Definition at line 47 of file h2py.py.