Beispiel #1
0
def get_all_ui_styles():
    """
    Return a dict mapping {ui_style_name -> style_dict}.
    """
    return {
        'default': Style.from_dict(default_ui_style),
        'blue': Style.from_dict(blue_ui_style),
    }
def test_substyles():
    style = Style([
        ('a.b', '#ff0000 bold'),
        ('a', '#0000ff'),
        ('b', '#00ff00'),
        ('b.c', '#0000ff italic'),
    ])

    # Starting with a.*
    expected = Attrs(color='0000ff', bgcolor='', bold=False, underline=False,
                     italic=False, blink=False, reverse=False, hidden=False)
    assert style.get_attrs_for_style_str('class:a') == expected

    expected = Attrs(color='ff0000', bgcolor='', bold=True, underline=False,
                     italic=False, blink=False, reverse=False, hidden=False)
    assert style.get_attrs_for_style_str('class:a.b') == expected
    assert style.get_attrs_for_style_str('class:a.b.c') == expected

    # Starting with b.*
    expected = Attrs(color='00ff00', bgcolor='', bold=False, underline=False,
                     italic=False, blink=False, reverse=False, hidden=False)
    assert style.get_attrs_for_style_str('class:b') == expected
    assert style.get_attrs_for_style_str('class:b.a') == expected

    expected = Attrs(color='0000ff', bgcolor='', bold=False, underline=False,
                     italic=True, blink=False, reverse=False, hidden=False)
    assert style.get_attrs_for_style_str('class:b.c') == expected
    assert style.get_attrs_for_style_str('class:b.c.d') == expected
def main():
    style = Style.from_dict({
        'hello': '#ff0066',
        'world': '#44ff44 italic',
    })

    # Print using a a list of text fragments.
    text_fragments = FormattedText([
        ('class:hello', 'Hello '),
        ('class:world', 'World'),
        ('', '\n'),
    ])
    print(text_fragments, style=style)

    # Print using an HTML object.
    print(HTML(
        '<hello>hello</hello> <world>world</world>\n'),
        style=style)

    # Print using an HTML object with inline styling.
    print(HTML(
        '<style fg="#ff0066">hello</style> '
        '<style fg="#44ff44"><i>world</i></style>\n'))

    # Print using ANSI escape sequences.
    print(ANSI(
        '\x1b[31mhello \x1b[32mworld\n'))
Beispiel #4
0
    def __init__(self, ipython_shell, *a, **kw):
        kw['_completer'] = create_completer(kw['get_globals'], kw['get_globals'],
                                            ipython_shell.magics_manager,
                                            ipython_shell.alias_manager)
        kw['_lexer'] = create_lexer()
        kw['_validator'] = IPythonValidator(
            get_compiler_flags=self.get_compiler_flags)

        super(IPythonInput, self).__init__(*a, **kw)
        self.ipython_shell = ipython_shell

        self.all_prompt_styles['ipython'] = IPythonPrompt(ipython_shell.prompts)
        self.prompt_style = 'ipython'

        # UI style for IPython. Add tokens that are used by IPython>5.0
        style_dict = {}
        style_dict.update(default_ui_style)
        style_dict.update({
            'pygments.prompt':        '#009900',
            'pygments.prompt-num':     '#00ff00 bold',
            'pygments.out-prompt':     '#990000',
            'pygments.out-prompt-num':  '#ff0000 bold',
        })

        self.ui_styles = {
            'default': Style.from_dict(style_dict),
        }
        self.use_ui_colorscheme('default')
def test_style_from_dict():
    style = Style.from_dict({
        'a': '#ff0000 bold underline italic',
        'b': 'bg:#00ff00 blink reverse',
    })

    # Lookup of class:a.
    expected = Attrs(color='ff0000', bgcolor='', bold=True, underline=True,
                     italic=True, blink=False, reverse=False, hidden=False)
    assert style.get_attrs_for_style_str('class:a') == expected

    # Lookup of class:b.
    expected = Attrs(color='', bgcolor='00ff00', bold=False, underline=False,
                     italic=False, blink=True, reverse=True, hidden=False)
    assert style.get_attrs_for_style_str('class:b') == expected

    # Test inline style.
    expected = Attrs(color='ff0000', bgcolor='', bold=False, underline=False,
                     italic=False, blink=False, reverse=False, hidden=False)
    assert style.get_attrs_for_style_str('#ff0000') == expected

    # Combine class name and inline style (Whatever is defined later gets priority.)
    expected = Attrs(color='00ff00', bgcolor='', bold=True, underline=True,
                     italic=True, blink=False, reverse=False, hidden=False)
    assert style.get_attrs_for_style_str('class:a #00ff00') == expected

    expected = Attrs(color='ff0000', bgcolor='', bold=True, underline=True,
                     italic=True, blink=False, reverse=False, hidden=False)
    assert style.get_attrs_for_style_str('#00ff00 class:a') == expected
Beispiel #6
0
def get_all_code_styles():
    """
    Return a mapping from style names to their classes.
    """
    result = dict((name, style_from_pygments_cls(get_style_by_name(name))) for name in get_all_styles())
    result['win32'] = Style.from_dict(win32_code_style)
    return result
def test_with_style():
    f = _Capture()
    style = Style.from_dict({
        'hello': '#ff0066',
        'world': '#44ff44 italic',
    })
    tokens = FormattedText([
        ('class:hello', 'Hello '),
        ('class:world', 'world'),
    ])
    pt_print(tokens, style=style, file=f)
    assert b'\x1b[0;38;5;197mHello' in f.data
    assert b'\x1b[0;38;5;83;3mworld' in f.data
def main():
    # Example 1: fixed text.
    text = prompt('Say something: ', bottom_toolbar='This is a toolbar')
    print('You said: %s' % text)

    # Example 2: fixed text from a callable:
    def get_toolbar():
        return 'Bottom toolbar: time=%r' % time.time()

    text = prompt('Say something: ', bottom_toolbar=get_toolbar,
                  refresh_interval=.5)
    print('You said: %s' % text)

    # Example 3: Using HTML:
    text = prompt('Say something: ', bottom_toolbar=HTML(
        '(html) <b>This</b> <u>is</u> a <style bg="ansired">toolbar</style>'))
    print('You said: %s' % text)

    # Example 4: Using ANSI:
    text = prompt('Say something: ', bottom_toolbar=ANSI(
        '(ansi): \x1b[1mThis\x1b[0m \x1b[4mis\x1b[0m a \x1b[91mtoolbar'))
    print('You said: %s' % text)

    # Example 5: styling differently.
    style = Style.from_dict({
        'bottom-toolbar':      '#aaaa00 bg:#ff0000',
        'bottom-toolbar.text': '#aaaa44 bg:#aa4444',
    })

    text = prompt('Say something: ', bottom_toolbar='This is a toolbar',
                  style=style)
    print('You said: %s' % text)

    # Example 6: Using a list of tokens.
    def get_bottom_toolbar():
        return [
            ('', ' '),
            ('bg:#ff0000 fg:#000000', 'This'),
            ('', ' is a '),
            ('bg:#ff0000 fg:#000000', 'toolbar'),
            ('', '. '),
        ]

    text = prompt('Say something: ', bottom_toolbar=get_bottom_toolbar)
    print('You said: %s' % text)

    # Example 7: multiline fixed text.
    text = prompt('Say something: ', bottom_toolbar='This is\na multiline toolbar')
    print('You said: %s' % text)
    def __init__(self, player):
        self.player = player
        self.playerClans = ' '.join(self.player.clantags)
        if len(self.player.clantags) > 0 : 
            self.playerName = FormattedText([
                ('#ffffff', player.aspect['name']),
                ('', ' '),
                ('#cc00cc', self.playerClans, "utf-8"),
            ]) 
        else: 
            self.playerClans =  self.playerName = FormattedText([
                ('#ffffff', player.aspect['name']),
            ]) 
        self.result = None

        self.mainRadiosRows = []
        self.listOfItems = []
        self.populateMainRadios() # declares self.mainRadios
        self.currentRadios = self.mainRadios
        self.description = self.mainRadios.description # description is whataver is in the description box on the right
        self.requestingConfirmation = False
        
        self.bindings = KeyBindings()
        self.bindings.add('right' )(focus_next)
        self.bindings.add('tab' )(focus_next)
        self.bindings.add('s-tab')(focus_previous)
        self.bindings.add('left')(focus_previous)
        self.bindings.add('c-m')(self.handleEnter)
        self.bindings.add('escape')(self.handleEscape)

        self.style = Style.from_dict({
            'dialog.body':        'bg:#000000 #ffcccc', #background color, text color
        })

        self.application = Application(
            layout=Layout(
                self.getRootContainer(),
                focused_element=self.mainRadios,
            ),
            key_bindings=self.bindings,
            style=self.style,
            mouse_support=True,
            full_screen=True,
            )
def test_class_combinations_1():
    # In this case, our style has both class 'a' and 'b'.
    # Given that the style for 'a b' is defined at the end, that one is used.
    style = Style([
        ('a', '#0000ff'),
        ('b', '#00ff00'),
        ('a b', '#ff0000'),
    ])
    expected = Attrs(color='ff0000', bgcolor='', bold=False, underline=False,
                     italic=False, blink=False, reverse=False, hidden=False)
    assert style.get_attrs_for_style_str('class:a class:b') == expected
    assert style.get_attrs_for_style_str('class:a,b') == expected
    assert style.get_attrs_for_style_str('class:a,b,c') == expected

    # Changing the order shouldn't matter.
    assert style.get_attrs_for_style_str('class:b class:a') == expected
    assert style.get_attrs_for_style_str('class:b,a') == expected
def main():
    # Printing a manually constructed list of (Token, text) tuples.
    text = [
        (Token.Keyword, 'print'),
        (Token.Punctuation, '('),
        (Token.Literal.String.Double, '"'),
        (Token.Literal.String.Double, 'hello'),
        (Token.Literal.String.Double, '"'),
        (Token.Punctuation, ')'),
        (Token.Text, '\n'),
    ]

    print_formatted_text(PygmentsTokens(text))

    # Printing the output of a pygments lexer.
    tokens = list(pygments.lex('print("Hello")', lexer=PythonLexer()))
    print_formatted_text(PygmentsTokens(tokens))

    # With a custom style.
    style = Style.from_dict({
        'pygments.keyword': 'underline',
        'pygments.literal.string': 'bg:#00ff00 #ffffff',
    })
    print_formatted_text(PygmentsTokens(tokens), style=style)
def test_class_combinations_2():
    # In this case, our style has both class 'a' and 'b'.
    # The style that is defined the latest get priority.
    style = Style([
        ('a b', '#ff0000'),
        ('b', '#00ff00'),
        ('a', '#0000ff'),
    ])
    expected = Attrs(color='00ff00', bgcolor='', bold=False, underline=False,
                     italic=False, blink=False, reverse=False, hidden=False)
    assert style.get_attrs_for_style_str('class:a class:b') == expected
    assert style.get_attrs_for_style_str('class:a,b') == expected
    assert style.get_attrs_for_style_str('class:a,b,c') == expected

    # Defining 'a' latest should give priority to 'a'.
    expected = Attrs(color='0000ff', bgcolor='', bold=False, underline=False,
                     italic=False, blink=False, reverse=False, hidden=False)
    assert style.get_attrs_for_style_str('class:b class:a') == expected
    assert style.get_attrs_for_style_str('class:b,a') == expected
from prompt_toolkit import print_formatted_text, HTML
from prompt_toolkit.styles import Style

print_formatted_text(HTML('<b>This is bold</b>'))
print_formatted_text(HTML('<i>This is italic</i>'))
print_formatted_text(HTML('<u>This is underlined</u>'))

# Colors from the ANSI palette.
print_formatted_text(HTML('<ansired>This is red</ansired>'))
print_formatted_text(HTML('<ansigreen>This is green</ansigreen>'))

# Named colors (256 color palette, or true color, depending on the output).
print_formatted_text(HTML('<skyblue>This is sky blue</skyblue>'))
print_formatted_text(HTML('<seagreen>This is sea green</seagreen>'))
print_formatted_text(HTML('<violet>This is violet</violet>'))

# Colors from the ANSI palette.
print_formatted_text(HTML('<aaa fg="ansiwhite" bg="ansigreen">White on green</aaa>'))

# style
style = Style.from_dict({
    'aaa': '#ff0066',
    'bbb': '#44ff00 italic',
})

print_formatted_text(HTML('<aaa>Hello</aaa> <bbb>world</bbb>!'), style=style)
Beispiel #14
0
# Key bindings.
bindings = KeyBindings()


@bindings.add("c-c")
@bindings.add("q")
def _(event):
    " Quit. "
    event.app.exit()


style = Style.from_dict(
    {
        "status": "reverse",
        "status.position": "#aaaa00",
        "status.key": "#ffaa00",
        "not-searching": "#888888",
    }
)


# create application.
application = Application(
    layout=Layout(root_container, focused_element=text_area),
    key_bindings=bindings,
    enable_page_navigation_bindings=True,
    mouse_support=True,
    style=style,
    full_screen=True,
)
Beispiel #15
0
#from treble.hci.cmd import Reset
#from treble.packet import HCICmd

from treble.controller import Controller
from treble.hci.cmd import HCICmd

from prompt_toolkit.patch_stdout import patch_stdout
from prompt_toolkit.shortcuts import PromptSession

from prompt_toolkit.completion import WordCompleter
from prompt_toolkit.lexers import PygmentsLexer
from prompt_toolkit.styles import Style

style = Style.from_dict({
    'completion-menu.completion': 'bg:#008888 #ffffff',
    'completion-menu.completion.current': 'bg:#00aaaa #000000',
    'scrollbar.background': 'bg:#88aaaa',
    'scrollbar.button': 'bg:#222222',
})

log = logging.getLogger('treble')


def die(msg: str):
    print(msg)
    sys.exit(1)


