コード例 #1
0
ファイル: IO.py プロジェクト: peterrenshaw/zerotasks
def write(filepathname, data, is_json=True, save_bit='w'):
    """write task data to a file"""
    if filepathname:
        fp = os.path.dirname(filepathname)
        if not os.path.isdir(fp):
            tools.DISCOM("IO.write", "mkdir <{}>".format(fp))
            os.makedirs(fp)
        try:
            f = open(filepathname, save_bit)
            tools.DISCOM("IO.write",
                         "open <{}> to <{}>".format(filepathname, save_bit))

            # write as json or raw file
            if is_json:
                task = "{}\n".format(
                    json.dumps(data, sort_keys=True, separators=(',', ': ')))
            else:
                task = "{}".format(data)

            f.flush()
            f.write(task)
            f.close()
            tools.DISCOM("IO.write",
                         "written to file {}\n\{}".format(filepathname, data))
        except IOError as e:
            f = None
            tools.DISERR("IO.write",
                         "Error writing file <{}>".format(filepathname))
            return False
        else:
            pass
        return True
コード例 #2
0
ファイル: IO.py プロジェクト: peterrenshaw/zerotasks
def read(filepathname):
    """read a file, return content"""
    data = []
    tools.DISCOM("IO.read", "fpn=<{}>".format(filepathname))
    if os.path.isfile(filepathname):
        try:
            for line in open(filepathname, 'r'):
                tools.DISCOM("IO.read", "line=<{}>".format(line))
                task = json.loads(line)
                data.append(task)
                #data.insert(0, task)
        except IOError as e:
            tools.DISERR("IO.read",
                         "error reading file <{}>".format(filepathname))
            return data
        else:
            pass
        if data:
            # multiple or single lines read?
            if len(data) == 1:
                return data[0]  # single line
            else:
                return data  # multiple lines
        else:
            return data
    else:
        return data
コード例 #3
0
ファイル: IO.py プロジェクト: peterrenshaw/zerotasks
def get_filepathname(filepath=config.TASKS_ARCHIVE, ext=config.FN_EXT):
    """extract all filepath names from the archive"""
    paths = []
    fp = "{}/*".format(filepath)
    filepaths = glob.glob(fp)

    tools.DISCOM("IO.get_filepathname", "fp=<{}>".format(fp))
    tools.DISCOM("IO.get_filepathname", "filepaths=<{}>".format(filepaths))

    #-------
    # explanation:
    #
    # the code below looks into the TASKS_ARCHIVE directory
    # and looks for multiple YYYY/ directories for child months
    # and days, slurping up all the files with the FN_EXT extension
    # stored as a list of filenamepaths.
    #
    #     TASKS_ARCHIVE/
    #         2016/
    #             DEC/
    #                 09/
    #                     zt-fix-forth-field.tsk
    #         YYYY/
    #             MMM/
    #                 NN/
    #                     zt-filename.FN_EXT
    #-------

    # extract filepaths by glob
    for dirs in filepaths:
        # TASKS_ARCHIVE dir
        directories = "{}/*".format(dirs)
        # YYYY dirs
        for d in glob.glob(directories):
            # filenames
            for fp in glob.glob("{}/*".format(d)):
                if ext:
                    # only grab files with extension
                    fn = "{}/*.{}".format(fp, ext)
                else:
                    # no extension/junk data
                    fn = "{}/*".format(fp)

                tools.DISCOM("IO.get_filepathname", "fn=<{}>".format(fn))

                # extract filenames
                fpn = os.path.join(fp, fn)
                tools.DISCOM("IO.get_filepathname", "fpn=<{}>".format(fpn))
                for tfn in glob.glob(fpn):
                    if os.path.isfile(tfn):
                        paths.insert(0, tfn)

    tools.DISCOM("IO.get_filepathname", "paths=<{}>".format(paths))
    return paths
コード例 #4
0
ファイル: IO.py プロジェクト: peterrenshaw/zerotasks
def write_tasks(fpn, data, save_bit='a'):
    """write all the tasks to a directory"""
    for task in data:
        if not fpn:
            fpn = path(task)

        tools.DISCOM("IO.write_tasks", "fpn={}".format(fpn))
        tools.DISCOM("IO.write_tasks", "task={}".format(task))
        if not write(fpn, task, is_json=True, save_bit=save_bit):
            tools.DISERR(
                "IO.write_tasks",
                "Error cannot save task to <{}>".format(options.output))
            return False

    return True
コード例 #5
0
ファイル: IO.py プロジェクト: peterrenshaw/zerotasks
 def build(self, path=""):
     """create a filepath from home and relative filepaths"""
     if path:  # update relative path
         self.p = path
         tools.DISCOM("IO.Filepath.build", \
                      "fp_home=<{}>".format(self.fp_home))
         tools.DISCOM("IO.Filepath.build", \
                      "bp=<{}>".format(self.bp))
         tools.DISCOM("IO.Filepath.build", \
                      "rp=<{}>".format(self.rp))
         tools.DISCOM("IO.Filepath.build", \
                      "path=<{}>".format(path))
         self.fp = os.path.join(self.fp_home, self.bp, self.rp, path)
     else:  # use existing relative path
         self.fp = os.path.join(self.fp_home, self.bp, self.rp)
     return True
コード例 #6
0
ファイル: IO.py プロジェクト: peterrenshaw/zerotasks
def get_path(task, basepath=config.TASKS_ARCHIVE):
    """build filepath from task data"""
    if 'name' in task:
        name = task['name']
        epoch = task['created']

        fp = Filepath().get(basepath)
        ymd = Path().get(epoch)
        fn = Filename().getlo(name)
        fpn = os.path.join(fp, ymd, fn)
        tools.DISCOM("fn = {}".format(fn))
        tools.DISCOM("ymd = {}".format(ymd))
        tools.DISCOM("fp = {}".format(fp))
        tools.DISCOM("fpn = {}".format(fpn))

        return fpn
    else:
        return False