prepare generic operators

Add alternate extension management to Command
Add and example of "inkscape" operator.
This commit is contained in:
Johann Dreo 2017-12-09 15:59:22 +01:00
commit fac6c7eb0f

View file

@ -76,7 +76,7 @@ class Flick:
class Operator: class Operator:
"""Interface example for an Operator (but can actually be any callable with the same signature).""" """Interface example for an Operator (but can actually be any callable with the same signature)."""
def __call__(self, target, flickname): def __call__(self, target, flick, alt_ext = None):
raise NotImplemented raise NotImplemented
@ -87,18 +87,38 @@ class Command(Operator):
For example: 'cp {target} {flick}' For example: 'cp {target} {flick}'
You can omit one of the named arguments. You can omit one of the named arguments.
For example: 'touch {flick}'""" For example: 'touch {flick}'"""
def __init__(self, command):
def __init__(self, command, alt_ext = None):
self.cmd = command self.cmd = command
def __call__(self, target, flickname): self.alt_ext = alt_ext
cmd = self.cmd.format(target=target,flick=flickname)
def __call__(self, target, flick):
# Change the extension, if asked.
flickname,flickext = os.path.splitext(flick)
if self.alt_ext:
flick = "{}.{}".format(flickname,self.alt_ext)
cmd = self.cmd.format(target=target,flick=flick)
logging.info("Run command: %s", cmd ) logging.info("Run command: %s", cmd )
# Subprocess want a list of cmd and arguments. try:
subprocess.run(cmd.split()) # We want the shell because we may call shell command or need environment variables.
proc = subprocess.run(cmd, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except subprocess.CalledProcessError:
logging.error("ERROR while calling command")
logging.debug("\tReturn code: %s",proc.returncode)
logging.info("\tOut: %s",proc.stdout)
logging.info("\tErr: %s",proc.stderr)
except:
logging.error("ERROR while calling subprocess: %s", sys.exc_info()[0])
logging.debug("Command ended.")
class Save(Operator): class Save(Operator):
"""Make a copy of the target file. """Make a copy of the target file.
Takes care to create a missing directory if necessary.""" Takes care to create a missing directory if necessary."""
def __call__(self, target, flickname): def __call__(self, target, flickname):
logging.info("Copy %s as %s", target, flickname) logging.info("Copy %s as %s", target, flickname)
try: try:
@ -136,7 +156,7 @@ class Handler(FileSystemEventHandler):
for op in self.ops: for op in self.ops:
logging.debug("Calling %s", op) logging.debug("Calling %s", op)
op(event.src_path, flickname) op(os.path.abspath(event.src_path), os.path.abspath(flickname))
def flicksave(target, operators=None, save_dir=".", delay=10, stamp_sep='_', date_template="%Y-%m-%dT%H:%M:%S"): def flicksave(target, operators=None, save_dir=".", delay=10, stamp_sep='_', date_template="%Y-%m-%dT%H:%M:%S"):
@ -198,7 +218,11 @@ if __name__=="__main__":
logging.basicConfig(level=log_as[asked.verbose], format='%(asctime)s -- %(message)s', datefmt=asked.template) logging.basicConfig(level=log_as[asked.verbose], format='%(asctime)s -- %(message)s', datefmt=asked.template)
operators = [Command("cp {target} {flick}"),Command("touch {flick}")] ops = {
"default" : Save(),
"inkscape": Command("inkscape {target} --without-gui --export-png={flick} --export-area-page", "png")
}
operators = [ops[instance] for instance in ops]
# Start it. # Start it.
flicksave(asked.target, operators, asked.directory, asked.delay, asked.separator, asked.template) flicksave(asked.target, operators, asked.directory, asked.delay, asked.separator, asked.template)