Ejemplo n.º 1
0
def module_path(object):
    """full pathname of the current module"""
    if sys.version_info[0] == 3:
        from normpath3 import normpath
    else:
        from normpath import normpath
    global MODULE_PATH
    if "MODULE_PATH" in globals(): return MODULE_PATH
    from sys import path
    from os import getcwd
    from os.path import basename, exists
    from inspect import getmodulename, getfile
    # 'getfile' retreives the source file name name compiled into the .pyc file.
    try:
        pathname = getfile(object)
    except:
        pathname = getfile(lambda x: x)
    ##print("module_path: pathname: %r" % pathname)
    if not exists(pathname):
        # The module might have been compiled on a different machine or in a
        # different directory.
        pathname = pathname.replace("\\", "/")
        filename = basename(pathname)
        ##print("module_path: filename: %r" % filename)
        dirs = [
            dir for dir in [getcwd()] + path if exists(dir + "/" + filename)
        ]
        if len(dirs) == 0: print("pathname of file %r not found" % filename)
        dir = dirs[0] if len(dirs) > 0 else "."
        pathname = dir + "/" + filename
        ##print("module_path: pathname: %r" % pathname)
    pathname = normpath(pathname)
    MODULE_PATH = pathname
    return pathname
Ejemplo n.º 2
0
 def content(self):
     from normpath3 import normpath
     filename = normpath(self.filename)
     from mmap import mmap, ACCESS_READ
     try:
         f = file(filename)
         content = mmap(f.fileno(), 0, access=ACCESS_READ)
     except IOError:
         content = ""
     return content
Ejemplo n.º 3
0
 def content(self):
     from os.path import exists, getsize
     from normpath3 import normpath
     filename = normpath(self.filename)
     if exists(filename):
         size_change = getsize(filename) - len(self.cached_content)
         if size_change > 0:
             ##debug("Logfile: Reading %d bytes" % size_change)
             f = file(filename)
             f.seek(len(self.cached_content))
             self.cached_content += f.read()
         elif size_change < 0:
             ##debug("Logfile: Reading %d bytes" % getsize(filename))
             self.cached_content = file(filename).read()
     else:
         self.cached_content = ""
     return self.cached_content
Ejemplo n.º 4
0
    def log(self, *args, **kwargs):
        """Append to logfile
        time: time in seconds since 1970-01-01 00:00:00 UTC
        """
        from time import time
        from time_string import date_time
        from normpath3 import normpath
        from os.path import exists, dirname
        from os import makedirs

        values = args
        if "time" in kwargs: timestamp = kwargs["time"]
        else: timestamp = time()

        with self.lock:  # Allow only one thread at a time inside this function.
            filename = normpath(self.filename)
            if not exists(dirname(filename)): makedirs(dirname(filename))
            if not exists(filename):
                header = "#" + "\t".join(self.columns) + "\n"
            else:
                header = ""
            fields = [date_time(timestamp)] + [str(v) for v in values]
            line = "\t".join(fields) + "\n"
            file(filename, "ab").write(header + line)
Ejemplo n.º 5
0
 def get_directory(self):
     from normpath3 import normpath
     return normpath(self.__directory__)