def init_logging():
    # Set the treble root level: process everything
    log.setLevel(logging.DEBUG)
    formatter = logging.Formatter(style='{',
Beispiel #16
0
    def download_footage(self, output_path=Path('downloaded_clips')):
        """
        - Search for footage & get ID values
        - Try to get the sizes for each clip
        - Download each clip on it's own
        - Name the clip and save it to disk in a folder for each camera
        """
        example = "/api/2.0/recording/5bb829e4b3a28701fe50b258/download"
        meta_cookies = {'cameras.isManagedFilterOn': 'false'}
        meta_cookies.update(self.session.cookies)
        # num_clips = len(self.clip_meta_data)
        url_id_params = str()

        # Progress Bar Styles and Format
        style = Style.from_dict({
            'label': 'bg:#000000 #ffffff',
            'percentage': 'bg:#000000 #ffffff',
            'current': '#448844',
            'bar': '',
        })

        custom_formatters = [
            formatters.Label(),
            formatters.Text(': [', style='class:percentage'),
            formatters.Percentage(),
            formatters.Text(']', style='class:percentage'),
            formatters.Text(' '),
            formatters.Bar(start='[', end=']', sym_a='#', sym_b='>',
                           sym_c='*'),
            formatters.Text('  '),
        ]

        # Create output if it doesn't exist yet
        self.outputPathCheck(output_path)

        # pprint(self.dict_info_clip, indent=4)
        num_clips = len(self.dict_info_clip)

        with ProgressBar(style=style, formatters=custom_formatters) as pb:
            for clip in pb(list(self.dict_info_clip),
                           label=f"Downloading {num_clips} Videos",
                           remove_when_done=False):
                req = requests.Request(
                    'GET',
                    f"{self.url}/api/2.0/recording/{self.dict_info_clip[clip].clip_id}/download",
                    cookies=meta_cookies)
                prepped = req.prepare()
                # print(prepped.url)
                self.logger.debug(
                    f'Attempting to download clip {self.dict_info_clip[clip].clip_id}'
                )

                r = self.session.send(prepped, stream=True)

                if r.status_code is 200:
                    self.logger.debug(
                        f'Successfully requested clip {self.dict_info_clip[clip].clip_id}'
                    )
                elif r.status_code is 401:
                    self.logger.critical(f'Unauthorized, exiting.')
                    sys.exit(1)
                else:
                    self.logger.critical(
                        f'Unexpected error occured: {r.status_code}. Exiting.')
                    # pprint(r.text)
                    sys.exit(1)

                total = r.headers.get('Content-Length')
                num_chunks = round(int(total) / self.chunk_size)

                file_path = Path(
                    output_path,
                    self.dict_info_clip[clip].cameraName.replace(' ', '_'),
                    self.dict_info_clip[clip].fullFileName)
                if not file_path.parent.exists():
                    file_path.parent.mkdir(exist_ok=True)

                with open(file_path, 'wb') as f:
                    for data in pb(
                            r.iter_content(chunk_size=self.chunk_size),
                            total=num_chunks,
                            label=f'Downloading',
                            remove_when_done=False,
                    ):
                        f.write(data)
Beispiel #17
0
    if os.path.exists(dst) or os.path.islink(dst):
        os.unlink(dst)
    os.symlink(src, dst)


def _get_dirs(target_dir):
    if not os.path.isdir(target_dir):
        _echo(cformat('%{red}Directory not found:%{red!} {}').format(target_dir))
        sys.exit(1)
    return get_root_path('indico'), os.path.abspath(target_dir)


PROMPT_TOOLKIT_STYLE = Style.from_dict({
    'help': '#aaaaaa',
    'prompt': '#5f87ff',
    'default': '#dfafff',
    'bracket': '#ffffff',
    'colon': '#ffffff',
    '': '#aaffaa',  # user input
})


def _prompt(message, default='', path=False, list_=None, required=True, validate=None, allow_invalid=False,
            password=False, help=None):
    def _get_prompt_tokens():
        rv = [
            ('class:prompt', message),
            ('class:colon', ': '),
        ]
        if first and help:
            rv.insert(0, ('class:help', wrap_text(help) + '\n'))
        return rv
Beispiel #18
0
class PyreRepl:
    file = None

    buffer: List[str]
    pyre = PyreParser()
    hist_file_name = '.pyre_repl_history'

    style = merge_styles([
        style_from_pygments_cls(FruityStyle),
        Style.from_dict({
            '': '#0a0eff',
            'pygments.text': '#0a0eff',
            'prompt': '#00ff00 bold',
            'pygments.number': '#7ec0ee'
        })
    ])
    prompt = [('class:prompt', ' Pyre >> ')]

    ruler = '='

    def __init__(self, *args, **kwargs):
        self.buffer = []
        self.hist_file = os.path.expanduser(f'~/{self.hist_file_name}')
        self.should_exit = False
        self.keywords = list(self.pyre.control_handlers.keys()) + list(
            self.pyre.rp.rule_handlers.keys())
        self.completer = WordCompleter(self.keywords)
        open(self.hist_file, 'a').close()
        self.session = PromptSession(history=FileHistory(self.hist_file))

    def emptyline(self):
        return

    def preloop(self):
        pass

    def print_topics(self, header, cmds, cmdlen, maxcol):
        if cmds:
            msg('blue', str(header))
            if self.ruler:
                print("%s" % str(self.ruler * len(header)))
            print(GREEN)
            columnize(cmds, maxcol - 1)
            print(RESET)

    def do_exit(self, *args):
        self.should_exit = True

    def do_help(self, *args):
        """List available commands with "help" or detailed help with "help cmd"."""
        if len(args) == 0:
            pyre_help_header = 'Pyre REPL Commands'
            self.print_topics(pyre_help_header, list(self.extra_cmds.keys()),
                              15, 80)

    def do_show(self, fmt=None, fmt2=None, *args):
        if fmt is not None:
            self.pyre = PyreParser()
            fmt_v4 = ['ipt', 'iptables', 'ip4', 'ipt4', 'iptables4']
            fmt_v6 = ['ipt6', 'ip6tables', 'ip6', 'iptables6']
            ip4, ip6 = self.pyre.parse_lines(self.buffer)
            if fmt in fmt_v4 or fmt2 in fmt_v4:
                msg('green',
                    'Current session parsed into IPv4 iptables rules:')
                msg('blue', '### Begin IPTables v4 Rules ###')
                for l in ip4:
                    print(l)
                msg('blue', '### End IPTables v4 Rules ###')

            if fmt in fmt_v6 or fmt2 in fmt_v6:
                msg('green',
                    'Current session parsed into IPv6 iptables rules:')
                msg('blue', '### Begin IPTables v6 Rules ###')
                for l in ip6:
                    print(l)
                msg('blue', '### End IPTables v6 Rules ###')
            return

        msg('green', 'Current PyreWall rules executed during this session:')
        msg('### Begin Pyre Rules ###')
        tokens = list()
        for l in self.buffer:
            if empty(l.strip()): continue
            tokens += list(pygments.lex(l.strip(), lexer=PyreLexer()))
        print_formatted_text(PygmentsTokens(tokens), style=self.style, end='')
        # print(l.strip())
        msg('### End Pyre Rules ###')

    def parse_lines(self, *lines):
        v4r, v6r = [], []
        msg()
        for ln in lines:
            l = ln.split()
            if len(l) == 0: continue
            cmdname, args = l[0].strip(), l[1:]

            cmds = [d for d in dir(self.__class__) if d[0:3] == 'do_']
            if 'do_' + cmdname in cmds:
                getattr(self, 'do_' + cmdname)(*args)
                continue

            if cmdname in self.extra_cmds:
                self.extra_cmds[cmdname](self, *args)
                continue

            try:
                _v4r, _v6r = self.pyre._parse(line=ln)
                if _v4r is None and _v6r is None:
                    msg(
                        'yellow',
                        "# Warning: The line entered does not appear to be a valid command, nor valid Pyre."
                    )
                    continue
                self.buffer.append(ln)
                v4r += _v4r
                v6r += _v6r
            except (BaseException, Exception) as e:
                msg('red', 'Got exception while parsing Pyre line!')
                msg('red', 'Exception:', type(e), str(e))
                return
        if len(v4r) > 0:
            msg('blue', '### IPv4 Rules ###')
            for r in v4r:
                print(r)
            msg('blue', '### End IPv4 Rules ###\n')
        if len(v6r) > 0:
            msg('blue', '### IPv6 Rules ###')
            for r in v6r:
                print(r)
            msg('blue', '### End IPv6 Rules ###\n')
        msg()

    def default(self, line):
        return self.parse_lines(*line.split('\n'))
        # line = str(line).strip()
        # if '\n' in line:
        #     lines = line.split('\n')
        #
        #
        # l = line.split()
        # if len(l) == 0: return
        # cmdname, args = l[0].strip(), l[1:]
        #
        # cmds = [d for d in dir(self.__class__) if d[0:3] == 'do_']
        # if 'do_' + cmdname in cmds:
        #     log.info('Calling %s', 'do_' + cmdname)
        #     return getattr(self, 'do_' + cmdname)(*args)
        # if cmdname in self.extra_cmds:
        #     log.info('Calling extra cmd %s', cmdname)
        #     return self.extra_cmds[cmdname](self, *args)
        #
        # try:
        #     # noinspection PyProtectedMember
        #     v4r, v6r = self.pyre._parse(line=line)
        #     if v4r is None and v6r is None:
        #         msg('yellow', "Warning: The line entered does not appear to be a valid command, nor valid Pyre.")
        #         return
        #     self.buffer.append(line)
        #     if len(v4r) > 0:
        #         msg('blue', '### IPv4 Rules ###')
        #         for r in v4r:
        #             print(r)
        #         msg('blue', '### End IPv4 Rules ###\n')
        #     if len(v6r) > 0:
        #         msg('blue', '### IPv6 Rules ###')
        #         for r in v6r:
        #             print(r)
        #         msg('blue', '### End IPv6 Rules ###\n')
        #
        # except (BaseException, Exception) as e:
        #     msg('red', 'Got exception while parsing Pyre line!')
        #     msg('red', 'Exception:', type(e), str(e))
        #     return

    def do_py(self, *args):
        """Execute raw python code for debugging purposes"""
        print(eval(' '.join(args)))

    def precmd(self, line):
        return

    def postcmd(self, line):
        readline.set_auto_history(False)
        return self.should_exit

    def cmdloop(self):
        while not self.should_exit:
            data = self.session.prompt(self.prompt,
                                       lexer=PygmentsLexer(PyreLexer),
                                       style=self.style,
                                       completer=self.completer)
            self.precmd(data)
            self.default(data)
            self.postcmd(data)

    extra_cmds = {
        '\\?': do_help,
        '\\print': do_show,
        '\\show': do_show,
    }
Beispiel #19
0
from prompt_toolkit.filters import Condition
from prompt_toolkit.application.current import get_app
from pymodbus.repl.helper import get_commands
from pymodbus.compat import string_types


@Condition
def has_selected_completion():
    complete_state = get_app().current_buffer.complete_state
    return (complete_state is not None and
            complete_state.current_completion is not None)


style = Style.from_dict({
    'completion-menu.completion': 'bg:#008888 #ffffff',
    'completion-menu.completion.current': 'bg:#00aaaa #000000',
    'scrollbar.background': 'bg:#88aaaa',
    'scrollbar.button': 'bg:#222222',
})


class CmdCompleter(Completer):
    """
    Completer for Pymodbus REPL.
    """

    def __init__(self, client, commands=None, ignore_case=True):
        """

        :param client: Modbus Client
        :param commands: Commands to be added for Completion (list)
        :param ignore_case: Ignore Case while looking up for commands
Beispiel #20
0
class Controller:
    analog_completer = AnalogCompleter(
        [['show', 'set', 'train', 'clear', 'help', 'exit'],
         ['statistics', 'analysis', 'log', 'progress'],
         ['requests', 'IP', 'UA', 'url'], ['current', 'last'],
         ['day', 'week', 'month', 'year', 'all'], ['top']],
        ignore_case=True,
    )
    style = Style.from_dict({
        # User input (default text).
        '': '#ff0066',
        # Prompt.
        'yellow': 'yellow',
        'green': 'green',
        'blue': 'blue',
        'black': 'black',
        'white': 'white',
        'analog': 'yellow',
        'help': 'blue',
        'cyan': 'ansicyan',
        'help_title': 'red',
    })
    analog_prompt = [
        ('class:yellow', 'analog> '),
    ]

    def __init__(self, path=None):

        self.path = path
        self.config_path = os.path.join(self.path, "config.ini")
        self.time = datetime.now()
        self.output = ColorOutput()
        self.output.print_banner("AnaLog")

        if os.path.exists(self.config_path):
            self.config = configparser.ConfigParser()
            self.config.read(self.config_path)
        else:
            raise DatabaseConfigError

        self.log_path = self.config.get('log', 'path')

        self.db = db(self.config)
        self.section_name_database = "Database"
        self.ip_db = ipdb.Reader(os.path.join(self.path,
                                              "analog/ipdb/ip.ipdb"))

        # 数据库初始化
        if self.config.get(self.section_name_database, 'initial') != '1':
            self.init_database()
            self.config.set(self.section_name_database, 'initial', '1')

            self.init_table()
            self.config.write(open(self.config_path, "w"))
        else:
            try:
                self.db.connect_db()
            except Exception as e:
                self.output.print_error(
                    "Connect DB failed. Please check your config file or make sure DB server is running."
                )
                self.output.print_info("Bye~")
                exit(0)
            self.output.print_info("Log had been loaded before.Type " +
                                   self.output.Fore.BLUE + "reload" +
                                   self.output.Style.RESET_ALL +
                                   self.output.Fore.LIGHTYELLOW_EX +
                                   " to flush")

        self.statistic = Statistics(database=self.db,
                                    output=self.output,
                                    ipdb=self.ip_db,
                                    controller=self)

        self.session = PromptSession()
        self.mode = NORMAL_MODE
        self.key_bindings = None

        # TF-IDF向量填充词料库
        tfidf_exist_flag = False
        self.tfidfVector = None
        try:
            self.tfidfVector = TfidfVector()
            tfidf_exist_flag = True
        except FileEmptyError:
            print_formatted_text(HTML(
                '<yellow>Detected that train.txt content is empty. '
                '<red>Disable abnormal detection.</red></yellow>'),
                                 style=self.style)
        except FileNotFound:
            print_formatted_text(HTML(
                '<yellow>Detected that train.txt does no exist. '
                '<red>Disable abnormal detection.</red></yellow>'),
                                 style=self.style)

        self.model = None
        self.queue = None
        self.train_progress = 0

        # 模型缓存载入
        if tfidf_exist_flag is False:
            print_formatted_text(HTML(
                '<yellow>Cause of you lack of TF-IDF corpus'
                '(<red>analog/sample_set/train.txt</red>), We can\'t calculate TF-IDF value'
                'of each log item and can\'t use train model also</yellow>'),
                                 style=self.style)
        elif self.check_model_cache_exist():
            print_formatted_text(HTML(
                '<yellow>Detection model cache file exist.\nLoad model from it.\nYou can type '
                '<blue>retrain</blue> to train a new model.</yellow>'),
                                 style=self.style)
            self.load_model()

        else:
            try:
                if self.train() is False:
                    print_formatted_text(HTML(
                        '<yellow>Train Failed! Cause of lack of train sample. '
                        '\nVisit <blue>https://www.wzsite.cn</blue> for help.</yellow>'
                    ),
                                         style=self.style)
            except Exception as e:
                raise e

        self.logger = Logger(database=self.db,
                             output=self.output,
                             ipdb=self.ip_db,
                             controller=self,
                             tfidfvector=self.tfidfVector,
                             model=self.model)
        self.analyser = Analyser(database=self.db,
                                 ipdb=self.ip_db,
                                 controller=self,
                                 tfidfvector=self.tfidfVector,
                                 model=self.model)

    def mainloop(self):
        self.key_bindings = KeyBindings()

        @Condition
        def is_logging_mode():
            return self.mode == LOGGING_MODE

        @self.key_bindings.add('up')
        def _(event):
            self.clear_screen()
            self.logger.show_log(decrease=True)
            print_formatted_text(HTML(
                '<yellow>Press <blue>↑</blue> or <blue>↓</blue> to turn pages or '
                '<blue>Esc</blue> to quit log mode</yellow>'),
                                 style=self.style)
            print_formatted_text(FormattedText(self.analog_prompt), end="")

        @self.key_bindings.add('down')
        def _(event):
            self.clear_screen()
            self.logger.show_log(increase=True)
            print_formatted_text(HTML(
                '<yellow>Press <blue>↑</blue> or <blue>↓</blue> to turn pages or '
                '<blue>Esc</blue> to quit log mode</yellow>'),
                                 style=self.style)
            print_formatted_text(FormattedText(self.analog_prompt), end="")

        @self.key_bindings.add('escape')
        def _(event):
            # self.clear_screen()
            self.mode = NORMAL_MODE
            self.logger.clear()
            print_formatted_text(
                HTML('<yellow>\nBack to normal mode.\nanalog> </yellow>'),
                style=self.style,
                end="")

        self.key_bindings = ConditionalKeyBindings(
            key_bindings=self.key_bindings, filter=is_logging_mode)

        while True:
            text = None
            try:
                text = self.session.prompt(self.analog_prompt,
                                           style=self.style,
                                           completer=self.analog_completer,
                                           key_bindings=self.key_bindings,
                                           refresh_interval=True)
                self.command_parser(text)

            except KeyboardInterrupt:
                if self.mode == NORMAL_MODE:
                    self.output.print_info(
                        "Are you wanna exit? Type 'exit' to quit the analog.")
                else:
                    self.output.print_lastLine(
                        "Press key Esc to quit log mode.")
            except CommandFormatError:
                self.output.print_error(
                    "Unknown command: {}. Type \"help\" for help.".format(
                        text if text else "(Failed to read command)"))

    def train(self):
        if not self.check_train_txt():
            return False
        model_train = Train(self.path)
        self.queue = multiprocessing.Manager().Queue()
        pool = multiprocessing.Pool(1)
        pool.apply_async(model_train.get_model,
                         args=(self.queue, ),
                         callback=self.load_model)
        pool.close()
        print_formatted_text(HTML(
            '<yellow>Start the model training process.'
            '\nYou can type <blue>train progress</blue> to get the progress of training.</yellow>'
        ),
                             style=self.style)

    def check_train_txt(self):
        train_log_path = os.path.join(self.path, "analog/sample_set/train.txt")
        test_black_path = os.path.join(self.path,
                                       "analog/sample_set/test_black_log.txt")
        test_raw_path = os.path.join(self.path,
                                     "analog/sample_set/test_white_log.txt")
        check_path = [train_log_path, test_black_path, test_raw_path]
        flag = True
        for path in check_path:
            try:
                if check_txt(path) is False:
                    flag = False
                    print_formatted_text(
                        HTML('<yellow>Necessary file ' + path +
                             ' is <red>empty</red> ! </yellow>'),
                        style=self.style)
            except FileNotFound:
                print_formatted_text(HTML('<yellow>Necessary file ' + path +
                                          '  <red>Not found</red> ! </yellow>'
                                          '\nGenerate file automatically.'),
                                     style=self.style)
        return flag

    def train_complete(self):
        self.load_model()
        print_formatted_text(HTML(
            '\n<blue>Training task complete! '
            'Try to type command <white>show analysis</white> to show'
            'abnormal analysis!</blue>'),
                             style=self.style)
        self.logger.model = self.model

    def load_model(self, *args):
        with open(os.path.join(self.path, r'analog/cache/model.pkl'),
                  'rb') as file:
            self.model = pickle.load(file)

    def help(self):
        help_text = FormattedText([
            ('class:help_title', 'Usage:\n'),
            ('class:green', ' ' * 4 + 'show  '),
            ('class:yellow', '<statistics|analysis|log>  '),
            ('class:yellow', '<IP|requests|UA|url>  '),
            ('class:green', '<current|last>  '),
            ('class:yellow', '<day|week|month|year|all>  '),
            ('class:yellow', '(top N)\n'),
            ('class:help_title', 'Example:\n'),
            ('class:yellow',
             " " * 4 + 'show statistics requests current day'.ljust(50)),
            ('class:white',
             'Draw a chart to show statistics of website visit\n'),
            ('class:yellow',
             " " * 4 + 'show statistics url last week top 10'.ljust(50)),
            ('class:white', 'Draw a chart to show statistics of requests \n'),
            ('class:yellow',
             " " * 4 + 'show analysis of current day'.ljust(50)),
            ('class:white',
             'Display log analysis using abnormal detection model.\n'),
            ('class:yellow', " " * 4 + 'show log of current day'.ljust(50)),
            ('class:white', 'Display the log in a table.\n'),
            ('class:help_title', 'More:\n'),
            ('class:yellow', " " * 4 + 'train|retrain'.ljust(50)),
            ('class:white', 'Train a model\n'),
            ('class:yellow', " " * 4 + 'train progress'.ljust(50)),
            ('class:white', 'Get progress of training model\n'),
            ('class:yellow',
             " " * 4 + 'set time|day|month|year|offset N'.ljust(50)),
            ('class:white', 'Set values\n'),
            ('class:help_title', 'More information:\n'),
            ('class:blue', " " * 4 + '<https://www.wzstie.cn>\n'),
            ('class:blue', " " * 4 + '<https://github.com/Testzero-wz>\n'),
        ])
        print_formatted_text(help_text, style=self.style)

    def get_train_progress(self):
        """
        获取训练模型进程的进度
        :return:
        """
        while True:
            try:
                if not self.queue:
                    return
                progress = self.queue.get_nowait()
                if progress is None or progress == '':
                    break
            except Exception:
                break
            self.train_progress = progress
        return self.train_progress

    def set_time(self,
                 year=None,
                 month=None,
                 day=None,
                 hour=None,
                 minute=0,
                 second=0) -> datetime:
        year = year if year is not None else self.time.year
        month = month if month is not None else self.time.month
        day = day if day is not None else self.time.day
        hour = hour if hour is not None else self.time.hour
        self.time = datetime(year=year,
                             month=month,
                             day=day,
                             hour=hour,
                             minute=minute,
                             second=second)
        return self.time

    def command_parser(self, command: str):
        """
        命令解析
        """
        command = command.lower()
        command = command.split()
        if len(command) == 0:
            return
        try:
            c = command[0]
            if c == 'show':
                if command[1] == 'statistics':
                    if command[2] == 'requests':
                        self.statistic.requests_num(
                            command[4],
                            True if command[3] == 'current' else False,
                            True if command[-1] == 'c' else False)
                    else:
                        if len(command) >= 7:
                            N = command[6]
                            self.statistic.top_n(
                                command[2],
                                command[4],
                                current_flag=(True if command[3] == 'current'
                                              else False),
                                N=N)
                        else:
                            self.statistic.top_n(
                                command[2],
                                command[4],
                                current_flag=(True if command[3] == 'current'
                                              else False))
                elif command[1] == 'analysis':
                    if command[2] == 'of' and command[3] == 'current':
                        self.analyser.show_analysis(command[4])
                elif command[1] == 'ip':
                    self.output.print_info("-".join(
                        self.statistic.ip_geolocation(command[2])))

                elif command[1] == 'log':
                    flag = False
                    if command[3] == 'ip':
                        self.logger.set_mode('ip')
                        self.logger.set_ip(command[4])
                        flag = self.logger.show_log()

                    elif command[3] in ['current', 'last']:
                        self.logger.set_mode('date')
                        flag = self.logger.show_log(
                            when=command[4],
                            current_flag=True
                            if command[3] == 'current' else False)

                    self.mode = LOGGING_MODE if flag else NORMAL_MODE

            elif c == 'set':

                if command[1] == 'time':
                    t = datetime.strptime(command[2], "%Y/%m/%d:%H")
                    self.output.print_changed(
                        "time",
                        self.set_time(
                            year=t.year, month=t.month, day=t.day,
                            hour=t.hour).strftime("%Y/%m/%d %H:00:00"))

                elif command[1] in ['hour', 'day', 'month', 'year']:
                    d = dict()
                    d[command[1]] = int(command[2])
                    self.output.print_changed(
                        "time",
                        self.set_time(**d).strftime("%Y/%m/%d %H:00:00"))
                    del d
                elif command[1] == 'offset':
                    self.logger.set_offset(int(command[2]))

                else:
                    raise CommandFormatError
            elif c == 'train' or c == 'retrain':
                if len(command) == 1:
                    self.train()
                elif command[-1] == "progress":
                    progress = float(self.get_train_progress()) * 100
                    if progress == 100:
                        self.train_complete()
                    else:
                        self.output.print_info(
                            "Now train progress is {:0.2f}%".format(progress))
            elif c == 'clear':
                self.clear_screen()
            elif c == 'help':
                self.help()
            elif c == 'exit':
                print_formatted_text(FormattedText([('class:yellow',
                                                     '[*] Bye~')]),
                                     style=self.style)
                exit(0)
            elif c == 'get':
                if command[1] == 'model' and command[2] == 'parameter':
                    if self.model:
                        params = self.model.get_params('nu')
                        print_formatted_text(FormattedText([
                            ('class:yellow', 'nu: {}'.format(params.get('nu')))
                        ]),
                                             style=self.style)
                        print_formatted_text(FormattedText([
                            ('class:yellow',
                             'gamma: {}'.format(params.get('gamma')))
                        ]),
                                             style=self.style)
                    else:
                        self.output.print_info("No model has been loaded.")
            else:
                raise CommandFormatError
        except Exception as e:
            raise CommandFormatError
            # import traceback
            # traceback.print_exc()
            # raise Exception(e)

    @staticmethod
    def clear_screen():
        os.system('cls' if os.name == 'nt' else 'clear')

    def init_database(self):
        """
        数据库初始化,数据库创建以及表创建
        """
        self.db.create_db()
        conn = self.db.connect_db()
        cursor = conn.cursor()

        # Create Table: access_record
        sql = """
                 CREATE TABLE IF NOT EXISTS access_count (
                 ip  CHAR(15) NOT NULL PRIMARY KEY,
                 total_access  INT DEFAULT 0,
                 year_access INT DEFAULT 0,  
                 month_access INT DEFAULT 0,
                 week_access INT DEFAULT 0, 
                 day_access INT DEFAULT 0);
                 """
        cursor.execute(sql)

        # Create Table: weblog
        sql = """CREATE TABLE IF NOT EXISTS weblog (
                             remote_addr  CHAR(15) NOT NULL ,
                             remote_user  VARCHAR(20),
                             time DATETIME DEFAULT '1970-01-01 00:00:00',
                             request TEXT DEFAULT NULL,
                             status  SMALLINT  UNSIGNED DEFAULT 0, 
                             body_bytes_sent INT DEFAULT 0,
                             http_referer TEXT DEFAULT NULL,
                             http_user_agent TEXT DEFAULT NULL);
                             """
        cursor.execute(sql)
        conn.commit()
        conn.close()
        self.output.print_info("Initial database => Done.")

    def init_table(self):
        """
        数据库表单载入初始化
        将日志载入数据库中
        """
        count = 0
        arg_list = []
        log_Pattern = r'^(?P<remote_addr>.*?) - (?P<remote_user>.*) \[(?P<time_local>.*?) \+[0-9]+?\] "(?P<request>.*?)" ' \
                      '(?P<status>.*?) (?P<body_bytes_sent>.*?) "(?P<http_referer>.*?)" "(?P<http_user_agent>.*?)"$'

        sql = "INSERT weblog(" \
              "remote_addr,remote_user,time,request,status," \
              "body_bytes_sent,http_referer,http_user_agent)  " \
              "VALUES " \
              "(%s,%s,%s,%s,%s,%s,%s,%s)"

        log_regx = re.compile(log_Pattern)
        with open(self.log_path, "r") as file:
            line = file.readline().strip("\r\n")
            while line:
                log_tuple = log_regx.search(line)
                line = file.readline().strip("\r\n")
                if log_tuple is not None:
                    # continue
                    t = datetime.strptime(log_tuple.group('time_local'),
                                          "%d/%b/%Y:%H:%M:%S")
                    arg_list.append((log_tuple.group('remote_addr'),
                                     log_tuple.group('remote_user'),
                                     t.strftime("%Y/%m/%d %H:%M:%S"),
                                     log_tuple.group('request'),
                                     int(log_tuple.group('status')),
                                     int(log_tuple.group('body_bytes_sent')),
                                     log_tuple.group('http_referer'),
                                     log_tuple.group('http_user_agent')))
                    count += 1
                if count % 5000 == 0 or line is None or line == "":
                    self.db.execute_many(sql, arg_list)
                    self.output.print_lastLine(
                        "Total process: {} logs".format(count))
                    self.db.commit()
                    arg_list.clear()
        self.output.print_info("Total process: {} logs".format(count))
        self.output.print_info("Load log => Done.")

    def get_time_condition(self,
                           when: str,
                           time_change=False,
                           current_flag=False) -> str:
        """
        数据查询时间(WHERE)条件获取
        :param when: ['day','week','month','year']
        :param time_change: 是否改变当前时间(减1单位)
        :param current_flag: 判断是current还是last
        :return: SQL语句约束(WHERE)时间条件
        """
        time_condition_sql = None
        t_w = None
        when = when.lower()

        if when == 'hour':
            t_w = self.time + relativedelta(hours=-1)
            if current_flag:
                time_condition_sql = " DATE(time) = '" + self.time.strftime("%Y-%m-%d") \
                                     + "' AND HOUR(time) = '" + self.time.strftime("%H") + "'"
            else:
                time_condition_sql = " time BETWEEN ('" + self.time.strftime("%Y-%m-%d %H:%M:00") \
                                     + "' - INTERVAL 1 HOUR ) and '" + self.time.strftime(
                        "%Y-%m-%d %H:%M:00") + "'"

        elif when == 'day':
            t_w = self.time + relativedelta(days=-1)
            if current_flag:
                time_condition_sql = " DATE(time) = '" + self.time.strftime(
                    "%Y-%m-%d") + "'"
            else:
                time_condition_sql = " time BETWEEN ('" + self.time.strftime("%Y-%m-%d %H:00:00") \
                                     + "' - INTERVAL 1 DAY ) and '" + self.time.strftime(
                        "%Y-%m-%d %H:00:00") + "'"
        elif when == 'week':
            t_w = self.time + relativedelta(weeks=-1)
            if current_flag:
                time_condition_sql = " YEAR(time) = '" + self.time.strftime(
                    "%Y") + "'and  WEEK(time) = '" + self.time.strftime(
                        "%W") + "'"
            else:
                time_condition_sql = " time BETWEEN ('" + self.time.strftime("%Y-%m-%d") \
                                     + "' - INTERVAL 1 WEEK ) and '" + self.time.strftime("%Y-%m-%d") + "'"
        elif when == 'month':
            t_w = self.time + relativedelta(months=-1)
            if current_flag:
                time_condition_sql = " YEAR(time) = '" + self.time.strftime(
                    "%Y") + "'and MONTH(time) = '" + self.time.strftime(
                        "%m") + "'"
            else:
                time_condition_sql = " time BETWEEN ('" + self.time.strftime("%Y-%m-%d") \
                                     + "' - INTERVAL 1 MONTH ) and '" + self.time.strftime("%Y-%m-%d") + "'"
        elif when == 'year':
            t_w = self.time + relativedelta(years=-1)
            if current_flag:
                time_condition_sql = " YEAR(time) = '" + self.time.strftime(
                    "%Y") + "'"
            else:
                time_condition_sql = " time BETWEEN ('" + self.time.strftime("%Y-%m-%d") \
                                     + "' - INTERVAL 1 YEAR ) and '" + self.time.strftime("%Y-%m-%d") + "'"
        if time_change:
            self.time = t_w

        return time_condition_sql

    def get_date_list(self,
                      when: str,
                      d_list: list,
                      time: datetime,
                      d_format_list=None):
        t = time
        if when == 'year':
            d_list.append(t.month)
            if d_format_list is not None:
                d_format_list.append(t.strftime("%Y-%m"))
        elif when == 'month':
            d_list.append(t.day)
            if d_format_list is not None:
                d_format_list.append(t.strftime("%Y-%m-%d"))
        elif when == 'week':
            d_list.append(t.day)
            if d_format_list is not None:
                d_format_list.append(t.strftime("%Y-%m-%d"))
        elif when == 'day':
            d_list.append(t.hour)
            if d_format_list is not None:
                d_format_list.append(t.strftime("%Y-%m-%d %H:00:00"))
        elif when == 'hour':
            d_list.append(t.minute)
            if d_format_list is not None:
                d_format_list.append(t.strftime("%H:%M:00"))

    def check_model_cache_exist(self):
        model_path = os.path.join(self.path, 'analog/cache/model.pkl')
        try:
            if os.path.getsize(model_path) != 0:
                return True
        except FileNotFoundError:
            return False
        return False
Beispiel #21
0
from prompt_toolkit.shortcuts import checkboxlist_dialog, message_dialog
from prompt_toolkit.styles import Style

results = checkboxlist_dialog(title="CheckboxList dialog",
                              text="What would you like in your breakfast ?",
                              values=[("eggs", "Eggs"), ("bacon", "Bacon"),
                                      ("croissants", "20 Croissants"),
                                      ("daily", "The breakfast of the day")],
                              style=Style.from_dict({
                                  'dialog':
                                  'bg:#cdbbb3',
                                  'button':
                                  'bg:#bf99a4',
                                  'checkbox':
                                  '#e8612c',
                                  'dialog.body':
                                  'bg:#a9cfd0',
                                  'dialog shadow':
                                  'bg:#c98982',
                                  'frame.label':
                                  '#fcaca3',
                                  'dialog.body label':
                                  '#fd8bb6',
                              })).run()
if results:
    message_dialog(title="Room service",
                   text="You selected: %s\nGreat choice sir !" %
                   ",".join(results)).run()
else:
    message_dialog("*starves*").run()
Beispiel #22
0
def main():  # protect
    RADAR_DB_PATH = '/store/msrad/radar/radar_database/'

    style_info = Style.from_dict({
        # User input (default text).
        'command': '#54B5EE',
        '': '#DCDCDC',
    })

    style = Style.from_dict({
        # User input (default text).
        '': '#ff0066',

        # Prompt.
        'username': '******',
    })

    style_prompt = Style.from_dict({
        # User input (default text).
        '': '#DCDCDC',
    })

    style_warning = Style.from_dict({
        # User input (default text).
        '': '#FF4500',
    })

    style_ok = Style.from_dict({
        # User input (default text).
        '': '#00FF7F',
    })

    title1 = """-----------------------------------------
QPE database python interface
Daniel Wolfensberger, LTE-MeteoSwiss, 2019
------------------------------------------"""

    title2 = """-----------------------------------------
Database operations menu
------------------------------------------"""

    title3 = """-----------------------------------------
QPE menu
------------------------------------------"""

    dbase = Database()

    current_menu = 'main'

    info = {}
    info['main'] = FormattedText([
        ('class:command', 'db'),
        ('',
         ': Enter database submenu (e.g. data queries, addition of new data) \n'
         ), ('class:command', 'qpe'),
        ('', ': Enter qpe submenu (e.g. compute qpe, generate maps) \n'),
        ('class:command', 'e'), ('', ': exit program \n')
    ])

    info['db'] = FormattedText([
        ('class:command', 'load_cscs'),
        ('', ': load all tables available on CSCS \n'),
        ('class:command', 'load'),
        ('', ': load one or several new tables from files \n'),
        ('class:command', 'query'), ('', ': run a new query \n'),
        ('class:command', 'populate'),
        ('', ': populate the database with new data \n'),
        ('class:command', 'display <name_of_table> n'),
        ('', ': displays n rows of a loaded table \n')
    ])

    info['qpe'] = FormattedText([
        ('class:command', 'compute'),
        ('', ': compute the QPE estimate for a given time period \n'),
        ('class:command', 'plot'),
        ('', ': plot a set of QPE estimates of a given time period \n')
    ])

    print(title1, style=style)

    code = None
    current_query = None
    while (code != 'e'):
        try:
            code = prompt(
                'Enter command (i for info), use ctrl+c to return to main menu anytime ',
                style=style_prompt)
            ########
            # Info
            ########
            if code == 'i':
                print(info[current_menu], style=style_info)

            if current_menu == 'main':
                if code == 'db':
                    print(title2, style=style)
                    current_menu = 'db'
                elif code == 'qpe':
                    print(title3, style=style)
                    current_menu = 'qpe'

            ########
            # Populate
            ########
            if current_menu == 'db':
                if 'display' in code:
                    nametab = code.split(' ')[1]
                    try:
                        nrows = int(code.split(' ')[2])
                    except:
                        print('Invalid number of rows, using 10',
                              style=style_warning)
                        nrows = 10

                    if nametab not in dbase.tables.keys():
                        print(
                            'Table name {:s} is not in loaded table names: {:s}'
                            .format(nametab,
                                    ','.join(list(dbase.tables.keys()))),
                            style=style_warning)
                    else:
                        dbase.tables[nametab].show(nrows)

                elif code == 'populate':

                    n = prompt_check(
                        'With which type of data would you like to populate the database: "gauge", "radar" or "reference"? ',
                        ['gauge', 'radar', 'reference'])

                    if n == 'gauge':
                        txt = 'Indicate start time of the data you want to add (format YYYYMMDDHHMM, HHMM is optional) '
                        t0 = prompt_check(txt, ['%Y%m%d', '%Y%m%d%H%M'])
                    else:
                        txt = 'Indicate start time of the data you want to add (format YYYYmmddHMM, HHMM is optional), leave empty to select automatically from gauge data: '
                        t0 = prompt_check(txt, ['%Y%m%d', '%Y%m%d%H%M', ''])

                    if n == 'gauge':
                        txt = 'Indicate end time of the data you want to add (format YYYYMMDDHHMM, HHMM is optional) '
                        t1 = prompt_check(txt, ['%Y%m%d', '%Y%m%d%H%M'])
                    else:
                        txt = 'Indicate end time of the data you want to add (format YYYYmmddHMM, HHMM is optional), leave empty to select automatically from gauge data: '
                        t1 = prompt_check(txt, ['%Y%m%d', '%Y%m%d%H%M', ''])

                    if n != 'gauge':
                        txt = """Select the gauge tables that will be used as a reference to select timesteps, indicate either the filepaths or the name of a table that has previously been added with the load table instruction: """
                        g = prompt_check(txt, list(dbase.tables.keys()))

                    o = prompt_check(
                        'Enter the location where the generated files will be stored: ',
                        check='makedirs',
                        default=str(Path(RADAR_DB_PATH, n)) + os.sep)

                    script_path = os.path.dirname(os.path.realpath(__file__))
                    default_config_path = Path(script_path, 'database',
                                               'default_config.yml')

                    c = prompt_check(
                        'Enter location of the configuration file (in yml format): ',
                        check='yaml',
                        default=str(default_config_path))

                    print(
                        'You want to update the database with the following parameters...'
                    )
                    print('Data type: ' + n)
                    print('Starting time: ' + str(t0))
                    print('End time: ' + str(t1))
                    if n != 'gauge':
                        print('Gauge reference: ' + g)
                    print('Output folder: ' + o)
                    print('Config file: ' + c)

                    ok = prompt('Do you want to start y/n: ')
                    if ok == 'y':
                        dbase.config_file = c
                        if n == 'gauge':
                            dbase.update_station_data(t0, t1, o)
                        elif n == 'reference':
                            dbase.update_reference_data(g, o, t0, t1)
                        elif n == 'radar':
                            dbase.update_radar_data(g, o, t0, t1)

                ########
                # Load
                ########
                elif code == 'load_cscs':
                    dic = {
                        'gauge': RADAR_DB_PATH + 'gauge/*.csv.gz',
                        'radar': RADAR_DB_PATH + 'radar/*.parquet',
                        'reference': RADAR_DB_PATH + 'reference/*.parquet'
                    }
                    try:
                        dbase.add_tables(dic)
                        print(
                            'The CSCS tables, "radar" "reference" and "gauge" were successfully added',
                            style=style_ok)
                    except Exception as e:
                        print('Could not CSCS add table!', style=style_warning)
                        print(e, style=style_warning)

                elif code == 'load':
                    n = prompt(
                        'Enter name of table(s) (you choose), use comma to separate multiple entries: ',
                        default='radar')
                    if n == 'gauge':
                        default_suf = '*.csv.gz'
                    else:
                        default_suf = '*.parquet'
                    d = prompt(
                        'Enter filepaths (ex. /mydir/*.csv) where the table(s) are stored, use comma to separate multiple entries: ',
                        default=str(Path(RADAR_DB_PATH, n, default_suf)))

                    try:
                        dic = {}
                        for nn, dd in zip(n.split(','), d.split(',')):
                            dic[nn] = dd
                        dbase.add_tables(dic)
                        print('The table was successfully added',
                              style=style_ok)
                    except Exception as e:
                        print('Could not add table!', style=style_warning)
                        print(e, style=style_warning)

                elif code == 'query':
                    q = prompt('Enter your SQL query: ')
                    try:
                        current_query = dbase.query(q, to_memory=False)
                    except Exception as e:
                        current_query = None
                        print(e, style=style_warning)

                    if current_query != None:
                        txt = 'Enter a filename if you want to save query (.csv, .csv.gz or .parquet), leave empty to pass: '******'':
                            if '.csv' in f:
                                if '.gz' in f:
                                    current_query.toPandas().to_csv(
                                        f, compression='gzip', index=False)
                                else:
                                    current_query.toPandas().to_csv(
                                        f, index=False)
                            elif 'parquet' in f:
                                current_query.toPandas().to_parquet(
                                    compression='GZIP')

                        txt = 'Enter name if you want to add query as a table to the dataset, leave empty to pass: '******'':
                            dbase.tables[a] = DataFrameWithInfo(
                                a, current_query)

            elif current_menu == 'qpe':
                if code == 'compute':
                    ########
                    # Compute new qpe
                    ########
                    txt = 'Indicate start time of the data you want to add (format YYYYMMDDHHMM, HHMM is optional) '
                    t0 = prompt_check(txt, ['%Y%m%d', '%Y%m%d%H%M'])

                    txt = 'Indicate end time of the data you want to add (format YYYYMMDDHHMM, HHMM is optional) '
                    t1 = prompt_check(txt, ['%Y%m%d', '%Y%m%d%H%M'])

                    script_path = os.path.dirname(os.path.realpath(__file__))
                    default_config_path = Path(script_path, 'qpe',
                                               'default_config.yml')

                    c = prompt_check(
                        'Enter location of the configuration file (in yml format): ',
                        check='yaml',
                        default=str(default_config_path))

                    success = False
                    while not success:
                        o = prompt(
                            'Enter the location where the generated files will be stored: '
                        )
                        try:
                            if not os.path.exists(o):
                                os.makedirs(o)
                            assert (os.path.exists(o))
                            success = True
                        except:
                            pass

                    folder_models = Path(script_path, 'qpe', 'rf_models')
                    txt = 'Enter the name of the RF models to use in the form of a json line of the following format '+ \
                                   '{"model1_name":"model1_filename",model2_name":"model2_filename,...,modeln_name":"modeln_filename}' + \
                                   ', all model filenames must be stored in the folder ' + str(folder_models) + ' : '
                    m = prompt_check(txt, 'json')

                    print(
                        'You want to compute a QPE with the following parameters...'
                    )
                    print('Starting time: ' + str(t0))
                    print('End time: ' + str(t1))
                    print('Output folder: ' + o)
                    print('Config file: ' + c)
                    print('Model(s): ' + str(m))

                    ok = prompt('Do you want to start y/n: ')
                    if ok == 'y':
                        print('Creating slurm job')
                        fname = 'qpe.job'
                        file = open(fname, 'w')
                        file.write(constants.SLURM_HEADER_PY)
                        file.write(
                            "qpe_compute -s {:s} -e {:s} -o {:s} -c {:s} -m '{:s}'"
                            .format(t0, t1, o, c, m))
                        file.close()
                        print('Submitting job')
                        subprocess.call('sbatch {:s}'.format(fname),
                                        shell=True)

        except KeyboardInterrupt:
            current_menu = 'main'
            print(title1, style=style)
            pass
Beispiel #23
0
root = VSplit([
    Box(Frame(buttons, style="class:button_panel"), padding=2),
    Box(Frame(text_area, title="Events", style="class:edit_panel"), padding=2)
])

layout = Layout(root)

# napojení na klávesové zkratky
key_bindings = KeyBindings()
key_bindings.add('up')(focus_previous)
key_bindings.add('down')(focus_next)

style = Style([
    ('button_panel', 'bg:#ansiblue #ansiwhite'),
    ('edit_panel', 'bg:#ansigreen #000000'),
    ('button', '#ansibrightyellow'),
    ('button focused', 'bg:#ff0000'),
    ('text-area focused', 'bg:#ff0000'),
])


@key_bindings.add('escape')
def on_escape_press(event):
    """Callback funkce volaná při stisku klávesy Esc."""
    print("\n\n[escape]\n\n")
    event.app.exit()


def main():
    # vytvoření aplikace s textovým uživatelským rozhraním
    application = Application(layout=layout,
Beispiel #24
0
def handle_fcomm_command(command, user, randomuri, implant_id):

    # convert randomuri to parent randomuri
    oldrandomuri = randomuri
    p = get_implantdetails(randomuri)
    newimplant_id = re.search(r'(?<=\s)\S*', p.Label).group()
    if newimplant_id is not None:
        randomuri = get_randomuri(newimplant_id)

    # alias mapping
    for alias in cs_alias:
        if alias[0] == command[:len(command.rstrip())]:
            command = alias[1]

    # alias replace
    for alias in cs_replace:
        if command.startswith(alias[0]):
            command = command.replace(alias[0], alias[1])

    original_command = command
    command = command.strip()

    run_autoloads_sharp(command,
                        randomuri,
                        user,
                        loadmodule_command="fcomm-loadmodule")

    if command.startswith("searchhistory"):
        searchterm = (command).replace("searchhistory ", "")
        with open('%s/.implant-history' % PoshProjectDirectory) as hisfile:
            for line in hisfile:
                if searchterm in line.lower():
                    print(Colours.PURPLE + line.replace("+", ""))

    elif command.startswith("searchhelp"):
        searchterm = (command).replace("searchhelp ", "")
        helpful = sharp_help.split('\n')
        for line in helpful:
            if searchterm in line.lower():
                print(Colours.PURPLE + line)

    elif command.startswith("searchallhelp"):
        searchterm = (command).replace("searchallhelp ", "")
        for line in allhelp:
            if searchterm in line.lower():
                print(Colours.GREEN + line)

    elif command.startswith("upload-file"):
        source = ""
        destination = ""
        if command == "upload-file":
            style = Style.from_dict({
                '': '#772953',
            })
            session = PromptSession(history=FileHistory('%s/.upload-history' %
                                                        PoshProjectDirectory),
                                    auto_suggest=AutoSuggestFromHistory(),
                                    style=style)
            try:
                source = session.prompt("Location file to upload: ",
                                        completer=FilePathCompleter(
                                            PayloadsDirectory, glob="*"))
                source = PayloadsDirectory + source
            except KeyboardInterrupt:
                return
            while not os.path.isfile(source):
                print("File does not exist: %s" % source)
                source = session.prompt("Location file to upload: ",
                                        completer=FilePathCompleter(
                                            PayloadsDirectory, glob="*"))
                source = PayloadsDirectory + source
            destination = session.prompt("Location to upload to: ")
        else:
            args = argp(command)
            source = args.source
            destination = args.destination
        try:
            destination = destination.replace("\\", "\\\\")
            print("")
            print("Uploading %s to %s" % (source, destination))
            uploadcommand = f"upload-file {source} {destination}"
            new_task(f"fcomm-command {uploadcommand}", user, randomuri)
        except Exception as e:
            print_bad("Error with source file: %s" % e)
            traceback.print_exc()

    elif command.startswith("unhide-implant"):
        unhide_implant(oldrandomuri)

    elif command.startswith("hide-implant"):
        hide_implant(oldrandomuri)

    elif command.startswith("inject-shellcode"):
        params = re.compile("inject-shellcode", re.IGNORECASE)
        params = params.sub("", command)
        style = Style.from_dict({
            '': '#772953',
        })
        session = PromptSession(history=FileHistory('%s/.shellcode-history' %
                                                    PoshProjectDirectory),
                                auto_suggest=AutoSuggestFromHistory(),
                                style=style)
        try:
            path = session.prompt("Location of shellcode file: ",
                                  completer=FilePathCompleter(
                                      PayloadsDirectory, glob="*.bin"))
            path = PayloadsDirectory + path
        except KeyboardInterrupt:
            return
        try:
            shellcodefile = load_file(path)
            if shellcodefile is not None:
                new_task(
                    "fcomm-command run-exe Core.Program Core Inject-Shellcode %s%s #%s"
                    % (base64.b64encode(shellcodefile).decode("utf-8"), params,
                       os.path.basename(path)), user, randomuri)
        except Exception as e:
            print("Error loading file: %s" % e)

    elif command.startswith("migrate"):
        params = re.compile("migrate", re.IGNORECASE)
        params = params.sub("", command)
        migrate(randomuri, user, params)

    elif command == "kill-implant" or command == "exit":
        impid = get_implantdetails(randomuri)
        ri = input(
            "Are you sure you want to terminate the implant ID %s? (Y/n) " %
            impid.ImplantID)
        if ri.lower() == "n":
            print("Implant not terminated")
        if ri == "" or ri.lower() == "y":
            pid = impid.PID
            new_task("fcomm-kill", user, randomuri)
            new_task("kill-process %s" % (pid), user, randomuri)
            kill_implant(oldrandomuri)

    elif command == "sharpsocks":
        from random import choice
        allchar = string.ascii_letters
        channel = "".join(choice(allchar) for x in range(25))
        sharpkey = gen_key().decode("utf-8")
        sharpurls = get_sharpurls()
        sharpurls = sharpurls.split(",")
        sharpurl = select_item("HostnameIP", "C2Server")
        print(
            PoshInstallDirectory +
            "SharpSocks/SharpSocksServerCore -c=%s -k=%s --verbose -l=%s\r\n" %
            (channel, sharpkey, SocksHost) + Colours.PURPLE)
        ri = input(
            "Are you ready to start the SharpSocks in the implant? (Y/n) ")
        if ri.lower() == "n":
            print("")
        if ri == "":
            new_task(
                "fcomm-command run-exe SharpSocksImplantTestApp.Program SharpSocks -s %s -c %s -k %s -url1 %s -url2 %s -b 2000 --session-cookie ASP.NET_SessionId --payload-cookie __RequestVerificationToken"
                % (sharpurl, channel, sharpkey, sharpurls[0].replace(
                    "\"", ""), sharpurls[1].replace("\"", "")), user,
                randomuri)
        if ri.lower() == "y":
            new_task(
                "fcomm-command run-exe SharpSocksImplantTestApp.Program SharpSocks -s %s -c %s -k %s -url1 %s -url2 %s -b 2000 --session-cookie ASP.NET_SessionId --payload-cookie __RequestVerificationToken"
                % (sharpurl, channel, sharpkey, sharpurls[0].replace(
                    "\"", ""), sharpurls[1].replace("\"", "")), user,
                randomuri)

    elif (command.startswith("stop-keystrokes")):
        new_task(
            "fcomm-command run-exe Logger.KeyStrokesClass Logger %s" % command,
            user, randomuri)
        update_label("", randomuri)

    elif (command.startswith("start-keystrokes")):
        check_module_loaded("Logger.exe", randomuri, user)
        new_task(
            "fcomm-command run-exe Logger.KeyStrokesClass Logger %s" % command,
            user, randomuri)
        update_label("KEYLOG", randomuri)

    elif (command.startswith("get-keystrokes")):
        new_task(
            "fcomm-command run-exe Logger.KeyStrokesClass Logger %s" % command,
            user, randomuri)

    elif (command.startswith("get-screenshotmulti")):
        pwrStatus = get_powerstatusbyrandomuri(randomuri)
        if (pwrStatus is not None and pwrStatus[7]):
            ri = input(
                "[!] Screen is reported as LOCKED, do you still want to attempt a screenshot? (y/N) "
            )
            if ri.lower() == "n" or ri.lower() == "":
                return
        new_task(f"fcomm-command {command}", user, randomuri)
        update_label("SCREENSHOT", randomuri)

    elif (command.startswith("get-screenshot")):
        pwrStatus = get_powerstatusbyrandomuri(randomuri)
        if (pwrStatus is not None and pwrStatus[7]):
            ri = input(
                "[!] Screen is reported as LOCKED, do you still want to attempt a screenshot? (y/N) "
            )
            if ri.lower() == "n" or ri.lower() == "":
                return
        new_task(f"fcomm-command {command}", user, randomuri)

    elif (command == "get-powerstatus"):
        getpowerstatus(randomuri)
        new_task(
            "fcomm-command run-dll PwrStatusTracker.PwrFrm PwrStatusTracker GetPowerStatusResult ",
            user, randomuri)

    elif (command == "getpowerstatus"):
        getpowerstatus(randomuri)
        new_task(
            "fcomm-command run-dll PwrStatusTracker.PwrFrm PwrStatusTracker GetPowerStatusResult ",
            user, randomuri)

    elif (command.startswith("stop-powerstatus")):
        new_task(f"fcomm-command {command}", user, randomuri)
        update_label("", randomuri)

    elif (command.startswith("stoppowerstatus")):
        new_task(f"fcomm-command {command}", user, randomuri)
        update_label("", randomuri)

    elif (command.startswith("pslo")):
        new_task(f"fcomm-{command}", user, randomuri)

    elif (command.startswith("run-exe SharpWMI.Program")
          ) and "execute" in command and "payload" not in command:
        style = Style.from_dict({'': '#772953'})
        session = PromptSession(history=FileHistory('%s/.shellcode-history' %
                                                    PoshProjectDirectory),
                                auto_suggest=AutoSuggestFromHistory(),
                                style=style)
        try:
            path = session.prompt("Location of base64 vbs/js file: ",
                                  completer=FilePathCompleter(
                                      PayloadsDirectory, glob="*.b64"))
            path = PayloadsDirectory + path
        except KeyboardInterrupt:
            return
        if os.path.isfile(path):
            with open(path, "r") as p:
                payload = p.read()
            new_task("fcomm-command %s payload=%s" % (command, payload), user,
                     randomuri)
        else:
            print_bad("Could not find file")

    elif (command.startswith("get-hash")):
        check_module_loaded("InternalMonologue.exe", randomuri, user)
        new_task(
            "fcomm-command run-exe InternalMonologue.Program InternalMonologue",
            user, randomuri)

    elif (command.startswith("safetykatz")):
        new_task("fcomm-command run-exe SafetyKatz.Program %s" % command, user,
                 randomuri)

    elif command.startswith("loadmoduleforce"):
        params = re.compile("loadmoduleforce ", re.IGNORECASE)
        params = params.sub("", command)
        new_task("fcomm-loadmodule %s" % params, user, randomuri)

    elif command.startswith("loadmodule"):
        params = re.compile("loadmodule ", re.IGNORECASE)
        params = params.sub("", command)
        new_task("fcomm-loadmodule %s" % params, user, randomuri)

    elif command.startswith("listmodules"):
        modules = os.listdir("%s/Modules/" % PoshInstallDirectory)
        modules = sorted(modules, key=lambda s: s.lower())
        print("")
        print("[+] Available modules:")
        print("")
        for mod in modules:
            if (".exe" in mod) or (".dll" in mod):
                print(mod)

    elif command.startswith("modulesloaded"):
        ml = get_implantdetails(randomuri)
        print(ml.ModsLoaded)
        new_task("fcomm-command listmodules", user, randomuri)

    elif command == "help" or command == "?":
        print(sharp_help)

    elif command.startswith("beacon") or command.startswith(
            "set-beacon") or command.startswith("setbeacon"):
        new_sleep = command.replace('set-beacon ', '')
        new_sleep = new_sleep.replace('setbeacon ', '')
        new_sleep = new_sleep.replace('beacon ', '').strip()
        if not validate_sleep_time(new_sleep):
            print(Colours.RED)
            print(
                "Invalid sleep command, please specify a time such as 50s, 10m or 1h"
            )
            print(Colours.PURPLE)
        else:
            new_task(f"fcomm-command {command}", user, randomuri)

    else:
        if command:
            new_task(f"fcomm-command {original_command}", user, randomuri)
        return
Beispiel #25
0
    def singleline(self,
                   auto_suggest=None,
                   enable_history_search=True,
                   multiline=True,
                   **kwargs):
        """Reads a single line of input from the shell. The store_in_history
        kwarg flags whether the input should be stored in PTK's in-memory
        history.
        """
        events.on_pre_prompt.fire()
        env = builtins.__xonsh__.env
        mouse_support = env.get("MOUSE_SUPPORT")
        auto_suggest = auto_suggest if env.get("AUTO_SUGGEST") else None
        refresh_interval = env.get("PROMPT_REFRESH_INTERVAL")
        refresh_interval = refresh_interval if refresh_interval > 0 else None
        complete_in_thread = env.get("COMPLETION_IN_THREAD")
        completions_display = env.get("COMPLETIONS_DISPLAY")
        complete_style = self.completion_displays_to_styles[
            completions_display]

        complete_while_typing = env.get("UPDATE_COMPLETIONS_ON_KEYPRESS")
        if complete_while_typing:
            # PTK requires history search to be none when completing while typing
            enable_history_search = False
        if HAS_PYGMENTS:
            self.styler.style_name = env.get("XONSH_COLOR_STYLE")
        completer = None if completions_display == "none" else self.pt_completer

        get_bottom_toolbar_tokens = self.bottom_toolbar_tokens

        if env.get("UPDATE_PROMPT_ON_KEYPRESS"):
            get_prompt_tokens = self.prompt_tokens
            get_rprompt_tokens = self.rprompt_tokens
        else:
            get_prompt_tokens = self.prompt_tokens()
            get_rprompt_tokens = self.rprompt_tokens()
            if get_bottom_toolbar_tokens:
                get_bottom_toolbar_tokens = get_bottom_toolbar_tokens()

        if env.get("VI_MODE"):
            editing_mode = EditingMode.VI
        else:
            editing_mode = EditingMode.EMACS

        if env.get("XONSH_HISTORY_MATCH_ANYWHERE"):
            self.prompter.default_buffer._history_matches = MethodType(
                _cust_history_matches, self.prompter.default_buffer)
        elif (self.prompter.default_buffer._history_matches
              is not self._history_matches_orig):
            self.prompter.default_buffer._history_matches = self._history_matches_orig

        prompt_args = {
            "mouse_support": mouse_support,
            "auto_suggest": auto_suggest,
            "message": get_prompt_tokens,
            "rprompt": get_rprompt_tokens,
            "bottom_toolbar": get_bottom_toolbar_tokens,
            "completer": completer,
            "multiline": multiline,
            "editing_mode": editing_mode,
            "prompt_continuation": self.continuation_tokens,
            "enable_history_search": enable_history_search,
            "reserve_space_for_menu": 0,
            "key_bindings": self.key_bindings,
            "complete_style": complete_style,
            "complete_while_typing": complete_while_typing,
            "include_default_pygments_style": False,
            "refresh_interval": refresh_interval,
            "complete_in_thread": complete_in_thread,
        }
        if env.get("COLOR_INPUT"):
            if HAS_PYGMENTS:
                prompt_args["lexer"] = PygmentsLexer(pyghooks.XonshLexer)
                style = style_from_pygments_cls(
                    pyghooks.xonsh_style_proxy(self.styler))
            else:
                style = style_from_pygments_dict(DEFAULT_STYLE_DICT)

            prompt_args["style"] = style

            style_overrides_env = env.get("PTK_STYLE_OVERRIDES")
            if style_overrides_env:
                try:
                    style_overrides = Style.from_dict(style_overrides_env)
                    prompt_args["style"] = merge_styles(
                        [style, style_overrides])
                except (AttributeError, TypeError, ValueError):
                    print_exception()

        if env["ENABLE_ASYNC_PROMPT"]:
            # once the prompt is done, update it in background as each future is completed
            prompt_args["pre_run"] = self.prompt_formatter.start_update

        line = self.prompter.prompt(**prompt_args)

        events.on_post_prompt.fire()
        return line
        MenuItem('Status Bar', handler=do_status_bar),
    ]),
    MenuItem('Info', children=[
        MenuItem('About', handler=do_about),
    ]),
], floats=[
    Float(xcursor=True,
          ycursor=True,
          content=CompletionsMenu(
              max_height=16,
              scroll_offset=1)),
], key_bindings=bindings)


style = Style.from_dict({
    'status': 'reverse',
    'shadow': 'bg:#440044',
})


layout = Layout(
    root_container,
    focused_element=text_field)


application = Application(
    layout=layout,
    enable_page_navigation_bindings=True,
    style=style,
    mouse_support=True,
    full_screen=True)
Beispiel #27
0
def implant_command_loop(implant_id, user):
    while(True):
        try:
            style = Style.from_dict({
                '': '#80d130',
            })
            session = PromptSession(history=FileHistory('%s/.implant-history' % PoshProjectDirectory), auto_suggest=AutoSuggestFromHistory(), style=style)
            implant_id_orig = implant_id
            if ("-" in implant_id) or ("all" in implant_id) or ("," in implant_id):
                print(Colours.GREEN)
                prompt_commands = POSH_COMMANDS
                command = session.prompt("%s> " % implant_id, completer=FirstWordFuzzyWordCompleter(prompt_commands, WORD=True))
                if command == "back" or command == 'clear':
                    do_back(user, command)
                    return
            else:
                implant = get_implantbyid(implant_id)
                if not implant:
                    print_bad("Unrecognised implant id or command: %s" % implant_id)
                    input("Press Enter to continue...")
                    clear()
                    return
                prompt_commands = POSH_COMMANDS
                if implant.Pivot.startswith('Python'):
                    prompt_commands = PY_COMMANDS
                if implant.Pivot.startswith('C#'):
                    prompt_commands = SHARP_COMMANDS
                if 'PB' in implant.Pivot:
                    style = Style.from_dict({
                        '': '#008ECC',
                    })
                    session = PromptSession(history=FileHistory('%s/.implant-history' % PoshProjectDirectory), auto_suggest=AutoSuggestFromHistory(), style=style)
                    prompt_commands = SHARP_COMMANDS
                    print(Colours.BLUE)
                else:
                    print(Colours.GREEN)
                print("%s\\%s @ %s (PID:%s)" % (implant.Domain, implant.User, implant.Hostname, implant.PID))
                command = session.prompt("%s %s> " % (get_implant_type_prompt_prefix(implant_id), implant_id), completer=FirstWordFuzzyWordCompleter(prompt_commands, WORD=True))
                if command == "back" or command == 'clear':
                    do_back(user, command)
                    return

            # if "all" run through all implants get_implants()
            if implant_id == "all":
                if command == "back" or command == 'clear':
                    do_back(user, command)
                    return
                allcommands = command
                if "\n" in command:
                    ri = input("Do you want to run commands separately? (Y/n) ")
                implants_split = get_implants()
                if implants_split:
                    for implant_details in implants_split:
                        # if "\n" in command run each command individually or ask the question if that's what they want to do
                        if "\n" in allcommands:
                            if ri.lower() == "y" or ri == "":
                                commands = allcommands.split('\n')
                                for command in commands:
                                    run_implant_command(command, implant_details.RandomURI, implant_id_orig, user)
                            else:
                                run_implant_command(command, implant_details.RandomURI, implant_id_orig, user)
                        else:
                            run_implant_command(command, implant_details.RandomURI, implant_id_orig, user)

            # if "separated list" against single uri
            elif "," in implant_id:
                allcommands = command
                if "\n" in command:
                    ri = input("Do you want to run commands separately? (Y/n) ")
                implant_split = implant_id.split(",")
                for split_implant_id in implant_split:
                    implant_randomuri = get_randomuri(split_implant_id)
                    # if "\n" in command run each command individually or ask the question if that's what they want to do
                    if "\n" in allcommands:
                        if ri.lower() == "y" or ri == "":
                            commands = allcommands.split('\n')
                            for command in commands:
                                run_implant_command(command, implant_randomuri, implant_id_orig, user)
                        else:
                            run_implant_command(command, implant_randomuri, implant_id_orig, user)
                    else:
                        run_implant_command(command, implant_randomuri, implant_id_orig, user)

            # if "range" against single uri
            elif "-" in implant_id:
                allcommands = command
                if "\n" in command:
                    ri = input("Do you want to run commands separately? (Y/n) ")
                implant_split = implant_id.split("-")
                for range_implant_id in range(int(implant_split[0]), int(implant_split[1]) + 1):
                    try:
                        implant_randomuri = get_randomuri(range_implant_id)
                        # if "\n" in command run each command individually or ask the question if that's what they want to do
                        if "\n" in allcommands:
                            if ri.lower() == "y" or ri == "":
                                commands = allcommands.split('\n')
                                for command in commands:
                                    run_implant_command(command, implant_randomuri, implant_id_orig, user)
                            else:
                                run_implant_command(command, implant_randomuri, implant_id_orig, user)
                        else:
                            run_implant_command(command, implant_randomuri, implant_id_orig, user)
                    except Exception:
                        print_bad("Unknown ImplantID")

            # else run against single uri
            else:
                allcommands = command
                if "\n" in command:
                    ri = input("Do you want to run commands separately? (Y/n) ")
                implant_randomuri = get_randomuri(implant_id)
                # if "\n" in command run each command individually or ask the question if that's what they want to do
                if "\n" in allcommands:
                    if ri.lower() == "y" or ri == "":
                        commands = allcommands.split('\n')
                        for command in commands:
                            run_implant_command(command, implant_randomuri, implant_id_orig, user)
                    else:
                        run_implant_command(command, implant_randomuri, implant_id_orig, user)
                else:
                    run_implant_command(command, implant_randomuri, implant_id_orig, user)

        except KeyboardInterrupt:
            continue
        except EOFError:
            new_c2_message("%s logged off." % user)
            sys.exit(0)
        except Exception as e:
            traceback.print_exc()
            print_bad(f"Error running against the selected implant ID, ensure you have typed the correct information: {e}")
            return
Beispiel #28
0
from prompt_toolkit.shortcuts import input_dialog, message_dialog
from prompt_toolkit.styles import Style
from os.path import join, realpath, dirname
import sys
sys.path.insert(0, realpath(join(__file__, "../../../")))

with open(realpath(join(dirname(__file__), '../../VERSION'))) as version_file:
    version = version_file.read().strip()

dialog_style = Style.from_dict({
    'dialog': 'bg:#171E2B',
    'dialog frame.label': 'bg:#ffffff #000000',
    'dialog.body': 'bg:#000000 #1CD085',
    'dialog shadow': 'bg:#171E2B',
    'button': 'bg:#000000',
    'text-area': 'bg:#000000 #ffffff',
})


def show_welcome():
    message_dialog(title='Welcome to Hummingbot',
                   text="""

    ██╗  ██╗██╗   ██╗███╗   ███╗███╗   ███╗██╗███╗   ██╗ ██████╗ ██████╗  ██████╗ ████████╗
    ██║  ██║██║   ██║████╗ ████║████╗ ████║██║████╗  ██║██╔════╝ ██╔══██╗██╔═══██╗╚══██╔══╝
    ███████║██║   ██║██╔████╔██║██╔████╔██║██║██╔██╗ ██║██║  ███╗██████╔╝██║   ██║   ██║
    ██╔══██║██║   ██║██║╚██╔╝██║██║╚██╔╝██║██║██║╚██╗██║██║   ██║██╔══██╗██║   ██║   ██║
    ██║  ██║╚██████╔╝██║ ╚═╝ ██║██║ ╚═╝ ██║██║██║ ╚████║╚██████╔╝██████╔╝╚██████╔╝   ██║
    ╚═╝  ╚═╝ ╚═════╝ ╚═╝     ╚═╝╚═╝     ╚═╝╚═╝╚═╝  ╚═══╝ ╚═════╝ ╚═════╝  ╚═════╝    ╚═╝

    =======================================================================================
#!/usr/bin/env python
"""
Styled just like an apt-get installation.
"""
from __future__ import unicode_literals
from prompt_toolkit.shortcuts import ProgressBar
from prompt_toolkit.styles import Style
from prompt_toolkit.shortcuts.progress_bar import formatters
import time

style = Style.from_dict({
    'label': 'bg:#ffff00 #000000',
    'percentage': 'bg:#ffff00 #000000',
    'current': '#448844',
    'bar': '',
})


def main():
    custom_formatters = [
        formatters.Label(),
        formatters.Text(': [', style='class:percentage'),
        formatters.Percentage(),
        formatters.Text(']', style='class:percentage'),
        formatters.Text(' '),
        formatters.Bar(sym_a='#', sym_b='#', sym_c='.'),
        formatters.Text('  '),
    ]

    with ProgressBar(style=style, formatters=custom_formatters) as pb:
        for i in pb(range(1600), label='Installing'):
Beispiel #30
0
NO_OR_YES = "(y/N)"

# Instruction for multiline input
INSTRUCTION_MULTILINE = "(Finish with 'Alt+Enter' or 'Esc, Enter')"

# Selection token used to indicate the selection cursor in a list
SELECTED_POINTER = "»"

# Item prefix to identify selected items in a checkbox list
INDICATOR_SELECTED = "●"

# Item prefix to identify unselected items in a checkbox list
INDICATOR_UNSELECTED = "○"

# Prefix displayed in front of questions
DEFAULT_QUESTION_PREFIX = "?"

# Message shown when a user aborts a question prompt using CTRL-C
DEFAULT_KBI_MESSAGE = "Cancelled by user"

# Default message style
DEFAULT_STYLE = Style([
    ("qmark", "fg:#5f819d"),  # token in front of the question
    ("question", "bold"),  # question text
    ("answer", "fg:#FF9D00 bold"),  # submitted answer text behind the question
    ("pointer", ""),  # pointer used in select and checkbox prompts
    ("selected", ""),  # style for a selected item of a checkbox
    ("separator", ""),  # separator in lists
    ("instruction", ""),  # user instructions for select, rawselect, checkbox
])
Beispiel #31
0
#!/usr/bin/env python
"""
A very simple progress bar which keep track of the progress as we consume an
iterator.
"""
from __future__ import unicode_literals
from prompt_toolkit.shortcuts import ProgressBar
from prompt_toolkit.styles import Style
import time

style = Style.from_dict({
    'title': '#4444ff underline',
    'label': '#ff4400 bold',
    'percentage': '#00ff00',
    'bar-a': 'bg:#00ff00 #004400',
    'bar-b': 'bg:#00ff00 #000000',
    'bar-c': '#000000 underline',
    'current': '#448844',
    'total': '#448844',
    'time-elapsed': '#444488',
    'time-left': 'bg:#88ff88 #000000',
})


def main():
    with ProgressBar(style=style,
                     title='Progress bar example with custom styling.') as pb:
        for i in pb(range(1600), label='Downloading...'):
            time.sleep(.01)


if __name__ == '__main__':
Beispiel #32
0
from prompt_toolkit.styles import Style

custom_style_fancy = Style([
    ("separator", "fg:#cc5454"),
    ("qmark", "fg:#673ab7 bold"),
    ("question", ""),
    ("selected", "fg:#cc5454"),
    ("pointer", "fg:#673ab7 bold"),
    ("highlighted", "fg:#673ab7 bold"),
    ("answer", "fg:#f44336 bold"),
    ("text", "fg:#FBE9E7"),
    ("disabled", "fg:#858585 italic"),
])

custom_style_dope = Style([
    ("separator", "fg:#6C6C6C"),
    ("qmark", "fg:#FF9D00 bold"),
    ("question", ""),
    ("selected", "fg:#5F819D"),
    ("pointer", "fg:#FF9D00 bold"),
    ("answer", "fg:#5F819D bold"),
])

custom_style_genius = Style([
    ("qmark", "fg:#E91E63 bold"),
    ("question", ""),
    ("selected", "fg:#673AB7 bold"),
    ("answer", "fg:#2196f3 bold"),
])
              content=CompletionsMenu(max_height=16, scroll_offset=1)),
    ])

