-
Notifications
You must be signed in to change notification settings - Fork 2
/
planet-cache.py
executable file
·228 lines (194 loc) · 6.32 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python3
"""Planet cache tool."""
__authors__ = [
"Scott James Remnant <[email protected]>",
"Jeff Waugh <[email protected]>",
]
__license__ = "Python"
import argparse
import configparser
import os
import shelve
import sys
import time
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(msg, " ".join(args), file=sys.stderr)
print("Perhaps you need --help ?", file=sys.stderr)
sys.exit(1)
def print_keys(item, title):
keys = item.keys()
key_len = max([len(k) for k in sorted(keys)])
print(title + ":")
for key in sorted(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__":
ids = []
parser = argparse.ArgumentParser(
description="Examine and modify information in the Planet cache."
)
parser.add_argument(
"-C",
"--channel",
action="store_const",
const="channel",
dest="command",
help="Display known information on the channel",
)
parser.add_argument(
"-L",
"--list",
action="store_const",
const="list",
dest="command",
help="List items in the channel",
)
parser.add_argument(
"-K",
"--keys",
action="store_const",
const="keys",
dest="command",
help="List all keys found in channel items",
)
parser.add_argument(
"-I",
"--item",
action="store_const",
const="item",
dest="command",
help="Display known information about the item(s)",
)
parser.add_argument(
"-H",
"--hide",
action="store_const",
const="hide",
dest="command",
help="Mark the item(s) as hidden",
)
parser.add_argument(
"-U",
"--unhide",
action="store_const",
const="unhide",
dest="command",
help="Mark the item(s) as not hidden",
)
parser.add_argument("cache_file", help="Cache file to operate on")
parser.add_argument(
"item_ids",
nargs="*",
help="Item IDs to operate on when using item-related commands",
)
args = parser.parse_args()
# Check if more than one command option was supplied
if "command" not in args or args.command is None:
usage_error("One command option must be supplied.")
elif (
len(
{
key
for key, value in vars(args).items()
if key == "command" and value is not None
}
)
> 1
):
usage_error("Only one command option may be supplied")
# Handle missing cache_file
if not args.cache_file:
usage_error("Missing expected cache filename")
# Handle commands that require item IDs
if args.command in ["item", "hide", "unhide"] and not args.item_ids:
usage_error("Missing expected entry ids")
# Open the cache file directly to get the URL it represents
try:
with shelve.open(args.cache_file, "r") as db:
url = db["url"]
except KeyError:
print(f"{args.cache_file}: Probably not a cache file", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"{args.cache_file}: {e!s}", file=sys.stderr)
sys.exit(1)
# Now do it the right way :-)
my_planet = planet.Planet(configparser.ConfigParser())
my_planet.cache_directory = os.path.dirname(args.cache_file)
channel = planet.Channel(my_planet, url)
for item_id in args.item_ids:
if not channel.has_item(item_id):
print(item_id + ": Not in channel", file=sys.stderr)
sys.exit(1)
# Do the user's bidding
if args.command == "channel":
print_keys(channel, "Channel Keys")
elif args.command == "item":
for item_id in args.item_ids:
item = channel.get_item(item_id)
print_keys(item, "Item Keys for %s" % item_id)
elif args.command == "list":
print("Items in Channel:")
for item in channel.items(hidden=True, sorted=True):
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 args.command == "keys":
keys = {}
for item in channel.items():
for key in item:
keys[key] = 1
keys = sorted(keys.keys())
print("Keys used in Channel:")
for key in keys:
print(" " + key)
print()
print("Use --item to output values of particular items.")
elif args.command == "hide":
for item_id in args.item_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 args.command == "unhide":
for item_id in args.item_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.")