inm_rspace.core module¶
Examples¶
Get all documents in an RSpace Folder or Notebook:¶
import inm_rspace as rs
docs = rs.get_docs_in_notebook(7074)
print([doc['name'] for doc in docs])
Output:
Create a new RSpace Form from a JSON metadata file:¶
json_content = json.load(open('metadata.json'))
form_fields = rs.form_fields_from_json(json_content, default=True)
rs_form = rs.ELN.create_form('inm-rspace_json', fields=form_fields)
print(f"Created form {rs_form['globalId']}")
For more examples on managing JSON schemas and RSpace Forms, refer to this script.
API documentation¶
- class inm_rspace.core.ELNClass[source]¶
Bases:
ELNClientELN class enhancing the rspace_client.eln.ELNClient class
- class inm_rspace.core.InventoryClass[source]¶
Bases:
InventoryClientInventory class enhancing the rspace_client.inv.InventoryClient class
- inm_rspace.core.document_fields_from_json(json_dict: dict)[source]¶
Convert a JSON-style dict with key,value pairs into a list of RSpace document fields of format [{‘name’: <key>, ‘content’: <value>}, …]
- Parameters:
json_dict (dict) – JSON-style dict
- Returns:
list<dict> – RSpace document-style list of {‘name’: …, ‘content’: …} dicts.
- inm_rspace.core.field_index(document, field_name)[source]¶
get index of the (first) field from an Rspace document dict with a given name.
- Parameters:
document (dict) – input document
field_name (str) – name of the field to be accessed
- Returns:
idx (int) – the index of the field with the given name
- inm_rspace.core.fields_are_compatible(fields1, fields2, subset=False)[source]¶
determine whether two field lists are identical (or one is a subset of the other).
- Parameters:
fields1 (dict) – a list of RSpace field dicts containing at least the keys ‘name’ and ‘type’.
fields2 (dict) – a list of RSpace field dicts containing at least the keys ‘name’ and ‘type’.
subset (bool) – if True, the function only checks whether the fields of <form1> are a subset of the fields in <form2>.
- Returns:
match (bool) – True if both forms are compatible, False otherwise.
- inm_rspace.core.fill_form_fields(meta: dict, form_fields: list, term_map={}, strict=False)[source]¶
Create RSpace Document (list of document fields) from a JSON-style metadata dict and an RSpace Form (list of form fields).
- Parameters:
meta (dict) – JSON-style metadata dict
form_fields (list<dict>) – list of Form field dicts of format {‘name’: …, ‘type’: …, (…)}
term_map (dict) – map between terms in the form and in the metadata
strict (bool) – If True, an error will be raised if the metadata dict contains any terms that are present in neither the form_fields nor the term_map. If False, only a warning is printed.
- Returns:
list<dict> – List of RSpace Document fields.
- inm_rspace.core.form_fields_from_json(json_dict: dict, default: bool = False)[source]¶
Convert a JSON-style dict with key,value pairs into a list of RSpace Form fields of format [{‘name’: <key>, ‘type’: type(<value>)}, …]
- Parameters:
json_dict (dict) – JSON-style dict
default (bool) – if True, supplied values in the JSON-style dict are considered to be the default in the RSpace Form.
- Returns:
list<dict> – RSpace Form field list of {‘name’: …, ‘type’: …, (…)} dicts.
- inm_rspace.core.forms_are_compatible(form1, form2, subset=False)[source]¶
determine whether two forms are identical (or one is a subset of the other).
- Parameters:
form1 (dict) – an RSpace Form-style dict containing at least the keys ‘name’ and ‘fields’.
form2 (dict) – an RSpace Form-style dict containing at least the keys ‘name’ and ‘fields’.
subset (bool) – if True, the function only checks whether the fields of <form1> are a subset of the fields in <form2>.
- Returns:
match (bool) – True if both forms are compatible, False otherwise.
- inm_rspace.core.get_docs_in_folder(folder_id, form_pattern=None, verbose=False)[source]¶
scan for Rspace documents in a given folder whose form name matches a pattern
- Parameters:
folder_id (str) – folderID of the Rspace folder to search for matches
form_pattern (str) – glob-style pattern that the form name must match
- Returns:
results (list<dict>) – list of documents matching the form name
- inm_rspace.core.get_docs_in_notebook(notebook_id, form_pattern=None, verbose=False)[source]¶
scan for Rspace documents in a given folder whose form name matches a pattern
- Parameters:
notebook_id (str) – notebookID of the Rspace notebook to search for matches
form_pattern (str) – glob-style pattern that the form name must match
- Returns:
results (list<dict>) – list of documents matching the form name
- inm_rspace.core.get_field(document, field_name)[source]¶
get (the first) field from an Rspace document dict with a given name.
- Parameters:
document (dict) – input document
field_name (str) – name of the field to be accessed
- Returns:
field (dict) – field with the given name
- inm_rspace.core.get_files(document, field_key=None)[source]¶
list files attached to (a field in) an Rspace document
- Parameters:
document (RspaceDocument) – input document
field_key (int or str, optional) – name of the field, from which files are extracted. If None, files from all fields are listed.
- Returns:
files (list<tuple<str,str>>) – files found as globalId,filename-pairs.
- inm_rspace.core.get_form_by_dict(new_form, subset=False)[source]¶
If it exists, return the (first) Rspace Form matching a given RSpace Form definition dict. Otherwise, create this Form and return it.
- Parameters:
new_form (dict) – a dict corresponding to an RSpace Form definition containing at least the keys ‘name’ and ‘fields’.
subset (bool) – if True, the function only checks whether the fields of <new_form> are a subset of the fields in an existing RSpace Form.
- Returns:
rs_form (dict) – the found/newly created RSpace form.
- inm_rspace.core.get_requests(shared_folder_id, verbose=False)[source]¶
get all shared documents requesting a workflow to be performed
- Parameters:
shared_folder_id (str) – folderId of the “Shared” Folder in Rspace
- Returns:
results (list<dict>) – list of shared Rspace documents using a Request:* form
- inm_rspace.core.html_ref(rspace_obj)[source]¶
html string to reference an Rspace object
- Parameters:
rspace_obj (file or document) – Rspace object
- Returns:
string (str) – string that can be inserted in an html string to reference the given object.
- Raises:
ValueError – raised if the rspace object type is not recognized
- inm_rspace.core.tables_from_xml(xml_string, file, delimiter=',', replace={' ': '_', ',': '.', '</p>': '', '<p>': ''})[source]¶
extract all tabular data from an xml string and save it as a csv file.
- Parameters:
xml_string (str) – input xml string
file (str) – file path to use, although an appendix is going to be inserted to enumerate multiple tables in the xml string.
delimiter (str, optional) – field delimiter to be used in the csv file
replace (dict, optional) – key,value pairs indicating strings (keys) to be replaced with their corresponding value.
- Returns:
files (list<str>) – List of files exported.