# Global key bindings.
bindings = KeyBindings()
bindings.add('tab')(focus_next)
bindings.add('s-tab')(focus_previous)

style = Style.from_dict({
    'window.border': '#888888',
    'shadow': 'bg:#222222',
    'menu-bar': 'bg:#aaaaaa #888888',
    'menu-bar.selected-item': 'bg:#ffffff #000000',
    'menu': 'bg:#888888 #ffffff',
    'menu.border': '#aaaaaa',
    'window.border shadow': '#444444',
    'focused  button': 'bg:#880000 #ffffff noinherit',

    # Styling for Dialog widgets.
    'radiolist focused': 'noreverse',
    'radiolist focused radio.selected': 'reverse',
    'button-bar': 'bg:#aaaaff'
})

application = Application(layout=Layout(
    root_container,
    focused_element=yes_button,
),
                          key_bindings=bindings,
                          style=style,
                          mouse_support=True,
"""
Example of a style dialog window.
All dialog shortcuts take a `style` argument in order to apply a custom
styling.

This also demonstrates that the `title` argument can be any kind of formatted
text.
"""
from prompt_toolkit.formatted_text import HTML
from prompt_toolkit.shortcuts import message_dialog
from prompt_toolkit.styles import Style

# Custom color scheme.
example_style = Style.from_dict({
    'dialog':             'bg:#88ff88',
    'dialog frame-label': 'bg:#ffffff #000000',
    'dialog.body':        'bg:#000000 #00ff00',
    'dialog shadow':      'bg:#00aa00',
})


def main():
    message_dialog(
        title=HTML('<style bg="blue" fg="white">Styled</style> '
                   '<style fg="ansired">dialog</style> window'),
        text='Do you want to continue?\nPress ENTER to quit.',
        style=example_style).run()


if __name__ == '__main__':
    main()
Beispiel #35
0
])

