Commit 146cb8da authored by Alexandre Boyer's avatar Alexandre Boyer
Browse files

add new concept: actions before and after a command: exp-init as use case

parent 1f29415c
Pipeline #123548 passed with stage
in 24 seconds
......@@ -9,12 +9,14 @@ class ActevCommand():
"""
__metaclass__ = abc.ABCMeta
def __init__(self, command, entry_point):
def __init__(self, command, entry_point, before_entry_point=None, after_entry_point=None):
"""
@param command: string representing the name of the command
"""
self.command = command
self.entry_point = entry_point
self.before_entry_point = before_entry_point
self.after_entry_point = after_entry_point
@abc.abstractmethod
def cli_parser(self, arg_parser):
......@@ -24,6 +26,18 @@ class ActevCommand():
"""
return
def before_command(self, args):
""" Execute an action before executing the command
"""
if self.before_entry_point:
self.before_entry_point(**args.__dict__)
def after_command(self, args):
""" Execute an action after executing the command
"""
if self.after_entry_point:
self.after_entry_point(**args.__dict__)
def command(self, args):
""" Gets arguments and passe them to an entry point. Catch the exception occured.
......@@ -35,7 +49,11 @@ class ActevCommand():
try:
logging.info("Starting %s" % self.command)
StatusFactory.generateStatus(self, 'start', args.__dict__)
self.before_command(args)
self.entry_point(**args.__dict__)
self.after_command(args)
logging.info("%s done" % self.command)
StatusFactory.generateStatus(self, 'done', args.__dict__)
except:
......
......@@ -20,12 +20,13 @@ Warning: this file should not be modified: see src/entry_points to add your sour
import logging
from diva_evaluation_cli.bin.commands.actev_command import ActevCommand
from diva_evaluation_cli.bin.private_src.entry_points.actev_experiment_init import before_entry_point
from diva_evaluation_cli.src.entry_points.actev_experiment_init import entry_point
class ActevExperimentInit(ActevCommand):
def __init__(self):
super(ActevExperimentInit, self).__init__('experiment-init', entry_point)
super(ActevExperimentInit, self).__init__('experiment-init', entry_point, before_entry_point=before_entry_point)
def cli_parser(self, arg_parser):
""" Configure the description and the arguments (positional and optional) to parse.
......
"""
ENTRY POINT
This file should not be modified.
"""
import os
from diva_evaluation_cli.bin.private_src.implementation.experiment_init.experiment_validation import experiment_validation
def before_entry_point(file_index, activity_index, chunks,
video_location, system_cache_dir, config_file=None):
""" Private entry points.
"""
experiment_validation(file_index, video_location)
"""
SOURCE
"""
import os
import json
def experiment_validation(file_index, video_location):
""" Check that every video files enumerated in file_index exists, is readable and has content.
"""
with open(file_index) as f:
file_dict = json.load(f)
if not os.path.isdir(video_location):
raise Exception("Invalid argument: video_location is not a directory")
for video_filename in file_dict:
file_index_exists = False
for dirpath, dirnames, filenames in os.walk(video_location):
for filename in filenames:
if filename == video_filename:
file_index_exists = True
video = os.path.join(dirpath, filename)
if not os.path.isfile(video):
raise Exception("Invalid file index: {} is not a file".format(video))
if os.stat(video).st_size == 0:
raise Exception("Invalid file index: {} is empty.".format(video))
if not file_index_exists:
raise Exception("Invalid file index: {} does not exist".format(video_filename))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment