-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.py
141 lines (108 loc) · 3.81 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import asyncio
import logging
import os
import sys
import threading
import click
import Filerize
from config import globalvar as gv
from config.config import Config
from filetools.FileToText import FileToText as fileparser
from filetools.fwatchdog import ListenForFiles
_common_options = [
click.argument("directory", type=str),
]
_config_options = [
click.option("--add", type=str, default=None),
click.option("--delete", type=str, default=None),
click.option("--show", default=None, count=True)
]
def _add_options(opts: list):
def wrap(func):
for opt in opts:
func = opt(func)
return func
return wrap
@click.group()
@click.option("-v", "--verbose", default=None, count=True)
def cli(verbose):
"""FilerizeCLI @ TeamLEAN - A Document Sorter using GPT-3"""
if verbose:
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
@cli.command("start", help="run config (on first run), sort, and daemon in order")
@_add_options(_common_options)
@click.help_option("-h", "--help")
@click.pass_context
def cli_start(ctx: click.Context, **args):
check_directory(directory=args['directory'])
folder = Filerize.init(args['directory'])
Filerize.sort(folder=folder)
Filerize.listen(folder=folder)
# run continous behaviour
@cli.command("sort", help="sort all folders in given directory into given labels")
@_add_options(_common_options)
@click.help_option("-h", "--help")
@click.pass_context
def cli_sort(ctx: click.Context, **args):
check_directory(directory=args['directory'])
folder = Filerize.init(args['directory'])
Filerize.sort(folder=folder)
@cli.command("listen", help="start a file listen daemon for a given directory")
@_add_options(_common_options)
@click.help_option("-h", "--help")
@click.pass_context
def cli_listen(ctx: click.Context, **args):
check_directory(directory=args['directory'])
folder = Filerize.init(args['directory'])
Filerize.listen(folder=folder)
@cli.command("config", help="edit config file")
@_add_options(_config_options)
@click.help_option("-h", "--help")
@click.pass_context
def cli_config(ctx: click.Context, **args):
if all([args["add"], args["delete"], args["show"]]):
print("usage: --add | --delete | --show | (not all)")
return
else:
Config.load(gv.DEFAULT_CONFIG_PATH)
if args['add']:
pth = os.path.abspath(args["add"])
if not os.path.exists(path=pth):
print("Warning: Path does not exist. Making one...")
os.mkdir(pth)
sum = str(input(f"Enter directory summary: "))
if sum == "":
print("Error: no summary. Aborting...")
return
Config.add_label(label=pth, summary=sum)
Config.save()
elif args["delete"]:
pth = os.path.abspath(args["delete"])
stat = Config.delete_label(label=pth)
if stat:
print("Entry Deleted")
else:
print("Error: entry does not exist")
Config.save()
elif args["show"]:
Config.print_config()
# @cli.command("debug", help="debug - not to be in production")
# @click.help_option("-h", "--help")
# @click.pass_context
# def cli_debug(ctx:click.Context, **args):
# Config.load(cfg_path=gv.DEFAULT_CONFIG_PATH)
# label = asyncio.run(Filerize.label_file(r"D:\dev\hacknotts\23\Filerize\testing\untouched\COMP1004-MySQL-setup.docx"))
# print(label)
# pass
# @cli.command("recursive_print", help="debug - not to be in production")
# @click.help_option("-h", "--help")
# @click.pass_context
# def cli_debug(ctx:click.Context, **args):
# asyncio.run(Filerize.sort(path=args['directory']))
# pass
def check_directory(directory: str) -> None:
if not os.path.exists(directory):
print("Invalid directory given. Aborting...")
exit()
if __name__ == "__main__":
cli()