-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.py
executable file
·68 lines (61 loc) · 2.66 KB
/
profile.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
#!/usr/bin/env python
# Author: Sawood Alam <[email protected]>
#
# This class creates structure of the profile, loads JSON profiles, and serializes the profile object to JSON.
import json
import time
class Profile(object):
"""Basic archive profile to be evolved by the profiler."""
def __init__(self, name="", description="", homepage="", accesspoint="", memento_compliance="", timegate="", timemap="", established="", profile_updated="", **kwargs):
"""Initialize a basic archive profile object."""
print("{0} => Initializing the profile for {1}".format(time.strftime("%Y-%m-%d %H:%M:%S"), name))
self.about = {
"name": name,
"description": description,
"homepage": homepage,
"accesspoint": accesspoint,
"memento_compliance": memento_compliance,
"timegate": timegate,
"timemap": timemap,
"established": established,
"profile_updated": profile_updated
}
self.__dict__["about"].update(kwargs)
self.stats = {}
setattr(self, "@context", "https://oduwsdl.github.io/contexts/archiveprofile.jsonld")
setattr(self, "@id", homepage)
def load(self, profile=None):
"""Load a JSON profile and populate the profile object"""
try:
self.__dict__ = json.load(profile)
print("JSON profile loaded!")
except Exception as e:
print("Could not load JSON profile!")
print(str(e))
def to_json(self):
"""Serializes processed profile object in JSON format."""
print("Converting to JSNON...")
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4, separators=(",", ": "))
def count_keys(self):
"""Generates statistics on profile keys."""
print("Generating key statistics...")
path = {}
domain = {}
for k in self.stats["suburi"].keys():
path_depth = k.strip("/").count("/")
if path_depth > 0:
try:
path[path_depth] += 1
except KeyError, e:
path[path_depth] = 1
else:
try:
domain[k.split(")")[0].count(",")+1] += 1
except KeyError, e:
domain[k.split(")")[0].count(",")+1] = 1
key_stats = {"time": len(self.stats["time"]),
"mediatype": len(self.stats["mediatype"]),
"language": len(self.stats["language"]),
"suburi": {"domain_depth": domain, "path_depth": path}}
setattr(self, "_key_stats", key_stats)
return key_stats