-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
146 lines (130 loc) · 4.37 KB
/
helpers.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
from copy import deepcopy
from datetime import datetime, date
def list_to_string(list,delimiter):
""" takes a list and a delimiter and turns list into a string """
string = ""
for i in list:
string += str(i) + delimiter
string = string[:string.rfind(delimiter)]
return string
def str_to_list(string,delimiter):
if not string: # catches null or empty strings
return []
array = string.split(delimiter)
if '' in array:
array.remove('')
return array
def update_str_to_list(row,keys,delimiter):
for key in keys:
row[key] = str_to_list(row[key],delimiter)
def numify_key(key,number):
return str(key)+str(number)
def in_maxes(key,maxes):
for k in maxes:
if k == 'max_' + str(key):
return k
return False
def remove_key(dictionary,keys):
""" creates new dict with out the key in it """
new = deepcopy(dictionary)
for i in keys:
try:
del new[i]
except Exception as e:
print (new)
raise e
return new
def make_key_list(row,keys,name_map = None):
#modifies dict to make things rows
for key in keys:
if isinstance(row[key],list):
pass
elif row[key] is None or row[key] == "":
row[key] = []
else:
row[key] = [row[key]]
def change_key_names(row,name_map = None):
if name_map is None:
name_map = {}
keys = list(row.keys())
for key in keys:
#if user wants fields renamed pass in dict {"original name": "updated name"}
if key in name_map:
row[name_map[key]] = row[key]
del row[key]
else:
result = row[key] #
del row[key] # for stupid sorting
row[key] = result #
def lambda_value_map(row,proc):
keys = list(row.keys())
for key in keys:
proc(row,key)
def delete_keys(row,keys):
for key in keys:
del row[key]
def create_embargo_col(row):
"""
takes in a record and modifies the record to now have embargo fields instead of
rdate type stuff.
{...,'rdate' : '2015-01-01','availability': 'restricted'} will become:
{...,
'embargo' : true,
'embargo_release_date' : '2015-01-01',
'embargo_visibility': [
'authenticated',
'public'
]
}
goes from auth to pub, and from pub to pub, and from private to auth. unless specified.
"""
restrictions = ['public','authenticated','private']
release_day = row['rdate']
current_restriction = row['availability']
if current_restriction == "unrestricted":
current_restriction = 0
elif current_restriction == "restricted":
current_restriction = 1
else:
current_restriction = 2
post_restriction = 0 # always make public once embargoes expire
if current_restriction == 0:
if release_day == '0000-00-00' or datetime.strptime(row['rdate'],"%Y-%m-%d") < datetime.today():
####################################
# No embargo or restriction needed #
####################################
row['embargo'] = False
row['embargo_release_date'] = None
del row['rdate']
del row['availability']
return row
else: # edge case given by Aaron Neslin Tuesday May 28th 2019
#############################################################################
# case: future rdate, but says 'unrestricted' : keep restricted until rdate #
#############################################################################
##
#############################################################################
row['embargo'] = True
row['embargo_visibility'] = ['authenticated','public'] #make current state its final state
row['embargo_release_date'] = row['rdate'] # release date in the past (it wont show up)
# print(f"WARNING: restricting previously unrestricted work: {row['identifier']}",\
# f"\n{release_day}{row['availability']}",\
# "\nSend Email to Aaron Neslin / Emily O'Brien about this")
del row['rdate']
del row['availability']
return row
##############################
# Work needs to be embargoed #
##############################
row['embargo'] = True
row['embargo_visibility'] = [restrictions[current_restriction], restrictions[post_restriction]]
row['embargo_release_date'] = release_day
if release_day == '0000-00-00': # needs to be restricted
######################################################
# edge case, restrict for ever if this date is used. #
######################################################
row['embargo_visibility'] = ['private',restrictions[current_restriction]] #make current state its final state
row['embargo_release_date'] = '1776-07-04' # release date in the past (it wont show up)
del row['rdate']
del row['availability']
return row