88__license__ = "Python"
99
1010
11+ import argparse
1112import configparser
1213import os
1314import shelve
@@ -45,11 +46,10 @@ def usage_error(msg, *args):
4546
4647def print_keys (item , title ):
4748 keys = item .keys ()
48- keys .sort ()
49- key_len = max ([len (k ) for k in keys ])
49+ key_len = max ([len (k ) for k in sorted (keys )])
5050
5151 print (title + ":" )
52- for key in keys :
52+ for key in sorted ( keys ) :
5353 if item .key_type (key ) == item .DATE :
5454 value = time .strftime (planet .TIMEFMT_ISO , item [key ])
5555 else :
@@ -65,87 +65,122 @@ def fit_str(string, length):
6565
6666
6767if __name__ == "__main__" :
68- cache_file = None
69- want_ids = 0
7068 ids = []
7169
72- command = None
73-
74- for arg in sys .argv [1 :]:
75- if arg == "-h" or arg == "--help" :
76- usage ()
77- elif arg == "-C" or arg == "--channel" :
78- if command is not None :
79- usage_error ("Only one command option may be supplied" )
80- command = "channel"
81- elif arg == "-L" or arg == "--list" :
82- if command is not None :
83- usage_error ("Only one command option may be supplied" )
84- command = "list"
85- elif arg == "-K" or arg == "--keys" :
86- if command is not None :
87- usage_error ("Only one command option may be supplied" )
88- command = "keys"
89- elif arg == "-I" or arg == "--item" :
90- if command is not None :
91- usage_error ("Only one command option may be supplied" )
92- command = "item"
93- want_ids = 1
94- elif arg == "-H" or arg == "--hide" :
95- if command is not None :
96- usage_error ("Only one command option may be supplied" )
97- command = "hide"
98- want_ids = 1
99- elif arg == "-U" or arg == "--unhide" :
100- if command is not None :
101- usage_error ("Only one command option may be supplied" )
102- command = "unhide"
103- want_ids = 1
104- elif arg .startswith ("-" ):
105- usage_error ("Unknown option:" , arg )
106- elif cache_file is None :
107- cache_file = arg
108- elif want_ids :
109- ids .append (arg )
110- else :
111- usage_error ("Unexpected extra argument:" , arg )
112-
113- if cache_file is None :
70+ parser = argparse .ArgumentParser (
71+ description = "Examine and modify information in the Planet cache."
72+ )
73+ parser .add_argument (
74+ "-C" ,
75+ "--channel" ,
76+ action = "store_const" ,
77+ const = "channel" ,
78+ dest = "command" ,
79+ help = "Display known information on the channel" ,
80+ )
81+ parser .add_argument (
82+ "-L" ,
83+ "--list" ,
84+ action = "store_const" ,
85+ const = "list" ,
86+ dest = "command" ,
87+ help = "List items in the channel" ,
88+ )
89+ parser .add_argument (
90+ "-K" ,
91+ "--keys" ,
92+ action = "store_const" ,
93+ const = "keys" ,
94+ dest = "command" ,
95+ help = "List all keys found in channel items" ,
96+ )
97+ parser .add_argument (
98+ "-I" ,
99+ "--item" ,
100+ action = "store_const" ,
101+ const = "item" ,
102+ dest = "command" ,
103+ help = "Display known information about the item(s)" ,
104+ )
105+ parser .add_argument (
106+ "-H" ,
107+ "--hide" ,
108+ action = "store_const" ,
109+ const = "hide" ,
110+ dest = "command" ,
111+ help = "Mark the item(s) as hidden" ,
112+ )
113+ parser .add_argument (
114+ "-U" ,
115+ "--unhide" ,
116+ action = "store_const" ,
117+ const = "unhide" ,
118+ dest = "command" ,
119+ help = "Mark the item(s) as not hidden" ,
120+ )
121+ parser .add_argument ("cache_file" , help = "Cache file to operate on" )
122+ parser .add_argument (
123+ "item_ids" ,
124+ nargs = "*" ,
125+ help = "Item IDs to operate on when using item-related commands" ,
126+ )
127+
128+ args = parser .parse_args ()
129+
130+ # Check if more than one command option was supplied
131+ if "command" not in args or args .command is None :
132+ usage_error ("One command option must be supplied." )
133+ elif (
134+ len (
135+ {
136+ key
137+ for key , value in vars (args ).items ()
138+ if key == "command" and value is not None
139+ }
140+ )
141+ > 1
142+ ):
143+ usage_error ("Only one command option may be supplied" )
144+
145+ # Handle missing cache_file
146+ if not args .cache_file :
114147 usage_error ("Missing expected cache filename" )
115- elif want_ids and not len (ids ):
148+
149+ # Handle commands that require item IDs
150+ if args .command in ["item" , "hide" , "unhide" ] and not args .item_ids :
116151 usage_error ("Missing expected entry ids" )
117152
118153 # Open the cache file directly to get the URL it represents
119154 try :
120- with shelve .open (cache_file , "r" ) as db :
121- url = db [b"url" ].decode ("utf-8" )
122- except shelve .error as e :
123- print (f"{ cache_file } : { e !s} " , file = sys .stderr )
124- sys .exit (1 )
155+ with shelve .open (args .cache_file , "r" ) as db :
156+ url = db ["url" ]
125157 except KeyError :
126- print (f"{ cache_file } : Probably not a cache file" , file = sys .stderr )
158+ print (f"{ args .cache_file } : Probably not a cache file" , file = sys .stderr )
159+ sys .exit (1 )
160+ except Exception as e :
161+ print (f"{ args .cache_file } : { e !s} " , file = sys .stderr )
127162 sys .exit (1 )
128163
129164 # Now do it the right way :-)
130165 my_planet = planet .Planet (configparser .ConfigParser ())
131- my_planet .cache_directory = os .path .dirname (cache_file )
166+ my_planet .cache_directory = os .path .dirname (args . cache_file )
132167 channel = planet .Channel (my_planet , url )
133168
134- for item_id in ids :
169+ for item_id in args . item_ids :
135170 if not channel .has_item (item_id ):
136171 print (item_id + ": Not in channel" , file = sys .stderr )
137172 sys .exit (1 )
138173
139174 # Do the user's bidding
140- if command == "channel" :
175+ if args . command == "channel" :
141176 print_keys (channel , "Channel Keys" )
142177
143- elif command == "item" :
144- for item_id in ids :
178+ elif args . command == "item" :
179+ for item_id in args . item_ids :
145180 item = channel .get_item (item_id )
146181 print_keys (item , "Item Keys for %s" % item_id )
147182
148- elif command == "list" :
183+ elif args . command == "list" :
149184 print ("Items in Channel:" )
150185 for item in channel .items (hidden = True , sorted = True ):
151186 print (" " + item .id )
@@ -155,7 +190,7 @@ def fit_str(string, length):
155190 if hasattr (item , "hidden" ):
156191 print (" (hidden)" )
157192
158- elif command == "keys" :
193+ elif args . command == "keys" :
159194 keys = {}
160195 for item in channel .items ():
161196 for key in item :
@@ -170,8 +205,8 @@ def fit_str(string, length):
170205
171206 print ("Use --item to output values of particular items." )
172207
173- elif command == "hide" :
174- for item_id in ids :
208+ elif args . command == "hide" :
209+ for item_id in args . item_ids :
175210 item = channel .get_item (item_id )
176211 if hasattr (item , "hidden" ):
177212 print (item_id + ": Already hidden." )
@@ -181,8 +216,8 @@ def fit_str(string, length):
181216 channel .cache_write ()
182217 print ("Done." )
183218
184- elif command == "unhide" :
185- for item_id in ids :
219+ elif args . command == "unhide" :
220+ for item_id in args . item_ids :
186221 item = channel .get_item (item_id )
187222 if hasattr (item , "hidden" ):
188223 del item .hidden
0 commit comments