-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
206 lines (173 loc) · 5.54 KB
/
main.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import os,argparse,settings,string,time,queue
from auditor import *
from os import path
from queue import Queue
from log import log
def parse_args():
parser = argparse.ArgumentParser(description="A program to organise your file system.")
parser.add_argument("-c","--config",nargs=1,help="Location of config file.")
parser.add_argument("-v",action='store_true',help="Verbose output.")
parser.add_argument("-vv",action='store_true',help="Debug output.")
args=parser.parse_args()
if(args.v):
settings.VERBOSITY=1
if(args.vv):
settings.VERBOSITY=2
if(args.config != None):
settings.USER_CONFIG_PATH=args.config
log(2,"Config path set to :"+args.config)
def scan(allowed,disallowed,iNoteAdd = True):
global fScanQ
global ini
pathlist = []
pathlist.extend(allowed)
for p in pathlist:
p = path.abspath(p)
if(not p in disallowed and not path.basename(p).startswith('.')):
if(iNoteAdd):
ini.startWatch(p,recDir=False)
cont = os.listdir(p)
#print(cont)
cont = [path.join(p,k) for k in cont]
for k in cont:
if path.isdir(k):
#print(k)
pathlist.extend([k])
else:
#print(k)
f = fData.get(k)
if(f==None or f.last_scanned < os.stat(k).st_ctime):
fScanQ.add(k,"attrib_update")
'''Parse any command line arguments.'''
parse_args()
log(1,"Parsed command line arguments.")
'''Init the config parser. Then load the default and current configs.'''
settings.load_config()
log(1,"Config data loaded.")
pm = plugin_manager.PluginManager(settings.PLUGIN_DIRECTORY,settings.CACHE_DIRECTORY)
log(1,"Plugin manager loaded.")
ini = inotify_interface.INotifyInterface(100)
log(1,"iNotify interface initialised.")
fData = file_data_tree.FileDataTree()
fScanQ = file_scan_queue.FileScanQueue()
fScan = file_scanner.FileScanner(fScanQ,fData,pm)
evHan = event_handler.EventHandler(fScanQ,fData)
log(1,"File data chain loaded.")
ini.setHandler(evHan.process)
pm.loadAll()
log(1,"Plugins loaded.")
fData.load(settings.TREE_LOC)
log(1,"Tree data loaded from file.")
scan(settings.ALLOWED_PATHS,settings.DISALLOWED_PATHS)
fScan.scan()
log(1,"File data updated for edits since last load.")
def inote_scan(a):
print("iNotify Scanning...")
ini.scan()
print("iNotify Scan Complete.")
inote_scan.desc = "Forces an iNotify update."
def fscan_scan(a):
print("File Scanning..")
fScan.scan()
print("File Scan Complete.")
fscan_scan.desc = "Forces a file scan queue update."
def k_near(a):
try:
i = a.rindex(" ")
f = a[:i]
k = a[i+1:]
k = int(k)
except ValueError:
print("USAGE: knear path k")
if(path.isfile(f)):
print("Scanning.")
print(k_nearest_neighbour.k_nearest_neighbour(f,fData,pm,k))
else:
print("USAGE: knear path k")
k_near.desc = "Performs a k-nearest-neighbour scan on a file."
def info(a):
f = fData.get(a)
if(f):
print("Name: %s"%f.name)
print("Last Scanned: %s"%str(f.last_scanned))
print("Type: %s"%f.type)
if(f.type == "folder"):
print("Children: %s"%str(f.children))
else:
print("Attributes: %s"%str(f.attributes))
else:
print("USAGE: info path")
info.desc = "Prints data on a file."
def reload_plugins(a):
print("Unloading existing plugins.")
pm.deinit()
print("Updating directories.")
pm.__init__(settings.PLUGIN_DIRECTORY,settings.CACHE_DIRECTORY)
print("Loading new plugins.")
pm.loadAll()
reload_plugins.desc = "Updates plugin related directories and reloads all plugins."
def change_plugin_dir(a):
if(path.isdir(a)):
print("Plugin directory changed to: %s"%a)
settings.PLUGIN_DIRECTORY = a
else:
print("USAGE: chpdir path")
change_plugin_dir.desc = "Changes the plugin directories location."
def change_cache_dir(a):
if(path.isdir(a)):
print("Cache directory changed to: %s"%a)
settings.CACHE_DIRECTORY = a
else:
print("USAGE: chcdir path")
change_cache_dir.desc = "Changes the plugin cache directories location."
def help_print(a):
for k in cmds:
print("%s: %s"%(k,cmds[k].desc))
help_print.desc = "Prints out this help message."
Q = Queue()
class Task():
def __init__(self,fn,a,rep=False,rep_i=0):
self.fn = fn
self.a = a
self.rep = rep
self.rep_i = rep_i
self.rep_c = rep_i
def run(self):
if self.a != None:
self.fn(self.a)
else:
self.fn()
f = lambda x:lambda a:Q.put(Task(x,a))
cmds = {
'help': f(help_print),
'iscan': f(inote_scan),
'fscan': f(fscan_scan),
'knear' : f(k_near),
'info' : f(info),
'chpdir': f(change_plugin_dir),
'chcdir': f(change_cache_dir),
'reload_plugins': f(reload_plugins)
}
tcl = cmdproc.ThreadedCmdLoop(cmds)
Q.put(Task(fScan.scan,None,True,3000))#Scan every 5m
Q.put(Task(ini.scan,None,True,300))#Scan every 30s
tcl.start()
while tcl.gather_live() :
try:
t = Q.get(block=False)
if t.rep_c > 0 :
t.rep_c -= 1
Q.put(t)
else:
t.run()
if t.rep :
t.rep_c = t.rep_i
Q.put(t)
except queue.Empty:
pass
tcl.run_commands(n=10)
time.sleep(0.1)
fData.save(settings.TREE_LOC)
log(1,"File tree saved.")
settings.store_config()
log(1,"Config saved.")