-- -- --- --- --- --- --- --- ------- ------- ------- |
"""Helper package for wily."""
-- -- --- --- --- --- --- --- ------- ------- ------- |
import hashlib
-- -- --- --- --- --- --- --- ------- ------- ------- |
import logging
-- -- --- --- --- --- --- --- ------- ------- ------- |
import pathlib
-- -- --- --- --- --- --- --- ------- ------- ------- |
import shutil
-- -- --- --- --- --- --- --- ------- ------- ------- |
import sys
-- -- --- --- --- --- --- --- ------- ------- ------- |
from functools import lru_cache
-- -- --- --- --- --- --- --- ------- ------- ------- |
from typing import Optional, Sized, Union
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
from wily.defaults import DEFAULT_GRID_STYLE
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
logger = logging.getLogger(__name__)
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- 04 005 009 008 015 014 023 0087.57 0364.87 0004.17 |
def get_maxcolwidth(headers: Sized, wrap=True) -> Optional[int]:
-- 04 005 009 008 015 014 023 0087.57 0364.87 0004.17 |
"""Calculate the maximum column width for a given terminal width."""
-- 04 005 009 008 015 014 023 0087.57 0364.87 0004.17 |
if not wrap:
-- 04 005 009 008 015 014 023 0087.57 0364.87 0004.17 |
return
-- 04 005 009 008 015 014 023 0087.57 0364.87 0004.17 |
width = shutil.get_terminal_size()[0]
-- 04 005 009 008 015 014 023 0087.57 0364.87 0004.17 |
columns = len(headers)
-- 04 005 009 008 015 014 023 0087.57 0364.87 0004.17 |
if width < 80:
-- 04 005 009 008 015 014 023 0087.57 0364.87 0004.17 |
padding = columns + 2
-- 04 005 009 008 015 014 023 0087.57 0364.87 0004.17 |
elif width < 120:
-- 04 005 009 008 015 014 023 0087.57 0364.87 0004.17 |
padding = columns - 2
-- 04 005 009 008 015 014 023 0087.57 0364.87 0004.17 |
else:
-- 04 005 009 008 015 014 023 0087.57 0364.87 0004.17 |
padding = columns - 4
-- 04 005 009 008 015 014 023 0087.57 0364.87 0004.17 |
maxcolwidth = (width // columns) - padding
-- 04 005 009 008 015 014 023 0087.57 0364.87 0004.17 |
return max(maxcolwidth, 1)
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- 04 004 008 004 008 012 012 0043.02 0086.04 0002.00 |
def get_style(style: str = DEFAULT_GRID_STYLE) -> str:
-- 04 004 008 004 008 012 012 0043.02 0086.04 0002.00 |
"""Select the tablefmt style for tabulate according to what sys.stdout can handle."""
-- 04 004 008 004 008 012 012 0043.02 0086.04 0002.00 |
if style == DEFAULT_GRID_STYLE:
-- 04 004 008 004 008 012 012 0043.02 0086.04 0002.00 |
encoding = sys.stdout.encoding
-- 04 004 008 004 008 012 012 0043.02 0086.04 0002.00 |
# StringIO has encoding=None, but it handles utf-8 fine.
-- 04 004 008 004 008 012 012 0043.02 0086.04 0002.00 |
if encoding is not None and encoding.lower() not in ("utf-8", "utf8"):
-- 04 004 008 004 008 012 012 0043.02 0086.04 0002.00 |
style = "grid"
-- 04 004 008 004 008 012 012 0043.02 0086.04 0002.00 |
return style
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
-- -- --- --- --- --- --- --- ------- ------- ------- |
@lru_cache(maxsize=128)
-- 01 001 004 002 004 005 006 0013.93 0006.97 0000.50 |
def generate_cache_path(path: Union[pathlib.Path, str]) -> str:
-- 01 001 004 002 004 005 006 0013.93 0006.97 0000.50 |
"""
-- 01 001 004 002 004 005 006 0013.93 0006.97 0000.50 |
Generate a reusable path to cache results.
-- 01 001 004 002 004 005 006 0013.93 0006.97 0000.50 |
-- 01 001 004 002 004 005 006 0013.93 0006.97 0000.50 |
Will use the --path of the target and hash into
-- 01 001 004 002 004 005 006 0013.93 0006.97 0000.50 |
a 9-character directory within the HOME folder.
-- 01 001 004 002 004 005 006 0013.93 0006.97 0000.50 |
-- 01 001 004 002 004 005 006 0013.93 0006.97 0000.50 |
:return: The cache path
-- 01 001 004 002 004 005 006 0013.93 0006.97 0000.50 |
"""
-- 01 001 004 002 004 005 006 0013.93 0006.97 0000.50 |
logger.debug(f"Generating cache for {path}")
-- 01 001 004 002 004 005 006 0013.93 0006.97 0000.50 |
sha = hashlib.sha1(str(path).encode()).hexdigest()[:9]
-- 01 001 004 002 004 005 006 0013.93 0006.97 0000.50 |
HOME = pathlib.Path.home()
-- 01 001 004 002 004 005 006 0013.93 0006.97 0000.50 |
cache_path = str(HOME / ".wily" / sha)
-- 01 001 004 002 004 005 006 0013.93 0006.97 0000.50 |
logger.debug(f"Cache path is {cache_path}")
-- 01 001 004 002 004 005 006 0013.93 0006.97 0000.50 |
return cache_path