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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
|
#! /bin/env python
import dataclasses
import os
from textwrap import dedent
from typing import Any
from collections.abc import Generator, Iterable
from isort.main import _build_arg_parser
from isort.settings import _DEFAULT_SETTINGS as config
OUTPUT_FILE = os.path.abspath(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "../docs/configuration/options.md")
)
MD_NEWLINE = " "
HUMAN_NAME = {
"py_version": "Python Version",
"vn": "Version Number",
"str": "String",
"frozenset": "List of Strings",
"tuple": "List of Strings",
}
CONFIG_DEFAULTS = {"False": "false", "True": "true", "None": ""}
DESCRIPTIONS = {}
IGNORED = {"source", "help", "sources", "directory"}
COLUMNS = ["Name", "Type", "Default", "Python / Config file", "CLI", "Description"]
HEADER = """# Configuration options for isort
As a code formatter isort has opinions. However, it also allows you to have your own. If your opinions disagree with those of isort,
isort will disagree but commit to your way of formatting. To enable this, isort exposes a plethora of options to specify
how you want your imports sorted, organized, and formatted.
Too busy to build your perfect isort configuration? For curated common configurations, see isort's [built-in
profiles](https://pycqa.github.io/isort/docs/configuration/profiles.html).
"""
parser = _build_arg_parser()
@dataclasses.dataclass
class Example:
section_complete: str = ""
cfg: str = ""
pyproject_toml: str = ""
cli: str = ""
def __post_init__(self):
if self.cfg or self.pyproject_toml or self.cli:
if self.cfg:
cfg = dedent(self.cfg).lstrip()
self.cfg = (
dedent(
"""
### Example `.isort.cfg`
```
[settings]
{cfg}
```
"""
)
.format(cfg=cfg)
.lstrip()
)
if self.pyproject_toml:
pyproject_toml = dedent(self.pyproject_toml).lstrip()
self.pyproject_toml = (
dedent(
"""
### Example `pyproject.toml`
```
[tool.isort]
{pyproject_toml}
```
"""
)
.format(pyproject_toml=pyproject_toml)
.lstrip()
)
if self.cli:
cli = dedent(self.cli).lstrip()
self.cli = (
dedent(
"""
### Example cli usage
`{cli}`
"""
)
.format(cli=cli)
.lstrip()
)
sections = [s for s in [self.cfg, self.pyproject_toml, self.cli] if s]
sections_str = "\n".join(sections)
self.section_complete = f"""**Examples:**
{sections_str}"""
else:
self.section_complete = ""
def __str__(self):
return self.section_complete
description_mapping: dict[str, str]
description_mapping = {
"length_sort_sections": "Sort the given sections by length",
"forced_separate": "Force certain sub modules to show separately",
"sections": "What sections isort should display imports for and in what order",
"known_other": "known_OTHER is how imports of custom sections are defined. "
"OTHER is a placeholder for the custom section name.",
"comment_prefix": "Allows customizing how isort prefixes comments that it adds or modifies on import lines"
"Generally ` #` (two spaces before a pound symbol) is use, though one space is also common.",
"lines_before_imports": "The number of blank lines to place before imports. -1 for automatic determination",
"lines_after_imports": "The number of blank lines to place after imports. -1 for automatic determination",
"lines_between_sections": "The number of lines to place between sections",
"lines_between_types": "The number of lines to place between direct and from imports",
"lexicographical": "Lexicographical order is strictly alphabetical order. "
"For example by default isort will sort `1, 10, 2` into `1, 2, 10` - but with "
"lexicographical sorting enabled it will remain `1, 10, 2`.",
"ignore_comments": "If enabled, isort will strip comments that exist within import lines.",
"constants": "An override list of tokens to always recognize as a CONSTANT for order_by_type regardless of casing.",
"classes": "An override list of tokens to always recognize as a Class for order_by_type regardless of casing.",
"variables": "An override list of tokens to always recognize as a var for order_by_type regardless of casing.",
"auto_identify_namespace_packages": "Automatically determine local namespace packages, generally by lack of any src files before a src containing directory.",
"namespace_packages": "Manually specify one or more namespace packages.",
"follow_links": "If `True` isort will follow symbolic links when doing recursive sorting.",
"git_ignore": "If `True` isort will honor ignores within locally defined .git_ignore files.",
"formatting_function": "The fully qualified Python path of a function to apply to format code sorted by isort.",
"group_by_package": "If `True` isort will automatically create section groups by the top-level package they come from.",
"indented_import_headings": "If `True` isort will apply import headings to indented imports the same way it does unindented ones.",
"import_headings": "A mapping of import sections to import heading comments that should show above them.",
"import_footers": "A mapping of import sections to import footer comments that should show below them.",
}
example_mapping: dict[str, Example]
example_mapping = {
"skip": Example(
cfg="""
skip=.gitignore,.dockerignore""",
pyproject_toml="""
skip = [".gitignore", ".dockerignore"]
""",
),
"extend_skip": Example(
cfg="""
extend_skip=.md,.json""",
pyproject_toml="""
extend_skip = [".md", ".json"]
""",
),
"skip_glob": Example(
cfg="""
skip_glob=docs/*
""",
pyproject_toml="""
skip_glob = ["docs/*"]
""",
),
"extend_skip_glob": Example(
cfg="""
extend_skip_glob=my_*_module.py,test/*
""",
pyproject_toml="""
extend_skip_glob = ["my_*_module.py", "test/*"]
""",
),
"known_third_party": Example(
cfg="""
known_third_party=my_module1,my_module2
""",
pyproject_toml="""
known_third_party = ["my_module1", "my_module2"]
""",
),
"known_first_party": Example(
cfg="""
known_first_party=my_module1,my_module2
""",
pyproject_toml="""
known_first_party = ["my_module1", "my_module2"]
""",
),
"known_local_folder": Example(
cfg="""
known_local_folder=my_module1,my_module2
""",
pyproject_toml="""
known_local_folder = ["my_module1", "my_module2"]
""",
),
"known_standard_library": Example(
cfg="""
known_standard_library=my_module1,my_module2
""",
pyproject_toml="""
known_standard_library = ["my_module1", "my_module2"]
""",
),
"extra_standard_library": Example(
cfg="""
extra_standard_library=my_module1,my_module2
""",
pyproject_toml="""
extra_standard_library = ["my_module1", "my_module2"]
""",
),
"forced_separate": Example(
cfg="""
forced_separate=glob_exp1,glob_exp2
""",
pyproject_toml="""
forced_separate = ["glob_exp1", "glob_exp2"]
""",
),
"length_sort_sections": Example(
cfg="""
length_sort_sections=future,stdlib
""",
pyproject_toml="""
length_sort_sections = ["future", "stdlib"]
""",
),
"add_imports": Example(
cfg="""
add_imports=import os,import json
""",
pyproject_toml="""
add_imports = ["import os", "import json"]
""",
),
"remove_imports": Example(
cfg="""
remove_imports=os,json
""",
pyproject_toml="""
remove_imports = ["os", "json"]
""",
),
"single_line_exclusions": Example(
cfg="""
single_line_exclusions=os,json
""",
pyproject_toml="""
single_line_exclusions = ["os", "json"]
""",
),
"no_lines_before": Example(
cfg="""
no_lines_before=future,stdlib
""",
pyproject_toml="""
no_lines_before = ["future", "stdlib"]
""",
),
"src_paths": Example(
cfg="""
src_paths = src,tests
""",
pyproject_toml="""
src_paths = ["src", "tests"]
""",
),
"treat_comments_as_code": Example(
cfg="""
treat_comments_as_code = # my comment 1, # my other comment
""",
pyproject_toml="""
treat_comments_as_code = ["# my comment 1", "# my other comment"]
""",
),
"supported_extensions": Example(
cfg="""
supported_extensions=pyw,ext
""",
pyproject_toml="""
supported_extensions = ["pyw", "ext"]
""",
),
"blocked_extensions": Example(
cfg="""
blocked_extensions=pyw,pyc
""",
pyproject_toml="""
blocked_extensions = ["pyw", "pyc"]
""",
),
"known_other": Example(
cfg="""
sections=FUTURE,STDLIB,THIRDPARTY,AIRFLOW,FIRSTPARTY,LOCALFOLDER
known_airflow=airflow""",
pyproject_toml="""
sections = ['FUTURE', 'STDLIB', 'THIRDPARTY', 'AIRFLOW', 'FIRSTPARTY', 'LOCALFOLDER']
known_airflow = ['airflow']""",
),
"multi_line_output": Example(cfg="multi_line_output=3", pyproject_toml="multi_line_output = 3"),
"show_version": Example(cli="isort --version"),
"py_version": Example(
cli="isort --py 39",
pyproject_toml="""
py_version=39
""",
cfg="""
py_version=39
""",
),
}
@dataclasses.dataclass
class ConfigOption:
name: str
type: type = str
default: Any = ""
config_name: str = "**Not Supported**"
cli_options: Iterable[str] = (" **Not Supported**",)
description: str = "**No Description**"
example: Example | None = None
def __str__(self):
if self.name in IGNORED:
return ""
if self.cli_options == (" **Not Supported**",):
cli_options = self.cli_options[0]
else:
cli_options = "\n\n- " + "\n- ".join(self.cli_options)
# new line if example otherwise nothing
example = f"\n{self.example}" if self.example else ""
return f"""
## {human(self.name)}
{self.description}
**Type:** {human(self.type.__name__)}{MD_NEWLINE}
**Default:** `{str(self.default) or " "}`{MD_NEWLINE}
**Config default:** `{config_default(self.default) or " "}`{MD_NEWLINE}
**Python & Config File Name:** {self.config_name}{MD_NEWLINE}
**CLI Flags:**{cli_options}
{example}"""
def config_default(default: Any) -> str:
if isinstance(default, (frozenset, tuple)):
default = list(default)
default_str = str(default)
if default_str in CONFIG_DEFAULTS:
return CONFIG_DEFAULTS[default_str]
if default_str.startswith("py"):
return default_str[2:]
return default_str
def human(name: str) -> str:
if name in HUMAN_NAME:
return HUMAN_NAME[name]
return " ".join(
part if part in ("of",) else part.capitalize() for part in name.replace("-", "_").split("_")
)
def config_options() -> Generator[ConfigOption, None, None]:
cli_actions = {action.dest: action for action in parser._actions}
for name, default in config.items():
extra_kwargs = {}
description: str | None = description_mapping.get(name, None)
cli = cli_actions.pop(name, None)
if cli:
extra_kwargs["cli_options"] = cli.option_strings
if cli.help and not description:
description = cli.help
default_display = default
if isinstance(default, (set, frozenset)) and len(default) > 0:
default_display = tuple(sorted(default))
# todo: refactor place for example params
# needs to integrate with isort/settings/_Config
# needs to integrate with isort/main/_build_arg_parser
yield ConfigOption(
name=name,
type=type(default),
default=default_display,
config_name=name,
description=description or "**No Description**",
example=example_mapping.get(name, None),
**extra_kwargs,
)
for name, cli in cli_actions.items():
extra_kwargs = {}
description: str | None = description_mapping.get(name, None)
if cli.type:
extra_kwargs["type"] = cli.type
elif cli.default is not None:
extra_kwargs["type"] = type(cli.default)
if cli.help and not description:
description = cli.help
yield ConfigOption(
name=name,
default=cli.default,
cli_options=cli.option_strings,
example=example_mapping.get(name, None),
description=description or "**No Description**",
**extra_kwargs,
)
def document_text() -> str:
return f"{HEADER}{''.join(str(config_option) for config_option in config_options())}"
def write_document():
with open(OUTPUT_FILE, "w") as output_file:
output_file.write(document_text())
if __name__ == "__main__":
write_document()
|