-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbd-extent-calculator.py
173 lines (150 loc) · 7.11 KB
/
bd-extent-calculator.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from bs4 import BeautifulSoup,Tag
import sys,os,glob,re
# This parses the components
def parse_component(componentTitle, collectionTitle):
# c element may be either series or file
did = componentTitle.parent
component = did.parent
# Search for c elements under the top level c
if component.find_all('c'):
# Create a list of c tags, i.e. file or subseries, under series
# This assumes there are no sub-series extents
listFileRecords =[]
for sibling in did.next_siblings:
if isinstance(sibling, Tag):
listFileRecords.append(sibling)
# Add c tags below subseries to list
for sibling in listFileRecords:
for child in sibling.find_all('c'):
if isinstance(child, Tag):
listFileRecords.append(child)
# Create a list of all the quantity tags
listQuantity =[]
for c in listFileRecords:
for quantity in c.find_all('quantity'):
listQuantity.append(quantity)
# For each quantity tag, if its next sibling (unittype tag) has bytes/files/websites, add to list
listKB = []
listMB = []
listGB = []
listFiles =[]
listWebsites = []
for quantity in listQuantity:
strQuantity = quantity.text
# This assumes there will always be a unittype somewhere after quantity
unittype = quantity.find_next_sibling("unittype")
if re.search('[kK]ilobytes?', unittype.text):
listKB.append(eval(strQuantity))
if re.search('[mM]egabytes?', unittype.text):
listMB.append(eval(strQuantity))
if re.search('[gG]igabytes?', unittype.text):
listGB.append(eval(strQuantity))
# Looks for the word at the beginning of start to avoid "electronic file transfer"
if re.match('[fF]iles?', unittype.text):
listFiles.append(eval(strQuantity))
if re.search('[wW]ebsites?', unittype.text):
listWebsites.append(eval(strQuantity))
# Get a sum total of the lists
totalKB = sum(listKB)
totalMB = sum(listMB)
totalGB = sum(listGB)
seriesTotalFiles = sum(listFiles)
seriesTotalWeb = sum(listWebsites)
# Add up the lists of bytes, files, and websites to get grand totals
seriesTotalMB = totalKB/1024+totalMB+totalGB*1024
seriesResult = '"' + collectionTitle + '/' + componentTitle.text + '"' + ',' + str(round(seriesTotalMB)) + ',' + str(seriesTotalFiles) + ',' + str(seriesTotalWeb)
else:
# Solo file record means it's not in a series
listSoloFileRecords =[]
for soloFile in component:
if isinstance(soloFile, Tag):
listSoloFileRecords.append(soloFile)
# Create a list of all the quantity tags
listQuantity =[]
for c in listSoloFileRecords:
for quantity in c.find_all('quantity'):
listQuantity.append(quantity)
# For each quantity tag, if its next sibling (unittype tag) has bytes/files/websites, add to list
listKB = []
listMB = []
listGB = []
listFiles =[]
listWebsites = []
for quantity in listQuantity:
strQuantity = quantity.text
# This assumes there will always be a unittype somewhere after quantity
unittype = quantity.find_next_sibling("unittype")
if re.search('[kK]ilobytes?', unittype.text):
listKB.append(eval(strQuantity))
if re.search('[mM]egabytes?', unittype.text):
listMB.append(eval(strQuantity))
if re.search('[gG]igabytes?', unittype.text):
listGB.append(eval(strQuantity))
# Looks for the word at the beginning of start to avoid "electronic file transfer"
if re.match('[fF]iles?', unittype.text):
listFiles.append(eval(strQuantity))
if re.search('[wW]ebsites?', unittype.text):
listWebsites.append(eval(strQuantity))
# Get a sum total of the lists
totalKB = sum(listKB)
totalMB = sum(listMB)
totalGB = sum(listGB)
seriesTotalFiles = sum(listFiles)
seriesTotalWeb = sum(listWebsites)
# Add up the lists of bytes, files, and websites to get grand totals
seriesTotalMB = totalKB/1024+totalMB+totalGB*1024
seriesResult = '"' + collectionTitle + '/' + componentTitle.text + '"' + ',' + str(round(seriesTotalMB)) + ',' + str(seriesTotalFiles) + ',' + str(seriesTotalWeb)
return [seriesTotalMB,seriesTotalFiles,seriesTotalWeb,seriesResult]
# This parses the collection
def parse_collection(soup):
collectionTotalSizeMB = 0
collectionTotalFiles = 0
collectionTotalWeb = 0
x = soup.select_one('archdesc > did > unittitle')
if x is not None:
collectionTitle = x.text
else:
print("The EAD XML file may be empty.")
sys.exit()
seriesResultList = []
# Get component titles
componentTitles = soup.select('dsc > c > did > unittitle')
for componentTitle in componentTitles:
seriesTotalMB, seriesTotalFiles, seriesTotalWeb, seriesResult = parse_component(componentTitle,collectionTitle)
collectionTotalSizeMB = collectionTotalSizeMB + seriesTotalMB
collectionTotalFiles = collectionTotalFiles + seriesTotalFiles
collectionTotalWeb = collectionTotalWeb + seriesTotalWeb
seriesResultList.append(seriesResult)
print('"' + collectionTitle + '",' + str(round(collectionTotalSizeMB)) + ',' + str(collectionTotalFiles) + ',' + str(collectionTotalWeb))
for seriesResult in seriesResultList:
print(seriesResult)
return [collectionTotalSizeMB,collectionTotalFiles, collectionTotalWeb]
# Main script
scriptName, path = sys.argv
parseOneEAD = path.endswith('.xml')
print("Title,MB,Files,Websites")
if parseOneEAD:
with open(path, 'r', encoding="utf-8") as f:
file = f.read()
soup = BeautifulSoup(file, 'xml')
parse_collection(soup)
else:
collectionsTotalSizeMB = 0
collectionsTotalFiles = 0
collectionsTotalWeb = 0
numberCollections = 0
fileNameList = glob.glob(os.path.join(path,'*.xml'))
if len(fileNameList) == 0:
print("There were no XML files in this directory.")
sys.exit()
# Calling each XML file from path to directory
for filename in fileNameList:
with open(filename, 'r', encoding="utf-8") as f:
file = f.read()
soup = BeautifulSoup(file, 'xml')
grandTotalSizeMB, grandTotalFiles, grandTotalWeb = parse_collection(soup)
collectionsTotalSizeMB = collectionsTotalSizeMB + grandTotalSizeMB
collectionsTotalFiles = collectionsTotalFiles + grandTotalFiles
collectionsTotalWeb = collectionsTotalWeb + grandTotalWeb
numberCollections = numberCollections + 1
print('"Total for ' + str(numberCollections) + ' collections",' + str(round(collectionsTotalSizeMB)) + ',' + str(collectionsTotalFiles) + ',' + str(collectionsTotalWeb))