Added small cli for templating leetcode exercises
This commit is contained in:
41
cli.py
Normal file
41
cli.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
import click
|
||||||
|
import jinja2
|
||||||
|
from os import path
|
||||||
|
|
||||||
|
|
||||||
|
class LeetCode:
|
||||||
|
@staticmethod
|
||||||
|
def filename_from_url(url: str) -> str:
|
||||||
|
num = input("Number: ")
|
||||||
|
name = url.split("/")[4].replace("-", "_")
|
||||||
|
return f"{num}_{name}.py"
|
||||||
|
|
||||||
|
|
||||||
|
@click.group()
|
||||||
|
@click.option("--template-dir", default="./templates")
|
||||||
|
@click.pass_context
|
||||||
|
def cli(ctx: click.Context, template_dir: str):
|
||||||
|
ctx.obj["template"] = jinja2.Environment(
|
||||||
|
loader=jinja2.FileSystemLoader(template_dir)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command()
|
||||||
|
@click.pass_context
|
||||||
|
@click.argument("url")
|
||||||
|
def leetcode(ctx: click.Context, url: str):
|
||||||
|
file_path = path.join("leetcode", LeetCode.filename_from_url(url))
|
||||||
|
if path.exists(file_path):
|
||||||
|
print("File already exists")
|
||||||
|
return 1
|
||||||
|
|
||||||
|
templates: jinja2.Environment = ctx.obj["template"]
|
||||||
|
templ = templates.get_template("leetcode.j2")
|
||||||
|
result = templ.render()
|
||||||
|
|
||||||
|
with open(file_path, mode="w", encoding="utf8") as f:
|
||||||
|
f.write(result)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
cli(obj={})
|
@ -1,2 +1,4 @@
|
|||||||
pytest
|
pytest
|
||||||
black
|
black
|
||||||
|
click
|
||||||
|
jinja2
|
||||||
|
15
templates/leetcode.j2
Normal file
15
templates/leetcode.j2
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def solution():
|
||||||
|
return Solution()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("", [])
|
||||||
|
def test_search(solution: Solution):
|
||||||
|
pass
|
Reference in New Issue
Block a user