Example #1
0
def get_completer():
    def get_paths():
        return [build_context.config.work_dir]

    return NestedCompleter.from_nested_dict({
        "help":
        None,
        "glob": {
            "build": None,
            "host": None
        },
        "exit":
        None,
        "ls":
        PathCompleter(get_paths=get_paths),
        "edit": {
            "file": PathCompleter(get_paths=get_paths),
            "script": None,
            "recipe": None,
        },
        "build":
        None,
        "patch": {
            "show": None,
            "save": None
        },
    })
Example #2
0
def test_pathcompleter_completes_in_current_directory():
    completer = PathCompleter()
    doc_text = ""
    doc = Document(doc_text, len(doc_text))
    event = CompleteEvent()
    completions = list(completer.get_completions(doc, event))
    assert len(completions) > 0
def test_pathcompleter_completes_in_current_directory():
    completer = PathCompleter()
    doc_text = ''
    doc = Document(doc_text, len(doc_text))
    event = CompleteEvent()
    completions = list(completer.get_completions(doc, event))
    assert len(completions) > 0
Example #4
0
def create_completer(
    get_globals,
    get_locals,
    magics_manager,
    alias_manager,
    get_enable_dictionary_completion,
):
    g = create_ipython_grammar()

    return GrammarCompleter(
        g,
        {
            "python":
            PythonCompleter(get_globals, get_locals,
                            get_enable_dictionary_completion),
            "magic":
            MagicsCompleter(magics_manager),
            "alias_name":
            AliasCompleter(alias_manager),
            "pdb_arg":
            WordCompleter(["on", "off"], ignore_case=True),
            "autocall_arg":
            WordCompleter(["0", "1", "2"], ignore_case=True),
            "py_filename":
            PathCompleter(only_directories=False,
                          file_filter=lambda name: name.endswith(".py")),
            "filename":
            PathCompleter(only_directories=False),
            "directory":
            PathCompleter(only_directories=True),
            "system":
            SystemCompleter(),
        },
    )
def create_completer(get_globals, get_locals, magics_manager, alias_manager):
    g = create_ipython_grammar()

    return GrammarCompleter(
        g, {
            'python':
            PythonCompleter(get_globals, get_locals),
            'magic':
            MagicsCompleter(magics_manager),
            'alias_name':
            AliasCompleter(alias_manager),
            'pdb_arg':
            WordCompleter(['on', 'off'], ignore_case=True),
            'autocall_arg':
            WordCompleter(['0', '1', '2'], ignore_case=True),
            'py_filename':
            PathCompleter(only_directories=False,
                          file_filter=lambda name: name.endswith('.py')),
            'filename':
            PathCompleter(only_directories=False),
            'directory':
            PathCompleter(only_directories=True),
            'system':
            SystemCompleter(),
        })
def test_pathcompleter_does_not_expanduser_by_default():
    completer = PathCompleter()
    doc_text = '~'
    doc = Document(doc_text, len(doc_text))
    event = CompleteEvent()
    completions = list(completer.get_completions(doc, event))
    assert [] == completions
def test_pathcompleter_can_expanduser():
    completer = PathCompleter(expanduser=True)
    doc_text = '~'
    doc = Document(doc_text, len(doc_text))
    event = CompleteEvent()
    completions = list(completer.get_completions(doc, event))
    assert len(completions) > 0
Example #8
0
 def get_path_matches(self, _, word_before_cursor):
     completer = PathCompleter(expanduser=True)
     document = Document(
         text=word_before_cursor, cursor_position=len(word_before_cursor)
     )
     for c in completer.get_completions(document, None):
         yield Match(completion=c, priority=(0,))
