convert to json config

This commit is contained in:
Ville Rantanen
2020-12-28 22:04:59 +02:00
parent 1e57071ad7
commit 9c1fab0d21

40
flit.py
View File

@@ -8,10 +8,11 @@ import random
import shutil
import string
from urllib.parse import quote
import json
DEL_TIME = '.delete_in'
DESCRIPTION = '.description'
CONFIG = '.flit.json'
def parse_opts():
class _HelpAction(argparse._HelpAction):
@@ -143,8 +144,6 @@ def copy_files(cwd, new_name, files):
symlinks = True,
)
def get_description(f):
try:
with open(f) as fp:
@@ -152,9 +151,11 @@ def get_description(f):
except:
return "-"
def get_stats(p):
# TODO: named tuple
try:
config = read_config(p)
if config is None:
with open(os.path.join(p, DEL_TIME), 'rt') as fp:
do_del = int(fp.read(64))
desc = get_description(os.path.join(p, DESCRIPTION))
@@ -164,10 +165,20 @@ def get_stats(p):
del_time = mtime + del_delta
to_del_time = del_time - now
is_due = now > del_time
config = {}
config['description'] = desc
config['delete_time'] = del_time.isoformat()
config['created'] = mtime.isoformat()
else:
desc = config['description']
del_time = datetime.fromisoformat(config['delete_time'])
now = datetime.now()
mtime = datetime.fromisoformat(config['created'])
to_del_time = del_time - now
is_due = now > del_time
write_config(p, config)
return mtime, del_time, to_del_time, is_due, desc
except Exception as e:
print(e)
return (None, None, False, False, "-")
def get_sub_files(p):
@@ -218,6 +229,21 @@ def del_due_folders():
shutil.rmtree(p)
def read_config(p):
try:
with open(os.path.join(p,CONFIG),'rt') as fp:
return json.load(fp)
except:
return None
def write_config(p, config):
with open(os.path.join(p,CONFIG),'wt') as fp:
return json.dump(config, fp, indent=2,sort_keys=True)
if __name__ == '__main__':
opts = parse_opts()
cwd = os.getcwd()