2 # -*- coding: UTF-8 -*-
7 __authors__ = [ "Scott James Remnant <scott@netsplit.com>",
8 "Jeff Waugh <jdub@perkypants.org>" ]
22 print "Usage: planet-cache [options] CACHEFILE [ITEMID]..."
24 print "Examine and modify information in the Planet cache."
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"
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"
36 print "Other Options:"
37 print " -h, --help Display this help message and exit"
40 def usage_error(msg, *args):
41 print >>sys.stderr, msg, " ".join(args)
42 print >>sys.stderr, "Perhaps you need --help ?"
45 def print_keys(item, title):
48 key_len = max([ len(k) for k in keys ])
52 if item.key_type(key) == item.DATE:
53 value = time.strftime(planet.TIMEFMT_ISO, item[key])
55 value = str(item[key])
56 print " %-*s %s" % (key_len, key, fit_str(value, 74 - key_len))
58 def fit_str(string, length):
59 if len(string) <= length:
62 return string[:length-4] + " ..."
65 if __name__ == "__main__":
72 for arg in sys.argv[1:]:
73 if arg == "-h" or arg == "--help":
75 elif arg == "-C" or arg == "--channel":
76 if command is not None:
77 usage_error("Only one command option may be supplied")
79 elif arg == "-L" or arg == "--list":
80 if command is not None:
81 usage_error("Only one command option may be supplied")
83 elif arg == "-K" or arg == "--keys":
84 if command is not None:
85 usage_error("Only one command option may be supplied")
87 elif arg == "-I" or arg == "--item":
88 if command is not None:
89 usage_error("Only one command option may be supplied")
92 elif arg == "-H" or arg == "--hide":
93 if command is not None:
94 usage_error("Only one command option may be supplied")
97 elif arg == "-U" or arg == "--unhide":
98 if command is not None:
99 usage_error("Only one command option may be supplied")
102 elif arg.startswith("-"):
103 usage_error("Unknown option:", arg)
105 if cache_file is None:
110 usage_error("Unexpected extra argument:", arg)
112 if cache_file is None:
113 usage_error("Missing expected cache filename")
114 elif want_ids and not len(ids):
115 usage_error("Missing expected entry ids")
117 # Open the cache file directly to get the URL it represents
119 db = dbhash.open(cache_file)
122 except dbhash.bsddb._db.DBError, e:
123 print >>sys.stderr, cache_file + ":", e.args[1]
126 print >>sys.stderr, cache_file + ": Probably not a cache file"
129 # Now do it the right way :-)
130 my_planet = planet.Planet(ConfigParser.ConfigParser())
131 my_planet.cache_directory = os.path.dirname(cache_file)
132 channel = planet.Channel(my_planet, url)
135 if not channel.has_item(item_id):
136 print >>sys.stderr, item_id + ": Not in channel"
139 # Do the user's bidding
140 if command == "channel":
141 print_keys(channel, "Channel Keys")
143 elif command == "item":
145 item = channel.get_item(item_id)
146 print_keys(item, "Item Keys for %s" % item_id)
148 elif command == "list":
149 print "Items in Channel:"
150 for item in channel.items(hidden=1, sorted=1):
152 print " " + time.strftime(planet.TIMEFMT_ISO, item.date)
153 if hasattr(item, "title"):
154 print " " + fit_str(item.title, 70)
155 if hasattr(item, "hidden"):
158 elif command == "keys":
160 for item in channel.items():
161 for key in item.keys():
167 print "Keys used in Channel:"
172 print "Use --item to output values of particular items."
174 elif command == "hide":
176 item = channel.get_item(item_id)
177 if hasattr(item, "hidden"):
178 print item_id + ": Already hidden."
182 channel.cache_write()
185 elif command == "unhide":
187 item = channel.get_item(item_id)
188 if hasattr(item, "hidden"):
191 print item_id + ": Not hidden."
193 channel.cache_write()