Example #1
0
def saveJython(func=None,libname=None):
    """ This function saves history of interactive session to IDV Jython Library.
    When supplied by a defined class/function argument saves the code relavent to that class/function only
    When supplied by second string argument it creates library from that name or write to exisiting library by that
    name, only if it is editable by current user."""
    try:
       from org.python.core import PyFunction
       from org.python.core import PyClass
       from ucar.unidata.util import IOUtil
       import os
       import readline
       from random import randint

       pythonDir = IOUtil.joinDir(idv.getStore().getUserDirectory().toString(),"python")

       if (libname == None):
          libname="temp_"+str(randint(1000,9999))

       fullname = IOUtil.cleanFileName(libname)

       if not fullname.endswith(".py"):
          fullname = fullname + ".py"

       fullname = IOUtil.joinDir(pythonDir,fullname) # this takes care of type of slash based on os

       if os.path.exists(str(fullname)):
          raise Exception("A file with library name "+str(libname)+" already exists")

       if func==None:
           readline.write_history_file(str(fullname))
       elif isinstance(func,PyFunction) or isinstance(func,PyClass): #can get rid of this else if pass is used above
           readline.write_history_file("temp.txt")
           funcname=func.__name__
           ## ad-hoc matching use regex for robust matching
           if isinstance(func,PyFunction):
               funcname="def "+funcname+"("
           elif isinstance(func,PyClass):
               funcname="class "+funcname+"("
           fread=open("temp.txt").read()
           fnameindx=fread.rindex(funcname)
           #fnameindx=fread[:fnameindx].rindex('def')
           prevIndt=None
           functxt=[]
           for ln,line in enumerate(fread[fnameindx:].splitlines()):
               currIndt=len(line)-len(line.lstrip())
               if prevIndt<=currIndt or prevIndt==None:
                   functxt.append(line)
               else:
                   break
               prevIndt=currIndt
           f=open(str(fullname),"w")
           f.write('\n'.join(functxt))
           f.close()
       else:
           raise Exception("Unknown Error saving to IDV Jython library")
    except Exception as exc:
       return "Could not create a IDV Jython library: ",exc
Example #2
0
def saveJython(func=None, libname=None):
    """ This function saves history of interactive session to IDV Jython Library.
    When supplied by a defined class/function argument saves the code relavent to that class/function only
    When supplied by second string argument it creates library from that name or write to exisiting library by that
    name, only if it is editable by current user."""
    try:
        from org.python.core import PyFunction
        from org.python.core import PyClass
        from ucar.unidata.util import IOUtil
        import os
        import readline
        from random import randint

        pythonDir = IOUtil.joinDir(
            idv.getStore().getUserDirectory().toString(), "python")

        if (libname == None):
            libname = "temp_" + str(randint(1000, 9999))

        fullname = IOUtil.cleanFileName(libname)

        if not fullname.endswith(".py"):
            fullname = fullname + ".py"

        fullname = IOUtil.joinDir(
            pythonDir,
            fullname)  # this takes care of type of slash based on os

        if os.path.exists(str(fullname)):
            raise Exception("A file with library name " + str(libname) +
                            " already exists")

        if func == None:
            readline.write_history_file(str(fullname))
        elif isinstance(func, PyFunction) or isinstance(
                func,
                PyClass):  #can get rid of this else if pass is used above
            readline.write_history_file("temp.txt")
            funcname = func.__name__
            ## ad-hoc matching use regex for robust matching
            if isinstance(func, PyFunction):
                funcname = "def " + funcname + "("
            elif isinstance(func, PyClass):
                funcname = "class " + funcname + "("
            fread = open("temp.txt").read()
            fnameindx = fread.rindex(funcname)
            #fnameindx=fread[:fnameindx].rindex('def')
            prevIndt = None
            functxt = []
            for ln, line in enumerate(fread[fnameindx:].splitlines()):
                currIndt = len(line) - len(line.lstrip())
                if prevIndt <= currIndt or prevIndt == None:
                    functxt.append(line)
                else:
                    break
                prevIndt = currIndt
            f = open(str(fullname), "w")
            f.write('\n'.join(functxt))
            f.close()
        else:
            raise Exception("Unknown Error saving to IDV Jython library")
    except Exception as exc:
        return "Could not create a IDV Jython library: ", exc