obsinfo.misc package
obsinfo.misc.configuration module
obsinfo.misc.const module
Exit values as constants as per UNIX BSD standard
obsinfo.misc.discoveryfiles module
- class obsinfo.misc.discoveryfiles.Datapath(datapath=None)
Bases:
object
Class to discover where information files are stored.
- datapath_list
directories where information files will be searched, in sequence
- Type
list of str
- infofiles_path
same as datapath_list, used by validate, kept for compatibility
- Type
list of str
- validate_path
one unique path to validation schemas
- Type
str
- static add_frag(path, frag)
Add the path and the frag to restore a partial or complete uri
- Parameters
path (str) – path portion of uri, possibly with other elements but without frag
frag (str) – fragment portion of uri
- Returns
(str)L path with frag
- build_datapath(file)
Create list of directories which may have data or schemas
If the file path is absolute, return the file itself.
If path starts by ./ or ../ complete to an absolute path using working directory
If the file has no prefix discover whether file exists in the datapath list. Use the first found file.
- Parameters
file (str or path) – filename of file to be found
- Returns
found file as string
- Raises
FileNotFoundError –
obsinfo.misc.printobs module
Functions to print obsinfo objects
- class obsinfo.misc.printobs.PrintObs
Bases:
object
Collection of methods to print obsinfo objects at different levels of depth. All methods are static.
- \*None*
- static print_component(obj, level='all')
Prints comoponent information and continues if level is not “component” or “channel”.
If level is not “channel” detailed equipment information is not printed.
- Parameters
level (str) – determines to which level the obsinfo object will be printed
- static print_instrumentation(obj, level='all')
Prints instrumentation information and continues if level is not “instrumentation”.
If level is “response” or “all” response stages information will be printed (if it exists). Recall at this point all the response is gathered in
instrument.stages.sgates
If level is “all” or “filter” (i.e. not “response”) all information will be printed (only filter is left at this point…)- Parameters
level (str) – determines to which level the obsinfo object will be printed
- static print_network(obj, level='all')
Prints network information and continues if level is not “network”.
- Parameters
level (str) – determines to which level the obsinfo object will be printed
- static print_station(obj, level='all')
Prints station information and continues if level is not “station”.
- Parameters
level (str) – determines to which level the obsinfo object will be printed
obsinfo.misc.remoteGitLab module
- class obsinfo.misc.remoteGitLab.gitLabFile
Bases:
object
Provide the methods to use the gitlab API to read a remote file and decode it
- \*None*
- static get_gitlab_file(uri, read_file=True)
Get the file content pointed at by uri and decode it.
Uses b64 first to get the remote file and convert to a byte string, and then utf-8 to convert to regular string.
- Parameters
uri (string or path-like) – uri to read
(bool) (read_file) – If true, reads the content. If not, simply checks if the file exists
- Returns
read content
- Return type
(str)
Raises: FileNotFoundError, ValueError
- static isRemote(file)
Checks if scheme means file is remote.
- Parameters
file (str) – filename to be checked, with complete uri
- Returns
boolean. True if remote, False otherwise
obsinfo.misc.yamlref module
Module to read and parse YAML or JSON files, locally or remotely (gitlab only)
jsonref with YAML reading added.
copied directly from jsonref v0.2, with added routines _yaml_load and _yaml_loads replacing json.load and json.loads Added/modified lines are marked “# WCC”
- class obsinfo.misc.yamlref.JsonLoader(store=(), cache_results=True)
Bases:
object
Provides a callable which takes a URI, and returns the loaded JSON referred to by that URI. Uses
requests
if available for HTTP URIs, and falls back tourllib
. By default it keeps a cache of previously loaded documents.Attributes:
- store: pre-populated dictionary matching URIs to loaded JSON
documents used as cache
cache_results (boolean): if this is set to false, the internal cache of
loaded JSON documents is not used
- __init__(store=(), cache_results=True)
- get_json_or_yaml(uri, **kwargs)
Open either a local file, if uri scheme is
file
or a remote one, calling a gitlab method which implements the gitlab API (version 4)- Parameters
uri (path-like object, string or byte string) – The URI of the JSON or YAML document to load
kwargs (dict) – Keyword arguments passed to
json.loads()
- Returns
dictionary of parsed YAML or JSON formats
- Raises
FileNotFoundError, IOError, OSError
- class obsinfo.misc.yamlref.JsonRef(refobj, base_uri='', loader=None, jsonschema=False, load_on_repr=True, _path=(), _store=None, datapath=None)
Bases:
proxytypes.LazyProxy
A lazy loading proxy to the dereferenced data pointed to by a JSON Reference object.
Attributes:
__reference__: dictionary object referenced to by a
$ref
base_uri: object of type
Path
which is used to build the full uriloader: a loader object (a callable) such as
JsonLoader
, to load a JSON or YAML file/stringjsonschema = Flag to turn on
JSON Schema
mode- load_on_repr = If set to
False
,repr()
call on a JsonRef
object will not cause the reference to be loaded if it hasn’t already. (defaults toTrue
)
- load_on_repr = If set to
path = list of string keywords: keywords of different $ref in lists or dictionaries
store = dictionary of cached objects used to prevent reading files over again
datapath = object of
Datapath
, stores directories to search for files
- __init__(refobj, base_uri='', loader=None, jsonschema=False, load_on_repr=True, _path=(), _store=None, datapath=None)
- callback()
Callback from proxytypes,
LazyProxy
.Resolves the pointer (part of the dictionary read from the info file) that is incorporated instead of
$ref
. Updatesbase_uri
- Returns
- the fragment portion of the base_doc, which has already had its
$ref
replaced.
- property full_uri
This method/property returns the full uri to reference a
$ref
object. It’s the heart of how a datapath is used to either access a local or remote (gitlab) file. All schema files are supposed to be local, part of the obsinfo distribution- Returns
updated full uri
- Raises
ValueError
- classmethod replace_refs(obj, _recursive=False, **kwargs)
Returns a deep copy of
obj
with all contained JSON reference objects replaced withJsonRef
instances.- Parameters
obj (JSONRef or collection`) – If a JSON reference object, a
JsonRef
instance will be created. If not, a deep copy of it will be created with all contained JSON reference objects replaced byJsonRef
instancesrecursive (bool) – Process
$ref
recursivelykwargs (dict) – Keyword arguments passed to
json.loads()
- Returns
the information in
$ref
file- Return type
obj ():class:JsonRef)
- Raises
TypeError, ValueError through JsonRef object creation –
kwargs
include:- base_uri (
Path
): URI to resolve relative references against. Can be remote (https://) or local(file://) This is how datapath is implemented
- datapath (
Datapath
): object to implement file discovery in a list of directories
- loader (loader object such as
JsonLoader
): Callable that takes a URI and returns the parsed JSON (defaults to global
jsonloader
, aJsonLoader
instance)- jsonschema (bool): Flag to turn on JSON Schema mode,
which means the file is a schema file. This makes ‘id’ keyword to change the
base_uri
for references contained within the object, such as $ref: ‘#/definitions’- load_on_repr (bool): If set to
False
,repr()
call on a JsonRef
object will not cause the reference to be loaded if it hasn’t already. (defaults toTrue
)
- base_uri (
- resolve_pointer(document, pointer)
Resolve a json pointer
pointer
within the referenceddocument
.- Parameters
document – the referent document
pointer (str) – a json pointer URI fragment to resolve within it
- Returns
part of document dictionary pointed at by pointer
- exception obsinfo.misc.yamlref.JsonRefError(message, reference, uri='', base_uri='', path=(), cause=None)
Bases:
Exception
Create exception for JSONRef
Attributes:
message (str): message to print with exception
reference (str): reference where exception occurred
uri (str or path-like): uri of file being processed
base_uri: (str or path-like): base_uri (complement) of file being processed
path = list of string keywords: keywords of different $ref in lists or dictionaries
cause (str): cause of exception
- __init__(message, reference, uri='', base_uri='', path=(), cause=None)
- obsinfo.misc.yamlref.dump(obj, fp, **kwargs)
Serialize
obj
as a JSON formatted stream to file-likefp
JsonRef
objects will be dumped as the original reference object they were created from.- Parameters
obj – Object to serialize
fp (File-like object) –
kwargs (dict) – Keyword arguments for
json.dumps()
- obsinfo.misc.yamlref.dumps(obj, **kwargs)
Serialize
obj
, which may containJsonRef
objects, to a JSON formatted string.JsonRef
objects will be dumped as the original reference object they were created from.- Parameters
obj – Object to serialize
kwargs (dict) – Keyword arguments for
json.dumps()
- Returns
dumped string
- obsinfo.misc.yamlref.load(fp, base_uri='', loader=None, jsonschema=False, load_on_repr=True, datapath=None, **kwargs)
Drop in replacement for
json.load()
, where JSON references are proxied to their referent data.The difference between load and loads is that the first uses a file-like object and the second a string.
- Parameters
fp (File-like object) – File-like object containing JSON document
base_uri (object of type
Path
) – URI to resolve relative references against. Can be remote (https://) or local(file://) This is how datapath is implementeddatapath (object of type
Datapath
) – object to implement file discovery in a list of directoriesloader (a loader object such as
JsonLoader
) – Callable that takes a URI and returns the parsed JSON (defaults to globaljsonloader
, aJsonLoader
instance)jsonschema (boolean) –
Flag to turn on JSON Schema mode, which means the file is a schema file. This makes ‘id’ keyword to change the
base_uri
for references contained within the object, such as $ref: ‘#/definitions’load_on_repr (boolean) – If set to
False
,repr()
call on a:class:JsonRef object will not cause the reference to be loaded if it hasn’t already. (defaults toTrue
)kwargs (dict) – This function takes any of the keyword arguments from
JsonRef.replace_refs()
. Any other keyword arguments will be passed to_yaml_load()
- Returns
dictionary of parsed YAML or JSON formats
- obsinfo.misc.yamlref.load_uri(uri, base_uri=None, datapath=None, loader=None, jsonschema=False, load_on_repr=True)
- Load JSON data from
uri
instead of file-like object or string. with JSON references proxied to their referent data. Not used in obsinfo.
- Parameters
uri (string or path-like object) – URI to fetch the JSON from
base_uri (
Path
) – URI to resolve relative references against. Can be remote (https://) or local(file://) This is how datapath is implementeddatapath (
Datapath
) – object to implement file discovery in a list of directoriesloader (loader object such as
JsonLoader
) – Callable that takes a URI and returns the parsed JSON (defaults to globaljsonloader
, aJsonLoader
instance)jsonschema (bool) –
Flag to turn on JSON Schema mode, which means the file is a schema file. This makes ‘id’ keyword to change the
base_uri
for references contained within the object, such as $ref: ‘#/definitions’load_on_repr (bool) – If set to
False
,repr()
call on a class:JsonRef object will not cause the reference to be loaded if it hasn’t already. (defaults toTrue
)
- Returns
parsed YAML or JSON formats
- Return type
newref (dict)
- Load JSON data from
- obsinfo.misc.yamlref.loads(s, base_uri='', loader=None, jsonschema=False, load_on_repr=True, datapath=None, recursive=True, **kwargs)
Drop in replacement for
json.loads()
, where JSON references are proxied to their referent data.The difference between load and loads is that the first uses a file-like object and the second a string.
- Parameters
s (str) – Input JSON document
base_uri (
Path
) – URI to resolve relative references against. Can be remote (https://) or local(file://) This is how datapath is implementeddatapath (
Datapath
) – object to implement file discovery in a list of directoriesloader (loader object such as
JsonLoader
) – Callable that takes a URI and returns the parsed JSON (defaults to globaljsonloader
, aJsonLoader
instance)jsonschema (bool) –
Flag to turn on JSON Schema mode, which means the file is a schema file. This makes ‘id’ keyword to change the
base_uri
for references contained within the object, such as $ref: ‘#/definitions’load_on_repr (bool) – If set to
False
,repr()
call on aJsonRef
object will not cause the reference to be loaded if it hasn’t already. (defaults toTrue
)kwargs (dict) – Any of the keyword arguments from
JsonRef.replace_refs()
. Any other keyword arguments will be passed to_yaml_load()
- Returns
decoded JSON or YAML
- Return type
dic (dict)