Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/olpcgames/util.py
blob: 44d42b5ccaafbc2151972d2ed36b4f97b45d0a8b (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
"""Abstraction layer for working outside the Sugar environment"""
import traceback, cStringIO
import logging
log = logging.getLogger( 'olpcgames.util' )
import os
import os.path


try:
    from sugar.activity.activity import get_bundle_path as _get_bundle_path
    def get_bundle_path( ):
        """Retrieve bundle path from activity with fix for silly registration bug"""
        path = _get_bundle_path()
        if path.endswith( '.activity.activity' ):
            log.warn( '''Found double .activity suffix in bundle path, truncating: %s''', path )
            path = path[:-9]
        return path
except ImportError:
    log.warn( '''Do not appear to be running under Sugar, stubbing-in get_bundle_path''' )
    def get_bundle_path():
        """Retrieve a substitute data-path for non OLPC systems"""
        return os.getcwd()


def get_activity_root( ):
    """Return the activity root for data storage operations

    If the activity is present, returns the activity's root,
    otherwise returns NON_SUGAR_ROOT as the directory.
    """
    import olpcgames
    if olpcgames.ACTIVITY:
        return olpcgames.ACTIVITY.get_activity_root()
    else:
        return os.path.expanduser( NON_SUGAR_ROOT )

def data_path(file_name):
    """Return the full path to a file in the data sub-directory of the bundle"""
    return os.path.join(get_bundle_path(), 'data', file_name)
def tmp_path(file_name):
    """Return the full path to a file in the temporary directory"""
    return os.path.join(get_activity_root(), 'tmp', file_name)

def get_traceback(error):
    """Get formatted traceback from current exception
    
    error -- Exception instance raised
    
    Attempts to produce a 10-level traceback as a string
    that you can log off.  Use like so:
    
    try:
        doSomething()
    except Exception, err:
        log.error( 
            '''Failure during doSomething with X,Y,Z parameters: %s''', 
            util.get_traceback( err ),
        )
    """
    exception = str(error)
    file = cStringIO.StringIO()
    try:
        traceback.print_exc( limit=10, file = file )
        exception = file.getvalue()
    finally:
        file.close()
    return exception