1
+ import json
2
+ import os
3
+ import re
4
+ import shutil
5
+ import urllib .parse
6
+
7
+ import cowsay
8
+ import requests
9
+ import tqdm
10
+ import typer
11
+ from rich .console import Console
12
+ from rich .table import Table
13
+
14
+ app = typer .Typer (
15
+ name = "Kasumi" ,
16
+ no_args_is_help = True
17
+ )
18
+ console = Console ()
19
+
20
+ @app .command (hidden = True , deprecated = True )
21
+ def moo ():
22
+ print (cowsay .get_output_string ('cow' , 'Hello, World!' ) + "\n " )
23
+ table = Table ("[bold dodger_blue2]Surprised?[/bold dodger_blue2]" )
24
+ table .add_row ("This is a temporary addition due to Typer's specifications." )
25
+ table .add_row ("We plan to remove it in the future, so I suggest you enjoy Super Cow Powers while you can :)" )
26
+ table .add_row ("" )
27
+ table .add_row ("Love from the author❤" )
28
+ console .print (table )
29
+
30
+ @app .command ()
31
+ def init (dir : str = os .getcwd (), template : str = None , external_template : str = None ):
32
+ dir = r"C:\\Users\AmaseCocoa\Desktop\\test"
33
+ if template is None :
34
+ while True :
35
+ table = Table ("[bold dodger_blue2]Select Internal Template[/bold dodger_blue2]" )
36
+ table .add_column ("ID" , justify = "right" , style = "cyan" , no_wrap = True )
37
+ table .add_column ("Name" , style = "magenta" )
38
+ table .add_column ("Theme's Description" , justify = "right" , style = "green" )
39
+ table .add_row ("1" , "default" , "The most standard and smallest Kasumi template." )
40
+ table .add_row ("2" , "with_gear" , "Kasumi template using Gear" )
41
+ console .print (table )
42
+ select = input ("select: " )
43
+ if select == "1" :
44
+ template = "default"
45
+ break
46
+ elif select == "2" :
47
+ template = "with_gear"
48
+ break
49
+ else :
50
+ table = Table ("[bold red]Error[/bold red]" )
51
+ table .add_row ("The specified template does not exist." )
52
+ console .print (table )
53
+ if not external_template :
54
+ template_dir = os .path .join (os .path .join (os .path .dirname (__file__ ), "__templates" ), template )
55
+ if os .path .isdir (template_dir ):
56
+ with open (os .path .join (template_dir , "manifest.json" ), "r" ) as f :
57
+ manifest = json .load (f )
58
+ for file in manifest ["files" ]:
59
+ if isinstance (file , dict ):
60
+ shutil .copytree (os .path .join (os .path .join (template_dir , file ["dir" ])), os .path .join (dir , file ["dir" ]))
61
+ else :
62
+ path = os .path .join (template_dir , file )
63
+ shutil .copy (path , dir )
64
+ else :
65
+ table = Table ("[bold red]Error[/bold red]" )
66
+ table .add_row ("The specified template does not exist." )
67
+ console .print (table )
68
+ raise
69
+ else :
70
+ pattern = r"https?://[\w/:%#\$&\?\(\)~\.=\+\-]+"
71
+ if re .match (pattern , external_template ):
72
+ if not external_template .endswith ("manifest.json" ):
73
+ manifest = urllib .parse .urljoin (external_template , "manifest.json" )
74
+ else :
75
+ raise ValueError
76
+ manifest = requests .get (external_template )
77
+ manifest_jsonized = manifest .json ()
78
+ for file in manifest_jsonized ["files" ]:
79
+ with open (os .path .join (dir , file ), 'wb' ) as fp :
80
+ url = urllib .parse .urljoin (external_template , file )
81
+ with requests .get (url , stream = True ) as req :
82
+ req .raise_for_status ()
83
+ total = int (req .headers .get ('content-length' , 0 ))
84
+ with tqdm .tqdm (** {'desc' : url ,'total' : total ,'miniters' : 1 ,'unit' : 'B' ,'unit_scale' : True ,'unit_divisor' : 1024 ,}) as pb :
85
+ for chunk in req .iter_content (chunk_size = 8192 ):
86
+ pb .update (len (chunk ))
87
+ fp .write (chunk )
88
+ else :
89
+ with open (os .path .join (external_template , "manifest.json" ), "r" ) as f :
90
+ manifest = json .load (f )
91
+ for file in manifest ["files" ]:
92
+ if isinstance (file , dict ):
93
+ os .mkdir (os .path .join (dir , file ["dir" ]))
94
+ for filed in file ["files" ]:
95
+ shutil .copy (os .path .join (os .path .join (os .path .join (external_template , file ["dir" ]), filed ), os .path .join (dir , file ["dir" ])))
96
+ else :
97
+ shutil .copy (os .path .join (external_template , file ), dir )
98
+ console .print ("[bold green]Project initialization succeeded![/bold green]" )
0 commit comments