Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/generate-profile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Generate SVGs
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: python -m generator.main --demo
run: python -m generator.main generate --demo

- name: Commit and push if changed
run: |
Expand Down
26 changes: 17 additions & 9 deletions generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,29 @@ def generate(args):
demo = getattr(args, "demo", False)

# Load config
root_dir = os.path.join(os.path.dirname(__file__), "..")
if demo:
config_path = os.path.join(os.path.dirname(__file__), "..", "config.example.yml")
config_candidates = [
os.path.join(root_dir, "config.example.yml"),
os.path.join(root_dir, "config.yml"),
]
else:
config_path = os.path.join(os.path.dirname(__file__), "..", "config.yml")
config_candidates = [os.path.join(root_dir, "config.yml")]

try:
with open(config_path, "r") as f:
config = yaml.safe_load(f)
except FileNotFoundError:
config_path = next((path for path in config_candidates if os.path.exists(path)), None)
if config_path is None:
if demo:
logger.error("config.example.yml not found.")
logger.error("Neither config.example.yml nor config.yml was found.")
else:
logger.error("config.yml not found. Copy config.example.yml to config.yml and edit it.")
sys.exit(1)

if demo and config_path.endswith("config.yml"):
logger.info("Demo mode: config.example.yml not found, falling back to config.yml.")

with open(config_path, "r", encoding="utf-8") as f:
config = yaml.safe_load(f)
Comment on lines +41 to +62

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Para melhorar a legibilidade e modernizar o código, você poderia considerar o uso do módulo pathlib para manipulação de caminhos de arquivo. Ele oferece uma API orientada a objetos que é geralmente mais intuitiva do que os.path.

Isso tornaria a definição de root_dir, a construção de caminhos candidatos e as verificações de existência um pouco mais limpas.

Não se esqueça de adicionar from pathlib import Path no início do arquivo.

    root_dir = Path(__file__).parent.parent
    if demo:
        config_candidates = [
            root_dir / "config.example.yml",
            root_dir / "config.yml",
        ]
    else:
        config_candidates = [root_dir / "config.yml"]

    config_path = next((path for path in config_candidates if path.exists()), None)
    if config_path is None:
        if demo:
            logger.error("Neither config.example.yml nor config.yml was found.")
        else:
            logger.error("config.yml not found. Copy config.example.yml to config.yml and edit it.")
        sys.exit(1)

    if demo and config_path.name == "config.yml":
        logger.info("Demo mode: config.example.yml not found, falling back to config.yml.")

    with config_path.open("r", encoding="utf-8") as f:
        config = yaml.safe_load(f)


try:
config = validate_config(config)
except ConfigError as e:
Expand Down Expand Up @@ -121,14 +129,14 @@ def main():
gen_parser.add_argument(
"--demo",
action="store_true",
help="Generate SVGs with demo data (no API calls, uses config.example.yml)",
help="Generate SVGs with demo data (no API calls, uses config.example.yml or config.yml)",
)

# Top-level --demo for backward compatibility (python -m generator.main --demo)
parser.add_argument(
"--demo",
action="store_true",
help="Generate SVGs with demo data (no API calls, uses config.example.yml)",
help="Generate SVGs with demo data (no API calls, uses config.example.yml or config.yml)",
)

args = parser.parse_args()
Expand Down