Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/bin/copy-to-journal
blob: e675441bdb1d075f3ab289baa6d08ec59433ceb3 (plain)
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
#!/usr/bin/python
#
# Simple script to import a file to the datastore
# Reinier Heeres, <reinier@heeres.eu>, 2007-12-20
#
# Modified by Phil Bordelon <phil@thenexusproject.org> 2007-12-20, 2007-12-21
# to support adding metadata.  Note that the MIME-type is required,
# as otherwise the datastore will not accept the file.

import os
import optparse
from gettext import gettext as _
import dbus

if os.path.exists("/tmp/olpc-session-bus"):
    os.environ["DBUS_SESSION_BUS_ADDRESS"] = "unix:path=/tmp/olpc-session-bus"

from sugar.datastore import datastore
from sugar import mime


def build_option_parser():

    usage = "Usage: %prog <file> [-m MIMETYPE] [-t TITLE] [-d DESC] " \
            "[-T tag1 [-T tag2 ...]]"
    parser = optparse.OptionParser(usage=usage)

    parser.add_option("-t", "--title", action="store", dest="title",
     help="Set the title of the journal entry to TITLE", metavar="TITLE",
     default=None)
    parser.add_option("-d", "--description", action="store",
     dest="description", metavar="DESC",
     help="Set the description of the journal entry to DESC",
     default=None)
    parser.add_option("-m", "--mimetype", action="store",
     dest="mimetype", metavar="MIMETYPE",
     help="Set the file's MIME-type to MIMETYPE",
     default=None)
    parser.add_option("-T", "--tag", action="append", dest="tag_list",
     help="Add tag TAG to the journal entry's tags; " \
             "this option can be repeated",
     metavar="TAG")
    return parser

if __name__ == "__main__":

    option_parser = build_option_parser()
    options, args = option_parser.parse_args()
    if len(args) < 1:
        option_parser.print_help()
        exit(0)

    fname = args[0]
    absname = os.path.abspath(fname)
    if not os.path.exists(absname):
        print 'Error: File does not exist.'
        option_parser.print_help()
        exit(0)

    try:
        entry = datastore.create()
        entry.set_file_path(absname)

        # Set the mimetype to the provided one.
        if options.mimetype is None:
            entry.metadata['mime_type'] = mime.get_for_file(absname)
        else:
            entry.metadata['mime_type'] = options.mimetype

        # If no title is given, use the filename.
        if options.title:
            entry.metadata['title'] = options.title
        else:
            entry.metadata['title'] = os.path.basename(fname)

        # Use the description given, otherwise leave it blank.
        if options.description:
            entry.metadata['description'] = options.description
        else:
            entry.metadata['description'] = _('From: %s') % fname

        # Lastly, if any tags are given, combine them into a single string
        # and save them.
        if options.tag_list:
            tag_string = " ".join(options.tag_list)
            entry.metadata['tags'] = tag_string

        datastore.write(entry)
        print 'Created as %s' % (entry.object_id)

        entry.destroy()

    except dbus.DBusException:
        print 'ERROR: Unable to connect to the datastore.\n'\
              'Check that you are running in the same environment as the '\
              'datastore service.'

    except Exception, e:
        print 'ERROR: %s' % (e)