added dynamic dns script (for everydns.net)
[xo.git] / jls
blob332cdf71c3dab2d0c8de66617440b9e89ea4f582
1 #!/usr/bin/env python
3 # Simple script to explore the datastore
4 # rupa <rupa@lrrr.us>
6 from os.path import basename
7 import optparse
9 from sugar.datastore import datastore
10 import sugar.mime
12 def build_option_parser():
14 usage = "Usage: %prog [-u|-v] [-a] [-t] SEARCH_STR"
15 parser = optparse.OptionParser(usage=usage)
17 parser.add_option("-a", "--all", action="store_true", dest="all",
18 help="even return items without a file path",
19 default=False)
21 parser.add_option("-t", "--title", action="store_true", dest="title",
22 help="only search the title",
23 default=False)
25 parser.add_option("-u", "--uid", action="store_true", dest="uid",
26 help="only print the object_id",
27 default=False)
29 parser.add_option("-v", "--debug", action="store_true", dest="debug",
30 help="show all metadata and properties",
31 default=False)
33 return parser
35 if __name__ == "__main__":
37 parser = build_option_parser()
38 options, args = parser.parse_args()
40 # Compose the query based on the options provided.
41 query = {}
43 if options.title:
44 query['title'] = " ".join(args)
45 else:
46 query['query'] = " ".join(args)
48 objects, count = datastore.find(query, sorting='-mtime')
50 for i in range(count):
51 if not options.all and not objects[i].file_path:
52 objects[i].destroy()
53 continue
54 if options.debug:
55 for key, val in objects[i].metadata.get_dictionary().iteritems():
56 if key != 'preview':
57 print '%20s -> %s' % (key, val)
58 print '%20s -> %s' % ("id", objects[i].object_id)
59 print '%20s -> %s\n' % ("file_path", objects[i].file_path)
60 elif options.uid:
61 print objects[i].object_id
62 else:
63 title = objects[i].metadata.get_dictionary()['title'].strip()
64 mime_type = objects[i].metadata.get_dictionary()['mime_type']
65 ext = sugar.mime.get_primary_extension(mime_type)
66 filename = objects[i].metadata.get_dictionary()['filename']
67 if filename:
68 title = title + " (" + filename + ")"
69 if not ext:
70 ext = "file"
71 print '%-45s %44s' % (title + "." + ext,
72 basename(objects[i].file_path))
73 objects[i].destroy()