New command: obi tail

This commit is contained in:
Celine Mercier
2018-08-10 10:39:26 +02:00
parent 2c634dae7c
commit e096b929dc

View File

@ -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='<N>',
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()