Debian Codegraph @

subprojects/meson-docs/mesonbuild/mesonlib.py

raw
1 import typing as T
2
3
4 def listify(item: T.Any, flatten: bool = True) -> T.List[T.Any]:
5 """
6 Returns a list with all args embedded in a list if they are not a list.
7 This function preserves order.
8 @flatten: Convert lists of lists to a flat list
9 """
10 if not isinstance(item, list):
11 return [item]
12 result = [] # type: T.List[T.Any]
13 for i in item:
14 if flatten and isinstance(i, list):
15 result += listify(i, flatten=True)
16 else:
17 result.append(i)
18 return result
19
20
21 class MesonException(Exception):
22 """Exceptions thrown by Meson"""
23
24 def __init__(
25 self,
26 *args: object,
27 file: T.Optional[str] = None,
28 lineno: T.Optional[int] = None,
29 colno: T.Optional[int] = None
30 ):
31 super().__init__(*args)
32 self.file = file
33 self.lineno = lineno
34 self.colno = colno
35
36
37 def relpath(path: str, start: str) -> str:
38 # On Windows a relative path can't be evaluated for paths on two different
39 # drives (i.e. c:\foo and f:\bar). The only thing left to do is to use the
40 # original absolute path.
41 try:
42 return os.path.relpath(path, start)
43 except (TypeError, ValueError):
44 return path