-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
101 lines (85 loc) · 2.74 KB
/
app.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
import configparser
import argparse
import time
import logging
import requests
from asnake.client import ASnakeClient
from tasks import *
def print_menu(tasks):
print("Choose a task:")
print("")
optnum = 1
for task in tasks:
print("%d) %s" % (optnum, task.prompt()))
optnum += 1
print("X) Exit")
print("")
def main_menu():
tasks = [
UserDefinedTask(args, client, logger),
BatchCreateTopContainer(args, client, logger),
BatchExportTopContainer(args, client, logger),
BatchUpdateTopContainer(args, client, logger),
BatchExportEAD(args, client, logger),
BatchExportEADArchiveswest(args, client, logger),
BatchExportEADArchiveswestPlus(args, client, logger),
BatchUpdateResource(args, client, logger),
BatchUpdateLocation(args, client, logger),
]
done = False
val = None
while not done:
print_menu(tasks)
try:
val = input(">> ").upper()
except EOFError:
print("")
val = "X"
try:
valnum = min(max(int(val) - 1, 0), len(tasks) - 1)
tasks[valnum].run()
except ValueError:
pass
if val == "X":
done = True
continue
class APIContactError(Exception):
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-y", "--yes", help="Supply Yes to all confirmation prompts", action="store_true")
args = parser.parse_args()
logger = logging.getLogger('archivespace')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('app.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)
config = configparser.ConfigParser()
config.read('settings.ini')
args.config = config
try:
client = ASnakeClient(baseurl=config['aspace_credentials']['api_host'],
username=config['aspace_credentials']['username'],
password=config['aspace_credentials']['password'])
except KeyError as e:
logger.error('settings.ini does not exist or is invalid')
raise e
# Simple sanity check to make sure client is setup
try:
resp = client.get('/')
if not resp.ok:
resp.raise_for_status()
except:
logger.error('Unable to contact ArchivesSpace instance at %s' % config['aspace_credentials']['api_host'])
raise APIContactError('Unable to contact ArchivesSpace instance at %s' % config['aspace_credentials']['api_host'])
main_menu()