New command: obi head

This commit is contained in:
Celine Mercier
2018-08-10 10:29:37 +02:00
parent 7a4cdc0cfe
commit 2c634dae7c

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 first lines of a view."
def addOptions(parser):
addMinimalInputOption(parser)
addMinimalOutputOption(parser)
group=parser.add_argument_group('obi head specific options')
group.add_argument('-n', '--sequence-count',
action="store", dest="head:count",
metavar='<N>',
default=10,
type=int,
help="Number of first records to keep.")
def run(config):
DMS.obi_atexit()
logger("info", "obi head")
# 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)
n = min(config['head']['count'], len(i_view))
selection = Line_selection(i_view)
for i in range(n):
selection.append(i)
# Create output view with the line selection
try:
o_view = selection.materialize(output_view_name, comments="obi head: "+str(config['head']['count'])+"\n")
except Exception, e:
raise RollbackException("obi head 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()