def test_pathcompleter_respects_completions_under_min_input_len():
    # setup: create a test dir with 10 files
    test_dir = tempfile.mkdtemp()
    write_test_files(test_dir)

    # min len:1 and no text
    with chdir(test_dir):
        completer = PathCompleter(min_input_len=1)
        doc_text = ''
        doc = Document(doc_text, len(doc_text))
        event = CompleteEvent()
        completions = list(completer.get_completions(doc, event))
        assert [] == completions

    # min len:1 and text of len 1
    with chdir(test_dir):
        completer = PathCompleter(min_input_len=1)
        doc_text = '1'
        doc = Document(doc_text, len(doc_text))
        event = CompleteEvent()
        completions = list(completer.get_completions(doc, event))
        result = [c.text for c in completions]
        assert [''] == result

    # min len:0 and text of len 2
    with chdir(test_dir):
        completer = PathCompleter(min_input_len=0)
        doc_text = '1'
        doc = Document(doc_text, len(doc_text))
        event = CompleteEvent()
        completions = list(completer.get_completions(doc, event))
        result = [c.text for c in completions]
        assert [''] == result

    # create 10 files with a 2 char long name
    for i in range(10):
        with open(os.path.join(test_dir, str(i) * 2), 'wb') as out:
            out.write(b'')

    # min len:1 and text of len 1
    with chdir(test_dir):
        completer = PathCompleter(min_input_len=1)
        doc_text = '2'
        doc = Document(doc_text, len(doc_text))
        event = CompleteEvent()
        completions = list(completer.get_completions(doc, event))
        result = sorted(c.text for c in completions)
        assert ['', '2'] == result

    # min len:2 and text of len 1
    with chdir(test_dir):
        completer = PathCompleter(min_input_len=2)
        doc_text = '2'
        doc = Document(doc_text, len(doc_text))
        event = CompleteEvent()
        completions = list(completer.get_completions(doc, event))
        assert [] == completions

    # cleanup
    shutil.rmtree(test_dir)
def test_pathcompleter_can_expanduser():
    completer = PathCompleter(expanduser=True)
    doc_text = '~'
    doc = Document(doc_text, len(doc_text))
    event = CompleteEvent()
    completions = list(completer.get_completions(doc, event))
    assert len(completions) > 0
Example #11
0
def test_pathcompleter_does_not_expanduser_by_default():
    completer = PathCompleter()
    doc_text = "~"
    doc = Document(doc_text, len(doc_text))
    event = CompleteEvent()
    completions = list(completer.get_completions(doc, event))
    assert [] == completions
def test_pathcompleter_completes_directories_with_only_directories():
    # setup: create a test dir with 10 files
    test_dir = tempfile.mkdtemp()
    write_test_files(test_dir)

    # create a sub directory there
    os.mkdir(os.path.join(test_dir, 'subdir'))

    if not test_dir.endswith(os.path.sep):
        test_dir += os.path.sep

    with chdir(test_dir):
        completer = PathCompleter(only_directories=True)
        doc_text = ''
        doc = Document(doc_text, len(doc_text))
        event = CompleteEvent()
        completions = list(completer.get_completions(doc, event))
        result = [c.text for c in completions]
        assert ['subdir'] == result

    # check that there is no completion when passing a file
    with chdir(test_dir):
        completer = PathCompleter(only_directories=True)
        doc_text = '1'
        doc = Document(doc_text, len(doc_text))
        event = CompleteEvent()
        completions = list(completer.get_completions(doc, event))
        assert [] == completions

    # cleanup
    shutil.rmtree(test_dir)
Example #13
0
 def _path_completer(self):
     if self._path_completer_cache is None:
         self._path_completer_cache = GrammarCompleter(
             self._path_completer_grammar, {
                 'var1': PathCompleter(expanduser=True),
                 'var2': PathCompleter(expanduser=True),
             })
     return self._path_completer_cache
Example #14
0
def test_pathcompleter_can_expanduser(monkeypatch):
    monkeypatch.setenv('HOME', '/tmp')
    completer = PathCompleter(expanduser=True)
    doc_text = "~"
    doc = Document(doc_text, len(doc_text))
    event = CompleteEvent()
    completions = list(completer.get_completions(doc, event))
    assert len(completions) > 0
Example #15
0
 def get_path_matches(self, _, word_before_cursor):
     # pylint: disable=no-self-use
     # function cannot be static since it has to be a callable for get_completions
     completer = PathCompleter(expanduser=True)
     document = Document(text=word_before_cursor,
                         cursor_position=len(word_before_cursor))
     for c in completer.get_completions(document, None):
         yield Match(completion=c, priority=(0, ))
