Changeset 3f924b1 in rattail


Ignore:
Timestamp:
07/01/18 22:02:13 (2 years ago)
Author:
Lance Edgar <lance@…>
Branches:
master
Children:
f6775f5
Parents:
eb08988
Message:

Add basic support for "command line" filemon action

Location:
rattail/filemon
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • rattail/filemon/actions.py

    reb08988 r3f924b1  
    1 # -*- coding: utf-8 -*-
     1# -*- coding: utf-8; -*-
    22################################################################################
    33#
    44#  Rattail -- Retail Software Framework
    5 #  Copyright © 2010-2017 Lance Edgar
     5#  Copyright © 2010-2018 Lance Edgar
    66#
    77#  This file is part of Rattail.
     
    3232from six.moves import queue
    3333import socket
     34import subprocess
    3435import logging
    3536from traceback import format_exception
     
    5657        """
    5758        raise NotImplementedError
     59
     60
     61class CommandAction(Action):
     62    """
     63    Simple file monitor action which can execute a command as a subprocess.
     64    """
     65
     66    def __init__(self, config, cmd):
     67        self.config = config
     68        self.cmd = cmd
     69
     70    def __call__(self, path):
     71        """
     72        Run the requested command.
     73        """
     74        cmd = self.cmd.format(
     75            path=path,
     76            filename=os.path.basename(path))
     77        subprocess.check_call(cmd, shell=True)
    5878
    5979
  • rattail/filemon/config.py

    reb08988 r3f924b1  
    1 # -*- coding: utf-8 -*-
     1# -*- coding: utf-8; -*-
    22################################################################################
    33#
    44#  Rattail -- Retail Software Framework
    5 #  Copyright © 2010-2017 Lance Edgar
     5#  Copyright © 2010-2018 Lance Edgar
    66#
    77#  This file is part of Rattail.
     
    3535from rattail.util import load_object
    3636from rattail.exceptions import ConfigurationError
     37from rattail.filemon.actions import CommandAction
    3738
    3839
     
    174175
    175176    def _config_action(self, name):
    176         function = self._config_string(u'action.{0}.func'.format(name))
    177         class_ = self._config_string(u'action.{0}.class'.format(name))
    178 
    179         if function and class_:
     177        function = self._config_string('action.{}.func'.format(name))
     178        class_ = self._config_string('action.{}.class'.format(name))
     179        cmd = self._config_string('action.{}.cmd'.format(name))
     180
     181        specs = [1 if spec else 0 for spec in (function, class_, cmd)]
     182        if sum(specs) != 1:
    180183            raise ConfigurationError(
    181                 u"File monitor profile '{0}' has both function *and* class defined for "
    182                 u"action '{1}' (must have one or the other).".format(self.key, name))
    183 
    184         if not function and not class_:
    185             raise ConfigurationError(
    186                 u"File monitor profile '{0}' has neither function *nor* class defined for "
    187                 u"action '{1}' (must have one or the other).".format(self.key, name))
     184                "File monitor profile '{}' (action '{}') must have exactly one of: "
     185                "function, class, command".format(self.key, name))
    188186
    189187        action = ProfileAction()
     
    193191            action.spec = function
    194192            action.action = load_object(action.spec)
    195         else:
     193        elif class_:
    196194            action.spec = class_
    197195            action.action = load_object(action.spec)(self.config)
     196        elif cmd:
     197            action.spec = cmd
     198            action.action = CommandAction(self.config, cmd)
    198199
    199200        action.args = self._config_list(u'action.{0}.args'.format(name))
Note: See TracChangeset for help on using the changeset viewer.