Clean up and improve error reporting.
[matthijs/upstream/mobilegtd.git] / src / main.py
1 # SYMBIAN_UID = 0xA0008CDC
2 # 0xA0008CDC
3 # 0x2001A1A0
4
5 #from logging import traceS60
6 #tr=traceS60.trace() 
7 #tr.go()
8
9 def run():
10     logger = None
11     try:
12         import sys
13         import e32
14         import os
15         if e32.in_emulator():
16             sys.path.append('c:/python/')
17             
18             
19         # Set up logging
20         from log.logging import logger as _logger
21         logger = _logger
22         logger.log_stderr()
23
24        
25         # Read config
26         from config import config
27         import inout
28         config.read_configurations()
29         inout.io.safe_chdir(config.gtd_directory)
30        
31         # Yield to the scheduler
32         e32.ao_yield()
33     
34         from model.projects import Projects
35         from gui.projects_list.project_list_view import ProjectListView
36         from persistence.projects_directory import ProjectsDirectory
37     
38         directory = os.path.join(config.gtd_directory,'@Projects')
39
40         projects = Projects()
41         projects_directory = ProjectsDirectory(projects)
42         projects_directory.add_directory(directory)
43         projects_directory.add_directory(os.path.join(directory,'@Review'))
44         projects_directory.read()
45 #        projects.process()
46         projects_view = ProjectListView(projects)
47         projects_view.run()
48     except Exception, e:
49         import appuifw,traceback,os
50         print e
51         
52         (type, value, tb) = sys.exc_info()
53         lines = []
54
55         # Create error message
56         for line in traceback.format_exception_only(type, value):
57                 lines.append(unicode(line))
58         lines.append("\n")
59
60         # Create stacktrace
61         trace = traceback.extract_tb(tb)
62         trace.reverse()
63
64         basedir = os.path.dirname(__file__) + os.sep
65         for (file, line, func, code) in trace:
66             # Remove the basedir from any filename, since it is not so
67             # interesting but takes up precious space.
68             if file.startswith(basedir):
69                     file = file[len(basedir):]
70             lines.append(u'\n%s:%s\nin %s:\n%s\n' % (file, line, func, code))
71
72         # Create and fill an error dialog
73         t = appuifw.Text()
74         for line in lines:
75             if logger:
76                 logger.log(line,1)
77             t.add(line)
78
79         # Put the focus back at the top
80         t.set_pos(0)
81
82         # Create a lock to wait on
83         lock = e32.Ao_lock()
84
85         # Set up the dialog
86         appuifw.app.menu=[(u'Exit', lock.signal)]
87     
88         appuifw.app.title=u'Error'
89         appuifw.app.body=t
90         appuifw.app.exit_key_handler=lock.signal
91
92         # Wait until the exit option or button is chosen
93         lock.wait()
94
95         # Exit app when script returns
96         appuifw.app.set_exit()
97
98     if logger:
99         logger.close()
100
101 try:
102     run()
103 except:
104     import traceback
105     traceback.print_exc()
106
107 #tr.stop()