3 # Simple script to explore the datastore
6 from os
.path
import basename
9 from sugar
.datastore
import datastore
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",
21 parser
.add_option("-t", "--title", action
="store_true", dest
="title",
22 help="only search the title",
25 parser
.add_option("-u", "--uid", action
="store_true", dest
="uid",
26 help="only print the object_id",
29 parser
.add_option("-v", "--debug", action
="store_true", dest
="debug",
30 help="show all metadata and properties",
35 if __name__
== "__main__":
37 parser
= build_option_parser()
38 options
, args
= parser
.parse_args()
40 # Compose the query based on the options provided.
44 query
['title'] = " ".join(args
)
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
:
55 for key
, val
in objects
[i
].metadata
.get_dictionary().iteritems():
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
)
61 print objects
[i
].object_id
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']
68 title
= title
+ " (" + filename
+ ")"
71 print '%-45s %44s' % (title
+ "." + ext
,
72 basename(objects
[i
].file_path
))