-
Notifications
You must be signed in to change notification settings - Fork 0
/
planet-cache.py
executable file
·194 lines (164 loc) · 5.79 KB
/
planet-cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""Planet cache tool.
"""
__authors__ = [ "Scott James Remnant <[email protected]>",
"Jeff Waugh <[email protected]>" ]
__license__ = "Python"
import os
import sys
import time
import dbhash
import ConfigParser
import planet
def usage():
print "Usage: planet-cache [options] CACHEFILE [ITEMID]..."
print
print "Examine and modify information in the Planet cache."
print
print "Channel Commands:"
print " -C, --channel Display known information on the channel"
print " -L, --list List items in the channel"
print " -K, --keys List all keys found in channel items"
print
print "Item Commands (need ITEMID):"
print " -I, --item Display known information about the item(s)"
print " -H, --hide Mark the item(s) as hidden"
print " -U, --unhide Mark the item(s) as not hidden"
print
print "Other Options:"
print " -h, --help Display this help message and exit"
sys.exit(0)
def usage_error(msg, *args):
print >>sys.stderr, msg, " ".join(args)
print >>sys.stderr, "Perhaps you need --help ?"
sys.exit(1)
def print_keys(item, title):
keys = item.keys()
keys.sort()
key_len = max([ len(k) for k in keys ])
print title + ":"
for key in keys:
if item.key_type(key) == item.DATE:
value = time.strftime(planet.TIMEFMT_ISO, item[key])
else:
value = str(item[key])
print " %-*s %s" % (key_len, key, fit_str(value, 74 - key_len))
def fit_str(string, length):
if len(string) <= length:
return string
else:
return string[:length-4] + " ..."
if __name__ == "__main__":
cache_file = None
want_ids = 0
ids = []
command = None
for arg in sys.argv[1:]:
if arg == "-h" or arg == "--help":
usage()
elif arg == "-C" or arg == "--channel":
if command is not None:
usage_error("Only one command option may be supplied")
command = "channel"
elif arg == "-L" or arg == "--list":
if command is not None:
usage_error("Only one command option may be supplied")
command = "list"
elif arg == "-K" or arg == "--keys":
if command is not None:
usage_error("Only one command option may be supplied")
command = "keys"
elif arg == "-I" or arg == "--item":
if command is not None:
usage_error("Only one command option may be supplied")
command = "item"
want_ids = 1
elif arg == "-H" or arg == "--hide":
if command is not None:
usage_error("Only one command option may be supplied")
command = "hide"
want_ids = 1
elif arg == "-U" or arg == "--unhide":
if command is not None:
usage_error("Only one command option may be supplied")
command = "unhide"
want_ids = 1
elif arg.startswith("-"):
usage_error("Unknown option:", arg)
else:
if cache_file is None:
cache_file = arg
elif want_ids:
ids.append(arg)
else:
usage_error("Unexpected extra argument:", arg)
if cache_file is None:
usage_error("Missing expected cache filename")
elif want_ids and not len(ids):
usage_error("Missing expected entry ids")
# Open the cache file directly to get the URL it represents
try:
db = dbhash.open(cache_file)
url = db["url"]
db.close()
except dbhash.bsddb._db.DBError, e:
print >>sys.stderr, cache_file + ":", e.args[1]
sys.exit(1)
except KeyError:
print >>sys.stderr, cache_file + ": Probably not a cache file"
sys.exit(1)
# Now do it the right way :-)
my_planet = planet.Planet(ConfigParser.ConfigParser())
my_planet.cache_directory = os.path.dirname(cache_file)
channel = planet.Channel(my_planet, url)
for item_id in ids:
if not channel.has_item(item_id):
print >>sys.stderr, item_id + ": Not in channel"
sys.exit(1)
# Do the user's bidding
if command == "channel":
print_keys(channel, "Channel Keys")
elif command == "item":
for item_id in ids:
item = channel.get_item(item_id)
print_keys(item, "Item Keys for %s" % item_id)
elif command == "list":
print "Items in Channel:"
for item in channel.items(hidden=1, sorted=1):
print " " + item.id
print " " + time.strftime(planet.TIMEFMT_ISO, item.date)
if hasattr(item, "title"):
print " " + fit_str(item.title, 70)
if hasattr(item, "hidden"):
print " (hidden)"
elif command == "keys":
keys = {}
for item in channel.items():
for key in item.keys():
keys[key] = 1
keys = keys.keys()
keys.sort()
print "Keys used in Channel:"
for key in keys:
print " " + key
print
print "Use --item to output values of particular items."
elif command == "hide":
for item_id in ids:
item = channel.get_item(item_id)
if hasattr(item, "hidden"):
print item_id + ": Already hidden."
else:
item.hidden = "yes"
channel.cache_write()
print "Done."
elif command == "unhide":
for item_id in ids:
item = channel.get_item(item_id)
if hasattr(item, "hidden"):
del(item.hidden)
else:
print item_id + ": Not hidden."
channel.cache_write()
print "Done."