jsonextended.plugins module

jsonextended.plugins.decode(dct, intype='json', raise_error=False)[source]

decode dict objects, via decoder plugins, to new type

Parameters:
  • intype (str) – use decoder method from_<intype> to encode
  • raise_error (bool) – if True, raise ValueError if no suitable plugin found

Examples

>>> load_builtin_plugins('decoders')
[]
>>> from decimal import Decimal
>>> decode({'_python_Decimal_':'1.3425345'})
Decimal('1.3425345')
>>> unload_all_plugins()
jsonextended.plugins.encode(obj, outtype='json', raise_error=False)[source]

encode objects, via encoder plugins, to new types

Parameters:
  • outtype (str) – use encoder method to_<outtype> to encode
  • raise_error (bool) – if True, raise ValueError if no suitable plugin found

Examples

>>> load_builtin_plugins('encoders')
[]
>>> from decimal import Decimal
>>> encode(Decimal('1.3425345'))
{'_python_Decimal_': '1.3425345'}
>>> encode(Decimal('1.3425345'),outtype='str')
'1.3425345'
>>> encode(set([1,2,3,4,4]))
{'_python_set_': [1, 2, 3, 4]}
>>> encode(set([1,2,3,4,4]),outtype='str')
'{1, 2, 3, 4}'
>>> unload_all_plugins()
jsonextended.plugins.get_plugins(category)[source]

get plugins for category

jsonextended.plugins.load_builtin_plugins(category=None, overwrite=False)[source]

load plugins from builtin directories

Parameters:
  • name (None or str) –
  • category (None or str) – if str, apply for single plugin category

Examples

>>> from pprint import pprint
>>> pprint(view_plugins())
{'decoders': {}, 'encoders': {}, 'parsers': {}}
>>> errors = load_builtin_plugins()
>>> errors
[]
>>> pprint(view_plugins(),width=200)
{'decoders': {'decimal.Decimal': 'encode/decode Decimal type',
              'fractions.Fraction': 'encode/decode Fraction type',
              'numpy.ndarray': 'encode/decode numpy.ndarray',
              'pint.Quantity': 'encode/decode pint.Quantity object',
              'python.set': 'decode/encode python set'},
 'encoders': {'decimal.Decimal': 'encode/decode Decimal type',
              'fractions.Fraction': 'encode/decode Fraction type',
              'numpy.ndarray': 'encode/decode numpy.ndarray',
              'pint.Quantity': 'encode/decode pint.Quantity object',
              'python.set': 'decode/encode python set'},
 'parsers': {'csv.basic': 'read *.csv delimited file with headers to {header:[column_values]}',
             'csv.literal': 'read *.literal.csv delimited files with headers to {header:column_values}, with number strings converted to int/float',
             'hdf5.read': 'read *.hdf5 (in read mode) files using h5py',
             'ipynb': 'read Jupyter Notebooks',
             'json.basic': 'read *.json files using json.load',
             'keypair': "read *.keypair, where each line should be; '<key> <pair>'",
             'yaml.ruamel': 'read *.yaml files using ruamel.yaml'}}
>>> unload_all_plugins()
jsonextended.plugins.load_plugin_classes(classes, category=None, overwrite=False)[source]

load plugins from class objects

Parameters:
  • classes (list) – list of classes
  • category (None or str) – if str, apply for single plugin category
  • overwrite (bool) – if True, allow existing plugins to be overwritten

Examples

>>> from pprint import pprint
>>> pprint(view_plugins())
{'decoders': {}, 'encoders': {}, 'parsers': {}}
>>> class DecoderPlugin(object):
...     plugin_name = 'example'
...     plugin_descript = 'a decoder for dicts containing _example_ key'
...     dict_signature = ('_example_',)
...
>>> errors = load_plugin_classes([DecoderPlugin])
>>> pprint(view_plugins())
{'decoders': {'example': 'a decoder for dicts containing _example_ key'},
 'encoders': {},
 'parsers': {}}
>>> unload_all_plugins()
jsonextended.plugins.load_plugins_dir(path, category=None, overwrite=False)[source]

load plugins from a directory

Parameters:
  • path (str or path_like) –
  • category (None or str) – if str, apply for single plugin category
  • overwrite (bool) – if True, allow existing plugins to be overwritten
jsonextended.plugins.load_source(modname, fname)[source]
jsonextended.plugins.parse(fpath, **kwargs)[source]

parse file contents, via parser plugins, to dict like object NB: the longest file regex will be used from plugins

Parameters:
  • fpath (file_like) – string, object with ‘open’ and ‘name’ attributes, or object with ‘readline’ and ‘name’ attributes
  • kwargs – to pass to parser plugin

Examples

