pyvix2/get_enum.py
The C-language API depends on a large number of enumerated
constants.
In order to be absolutely sure that the current values are used,
this script extracts the values from the vix.h header.
Overheads
from __future__ import print_function
import datetime
import sys
File Processing
We create an iterator to visit
source stripped of leading and trailing whitespace.
The interesting enum lines are (fortunately) easy to find.
Further, each enumerated name has an explicit
value, saving us from really parsing the source.
def get_enum( source, target ):
"""Get VIX enum values.
:param source: Source file-like object, usually :samp:`open("vix.h","r")`.
:param target: Target file-like object, usually :samp:`open("pyvix2/enum.py","w")`.
"""
print( '"""{2!r} created {0} from {1!r}"""'.format(
datetime.datetime.now(), source.name, target.name ),
file= target )
source_iter= (l.strip() for l in source)
for line in source_iter:
if line.startswith( "enum" ):
for line in source_iter:
# end of enum?
if line.endswith( "};" ): break
# C++ comments?
line, delim, text = line.partition( '//' )
# Block comments?
if line.startswith('/*'):
print( '#', line, file=target )
if '*/' not in line:
for line in source_iter:
print( '#', line, file=target )
if '*/' in line: break
continue
# Finally, the values.
if line.endswith(','):
words= line[:-1].split()
else:
words= line.split()
assert len(words) == 0 or (len(words) == 3 and words[1] == "=")
print( ' '.join(words), file=target )
else:
pass # non-enum lines
print( '# Finished.', file=target )
Main-Import Switch
This allows us to either execute this module or import
the relevant function.
def main():
if sys.platform.startswith("win"):
vix_h= r"C:\Program Files\VMware\VMware VIX\vix.h"
else:
vix_h= "/usr/lib/vmware-vix/lib/vix.h"
with open( vix_h, "r" ) as source:
with open( "pyvix2/enum.py", "w" ) as target:
#with sys.stdout as target:
get_enum(source, target)
print( "get_enum created {1!r}".format( source.name, target.name ) )
if __name__ == "__main__":
main()