# Key bindings.
bindings = KeyBindings()


@bindings.add('c-c')
@bindings.add('q')
def _(event):
    " Quit. "
    event.app.exit()


style = Style.from_dict({
    'status': 'reverse',
    'status.position': '#aaaa00',
    'status.key': '#ffaa00',
    'not-searching': '#888888',
})

# create application.
application = Application(layout=Layout(
    root_container,
    focused_element=text_area,
),
                          key_bindings=bindings,
                          enable_page_navigation_bindings=True,
                          mouse_support=True,
                          style=style,
                          full_screen=True)

Beispiel #36
0
    def singleline(
        self, auto_suggest=None, enable_history_search=True, multiline=True, **kwargs
    ):
        """Reads a single line of input from the shell. The store_in_history
        kwarg flags whether the input should be stored in PTK's in-memory
        history.
        """
        events.on_pre_prompt.fire()
        env = builtins.__xonsh__.env
        mouse_support = env.get("MOUSE_SUPPORT")
        auto_suggest = auto_suggest if env.get("AUTO_SUGGEST") else None
        completions_display = env.get("COMPLETIONS_DISPLAY")
        complete_style = self.completion_displays_to_styles[completions_display]

        complete_while_typing = env.get("UPDATE_COMPLETIONS_ON_KEYPRESS")
        if complete_while_typing:
            # PTK requires history search to be none when completing while typing
            enable_history_search = False
        if HAS_PYGMENTS:
            self.styler.style_name = env.get("XONSH_COLOR_STYLE")
        completer = None if completions_display == "none" else self.pt_completer

        if env.get("UPDATE_PROMPT_ON_KEYPRESS"):
            get_prompt_tokens = self.prompt_tokens
            get_rprompt_tokens = self.rprompt_tokens
            get_bottom_toolbar_tokens = self.bottom_toolbar_tokens
        else:
            get_prompt_tokens = self.prompt_tokens()
            get_rprompt_tokens = self.rprompt_tokens()
            get_bottom_toolbar_tokens = self.bottom_toolbar_tokens()

        if env.get("VI_MODE"):
            editing_mode = EditingMode.VI
        else:
            editing_mode = EditingMode.EMACS

        if env.get("XONSH_HISTORY_MATCH_ANYWHERE"):
            self.prompter.default_buffer._history_matches = MethodType(
                _cust_history_matches, self.prompter.default_buffer
            )
        elif (
            self.prompter.default_buffer._history_matches
            is not self._history_matches_orig
        ):
            self.prompter.default_buffer._history_matches = self._history_matches_orig

        prompt_args = {
            "mouse_support": mouse_support,
            "auto_suggest": auto_suggest,
            "message": get_prompt_tokens,
            "rprompt": get_rprompt_tokens,
            "bottom_toolbar": get_bottom_toolbar_tokens,
            "completer": completer,
            "multiline": multiline,
            "editing_mode": editing_mode,
            "prompt_continuation": self.continuation_tokens,
            "enable_history_search": enable_history_search,
            "reserve_space_for_menu": 0,
            "key_bindings": self.key_bindings,
            "complete_style": complete_style,
            "complete_while_typing": complete_while_typing,
            "include_default_pygments_style": False,
        }
        if builtins.__xonsh__.env.get("COLOR_INPUT"):
            if HAS_PYGMENTS:
                prompt_args["lexer"] = PygmentsLexer(pyghooks.XonshLexer)
                style = style_from_pygments_cls(pyghooks.xonsh_style_proxy(self.styler))
            else:
                style = style_from_pygments_dict(DEFAULT_STYLE_DICT)

            prompt_args["style"] = style

            style_overrides_env = env.get("PTK_STYLE_OVERRIDES")
            if style_overrides_env:
                try:
                    style_overrides = Style.from_dict(style_overrides_env)
                    prompt_args["style"] = merge_styles([style, style_overrides])
                except (AttributeError, TypeError, ValueError):
                    print_exception()

        line = self.prompter.prompt(**prompt_args)
        events.on_post_prompt.fire()
        return line
