Skip to content

Commit 888b6d9

Browse files
committed
Added a initializing function.
Allows the code to be run through if it is called directly as a script and not if otherwise.
1 parent a81a7c8 commit 888b6d9

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

find.py

+23-19
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ def initialize_args(argslist = sys.argv[1:]):
2020
global args
2121
global parser
2222
args = parser.parse_args(argslist)
23-
23+
2424
return args
2525

2626
def do_search(provided_path, regex_pattern, name_to_match, type_ForD):
2727
outputFromSearch = io.StringIO()
28-
28+
2929
if regex_pattern:
3030
try:
3131
compiled_re = re.compile(regex_pattern)
@@ -34,52 +34,56 @@ def do_search(provided_path, regex_pattern, name_to_match, type_ForD):
3434
exit(1)
3535
else:
3636
compiled_re = None
37-
37+
3838
if not os.path.isdir(provided_path):
3939
exit("That is not a valid directory")
40-
40+
4141
outputFromSearch = search_recursively(provided_path, compiled_re, name_to_match, type_ForD)
4242
return outputFromSearch
4343

4444

4545
def process_asset(fileOrDir, fileType, fileTypeRequired, compiled_re, name_to_match, rootDir=""):
46-
46+
4747
global results_found
4848
abs_url = os.path.join(rootDir, fileOrDir)
4949
base_name = os.path.basename(fileOrDir)
50-
50+
5151
if fileTypeRequired and fileType != fileTypeRequired:
5252
return None
5353
if compiled_re is not None and not compiled_re.search(base_name):
5454
return None
5555
if name_to_match is not None and not fnmatch.fnmatch(base_name, name_to_match):
5656
return None
57-
57+
5858
results_found = True
5959
return abs_url
6060

6161

6262
def search_recursively(provided_path, regex_pattern, name_to_match, type_ForD):
63-
63+
6464
search_results_output = io.StringIO()
65-
66-
for root, dirs, files in os.walk(provided_path, topdown=True):
65+
66+
for root, dirs, files in os.walk(provided_path, topdown=True):
6767
d_processed = process_asset(root, 'd', type_ForD, regex_pattern, name_to_match)
6868
if d_processed is not None:
6969
print(d_processed, file=search_results_output)
70-
70+
7171
for f in files:
7272
f_processed = process_asset(f, 'f', type_ForD, regex_pattern, name_to_match, root)
7373
if f_processed is not None:
7474
print(f_processed, file=search_results_output)
75-
75+
7676
return search_results_output
7777

7878

79-
#execute the search
80-
initArgs = initialize_args()
81-
results = do_search(args.path, args.regex, args.name, args.type)
82-
print(results.getvalue())
83-
results.close()
84-
if results_found is not True:
85-
print("Sorry, no results were found")
79+
def do_script_run_through():
80+
#execute the search
81+
initArgs = initialize_args()
82+
results = do_search(args.path, args.regex, args.name, args.type)
83+
print(results.getvalue())
84+
results.close()
85+
if results_found is not True:
86+
print("Sorry, no results were found")
87+
88+
if __name__ == '__main__':
89+
do_script_run_through()

0 commit comments

Comments
 (0)