コード例 #1
0
ファイル: resolver.py プロジェクト: hexode/avalon_python
 def help():
     ''' Выводит справку '''
     print get_cstring('Usage:', 'HEADER')
     help_lines = cli.get_help().split('\n')
     for help_line in help_lines:
         print '%-30s - %s' % tuple(help_line.split(' - '))
     print
コード例 #2
0
ファイル: constants.py プロジェクト: hexode/avalon_python
def display_constant(const_name, const_accuracy=None):
    if not const_accuracy:
        const_accuracy = 5
    const_value = get_const(const_name)
    const_accuracy = int(const_accuracy)
    if const_value:
        print 'const %s = %.*f' % (get_cstring(const_name, 'HEADER'), const_accuracy, const_value)
コード例 #3
0
ファイル: resolver.py プロジェクト: hexode/avalon_python
    def find(phraze):
        ''' Ищет хосты содержащие строку phraze '''
        # Найдем записи, которые содержат искомую фразу
        records = [record for record in get_records() if phraze in record[1]]

        # подсветим фразу в имени хоста
        highlight = lambda s: get_cstring(s, 'STRONG')
        inject = lambda fn, sep, string: fn(sep).join(string.split(sep))
        records = [[i, inject(highlight, phraze, h)] for i, h in records]

        # отобразим записи
        display_records(records)
コード例 #4
0
ファイル: constants.py プロジェクト: hexode/avalon_python
 def display_error(cmd):
     print get_cstring('Unknow command %s' % cmd, 'FAIL')
     print get_cstring('use help to show all available commands', 'FAIL')
コード例 #5
0
ファイル: resolver.py プロジェクト: hexode/avalon_python
 def display_records(records):
     ''' Выполняет отображение записей '''
     for ip, hostname in records:
         ip_string = '%03.d.%03.d.%03.d.%03.d' % tuple(map(int, ip.split('.')))
         print '%s  %s' % (get_cstring(ip_string, 'WARNING'), hostname)
コード例 #6
0
ファイル: resolver.py プロジェクト: hexode/avalon_python
def main():

    def help():
        ''' Выводит справку '''
        print get_cstring('Usage:', 'HEADER')
        help_lines = cli.get_help().split('\n')
        for help_line in help_lines:
            print '%-30s - %s' % tuple(help_line.split(' - '))
        print


    def get_records():
        ''' Возвращает список записей из файла '''
        return parse_hosts_file().items()

    def display_records(records):
        ''' Выполняет отображение записей '''
        for ip, hostname in records:
            ip_string = '%03.d.%03.d.%03.d.%03.d' % tuple(map(int, ip.split('.')))
            print '%s  %s' % (get_cstring(ip_string, 'WARNING'), hostname)

    def show_all_records():
        ''' Выводит все записи '''
        print get_cstring('All records:', 'HEADER')
        display_records(get_records())

    def tail(n=5):
        ''' Выводит n последних записей '''
        print get_cstring('Last %s records:' % n, 'HEADER')
        display_records(get_records()[-int(n):])

    def head(n=5):
        ''' Выводит n первых записей '''
        print get_cstring('First %s records:' % n, 'HEADER')
        display_records(get_records()[:int(n)])

    def find(phraze):
        ''' Ищет хосты содержащие строку phraze '''
        # Найдем записи, которые содержат искомую фразу
        records = [record for record in get_records() if phraze in record[1]]

        # подсветим фразу в имени хоста
        highlight = lambda s: get_cstring(s, 'STRONG')
        inject = lambda fn, sep, string: fn(sep).join(string.split(sep))
        records = [[i, inject(highlight, phraze, h)] for i, h in records]

        # отобразим записи
        display_records(records)

    def resolv(request):
        ''' Разрешает ip адрес или имя хоста '''
        check = re.compile(ip_address_re).match(request)
        ix = 0 if check else 1

        record = [rec for rec in get_records() if request == rec[ix]][0]
        if len(record):
            print record[not ix]

    cli = SimpleCLI()

    ip_address_re = r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'

    cli.reg_cmd((re.compile(r'add\s+(%s)\s+(\S+)' % ip_address_re),),
                'create record in file',
                add_resolv_record,
                'add <ip_addr> <name>')

    print r'remove\s+(%s)' % ip_address_re

    remove_re = (re.compile(r'remove\s+(%s)' % ip_address_re), )
    cli.reg_cmd(remove_re, 'remove resolv record by ip', remove_resolv_record, 'remove <ip_addr>')

    cli.reg_cmd(('show', 's', ), 'show records', show_all_records)

    tail_re = (re.compile(r'tail\s+(\d+)'), re.compile(r'tail'))
    cli.reg_cmd(tail_re, 'show last n records', tail, 'tail [n]')

    head_re = (re.compile(r'head\s+(\d+)'), re.compile(r'head'))
    cli.reg_cmd(head_re, 'show first n records', head, 'head [n]')

    find_re = (re.compile(r'find\s+(\S+)'), )
    cli.reg_cmd(find_re, 'find hostnames containing substring', find, 'find <substring>')

    resolv_re = (re.compile(r'resolv\s+(\S+)'), )
    cli.reg_cmd(resolv_re, 'resolv hostname or ip address', resolv, 'resolv <ip_addr>|<host_name>')


    cli.reg_cmd(('help', 'h', '?'), 'display help', help)
    cli.reg_cmd(('clear',), 'clear screen', clear)
    cli.reg_cmd(('quit', 'exit', 'q', 'x'), 'quit from REPL', quit)

    cli.on_syntax_error = display_error
    prompt = get_cstring("> ", 'OKGREEN')

    help()
    tail()

    while True:
        user_input = raw_input(prompt).strip()
        if not user_input:
            continue
        cli.analyze(user_input)
コード例 #7
0
ファイル: resolver.py プロジェクト: hexode/avalon_python
def display_error(cmd):
    print '%s: command not found: %s' % (
        get_cstring('Syntax Error', 'FAIL'), cmd
    )
コード例 #8
0
ファイル: resolver.py プロジェクト: hexode/avalon_python
 def head(n=5):
     ''' Выводит n первых записей '''
     print get_cstring('First %s records:' % n, 'HEADER')
     display_records(get_records()[:int(n)])
コード例 #9
0
ファイル: resolver.py プロジェクト: hexode/avalon_python
 def tail(n=5):
     ''' Выводит n последних записей '''
     print get_cstring('Last %s records:' % n, 'HEADER')
     display_records(get_records()[-int(n):])
コード例 #10
0
ファイル: resolver.py プロジェクト: hexode/avalon_python
 def show_all_records():
     ''' Выводит все записи '''
     print get_cstring('All records:', 'HEADER')
     display_records(get_records())