Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/globals.py
blob: 5cadaebe0feb94a6b98e74d139b08980b98ddb64 (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
# 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
#


import os
import logging
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.__logo = 'resources/GameLogoCharacter.png'
    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.__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 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'''
    return '/tmp' #os.path.join(self.__pwd, 'tmp')
  
  @property
  def logo(self):
    return os.path.join(self.pwd, self.__logo)
  
  @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):
    # TODO - remember these and delete them all on app exit
    file_name = os.tempnam(self.tmpdir)
    self.__temporary_filenames.append(file_name)
    return file_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()