Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more options to config file (spaces, line length and safety checks) #352

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions gdtoolkit/formatter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
from types import MappingProxyType
from typing import Optional

from types import MappingProxyType
from lark import Tree

from ..parser import parser
from .formatter import format_code # noqa: F401
from .safety_checks import ( # noqa: F401
check_tree_invariant,
check_formatting_stability,
from .safety_checks import LoosenTreeTransformer # noqa: F401
from .safety_checks import (
check_comment_persistence,
LoosenTreeTransformer,
check_formatting_stability,
check_tree_invariant,
)

DEFAULT_CONFIG = MappingProxyType(
{
"excluded_directories": {".git"},
"safety_checks": True,
"use_spaces": 4,
"line_length": 80,
}
)

Expand Down
55 changes: 33 additions & 22 deletions gdtoolkit/formatter/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,32 @@
Examples:
echo 'pass' | gdformat - # reads from STDIN
"""
import sys
import os

import difflib
import logging
import os
import pathlib
import difflib
from typing import List, Tuple, Optional
import sys
from types import MappingProxyType
import pkg_resources
from typing import List, Optional, Tuple

from docopt import docopt
import lark
import pkg_resources
import yaml
from docopt import docopt

from gdtoolkit.formatter import format_code, check_formatting_safety, DEFAULT_CONFIG
from gdtoolkit.common.exceptions import (
lark_unexpected_input_to_str,
lark_unexpected_token_to_str,
)
from gdtoolkit.common.utils import find_gd_files_from_paths
from gdtoolkit.formatter import DEFAULT_CONFIG, check_formatting_safety, format_code
from gdtoolkit.formatter.exceptions import (
TreeInvariantViolation,
FormattingStabilityViolation,
CommentPersistenceViolation,
FormattingStabilityViolation,
TreeInvariantViolation,
)
from gdtoolkit.parser import parser
from gdtoolkit.common.utils import find_gd_files_from_paths
from gdtoolkit.common.exceptions import (
lark_unexpected_token_to_str,
lark_unexpected_input_to_str,
)

CONFIG_FILE_NAME = "gdformatrc"

Expand All @@ -69,14 +70,6 @@ def main():
if arguments["--diff"]:
arguments["--check"] = True

line_length = int(arguments["--line-length"])
spaces_for_indent = (
int(arguments["--use-spaces"])
if arguments["--use-spaces"] is not None
else None
)
safety_checks = not arguments["--fast"]

config_file_path = _find_config_file()
config = _load_config_file_or_default(config_file_path)
_log_config_entries(config)
Expand All @@ -86,6 +79,24 @@ def main():
arguments["<path>"], excluded_directories=set(config["excluded_directories"])
)

line_length = (
int(arguments["--line-length"])
if arguments["--line-length"]
else config.get("line_length", 80)
)

spaces_for_indent = (
int(arguments["--use-spaces"])
if arguments["--use-spaces"]
else config.get("use_spaces", None)
)

safety_checks = (
not arguments["--fast"]
if arguments.get("--fast")
else config.get("safety_checks", True)
)

if files == ["-"]:
_format_stdin(line_length, spaces_for_indent, safety_checks)
elif arguments["--check"]:
Expand Down