-
Notifications
You must be signed in to change notification settings - Fork 0
/
combine.py
167 lines (137 loc) · 6.99 KB
/
combine.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Copyright 2023, 2024 Consoli Solutions, LLC. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may also obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
The license is free for single customer use (internal applications). Use of this module in the production,
redistribution, or service delivery for commerce requires an additional license. Contact [email protected] for
details.
**Description**
Combines the output of multiple capture or combine files into a single project object.
**Version Control**
+-----------+---------------+-----------------------------------------------------------------------------------+
| Version | Last Edit | Description |
+===========+===============+===================================================================================+
| 4.0.0 | 04 Aug 2023 | Re-Launch |
+-----------+---------------+-----------------------------------------------------------------------------------+
| 4.0.1 | 06 Mar 2024 | Improved error messages. |
+-----------+---------------+-----------------------------------------------------------------------------------+
| 4.0.2 | 03 Apr 2024 | Added version numbers of imported libraries. |
+-----------+---------------+-----------------------------------------------------------------------------------+
"""
__author__ = 'Jack Consoli'
__copyright__ = 'Copyright 2023, 2024 Consoli Solutions, LLC'
__date__ = '03 Apr 2024'
__license__ = 'Apache License, Version 2.0'
__email__ = '[email protected]'
__maintainer__ = 'Jack Consoli'
__status__ = 'Released'
__version__ = '4.0.2'
import sys
import os
import datetime
import brcdapi.log as brcdapi_log
import brcdapi.gen_util as gen_util
import brcdapi.file as brcdapi_file
import brcddb.brcddb_project as brcddb_project
import brcddb.util.copy as brcddb_copy
import brcddb.brcddb_common as brcddb_common
_version_d = dict(
brcdapi_log=brcdapi_log.__version__,
gen_util=gen_util.__version__,
brcdapi_file=brcdapi_file.__version__,
brcddb_project=brcddb_project.__version__,
brcddb_copy=brcddb_copy.__version__,
brcddb_common=brcddb_common.__version__,
)
_DOC_STRING = False # Should always be False. Prohibits any code execution. Only useful for building documentation
# _STAND_ALONE: True: Executes as a standalone module taking input from the command line. False: Does not automatically
# execute. This is useful when importing this module into another module that calls psuedo_main().
_STAND_ALONE = True # See note above
_input_d = dict(
i=dict(h='Required. Directory of captured data files. Only files with a ".json" extension are read.'),
o=dict(h='Required. Name of combined data capture file. Placed in the folder specified by -i. The extension '
'".json" is automatically appended.')
)
_input_d.update(gen_util.parseargs_log_d)
def pseudo_main(inf, outf):
"""Basically the main(). Did it this way so that it can easily be used as a standalone module or called externally.
:param inf: Name of the input folder containing capture.py or combine.py output
:type inf: str
:param outf: Name of the output file
:type outf: str
:return: Exit code
:rtype: int
"""
ec, el, file_l = brcddb_common.EXIT_STATUS_OK, list(), list()
# Create project
proj_obj = brcddb_project.new(inf, datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S'))
proj_obj.s_python_version(sys.version)
proj_obj.s_description('Captured data from ' + inf)
# Get a list of files - Filter out directories is just to protect the user. It shouldn't be necessary.
try:
file_l = brcdapi_file.read_directory(inf)
except FileExistsError:
el.append('Folder ' + inf + ', specified with -i, does not exist.')
except PermissionError:
el.append('You do not have access rights to read the folder ' + inf + ' specified with -i')
if outf in file_l:
el.extend('Combined output file, ' + outf + ', already exists in: ' + inf + '. Processing halted')
else:
x = len('.json')
for file in [f for f in file_l if len(f) > x and f.lower()[len(f)-x:] == '.json']:
brcdapi_log.log('Processing file: ' + file, echo=True)
obj = brcdapi_file.read_dump(inf + '/' + file)
brcddb_copy.plain_copy_to_brcddb(obj, proj_obj)
# Now save the combined file
plain_copy = dict()
brcddb_copy.brcddb_to_plain_copy(proj_obj, plain_copy)
try:
brcdapi_file.write_dump(plain_copy, inf + '/' + outf)
except PermissionError:
el.extend(['', 'Permission error writing ' + outf, ''])
# Wrap up
if len(el) > 0:
brcdapi_log.log(el, echo=True)
ec = brcddb_common.EXIT_STATUS_INPUT_ERROR
return ec
def _get_input():
"""Parses the module load command line
:return ec: Error code
:rtype ec: int
"""
global __version__, _input_d, _version_d
ec = brcddb_common.EXIT_STATUS_OK
# Get command line input
args_d = gen_util.get_input('Combine the output of multiple JSON files from capture.py or this utility.', _input_d)
# Set up logging
brcdapi_log.open_log(folder=args_d['log'], supress=args_d['sup'], no_log=args_d['nl'], version_d=_version_d)
# User feedback
ml = [
os.path.basename(__file__) + ', ' + __version__,
'Directory, -i: ' + args_d['i'],
'Output file, -o: ' + args_d['o'],
'Log, -log: ' + str(args_d['log']),
'No log, -nl: ' + str(args_d['nl']),
'Supress, -sup: ' + str(args_d['sup']),
'',
]
brcdapi_log.log(ml, echo=True)
return ec if ec != brcddb_common.EXIT_STATUS_OK else \
pseudo_main(args_d['i'], brcdapi_file.full_file_name(args_d['o'], '.json'))
##################################################################
#
# Main Entry Point
#
###################################################################
if _DOC_STRING:
print('_DOC_STRING is True. No processing')
exit(brcddb_common.EXIT_STATUS_OK)
if _STAND_ALONE:
_ec = _get_input()
brcdapi_log.close_log(['', 'Processing Complete. Exit code: ' + str(_ec)], echo=True)
exit(_ec)