예제 #1
0
파일: __init__.py 프로젝트: Xuefeng-Zhu/clf
def run():
    arguments = docopt(__doc__, version=VERSION)

    f = Clf(format="json",
            order=arguments['--order'],
            proxy=arguments['--proxy'])

    if arguments['--browse']:
        commands = f.browse()
    elif arguments['<command>']:
        commands = f.command(arguments['<command>'])
    elif arguments['<keyword>']:
        commands = f.search(arguments['<keyword>'])

    for command in commands:
        if (arguments['--color']) or (os.getenv('CLF_COLOR')):
            output = '{}# {}{}\n'.format(BLUE, command.summary, END)
            output += highlight(command.command,
                                BashLexer(),
                                TerminalFormatter(bg="dark"))
        else:
            output = '# {}\n'.format(command.summary)
            output += command.command + "\n"

        print(output)
예제 #2
0
def run():
    arguments = docopt(__doc__, version=VERSION)

    f = Clf(format="json",
            order=arguments['--order'],
            proxy=arguments['--proxy'])

    if arguments['--browse']:
        commands = f.browse()
    elif arguments['<command>']:
        commands = f.command(arguments['<command>'])
    elif arguments['<keyword>']:
        commands = f.search(arguments['<keyword>'])

    if (arguments['--color']) or (os.getenv('CLF_COLOR')):

        def get_output(command):
            detail = highlight(command.command, BashLexer(),
                               TerminalFormatter(bg="dark"))
            return '{}# {}{}\n{}'.format(BLUE, command.summary, END, detail)
    else:

        def get_output(command):
            return '# {}\n{}\n'.format(command.summary, command.command)

    for command in commands:
        print(get_output(command))
예제 #3
0
파일: test_clf.py 프로젝트: optionalg/clf
    def test_command_urls(self):
        for order in ['votes', 'date']:
            clf = Clf(order=order)

            for format in ['json', 'xml', 'plaintext']:
                clf.format = format
                url = self.urls['command_by_' + order + '_in_' + format]
                self.assertEqual(clf._prepare_command_url('tar'), url)
예제 #4
0
파일: test_clf.py 프로젝트: optionalg/clf
    def test_exceptions(self):
        with self.assertRaises(FormatException):
            Clf(format='foo')

        with self.assertRaises(OrderException):
            Clf(order='foo')

        with self.assertRaises(QueryException):
            Clf()._prepare_search_url({'foo': 'bar'})
예제 #5
0
파일: test_clf.py 프로젝트: Xuefeng-Zhu/clf
    def test_command_urls(self):
        for order in ['votes', 'date']:
            clf = Clf(order=order)

            for format in ['json', 'xml', 'plaintext']:
                clf.format = format
                url = self.urls['command_by_' + order + '_in_' + format]
                self.assertEqual(clf._prepare_command_url('tar'),
                                 url)
예제 #6
0
파일: test_clf.py 프로젝트: optionalg/clf
    def test_search_urls(self):
        for order in ['votes', 'date']:
            clf = Clf(order=order)

            for format in ['json', 'xml', 'plaintext']:
                clf.format = format

                url = self.urls['search_by_' + order + '_in_' + format]
                self.assertEqual(clf._prepare_search_url('python server'), url)
                self.assertEqual(clf._prepare_search_url(['python', 'server']),
                                 url)
예제 #7
0
파일: test_clf.py 프로젝트: Xuefeng-Zhu/clf
    def test_search_urls(self):
        for order in ['votes', 'date']:
            clf = Clf(order=order)

            for format in ['json', 'xml', 'plaintext']:
                clf.format = format

                url = self.urls['search_by_' + order + '_in_' + format]
                self.assertEqual(clf._prepare_search_url('python server'),
                                 url)
                self.assertEqual(clf._prepare_search_url(['python', 'server']),
                                 url)
예제 #8
0
파일: __init__.py 프로젝트: optionalg/clf
def run():
    arguments = docopt(__doc__, version=VERSION)

    f = Clf(format="json",
            order=arguments['--order'],
            proxy=arguments['--proxy'])

    # Save the snippet locally
    if arguments['-s']:
        try:
            snippet = save_snippet(arguments['-s'], f._get_proxies())
        except(RequestsException, OSException, DuplicateException) as e:
            print(e)
        else:
            print("The snippet has been successfully saved.")
        exit()

    # Retrieve the snippets list
    if arguments['--local']:
        commands = get_local_snippets()
    elif arguments['<command>']:
        commands = f.command(arguments['<command>'])
    elif arguments['<keyword>']:
        commands = f.search(arguments['<keyword>'])
    else:
        commands = f.browse()

    # Show the snippets id
    if arguments['--id']:
        sid = lambda c: '({})'.format(c.id)
    else:
        sid = lambda c: ''

    # Display in colors
    if (arguments['--color']) or (os.getenv('CLF_COLOR')):
        def get_output(command):
            detail = highlight(command.command,
                               BashLexer(), TerminalFormatter(bg="dark"))
            return '{}#{} {}{}\n{}'.format(BLUE, sid(command),
                                           command.summary, END, detail)
    else:
        def get_output(command):
            return '#{} {}\n{}\n'.format(sid(command),
                                         command.summary, command.command)

    # Limit the number of snippets
    try:
        limit = int(arguments['-n'])
    except ValueError:
        limit = 25

    # Display the snippets
    for idx, command in enumerate(commands):
        if limit == idx:
            break

        print(get_output(command))
예제 #9
0
def run():
    arguments = docopt(__doc__, version=VERSION)

    f = Clf(format="json",
            order=arguments['--order'],
            proxy=arguments['--proxy'])

    if arguments['--browse']:
        commands = f.browse()
    elif arguments['<command>']:
        commands = f.command(arguments['<command>'])
    elif arguments['<keyword>']:
        commands = f.search(arguments['<keyword>'])

    for command in commands:
        if (arguments['--color']) or (os.getenv('CLF_COLOR')):
            output = '{}# {}{}\n'.format(BLUE, command.summary, END)
            output += highlight(command.command, BashLexer(),
                                TerminalFormatter(bg="dark"))
        else:
            output = '# {}\n'.format(command.summary)
            output += command.command + "\n"

        print(output)
예제 #10
0
파일: test_clf.py 프로젝트: optionalg/clf
 def test_search(self):
     commands = Clf().search('python server')
     self.assertIsInstance(commands, types.GeneratorType)
     self.assertIsInstance(next(commands), Command)
     self.assertGreater(sum(1 for _ in commands), 0)
예제 #11
0
파일: test_clf.py 프로젝트: optionalg/clf
 def test_command(self):
     commands = Clf().command('tar')
     self.assertIsInstance(commands, types.GeneratorType)
     self.assertIsInstance(next(commands), Command)
     self.assertEqual(24, sum(1 for _ in commands))