Beispiel #37
0
    def __init__(self, url: str = "127.0.0.1", port: int = "9999"):
        self.communicator = Communicator(url, port)

        self.CMDIN = CommandInput(title="DLNest Command Line(F1)",
                                  onAccept=self.onCommandAccept)
        self.w1 = self.CMDIN.getWindow()

        self.DLOutput = ResultsOutput(routineTask=self.routineTaskDLOutput,
                                      title="DLNest Output (F2)",
                                      style="class:dlnest_output")
        self.w2 = self.DLOutput.getWindow()

        self.ANOutput = AnalyzeOutput(routineTask=self.routineTaskANOutput,
                                      title="Analyzer Output (F3)",
                                      style="class:analyzer_output")
        self.w3 = self.ANOutput.getWindow()
        self.analyzeTaskID = ""

        self.TaskInfo = TaskInfoShower(routineTask=self.routineTaskInfo,
                                       title="Tasks (F4)")
        self.w4 = self.TaskInfo.getWindow()

        self.DevicesInfo = DevicesInfoShower(
            routineTask=self.routineTaskDevices, title="Devices (F5)")
        self.w5 = self.DevicesInfo.getWindow()

        self.container_fat = HSplit(
            [self.w1,
             VSplit([self.w2, self.w3]),
             VSplit([self.w4, self.w5])])
        self.container_tall = HSplit(
            [self.w1, self.w2, self.w3, self.w4, self.w5])

        self.kb = KeyBindings()

        @self.kb.add('c-c')
        def exit_(event):
            event.app.exit()

        @self.kb.add('f1')
        def focus1(event):
            event.app.layout.focus(self.w1)

        @self.kb.add('f2')
        def focus2(event):
            event.app.layout.focus(self.w2)

        @self.kb.add('f3')
        def focus3(event):
            event.app.layout.focus(self.w3)

        @self.kb.add('f4')
        def focus4(event):
            event.app.layout.focus(self.w4)

        @self.kb.add('f5')
        def focus5(event):
            event.app.layout.focus(self.w5)

        self.style = Style.from_dict({
            "frame.border": "fg:#ffb6c1",
            "frame.title": "fg:#1ef0ff",
            "command_frame": "bg:#008b8b",
            "dlnest_output": "bg:#451a4a",
            "analyzer_output": "bg:#451a4a",
            "analyzer_info_label": "bg:#da70d6",
            "analyzer_info_text1": "bg:#3f3f00",
            "analyzer_info_text2": "bg:#ff00ff",
            "running_task_status": "bg:#a01010 bold",
            "running_task_id": "bg:#303030",
            "running_task_gpu": "bg:#556b2f",
            "running_task_des": "bg:#c71585",
            "running_task_time": "bg:#2e3b37",
            "pending_task_status": "bg:#1010a0 bold",
            "pending_task_id": "bg:#303030",
            "pending_task_gpu": "bg:#556b2f",
            "pending_task_des": "bg:#c71585",
            "pending_task_time": "bg:#2e3b37",
            "suspend_task_status": "bg:#10a010 bold",
            "suspend_task_id": "bg:#303030",
            "suspend_task_gpu": "bg:#556b2f",
            "suspend_task_des": "bg:#c71585",
            "suspend_task_time": "bg:#2e3b37",
            "task_info_shower": "bg:#008bc0",
            "devices_info_shower": "bg:#008bc0",
            "devices_id": "bg:#303030",
            "devices_status_valid": "bg:#3cb371 bold",
            "devices_status_break": "bg:#a01010 bold",
            "devices_free_memory": "bg:#556b2f",
            "devices_tasks": "bg:#c71585"
        })

        self.layout = Layout(self.container_fat, focused_element=self.w1)
        self.app = Application(key_bindings=self.kb,
                               layout=self.layout,
                               full_screen=True,
                               style=self.style)
        self.app._on_resize = self.on_resize
