def _main(): """Main entry point. """ doc = pyesdoc.read(options.file, options.encoding) print pyesdoc.encode(doc, pyesdoc.ENCODING_JSON)
def _main(): """Main entry point. """ doc = pyesdoc.read(options.file, options.encoding) print pyesdoc.encode(doc, 'json')
def _encode_docs(ctx): """Encodes documents loaded from pyesdoc archive. """ if ctx.encoding != pyesdoc.constants.ENCODING_JSON: ctx.docs = pyesdoc.encode(ctx.docs, ctx.encoding) else: ctx.docs = pyesdoc.encode(ctx.docs, pyesdoc.constants.ENCODING_DICT)
def _get_content(i, s, settings): """Generates a CIM document for a CMIP6 institute | source combination. """ # Set JSON content accessors. accessors = _get_data_accessors(i, s, settings) # Map JSON -> CIM. doc = _map_model(i, s, accessors) if doc is None: return # Destructure injected properties. _destructure(doc) # Emit validation report. errors = pyesdoc.validate(doc) errors = [ e for e in errors if e.endswith('values --> is an empty list') == False ] if errors: print("INVALID CIM DOCUMENT: {}".format(s)) for err in errors: print(err) # Return JSON string. return pyesdoc.encode(doc)
def _write(doc): """Writes a document html file. """ with open(_get_file_path(doc), 'w') as op_file: doc = pyesdoc.encode(doc, pyesdoc.ENCODING_HTML) doc = _TEMPLATE.generate(body=doc) op_file.write(doc)
def _write_file(mod, doc, encoding): """Writes test file to file system. """ fpath = _get_file_path(mod, encoding) with open(fpath, 'w') as op_file: encoded = pyesdoc.encode(doc, encoding) op_file.write(encoded)
def _do_test(encoding): """Executes a serialization test.""" for desc in (_get_description(t) for t in (str, unicode)): _MODEL.description = desc representation = pyesdoc.encode(_MODEL, encoding) tu.assert_object(representation, str) as_doc = pyesdoc.decode(representation, encoding) tu.assert_object(as_doc, cim.v1.ModelComponent)
def _set_output(): """Sets output data to be returned to client. """ if self.doc is None: self.set_status(404) else: self.output_encoding = self.encoding self.output = pyesdoc.encode(self.doc, self.encoding)
def get_content(self, encoding): """Returns content of an archived file in the desired encoding. :param str encoding: Desired document content encoding. :returns: The contents of an archived file in the desired encoding. :rtype: unicode """ return pyesdoc.encode(self.get_document(), encoding)
def encode(doc, encoding, doc_type=None): """Encode a document.""" doc = pyesdoc.encode(doc, encoding) # Perform basic assertions. assert doc is not None if doc_type is not None: assert isinstance(doc, doc_type) if isinstance(doc_type, str): doc.decode('utf-8') return doc
def _test_retrieve(encoding): """Tests retrieving a specific document encoding.""" params = _URL_GET_PARAMS.format(encoding, _DOC.meta.id, _DOC.meta.version) url = "{}{}".format(_URL_GET, params) response = requests.get(url) assert response.status_code == 200 if encoding != 'html': doc = pyesdoc.decode(response.text, encoding) assert doc.meta.id == _DOC.meta.id assert doc.meta.version == _DOC.meta.version if pyesdoc.encode(_DOC, encoding) != response.text: pass
def test_publish(): """ES-DOC :: WS :: Test publishing a document. """ # Create a document for testing. _set_test_document() # Post document to web-service. data = pyesdoc.encode(_DOC, 'json') headers = {'Content-Type': 'application/json'} url = _URL_POST response = requests.post(url, data=data, headers=headers) assert response.status_code == 200
def write(document, fpath, encoding=pyesdoc.constants.ENCODING_JSON): """Writes a document to the file system. :param object document: A pyesdoc document instance. :param str fpath: Path to I/O directory or file. :param str encoding: Document encoding. :returns: Path to created file. :rtype: str """ if os.path.isdir(fpath): fpath = _get_filepath(fpath, document, encoding) with open(fpath, "w") as fstream: fstream.write(pyesdoc.encode(document, encoding)) return fpath
def write(document, fpath, encoding=constants.ENCODING_JSON): """Writes a document to the file system. :param object document: A pyesdoc document instance. :param str fpath: Path to I/O directory or file. :param str encoding: Document encoding. :returns: Path to created file. :rtype: str """ if os.path.isdir(fpath): fpath = _get_filepath(fpath, document, encoding) fpath = os.path.expanduser(fpath) with open(fpath, 'w') as fstream: fstream.write(pyesdoc.encode(document, encoding)) return fpath
def write(document, io_dir, encoding=pyesdoc.constants.ENCODING_JSON): """Writes a document to the file system. :param object document: A pyesdoc document instance. :param str io_dir: I/O directory path. :param str encoding: Document encoding. :returns: Path to created file. :rtype: str """ # Validate I/O directory. if not os.path.isdir(io_dir): raise ValueError("Output directory does not exist.") fpath = _get_filepath(io_dir, document, encoding) with open(fpath, 'w') as fstream: fstream.write(pyesdoc.encode(document, encoding)) return fpath
def _get_content(i, s, settings): """Generates a CIM document for a CMIP6 institute | source combination. """ doc = _map_model(i, s, _get_data_accessors(i, s, settings)) if doc is None: return # Destructure injected properties. _destructure(doc) # Emit validation report. errors = pyesdoc.validate(doc) errors = [e for e in errors if e.endswith('values --> is an empty list') == False] if errors: print "INVALID CIM DOCUMENT:", s for err in errors: print err # Return JSON string. return pyesdoc.encode(doc)
def write(self, doc): """Writes a document to the archive. :param object doc: Document to be written. :returns: Pointer to archive file. :rtype: pyesdoc.archive.ArchiveFileInfo """ # Ensure document is extended. extend_doc(doc) # Set file path. fname = "{}_{}_{}.{}".format(doc.meta.type, doc.meta.id, doc.meta.version, DEFAULT_ENCODING) fpath = os.path.join(self.path, fname) # Write to file system. with open(fpath, 'w') as fstream: fstream.write(pyesdoc.encode(doc, DEFAULT_ENCODING)) # Return file wrapper. return ArchiveFileInfo(self, fpath)
def test_republish(): """ES-DOC :: WS :: Test republishing a document. """ _DOC.rationale = "to restate the bleeding obvious" _DOC.meta.version += 1 data = pyesdoc.encode(_DOC, 'json') headers = {'Content-Type': 'application/json'} url = _URL_PUT response = requests.put(url, data=data, headers=headers) assert response.status_code == 200 params = _URL_GET_PARAMS.format('json', _DOC.meta.id, _DOC.meta.version) url = "{}{}".format(_URL_GET, params) response = requests.get(url) assert response.status_code == 200 doc = pyesdoc.decode(response.text, 'json') assert doc.meta.id == _DOC.meta.id assert doc.meta.version == _DOC.meta.version assert doc.rationale == "to restate the bleeding obvious"
def _write_file(mod, doc, encoding): """Writes test file to file system.""" encoded = pyesdoc.encode(doc, encoding) with open(_get_file_name(mod, encoding), 'w') as op_file: op_file.write(encoded)
for index, input_dict in enumerate(inputs): # Return machine doc with applicable models and experiments: cim_out, apply_models_out, appl_exp_out = generate_outputs( input_dict, two_c_pools=two_c_pools[index], two_s_pools=two_s_pools[index], docs_given=has_docs[index]) # Validate the CIM document - there should not be any errors if pyesdoc.is_valid(cim_out): print("Complete: machine CIM document generated and is valid.") else: print("Machine CIM document generated is not valid:") for err in pyesdoc.validate(cim_out): print(err) # Test serialisation of the machine doc... j = pyesdoc.encode(cim_out, pyesdoc.constants.ENCODING_JSON) assert json.loads(j) assert isinstance(pyesdoc.decode(j, pyesdoc.constants.ENCODING_JSON), cim.Machine) x = pyesdoc.encode(cim_out, pyesdoc.constants.ENCODING_XML) assert isinstance(pyesdoc.decode(x, pyesdoc.constants.ENCODING_XML), cim.Machine) # CIM document is valid and can be encoded correctly, so ready to # store it in the specified location as JSON: pyesdoc.write(cim_out, CIM_OUT_PATH, encoding=encoding) print("Machine CIM document successfully written to filesystem.")
def _write(doc): """Writes a document html file.""" as_html = pyesdoc.encode(doc, pyesdoc.ESDOC_ENCODING_HTML) with open(_get_file_name(doc), 'w') as op_file: op_file.write(_TEMPLATE.generate(body=as_html))