Example #16
0
    def __init__(self):
        self.path_completer = PathCompleter(
            file_filter=lambda s: os.path.isdir(s) or s.endswith('.lua'))

        self.word_completer = WordCompleter(
            words=self.CROW_COMMANDS.keys(),
            ignore_case=True,
            meta_dict=self.CROW_COMMANDS,
        )
Example #17
0
    def __init__(self, hummingbot_application):
        super(HummingbotCompleter, self).__init__()
        self.hummingbot_application = hummingbot_application

        # static completers
        self._path_completer = PathCompleter(get_paths=lambda: [f"./{CONF_FILE_PATH}"],
                                             file_filter=lambda fname: fname.endswith(".yml"))
        self._command_completer = WordCompleter(self.parser.commands, ignore_case=True)
        self._exchange_completer = WordCompleter(EXCHANGES, ignore_case=True)
        self._strategy_completer = WordCompleter(STRATEGIES, ignore_case=True)
Example #18
0
 def _path_completer(self) -> GrammarCompleter:
     if self._path_completer_cache is None:
         self._path_completer_cache = GrammarCompleter(
             self._path_completer_grammar,
             {
                 "var1": PathCompleter(expanduser=True),
                 "var2": PathCompleter(expanduser=True),
             },
         )
     return self._path_completer_cache
Example #19
0
    def __init__(self, hummingbot_application):
        super(HummingbotCompleter, self).__init__()
        self.hummingbot_application = hummingbot_application
        self._symbols: Dict[str, List[str]] = {}

        # static completers
        self._path_completer = PathCompleter()
        self._command_completer = WordCompleter(self.parser.commands,
                                                ignore_case=True)
        self._exchange_completer = WordCompleter(EXCHANGES, ignore_case=True)
        self._strategy_completer = WordCompleter(STRATEGIES, ignore_case=True)

        asyncio.ensure_future(self._fetch_symbols())
Example #20
0
 def arg_completer(arg: ArgInfo) -> Optional[Completer]:
     if arg.possible_values:
         meta = {a: arg.help_text
                 for a in arg.possible_values
                 } if arg.help_text is not None else None
         return FuzzyWordCompleter(arg.possible_values, meta_dict=meta)
     elif arg.value_hint == "file":
         return PathCompleter()
     elif arg.value_hint == "kind":
         return KindCompleter(kinds, props)
     elif arg.value_hint == "property":
         return PropertyCompleter(kinds, props)
     elif arg.value_hint == "property_list_plain":
         return PropertyListCompleter(kinds, props, with_as=False)
     elif arg.value_hint == "property_list_with_as":
         return PropertyListCompleter(kinds, props, with_as=True)
     elif arg.value_hint == "command":
         meta = {cmd.name: "command" for cmd in cmds}
         return FuzzyWordCompleter([cmd.name for cmd in cmds],
                                   meta_dict=meta)
     elif arg.value_hint == "search":
         return SearchCompleter(kinds, props)
     elif arg.value_hint == "aggregate":
         return AggregateCompleter(kinds, props)
     elif arg.help_text:
         return HintCompleter("<value>", arg.help_text)
     else:
         return HintCompleter("<value>")
Example #21
0
def edit(file_path, db_path):
    """Create and edit piece information"""
    if not file_path:
        try:
            with open(PATHSAVE, "r") as savepath:
                file_path = Path(savepath.read())
        except FileNotFoundError:
            file_path = prompt(
                "No path specified or saved. Please enter the path "
                "to the config file. ",
                completer=PathCompleter())
    try:
        piece = yaml_interface.read_config(Path(file_path))
        print('loaded piece')
        print(piece)
    except (ValueError, FileNotFoundError, AttributeError):
        piece = None
    db = db_interface.init_db(db_path)
    tables = db_interface.explore_db(db)
    if "composers" not in tables or "instruments" not in tables:
        bootstrap = prompt(
            "Lilyskel supports a small database to help with "
            "commonly used items (such as instruments and "
            "composers). You do not appear to have one. "
            "Would you like to copy the included one? ",
            default='Y',
            validator=YNValidator())
        if bootstrap.lower()[0] == 'y':
            db_interface.bootstrap_db(db_path)
    edit_prompt(piece, Path(file_path), db, PATHSAVE)
