-
Notifications
You must be signed in to change notification settings - Fork 0
/
droidcsvhandlerclass.py
78 lines (64 loc) · 2.48 KB
/
droidcsvhandlerclass.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
"""Handler class for the DROID csv."""
import os.path
from urllib import parse as urlparse
import csv
class genericCSVHandler:
def __getCSVheaders__(self, csvcolumnheaders):
header_list = []
for header in csvcolumnheaders:
header_list.append(header)
return header_list
# returns list of rows, each row is a dictionary
# header: value, pair.
def csvaslist(self, csvfname):
columncount = 0
csvlist = None
if os.path.isfile(csvfname):
csvlist = []
with open(csvfname, "r", encoding="UTF-8") as csvfile:
csvreader = csv.reader(csvfile)
for row in csvreader:
if csvreader.line_num == 1: # not zero-based index
header_list = self.__getCSVheaders__(row)
columncount = len(header_list)
else:
csv_dict = {}
# for each column in header
# note: don't need ID data. Ignoring multiple ID.
for i in range(columncount):
csv_dict[header_list[i]] = row[i]
csvlist.append(csv_dict)
return csvlist
class droidCSVHandler:
# returns droidlist type
def readDROIDCSV(self, droidcsvfname):
csvhandler = genericCSVHandler()
self.csv = csvhandler.csvaslist(droidcsvfname)
return self.csv
def removecontainercontents(self, droidlist):
newlist = [] # naive remove causes loop to skip items
for row in droidlist:
if self.getURIScheme(row["URI"]) == "file":
newlist.append(row)
return newlist
def removefolders(self, droidlist):
# TODO: We can generate counts here and store in member vars
newlist = [] # naive remove causes loop to skip items
for i, row in enumerate(droidlist):
if row["TYPE"] != "Folder":
newlist.append(row)
return newlist
def retrievefolderlist(self, droidlist):
newlist = []
for row in droidlist:
if row["TYPE"] == "Folder":
newlist.append(row["FILE_PATH"])
return newlist
def retrievefoldernames(self, droidlist):
newlist = []
for row in droidlist:
if row["TYPE"] == "Folder":
newlist.append(row["NAME"])
return newlist
def getURIScheme(self, url):
return urlparse(url).scheme