convert to json config
This commit is contained in:
42
flit.py
42
flit.py
@@ -8,10 +8,11 @@ import random
|
|||||||
import shutil
|
import shutil
|
||||||
import string
|
import string
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
import json
|
||||||
|
|
||||||
DEL_TIME = '.delete_in'
|
DEL_TIME = '.delete_in'
|
||||||
DESCRIPTION = '.description'
|
DESCRIPTION = '.description'
|
||||||
|
CONFIG = '.flit.json'
|
||||||
|
|
||||||
def parse_opts():
|
def parse_opts():
|
||||||
class _HelpAction(argparse._HelpAction):
|
class _HelpAction(argparse._HelpAction):
|
||||||
@@ -143,8 +144,6 @@ def copy_files(cwd, new_name, files):
|
|||||||
symlinks = True,
|
symlinks = True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_description(f):
|
def get_description(f):
|
||||||
try:
|
try:
|
||||||
with open(f) as fp:
|
with open(f) as fp:
|
||||||
@@ -152,9 +151,11 @@ def get_description(f):
|
|||||||
except:
|
except:
|
||||||
return "-"
|
return "-"
|
||||||
|
|
||||||
|
|
||||||
def get_stats(p):
|
def get_stats(p):
|
||||||
# TODO: named tuple
|
# TODO: named tuple
|
||||||
try:
|
config = read_config(p)
|
||||||
|
if config is None:
|
||||||
with open(os.path.join(p, DEL_TIME), 'rt') as fp:
|
with open(os.path.join(p, DEL_TIME), 'rt') as fp:
|
||||||
do_del = int(fp.read(64))
|
do_del = int(fp.read(64))
|
||||||
desc = get_description(os.path.join(p, DESCRIPTION))
|
desc = get_description(os.path.join(p, DESCRIPTION))
|
||||||
@@ -164,10 +165,20 @@ def get_stats(p):
|
|||||||
del_time = mtime + del_delta
|
del_time = mtime + del_delta
|
||||||
to_del_time = del_time - now
|
to_del_time = del_time - now
|
||||||
is_due = now > del_time
|
is_due = now > del_time
|
||||||
return mtime, del_time, to_del_time, is_due, desc
|
config = {}
|
||||||
except Exception as e:
|
config['description'] = desc
|
||||||
print(e)
|
config['delete_time'] = del_time.isoformat()
|
||||||
return (None, None, False, False, "-")
|
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
|
||||||
|
|
||||||
|
|
||||||
def get_sub_files(p):
|
def get_sub_files(p):
|
||||||
@@ -218,6 +229,21 @@ def del_due_folders():
|
|||||||
shutil.rmtree(p)
|
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__':
|
if __name__ == '__main__':
|
||||||
opts = parse_opts()
|
opts = parse_opts()
|
||||||
cwd = os.getcwd()
|
cwd = os.getcwd()
|
||||||
|
|||||||
Reference in New Issue
Block a user