Example #22
0
 def __init__(self, completions, meta_dict, project_root):
     self.words = WordCompleter(completions,
                                meta_dict=meta_dict,
                                WORD=True,
                                ignore_case=True)
     # TODO Rewrite using the one in prompt-toolkit/completion/filesystem.py as ref, but include '/' at the end of dir completions
     self.paths = PathCompleter(get_paths=(lambda: [str(project_root)]))
Example #23
0
    async def coroutine():
        global current_file
        global isort_config
        global black_config

        open_dialog = TextInputDialog(
            title="Open file",
            label_text="Enter the path of a file:",
            completer=PathCompleter(),
        )

        filename = await show_dialog_as_float(open_dialog)

        if filename is not None:
            current_file = Path(filename).resolve()
            isort_config = isort.Config(settings_path=current_file.parent)
            black_config_file = black.find_pyproject_toml(
                (str(current_file), ))
            if black_config_file:
                black_config = black.parse_pyproject_toml(black_config_file)
            else:
                black_config = {}

            try:
                with open(current_file, "r",
                          encoding="utf8") as new_file_conent:
                    code.buffer.text = new_file_conent.read()
                    open_file_frame.title = current_file.name
                feedback(f"Successfully opened {current_file}")
            except IOError as error:
                feedback(f"Error: {error}")
Example #24
0
def chose_install_dir(json_file):
    validator = Validator.from_callable(
        lambda x: os.path.isdir(x) or x == '',
        error_message="Not a directory!",
        move_cursor_to_end=True,
    )
    path_completer = PathCompleter(only_directories=True)

    default_dir = json_file['install_dir'] or './'

    question = "\nPath to install datasets [empty to default " + \
        default_dir + "] "
    install_dir = prompt(question,
                         validator=validator,
                         completer=path_completer)
    if not install_dir:
        if not os.path.isdir(default_dir):
            os.mkdir(default_dir)
        install_dir = default_dir

    if install_dir.endswith('/'):
        install_dir = install_dir[:-1]

    json_file['install_dir'] = install_dir
    return install_dir
Example #25
0
    async def coroutine():
        global current_file
        global isort_config
        global black_config

        save_dialog = TextInputDialog(
            title="Save file",
            label_text="Enter the path of a file:",
            completer=PathCompleter(),
        )

        filename = await show_dialog_as_float(save_dialog)

        if filename is not None:
            current_file = Path(filename).resolve()
            isort_config = isort.Config(settings_path=current_file.parent)
            black_config_file = black.find_pyproject_toml(
                (str(current_file), ))
            if black_config_file:
                black_config = black.parse_pyproject_toml(black_config_file)
            else:
                black_config = {}
            if not current_file.suffixes and not current_file.exists():
                current_file = current_file.with_suffix(".py")
            open_file_frame.title = current_file.name
            save_file()
Example #26
0
def configure(config: Config):
    bastion = InstanceHelper().select_instance(message='Choose bastion host',
                                               include_none=True)

    bastion_user = questionary.text(message="Bastion host user",
                                    default=config.bastion_user or "").ask()

    bastion_port = questionary.text(message="Bastion host port",
                                    default=config.bastion_port or "22").ask()

    instance_user = questionary.text(message="Default instance user",
                                     default=config.instance_user or "").ask()

    key = Path(
        questionary.text(
            message="Private SSH Key Path",
            default=str(config.key) if config.key is not None else "",
            validate=KeyValidator,
            completer=PathCompleter(expanduser=True)).ask())

    # Save all of the configuration options
    config.bastion = bastion
    config.bastion_port = bastion_port
    config.bastion_user = bastion_user
    config.instance_user = instance_user
    config.key = key