>>> load_builtin_plugins('parsers')
[]
>>> from pprint import pformat
>>> json_file = StringIO('{"a":[1,2,3.4]}')
>>> json_file.name = 'test.json'
>>> dct = parse(json_file)
>>> print(pformat(dct).replace("u'","'"))
{'a': [1, 2, 3.4]}
>>> reset = json_file.seek(0)
>>> from decimal import Decimal
>>> dct = parse(json_file, parse_float=Decimal,other=1)
>>> print(pformat(dct).replace("u'","'"))
{'a': [1, 2, Decimal('3.4')]}
>>> class NewParser(object):
...     plugin_name = 'example'
...     plugin_descript = 'loads test.json files'
...     file_regex = 'test.json'
...     def read_file(self, file_obj, **kwargs):
...         return {'example':1}
>>> load_plugin_classes([NewParser],'parsers')
[]
>>> reset = json_file.seek(0)
>>> parse(json_file)
{'example': 1}
>>> unload_all_plugins()
jsonextended.plugins.parser_available(fpath)[source]

test if parser plugin available for fpath

Examples

>>> load_builtin_plugins('parsers')
[]
>>> test_file = StringIO('{"a":[1,2,3.4]}')
>>> test_file.name = 'test.json'
>>> parser_available(test_file)
True
>>> test_file.name = 'test.other'
>>> parser_available(test_file)
False
>>> unload_all_plugins()
jsonextended.plugins.plugins_context(classes, category=None)[source]

context manager to load plugin class(es) then unload on exit

Parameters:
  • classes (list) – list of classes
  • category (None or str) – if str, apply for single plugin category

Examples

>>> from pprint import pprint
>>> class DecoderPlugin(object):
...     plugin_name = 'example'
...     plugin_descript = 'a decoder for dicts containing _example_ key'
...     dict_signature = ('_example_',)
...
>>> with plugins_context([DecoderPlugin]):
...     pprint(view_plugins())
{'decoders': {'example': 'a decoder for dicts containing _example_ key'},
 'encoders': {},
 'parsers': {}}
>>> pprint(view_plugins())
{'decoders': {}, 'encoders': {}, 'parsers': {}}
jsonextended.plugins.unload_all_plugins(category=None)[source]

clear all plugins

Parameters:category (None or str) – if str, apply for single plugin category

Examples

>>> from pprint import pprint
>>> pprint(view_plugins())
{'decoders': {}, 'encoders': {}, 'parsers': {}}
>>> class DecoderPlugin(object):
...     plugin_name = 'example'
...     plugin_descript = 'a decoder for dicts containing _example_ key'
...     dict_signature = ('_example_',)
...
>>> errors = load_plugin_classes([DecoderPlugin])
>>> pprint(view_plugins())
{'decoders': {'example': 'a decoder for dicts containing _example_ key'},
 'encoders': {},
 'parsers': {}}
>>> unload_all_plugins()
>>> pprint(view_plugins())
{'decoders': {}, 'encoders': {}, 'parsers': {}}
jsonextended.plugins.unload_plugin(name, category=None)[source]

remove single plugin

Parameters:
  • name (str) – plugin name
  • category (str) – plugin category

Examples

>>> from pprint import pprint
>>> pprint(view_plugins())
{'decoders': {}, 'encoders': {}, 'parsers': {}}
>>> class DecoderPlugin(object):
...     plugin_name = 'example'
...     plugin_descript = 'a decoder for dicts containing _example_ key'
...     dict_signature = ('_example_',)
...
>>> errors = load_plugin_classes([DecoderPlugin],category='decoders')
>>> pprint(view_plugins())
{'decoders': {'example': 'a decoder for dicts containing _example_ key'},
 'encoders': {},
 'parsers': {}}
>>> unload_plugin('example','decoders')
>>> pprint(view_plugins())
{'decoders': {}, 'encoders': {}, 'parsers': {}}
jsonextended.plugins.view_interfaces(category=None)[source]

return a view of the plugin minimal class attribute interface(s)

Parameters:category (None or str) – if str, apply for single plugin category

Examples

>>> from pprint import pprint
>>> pprint(view_interfaces())
{'decoders': ['plugin_name', 'plugin_descript', 'dict_signature'],
 'encoders': ['plugin_name', 'plugin_descript', 'objclass'],
 'parsers': ['plugin_name', 'plugin_descript', 'file_regex', 'read_file']}
jsonextended.plugins.view_plugins(category=None)[source]

return a view of the loaded plugin names and descriptions

Parameters:category (None or str) – if str, apply for single plugin category

Examples

>>> from pprint import pprint
>>> pprint(view_plugins())
{'decoders': {}, 'encoders': {}, 'parsers': {}}
>>> class DecoderPlugin(object):
...     plugin_name = 'example'
...     plugin_descript = 'a decoder for dicts containing _example_ key'
...     dict_signature = ('_example_',)
...
>>> errors = load_plugin_classes([DecoderPlugin])
>>> pprint(view_plugins())
{'decoders': {'example': 'a decoder for dicts containing _example_ key'},
 'encoders': {},
 'parsers': {}}
>>> view_plugins('decoders')
{'example': 'a decoder for dicts containing _example_ key'}
>>> unload_all_plugins()