Esempio n. 1
0
    def configure(self, prompt, default, parent, section_name):
        each_prompt = '?'
        if isinstance(prompt, tuple):
            each_prompt = prompt[1]
            prompt = prompt[0]

        if default is not NO_DEFAULT:
            default_prompt = ','.join(
                ['"{}"'.format(item) for item in default])
            prompt = '{} [{}]'.format(prompt, default_prompt)
        else:
            default = []
        print(prompt)
        values = []
        value = get_input(each_prompt + ' ') or default
        if (value == default) and not default:
            value = ''
        while value:
            if value == default:
                values.extend(value)
            else:
                values.append(value)
            value = get_input(each_prompt + ' ')

        section = getattr(parent, section_name)
        values = self._serialize(values, parent, section)
        return self._parse(values, parent, section)
Esempio n. 2
0
    def configure(self, prompt, default, parent, section_name):
        """
        With the ``prompt`` and ``default``, parse and return a value from
        terminal.
        """
        each_prompt = '?'
        if isinstance(prompt, tuple):
            each_prompt = prompt[1]
            prompt = prompt[0]

        if default is not NO_DEFAULT:
            default_prompt = ','.join(
                ['"{}"'.format(item) for item in default])
            prompt = '{} [{}]'.format(prompt, default_prompt)
        else:
            default = []
        print(prompt)
        values = []
        value = get_input(each_prompt + ' ') or default
        if (value == default) and not default:
            value = ''
        while value:
            if value == default:
                values.extend(value)
            else:
                values.append(value)
            value = get_input(each_prompt + ' ')
        return self.parse(self.serialize(values))
Esempio n. 3
0
 def _modules(self):
     home = os.getcwd()
     modules_dir = os.path.join(home, 'modules')
     filenames = sopel.loader.enumerate_modules(self)
     os.sys.path.insert(0, modules_dir)
     for name, mod_spec in iteritems(filenames):
         path, type_ = mod_spec
         try:
             module, _ = sopel.loader.load_module(name, path, type_)
         except Exception as e:
             filename, lineno = sopel.tools.get_raising_file_and_line()
             rel_path = os.path.relpath(filename, os.path.dirname(__file__))
             raising_stmt = "%s:%d" % (rel_path, lineno)
             stderr("Error loading %s: %s (%s)" % (name, e, raising_stmt))
         else:
             if hasattr(module, 'configure'):
                 prompt = name + ' module'
                 if module.__doc__:
                     doc = module.__doc__.split('\n', 1)[0]
                     if doc:
                         prompt = doc
                 prompt = 'Configure {} (y/n)? [n]'.format(prompt)
                 do_configure = get_input(prompt)
                 do_configure = do_configure and do_configure.lower() == 'y'
                 if do_configure:
                     module.configure(self)
     self.save()
Esempio n. 4
0
    def configure(self, prompt, default, parent, section_name):
        """Parse and return a value from user's input.

        :param str prompt: text to show the user
        :param mixed default: default value used if no input given
        :param parent: usually the parent Config object
        :type parent: :class:`~sopel.config.Config`
        :param str section_name: the name of the containing section

        This method displays the ``prompt`` and waits for user's input on the
        terminal. If no input is provided (i.e. the user just presses "Enter"),
        the ``default`` value is returned instead.

        If :attr:`.is_secret` is ``True``, the input will be hidden, using the
        built-in :func:`~getpass.getpass` function.
        """
        if default is not NO_DEFAULT and default is not None:
            prompt = '{} [{}]'.format(prompt, default)

        if self.is_secret:
            value = getpass.getpass(prompt + ' (hidden input) ')
        else:
            value = get_input(prompt + ' ')

        if not value and default is NO_DEFAULT:
            raise ValueError("You must provide a value for this option.")

        value = value or default
        section = getattr(parent, section_name)

        return self._parse(value, parent, section)