Example #27
0
 def input_path(self):
     msg, default, validator = self.__get_commond_attr()
     path_completer = PathCompleter(only_directories=True, min_input_len=1)
     return prompt(msg,
                   completer=path_completer,
                   default=default,
                   validator=validator,
                   key_bindings=self.bindings)
Example #28
0
def ask_dir():
    Path = PathCompleter(only_directories=True)
    while True:
        with quittable():
            path = prompt('> ', completer=Path)
        if os.path.isdir(path):
            return path
        print("Not a valid directory")
Example #29
0
    def _run ():
        with patch_stdout():
            application = input_dialog( title = title, text = text, completer = PathCompleter() )

            with application.input.raw_mode():
                application.input.read_keys()

            application.layout.current_control.buffer.insert_text(default_value or "")
            
            return application.run_async()
Example #30
0
def prepare_completers(commands):
    completer_commands = WordCompleter(commands)
    repos = [str(r.parts[-1:][0]) for r in list_all_repos()]
    completer_repos = WordCompleter(repos)
    completer_files = PathCompleter(
        file_filter=lambda filename: str(filename).endswith('.csv'),
        min_input_len=0,
        get_paths=lambda: [current_path])
    return merge_completers(
        [completer_commands, completer_repos, completer_files])
Example #31
0
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

    completer = None
    if path:
        completer = PathCompleter(only_directories=True, expanduser=True)
    elif list_:
        completer = WordCompleter(
            sorted(list_),
            ignore_case=True,
            sentence=True,
            meta_dict=(list_ if isinstance(list_, dict) else None))
        if validate is None:
            validate = list_.__contains__

    first = True
    while True:
        try:
            rv = prompt(_get_prompt_tokens(),
                        default=default,
                        is_password=password,
                        completer=completer,
                        style=PROMPT_TOOLKIT_STYLE)
        except (EOFError, KeyboardInterrupt):
            sys.exit(1)
        # pasting a multiline string works even with multiline disabled :(
        rv = rv.replace('\n', ' ').strip()
        if not rv:
            if not required:
                break
        else:
            if path:
                rv = os.path.abspath(os.path.expanduser(rv))
            if validate is None or validate(rv):
                break
            if allow_invalid and _confirm('Keep this value anyway?'):
                break
            default = rv
        first = False
    return rv
Example #32
0
 def create_cmd_buffer(self, command):
     completer = PathCompleter()
     # if command == 'open' or command == 'inspect':
     # completer=PathCompleter()
     # else:
     # completer=None
     accept_handler = getattr(self, 'do_' + command)
     return Buffer(completer=completer,
                   name='cmd_buffer_' + command,
                   accept_handler=accept_handler,
                   multiline=False)
Example #33
0
def create_command_completer(editor):
    commands = [c + ' ' for c in get_commands()]

    return GrammarCompleter(COMMAND_GRAMMAR, {
        'command': WordCompleter(commands),
        'location': PathCompleter(expanduser=True),
        'set_option': WordCompleter(sorted(SET_COMMANDS)),
        'buffer_name': BufferNameCompleter(editor),
        'colorscheme': ColorSchemeCompleter(editor),
        'shell_command': SystemCompleter(),
    })
Example #34
0
    def add_workspace(self, initial=None, accept_all=False):
        if initial is None:
            initial = os.getcwd()
        else:
            initial = os.path.abspath(initial)
        existing_workspace = self.search_parent_in_workspaces(initial)
        if not existing_workspace:
            if accept_all:
                response = initial
            else:
                session = PromptSession(u"> ",
                                        completer=FuzzyCompleter(
                                            PathCompleter()))
                response = session.prompt(
                    "Path to find new components\n> ",
                    complete_while_typing=True,
                    default=initial,
                    pre_run=session.default_buffer.start_completion,
                    validator=dir_validator)
            # Check the final '/' characters
            if response in self.workspace_paths:
                print(f"{response} already exist in workspaces")
                return

            new_components = self.get_recursive_components_in_dir(response)

            print("%s\n%d components found in %s" %
                  (colored('\n'.join(new_components),
                           'green'), len(new_components), response))
            if len(new_components) == 0:
                print(
                    f"No component found in {response}. Workspaces not updated."
                )
                return

            if accept_all:
                answer = True
            else:
                answer = confirm(
                    f'Do you want to add {response} to workspaces?')

            if answer:
                print(f"{response} added to workspaces")
                self.components += new_components
                self.workspace_paths.append(response)
                self.save_workspace()
            else:
                print("Workspaces not updated.")
        else:
            print(
                f"{initial} is already part of an existing workspace ({existing_workspace})"
            )
            self.update_components_in_workspaces(existing_workspace)
