Skip to content

Commit 2e5702d

Browse files
committed
refactor(cli): standardize string quotes and improve code formatting across CLI components
1 parent 1fab5c7 commit 2e5702d

File tree

7 files changed

+40
-53
lines changed

7 files changed

+40
-53
lines changed

mvc_flask/cli.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,14 @@ def generate():
3131

3232

3333
@generate.command()
34-
@click.argument('name')
34+
@click.argument("name")
3535
@click.option(
36-
'--path', '-p',
36+
"--path",
37+
"-p",
3738
default=CLIConfig.DEFAULT_CONTROLLERS_PATH,
38-
help='Path where to create the controller'
39-
)
40-
@click.option(
41-
'--force', '-f',
42-
is_flag=True,
43-
help='Overwrite existing controller file'
39+
help="Path where to create the controller",
4440
)
41+
@click.option("--force", "-f", is_flag=True, help="Overwrite existing controller file")
4542
@with_appcontext
4643
def controller(name: str, path: str, force: bool) -> None:
4744
"""Generate a new controller.
@@ -64,53 +61,44 @@ def controller(name: str, path: str, force: bool) -> None:
6461
click.echo(
6562
click.style(
6663
f"✓ Controller created successfully at {controller_file}",
67-
fg=CLIConfig.SUCCESS_COLOR
64+
fg=CLIConfig.SUCCESS_COLOR,
6865
)
6966
)
7067

7168
# Provide helpful next steps
72-
controller_name = name if name.endswith('_controller') else f"{name}_controller"
73-
click.echo(
74-
click.style(
75-
"\nNext steps:",
76-
fg=CLIConfig.INFO_COLOR,
77-
bold=True
78-
)
79-
)
69+
controller_name = name if name.endswith("_controller") else f"{name}_controller"
70+
click.echo(click.style("\nNext steps:", fg=CLIConfig.INFO_COLOR, bold=True))
8071
click.echo(
8172
click.style(
8273
"1. Add routes for your controller in your routes.py file",
83-
fg=CLIConfig.INFO_COLOR
74+
fg=CLIConfig.INFO_COLOR,
8475
)
8576
)
8677
click.echo(
8778
click.style(
8879
f"2. Create templates in views/{name}/ directory",
89-
fg=CLIConfig.INFO_COLOR
80+
fg=CLIConfig.INFO_COLOR,
9081
)
9182
)
9283
click.echo(
9384
click.style(
9485
f"3. Implement your business logic in {controller_name}.py",
95-
fg=CLIConfig.INFO_COLOR
86+
fg=CLIConfig.INFO_COLOR,
9687
)
9788
)
9889

9990
except (ControllerGenerationError, InvalidControllerNameError) as e:
10091
logger.error(f"Controller generation failed: {e}")
101-
click.echo(
102-
click.style(f"✗ Error: {e}", fg=CLIConfig.ERROR_COLOR),
103-
err=True
104-
)
92+
click.echo(click.style(f"✗ Error: {e}", fg=CLIConfig.ERROR_COLOR), err=True)
10593
raise click.Abort() from e
10694
except Exception as e:
10795
logger.exception(f"Unexpected error during controller generation: {e}")
10896
click.echo(
10997
click.style(
11098
f"✗ Unexpected error: {e}. Please check the logs for more details.",
111-
fg=CLIConfig.ERROR_COLOR
99+
fg=CLIConfig.ERROR_COLOR,
112100
),
113-
err=True
101+
err=True,
114102
)
115103
raise click.Abort() from e
116104

mvc_flask/core/config.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ class CLIConfig:
1414
DEFAULT_MODELS_PATH = "app/models"
1515

1616
# Template configuration
17-
TEMPLATES_DIR = Path(__file__).parent.parent / 'templates'
17+
TEMPLATES_DIR = Path(__file__).parent.parent / "templates"
1818

1919
# Controller template settings
20-
CONTROLLER_TEMPLATE = 'base_controller.jinja2'
20+
CONTROLLER_TEMPLATE = "base_controller.jinja2"
2121

2222
# File encoding
23-
FILE_ENCODING = 'utf-8'
23+
FILE_ENCODING = "utf-8"
2424

2525
# CLI styling
26-
SUCCESS_COLOR = 'green'
27-
ERROR_COLOR = 'red'
28-
WARNING_COLOR = 'yellow'
29-
INFO_COLOR = 'blue'
26+
SUCCESS_COLOR = "green"
27+
ERROR_COLOR = "red"
28+
WARNING_COLOR = "yellow"
29+
INFO_COLOR = "blue"
3030

3131
# Environment variables for overriding defaults
32-
ENV_PREFIX = 'FLASK_MVC_'
32+
ENV_PREFIX = "FLASK_MVC_"
3333

3434
@classmethod
3535
def get_controllers_path(cls) -> str:
@@ -39,8 +39,7 @@ def get_controllers_path(cls) -> str:
3939
Controllers directory path
4040
"""
4141
return os.getenv(
42-
f'{cls.ENV_PREFIX}CONTROLLERS_PATH',
43-
cls.DEFAULT_CONTROLLERS_PATH
42+
f"{cls.ENV_PREFIX}CONTROLLERS_PATH", cls.DEFAULT_CONTROLLERS_PATH
4443
)
4544

