-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdefaults_setup.py
190 lines (179 loc) · 9.19 KB
/
defaults_setup.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import json
import os
from as_xtf_GUI import logger
from pathlib import Path
@logger.catch
def set_defaults_file():
"""
Checks defaults.json file and if there is an error, creates a new defaults.json file and returns the data.
For an in-depth review on how this code is structured, see the wiki:
https://github.com/uga-libraries/ASpace_Batch_Export-Cleanup-Upload/wiki/Code-Structure#set_default_file
Returns:
defaults_data (dict): contains all the data for default behavior for the GUI
"""
create_default_folders()
clean_eads = str(Path(os.getcwd(), "clean_eads"))
source_eads = str(Path(os.getcwd(), "source_eads"))
source_marcs = str(Path(os.getcwd(), "source_marcs"))
source_pdfs = str(Path(os.getcwd(), "source_pdfs"))
source_labels = str(Path(os.getcwd(), "source_labels"))
xtf_default = ["ead_export_default", "_INCLUDE_UNPUB_", "_INCLUDE_DAOS_", "_NUMBERED_CS_", "_USE_EAD3_",
"_KEEP_RAW_", "_CLEAN_EADS_", "_OUTPUT_DIR_", "_SOURCE_DIR_", "_RMV_NANC_", "_OPEN_OUTPUT_",
"marc_export_default", "pdf_export_default", "labels_export_default", "ead_cleanup_defaults",
"_ADD_EADID_", "_DEL_NOTES_", "_CLN_EXTENTS_", "_ADD_CERTAIN_", "_ADD_LABEL_", "_DEL_LANGTRAIL_",
"_DEL_CONTAIN_", "_ADD_PHYSLOC_", "_DEL_ATIDS_", "_DEL_ARCHIDS_", "_CNT_XLINKS_", "_DEL_NMSPCS_",
"_DEL_ALLNS_", "as_api", "repo_default", "_REPO_NAME_", "_REPO_ID_", "xtf_default", "xtf_version",
"xtf_host", "xtf_remote_path", "xtf_local_path", "xtf_indexer_path", "xtf_lazyindex_path",
"_REINDEX_AUTO_", "_UPDATE_PERMISSIONS_"]
defaults_keys = []
filepath_keys = ["_OUTPUT_DIR_", "_SOURCE_DIR_", "xtf_local_path"]
try:
with open("defaults.json", "r") as DEFAULTS:
defaults_data = json.load(DEFAULTS)
for default, setting in defaults_data.items():
if default not in defaults_keys:
defaults_keys.append(default)
if isinstance(setting, dict):
for setting_key, setting_value in setting.items():
if setting_key not in defaults_keys:
defaults_keys.append(setting_key)
if setting_key in filepath_keys:
if os.path.exists(setting_value) is False:
raise Exception
for default in xtf_default:
if default not in defaults_keys:
raise Exception
DEFAULTS.close()
except Exception as defaults_error:
print(defaults_error)
logger.error(f'Generating new defaults file due to error: {defaults_error}')
print("Generating new defaults file...", end='', flush=True)
with open("defaults.json", "w") as DEFAULTS:
defaults = {"ead_export_default": {"_INCLUDE_UNPUB_": False, "_INCLUDE_DAOS_": True, "_NUMBERED_CS_": True,
"_USE_EAD3_": False, "_RMV_NANC_": True, "_KEEP_RAW_": False,
"_CLEAN_EADS_": True, "_OUTPUT_DIR_": clean_eads,
"_SOURCE_DIR_": source_eads},
"marc_export_default": {"_INCLUDE_UNPUB_": False, "_RMV_NANC_": True, "_OPEN_OUTPUT_": False,
"_OUTPUT_DIR_": source_marcs},
"pdf_export_default": {"_INCLUDE_UNPUB_": False, "_INCLUDE_DAOS_": True, "_NUMBERED_CS_": True,
"_USE_EAD3_": False, "_RMV_NANC_": True,"_OPEN_OUTPUT_": False,
"_OUTPUT_DIR_": source_pdfs},
"labels_export_default": {"_RMV_NANC_": True, "_OPEN_OUTPUT_": False,
"_OUTPUT_DIR_": source_labels},
"ead_cleanup_defaults": {"_ADD_EADID_": True, "_DEL_NOTES_": True, "_CLN_EXTENTS_": True,
"_ADD_CERTAIN_": True, "_ADD_LABEL_": True, "_DEL_LANGTRAIL_": True,
"_DEL_CONTAIN_": True, "_ADD_PHYSLOC_": True, "_DEL_ATIDS_": True,
"_DEL_ARCHIDS_": True, "_CNT_XLINKS_": True, "_DEL_NMSPCS_": True,
"_DEL_ALLNS_": True},
"as_api": "",
"repo_default": {"_REPO_NAME_": "", "_REPO_ID_": ""},
"xtf_default": {"xtf_version": True,
"xtf_host": "",
"xtf_remote_path": "",
"xtf_local_path": clean_eads,
"xtf_indexer_path": "",
"xtf_lazyindex_path": "",
"_REINDEX_AUTO_": True,
"_UPDATE_PERMISSIONS_": True}}
dump_defaults = json.dumps(defaults)
DEFAULTS.write(dump_defaults)
DEFAULTS.close()
print("Done")
with open("defaults.json", "r") as DEFAULTS:
defaults_data = json.load(DEFAULTS)
DEFAULTS.close()
return defaults_data
@logger.catch
# TODO: add logging to create_default_folders
def create_default_folders():
"""
Checks for clean_eads, source_eads, source_labels, source_marcs, and source_pdfs within current working directory.
Returns:
None
"""
# search for existence of a clean_eads folder for ArchivesSpace EAD records
try:
current_directory = os.getcwd()
for root, directories, files in os.walk(current_directory):
if "clean_eads" in directories:
break
else:
raise Exception
except Exception as clean_ead_error:
print(str(clean_ead_error) + "\nNo clean_eads folder found, creating new one...", end='', flush=True)
current_directory = os.getcwd()
folder = "clean_eads"
clean_path = os.path.join(current_directory, folder)
os.mkdir(clean_path)
print(" Done.")
# search for existence of a source folder for ArchivesSpace EAD records
try:
current_directory = os.getcwd()
for root, directories, files in os.walk(current_directory):
if "source_eads" in directories:
break
else:
raise Exception
except Exception as source_ead_error:
print(str(source_ead_error) + "\nNo source_eads folder found, creating new one...", end='', flush=True)
current_directory = os.getcwd()
folder = "source_eads"
source_path = os.path.join(current_directory, folder)
os.mkdir(source_path)
print("{} folder created\n".format(folder))
# search for existence of a source folder for ArchivesSpace MARCXML records
try:
current_directory = os.getcwd()
for root, directories, files in os.walk(current_directory):
if "source_marcs" in directories:
break
else:
raise Exception
except Exception as source_marc_error:
print(str(source_marc_error) + "\nNo source_marcs folder found, creating new one...", end='', flush=True)
current_directory = os.getcwd()
folder = "source_marcs"
source_path = os.path.join(current_directory, folder)
os.mkdir(source_path)
print("{} folder created\n".format(folder))
# search for existence of a source folder for ArchivesSpace PDF records
try:
current_directory = os.getcwd()
for root, directories, files in os.walk(current_directory):
if "source_pdfs" in directories:
break
else:
raise Exception
except Exception as source_pdf_error:
print(str(source_pdf_error) + "\nNo source_pdfs folder found, creating new one...", end='', flush=True)
current_directory = os.getcwd()
folder = "source_pdfs"
source_path = os.path.join(current_directory, folder)
os.mkdir(source_path)
print("{} folder created\n".format(folder))
# search for existence of a source folder for ArchivesSpace Container Labels
try:
current_directory = os.getcwd()
for root, directories, files in os.walk(current_directory):
if "source_labels" in directories:
break
else:
raise Exception
except Exception as source_label_error:
print(str(source_label_error) + "\nNo source_labels folder found, creating new one...", end='', flush=True)
current_directory = os.getcwd()
folder = "source_labels"
source_path = os.path.join(current_directory, folder)
os.mkdir(source_path)
print("{} folder created\n".format(folder))
@logger.catch
def reset_defaults():
"""
Deletes and recreates defaults.json file.
Returns:
None
"""
if os.path.isfile(Path(os.getcwd(), "defaults.json")) is True:
os.remove(Path(os.getcwd(), "defaults.json"))
print("defaults.json deleted")
set_defaults_file()