Example #35
0
    def _make_directory_completer(self):
        """
        Private method that creates a completer for a field
        with ``data_type`` of "dir".

        Returns
        -------
        completer : prompt_toolkit.completion.base.Completer
            A ``Completer`` instance that suggests directory names
            as potential completions for user input.
        """
        return PathCompleter(expanduser=False, only_directories=True)
def test_pathcompleter_completes_files_in_current_directory():
    # setup: create a test dir with 10 files
    test_dir = tempfile.mkdtemp()
    write_test_files(test_dir)

    expected = sorted([str(i) for i in range(10)])

    if not test_dir.endswith(os.path.sep):
        test_dir += os.path.sep

    with chdir(test_dir):
        completer = PathCompleter()
        # this should complete on the cwd
        doc_text = ''
        doc = Document(doc_text, len(doc_text))
        event = CompleteEvent()
        completions = list(completer.get_completions(doc, event))
        result = sorted(c.text for c in completions)
        assert expected == result

    # cleanup
    shutil.rmtree(test_dir)
def test_pathcompleter_can_apply_file_filter():
    # setup: create a test dir with 10 files
    test_dir = tempfile.mkdtemp()
    write_test_files(test_dir)

    # add a .csv file
    with open(os.path.join(test_dir, 'my.csv'), 'wb') as out:
        out.write(b'')

    file_filter = lambda f: f and f.endswith('.csv')

    with chdir(test_dir):
        completer = PathCompleter(file_filter=file_filter)
        doc_text = ''
        doc = Document(doc_text, len(doc_text))
        event = CompleteEvent()
        completions = list(completer.get_completions(doc, event))
        result = [c.text for c in completions]
        assert ['my.csv'] == result

    # cleanup
    shutil.rmtree(test_dir)
def test_pathcompleter_completes_files_in_absolute_directory():
    # setup: create a test dir with 10 files
    test_dir = tempfile.mkdtemp()
    write_test_files(test_dir)

    expected = sorted([str(i) for i in range(10)])

    test_dir = os.path.abspath(test_dir)
    if not test_dir.endswith(os.path.sep):
        test_dir += os.path.sep

    completer = PathCompleter()
    # force unicode
    doc_text = text_type(test_dir)
    doc = Document(doc_text, len(doc_text))
    event = CompleteEvent()
    completions = list(completer.get_completions(doc, event))
    result = sorted([c.text for c in completions])
    assert expected == result

    # cleanup
    shutil.rmtree(test_dir)
def test_pathcompleter_get_paths_constrains_path():
    # setup: create a test dir with 10 files
    test_dir = tempfile.mkdtemp()
    write_test_files(test_dir)

    # add a subdir with 10 other files with different names
    subdir = os.path.join(test_dir, 'subdir')
    os.mkdir(subdir)
    write_test_files(subdir, 'abcdefghij')

    get_paths = lambda: ['subdir']

    with chdir(test_dir):
        completer = PathCompleter(get_paths=get_paths)
        doc_text = ''
        doc = Document(doc_text, len(doc_text))
        event = CompleteEvent()
        completions = list(completer.get_completions(doc, event))
        result = [c.text for c in completions]
        expected = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
        assert expected == result

    # cleanup
    shutil.rmtree(test_dir)
Example #40
0
 def get_path_matches(self, _, word_before_cursor):
     completer = PathCompleter(expanduser=True)
     document = Document(text=word_before_cursor,
                         cursor_position=len(word_before_cursor))
     for c in completer.get_completions(document, None):
         yield Match(completion=c, priority=(0,))