|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import re |
| 4 | +from dataclasses import dataclass |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import fire |
| 8 | + |
| 9 | +project_dir = Path('.') |
| 10 | + |
| 11 | + |
| 12 | +@dataclass |
| 13 | +class ProblemInfo: |
| 14 | + id: int |
| 15 | + title: str |
| 16 | + |
| 17 | + def title_slug(self): |
| 18 | + title_parts = re.split(r'\s+', self.title) |
| 19 | + return f'{self.id:04d}-' + '-'.join(title_parts) |
| 20 | + |
| 21 | + |
| 22 | +@dataclass |
| 23 | +class Article: |
| 24 | + problem: ProblemInfo |
| 25 | + path: Path |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def create(cls, problem: ProblemInfo, path: Path): |
| 29 | + article = Article(problem, path) |
| 30 | + article._create_dirs() |
| 31 | + article._create_doc() |
| 32 | + return article |
| 33 | + |
| 34 | + def _create_dirs(self): |
| 35 | + for d in ('Animation', 'Article', 'Code'): |
| 36 | + (self.path / d).mkdir() |
| 37 | + |
| 38 | + def _create_doc(self): |
| 39 | + doc_file = self.path / 'Article' / (self.problem.title_slug() + '.md') |
| 40 | + with doc_file.open('w') as f: |
| 41 | + pass |
| 42 | + |
| 43 | + |
| 44 | +def create_article_r(directory: Path, paths) -> None: |
| 45 | + if isinstance(paths, str): |
| 46 | + (directory / paths).mkdir() |
| 47 | + elif isinstance(paths, list): |
| 48 | + for path in paths: |
| 49 | + create_article_r(directory, path) |
| 50 | + |
| 51 | + |
| 52 | +def create_article(problem_id: int, problem_title: str) -> None: |
| 53 | + problem = ProblemInfo(problem_id, problem_title) |
| 54 | + article_dir = project_dir / problem.title_slug() |
| 55 | + |
| 56 | + if article_dir.exists(): |
| 57 | + print(f'创建失败,文件夹 {article_dir} 已存在') |
| 58 | + exit(1) |
| 59 | + article_dir.mkdir() |
| 60 | + |
| 61 | + article = Article.create(problem, article_dir) |
| 62 | + print(f'题解框架创建完毕,位于文件夹 {article_dir}') |
| 63 | + |
| 64 | + |
| 65 | +class Anima: |
| 66 | + """ |
| 67 | + LeetCode Animation Manager |
| 68 | + """ |
| 69 | + |
| 70 | + def new(self, id: str, title: str): |
| 71 | + create_article(id, title) |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + anima = Anima() |
| 76 | + fire.Fire(anima) |
0 commit comments