Beispiel #38
0
    def __init__(self, player, enemy, song='worry 2.wav'):
        self.song = Sound( player,fileName = song, loop=-1)
        self.player = player
        self.playerClans = ' '.join(self.player.clantags)
        if len(self.player.clantags) > 0 : 
            self.playerName = FormattedText([
                ('#ffffff', str(player.aspect['name'])),
                ('', ' '),
                ('#cc00cc', str(self.playerClans)),
            ]) 
        else: 
            self.playerClans =  self.playerName = FormattedText([
                ('#ffffff', str(player.aspect['name'])),
            ]) 
        self.enemy = enemy

        self.playerGoesNext = True # by default, enemy always gets first strike
        self.playerJustDodged = False
        self.escapeTries = 0
        self.escapeChance = .3

        self.battleLog = '\n\n\n\n\n\n'
        self.maxHeightOfBattleLogWindow = 7
        self.totalWidth = 90
        self.actionsWidth = 30
        self.statsWidth = 20 

        self.selectedIndexText = ''
        self.result = None

        self.playerHPBar = ProgressBar()
        self.setHealthProgressBar(self.playerHPBar, self.toPercent(self.player.hp, self.player.maxhp)) 
        self.enemyHPBar = ProgressBar()
        self.setHealthProgressBar(self.enemyHPBar, 100) 

        self.radios = RadioList(
            values=[ #value, lable
                ('Attack', 'Attack'), # use eqipped weapon
                ('Dodge', 'Dodge'), # icrease miss chance for enemy
                ('Item', 'Item'),
                ('Run', 'Run') # try to escape
                # more options could be:
                # check - returns text about enemy potentially indicating weaknessess
            ],
            player = self.player,
            width = self.actionsWidth)
        
        self.bindings = KeyBindings()
        self.bindings.add('right' )(focus_next)
        self.bindings.add('tab' )(focus_next)
        self.bindings.add('s-tab')(focus_previous)
        self.bindings.add('left')(focus_previous)
        self.bindings.add('c-m')(self.handleEnter)
        self.bindings.add('escape')(self.tryToEscape)
        # self.bindings.add('up')(self.setSelectedIndexTextUp)
        # TODO: make secret easter egg key bindings # self.bindings.add('a', 'a')(self.test)  
        self.style = Style.from_dict({
            'dialog.body':        'bg:#000000 #ffcccc', #background color, text color
        })

        self.application = Application(
            layout=Layout(
                self.getRootContainer(),
                focused_element=self.radios,
            ),
            key_bindings=self.bindings,
            style=self.style,
            mouse_support=True,
            full_screen=True,
            )
