From e096b929dc848b4cd86d353eabb4faaf31392e74 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Fri, 10 Aug 2018 10:39:26 +0200 Subject: [PATCH] New command: obi tail --- python/obitools3/commands/tail.pyx | 77 ++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 python/obitools3/commands/tail.pyx diff --git a/python/obitools3/commands/tail.pyx b/python/obitools3/commands/tail.pyx new file mode 100644 index 0000000..c5b0d4e --- /dev/null +++ b/python/obitools3/commands/tail.pyx @@ -0,0 +1,77 @@ +#cython: language_level=3 + +from obitools3.apps.progress cimport ProgressBar # @UnresolvedImport +from obitools3.dms import DMS +from obitools3.dms.view.view cimport View, Line_selection +from obitools3.uri.decode import open_uri +from obitools3.apps.optiongroups import addMinimalInputOption, addMinimalOutputOption +from obitools3.dms.view import RollbackException +from obitools3.apps.config import logger + +import time + + +__title__="Keep the N last lines of a view." + + +def addOptions(parser): + + addMinimalInputOption(parser) + addMinimalOutputOption(parser) + + group=parser.add_argument_group('obi tail specific options') + + group.add_argument('-n', '--sequence-count', + action="store", dest="tail:count", + metavar='', + default=10, + type=int, + help="Number of last records to keep.") + + +def run(config): + + DMS.obi_atexit() + + logger("info", "obi tail") + + # Open the input + input = open_uri(config['obi']['inputURI']) + if input is None: + raise Exception("Could not read input view") + i_view = input[1] + + # Read the name of the output view + uri = config['obi']['outputURI'].split('/') + if len(uri)==2: + # Check that input and output DMS are the same (predicate, to discuss) + if config['obi']['inputURI'].split('/')[0] != uri[0]: + raise Exception("Input and output DMS must be the same") + output_view_name = uri[1] + else: + output_view_name = uri[0] + + # Initialize the progress bar + pb = ProgressBar(len(i_view), config, seconde=5) + + start = max(len(i_view) - config['tail']['count'], 0) + + selection = Line_selection(i_view) + + for i in range(start, len(i_view)): + selection.append(i) + + # Create output view with the line selection + try: + o_view = selection.materialize(output_view_name, comments="obi tail: "+str(len(selection))+"\n") + except Exception, e: + raise RollbackException("obi tail error, rollbacking view: "+str(e), o_view) + + # TODO DISCUSS if output URI to different DMS, copy view? + + print("\n") + print(repr(o_view)) + + input[0].close() + #output[0].close() +