Exemple #1
0
 def __init__(self, ch: str = '#', style: Optional[Dict[str, Any]] = None) -> None:
     """
     :param ch: The character (or characters) to fill the line with
     :param style: Click style dictionary
     """
     self.ch = force_text(ch)
     self.style = (style or {})
Exemple #2
0
 def __init__(self, ch='#', style=None):
     """
     :param ch: The character (or characters) to fill the line with
     :type ch: str
     :param style: Click style dictionary
     """
     self.ch = force_text(ch)
     self.style = (style or {})
Exemple #3
0
def _get_current_api_session() -> APISession:
    """
    Get an API session, either from the Click context cache, or a new one from the config.
    """
    host, token = get_host_and_token()
    ctx = click.get_current_context(silent=True) or None
    cache_key: str = force_text(f'_api_session_{host}_{token}')
    session: Optional[APISession] = (getattr(ctx, cache_key, None)
                                     if ctx else None)
    if not session:
        session = APISession(host, token)
        if ctx:
            setattr(ctx, cache_key, session)
    return session
Exemple #4
0
def _get_current_api_session():
    """
    Get an API session, either from the Click context cache, or a new one from the config.

    :return: API session
    :rtype: APISession
    """
    host, token = get_host_and_token()
    ctx = click.get_current_context(silent=True) or None
    cache_key = force_text('_api_session_%s_%s' % (host, token))
    session = (getattr(ctx, cache_key, None) if ctx else None)
    if not session:
        session = APISession(host, token)
        if ctx:
            setattr(ctx, cache_key, session)
    return session
Exemple #5
0
    def add(self, content, flex=1, style=None, align='left'):
        """
        Add a content column to the flex.

        :param content: String content
        :type content: str
        :param flex: Flex value; if 0, the column will always take as much space as its content needs.
        :type flex: int
        :param style: Click style dictionary
        :type style: dict
        :param align: Alignment for the content (left/right/center).
        :type align: str
        :return: The Flex, for chaining
        :rtype: Flex
        """
        self.cells.append({
            'content': force_text(content),
            'flex': flex,
            'style': style or {},
            'align': align,
        })
        return self
Exemple #6
0
def match_prefix(choices: Iterable[Any],
                 value: str,
                 return_unique: bool = True) -> Union[List[Any], Any, None]:
    """
    Match `value` in `choices` by case-insensitive prefix matching.

    If the exact `value` is in `choices` (and `return_unique` is set),
    that exact value is returned as-is.

    :param choices: Choices to match in. May be non-string; `str()` is called on them if not.
    :param value: The value to use for matching.
    :param return_unique: If only one option was found, return it; otherwise return None.
                          If this is not true, all of the filtered choices are returned.
    :return: list, object or none; see the `return_unique` option.
    """
    if return_unique and value in choices:
        return value
    value_re = re.compile(f'^{re.escape(value)}', re.I)
    choices = [
        choice for choice in choices if value_re.match(force_text(choice))
    ]
    if return_unique:
        return (choices[0] if len(choices) == 1 else None)
    return choices
Exemple #7
0
def test_force_text_and_bytes():
    assert force_text(b'f\xc3\xb6\xc3\xb6') == 'föö'
    assert force_bytes('föö', encoding='iso-8859-1') == b'f\xf6\xf6'
    assert force_bytes(8) == b'8'
    assert force_text([]) == '[]'