From e27e1914f17fb31b9a24364f83b9984ecd4ea231 Mon Sep 17 00:00:00 2001 From: nettee Date: Thu, 16 Apr 2020 22:37:38 +0800 Subject: [PATCH] =?UTF-8?q?=E8=84=9A=E6=9C=AC=E7=94=9F=E6=88=90=E9=A2=98?= =?UTF-8?q?=E8=A7=A3=E7=9B=AE=E5=BD=95=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- anima.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 2 files changed, 77 insertions(+) create mode 100755 anima.py create mode 100644 requirements.txt diff --git a/anima.py b/anima.py new file mode 100755 index 0000000..9dbdf75 --- /dev/null +++ b/anima.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python3 + +import re +from dataclasses import dataclass +from pathlib import Path + +import fire + +project_dir = Path('.') + + +@dataclass +class ProblemInfo: + id: int + title: str + + def title_slug(self): + title_parts = re.split(r'\s+', self.title) + return f'{self.id:04d}-' + '-'.join(title_parts) + + +@dataclass +class Article: + problem: ProblemInfo + path: Path + + @classmethod + def create(cls, problem: ProblemInfo, path: Path): + article = Article(problem, path) + article._create_dirs() + article._create_doc() + return article + + def _create_dirs(self): + for d in ('Animation', 'Article', 'Code'): + (self.path / d).mkdir() + + def _create_doc(self): + doc_file = self.path / 'Article' / (self.problem.title_slug() + '.md') + with doc_file.open('w') as f: + pass + + +def create_article_r(directory: Path, paths) -> None: + if isinstance(paths, str): + (directory / paths).mkdir() + elif isinstance(paths, list): + for path in paths: + create_article_r(directory, path) + + +def create_article(problem_id: int, problem_title: str) -> None: + problem = ProblemInfo(problem_id, problem_title) + article_dir = project_dir / problem.title_slug() + + if article_dir.exists(): + print(f'创建失败,文件夹 {article_dir} 已存在') + exit(1) + article_dir.mkdir() + + article = Article.create(problem, article_dir) + print(f'题解框架创建完毕,位于文件夹 {article_dir}') + + +class Anima: + """ + LeetCode Animation Manager + """ + + def new(self, id: str, title: str): + create_article(id, title) + + +if __name__ == '__main__': + anima = Anima() + fire.Fire(anima) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..69b2165 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +fire \ No newline at end of file