Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/globals.py
blob: 5730c3d12556390e490c4a3ba202bc7d09243d6b (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
134
# Copyright 2007 World Wide Workshop Foundation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# If you find this activity useful or end up using parts of it in one of your
# own creations we would love to hear from you at info@WorldWideWorkshop.org !
#

import os
import logging
import tempfile
from sugar.activity import activity
try:
  from hashlib import sha1
except ImportError:
  # Python < 2.5
  from sha import new as sha1

from persistence.jokemachinestate import JokeMachineState


class __globals(object):
  '''All applications have code that affects global state. These are the
  globals for Jokemachine'''
  
  def __init__(self):
    self.__pwd = activity.get_bundle_path()
    self.__root = None
    self.__logo = 'resources/GameLogoCharacter.png'
    self.__laugh = 'resources/laugh_attack.au'
    self.__activity_state = None
    self.__activity = None
    
    # owner
    self.__owner = None
    self.__owner_sha1 = None
    
    self.__temporary_filenames = []
    

  # convert all of these to @Property
  def set_activity_instance(self, activity_instance):
    logging.debug('setting actifity %r' % activity_instance)    
    self.__root = activity_instance.get_activity_root()        
    self.__activity = activity_instance
    
  # TODO -> Should we refresh the GUI for this one ?
  def set_activity_state(self, activity_state):
    self.__activity_state = activity_state
    
  def set_owner(self, owner):
    logging.debug('Owner is %s' % owner.props.nick)
    self.__owner = owner
    self.__owner_sha1 = sha1(owner.props.nick).hexdigest()

  @property
  def pwd(self):
    return self.__pwd
  
  @property
  def root(self):
    return self.__root
      
  @property
  def tmpdir(self):
    '''Temporary directory - currently this exists for the sole purpose of
    having a place to dump sounds and images into so we don't have to keep
    them in memory - don't know if this will still be valid under bitfrost,
    don't know if sounds and images can be pulled directly out of the journal
    when needing to be (dis)played'''
    logging.debug('Temp dir is %s' % os.path.join(self.__root, 'tmp'))
    return os.path.join(self.__root, 'tmp')
  
  @property
  def logo(self):
    return os.path.join(self.pwd, self.__logo)
  
  @property
  def laugh_uri(self):
    return os.path.join(self.pwd, self.__laugh)
  
  @property
  def JokeMachineState(self):
    if not self.__activity_state:
      # Okay - When app is not run from Journal activity.read_file() is never
      # called, which means we never call Globals.set_activity_state so we
      # create a default state here:
      logging.debug('Globals.JokeMachineState() - creating default data')
      self.__activity_state = JokeMachineState().test_data() # TODO - implement JokeMachineState.new()
    return self.__activity_state
  
  @property
  def JokeMachineActivity(self):
    if not self.__activity:
      # todo log this!!
      logging.debug('no activity set! - use Globals.set_activity_instance to register activity with Global manager')
      return None
    return self.__activity
  
  @property
  def nickname(self):
    return self.__owner.props.nick  
  
  
  # utility functions with global state
  
  def temporary_filename(self):
    (fd, name) = tempfile.mkstemp(prefix='jokemachine_') 
    self.__temporary_filenames.append(name)
    return name

  
  def shutdown(self):
    for filename in self.__temporary_filenames:
      if os.path.exists(filename):
        logging.info('  deleting temporary file: %s' % filename)
        os.remove(filename)


Globals = __globals()