-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
mode.py
70 lines (62 loc) · 2.08 KB
/
mode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
from dataclasses import dataclass, field
from pathlib import Path
from typing import List, Optional
from sqlfmt.dialect import ClickHouse, Polyglot
from sqlfmt.exception import SqlfmtConfigError
@dataclass
class Mode:
"""
A Mode is a container for all sqlfmt config, including formatting config and
report config. For more info on each option, see cli.py
"""
SQL_EXTENSIONS: List[str] = field(default_factory=lambda: [".sql", ".sql.jinja"])
dialect_name: str = "polyglot"
line_length: int = 88
check: bool = False
diff: bool = False
exclude: List[str] = field(default_factory=list)
exclude_root: Optional[Path] = None
encoding: str = "utf-8"
fast: bool = False
single_process: bool = False
no_jinjafmt: bool = False
reset_cache: bool = False
verbose: bool = False
quiet: bool = False
no_progressbar: bool = False
no_color: bool = False
force_color: bool = False
def __post_init__(self) -> None:
# get the dialect from its name.
dialects = {
"polyglot": Polyglot,
"clickhouse": ClickHouse,
}
try:
self.dialect = dialects[self.dialect_name.lower()]()
except KeyError as e:
raise SqlfmtConfigError(
f"Mode was created with dialect_name={self.dialect_name}, "
"which is not supported. Did you mean 'polyglot'?"
) from e
@property
def color(self) -> bool:
"""
There are 4 considerations for setting color:
1. The --force-color option
2. The --no-color option
3. The NO_COLOR environment variable
4. The default behavior, which is to colorize output
This property checks these flags, in descending order of priority,
and sets the authoritative flag accordingly
See no-color.org for details.
"""
if self.force_color:
return True
elif self.no_color:
return False
elif os.environ.get("NO_COLOR", False):
return False
else:
return True