1- import getpass
21import re
32import sys
3+ from typing import Tuple
44
55from collections import namedtuple
66from pprint import pprint
77
88import typer
99
1010from pythonanywhere .files import PAPath
11+ from pythonanywhere .scripts_commons import get_logger
1112
1213app = typer .Typer ()
1314
1415
15- def standarize_path (path ):
16- return path .replace ("~" , f"/home/{ getpass .getuser ()} " ) if path .startswith ("~" ) else path
16+ def setup (path : str , quiet : bool ) -> Tuple [str , PAPath ]:
17+ logger = get_logger (set_info = True )
18+ if quiet :
19+ logger .disabled = True
20+ return PAPath (path )
1721
1822
1923@app .command ()
2024def get (
21- path : str = typer .Argument (..., help = "Path to PythonAnywhere file or directory" ),
22- only_files : bool = typer .Option (False , "-f" , "--files" , help = "List only files" ),
23- only_dirs : bool = typer .Option (False , "-d" , "--dirs" , help = "List only directories" ),
24- sort_by_type : bool = typer .Option (False , "-t" , "--type" , help = "Sort by type" ),
25+ path : str = typer .Argument (..., help = "Path to PythonAnywhere file or directory" ),
26+ only_files : bool = typer .Option (False , "-f" , "--files" , help = "List only files" ),
27+ only_dirs : bool = typer .Option (False , "-d" , "--dirs" , help = "List only directories" ),
28+ sort_by_type : bool = typer .Option (False , "-t" , "--type" , help = "Sort by type" ),
2529 sort_reverse : bool = typer .Option (False , "-r" , "--reverse" , help = "Sort in reverse order" ),
26- raw : bool = typer .Option (False , "-a" , "--raw" , help = "Print API response (if PATH is file that's the only option)" ),
30+ raw : bool = typer .Option (
31+ False , "-a" , "--raw" , help = "Print API response (if PATH is file that's the only option)"
32+ ),
33+ quiet : bool = typer .Option (False , "-q" , "--quiet" , help = "Disable additional logging" ),
2734):
2835 """
2936 Get contents of PATH.
3037 If PATH points to a directory, show list of it's contents.
3138 If PATH points to a file, print it's contents.
3239 """
33- path = standarize_path (path )
34- contents = PAPath ( path ) .contents
40+ pa_path = setup (path , quiet )
41+ contents = pa_path .contents
3542
3643 if contents is None :
3744 sys .exit (1 )
@@ -47,7 +54,7 @@ def get(
4754 if sort_reverse or sort_by_type :
4855 data .sort (key = lambda x : x .type if sort_by_type else x .name , reverse = sort_reverse )
4956
50- typer .echo (f"{ path } :" )
57+ typer .echo (f"{ pa_path . path } :" )
5158 for name , type_ in data :
5259 if item == "every" :
5360 typer .echo (f"{ type_ [0 ].upper ()} { name } " )
@@ -79,14 +86,19 @@ def _format_tree(data, current):
7986
8087
8188@app .command ()
82- def tree (path : str = typer .Argument (..., help = "Path to PythonAnywhere file or directory" )):
83- path = standarize_path (path )
84- tree = PAPath (path ).tree
89+ def tree (
90+ path : str = typer .Argument (..., help = "Path to PythonAnywhere file or directory" ),
91+ quiet : bool = typer .Option (False , "-q" , "--quiet" , help = "Disable additional logging" )
92+ ):
93+ pa_path = setup (path , quiet )
94+ tree = pa_path .tree
8595
8696 if tree is not None :
87- typer .echo (f"{ path } :" )
97+ typer .echo (f"{ pa_path . path } :" )
8898 typer .echo ("." )
89- typer .echo (_format_tree (tree , path ))
99+ typer .echo (_format_tree (tree , pa_path .path ))
100+ else :
101+ sys .exit (1 )
90102
91103
92104@app .command ()
@@ -104,47 +116,44 @@ def upload(
104116 "--contents" ,
105117 help = "Path to exisitng file or stdin stream that should be uploaded to PATH"
106118 ),
119+ quiet : bool = typer .Option (False , "-q" , "--quiet" , help = "Disable additional logging" )
107120):
108- path = standarize_path (path )
109- pa_path = PAPath (path )
110-
121+ pa_path = setup (path , quiet )
111122 success = pa_path .upload (file )
112-
113123 sys .exit (0 if success else 1 )
114124
115125
116126@app .command ()
117127def delete (
118128 path : str = typer .Argument (..., help = "Path to PythonAnywhere file or directory to be deleted" ),
129+ quiet : bool = typer .Option (False , "-q" , "--quiet" , help = "Disable additional logging" )
119130):
120- path = standarize_path (path )
121- pa_path = PAPath (path )
122-
131+ pa_path = setup (path , quiet )
123132 success = pa_path .delete ()
124-
125133 sys .exit (0 if success else 1 )
126134
127135
128136@app .command ()
129137def share (
130138 path : str = typer .Argument (..., help = "Path to PythonAnywhere file to be shared" ),
131- check : bool = typer .Option (False , "-c" , "--check" , help = "Check sharing status" )
139+ check : bool = typer .Option (False , "-c" , "--check" , help = "Check sharing status" ),
140+ porcelain : bool = typer .Option (False , "-p" , "--porcelain" , help = "Return sharing url in easy-to-parse format" ),
141+ quiet : bool = typer .Option (False , "-q" , "--quiet" , help = "Disable logging" ),
132142):
133- path = standarize_path (path )
134- pa_path = PAPath (path )
135-
143+ pa_path = setup (path , quiet or porcelain )
136144 link = pa_path .get_sharing_url () if check else pa_path .share ()
137145
138146 if not link :
139147 sys .exit (1 )
140- typer .echo (link )
148+ if porcelain :
149+ typer .echo (link )
141150
142151
143152@app .command ()
144- def unshare (path : str = typer .Argument (..., help = "Path to PythonAnywhere file to be unshared" )):
145- path = standarize_path (path )
146- pa_path = PAPath (path )
147-
153+ def unshare (
154+ path : str = typer .Argument (..., help = "Path to PythonAnywhere file to be unshared" ),
155+ quiet : bool = typer .Option (False , "-q" , "--quiet" , help = "Disable additional logging" )
156+ ):
157+ pa_path = setup (path , quiet )
148158 success = pa_path .unshare ()
149-
150159 sys .exit (0 if success else 1 )
0 commit comments