1- #!/usr/bin/env python
2- # -*- coding: UTF-8 -*-
1+ #!/usr/bin/env python3
32"""Planet cache tool.
43
54"""
1211import os
1312import sys
1413import time
15- import dbhash
16- import ConfigParser
14+ import dbm
15+ import configparser
1716
1817import planet
1918
2019
2120def usage ():
22- print "Usage: planet-cache [options] CACHEFILE [ITEMID]..."
23- print
24- print "Examine and modify information in the Planet cache."
25- print
26- print "Channel Commands:"
27- print " -C, --channel Display known information on the channel"
28- print " -L, --list List items in the channel"
29- print " -K, --keys List all keys found in channel items"
30- print
31- print "Item Commands (need ITEMID):"
32- print " -I, --item Display known information about the item(s)"
33- print " -H, --hide Mark the item(s) as hidden"
34- print " -U, --unhide Mark the item(s) as not hidden"
35- print
36- print "Other Options:"
37- print " -h, --help Display this help message and exit"
21+ print ( "Usage: planet-cache [options] CACHEFILE [ITEMID]..." )
22+ print ()
23+ print ( "Examine and modify information in the Planet cache." )
24+ print ()
25+ print ( "Channel Commands:" )
26+ print ( " -C, --channel Display known information on the channel" )
27+ print ( " -L, --list List items in the channel" )
28+ print ( " -K, --keys List all keys found in channel items" )
29+ print ()
30+ print ( "Item Commands (need ITEMID):" )
31+ print ( " -I, --item Display known information about the item(s)" )
32+ print ( " -H, --hide Mark the item(s) as hidden" )
33+ print ( " -U, --unhide Mark the item(s) as not hidden" )
34+ print ()
35+ print ( "Other Options:" )
36+ print ( " -h, --help Display this help message and exit" )
3837 sys .exit (0 )
3938
4039def usage_error (msg , * args ):
41- print >> sys . stderr , msg , " " .join (args )
42- print >> sys . stderr , "Perhaps you need --help ?"
40+ print ( msg , " " .join (args ), file = sys . stderr )
41+ print ( "Perhaps you need --help ?" , file = sys . stderr )
4342 sys .exit (1 )
4443
4544def print_keys (item , title ):
4645 keys = item .keys ()
4746 keys .sort ()
4847 key_len = max ([ len (k ) for k in keys ])
4948
50- print title + ":"
49+ print ( title + ":" )
5150 for key in keys :
5251 if item .key_type (key ) == item .DATE :
5352 value = time .strftime (planet .TIMEFMT_ISO , item [key ])
5453 else :
5554 value = str (item [key ])
56- print " %-*s %s" % (key_len , key , fit_str (value , 74 - key_len ))
55+ print ( " %-*s %s" % (key_len , key , fit_str (value , 74 - key_len ) ))
5756
5857def fit_str (string , length ):
5958 if len (string ) <= length :
@@ -116,24 +115,23 @@ def fit_str(string, length):
116115
117116 # Open the cache file directly to get the URL it represents
118117 try :
119- db = dbhash .open (cache_file )
120- url = db ["url" ]
121- db .close ()
122- except dbhash .bsddb ._db .DBError , e :
123- print >> sys .stderr , cache_file + ":" , e .args [1 ]
118+ with dbm .open (cache_file , 'r' ) as db :
119+ url = db [b"url" ].decode ('utf-8' )
120+ except dbm .error as e :
121+ print (f"{ cache_file } : { str (e )} " , file = sys .stderr )
124122 sys .exit (1 )
125123 except KeyError :
126- print >> sys . stderr , cache_file + " : Probably not a cache file"
124+ print ( f" { cache_file } : Probably not a cache file", file = sys . stderr )
127125 sys .exit (1 )
128126
129127 # Now do it the right way :-)
130- my_planet = planet .Planet (ConfigParser .ConfigParser ())
128+ my_planet = planet .Planet (configparser .ConfigParser ())
131129 my_planet .cache_directory = os .path .dirname (cache_file )
132130 channel = planet .Channel (my_planet , url )
133131
134132 for item_id in ids :
135133 if not channel .has_item (item_id ):
136- print >> sys . stderr , item_id + ": Not in channel"
134+ print ( item_id + ": Not in channel" , file = sys . stderr )
137135 sys .exit (1 )
138136
139137 # Do the user's bidding
@@ -146,49 +144,48 @@ def fit_str(string, length):
146144 print_keys (item , "Item Keys for %s" % item_id )
147145
148146 elif command == "list" :
149- print "Items in Channel:"
147+ print ( "Items in Channel:" )
150148 for item in channel .items (hidden = 1 , sorted = 1 ):
151- print " " + item .id
152- print " " + time .strftime (planet .TIMEFMT_ISO , item .date )
149+ print ( " " + item .id )
150+ print ( " " + time .strftime (planet .TIMEFMT_ISO , item .date ) )
153151 if hasattr (item , "title" ):
154- print " " + fit_str (item .title , 70 )
152+ print ( " " + fit_str (item .title , 70 ) )
155153 if hasattr (item , "hidden" ):
156- print " (hidden)"
154+ print ( " (hidden)" )
157155
158156 elif command == "keys" :
159157 keys = {}
160158 for item in channel .items ():
161159 for key in item .keys ():
162160 keys [key ] = 1
163161
164- keys = keys .keys ()
165- keys .sort ()
162+ keys = sorted (keys .keys ())
166163
167- print "Keys used in Channel:"
164+ print ( "Keys used in Channel:" )
168165 for key in keys :
169- print " " + key
170- print
166+ print ( " " + key )
167+ print ()
171168
172- print "Use --item to output values of particular items."
169+ print ( "Use --item to output values of particular items." )
173170
174171 elif command == "hide" :
175172 for item_id in ids :
176173 item = channel .get_item (item_id )
177174 if hasattr (item , "hidden" ):
178- print item_id + ": Already hidden."
175+ print ( item_id + ": Already hidden." )
179176 else :
180177 item .hidden = "yes"
181178
182179 channel .cache_write ()
183- print "Done."
180+ print ( "Done." )
184181
185182 elif command == "unhide" :
186183 for item_id in ids :
187184 item = channel .get_item (item_id )
188185 if hasattr (item , "hidden" ):
189- del ( item .hidden )
186+ del item .hidden
190187 else :
191- print item_id + ": Not hidden."
188+ print ( item_id + ": Not hidden." )
192189
193190 channel .cache_write ()
194- print "Done."
191+ print ( "Done." )
0 commit comments