degal/command.py
author Tero Marttila <terom@fixme.fi>
Thu, 11 Jun 2009 20:39:59 +0300
branchthreaded-tasks
changeset 88 b1b0939517e7
parent 76 e22d9f699081
permissions -rw-r--r--
initial implementation of threaded rendering of a folder's images
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    Command implementations
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
88
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
     5
import task
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
     6
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
     7
import inspect, logging, traceback
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
class CommandList (object) :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
    """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
        A list of available Commands
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
        XXX: not yet used
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    def __init__ (self, commands) :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
            Store with given initial commands
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
        self.list = commands
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
        self.dict = dict((cmd.name, cmd) for cmd in commands)
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
    def lookup (self, name) :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
            Lookup a command by name
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
        return self.dict[name]
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
class Command (object) :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
    """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
        A Command is simply a function that can be executed from the command line with some options/arguments
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
    """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
    def __init__ (self, name, func, doc=None) :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
            Create a new Command
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
             name       - the name of the command
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
             func       - the callable python function
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
             doc        - descriptive help text
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
        self.name = name
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
        self.func = func
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
        self.doc = doc
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
    49
    def setup (self, config, gallery) :
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
            Run the command with the given context
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
        
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
    54
        return CommandContext(self, config, gallery)
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
class CommandContext (object) :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
    """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
        A CommandContext is the context that a Command executes in
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
        It is bound to a Configuration and a Gallery.
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
    """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
    def __init__ (self, command, config, gallery) :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
            Create the execution environment
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
        self.command = command
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
        self.config = config
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
        self.gallery = gallery
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
88
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    72
        # set up a task executor for this
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    73
        self.tasks = task.TaskManager(config)
b1b0939517e7 initial implementation of threaded rendering of a folder's images
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    74
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
    def __call__ (self, *args, **kwargs) :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
            Run the command in this context
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
        return self.command.func(self, *args, **kwargs)
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
    def log_msg (self, level, msg, *args, **kwargs) :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
            Output a log message with the given level
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
            XXX: unicode
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
        
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
        # control level of output
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
        if level < self.config.log_level :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
            return
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
        
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
        # format?
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
        if args or kwargs :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
            if args and not kwargs :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
                msg = msg % args
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
            elif kwargs and not args :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
                msg = msg % kwargs
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
            else :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
                raise Exception("log_msg called with both args and kwargs")
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
        
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
        # output
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
        # XXX: stdout/err?
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
        print msg
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
    def log_debug (self, msg, *args, **kwargs) :
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   109
        self.log_msg(logging.DEBUG, msg, *args, **kwargs)
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
    def log_info (self, msg, *args, **kwargs) :
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   112
        self.log_msg(logging.INFO, msg, *args, **kwargs)
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
    
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
    def log_warning (self, msg, *args, **kwargs) :
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   115
        self.log_msg(logging.WARNING, msg, *args, **kwargs)
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
    def log_error (self, msg, *args, **kwargs) :
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   118
        self.log_msg(logging.ERROR, msg, *args, **kwargs)
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   119
    
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   120
    def handle_error (self, exc_info=None) :
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   121
        """
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   122
            Do something to handle an error that occured
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   123
        """
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   124
        
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   125
        if exc_info :
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   126
            traceback.print_execption(*exc_info)
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   127
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   128
        else :
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   129
            traceback.print_exc()
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   130
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   131
def command (func) :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
    """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   133
        A function decorator used to define Commands automatically
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   134
    """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   135
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   136
    return Command(func.__name__, func, inspect.getdoc(func))
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   137