def process(cls, db, opts): # The process function always gets called for each filter, so it's up # to the filter to check opts and determine if it actually has # anything to do. if opts.out_format != 'text': return # That covers the basics of the output filter API. The rest of the work # involves interpreting the MorkDatabase contents and writing the # result. This part is not as straight-forward, because it requires # some understanding of the MorkDatabase class's internals. # outname is a common option for describing the output name (file, # directory, or whatever). Here we use '-' or the absence of outname # to select stdout. outname = opts.outname or '-' if outname == '-': f = EncodingStream(opts.out_encoding, sys.stdout) else: f = EncodingStream.open(opts.out_encoding, outname) if opts.tabs: indent = '\t' else: indent = ' ' # Basic iteration over tables generally looks like this. db.tables is a # customized dict where the keys are always (namespace, oid) tuples. # The items() method is modified to reflect this, returning the key # components separately. for (namespace, oid, table) in db.tables.items(): # This simple example completely skips meta-tables. I don't # recommend this in general, since they may contain useful data. cls._write_table(f, namespace, oid, table, indent)
def open(self, filename): ''' Simple helper function to use EncodingStream. ''' if filename == '-': return EncodingStream(self.opts.out_encoding, sys.stdout) else: return EncodingStream.open(self.opts.out_encoding, filename)
def process(self, db, opts): if opts.out_format != 'xml': return if opts.outname is None or opts.outname == '-': f = EncodingStream(opts.out_encoding, sys.stdout) else: f = EncodingStream.open(opts.out_encoding, opts.outname) self._output(db, f)