forked from YaleArchivesSpace/yams_data_auditing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
111 lines (99 loc) · 3.57 KB
/
utilities.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
#/usr/bin/python3
#~/anaconda3/bin/python
'''a collection of file handling and other functions'''
import csv, logging, subprocess, os, time, sys
def error_log(filepath=None):
if sys.platform == "win32":
if filepath == None:
logger = '\\Windows\\Temp\\error_log.log'
else:
logger = filepath
else:
if filepath == None:
logger = '/tmp/error_log.log'
else:
logger = filepath
logging.basicConfig(filename=logger, level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(name)s %(message)s')
return logger
def login():
import requests, json
try:
url = input('Please enter the ArchivesSpace API URL: ')
username = input('Please enter your username: ')
password = input('Please enter your password: ')
auth = requests.post(url+'/users/'+username+'/login?password='+password).json()
#if session object is returned then login was successful; if not it failed.
if 'session' in auth:
session = auth["session"]
h = {'X-ArchivesSpace-Session':session, 'Content_Type': 'application/json'}
print('Login successful!')
logging.debug('Success!')
return (url, h)
else:
print('Login failed! Check credentials and try again.')
logging.debug('Login failed')
logging.debug(auth.get('error'))
#try again
u, heads = login()
return u, heads
except:
print('Login failed! Check credentials and try again!')
logging.exception('Error: ')
u, heads = login()
return u, heads
#Open a CSV in reader mode
def opencsv():
try:
input_csv = input('Please enter path to CSV: ')
file = open(input_csv, 'r', encoding='utf-8')
csvin = csv.reader(file)
headline = next(csvin, None)
return headline, csvin
except:
logging.exception('Error: ')
logging.debug('Trying again...')
print('CSV not found. Please try again.')
h, c = opencsv()
return h, c
#Open a CSV file in writer mode
def opencsvout():
try:
output_csv = input('Please enter path to output CSV: ')
fileob = open(output_csv, 'a', encoding='utf-8', newline='')
csvout = csv.writer(fileob)
return (fileob, csvout)
except:
logging.exception('Error: ')
print('\nError creating outfile. Please try again.\n')
f, c = opencsvout()
return (f, c)
def opentxt():
filepath = input('Please enter path to output text file: ')
filename = open(filepath, 'a', encoding='utf-8')
return filename
#check if the directory exists. If not, make dir
def setdirectory():
directory = input('Please enter path to output directory: ')
if os.path.isdir(directory):
pass
else:
os.mkdir(directory)
return directory
def openjson(directory, filename):
filepath = open(directory + '/' + filename + '.json', 'w', encoding='utf-8')
return filepath
def openxml(directory, filename):
filepath = open(directory + '/' + filename.strip() + '.xml', 'w', encoding='utf-8')
return filepath
def keeptime(start):
elapsedtime = time.time() - start
m, s = divmod(elapsedtime, 60)
h, m = divmod(m, 60)
logging.debug('Total time elapsed: ' + '%d:%02d:%02d' % (h, m, s) + '\n')
def open_outfile(filepath):
if sys.platform == "win32":
os.startfile(filepath)
else:
opener = "open" if sys.platform == "darwin" else "xdg-open"
subprocess.call([opener, filepath])