Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugargame/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'sugargame/util.py')
-rw-r--r--sugargame/util.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/sugargame/util.py b/sugargame/util.py
new file mode 100644
index 0000000..63685c8
--- /dev/null
+++ b/sugargame/util.py
@@ -0,0 +1,68 @@
+"""Abstraction layer for working outside the Sugar environment"""
+import traceback
+import cStringIO
+import os
+import os.path
+from sugar3.activity.activity import get_bundle_path
+
+NON_SUGAR_ROOT = '~/.sugar/default/olpcgames'
+
+
+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 sugargame
+ if sugargame.ACTIVITY:
+ return sugargame.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),
+ )
+ """
+ if error is None:
+ error = []
+ for (f, l, func, statement) in traceback.extract_stack()[:-2]:
+ if statement:
+ statement = ': %s' % (statement)
+ if func:
+ error.append('%s.%s (%s)%s' % (f, func, l, statement))
+ else:
+ error.append('%s (%s)%s' % (f, l, statement))
+ return "\n".join(error)
+ else:
+ exception = str(error)
+ file_path = cStringIO.StringIO()
+ try:
+ traceback.print_exc(limit=10, file=file_path)
+ exception = file.getvalue()
+ finally:
+ file.close()
+ return exception