Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/MAFH2/JournalIntegration.py
blob: aa3fb9e1f1ea6ab586841db34af9526e321b8309 (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
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
try:
    from sugar.datastore import datastore
except:
    print "**Warning**: Not running in sugar env"
    print "\t* Sugar datastore required for custom map functionality"
    print "\t* Trying to load custom map or export will cause crash"
import os
import re
class BadInputException(Exception):pass

FILE_MIME = "application/x-fortune-map"
FM_VERSION = '1' #Must be string for dict


def export_textfile(activity, filename, dungeon_id, filetext=''):
    """
    Exports text to journal (in fortune map format)
    Requires activity instance, file name, dungeon id, and text

    @Returns: a DSObject representing the file in the datastore.
    """
    ds_objects, num_objects = datastore.find({'title':filename,'FortuneMaker_VERSION':FM_VERSION})

    if num_objects == 0:
        # Create a datastore object
        file_dsobject = datastore.create()

    else:
        file_dsobject = ds_objects[0]

    # Store unique id for easy search of journal
    file_dsobject.metadata['FM_UID'] = dungeon_id

    # Write any metadata (here we specifically set the title of the file and
    # specify that this is a plain text file).
    file_dsobject.metadata['title'] = filename
    file_dsobject.metadata['mime_type'] = FILE_MIME
    file_dsobject.metadata['FortuneMaker_VERSION'] = FM_VERSION

    #Write the actual file to the data directory of this activity's root.
    file_path = os.path.join(activity.get_activity_root(), 'instance', filename)
    f = open(file_path, 'w')
    try:
        f.write(filetext)
    finally:
        f.close()

    #Set the file_path in the datastore.
    file_dsobject.set_file_path(file_path)

    datastore.write(file_dsobject)
    file_dsobject.destroy()


def list_fh_files():
    ds_objects, num_objects = datastore.find({'FortuneMaker_VERSION':FM_VERSION})
    file_list = []
    for i in xrange(0, num_objects, 1):
        if ds_objects[i].metadata.has_key('FM_UID'):
            file_list.append( ds_objects[i] )
        else:
            #TODO: Attempt to read uid from file?
            pass
    return file_list

def load_dungeon_by_id(id):
    ds_objects, num_objects = datastore.find({'FortuneMaker_VERSION':FM_VERSION,'FM_UID':id})

    if num_objects == 0:
        return False

    return load_dungeon(ds_objects[0])

def load_dungeon(file_data):
    """
    Gets dungeon data dictionary from journal file object

    Internally opens file from xo journal object and calls do_load

    Throws BadInputException if failed to parse parts of the file
    """
    dgnFile=open(file_data.get_file_path(),'r')
    dungeon_data = do_load( dgnFile )
    dngFile.close()

    return dungeon_data

def do_load( dgnFile ):
    """
    Gets dungeon data dictionary from file stream

    Throws BadInputException if failed to parse parts of the file
    """
    grab = 0
    room_str = []
    for line in dgnFile:
        if grab == 0:
            name = line.strip()
            grab = 1

        elif grab == 1:
            d_id = line.strip()
            grab = 2

        elif grab == 2:
            match = re.match('(\d+)x(\d+)',line)
            if match:
                x = int(match.group(1))
                y = int(match.group(2))
                grab = 3
            else:
                raise BadInputException()

        elif grab == 3:
            theme = int(line)
            grab = 4

        elif grab == 4:
            next = line.strip()
            grab = 5

        elif grab == 5:
            room_str.append(line.strip())

    if grab == 5:
        dungeon_dict = {
            'name': name, 'theme': theme, 'next': next,
            'x': x, 'y': y, 'roomstr': room_str, 'd_id': d_id
        }
        return dungeon_dict

    else:
        raise BadInputException()