-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparams.py
More file actions
137 lines (120 loc) · 5.06 KB
/
params.py
File metadata and controls
137 lines (120 loc) · 5.06 KB
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
"""
===========================================
Parameters Object Class
===========================================
"""
import os
import glob
import json
import sys
import datetime
import utils
from enum import Enum
import lxml.etree as et
from pathlib import Path
# Phase enumeration constants
class Phases(Enum):
EXTRACT = 0
UPDATE = 2
BUILD = 1
# XML Schema enumeration constants
class Schema(Enum):
BITS = 0
DATACITE = 1
WORDPRESS = 2
class Parameters:
def __init__(self):
# date timestamp
now = datetime.datetime.now()
self.datestamp = "{}{}{}".format(now.year, now.month, now.day)
# Timestamp FORMAT: YYYY-MM-DD HH:MM:SS
self.postdate = "{}-{:02d}-{:02d} {}:{}:{}".format(now.year, now.month, now.day, now.hour, now.minute,
now.second)
# Timestamp FORMAT: Mon, 20 May 2019 05:50:45 +0000
self.timestamp = now.strftime("%a, %d %b %Y %H:%M:%S %z")
self.phase = None
self.schema = None
self.empty_nodes = ['self-uri']
self.element_name = 'element'
# load configuration data
with open('config.json') as fp:
cf = json.load(fp)
self.paths = cf['paths']
self.skos = et.parse(os.path.join(Path(__file__).parent, cf['paths']['taxonomy']['CCS2012'])).getroot()
self.csv = cf['csv']
try:
# --------- parse command line input ---------
if len(sys.argv) > 2:
# get resource paths from json file
if sys.argv[1]:
with open(sys.argv[1]) as fp:
print('Parsing paths file:', sys.argv[1])
user_paths = json.load(fp)
self.paths.update(user_paths)
# load base settings file
self.base = utils.load_json(self.get_path('base', 'metadata'))
# create directories for output files if does not exist
article_path = os.path.join(self.paths['root'], self.paths['metadata']['articles'])
if not os.path.isdir(article_path):
os.makedirs(article_path)
for path in self.paths['output'].values():
output_path = os.path.join(self.paths['root'], path)
if not os.path.isdir(output_path):
print("Creating new directory at {}".format(path))
os.makedirs(output_path)
else:
print("Path file (paths.json) is not specified.")
exit(1)
# extraction phase
if sys.argv[2] == "-extract":
self.phase = Phases.EXTRACT
self.ext = "*.pdf"
# option to use existing raw text (.txt) instead of PDF files (.pdf)
elif sys.argv[2] == "-update":
self.phase = Phases.UPDATE
self.ext = "*.txt"
# xml build phase
elif sys.argv[2] == "-build":
self.phase = Phases.BUILD
self.ext = "*.json"
# set schema for XML transformation
if len(sys.argv) > 3:
if sys.argv[3] == "-bits":
self.schema = Schema.BITS
elif sys.argv[3] == "-datacite":
self.schema = Schema.DATACITE
elif sys.argv[3] == "-wordpress":
self.schema = Schema.WORDPRESS
else:
print("Schema requested is missing or invalid.")
exit(1)
else:
print("Schema not specified.")
exit(1)
else:
print("Processing phase requested is not valid.")
exit(1)
else:
print("Missing arguments.")
exit(1)
except StopIteration as err:
print(err)
exit(1)
# --------------------------------------
# Get file or directory path
def get_path(self, sub_dir, parent_dir=None):
if sub_dir in self.paths or parent_dir is not None and parent_dir not in self.paths:
print("Path keys {} {} not found. Please check paths.json.".format(sub_dir, parent_dir))
return
else:
return os.path.join(self.paths['root'], self.paths[parent_dir][sub_dir]) \
if parent_dir is not None else os.path.join(self.paths['root'], self.paths[sub_dir])
# --------------------------------------
# Get multiple files from
def get_files(self, subdir, parent_dir=None):
if parent_dir is None:
return sorted(glob.glob(os.path.join(self.get_path(subdir), self.ext)))
else:
return sorted(glob.glob(os.path.join(self.get_path(subdir, parent_dir), self.ext)))
# instantiate paths
params = Parameters()