mirror of
https://github.com/iluvcapra/wavinfo.git
synced 2025-12-31 08:50:41 +00:00
79 lines
2.3 KiB
Python
79 lines
2.3 KiB
Python
import datetime
|
|
from . import WavInfoReader
|
|
from . import __version__
|
|
|
|
from optparse import OptionParser
|
|
import sys
|
|
import json
|
|
from enum import Enum
|
|
from base64 import b64encode
|
|
|
|
|
|
class MyJSONEncoder(json.JSONEncoder):
|
|
def default(self, o):
|
|
if isinstance(o, Enum):
|
|
return o._name_
|
|
elif isinstance(o, bytes):
|
|
return 'base64:' + b64encode(o).decode('ascii')
|
|
else:
|
|
return super().default(o)
|
|
|
|
|
|
class MissingDataError(RuntimeError):
|
|
pass
|
|
|
|
|
|
def main():
|
|
parser = OptionParser()
|
|
|
|
parser.usage = 'wavinfo (--adm | --ixml) <FILE> +'
|
|
|
|
parser.add_option('--adm', dest='adm',
|
|
help='Output ADM XML',
|
|
default=False,
|
|
action='store_true')
|
|
|
|
parser.add_option('--ixml', dest='ixml',
|
|
help='Output iXML',
|
|
default=False,
|
|
action='store_true')
|
|
|
|
(options, args) = parser.parse_args(sys.argv)
|
|
for arg in args[1:]:
|
|
try:
|
|
this_file = WavInfoReader(path=arg)
|
|
if options.adm:
|
|
if this_file.adm:
|
|
sys.stdout.write(this_file.adm.xml_str())
|
|
else:
|
|
raise MissingDataError("adm")
|
|
elif options.ixml:
|
|
if this_file.ixml:
|
|
sys.stdout.write(this_file.ixml.xml_str())
|
|
else:
|
|
raise MissingDataError("ixml")
|
|
else:
|
|
ret_dict = {
|
|
'filename': arg,
|
|
'run_date': datetime.datetime.now().isoformat(),
|
|
'application': "wavinfo " + __version__,
|
|
'scopes': {}
|
|
}
|
|
for scope, name, value in this_file.walk():
|
|
if scope not in ret_dict['scopes'].keys():
|
|
ret_dict['scopes'][scope] = {}
|
|
|
|
ret_dict['scopes'][scope][name] = value
|
|
|
|
json.dump(ret_dict, cls=MyJSONEncoder, fp=sys.stdout, indent=2)
|
|
except MissingDataError as e:
|
|
print("MissingDataError: Missing metadata (%s) in file %s" %
|
|
(e, arg), file=sys.stderr)
|
|
continue
|
|
except Exception as e:
|
|
raise e
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|