66import typer
77from pythonanywhere_core .webapp import Webapp
88from snakesay import snakesay
9+ from tabulate import tabulate
910
1011from pythonanywhere .project import Project
1112from pythonanywhere .utils import ensure_domain
1213
1314app = typer .Typer (no_args_is_help = True )
1415
1516
17+ @app .command (name = "list" )
18+ def list_ ():
19+ """List all your webapps"""
20+ webapps = Webapp .list_webapps ()
21+ if not webapps :
22+ typer .echo (snakesay ("No webapps found." ))
23+ return
24+
25+ for webapp in webapps :
26+ typer .echo (webapp ['domain_name' ])
27+
28+
29+ @app .command ()
30+ def get (
31+ domain_name : str = typer .Option (
32+ "your-username.pythonanywhere.com" ,
33+ "-d" ,
34+ "--domain" ,
35+ help = "Domain name, eg www.mydomain.com" ,
36+ )
37+ ):
38+ """Get details for a specific webapp"""
39+ domain_name = ensure_domain (domain_name )
40+ webapp = Webapp (domain_name )
41+ webapp_info = webapp .get ()
42+
43+ table = [
44+ ["Domain" , webapp_info ['domain_name' ]],
45+ ["Python version" , webapp_info .get ('python_version' , 'unknown' )],
46+ ["Source directory" , webapp_info .get ('source_directory' , 'not set' )],
47+ ["Virtualenv path" , webapp_info .get ('virtualenv_path' , 'not set' )],
48+ ["Enabled" , webapp_info .get ('enabled' , 'unknown' )]
49+ ]
50+
51+ typer .echo (tabulate (table , tablefmt = "simple" ))
52+
53+
1654@app .command ()
1755def create (
1856 domain_name : str = typer .Option (
@@ -32,6 +70,7 @@ def create(
3270 help = "*Irrevocably* delete any existing web app config on this domain. Irrevocably." ,
3371 ),
3472):
73+ """Create a new webapp with virtualenv and project setup"""
3574 domain = ensure_domain (domain_name )
3675 project = Project (domain , python_version )
3776 project .sanity_checks (nuke = nuke )
@@ -50,58 +89,21 @@ def create(
5089 )
5190
5291
53- class LogType (str , Enum ):
54- access = "access"
55- error = "error"
56- server = "server"
57- all = "all"
58-
59-
60- def index_callback (value : str ):
61- if value == "all" or (value .isnumeric () and int (value ) in range (10 )):
62- return value
63- raise typer .BadParameter (
64- "log_index has to be 0 for current log, 1-9 for one of archive logs or all for all of them"
65- )
66-
67-
6892@app .command ()
69- def delete_logs (
93+ def reload (
7094 domain_name : str = typer .Option (
7195 "your-username.pythonanywhere.com" ,
7296 "-d" ,
7397 "--domain" ,
7498 help = "Domain name, eg www.mydomain.com" ,
75- ),
76- log_type : LogType = typer .Option (
77- LogType .all ,
78- "-t" ,
79- "--log-type" ,
80- ),
81- log_index : str = typer .Option (
82- "all" ,
83- "-i" ,
84- "--log-index" ,
85- callback = index_callback ,
86- help = "0 for current log, 1-9 for one of archive logs or all for all of them" ,
87- ),
99+ )
88100):
89- webapp = Webapp (ensure_domain (domain_name ))
90- log_types = ["access" , "error" , "server" ]
91- logs = webapp .get_log_info ()
92- if log_type == "all" and log_index == "all" :
93- for key in log_types :
94- for log in logs [key ]:
95- webapp .delete_log (key , log )
96- elif log_type == "all" :
97- for key in log_types :
98- webapp .delete_log (key , int (log_index ))
99- elif log_index == "all" :
100- for i in logs [log_type ]:
101- webapp .delete_log (log_type , int (i ))
102- else :
103- webapp .delete_log (log_type , int (log_index ))
104- typer .echo (snakesay ("All done!" ))
101+ """Reload a webapp to apply code or configuration changes"""
102+ domain_name = ensure_domain (domain_name )
103+ webapp = Webapp (domain_name )
104+ typer .echo (snakesay (f"Reloading { domain_name } via API" ))
105+ webapp .reload ()
106+ typer .echo (snakesay (f"{ domain_name } has been reloaded" ))
105107
106108
107109@app .command ()
@@ -134,6 +136,7 @@ def install_ssl(
134136 "-- this happens by default, use this option to suppress it." ,
135137 ),
136138):
139+ """Install SSL certificate and private key for a webapp"""
137140 with open (certificate_file , "r" ) as f :
138141 certificate = f .read ()
139142
@@ -157,17 +160,73 @@ def install_ssl(
157160 )
158161
159162
163+ class LogType (str , Enum ):
164+ access = "access"
165+ error = "error"
166+ server = "server"
167+ all = "all"
168+
169+
170+ def index_callback (value : str ):
171+ if value == "all" or (value .isnumeric () and int (value ) in range (10 )):
172+ return value
173+ raise typer .BadParameter (
174+ "log_index has to be 0 for current log, 1-9 for one of archive logs or all for all of them"
175+ )
176+
177+
160178@app .command ()
161- def reload (
179+ def delete_logs (
180+ domain_name : str = typer .Option (
181+ "your-username.pythonanywhere.com" ,
182+ "-d" ,
183+ "--domain" ,
184+ help = "Domain name, eg www.mydomain.com" ,
185+ ),
186+ log_type : LogType = typer .Option (
187+ LogType .all ,
188+ "-t" ,
189+ "--log-type" ,
190+ ),
191+ log_index : str = typer .Option (
192+ "all" ,
193+ "-i" ,
194+ "--log-index" ,
195+ callback = index_callback ,
196+ help = "0 for current log, 1-9 for one of archive logs or all for all of them" ,
197+ ),
198+ ):
199+ """Delete webapp log files (access, error, server logs)"""
200+ webapp = Webapp (ensure_domain (domain_name ))
201+ log_types = ["access" , "error" , "server" ]
202+ logs = webapp .get_log_info ()
203+ if log_type == "all" and log_index == "all" :
204+ for key in log_types :
205+ for log in logs [key ]:
206+ webapp .delete_log (key , log )
207+ elif log_type == "all" :
208+ for key in log_types :
209+ webapp .delete_log (key , int (log_index ))
210+ elif log_index == "all" :
211+ for i in logs [log_type ]:
212+ webapp .delete_log (log_type , int (i ))
213+ else :
214+ webapp .delete_log (log_type , int (log_index ))
215+ typer .echo (snakesay ("All done!" ))
216+
217+
218+ @app .command ()
219+ def delete (
162220 domain_name : str = typer .Option (
163221 "your-username.pythonanywhere.com" ,
164222 "-d" ,
165223 "--domain" ,
166224 help = "Domain name, eg www.mydomain.com" ,
167225 )
168226):
227+ """Delete a webapp"""
169228 domain_name = ensure_domain (domain_name )
170229 webapp = Webapp (domain_name )
171- typer .echo (snakesay (f"Reloading { domain_name } via API" ))
172- webapp .reload ()
173- typer .echo (snakesay (f"{ domain_name } has been reloaded " ))
230+ typer .echo (snakesay (f"Deleting { domain_name } via API" ))
231+ webapp .delete ()
232+ typer .echo (snakesay (f"{ domain_name } has been deleted " ))
0 commit comments