-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasort.py
130 lines (113 loc) · 5.51 KB
/
datasort.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
#!/usr/bin/env python3
Author = 'Clayton McCray'
Version = 2.1
import os
from shutil import copy2
import sys
###############################################################################################
# TODO:
# URGENT:
# See TODO in flags()
# Add a flag for interaction i.e. confirm every copy
# Add a flag to exclude some extensions
# Add a flag to ask what to do on particular extensions
###############################################################################################
extensions = []
unsorted = []
def read_input():
try:
if sys.argv[1][0] == '-':
base, target = flags(sys.argv[1], sys.argv[2], sys.argv[3])
else:
base = sys.argv[1]
target = sys.argv[2]
os.mkdir(target)
try:
os.mkdir(target + '/unsorted') # files without clear extension
main(base, target)
except FileExistsError:
print('Error! Does the target exist? Try [-e].')
except IndexError:
flags('-h', '', '')
# This will handle flags passed in
def flags(flag, base, target):
HELP = 'datasort ' + str(Version) + \
'\nAuthor: ' + Author + '' \
'\ndatasort [-option] /path/to/base /path/to/target' \
'\n\nNote that flags should be compounded, i.e. -tb instead of -t -b' \
'\n-e\t\tTarget file \'e\'xists; do not overwrite (default is to overwrite target)' \
'\n-b\t\tBase path is abbreviated; it begins in the pwd. Do not lead with / if in pwd' \
'\n-t\t\tTarget path is abbreviated; it beings in the pwd. Do not lead with / if in pwd' \
'\n-h\t\tDisplay this menu'
BUGS = '\n\nKnown bugs include:\n\n' \
'1. Currently, datasort will break if called with a `-h` flag AND a nonexistent target directory.'
if 'h' in flag:
print(HELP + BUGS)
if 'b' in flag:
base = os.getcwd() + '/' + base
if 't' in flag:
target = os.getcwd() + '/' + target
if 'e' in flag: # -e for 'exists'
if target[len(target)-1] == '/':
target = target[:len(target)-1] # this will strip / if the user includes it for the directory
# TODO
# This won't work permanently! consider the case `datasort -tbh path/to/base path/to/target`
# In this instance, the code will not function properly; it should assume that path/to/target does not exist,
# but because there is an `h` flag, it will skip the below os.mkdir(). It only works if `-h` is not included
# on a call meant to actually sort data, or if the target directory already exists. i.e. it is broken in the case
# that one calls datasort with the `-h` flag, intending to sort data into a nonexistent target.
elif 'h' not in flag:
os.mkdir(target)
return base, target
def is_extension(ext, f_name):
check_at = f_name.find(ext) + len(ext) # this basically finds what should be the character after the
try: # the extension if it's correct; if it's not correct it errors
f_name[check_at] # this checks that there are no characters at the end of where the extension should be
return False
except IndexError: # if the extension is actually the last piece, there will be an index error
return True
# this function moves the files listed in no_ext into the /unsorted directory
def group_unsorted(no_ext, target):
error = []
for f in no_ext:
try:
copy2(f, target + '/unsorted')
print(f)
except:
error.append(f)
return error
def main(base, target):
# not_sortable = True
MAX_EXT_LEN = 6 # this is just an intelligent guess for a max length of extensions. Change at will/need
# walk the base to learn extensions and create the directories to house them
for subdir, dirs, files in os.walk(base):
for file_name in files:
not_sortable = True # check if each file is sortable # causes a problem with everything going to unsorted
for c in range(len(file_name)):
if file_name[c] == '.':
if file_name[c:].lower() in extensions:
not_sortable = False
elif len(file_name[c:]) <= MAX_EXT_LEN:
extensions.append(file_name[c:].lower())
os.mkdir(target + '/' + file_name[c + 1:].lower()) # +1 so that the directory isn't a dotfile
print(file_name[c:].lower())
not_sortable = False # this file WAS sorted
if not_sortable:
unsorted.append(subdir + '/' + file_name)
for subdir, dirs, files in os.walk(base):
for file_name in files:
if subdir + '/' + file_name in unsorted: # don't do all the other stuff if the file has been determined
pass # to not have a sortable extension
else:
for i in extensions: # check every extension in the file_name until success
if i in file_name:
if is_extension(i, file_name):
print(subdir + '/' + file_name)
copy2(subdir + '/' + file_name, target + '/' + i[1:])
break # break one level -- we can stop checking extensions after success of is_extension
error_out = group_unsorted(unsorted, target) # attempt to move unsorted
if len(error_out) > 0:
print('Error on:')
print(error_out)
input('Press Enter to quit')
read_input()