| 1 | import codecs |
| 2 | import importlib |
| 3 | import math |
| 4 | import os |
| 5 | import re |
| 6 | import sys |
| 7 | import warnings |
| 8 | from typing import IO, Any, AnyStr, Callable |
| 9 | |
| 10 | from pyinstrument.vendor.decorator import decorator |
| 11 | |
| 12 | |
| 13 | def object_with_import_path(import_path: str) -> Any: |
| 14 | if "." not in import_path: |
| 15 | raise ValueError("Can't import '%s', it is not a valid import path" % import_path) |
| 16 | module_path, object_name = import_path.rsplit(".", 1) |
| 17 | |
| 18 | module = importlib.import_module(module_path) |
| 19 | return getattr(module, object_name) |
| 20 | |
| 21 | |
| 22 | def truncate(string: str, max_length: int) -> str: |
| 23 | if len(string) > max_length: |
| 24 | return string[0 : max_length - 3] + "..." |
| 25 | return string |
| 26 | |
| 27 | |
| 28 | @decorator |
| 29 | def deprecated(func: Callable[..., Any], *args: Any, **kwargs: Any) -> Any: |
| 30 | """Marks a function as deprecated.""" |
| 31 | warnings.warn( |
| 32 | f"{func} is deprecated and should no longer be used.", |
| 33 | DeprecationWarning, |
| 34 | stacklevel=3, |
| 35 | ) |
| 36 | return func(*args, **kwargs) |
| 37 | |
| 38 | |
| 39 | def deprecated_option(option_name: str, message: str = "") -> Any: |
| 40 | """Marks an option as deprecated.""" |
| 41 | |
| 42 | def caller(func, *args, **kwargs): |
| 43 | if option_name in kwargs: |
| 44 | warnings.warn( |
| 45 | f"{option_name} is deprecated. {message}", |
| 46 | DeprecationWarning, |
| 47 | stacklevel=3, |
| 48 | ) |
| 49 | |
| 50 | return func(*args, **kwargs) |
| 51 | |
| 52 | return decorator(caller) |
| 53 | |
| 54 | |
| 55 | def file_supports_color(file_obj: IO[AnyStr]) -> bool: |
| 56 | """ |
| 57 | Returns True if the running system's terminal supports color. |
| 58 | |
| 59 | Borrowed from Django |
| 60 | https://github.com/django/django/blob/master/django/core/management/color.py |
| 61 | """ |
| 62 | plat = sys.platform |
| 63 | supported_platform = plat != "Pocket PC" and (plat != "win32" or "ANSICON" in os.environ) |
| 64 | |
| 65 | is_a_tty = file_is_a_tty(file_obj) |
| 66 | |
| 67 | return supported_platform and is_a_tty |
| 68 | |
| 69 | |
| 70 | def file_supports_unicode(file_obj: IO[AnyStr]) -> bool: |
| 71 | encoding = getattr(file_obj, "encoding", None) |
| 72 | if not encoding: |
| 73 | return False |
| 74 | |
| 75 | codec_info = codecs.lookup(encoding) |
| 76 | |
| 77 | return "utf" in codec_info.name |
| 78 | |
| 79 | |
| 80 | def file_is_a_tty(file_obj: IO[AnyStr]) -> bool: |
| 81 | return hasattr(file_obj, "isatty") and file_obj.isatty() |
| 82 | |
| 83 | |
| 84 | def unwrap(string: str) -> str: |
| 85 | string = string.replace("\n", " ") |
| 86 | string = re.sub(r"\s+", " ", string) |
| 87 | return string.strip() |
| 88 | |
| 89 | |
| 90 | def format_float_with_sig_figs(value: float, sig_figs: int = 3, trim_zeroes=False) -> str: |
| 91 | """ |
| 92 | Format a float to a string with a specific number of significant figures. |
| 93 | Doesn't use scientific notation. |
| 94 | """ |
| 95 | if value == 0: |
| 96 | return "0" |
| 97 | |
| 98 | precision = math.ceil(-math.log10(abs(value))) + sig_figs - 1 |
| 99 | if precision < 0: |
| 100 | precision = 0 |
| 101 | result = "{:.{precision}f}".format(value, precision=precision) |
| 102 | |
| 103 | if trim_zeroes and "." in result: |
| 104 | result = result.rstrip("0").rstrip(".") |
| 105 | |
| 106 | return result |
| 107 | |
| 108 | |
| 109 | def strtobool(val: str) -> bool: |
| 110 | return val.lower() in {"y", "yes", "t", "true", "on", "1"} |