degal/command.py
author Tero Marttila <terom@fixme.fi>
Sun, 14 Jun 2009 18:04:53 +0300
changeset 114 4096f8a7e63c
parent 92 eb50b4f7812d
child 117 a2e4562deaab
permissions -rw-r--r--
fix use of handle_error in command.py
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
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
     5
import inspect, logging, traceback
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
92
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    70
    def execute (self, *args, **kwargs) :
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
            Run the command in this context
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
        """
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
        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
    76
    
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    77
    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
    78
        """
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    79
            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
    80
        """
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
        try :
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    83
            # run it
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    84
            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
    85
        
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    86
        except KeyboardInterrupt :
eb50b4f7812d move Command exception handling into command.py, and handle KeyboardInterrupt
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
    87
            self.log_error("Interrupted")
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 :
114
4096f8a7e63c fix use of handle_error in command.py
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
    90
            # dump traceback
4096f8a7e63c fix use of handle_error in command.py
Tero Marttila <terom@fixme.fi>
parents: 92
diff changeset
    91
            self.handle_error()
65
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
    def log_msg (self, level, msg, *args, **kwargs) :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
        """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
            Output a log message with the given level
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
            XXX: unicode
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
        
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
        # control level of output
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
        if level < self.config.log_level :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
            return
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
        # format?
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
        if args or kwargs :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
            if args and not kwargs :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
                msg = msg % args
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
            elif kwargs and not args :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
                msg = msg % kwargs
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
            else :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
                raise Exception("log_msg called with both args and kwargs")
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
        
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
        # output
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
        # XXX: stdout/err?
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
        print msg
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
    def log_debug (self, msg, *args, **kwargs) :
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   120
        self.log_msg(logging.DEBUG, msg, *args, **kwargs)
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   121
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   122
    def log_info (self, msg, *args, **kwargs) :
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   123
        self.log_msg(logging.INFO, msg, *args, **kwargs)
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   124
    
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   125
    def log_warning (self, msg, *args, **kwargs) :
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   126
        self.log_msg(logging.WARNING, msg, *args, **kwargs)
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   127
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   128
    def log_error (self, msg, *args, **kwargs) :
76
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   129
        self.log_msg(logging.ERROR, msg, *args, **kwargs)
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   130
    
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   131
    def handle_error (self, exc_info=None) :
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   132
        """
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   133
            Do something to handle an error that occured
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
        
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   136
        if exc_info :
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   137
            traceback.print_execption(*exc_info)
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
        else :
e22d9f699081 misc. fixes
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   140
            traceback.print_exc()
65
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   141
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   142
def command (func) :
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   143
    """
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   144
        A function decorator used to define Commands automatically
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
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   147
    return Command(func.__name__, func, inspect.getdoc(func))
97e1bc208574 initial commands infrastructure
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   148