Esempio n. 5
0
 def _modules(self):
     home = os.getcwd()
     modules_dir = os.path.join(home, 'modules')
     filenames = sopel.loader.enumerate_modules(self)
     os.sys.path.insert(0, modules_dir)
     for name, mod_spec in iteritems(filenames):
         path, type_ = mod_spec
         try:
             module, _ = sopel.loader.load_module(name, path, type_)
         except Exception as e:
             filename, lineno = sopel.tools.get_raising_file_and_line()
             rel_path = os.path.relpath(filename, os.path.dirname(__file__))
             raising_stmt = "%s:%d" % (rel_path, lineno)
             stderr("Error loading %s: %s (%s)" % (name, e, raising_stmt))
         else:
             if hasattr(module, 'configure'):
                 prompt = name + ' module'
                 if module.__doc__:
                     doc = module.__doc__.split('\n', 1)[0]
                     if doc:
                         prompt = doc
                 prompt = 'Configure {} (y/n)? [n]'.format(prompt)
                 do_configure = get_input(prompt)
                 do_configure = do_configure and do_configure.lower() == 'y'
                 if do_configure:
                     module.configure(self)
     self.save()
Esempio n. 6
0
File: types.py Progetto: xnaas/sopel
 def configure(self, prompt, default, parent, section_name):
     if default is not NO_DEFAULT and default is not None:
         prompt = '{} [{}]'.format(prompt, default)
     value = get_input(prompt + ' ')
     if not value and default is NO_DEFAULT:
         raise ValueError("You must provide a value for this option.")
     value = value or default
     return self.parse(value, parent, section_name)
Esempio n. 7
0
    def configure(self, prompt, default, parent, section_name):
        each_prompt = '?'
        if isinstance(prompt, tuple):
            each_prompt = prompt[1]
            prompt = prompt[0]

        if default is not NO_DEFAULT:
            default = ','.join(default)
            prompt = '{} [{}]'.format(prompt, default)
        else:
            default = ''
        print(prompt)
        values = []
        value = get_input(each_prompt + ' ') or default
        while value:
            values.append(value)
            value = get_input(each_prompt + ' ')
        return self.parse(','.join(values))
Esempio n. 8
0
    def configure(self, prompt, default, parent, section_name):
        each_prompt = '?'
        if isinstance(prompt, tuple):
            each_prompt = prompt[1]
            prompt = prompt[0]

        if default is not NO_DEFAULT:
            default = ','.join(default)
            prompt = '{} [{}]'.format(prompt, default)
        else:
            default = ''
        print(prompt)
        values = []
        value = get_input(each_prompt + ' ') or default
        while value:
            values.append(value)
            value = get_input(each_prompt + ' ')
        return self.parse(','.join(values))
Esempio n. 9
0
 def configure(self, prompt, default):
     """With the prompt and default, parse and return a value from terminal.
     """
     if default is not NO_DEFAULT and default is not None:
         prompt = '{} [{}]'.format(prompt, default)
     value = get_input(prompt + ' ')
     if not value and default is NO_DEFAULT:
         raise ValueError("You must provide a value for this option.")
     value = value or default
     return self.parse(value)
Esempio n. 10
0
 def configure(self, prompt, default, parent, section_name):
     """With the prompt and default, parse and return a value from terminal.
     """
     if default is not NO_DEFAULT and default is not None:
         prompt = '{} [{}]'.format(prompt, default)
     value = get_input(prompt + ' ')
     if not value and default is NO_DEFAULT:
         raise ValueError("You must provide a value for this option.")
     value = value or default
     return self.parse(parent, section_name, value)
Esempio n. 11
0
    def configure(self, prompt, default, parent, section_name):
        each_prompt = '?'
        if isinstance(prompt, tuple):
            each_prompt = prompt[1]
            prompt = prompt[0]

        if default is not NO_DEFAULT:
            default_prompt = ','.join(['"{}"'.format(item) for item in default])
            prompt = '{} [{}]'.format(prompt, default_prompt)
        else:
            default = []
        print(prompt)
        values = []
        value = get_input(each_prompt + ' ') or default
        if (value == default) and not default:
            value = ''
        while value:
            if value == default:
                values.extend(value)
            else:
                values.append(value)
            value = get_input(each_prompt + ' ')
        return self.parse(self.serialize(values))
