6 from collections
import OrderedDict
7 from collections
.abc
import Iterable
8 from contextlib
import contextmanager
13 __all__
= ["flatten", "union" , "log2_int", "bits_for", "memoize", "final", "deprecated",
14 "get_linter_options", "get_linter_option"]
19 if isinstance(e
, Iterable
):
25 def union(i
, start
=None):
47 raise TypeError("Subclassing {}.{} is not supported"
48 .format(cls
.__module
__, cls
.__name
__))
49 cls
.__init
_subclass
__ = init_subclass
53 def deprecated(message
, stacklevel
=2):
56 def wrapper(*args
, **kwargs
):
57 warnings
.warn(message
, DeprecationWarning, stacklevel
=stacklevel
)
58 return f(*args
, **kwargs
)
63 def _ignore_deprecated(f
=None):
65 @contextlib.contextmanager
67 with warnings
.catch_warnings():
68 warnings
.filterwarnings(action
="ignore", category
=DeprecationWarning)
73 def decorator_like(*args
, **kwargs
):
74 with warnings
.catch_warnings():
75 warnings
.filterwarnings(action
="ignore", category
=DeprecationWarning)
82 if isinstance(f
, property):
83 name
= f
.fget
.__name
__
90 def get_linter_options(filename
):
91 first_line
= linecache
.getline(filename
, 1)
93 match
= re
.match(r
"^#\s*nmigen:\s*((?:\w+=\w+\s*)(?:,\s*\w+=\w+\s*)*)\n$", first_line
)
95 return dict(map(lambda s
: s
.strip().split("=", 2), match
.group(1).split(",")))
99 def get_linter_option(filename
, name
, type, default
):
100 options
= get_linter_options(filename
)
101 if name
not in options
:
104 option
= options
[name
]
106 if option
in ("1", "yes", "enable"):
108 if option
in ("0", "no", "disable"):
113 return int(option
, 0)