degal/command.py
author Tero Marttila <terom@fixme.fi>
Sun, 14 Jun 2009 23:43:40 +0300
changeset 120 55cb7fc9c8fb
parent 117 a2e4562deaab
child 144 97505a789003
permissions -rw-r--r--
add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
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
117
a2e4562deaab implement concurrency... :)
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
     5
import inspect, logging, traceback, concurrent
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
class CommandList (object) :
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
        A list of available Commands
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
        XXX: not yet used
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
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    def __init__ (self, commands) :
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
            Store with given initial 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
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
        self.list = commands
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
        self.dict = dict((cmd.name, cmd) for cmd in commands)
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
    def lookup (self, name) :
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
            Lookup a command by 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
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
        return self.dict[name]
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
class Command (object) :
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
        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
    32
    """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
    def __init__ (self, name, func, doc=None) :
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
            Create a new Command
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
             name       - the name of the command
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
             func       - the callable python function
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
             doc        - descriptive help text
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
        self.name = name
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
        self.func = func
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
        self.doc = doc
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
    47
    def setup (self, config, gallery) :
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
            Run the command with the given context
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
        
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
    52
        return CommandContext(self, config, gallery)
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
class CommandContext (object) :
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
        A CommandContext is the context that a Command executes in
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
        It is bound to a Configuration and a Gallery.
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
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
    def __init__ (self, command, config, gallery) :
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
            Create the execution environment
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
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
        self.command = command
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
        self.config = config
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
        self.gallery = gallery
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
117
a2e4562deaab implement concurrency... :)
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    70
        # conccurency
a2e4562deaab implement concurrency... :)
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    71
        self.concurrent = concurrent.Manager(thread_count=config.thread_count)
a2e4562deaab implement concurrency... :)
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    72
92
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    73
    def execute (self, *args, **kwargs) :
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
            Run the command in this context
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
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
        return self.command.func(self, *args, **kwargs)
92
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    79
    
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    80
    def run (self, *args, **kwargs) :
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    81
        """
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    82
            Run the command with error handling
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    83
        """
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    84
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    85
        try :
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    86
            # run it
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    87
            return self.execute(*args, **kwargs)
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    88
        
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    89
        except KeyboardInterrupt :
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    90
            self.log_error("Interrupted")
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    91
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    92
        except :
114
4096f8a7e63c fix use of handle_error in command.py
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
    93
            # dump traceback
120
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents: 117
diff changeset
    94
            # XXX: skip all crap up to the actual function
114
4096f8a7e63c fix use of handle_error in command.py
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
    95
            self.handle_error()
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
    def log_msg (self, level, msg, *args, **kwargs) :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
            Output a log message with the given level
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
            XXX: unicode
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
        """
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
        # control level of output
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
        if level < self.config.log_level :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
            return
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
        # format?
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
        if args or kwargs :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
            if args and not kwargs :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
                msg = msg % args
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
            elif kwargs and not args :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
                msg = msg % kwargs
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
            else :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
                raise Exception("log_msg called with both args and kwargs")
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118
        
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   119
        # output
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   120
        # XXX: stdout/err?
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   121
        print msg
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   122
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   123
    def log_debug (self, msg, *args, **kwargs) :
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   124
        self.log_msg(logging.DEBUG, msg, *args, **kwargs)
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   125
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   126
    def log_info (self, msg, *args, **kwargs) :
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   127
        self.log_msg(logging.INFO, msg, *args, **kwargs)
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   128
    
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   129
    def log_warning (self, msg, *args, **kwargs) :
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   130
        self.log_msg(logging.WARNING, msg, *args, **kwargs)
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   131
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
    def log_error (self, msg, *args, **kwargs) :
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   133
        self.log_msg(logging.ERROR, msg, *args, **kwargs)
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   134
    
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   135
    def handle_error (self, exc_info=None) :
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   136
        """
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   137
            Do something to handle an error that occured
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   138
        """
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   139
        
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   140
        if exc_info :
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   141
            traceback.print_execption(*exc_info)
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   142
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   143
        else :
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   144
            traceback.print_exc()
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   145
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   146
def command (func) :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   147
    """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   148
        A function decorator used to define Commands automatically
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   149
    """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   150
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   151
    return Command(func.__name__, func, inspect.getdoc(func))
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   152