-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdset2iso.py
131 lines (91 loc) · 4.13 KB
/
dset2iso.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
# Python script:
#
# 1. Translate DataCite JSON records to ISO 19139 XML records
# 2. Push ISO records to a CSW Server like GeoNetwork.
#
import argparse
import sys
import os.path
__version_info__ = ('2021', '01', '14')
__version__ = '-'.join(__version_info__)
PROGRAM_DESCRIPTION = '''
A program for translating JSON metadata into ISO 19139 metadata.
Here are some working examples showing the two main uses of this program:
* Convert a single DSET metadata record using STDIN and STDOUT:
python dset2iso.py < defaultInputRecords/test_dset_full.txt > test_dset_full.xml
* Perform batch DSET metadata record processing:
python dset2iso.py --inputDir ./defaultInputRecords --outputDir ./defaultOutputRecords
Program Version: '''
class PrintHelpOnErrorParser(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write('error: %s\n' % message)
self.print_help()
sys.exit(2)
def checkDirectoryExistence(directoryPath, directoryDescription):
""" generate an error if directory does not exist. """
if not os.path.isdir(directoryPath):
message = directoryDescription + ' does not exist: %s\n' % directoryPath
parser.error(message)
def checkFileExistence(filePath, description):
""" generate an error if file does not exist. """
if not os.path.isfile(filePath):
message = description + ' does not exist: %s\n' % filePath
parser.error(message)
#
# Parse and validate command line options.
#
programHelp = PROGRAM_DESCRIPTION + __version__
parser = PrintHelpOnErrorParser(description=programHelp, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--template', nargs=1, help="path to ISO XML template file, default is "
"'./templates_ISO19139/dset_full.xml'")
parser.add_argument('--inputDir', nargs=1, help="base directory for input records")
parser.add_argument('--outputDir', nargs=1, help="base directory for output records")
parser.add_argument('--version', action='version', version="%(prog)s (" + __version__ + ")")
args = parser.parse_args()
# Require that --input-dir and --output-dir both be used if either is used.
if len([x for x in (args.inputDir, args.outputDir) if x is not None]) == 1:
parser.error('--inputDir and --outputDir must be given together')
# Check that input and output directories exist.
readSTDIN = (args.inputDir == None)
if not readSTDIN:
checkDirectoryExistence(args.inputDir[0], 'Input directory')
checkDirectoryExistence(args.outputDir[0], 'Output directory')
import api.inputjson as dset_input
import api.translate.dset as dset_translate
import api.output as dset_output
import pprint
ISO_TEMPLATE_PATH = './templates_ISO19139/dset_full.xml'
###
### START OF MAIN PROGRAM
###
if args.template and args.template[0]:
## Insert new concepts into an existing ISO XML file
ISO_TEMPLATE_PATH = args.template[0]
checkFileExistence(ISO_TEMPLATE_PATH, 'ISO template')
if readSTDIN:
inputText = sys.stdin.readlines()
inputText = "".join(inputText)
jsonData = dset_input.getJSONData(inputText)
# pprint.pprint(jsonData)
isoText = dset_translate.transformDSETToISO(jsonData, ISO_TEMPLATE_PATH)
# Python 3 needs conversion from byte array to string
isoText = str(isoText)
print(isoText, file=sys.stdout)
else:
inputDir = args.inputDir[0]
outputDir = args.outputDir[0]
print(inputDir, file=sys.stdout)
jsonFiles = dset_input.getJSONFileNames(inputDir)
print("Found " + str(len(jsonFiles)) + " input files.", file=sys.stdout)
for inputFile in jsonFiles:
with open(inputFile, 'r') as myfile:
inputText = myfile.readlines()
inputText = "".join(inputText)
jsonData = dset_input.getJSONData(inputText)
print((" Translating file: " + inputFile), file=sys.stdout)
isoText = dset_translate.transformDSETToISO(jsonData, ISO_TEMPLATE_PATH)
outputFile = dset_output.prepareOutputFile(inputFile, inputDir, outputDir)
print((inputFile + " -> " + outputFile), file=sys.stdout)
file = open(outputFile, 'w')
file.write(isoText)
file.close()