Beispiel #39
0
from enum import Enum
from prompt_toolkit import HTML
from prompt_toolkit.shortcuts import message_dialog, button_dialog
from prompt_toolkit.styles import Style

dialog_stylesheet_1 = Style.from_dict({
    'dialog':             'bg:yellow',
    'dialog frame-label': 'bg:white black',
    'dialog.body':        'bg:#000000 #00ff00',
    'dialog shadow':      'bg:#00aa00',
    })

Response = Enum('Response', 'abort retry fail')

response = button_dialog(
    title=HTML('Tento program provedl <white>neplatnou</white> operaci'),
    text=HTML('Not <u>ready</u> reading drive <b>A</b>'),
    buttons=[
        ('Abort', Response.abort),
        ('Retry', Response.retry),
        ('Fail',  Response.fail)],
    style=dialog_stylesheet_1)


dialog_stylesheet_2 = Style.from_dict({
    'dialog':             'bg:black',
    'dialog frame-label': 'bg:white black',
    })

message_dialog(
    title='Zadali jste volbu',
Beispiel #40
0
        "1. Send error response 3 for 4 requests\n\t" \
        "   <ansiblue>manipulator response_type=error error_code=3 clear_after=4</ansiblue>\n\t" \
        "2. Delay outgoing response by 5 seconds indefinitely\n\t" \
        "   <ansiblue>manipulator response_type=delayed delay_by=5</ansiblue>\n\t" \
        "3. Send empty response\n\t" \
        "   <ansiblue>manipulator response_type=empty</ansiblue>\n\t" \
        "4. Send stray response of lenght 12 and revert to normal after 2 responses\n\t" \
        "   <ansiblue>manipulator response_type=stray data_len=11 clear_after=2</ansiblue>\n\t" \
        "5. To disable response manipulation\n\t" \
        "   <ansiblue>manipulator response_type=normal</ansiblue>"
COMMAND_HELPS = {
    "manipulator": "Manipulate response from server.\nUsage: {}".format(USAGE),
    "clear": "Clears screen"
}

STYLE = Style.from_dict({"": "cyan"})
CUSTOM_FORMATTERS = [
    formatters.Label(suffix=": "),
    formatters.Bar(start="|", end="|", sym_a="#", sym_b="#", sym_c="-"),
    formatters.Text(" "),
    formatters.Text(" "),
    formatters.TimeElapsed(),
    formatters.Text("  "),
]


def info(message):
    click.secho(str(message), fg="green")


def warning(message):
iterator.
"""
from __future__ import unicode_literals
from prompt_toolkit.formatted_text import HTML
from prompt_toolkit.shortcuts import ProgressBar
from prompt_toolkit.shortcuts.progress_bar import formatters
from prompt_toolkit.styles import Style
import time


style = Style.from_dict({
    'progressbar title': '#0000ff',
    'item-title': '#ff4400 underline',
    'percentage': '#00ff00',
    'bar-a': 'bg:#00ff00 #004400',
    'bar-b': 'bg:#00ff00 #000000',
    'bar-c': 'bg:#000000 #000000',
    'tildes': '#444488',
    'time-left': 'bg:#88ff88 #ffffff',
    'spinning-wheel': 'bg:#ffff00 #000000',
})


def main():
    custom_formatters = [
        formatters.Label(),
        formatters.Text(' '),
        formatters.SpinningWheel(),
        formatters.Text(' '),
        formatters.Text(HTML('<tildes>~~~</tildes>')),
        formatters.Bar(sym_a='#', sym_b='#', sym_c='.'),
#!/usr/bin/env python
"""
Styled similar to tqdm, another progress bar implementation in Python.

See: https://github.com/noamraph/tqdm
"""
import time

from prompt_toolkit.shortcuts import ProgressBar
from prompt_toolkit.shortcuts.progress_bar import formatters
from prompt_toolkit.styles import Style

style = Style.from_dict({
    '': 'cyan',
})


def main():
    custom_formatters = [
        formatters.Label(suffix=': '),
        formatters.Bar(start='|', end='|', sym_a='#', sym_b='#', sym_c='-'),
        formatters.Text(' '),
        formatters.Progress(),
        formatters.Text(' '),
        formatters.Percentage(),
        formatters.Text(' [elapsed: '),
        formatters.TimeElapsed(),
        formatters.Text(' left: '),
        formatters.TimeLeft(),
        formatters.Text(', '),
        formatters.IterationsPerSecond(),
    "scanid=",
    "resolution=",
    "timeout=",
    "limit=",
    "extended_info=",
]

COMPLETER = SUPPORTED_ACTIONS + PARAMETERS

# Style set
style_set = Style([
    # ("title", "bg:#000000 #1b03a3"),
    # ("subtitle", "bg:#000000 #1b03a3"),
    # ("output", "bg:#000044 #ffffff"),
    # ("input", "bg:#000000 #ffffff"),
    # ("line", "bg:#000000 #1b03a3"),
    # ("conf_title", "bg:#000000 #1b03a3"),
    # ("conf_prompt", "bg:#000000 #1b03a3"),
    # ("conf_input", "bg:#1b03a3 #000000"),
    ("conf_notice", "blink"),
    ("conf_input", "reverse"),
])

# The auto-completer for commands/actions.
action_set = WordCompleter(COMPLETER, ignore_case=True, WORD=True)

# Prerequisites ########################################################################################################
if os.path.exists(CONF_DIRECTORY) is False:
    os.makedirs(CONF_DIRECTORY)

if os.path.exists(OUT_DIRECTORY) is False:
    os.makedirs(OUT_DIRECTORY)
Beispiel #44
0
async def init():
    global root
    global chat_container
    global app
    global log_buf
    global log_win
    global input_buf
    global input_win
    global convos
    global status_bar
    global status_label
    global client
    global convo_stack
    global websocket
    global app
    global uri
    global websocket
    global ws_handler

    uri = "ws://localhost:15555"
    ws_handler = handler(uri)
    await ws_handler.connect()

    # message area
    log_buf = Buffer(document=Document())
    log_win = Window(BufferControl(log_buf), wrap_lines=True)

    # input area
    input_buf = Buffer(document=Document())
    input_win = Window(BufferControl(input_buf), height=1, wrap_lines=True)

    # status bar
    status_bar = FormattedTextToolbar(
        text=HTML("<b>Chatting with: Loading </b>"),
        style="bg:ansired fg:ansiblack")
    status_label = Label(text="[ 00:29 ] ", width=10)

    # call backs
    input_buf.accept_handler = accept_message
    input_buf.on_text_changed += resize_input
    log_buf.on_text_changed += auto_scroll
    convos = convo_list_widget()

    chat_container = HSplit(
        [log_win, status_bar,
         VSplit([status_label, input_win])])

    root = VSplit([
        convos,
        chat_container,
    ])

    style = Style.from_dict(
        {"select-box cursor-line": "nounderline bg:ansired fg:ansiwhite"})

    app = Application(editing_mode=EditingMode.VI,
                      key_bindings=kb,
                      layout=Layout(chat_container),
                      full_screen=True,
                      style=style)
    app.invalidate()
    app.layout.focus(input_buf)
    ViState._input_mode = InputMode.INSERT
    ViState.input_mode = property(get_input_mode, set_input_mode)

    asyncio.ensure_future(ws_handler.listen())
    asyncio.ensure_future(ws_handler.command(('get_convo', 'all')))

    auto_scroll(log_buf)
    await app.run_async()
Beispiel #45
0
    def __init__(self, vi_mode=False, style=None, search_text=None,
                 titlebar_tokens=None):
        assert isinstance(vi_mode, bool)
        assert style is None or isinstance(style, Style)

        self.sources = []
        self.current_source_index = 0  # Index in `self.sources`.
        self.highlight_search = True
        self.in_colon_mode = False
        self.message = None
        self.displaying_help = False
        self.search_text = search_text
        self.display_titlebar = bool(titlebar_tokens)
        self.titlebar_tokens = titlebar_tokens or []

        self._dummy_source = DummySource()

        # When this is True, always make sure that the cursor goes to the
        # bottom of the visible content. This is similar to 'tail -f'.
        self.forward_forever = False

        # Status information for all sources. Source -> _SourceInfo.
        # (Remember this info as long as the Source object exists.)
        self.source_info = weakref.WeakKeyDictionary()

        # Create prompt_toolkit stuff.

        def open_file(buff):
            # Open file.
            self.open_file(buff.text)

            # Focus main buffer again.
            buff.reset()

        # Buffer for the 'Examine:' input.
        self.examine_buffer = Buffer(
            name='EXAMINE',
            completer=PathCompleter(expanduser=True),
            accept_handler=open_file,
            multiline=False)

        # Search buffer.
        self.search_buffer = Buffer(multiline=False)

        self.layout = PagerLayout(self)

        bindings = create_key_bindings(self)
        self.application = Application(
            input=create_input(sys.stdout),
            layout=Layout(container=self.layout.container),
            enable_page_navigation_bindings=True,
            key_bindings=bindings,
            style=style or Style.from_dict(ui_style),
            mouse_support=True,
            after_render=self._after_render,
            full_screen=True)

        # Hide message when a key is pressed.
        def key_pressed(_):
            self.message = None
        self.application.key_processor.before_key_press += key_pressed

        if vi_mode:
            self.application.editing_mode = EditingMode.VI
Beispiel #46
0
from vbox_operator.menu import help_menu
from vbox_operator.operations import mkdir_vm
from prompt_toolkit import prompt
from vbox_operator.commands_dict import completer_dict
from prompt_toolkit.styles import Style, style
from prompt_toolkit.formatted_text import FormattedText
from prompt_toolkit.completion import NestedCompleter
from prompt_toolkit import PromptSession, print_formatted_text

# from: https://www.colourlovers.com/palette/1718713/Monokai
# and:  https://www.color-hex.com/color-palette/59231
cli_style = Style.from_dict({
    # User's input.
    '': '#66D9EF',  # cyanish
    # Prompt
    'vm': '#F92672',  # pinkish
    'gt': '#A6E22E',  # greenish
    'output1': '#FD971F',  # orangish
    'output2': '#AE81FF'  # purplish
})

pybox_completer = NestedCompleter.from_nested_dict(completer_dict)
# current vm
curr_vm = None
# all vms
vms = 0
# running vms
r_vms = 0
# Create prompt object.
session = PromptSession()
##########################################
Beispiel #47
0
    def __init__(self, history: History = None):
        self._last_exception = None
        self._session = PromptSession(history=history)

        self.prompt = '> '
        self.prompt_style = Style.from_dict({'': 'bold'})
Beispiel #48
0
def main_loop(campaign, player_view):
    # Load the campaign
    campaign_file = f'{base_dir}/campaigns/{campaign}/settings.toml'
    campaign_data = toml.load(open(campaign_file, 'r'))

    # Load the calendar
    calendar_file = default_calendar_file
    if 'calendar_file' in campaign_data:
        calendar_file = f"{base_dir}/{campaign_data['calendar_file']}"
    cal_data = toml.load(open(calendar_file, 'r'))
    calendar = Calendar(cal_data)

    # Load the clock
    clock = Clock(cal_data['hours_in_day'], cal_data['minutes_in_hour'])

    # Load the almanac (sunrise/sunset times, moon phases)
    almanac = Almanac(calendar)

    # Load other things from the campaign settings data...
    encounters_dir = default_encounters_dir
    if 'encounters' in campaign_data:
        encounters_dir = f"{base_dir}/{campaign_data['encounters']}"

    party_file = default_party_file
    if 'party_file' in campaign_data:
        party_file = f"{base_dir}/{campaign_data['party_file']}"

    log_file = None
    if 'log_file' in campaign_data:
        log_file = f"{base_dir}/{campaign_data['log_file']}"

    game = Game(base_dir=base_dir,
                encounters_dir=encounters_dir,
                party_file=party_file,
                log_file=log_file,
                calendar=calendar,
                clock=clock,
                almanac=almanac,
                latitude=default_latitude)

    session = PromptSession()

    player_view_manager = PlayerViewManager(base_dir, game)

    load_commands(game, session, player_view_manager)

    def bottom_toolbar():
        date = game.calendar.date
        latitude = game.latitude

        dawn, _ = game.almanac.dawn(date, latitude)
        sunrise, _ = game.almanac.sunrise(date, latitude)
        sunset, _ = game.almanac.sunset(date, latitude)
        dusk, _ = game.almanac.dusk(date, latitude)
        day_night = "✨"
        if dawn <= (game.clock.hour, game.clock.minute) < sunrise:
            day_night = "🌅"
        elif sunrise <= (game.clock.hour, game.clock.minute) < sunset:
            day_night = "☀️"
        elif sunset <= (game.clock.hour, game.clock.minute) < dusk:
            day_night = "🌅"

        moon_icons = []
        for moon_key in game.calendar.cal_data['moons']:
            phase, _ = game.almanac.moon_phase(moon_key, date)
            icons = {
                "full": "🌕",
                "waning gibbous": "🌖",
                "third quarter": "🌗",
                "waning crescent": "🌘",
                "new": "🌑",
                "waxing crescent": "🌒",
                "first quarter": "🌓",
                "waxing gibbous": "🌔",
            }
            moon_icons.append(icons[phase])

        n_s = "N" if game.latitude >= 0 else "S"
        pos = f"🌎 {abs(game.latitude)}°{n_s}"
        return [("class:bottom-toolbar",
                 " dndme 0.0.5 - help for help, exit to exit"
                 f" - 📆 {game.calendar}"
                 f" ⏰ {game.clock} {pos} {day_night} "
                 f"{''.join(moon_icons)}")]

    style = Style.from_dict({
        'bottom-toolbar': '#333333 bg:#ffcc00',
    })

    kb = KeyBindings()

    if player_view:
        print("Starting player view on port 5000...")
        player_view_manager.start()
        player_view_manager.update()
        print("Started! Browse to http://localhost:5000/player-view")

    # Attempte to init date, time, and latitude state from log file
    if log_file:
        grepproc = subprocess.Popen(['grep', 'Session ended', log_file],
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
        lines, _ = grepproc.communicate()
        lines = str(lines, 'utf-8').split('\n')
        for line in reversed(lines):
            m = re.match('.* (\d+ \w+ \d+) at (.+) at ([0-9\.]+)', line)
            if not m:
                continue
            date, time, lat = m.groups()
            game.commands['date'].do_command(date)
            game.commands['time'].do_command(time)
            game.commands['lat'].do_command(lat)
            break
        else:
            print("Couldn't init date/time/lat from log file")

    while True:
        try:
            user_input = session.prompt("> ",
                                        completer=DnDCompleter(
                                            commands=game.commands,
                                            ignore_case=True),
                                        bottom_toolbar=bottom_toolbar,
                                        auto_suggest=AutoSuggestFromHistory(),
                                        key_bindings=kb,
                                        style=style)
            if not user_input:
                continue
            else:
                user_input = user_input.split()

            command = game.commands.get(user_input[0]) or None
            if not command:
                print("Unknown command.")
                continue

            command.do_command(*user_input[1:])

            if game.changed:
                player_view_manager.update()

            print()
        except (EOFError, KeyboardInterrupt):
            pass
        except Exception as e:
            traceback.print_exc()
Beispiel #49
0
ui_style = Style.from_dict({
    'border':                         '#888888',
    'terminal.focused border':      'ansigreen bold',

    #'terminal titleba':            'bg:#aaaaaa #dddddd ',
    'terminal titlebar':            'bg:#888888 #ffffff',
#    'terminal titlebar paneindex':  'bg:#888888 #000000',

    'terminal.focused titlebar':   'bg:#448844 #ffffff',
    'terminal.focused titlebar name':   'bg:#88aa44 #ffffff',
    'terminal.focused titlebar paneindex':         'bg:#ff0000',

#    'titlebar title':               '',
#    'titlebar name':                '#ffffff noitalic',
#    'focused-terminal titlebar name':       'bg:#88aa44',
#    'titlebar.line':                '#444444',
#    'titlebar.line focused':       '#448844 noinherit',
#    'titlebar focused':            'bg:#5f875f #ffffff bold',
#    'titlebar.title focused':      '',
#    'titlebar.zoom':                'bg:#884400 #ffffff',
#    'titlebar paneindex':           '',
#    'titlebar.copymode':            'bg:#88aa88 #444444',
#    'titlebar.copymode.position':   '',

#    'focused-terminal titlebar.copymode':          'bg:#aaff44 #000000',
#    'titlebar.copymode.position': '#888888',

    'commandline':                  'bg:#4e4e4e #ffffff',
    'commandline.command':          'bold',
    'commandline.prompt':           'bold',
    #'statusbar':                    'noreverse bg:#448844 #000000',
    'statusbar':                    'noreverse bg:ansigreen #000000',
    'statusbar window':             '#ffffff',
    'statusbar window.current':     'bg:#44ff44 #000000',
    'auto-suggestion':               'bg:#4e5e4e #88aa88',
    'message':                      'bg:#bbee88 #222222',
    'background':                   '#888888',
    'clock':                        'bg:#88aa00',
    'panenumber':                   'bg:#888888',
    'panenumber focused':          'bg:#aa8800',
    'terminated':                   'bg:#aa0000 #ffffff',

    'confirmationtoolbar':          'bg:#880000 #ffffff',
    'confirmationtoolbar question': '',
    'confirmationtoolbar yesno':    'bg:#440000',

    'copy-mode-cursor-position':   'bg:ansiyellow ansiblack',

#    'search-toolbar':                       'bg:#88ff44 #444444',
    'search-toolbar.prompt':                'bg:#88ff44 #444444',
    'search-toolbar.text':                  'bg:#88ff44 #000000',
#    'search-toolbar focused':              'bg:#aaff44 #444444',
#    'search-toolbar.text focused':         'bold #000000',

    'search-match':                  '#000000 bg:#88aa88',
    'search-match.current':          '#000000 bg:#aaffaa underline',

    # Pop-up dialog. Ignore built-in style.
    'dialog':                        'noinherit',
    'dialog.body':                   'noinherit',
    'dialog frame':                  'noinherit',
    'dialog.body text-area':         'noinherit',
    'dialog.body text-area last-line': 'noinherit',

}, priority=Priority.MOST_PRECISE)
Beispiel #50
0
    'count',
    'group by',
    'order by',
    'isnull',
    'join',
    'outer',
    'replace',
    'with',
    'commit',
    'rollback',
    'current_date'
]

style = Style.from_dict({
    "completion-menu.completion": "bg:#004444 #ffffff",
    "completion-menu.completion.current": "bg:#00aaaa #000000",
    "scrollbar.background": "bg:#88aaaa",
    "scrollbar.button": "bg:#222222",
})


def main(root_dir=None):
    db = CSVDB(root_dir)
    populate_table_list(db)
    session = PromptSession(lexer=PygmentsLexer(SqlLexer),
                            completer=WordCompleter(sql_csv_keywords,
                                                    ignore_case=True),
                            style=style,
                            history=FileHistory('history.txt'),
                            auto_suggest=AutoSuggestFromHistory())

    while True:
# Global key bindings.
bindings = KeyBindings()
bindings.add('tab')(focus_next)
bindings.add('s-tab')(focus_previous)


style = Style.from_dict({
    'window.border': '#888888',
    'shadow': 'bg:#222222',

    'menu-bar': 'bg:#aaaaaa #888888',
    'menu-bar.selected-item': 'bg:#ffffff #000000',
    'menu': 'bg:#888888 #ffffff',
    'menu.border': '#aaaaaa',
    'window.border shadow': '#444444',

    'focused  button': 'bg:#880000 #ffffff noinherit',

    # Styling for Dialog widgets.

    'radiolist focused': 'noreverse',
    'radiolist focused radio.selected': 'reverse',

    'button-bar': 'bg:#aaaaff'
})


application = Application(
    layout=Layout(
        root_container,
        focused_element=yes_button,
Beispiel #52
0
import os
import sys
import re
from pathlib import Path

import questionary
from prompt_toolkit.styles import Style

style = Style([
    ('qmark', 'fg:#673ab7 bold'),  # token in front of the question
    ('question', 'bold fg:#268bd2'),  # question text
    ('answer', 'fg:#f44336 bold'),  # submitted answer text behind the question
    ('pointer',
     'fg:#673ab7 bold'),  # pointer used in select and checkbox prompts
    ('highlighted',
     'fg:#cb4b16 bold'),  # pointed-at choice in select and checkbox prompts
    ('selected', 'fg:#cc5454'),  # style for a selected item of a checkbox
    ('separator', 'fg:#cc5454'),  # separator in lists
    ('instruction',
     'fg:#6c71c4'),  # user instructions for select, rawselect, checkbox
    ('text', 'fg:#d33682'),  # plain text
    ('disabled', 'fg:#858585 italic'
     )  # disabled choices for select and checkbox prompts
])

make_target_pattern = re.compile(r'^([a-zA-Z0-9][^$#\/\t=]+?):[^$#\/\t=].*$')


def get_makefile_rows(makefile_path):
    with open(makefile_path) as makefile:
        return makefile.readlines()
#!/usr/bin/env python
"""
A very simple progress bar which keep track of the progress as we consume an
iterator.
"""
from __future__ import unicode_literals
from prompt_toolkit.shortcuts import ProgressBar
from prompt_toolkit.styles import Style
import time

style = Style.from_dict({
    'title': '#4444ff underline',
    'label': '#ff4400 bold',
    'percentage': '#00ff00',
    'bar-a': 'bg:#00ff00 #004400',
    'bar-b': 'bg:#00ff00 #000000',
    'bar-c': '#000000 underline',
    'current': '#448844',
    'total': '#448844',
    'time-elapsed': '#444488',
    'time-left': 'bg:#88ff88 #000000',
})


def main():
    with ProgressBar(style=style, title='Progress bar example with custom styling.') as pb:
        for i in pb(range(1600), label='Downloading...'):
            time.sleep(.01)


if __name__ == '__main__':
    main()
Beispiel #54
0
])

# Key bindings.
bindings = KeyBindings()


@bindings.add("c-c")
@bindings.add("q")
def _(event):
    "Quit."
    event.app.exit()


style = Style.from_dict({
    "status": "reverse",
    "status.position": "#aaaa00",
    "status.key": "#ffaa00",
    "not-searching": "#888888",
})

# create application.
application = Application(
    layout=Layout(root_container, focused_element=text_area),
    key_bindings=bindings,
    enable_page_navigation_bindings=True,
    mouse_support=True,
    style=style,
    full_screen=True,
)


def run():
Beispiel #55
0
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.completion import WordCompleter
from prompt_toolkit.formatted_text import HTML
from prompt_toolkit.styles import Style
from termcolor import colored
from terminaltables import AsciiTable

import core.state as state
from core.listeners import Listeners
from core.modules import Modules
from core.sessions import Sessions
from core.stagers import Stagers
from core.utils import print_bad, print_banner

rprompt_style = Style.from_dict({
    'rprompt': 'bg:#ff0066 #ffffff',
})


def bottom_toolbar():
    return HTML(f"(Sessions: {state.SESSIONS} Listeners: {state.LISTENERS})")


def get_rprompt(error=False):
    return ' Error ' if error else ''


class UserExit(Exception):
    pass

Beispiel #56
0
def print_in_colors(out):
    style = Style.from_dict({"cli_out": "fg:#ffb4ad"})
    print_formatted_text(FormattedText([("class:cli_out", str(out))]), style=style)
"""
from __future__ import unicode_literals

from prompt_toolkit import prompt
from prompt_toolkit.styles import Style
from prompt_toolkit.formatted_text import HTML, ANSI


style = Style.from_dict({
    # Default style.
    '':          '#ff0066',

    # Prompt.
    'username': '******',
    'at':       '#00aa00',
    'colon':    '#00aa00',
    'pound':    '#00aa00',
    'host':     '#000088 bg:#aaaaff',
    'path':     '#884444 underline',

    # Make a selection reverse/underlined.
    # (Use Control-Space to select.)
    'selected-text': 'reverse underline',
})


def example_1():
    """
    Style and list of (style, text) tuples.
    """
    # Not that we can combine class names and inline styles.
    prompt_fragments = [
Beispiel #58
0
def checkbox(
    message: str,
    choices: Sequence[Union[str, Choice, Dict[str, Any]]],
    default: Optional[str] = None,
    validate: Callable[[List[str]], Union[bool, str]] = lambda a: True,
    qmark: str = DEFAULT_QUESTION_PREFIX,
    pointer: Optional[str] = DEFAULT_SELECTED_POINTER,
    style: Optional[Style] = None,
    initial_choice: Optional[Union[str, Choice, Dict[str, Any]]] = None,
    **kwargs: Any,
) -> Question:
    """Ask the user to select from a list of items.

    This is a multiselect, the user can choose one, none or many of the
    items.

    Example:
        >>> import questionary
        >>> questionary.checkbox(
        ...    'Select toppings',
        ...    choices=[
        ...        "Cheese",
        ...        "Tomato",
        ...        "Pineapple",
        ...    ]).ask()
        ? Select toppings done (2 selections)
        ['Cheese', 'Pineapple']

    .. image:: ../images/checkbox.gif

    This is just a realy basic example, the prompt can be customised using the
    parameters.


    Args:
        message: Question text

        choices: Items shown in the selection, this can contain :class:`Choice` or
                 or :class:`Separator` objects or simple items as strings. Passing
                 :class:`Choice` objects, allows you to configure the item more
                 (e.g. preselecting it or disabling it).

        default: Default return value (single value). If you want to preselect
                 multiple items, use ``Choice("foo", checked=True)`` instead.

        validate: Require the entered value to pass a validation. The
                  value can not be submitted until the validator accepts
                  it (e.g. to check minimum password length).

                  This should be a function accepting the input and
                  returning a boolean. Alternatively, the return value
                  may be a string (indicating failure), which contains
                  the error message to be displayed.

        qmark: Question prefix displayed in front of the question.
               By default this is a ``?``.

        pointer: Pointer symbol in front of the currently highlighted element.
                 By default this is a ``»``.
                 Use ``None`` to disable it.

        style: A custom color and style for the question parts. You can
               configure colors as well as font types for different elements.

        initial_choice: A value corresponding to a selectable item in the choices,
                        to initially set the pointer position to.

    Returns:
        :class:`Question`: Question instance, ready to be prompted (using ``.ask()``).
    """

    merged_style = merge_styles([
        DEFAULT_STYLE,
        # Disable the default inverted colours bottom-toolbar behaviour (for
        # the error message). However it can be re-enabled with a custom
        # style.
        Style([("bottom-toolbar", "noreverse")]),
        style,
    ])

    if not callable(validate):
        raise ValueError("validate must be callable")

    ic = InquirerControl(choices,
                         default,
                         pointer=pointer,
                         initial_choice=initial_choice)

    def get_prompt_tokens() -> List[Tuple[str, str]]:
        tokens = []

        tokens.append(("class:qmark", qmark))
        tokens.append(("class:question", " {} ".format(message)))

        if ic.is_answered:
            nbr_selected = len(ic.selected_options)
            if nbr_selected == 0:
                tokens.append(("class:answer", "done"))
            elif nbr_selected == 1:
                if isinstance(ic.get_selected_values()[0].title, list):
                    ts = ic.get_selected_values()[0].title
                    tokens.append((
                        "class:answer",
                        "".join([token[1] for token in ts]),  # type:ignore
                    ))
                else:
                    tokens.append((
                        "class:answer",
                        "[{}]".format(ic.get_selected_values()[0].title),
                    ))
            else:
                tokens.append(("class:answer",
                               "done ({} selections)".format(nbr_selected)))
        else:
            tokens.append((
                "class:instruction",
                "(Use arrow keys to move, "
                "<space> to select, "
                "<a> to toggle, "
                "<i> to invert)",
            ))
        return tokens

    def get_selected_values() -> List[Any]:
        return [c.value for c in ic.get_selected_values()]

    def perform_validation(selected_values: List[str]) -> bool:

        verdict = validate(selected_values)
        valid = verdict is True

        if not valid:
            if verdict is False:
                error_text = INVALID_INPUT
            else:
                error_text = str(verdict)

            error_message = FormattedText([("class:validation-toolbar",
                                            error_text)])

        ic.error_message = (error_message
                            if not valid and ic.submission_attempted else None)

        return valid

    layout = common.create_inquirer_layout(ic, get_prompt_tokens, **kwargs)

    bindings = KeyBindings()

    @bindings.add(Keys.ControlQ, eager=True)
    @bindings.add(Keys.ControlC, eager=True)
    def _(event):
        event.app.exit(exception=KeyboardInterrupt, style="class:aborting")

    @bindings.add(" ", eager=True)
    def toggle(_event):
        pointed_choice = ic.get_pointed_at().value
        if pointed_choice in ic.selected_options:
            ic.selected_options.remove(pointed_choice)
        else:
            ic.selected_options.append(pointed_choice)

        perform_validation(get_selected_values())

    @bindings.add("i", eager=True)
    def invert(_event):
        inverted_selection = [
            c.value for c in ic.choices if not isinstance(c, Separator)
            and c.value not in ic.selected_options and not c.disabled
        ]
        ic.selected_options = inverted_selection

        perform_validation(get_selected_values())

    @bindings.add("a", eager=True)
    def all(_event):
        all_selected = True  # all choices have been selected
        for c in ic.choices:
            if (not isinstance(c, Separator)
                    and c.value not in ic.selected_options and not c.disabled):
                # add missing ones
                ic.selected_options.append(c.value)
                all_selected = False
        if all_selected:
            ic.selected_options = []

        perform_validation(get_selected_values())

    @bindings.add(Keys.Down, eager=True)
    @bindings.add("j", eager=True)
    def move_cursor_down(_event):
        ic.select_next()
        while not ic.is_selection_valid():
            ic.select_next()

    @bindings.add(Keys.Up, eager=True)
    @bindings.add("k", eager=True)
    def move_cursor_up(_event):
        ic.select_previous()
        while not ic.is_selection_valid():
            ic.select_previous()

    @bindings.add(Keys.ControlM, eager=True)
    def set_answer(event):

        selected_values = get_selected_values()
        ic.submission_attempted = True

        if perform_validation(selected_values):
            ic.is_answered = True
            event.app.exit(result=selected_values)

    @bindings.add(Keys.Any)
    def other(_event):
        """Disallow inserting other text. """
        pass

    return Question(
        Application(
            layout=layout,
            key_bindings=bindings,
            style=merged_style,
            **utils.used_kwargs(kwargs, Application.__init__),
        ))

operators1 = ['add', 'sub', 'div', 'mul']
operators2 = ['sqrt', 'log', 'sin', 'ln']


def create_grammar():
    return compile("""
        (\s*  (?P<operator1>[a-z]+)   \s+   (?P<var1>[0-9.]+)   \s+   (?P<var2>[0-9.]+)   \s*) |
        (\s*  (?P<operator2>[a-z]+)   \s+   (?P<var1>[0-9.]+)   \s*)
    """)


example_style = Style.from_dict({
    'operator':       '#33aa33 bold',
    'number':         '#ff0000 bold',

    'trailing-input': 'bg:#662222 #ffffff',
})


if __name__ == '__main__':
    g = create_grammar()

    lexer = GrammarLexer(g, lexers={
        'operator1': SimpleLexer('class:operator'),
        'operator2': SimpleLexer('class:operator'),
        'var1': SimpleLexer('class:number'),
        'var2': SimpleLexer('class:number'),
    })

    completer = GrammarCompleter(g, {
from prompt_toolkit.lexers import PygmentsLexer
from prompt_toolkit import PromptSession


def show_help():
    print("""Help
--------
quit - quit this application
exit - exit from this application
eval - evaluate
""")


new_tui_style = Style.from_dict({
    "rprompt": "bg:#ff0066 #ffffff",
    "bottom-toolbar": "bg:#ffffff #333333 reverse",
    "prompt": "bg:#ansiyellow #000000",
})

s = PromptSession()

while True:
    try:
        cmd = s.prompt(
            "Command: ",
            validate_while_typing=True,
            enable_open_in_editor=True,
            bottom_toolbar="Available commands: quit, exit, help, eval",
            rprompt="Don't panic!",
            style=new_tui_style,
            lexer=PygmentsLexer(PythonLexer),