-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
47 lines (31 loc) · 1.2 KB
/
parser.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
#!/usr/bin/env python3
import os
from preferences import *
def read_dir (dirpath: str):
"""
Parse dirpath to reference files and their beacon's path
:param dirpath: dirpath to parse
:return: dictionary of files indexed by their beacon's path
"""
def recur (dirpath: str, file_dict: dict):
# Path not existing (error)
if not os.path.exists (dirpath):
return file_dict # INCLUDE IN LOG
# Initialise list of subdirs to parse
subdirs = list()
# Generate beacon path (regardless of its existence)
beacon_path = os.path.join (dirpath, BEACON_FILENAME)
# Initialise dict entry
file_dict [beacon_path] = list()
# Parse current dir
with os.scandir (dirpath) as file_dir:
for file in file_dir:
if file.is_dir():
subdirs.append (file.path)
else:
file_dict [beacon_path] .append (file) # OR append filepath
# Recursively run for subdirs
for subdirpath in subdirs:
file_dict = recur (subdirpath, file_dict)
return file_dict
return recur( dirpath, dict() )