-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsoftware.py
84 lines (69 loc) · 2.61 KB
/
software.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
from citation import get_bib_source_from_dict, citations, reference_manager_exports
from steps.user_input import UserInputStep
from steps.exceptions import NoChildrenException
class Software(object):
def __init__(self, user_supplied_id):
self.user_supplied_id = user_supplied_id
self.completed_steps = []
def find_metadata(self):
my_step = UserInputStep()
my_step.set_content_url(self.user_supplied_id)
my_step.set_content(self.user_supplied_id)
self.completed_steps.append(my_step)
cursor = 0
while not self.completed_steps[-1].is_metadata:
current_step = self.completed_steps[cursor]
try:
next_step = current_step.get_child()
self.completed_steps.append(next_step)
cursor = len(self.completed_steps) - 1
except NoChildrenException:
cursor -= 1
@property
def name(self):
if self.metadata and self.metadata.get("title", ""):
response = self.metadata.get("title", "")
if response.__class__.__name__ == "MixedString":
return response.pop()
else:
return response
return self.display_url
@property
def display_url(self):
return self.completed_steps[0].content_url
@property
def metadata(self):
metadata_step = self.completed_steps[-1]
if metadata_step.content.get("issued"):
try:
year = metadata_step.content["issued"]["date-parts"][0][0]
except IndexError:
year = ""
metadata_step.content["year"] = year
metadata_dict = metadata_step.content
for step in reversed(self.completed_steps):
if step.url and step.content:
metadata_dict["URL"] = step.url
break
return metadata_dict
def get_provenance(self):
ret = [s.to_dict() for s in self.completed_steps]
return ret
@property
def citation_plain(self):
citations = self.to_dict()["citations"]
return next(
(i["citation"] for i in citations if i["style_shortname"] == "harvard1"),
None,
)
def to_dict(self):
bibtex_metadata = get_bib_source_from_dict(self.metadata)
ret = {
"url": self.display_url,
"name": self.name,
"citations": citations(bibtex_metadata),
"exports": reference_manager_exports(self.metadata),
"metadata": self.metadata,
"provenance": self.get_provenance(),
}
return ret