def process(source_path, dest_path): print 'updating the PHP files.' for file in GlobDirectoryWalker(source_path, "*.py*"): # print 'file',file if file.endswith('.pyc'): # skip the .pyc files continue (source_filepath, source_filename) = os.path.split(file) (source_shortname, source_extension) = os.path.splitext(source_filename) if (source_shortname.startswith('._') or source_shortname.startswith('.')): continue if file.endswith('.py'): unindented_source = py2php.get_source(compiler.parseFile(file)) phpcode = py2php.indent_source(py2php.add_semicolons(unindented_source)) elif file.endswith('.pyp'): # .pyp file reg = re.compile('(<\?pyp.*?\?>)', re.S) # trouver les <?pyp .. ?> matched = reg.split(open(file).read()) phpcode = '' for match in matched: if match.startswith('<?pyp') and match.endswith('?>'): pypCode = match[5:-2] unindented_source = py2php.get_source(compiler.parse(pypCode)) phpcode += py2php.indent_source(py2php.add_semicolons(unindented_source)) else: phpcode += match directories = source_filepath[len(sys.argv[1]):] # create the subdirectories : try : os.makedirs(dest_path + '/' + directories) except OSError: pass if len(directories) > 0: dir_file = directories + '/' + source_shortname else: dir_file = source_shortname phpfile = open(dest_path +'/'+ dir_file + '.php', 'w') phpfile.write( phpcode ) # write the converted code phpfile.close()
# test import py2php import compiler #unindented_source = py2php.get_source(compiler.parseFile('source.py')) py2php.PHPVERSION = 4 unindented_source = py2php.get_source(compiler.parse(''' var = obj.method().method2()''')) print py2php.indent_source(py2php.add_semicolons(unindented_source))
def parsepyp(pypsource): unindented_source = py2php.get_source(compiler.parse(pypsource)) return py2php.indent_source(py2php.add_semicolons(unindented_source))