def write(self, format='rdfxml', stream=None): """ This convenience method will write out all of the graphs associated with the source. Right now these are hardcoded to be a single "graph" and a "dataset". If you do not supply stream='stdout' it will default write these to files :return: None """ format_to_xtn = { 'rdfxml': 'xml', 'turtle': 'ttl' } # make the regular graph output file file = None if self.name is not None: file = '/'.join((self.outdir, self.name)) if format in format_to_xtn: file = '.'.join((file, format_to_xtn.get(format))) else: file = '.'.join((file, format)) # make the datasetfile name datasetfile = '/'.join((self.outdir, self.name+'_dataset')) if format in format_to_xtn: datasetfile = '.'.join((datasetfile, format_to_xtn.get(format))) else: datasetfile = '.'.join((datasetfile, format)) else: logger.warn("No output file set. Using stdout") stream = 'stdout' # start off with only the dataset descriptions graphs = [ {'g': self.dataset.getGraph(), 'file': datasetfile}, ] # add the other graphs to the set to write, if not in the test mode if self.testMode: graphs += [{'g': self.testgraph, 'file': self.testfile}] else: graphs += [{'g': self.graph, 'file': file}] gu = GraphUtils(None) # loop through each of the graphs and print them out for g in graphs: if stream is None: gu.write(g['g'], format, file=g['file']) elif stream.lowercase().strip() == 'stdout': gu.write(g['g'], format) else: logger.error("I don't understand your stream.") return
def write(self, fmt='turtle', stream=None, write_metadata_in_main_graph=False): """ This convenience method will write out all of the graphs associated with the source. Right now these are hardcoded to be a single main "graph" and a "src_dataset.ttl" and a "src_test.ttl" If you do not supply stream='stdout' it will default write these to files. In addition, if the version number isn't yet set in the dataset, it will be set to the date on file. :return: None """ fmt_ext = { 'rdfxml': 'xml', 'turtle': 'ttl', 'nt': 'nt', # ntriples 'nquads': 'nq', 'n3': 'n3' # notation3 } # make the regular graph output file dest = None if self.name is not None: dest = '/'.join((self.outdir, self.name)) if fmt in fmt_ext: dest = '.'.join((dest, fmt_ext.get(fmt))) else: dest = '.'.join((dest, fmt)) LOG.info("Setting outfile to %s", dest) # make the dataset_file name, always format as turtle self.datasetfile = '/'.join( (self.outdir, self.name + '_dataset.ttl')) LOG.info("Setting dataset file to %s", self.datasetfile) else: LOG.warning("No output file set. Using stdout") stream = 'stdout' graph_util = GraphUtils(None) # the _dataset description is always turtle graph_util.write(self.dataset.get_graph(), 'turtle', filename=self.datasetfile) if self.test_mode: # unless we stop hardcoding, the test dataset is always turtle LOG.info("Setting testfile to %s", self.testfile) graph_util.write(self.testgraph, 'turtle', filename=self.testfile) if write_metadata_in_main_graph: self.graph = self.graph + self.dataset.get_graph() # print graph out if stream is None: outfile = dest elif stream.lower().strip() == 'stdout': outfile = None else: LOG.error("I don't understand our stream.") return graph_util.write(self.graph, fmt, filename=outfile)
def write(self, fmt='turtle', stream=None): """ This convenience method will write out all of the graphs associated with the source. Right now these are hardcoded to be a single "graph" and a "src_dataset.ttl" and a "src_test.ttl" If you do not supply stream='stdout' it will default write these to files. In addition, if the version number isn't yet set in the dataset, it will be set to the date on file. :return: None """ fmt_ext = { 'rdfxml': 'xml', 'turtle': 'ttl', 'nt': 'nt', # ntriples 'nquads': 'nq', 'n3': 'n3' } # make the regular graph output file dest = None if self.name is not None: dest = '/'.join((self.outdir, self.name)) if fmt in fmt_ext: dest = '.'.join((dest, fmt_ext.get(fmt))) else: dest = '.'.join((dest, fmt)) logger.info("Setting outfile to %s", dest) # make the datasetfile name, always format as turtle datasetfile = '/'.join((self.outdir, self.name + '_dataset.ttl')) if self.dataset is not None and self.dataset.version is None: self.dataset.set_version_by_date() logger.info("No version for " + self.name + " setting to date issued.") else: logger.warning("No output file set. Using stdout") stream = 'stdout' gu = GraphUtils(None) # the _dataset descriptions is always turtle gu.write(self.dataset.getGraph(), 'turtle', file=datasetfile) # unless we stop hardcoding above, the test dataset is always turtle if self.testMode: gu.write(self.testgraph, 'turtle', file=self.testfile) # print graph out if stream is None: f = dest elif stream.lower().strip() == 'stdout': f = None else: logger.error("I don't understand your stream.") return gu.write(self.graph, fmt, file=f) return
def write(self, fmt='turtle', stream=None): """ This convenience method will write out all of the graphs associated with the source. Right now these are hardcoded to be a single "graph" and a "src_dataset.ttl" and a "src_test.ttl" If you do not supply stream='stdout' it will default write these to files. In addition, if the version number isn't yet set in the dataset, it will be set to the date on file. :return: None """ fmt_ext = { 'rdfxml': 'xml', 'turtle': 'ttl', 'nt': 'nt', # ntriples 'nquads': 'nq', 'n3': 'n3' # notation3 } # make the regular graph output file dest = None if self.name is not None: dest = '/'.join((self.outdir, self.name)) if fmt in fmt_ext: dest = '.'.join((dest, fmt_ext.get(fmt))) else: dest = '.'.join((dest, fmt)) LOG.info("Setting outfile to %s", dest) # make the dataset_file name, always format as turtle self.datasetfile = '/'.join( (self.outdir, self.name + '_dataset.ttl')) LOG.info("Setting dataset file to %s", self.datasetfile) if self.dataset is not None and self.dataset.version is None: self.dataset.set_version_by_date() LOG.info("No version for %s setting to date issued.", self.name) else: LOG.warning("No output file set. Using stdout") stream = 'stdout' gu = GraphUtils(None) # the _dataset description is always turtle gu.write(self.dataset.getGraph(), 'turtle', filename=self.datasetfile) if self.test_mode: # unless we stop hardcoding, the test dataset is always turtle LOG.info("Setting testfile to %s", self.testfile) gu.write(self.testgraph, 'turtle', filename=self.testfile) # print graph out if stream is None: outfile = dest elif stream.lower().strip() == 'stdout': outfile = None else: LOG.error("I don't understand our stream.") return gu.write(self.graph, fmt, filename=outfile)