Skip to content

Commit

Permalink
Format files
Browse files Browse the repository at this point in the history
  • Loading branch information
katiayn committed Apr 26, 2024
1 parent 0974d66 commit aebec69
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 48 deletions.
62 changes: 17 additions & 45 deletions dockerfile_django/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,29 +127,19 @@ def get_server_info(dependency_file: Path) -> ServerEnum:
dependency_file, "uvicorn", "#"
) and check_for_keyword_in_file(dependency_file, "gunicorn", "#"):
server = ServerEnum.uvicorn
console.print(
f"[INFO] Uvicorn Web Server was found in '{dependency_file}'"
)
console.print(f"[INFO] Uvicorn Web Server was found in '{dependency_file}'")
elif check_for_keyword_in_file(dependency_file, "gunicorn", "#"):
server = ServerEnum.gunicorn
console.print(
f"[INFO] Gunicorn Web Server was found in '{dependency_file}'"
)
console.print(f"[INFO] Gunicorn Web Server was found in '{dependency_file}'")
elif check_for_keyword_in_file(dependency_file, "daphne", "#"):
server = ServerEnum.daphne
console.print(
f"[INFO] Daphne Web Server was found in '{dependency_file}'"
)
console.print(f"[INFO] Daphne Web Server was found in '{dependency_file}'")
elif check_for_keyword_in_file(dependency_file, "hypercorn", "#"):
server = ServerEnum.hypercorn
console.print(
f"[INFO] Hypercorn Web Server was found in '{dependency_file}'"
)
console.print(f"[INFO] Hypercorn Web Server was found in '{dependency_file}'")
elif check_for_keyword_in_file(dependency_file, "granian", "#"):
server = ServerEnum.granian
console.print(
f"[INFO] Granian Web Server was found in '{dependency_file}'"
)
console.print(f"[INFO] Granian Web Server was found in '{dependency_file}'")
else:
server = ServerEnum.gunicorn
console.print(
Expand All @@ -167,16 +157,12 @@ def get_database_info(dependency_file: Path) -> DatabaseEnum:
:param dependency_file:
:return: The database type.
"""
console.print(
f"[INFO] Check for database in '{dependency_file}'"
)
console.print(f"[INFO] Check for database in '{dependency_file}'")
if check_for_keyword_in_file(
dependency_file, "psycopg", "#"
) or check_for_keyword_in_file(dependency_file, "psycopg2", "#"):
database = DatabaseEnum.postgres
console.print(
f"[INFO] Postgres was found in '{dependency_file}'"
)
console.print(f"[INFO] Postgres was found in '{dependency_file}'")
# TODO: Add more database checks (e.g. sqlite, mysql, oracle, etc.)
else:
console.print(
Expand Down Expand Up @@ -266,9 +252,7 @@ def get_settings_config(dir: Path = Path(".")) -> SettingsConfig:
)

# settings_file = settings_files[0]
console.print(
f"[INFO] Extracting config from '{closest_settings}'"
)
console.print(f"[INFO] Extracting config from '{closest_settings}'")

has_collectstatic = check_for_keyword_in_file(closest_settings, "STATIC_ROOT", "#")

Expand All @@ -285,9 +269,7 @@ def get_settings_config(dir: Path = Path(".")) -> SettingsConfig:
random_secret_key = get_random_secret_key(50)
elif not has_random_secret_key:
random_secret_key = get_random_secret_key(50)
console.print(
f"[INFO] A random secret key was generated for the Dockerfile"
)
console.print(f"[INFO] A random secret key was generated for the Dockerfile")
else:
random_secret_key = None

Expand Down Expand Up @@ -463,9 +445,7 @@ def find_settings_files(start_path: Path = Path(".")) -> list[Path]:
if not settings_files:
settings_files, closest_settings = find_files("*settings/*prod*.py", start_path)
if not settings_files:
console.print(
"[ERROR] No 'settings.py' files were found.", style="red"
)
console.print("[ERROR] No 'settings.py' files were found.", style="red")
raise typer.Abort()

for file in settings_files:
Expand Down Expand Up @@ -517,9 +497,7 @@ def render_and_write_template(self, template_name: str, output_name: str) -> Non
if self.diff:
console.print(f"[INFO] Diff {output_name}\n")
if existing_content == generated_lines:
console.print(
f"[INFO] Identical {output_name}\n"
)
console.print(f"[INFO] Identical {output_name}\n")
else:
colorize_diff(output_name, existing_content, generated_lines)
return
Expand All @@ -544,9 +522,7 @@ def render_and_write_template(self, template_name: str, output_name: str) -> Non

with open(output_path, "w") as f:
f.write(generated_content)
console.print(
f"[INFO] Generated '{output_path}' was saved successfully!"
)
console.print(f"[INFO] Generated '{output_path}' was saved successfully!")


def load_config(start_path: Path = Path(".")) -> dict:
Expand All @@ -555,11 +531,11 @@ def load_config(start_path: Path = Path(".")) -> dict:
:param start_path:
:return:
"""
_, config_path = find_files('pyproject.toml', start_path)
_, config_path = find_files("pyproject.toml", start_path)
if config_path:
with config_path.open("r") as file:
config_data = toml.load(file)
return config_data.get('tool', {}).get('dockerfile_django', {})
return config_data.get("tool", {}).get("dockerfile_django", {})
return {}


Expand All @@ -574,7 +550,7 @@ def generate(
dir_okay=True,
writable=False,
readable=True,
resolve_path=True
resolve_path=True,
),
force: bool = typer.Option(
False, "--force", help="Force overwriting the existing Dockerfile"
Expand All @@ -595,15 +571,11 @@ def generate(
"🐳🦄✨ Welcome to Dockerfile Generator for Django projects ✨🦄🐳",
style="magenta",
)
console.print(
f"[INFO] Extracting Django project config info from '{dir}'"
)
console.print(f"[INFO] Extracting Django project config info from '{dir}'")

config = load_config(dir)
if config:
console.print(
f"[INFO] Using the configuration from 'pyproject.toml' file:"
)
console.print(f"[INFO] Using the configuration from 'pyproject.toml' file:")
table = Table(title="Config")
table.add_column("Key", style="green", no_wrap=True)
table.add_column("Value", style="green", no_wrap=True)
Expand Down
4 changes: 3 additions & 1 deletion dockerfile_django/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ def find_files(pattern, start_path: Path = Path(".")) -> tuple[list[Path], Path]
return files, closest_file


def check_for_keyword_in_file(file_path: Path, keyword: str, skip_string_starts_with: str = "") -> bool:
def check_for_keyword_in_file(
file_path: Path, keyword: str, skip_string_starts_with: str = ""
) -> bool:
"""
Check if a keyword is present in a file.
Expand Down
6 changes: 4 additions & 2 deletions tests/test_dockerfile_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def test_generate_not_supported_python():
)
assert result.exit_code == 1
assert (
f"[WARNING] It looks like you are using Python {PYTHON_NOT_SUPPORTED}" in result.stdout
f"[WARNING] It looks like you are using Python {PYTHON_NOT_SUPPORTED}"
in result.stdout
)


Expand All @@ -89,5 +90,6 @@ def test_generate_pinned_supported_python():
)
assert result.exit_code == 1
assert (
f"[WARNING] It looks like you are using Python {PYTHON_PINNED}" in result.stdout
f"[WARNING] It looks like you are using Python {PYTHON_PINNED}"
in result.stdout
)

0 comments on commit aebec69

Please sign in to comment.