Skip to content
Snippets Groups Projects
Commit 64cab2d9 authored by AustinSanders's avatar AustinSanders Committed by Jesse Mapel
Browse files

Address spiceinit slowdown (#395)

* Added the abilty to pass a parsed label to drivers

* Parameterized driver constructor with parsed label

* Addresses PR feedback

Adds grammar to exception message
Adds docstring
Allows processing to continue on parse failure

* Fixed documentation for parse_label
parent 8d596fa6
No related branches found
No related tags found
No related merge requests found
......@@ -11,7 +11,7 @@ class Driver():
Reference to file path to be used by mixins for opening.
"""
def __init__(self, file, num_ephem=909, num_quats=909, props={}):
def __init__(self, file, num_ephem=909, num_quats=909, props={}, parsed_label=None):
"""
Parameters
----------
......@@ -31,6 +31,9 @@ class Driver():
self._num_ephem = num_ephem
self._file = file
if parsed_label:
self._label = parsed_label
@property
def image_lines(self):
"""
......
......@@ -93,11 +93,25 @@ def load(label, props={}, formatter='ale', verbose=False):
drivers = chain.from_iterable(inspect.getmembers(dmod, lambda x: inspect.isclass(x) and "_driver" in x.__module__) for dmod in __driver_modules__)
drivers = sort_drivers([d[1] for d in drivers])
try:
# Try default grammar for pds3 label
parsed_label = parse_label(label)
except ValueError as e:
if verbose:
print(e)
# If pds3 label fails, try isis grammar
parsed_label = parse_label(label, pvl.grammar.ISISGrammar)
except Exception as e:
if verbose:
print(e)
# If both fail, then don't parse the label, and just pass the driver a file.
parsed_label = None
for driver in drivers:
if verbose:
print(f'Trying {driver}')
try:
res = driver(label, props=props)
res = driver(label, props=props, parsed_label=parsed_label)
# get instrument_id to force early failure
res.instrument_id
......@@ -130,3 +144,36 @@ def loads(label, props='', formatter='ale', verbose=False):
"""
res = load(label, props, formatter, verbose=verbose)
return json.dumps(res, cls=AleJsonEncoder)
def parse_label(label, grammar=pvl.grammar.PVLGrammar):
"""
Attempt to parse a PVL label.
Parameters
----------
label
The label as a pvl string or pvl file.
grammar
The pvl grammar with which to parse the label. If None, default to PVLGrammar
Returns
-------
pvl.collections.PVLModule
The PVL label deserialized to a Python object
See Also
--------
load
loads
"""
try:
parsed_label = pvl.loads(label, grammar=grammar)
except Exception:
parsed_label = pvl.load(label, grammar=grammar)
except:
raise ValueError("{} is not a valid label for grammar {}".format(label, grammar.__name__))
return parsed_label
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment