Skip to content

Commit a53d431

Browse files
committed
no period in extensions
1 parent cdeda65 commit a53d431

File tree

1 file changed

+45
-22
lines changed

1 file changed

+45
-22
lines changed

classifier/classifier.py

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,15 @@ def __init__(self):
6060
self.parser.add_argument("-t", "--types", action='store_true',
6161
help="Show the current list of types and formats")
6262

63+
self.parser.add_argument("-rst", "--reset", action='store_true',
64+
help="Reset the default Config file")
65+
66+
"""
6367
self.parser.add_argument("-r", "--recursive", action='store_true',
6468
help="Recursively search your source directory. " +
6569
"WARNING: Ensure you use the correct path as this " +
6670
"WILL move all files from your selected types.")
71+
"""
6772

6873
self.parser.add_argument("-st", "--specific-types", type=str, nargs='+',
6974
help="Move all file extensions, given in the args list, " +
@@ -91,27 +96,33 @@ def __init__(self):
9196
self.checkconfig()
9297
self.run()
9398

99+
def create_default_config(self):
100+
with open(CONFIG, "w") as conffile:
101+
conffile.write("IGNORE: part, desktop\n" +
102+
"Music: mp3, aac, flac, ogg, wma, m4a, aiff, wav, amr\n" +
103+
"Videos: flv, ogv, avi, mp4, mpg, mpeg, 3gp, mkv, ts, webm, vob, wmv\n" +
104+
"Pictures: png, jpeg, gif, jpg, bmp, svg, webp, psd, tiff\n" +
105+
"Archives: rar, zip, 7z, gz, bz2, tar, dmg, tgz, xz, iso, cpio\n" +
106+
"Documents: txt, pdf, doc, docx, odf, xls, xlsv, xlsx, " +
107+
"ppt, pptx, ppsx, odp, odt, ods, md, json, csv\n" +
108+
"Books: mobi, epub, chm\n" +
109+
"DEBPackages: deb\n" +
110+
"Programs: exe, msi\n" +
111+
"RPMPackages: rpm")
112+
print("CONFIG file created at: "+CONFIG)
113+
94114
def checkconfig(self):
95115
""" create a default config if not available """
96116
if not os.path.isdir(os.path.dirname(CONFIG)):
97117
os.makedirs(os.path.dirname(CONFIG))
98118
if not os.path.isfile(CONFIG):
99-
conffile = open(CONFIG, "w")
100-
conffile.write("IGNORE:.part,.desktop\n" +
101-
"Music:.mp3,.aac,.flac,.ogg,.wma,.m4a,.aiff,.wav,.amr\n" +
102-
"Videos:.flv,.ogv,.avi,.mp4,.mpg,.mpeg,.3gp,.mkv,.ts,.webm,.vob,.wmv\n" +
103-
"Pictures:.png,.jpeg,.gif,.jpg,.bmp,.svg,.webp,.psd,.tiff\n" +
104-
"Archives:.rar,.zip,.7z,.gz,.bz2,.tar,.dmg,.tgz,.xz,.iso,.cpio\n" +
105-
"Documents:.txt,.pdf,.doc,.docx,.odf,.xls,.xlsv,.xlsx," +
106-
".ppt,.pptx,.ppsx,.odp,.odt,.ods,.md,.json,.csv\n" +
107-
"Books:.mobi,.epub,.chm\n" +
108-
"DEBPackages:.deb\n" +
109-
"Programs:.exe,.msi\n" +
110-
"RPMPackages:.rpm")
111-
conffile.close()
119+
self.create_default_config()
120+
112121
with open(CONFIG, 'r') as file:
113122
for items in file:
114-
(key, val) = items.replace('\n', '').split(':')
123+
spl = items.replace('\n', '').split(':')
124+
key = spl[0].replace(" ","")
125+
val = spl[1].replace(" ","")
115126
self.formats[key] = val
116127
return
117128

@@ -120,7 +131,7 @@ def moveto(self, filename, from_folder, to_folder):
120131
to_file = os.path.join(to_folder, filename)
121132
# to move only files, not folders
122133
if not to_file == from_file:
123-
print(' moving:', to_file)
134+
print('moved: ' + str(to_file))
124135
if os.path.isfile(from_file):
125136
if not os.path.exists(to_folder):
126137
os.makedirs(to_folder)
@@ -133,7 +144,7 @@ def classify(self, formats, output, directory):
133144
# set up a config per folder
134145
if not file == DIRCONFFILE and os.path.isfile(os.path.join(directory, file)):
135146
filename, file_ext = os.path.splitext(file)
136-
file_ext = file_ext.lower()
147+
file_ext = file_ext.lower().replace('.', '')
137148
if 'IGNORE' in self.formats:
138149
for ignored in self.formats['IGNORE'].replace('\n', '').split(','):
139150
if file_ext == ignored:
@@ -152,9 +163,10 @@ def classify(self, formats, output, directory):
152163
self.moveto(file, directory, folder)
153164
except Exception as e:
154165
print('Cannot move file - {} - {}'.format(file, str(e)))
166+
"""
155167
elif os.path.isdir(os.path.join(directory, file)) and self.args.recursive:
156168
self.classify(self.formats, output, os.path.join(directory, file))
157-
169+
"""
158170
return
159171

160172
def classify_by_date(self, date_format, output, directory):
@@ -169,7 +181,6 @@ def classify_by_date(self, date_format, output, directory):
169181
folder = os.path.join(output, folder)
170182
self.moveto(file, directory, folder)
171183

172-
print("Done!")
173184
return
174185

175186
def _format_text_arg(self, arg):
@@ -186,13 +197,15 @@ def _format_arg(self, arg):
186197
def run(self):
187198
if self.args.version:
188199
# Show version information and quit
189-
print(VERSION + '\n' + os.path.realpath(__file__))
200+
print(VERSION)
190201
return False
202+
191203
if self.args.types:
192204
# Show file format information then quit
193205
for key, value in self.formats.items():
194-
print(key, '\n', value)
206+
print(key + ': '+ value)
195207
return False
208+
196209
if self.args.edittypes:
197210
if PLATFORM == 'darwin':
198211
subprocess.call(('open', CONFIG))
@@ -201,6 +214,11 @@ def run(self):
201214
elif PLATFORM == 'linux' or PLATFORM == 'linux2' or OS == 'posix':
202215
subprocess.Popen(['xdg-open', CONFIG])
203216
return False
217+
218+
if self.args.reset:
219+
self.create_default_config()
220+
return
221+
204222
if bool(self.args.specific_folder) ^ bool(self.args.specific_types):
205223
print(
206224
'Specific Folder and Specific Types need to be specified together')
@@ -231,6 +249,12 @@ def run(self):
231249
elif os.path.isfile(os.path.join(os.getcwd(), DIRCONFFILE)):
232250
self.dirconf = os.path.join(os.getcwd(), DIRCONFFILE)
233251

252+
if self.args.dateformat:
253+
if not self.args.date:
254+
print(
255+
'Dateformat -df must be given alongwith date -dt option')
256+
sys.exit()
257+
234258
if self.args.date:
235259
if self.args.dateformat:
236260
self.classify_by_date(self.args.dateformat, output, directory)
@@ -256,8 +280,7 @@ def run(self):
256280
return False
257281
else:
258282
print("\nScanning Folder: " + directory +
259-
"\nFor: " + str(self.formats.items()) +
260-
'\nRecursive: ' + str(self.args.recursive))
283+
"\nFor: " + str(self.formats.items()))
261284
self.classify(self.formats, output, directory)
262285

263286
print("Done!\n")

0 commit comments

Comments
 (0)