Esempio n. 12
0
    def option(self, question, default=False):
        """Ask "y/n" and return the corresponding boolean answer.

        Show user in terminal a "y/n" prompt, and return true or false based on
        the response. If default is passed as true, the default will be shown
        as ``[y]``, else it will be ``[n]``. ``question`` should be phrased as
        a question, but without a question mark at the end.

        """
        d = 'n'
        if default:
            d = 'y'
        ans = tools.get_input(question + ' (y/n)? [' + d + '] ')
        if not ans:
            ans = d
        return ans.lower() == 'y'
Esempio n. 13
0
    def option(self, question, default=False):
        """Ask "y/n" and return the corresponding boolean answer.

        Show user in terminal a "y/n" prompt, and return true or false based on
        the response. If default is passed as true, the default will be shown
        as ``[y]``, else it will be ``[n]``. ``question`` should be phrased as
        a question, but without a question mark at the end.

        """
        d = 'n'
        if default:
            d = 'y'
        ans = get_input(question + ' (y/n)? [' + d + '] ')
        if not ans:
            ans = d
        return ans.lower() == 'y'
Esempio n. 14
0
    def configure(self, prompt, default, parent, section_name):
        """Parse and return a value from user's input.

        :param str prompt: text to show the user
        :param bool default: default value used if no input given
        :param parent: usually the parent Config object
        :type parent: :class:`~sopel.config.Config`
        :param str section_name: the name of the containing section

        This method displays the ``prompt`` and waits for user's input on the
        terminal. If no input is provided (i.e. the user just presses "Enter"),
        the ``default`` value is returned instead.
        """
        prompt = '{} ({})'.format(prompt, 'Y/n' if default else 'y/N')
        value = get_input(prompt + ' ') or default
        section = getattr(parent, section_name)
        return self._parse(value, parent, section)
Esempio n. 15
0
    def option(self, question, default=False):
        """Ask the user a "y/n" question.

        :param str question: the question to ask the user
        :param bool default: ``True`` to show ``[y]`` as the default choice;
                             ``False`` to show ``[n]`` (optional; defaults to
                             ``False``)
        :return: the Boolean value corresponding to the user's choice
        :rtype: bool

        This will show a "y/n" prompt in the user's terminal, and return
        ``True`` or ``False`` based on the response. ``question`` should be
        phrased as a question, but without a question mark at the end.
        """
        d = 'n'
        if default:
            d = 'y'
        ans = tools.get_input(question + ' (y/n)? [' + d + '] ')
        if not ans:
            ans = d
        return ans.lower() == 'y'
Esempio n. 16
0
    from sopel.tools import get_input
    from ConfigParser import RawConfigParser

    try:
        current_config = Config('pulsebot.cfg')
    except:
        current_config = None
    config = Config('pulsebot.cfg.in')
    new_config = RawConfigParser()

    for section in config.parser.sections():
        new_config.add_section(section)

        for name, value in config.parser.items(section):
            if value.startswith('@') and value.endswith('@'):
                s, n = value[1:-1].split('.', 1)
                if current_config and current_config.parser.has_option(s, n):
                    value = current_config.parser.get(s, n)
                else:
                    prompt = 'Please enter a value for %s.%s: ' % (section,
                                                                   name)
                    while True:
                        sys.stderr.write(prompt)
                        value = get_input('')
                        if value:
                            break

            new_config.set(section, name, value)

    new_config.write(sys.stdout)
Esempio n. 17
0
    from sopel.tools import get_input
    from ConfigParser import RawConfigParser

    try:
        current_config = Config('pulsebot.cfg')
    except Exception:
        current_config = None
    config = Config('pulsebot.cfg.in')
    new_config = RawConfigParser()

    for section in config.parser.sections():
        new_config.add_section(section)

        for name, value in config.parser.items(section):
            if value.startswith('@') and value.endswith('@'):
                s, n = value[1:-1].split('.', 1)
                if current_config and current_config.parser.has_option(s, n):
                    value = current_config.parser.get(s, n)
                else:
                    prompt = 'Please enter a value for %s.%s: ' % (section,
                                                                   name)
                    while True:
                        sys.stderr.write(prompt)
                        value = get_input('')
                        if value:
                            break

            new_config.set(section, name, value)

    new_config.write(sys.stdout)