Changeset 127

Show
Ignore:
Timestamp:
06/16/05 21:08:27 (3 years ago)
Author:
athomas
Message:
  • Added history management commands (closes #6)
  • Packaged with distutils.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • fwc/trunk/Config.py

    r126 r127  
    8282                        text = Config.Template(text).safe_substitute(vars) 
    8383                return text 
    84  
    85 if __name__ == "__main__": 
    86         c = Config() 
    87         c.define('foo-bar', 0, 'The FOO') 
    88         c.define("x-axis", 1, "The X-Axis") 
    89         print c.expand("Alec is ${foo-bar} times cooler") 
    90         c.foo_bar = 10 
    91         print c.expand("Alec is ${foo-bar} times cooler") 
    92         for k in c.keys(): 
    93                 print c.help(k), ":", c[k] 
  • fwc/trunk/Engine.py

    r126 r127  
    22import sys 
    33import socket 
     4import readline 
     5import textwrap 
    46from ConfigParser import RawConfigParser 
    57from Config import Config 
     
    3739                        'to' : [], 
    3840                } 
     41                self.existing_input = '' 
    3942 
    4043                # Currently active firewall 
     
    299302                        }, 
    300303                        'quit|exit' : { 
     304                                GROUP : 20000, 
    301305                                HELP : [ 'quit', 'Exit.' ], 
    302306                                ACTION : { 
    303307                                        HELP : 'Exit.', 
    304308                                        ACTION : self.quit, 
     309                                }, 
     310                        }, 
     311                        '!\d+' : { 
     312                                VAR : 'index', 
     313                                ACTION : lambda ctx, index: self.__set_history(ctx, index[1:]), 
     314                        }, 
     315                        'history' : { 
     316                                GROUP : 20000, 
     317                                HELP : 'Command history management.', 
     318                                ACTION : { 
     319                                        HELP : 'Show history.', 
     320                                        ACTION : self.__list_history, 
     321                                }, 
     322                                'clear' : { 
     323                                        HELP : 'Clear history.', 
     324                                        ACTION : self.__clear_history, 
     325                                }, 
     326                                '\d+' : { 
     327                                        VAR : 'index', 
     328                                        HELP : ('<index>', 'History item index.'), 
     329                                        ACTION : self.__set_history, 
    305330                                }, 
    306331                        }, 
     
    516541                self._parser = Parser(self.__grammar) 
    517542 
     543        def __list_history(self, ctx): 
     544                cols = 'COLUMNS' in os.environ and int(os.environ['COLUMNS']) or 80 
     545                for index in range(1, readline.get_current_history_length() + 1): 
     546                        #cprint('\n'.join(textwrap.wrap("^B%3i:^B %s" % (index, readline.get_history_item(index)), cols, subsequent_indent = '       '))) 
     547                        cprint("^B%3i:^B %s" % (index, readline.get_history_item(index))) 
     548 
     549        def __clear_history(self, ctx): 
     550                readline.clear_history() 
     551 
     552        def __set_history(self, ctx, index): 
     553                try: 
     554                        index = int(index) 
     555                        if index > readline.get_current_history_length(): 
     556                                raise IndexError() 
     557                        self.existing_input = readline.get_history_item(index) 
     558                except: 
     559                        error("No such history index '%s'" % index) 
     560 
    518561        def __firewall_set(self, ctx, setting, value): 
    519562                try: 
  • fwc/trunk/fwc

    r125 r127  
    2626""" % Engine.version) 
    2727 
     28try: 
     29        readline.read_history_file(os.path.expanduser('~/.fwchistory')) 
     30except: 
     31        pass 
     32 
    2833while True: 
    29         result = interact(engine._parser, engine.config.prompt) 
     34        result = interact(engine._parser, engine.config.prompt, engine.existing_input) 
     35        engine.existing_input = '' 
    3036 
    3137        if result.state == Result.NOP: 
    3238                continue 
    3339        elif result.state == Result.ENDOFINPUT: 
     40                try: 
     41                        readline.write_history_file(os.path.expanduser('~/.fwchistory')) 
     42                except Exception, e: 
     43                        error(e) 
    3444                engine.quit() 
    3545        elif result.state == Result.OK: