Пример #1
0
    def _load_env(self, data, encoding):
        """Merge environment variables"""
        if (DEBUG):
            print('entity.meta.py - _load_env(self,data,encoding)- caller:'+str(inspect.stack()[1][3]))

        d = copy.copy(self.env)
        d.update([(to_unicode(k, encoding), to_unicode(v, encoding)) for k, v in data.items()])
        self.env = d
        return self
Пример #2
0
    def parse(data, meta, encoding='utf-8'):
        """
        Parse one command line.
        :param data: string or dict:
        :param meta: Meta: meta configuration inherited from the parent menu
        :param encoding: string:
        :return: CommandLine:
        """
        if (DEBUG):
            print('entity.command_line.py  CommandLine- parse()- caller:' +
                  str(inspect.stack()[1][3]))

        assert isinstance(meta, Meta)

        def f(s):
            return to_unicode(s, encoding)

        if is_strlike(data):
            return CommandLine(f(data), meta, encoding)
        elif isinstance(data, dict):
            cmd, params = get_single_item(data)
            assert is_strlike(
                cmd), 'cmd must be string, not %s.' % type(cmd).__name__
            new_meta = meta.updated(params, encoding)
            return CommandLine(to_unicode(cmd, encoding), new_meta, encoding)
        else:
            raise ValueError('CommandLine must be string or dict, not %s.' %
                             type(data).__name__)
Пример #3
0
    def _load_work_dir(self, data, encoding):
        """Overwrite working directory"""
        if (DEBUG):
            print('entity.meta.py - Meta -  _load_work_dir(self,data,encoding)- caller:'+str(inspect.stack()[1][3]))

        self.work_dir = to_unicode(data, encoding)
        return self
Пример #4
0
    def parse(data, meta, loader, encoding='utf-8', depth=0):
        """
        Parse one command operation.
        :param data: dict:
        :param meta: Meta: meta configuration inherited from the parent menu
        :param loader: not used
        :param encoding: string:
        :param depth: not used
        :return: Command:
        """
        if (DEBUG):
            print('entity.command.py - Command -parse(data,meta,loader,encoding,depth) - caller:'+str(inspect.stack()[1][3]))

        if len(data) != 1:
            raise ValueError('Command should have only one element, not %s.' % len(data))

        title, content = get_single_item(data)
        assert isinstance(title, six.string_types), 'Command title must be string, not %s' % type(title).__name__
        title = to_unicode(title, encoding)

        if isinstance(content, six.string_types):
            # single command
            return Command(title, [CommandLine.parse(content, meta, encoding)])
        elif isinstance(content, list):
            # command list
            return Command(title, [CommandLine.parse(d, meta, encoding) for d in content])
        else:
            raise ValueError('Invalid command content type: %s' % type(content).__name__)
Пример #5
0
def print_safe(str_or_bytes,
               encoding='utf-8',
               errors='ignore',
               output=sys.stdout,
               newline='\n'):
    """
    Print unicode or bytes universally.

    :param str_or_bytes: string
    :param encoding: encoding
    :param output: output file handler
    :param errors: error handling scheme. Refer to codecs.register_error.
    """

    if (DEBUG):
        print(
            'commons.io.py - print_safe(str_or_bytes,encoding=utf-8,errors=ignore,output-sys.stdout,newline=\n)- called by:'
            + str(inspect.stack()[1][3]))

    writer = output.buffer if hasattr(output, 'buffer') else output

    # When the input type is bytes, verify it can be decoded with the specified encoding.
    decoded = str_or_bytes if is_unicode(str_or_bytes) else to_unicode(
        str_or_bytes, encoding, errors)
    encoded = to_bytes(decoded, encoding, errors)

    writer.write(encoded + to_bytes(newline, encoding, errors))
    output.flush()
Пример #6
0
    def parse(data, meta, loader, encoding='utf-8', depth=0):
        """
        :param data:
        :param meta:
        :param loader:
        :param encoding:
        :param depth:
        :return:
        """
        if (DEBUG):
            print('entity.menu.py - Menu - parse()- caller:' +
                  str(inspect.stack()[1][3]))

        from auth0_client.menu.entity import KEYWORD_META, Item

        if (DEBUG):
            print('read meta configurations')
            print(data)

        if KEYWORD_META in data:
            meta = meta.updated(data[KEYWORD_META], encoding)
            del data[KEYWORD_META]

        assert len(
            data) == 1, 'Menu should have only one item, not %s.' % len(data)

        title, content = get_single_item(data)
        assert isinstance(
            title, six.string_types
        ), 'Menu title must be string, not %s.' % type(title).__name__
        assert isinstance(
            content, list
        ), 'Menu content must be list, not %s.' % type(content).__name__
        title = to_unicode(title, encoding)

        items = [
            Item.parse(item, meta, loader, encoding, depth + 1)
            for item in content
        ]
        return Menu(title, items, meta)
Пример #7
0
    def getch(self):
        """
        Read one character from stdin.

        If stdin is not a tty or set `getch_enabled`=False, read input as one line.
        :return: unicode:
        """
        if (DEBUG):
            print('commons.terminal.py - getch() - called by:' +
                  str(inspect.stack()[1][3]))

        ch = self._get_one_char()
        if self.keep_input_clean:
            self.clear_input_buffer()

        try:
            # accept only unicode characters (for Python 2)
            uch = to_unicode(ch, 'ascii')
        except UnicodeError:
            return ''

        return uch if self._check_key_repeat(uch) else ''
Пример #8
0
 def f(s):
     return to_unicode(s, encoding)