def get_strings(file_name): import python_code src = cStringIO.StringIO(python_code.get_py_code(file_name)[0]) src.seek(0) res = transform_script.transform(src, out = cStringIO.StringIO(), func=get_trans) return res.strings
def parse_script(self): """Parse a Python script : find the functions defined at module level, transform print to PRINT()""" # get Python code from source file cached = False if self.handler.config.cache_dir is not None: elts = self.baseurl.split('/') cache_dir_name = os.path.join(self.handler.config.cache_dir, *elts) cache_file_name = os.path.join(cache_dir_name, os.path.basename(self.name)) if os.path.exists(cache_dir_name): if os.path.exists(cache_file_name): source_mod_time = os.path.getmtime(self.name) cache_mod_time = os.path.getmtime(cache_file_name) if cache_mod_time > source_mod_time: try: cached = True cache_file_obj = open(cache_file_name, "r") src = cache_file_obj.read() funcs,self.py_code = src.split("\n",1) self.functions = eval(funcs) cache_file_obj.close() except: cached = False pass else: try: os.makedirs(cache_dir_name) except: # eg if write mode not set for the folder import traceback traceback.print_exc(file=sys.stderr) if not cached: src,self.line_mapping = python_code.get_py_code(self.name, self.handler.config.output_encoding) src = cStringIO.StringIO(src) py_code = cStringIO.StringIO() # transform Python script and get function names result = transform_script.transform(src,py_code,translate_func) self.functions = result.functions self.py_code = py_code.getvalue()+"\n" # cache: write try: cache_file_obj = open(cache_file_name, "w") cache_file_obj.write(str(self.functions)+"\n") cache_file_obj.write(self.py_code) cache_file_obj.close() except: pass
if state.next_is_func: if not token_string.startswith("_"): state.functions.append(token_string) state.next_is_func = False elif token_string=="print": state.in_print = True res = "PRINT(" elif token_string == "def" and scol==0: state.next_is_func = True elif state.in_print and ((typ == "OP" and token_string == ";") or \ (typ in ["NEWLINE","ENDMARKER","COMMENT"])): res = ")"+token_string state.in_print = False return res,state name = "../webapps/demo/tour/hello.hip" src,line_mapping = python_code.get_py_code(name) src = cStringIO.StringIO(src) src = open("../webapps/demo/tour/hello.hip") py_code = cStringIO.StringIO() # transform Python script and get function names result = transform_script.transform(src,py_code,debug=True) #,translate_func) #functions = result.functions print py_code.getvalue()