Exemplo n.º 1
0
def add(task):
    filename = Path(directory) / "tasks.json"
    encoder = json.JSONEncoder(indent=2)
    tasklist = tasks() + [task]  # Need to read before we open for writing.
    with filename.open(mode='w') as f:
        f.write(encoder.encode(tasklist))
    vcs.commit("Tasklist: add task")
Exemplo n.º 2
0
def remove_record_by_id(ident):
    db = get_database_object()
    item = db.table('files').get(doc_id=int(ident))
    if item == None:
        raise UpdateMissingFile(ident)

    filepath = get_single_record_path(ident, item['Buc_name'])
    if 'Buc_source' in item:
        sourcepath = get_single_record_src_path(ident, item['Buc_source'])
    else:
        sourcepath = None

    print("*** Deleting record " + str(ident) + " which lives at " +
          str(filepath))

    # Now we delete.
    db.table('files').remove(doc_ids=[int(ident)])
    filepath.unlink()
    if not (sourcepath == None):
        sourcepath.unlink()

    # Remove deleted item from recents, if needed
    recent = get_recents()
    if int(ident) in recent:
        recent.pop(recent.index(int(ident)))
    set_recent(recent)

    # Remove deleted item from pinned, if needed
    if get_pinned() == int(ident):
        unset_pinned()

    print("*** Deleted.")
    vcs.commit("dbops: delete record")
    return True
Exemplo n.º 3
0
def add_record(title,
               author,
               tags,
               meat,
               source=None,
               metadata=None,
               delay=False,
               pin=False):
    meatpath = Path(meat)
    srcpath = None

    if (metadata == None):
        # Sort out metadata
        date = datetime.datetime.today()
        metadata = {
            'ts_year': date.year,
            'ts_year2': date.year,
            'ts_month': date.month,
            'ts_month2': date.month,
            'ts_day': date.day,
            'ts_day2': date.day,
            'ts_hour': date.hour,
            'ts_hour2': date.hour,
            'ts_minute': date.minute,
            'ts_minute2': date.minute,
            'ts_second': date.second,
            'ts_second2': date.second,
            'Buc_tags': tags,
            'Buc_author': author,
            'Buc_title': title,
            'Buc_name': meatpath.name
        }
        if not (source == None):
            srcpath = Path(source)
            metadata['Buc_source'] = srcpath.name
    else:
        if not (source == None):
            srcpath = Path(source)
            if str(srcpath) != metadata['Buc_source']:
                raise MetadataMismatch(
                    'Buc_source=\'' + metadata['Buc_source'] +
                    '\' key of metadata does not match source=\'' +
                    str(srcpath) + '\' parameter')

    if delay == True:
        return metadata

    add_file(metadata, False, meatpath, srcpath)
    dbid = write_metadata(metadata)
    if pin:
        set_pinned(dbid)

    metadata['doc_id'] = dbid

    vcs.commit("dbops: add new record")
    return metadata
Exemplo n.º 4
0
def rm(taskids):
    filename = Path(directory) / "tasks.json"
    encoder = json.JSONEncoder(indent=2)
    tasklist = tasks()
    taskids = sorted([int(x) for x in taskids], reverse=True)
    for i in taskids:
        tasklist.pop(i)
    with filename.open(mode='w') as f:
        f.write(encoder.encode(tasklist))
    vcs.commit("Tasklist: rm task")
Exemplo n.º 5
0
def update_record(ident, meat, source=None, pin=False):
    db = get_database_object()
    metatable = db.table('files')
    oldrecord = get_record_by_id(ident)
    if (oldrecord == None):
        raise UpdateMissingFile(ident)
    if (oldrecord['Buc_name'] != Path(meat).name):
        raise UpdateDifferentFile(oldrecord['Buc_name'], str(Path(meat).name))

    add_file(oldrecord, True, meat, source)
    dbid = write_metadata(oldrecord)
    if pin:
        set_pinned(dbid)
    vcs.commit("dbops: update record")
    return True
Exemplo n.º 6
0
                   metavar=('KEY', 'VALUE'),
                   type=str,
                   nargs=2,
                   help='add a default key with given value')
group.add_argument('-r', metavar='KEY', type=str, help='remove a default key')

args = vars(parser.parse_args())

filename = config.get_defaults_file_path()
decoder = json.JSONDecoder()
if filename.exists():
    with filename.open() as f:
        defaults = decoder.decode(f.read())

need_write = False
if args['a'] != None:
    defaults[args['a'][0]] = args['a'][1]
    need_write = True
elif args['r'] != None:
    defaults.pop(args['r'])
    need_write = True
else:
    for key, val in defaults.items():
        print(": " + str(key) + "\t\t" + str(val))

if need_write:
    encoder = json.JSONEncoder(indent=2)
    with filename.open(mode='w') as f:
        f.write(encoder.encode(defaults))
    vcs.commit("Defaults_cmd: change defaults")