4645
@classmethod
@@ -50,7 +49,7 @@ def get_templates_dir(cls) -> Path:
5049
Returns:
5150
Templates directory path
5251
"""
53-
custom_dir = os.getenv(f'{cls.ENV_PREFIX}TEMPLATES_DIR')
52+
custom_dir = os.getenv(f"{cls.ENV_PREFIX}TEMPLATES_DIR")
5453
if custom_dir:
5554
return Path(custom_dir)
5655
return cls.TEMPLATES_DIR
@@ -62,7 +61,4 @@ def get_file_encoding(cls) -> str:
6261
Returns:
6362
File encoding string
6463
"""
65-
return os.getenv(
66-
f'{cls.ENV_PREFIX}FILE_ENCODING',
67-
cls.FILE_ENCODING
68-
)
64+
return os.getenv(f"{cls.ENV_PREFIX}FILE_ENCODING", cls.FILE_ENCODING)

mvc_flask/core/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@
33

44
class MVCFlaskError(Exception):
55
"""Base exception for MVC Flask CLI errors."""
6+
67
pass
78

89

910
class ControllerGenerationError(MVCFlaskError):
1011
"""Exception raised when controller generation fails."""
12+
1113
pass
1214

1315

1416
class TemplateNotFoundError(MVCFlaskError):
1517
"""Exception raised when template file is not found."""
18+
1619
pass
1720

1821

1922
class InvalidControllerNameError(MVCFlaskError):
2023
"""Exception raised when controller name is invalid."""
24+
2125
pass

mvc_flask/core/file_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def write_file(file_path: Path, content: str) -> None:
4747
ControllerGenerationError: If file writing fails
4848
"""
4949
try:
50-
with open(file_path, 'w', encoding='utf-8') as f:
50+
with open(file_path, "w", encoding="utf-8") as f:
5151
f.write(content)
5252
except Exception as e:
5353
raise ControllerGenerationError(

mvc_flask/core/generators.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ def __init__(self, templates_dir: Optional[Path] = None):
2626
self.name_utils = NameUtils()
2727
self.config = CLIConfig()
2828

29-
def generate(self, name: str, output_path: Optional[str] = None, force: bool = False) -> Path:
29+
def generate(
30+
self, name: str, output_path: Optional[str] = None, force: bool = False
31+
) -> Path:
3032
"""Generate a new controller file.
3133
3234
Args:
@@ -68,8 +70,7 @@ def generate(self, name: str, output_path: Optional[str] = None, force: bool = F
6870

6971
# Render template
7072
content = self.template_renderer.render(
71-
self.config.CONTROLLER_TEMPLATE,
72-
{'class_name': class_name}
73+
self.config.CONTROLLER_TEMPLATE, {"class_name": class_name}
7374
)
7475

7576
# Write file

mvc_flask/core/name_utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def validate_controller_name(name: str) -> None:
2121
if not name:
2222
raise InvalidControllerNameError("Controller name cannot be empty")
2323

24-
if not re.match(r'^[a-zA-Z]\w*$', name):
24+
if not re.match(r"^[a-zA-Z]\w*$", name):
2525
raise InvalidControllerNameError(
2626
"Controller name must start with a letter and contain only "
2727
"letters, numbers, and underscores"
@@ -39,7 +39,7 @@ def normalize_controller_name(name: str) -> str:
3939
"""
4040
NameUtils.validate_controller_name(name)
4141

42-
if not name.endswith('_controller'):
42+
if not name.endswith("_controller"):
4343
return f"{name}_controller"
4444
return name
4545

@@ -54,10 +54,10 @@ def generate_class_name(controller_name: str) -> str:
5454
Properly formatted class name
5555
"""
5656
# Remove _controller suffix if present
57-
base_name = controller_name.replace('_controller', '')
57+
base_name = controller_name.replace("_controller", "")
5858

5959
# Split by underscore and capitalize each word
60-
words = base_name.split('_')
61-
class_name = ''.join(word.capitalize() for word in words)
60+
words = base_name.split("_")
61+
class_name = "".join(word.capitalize() for word in words)
6262

6363
return f"{class_name}Controller"

mvc_flask/core/template_renderer.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ def __init__(self, templates_dir: Path):
1818
"""
1919
self.templates_dir = templates_dir
2020
self._env = Environment(
21-
loader=FileSystemLoader(templates_dir),
22-
trim_blocks=True,
23-
lstrip_blocks=True
21+
loader=FileSystemLoader(templates_dir), trim_blocks=True, lstrip_blocks=True
2422
)
2523

2624
def render(self, template_name: str, context: Dict[str, Any]) -> str:

0 commit comments

Comments
 (0)