Beispiel #1
0
 def __init__(self,filename,indent=""):
     # indent is used by Include() ???
     output = cStringIO.StringIO()
     self.cur_row = -1
     self.prev_tok = None
     transform_script.transform(open(filename),output,
         self.translate_func)
     self.output = cStringIO.StringIO()
     output.seek(0)
     for line in output.readlines():
         self.output.write(indent+line)
Beispiel #2
0
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
Beispiel #3
0
def diag2to3(top,dirpath,file_name,out):

    name = os.path.join(dirpath,file_name)
    py_code = cStringIO.StringIO()
    ext = os.path.splitext(name)[1]
    if ext in [".py",".ks"]:
        src = open(name)
    elif ext == ".pih":
        import PythonInsideHTML
        pih = PythonInsideHTML.PIH(name)
        src = pih.output
        src.seek(0)
    elif ext == ".hip":
        import HIP
        pih = HIP.HIP(name)
        src = pih.output
        src.seek(0)
    else:
        return

    # transform Python script and get function names
    result = transform_script.transform(src,py_code,trans_import)
    if result.modules:
        out.write("File %s\n" %name[len(top):])
        out.write("1. import of user-defined modules\n")
        for module in result.modules:
            out.write(module.rstrip()+"\n")
        out.write("\n")
Beispiel #4
0
    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
Beispiel #5
0
def _iterDirectory(oldActions, propnames):
	'''
	Iterate the specified application directories and store the new web actions to
	database, these web actions are functions supplied by '.ks' files .

	Steps:
	1 Iterate directories specified in config file 'config.py',
	  filtere all the '.ks' file and functions in '.ks' files whose has prefix string
	  'page' or 'index';
	2 Then the '.ks' files' names list will be constructed into a tree;
	3 Flatten the tree and check each node should has a serial br contrasting with
   	  the old actions which is a argument transfered to this function.
	  If the node has serial, then add it to its 'serial' attribute, if the node
	  has no serial, that means it's a new created action, set the node's serial
	  attribute to be None;
	4 Iterate the flatten tree again and filter the nodes that have no serial attribute,
	  create serial for each no serial node;
	5 Add those new created actions to database .

	Parameters:
	    oldActions - the properties' values of items of 'webaction' in database
	    actionIndex - the index of 'action' in each item of oldActions
	'''
	# config data object, 'CONFIG' is a variable trnasfered to this module file
	config = Import( '/'.join((RELPATH, 'config.py')), rootdir=CONFIG.root_dir)
	root, ksFiles = CONFIG.root_dir, []
	for appdir in config.getData('appdirs'):
		path = os.sep.join((root, appdir))
		if not os.path.isdir(path):
			continue

		# get all the '.ks' files under this directory
		toSet = []
		# Parameters:
		# path- the directory to be scaning
		# _filterKs- the function to handle the file name
		# toSet- a list to hold the result of _filterKs function
		os.path.walk(path, _filterKs, toSet)
		toSet.sort()
		ksFiles.extend(toSet)
	
	# construct a nodes list by the ks filenames list
	nodesList = []
	for ks in ksFiles:
		fnSplit = ks.split('/') 
		app = fnSplit[-2] 	
		if app not in nodesList:
			nodesList.append(app)
		
		page = '/'.join(fnSplit[-2:])
		nodesList.append(page)
		fileObj= cStringIO.StringIO(open(ks).read())
		out = cStringIO.StringIO()
		# get functions' names in the ks file
		fns = transform(fileObj, out, func=translate_func, debug=False).functions
		fns.sort()
		pages = ['/'.join((page, function))
			  for function in fns if 'page' in function or 'index' in function ]
		nodesList.extend(pages)
	
	# construct a tree from the nodes list
	# by Class Node definition each node has three important attributes,
	# 'id', 'prent', 'children'
	tree = treeHandler.TreeHandler(nodesList, lambda name : name, lambda name: '/'.join(name.split('/')[:-1]))
	flattenTree = tree.flatten()

	# add 'serial' attribute to each node	
	actionIndex, serialIndex = [propnames.index(prop) for prop in ('actionpage', 'serial')]	
	for node in flattenTree:
		node.serial = None
		action = node.id
		isExisted = None
		for oldAction in oldActions :
			if oldAction[actionIndex] == action :
				node.serial = oldAction[serialIndex]
				oldActions.remove(oldAction)
				break
	
		
	newActions = []	
	for node in flattenTree[1:]:
		if not node.serial:
			node.serial = _createSerial(node)
			newActions.append([node.id, node.serial])

	return newActions, oldActions 
Beispiel #6
0
        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()