~strainanalyser/strainanalyser/trunk

43 by debianpkg at org
Reorganise tree; add livecoding module to aid debugging
1
#!/usr/bin/python
61 by Malcolm Scott
Add license boilerplate
2
#
3
# Strain Analyser
4
# Copyright (C) 2009-2010 Malcolm Scott <Malcolm.Scott@cl.cam.ac.uk>
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License version 2 as
8
# published by the Free Software Foundation.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License along
16
# with this program; if not, write to the Free Software Foundation, Inc.,
17
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
43 by debianpkg at org
Reorganise tree; add livecoding module to aid debugging
19
20
from optparse import OptionParser
62 by Malcolm Scott
Move gtkcrashhandler into strainanalyser module (commit to keeping a local fork for now...)
21
from strainanalyser import gtkcrashhandler
55 by Malcolm Scott
Add a GTK exception hook to avoid silent errors
22
23
gtkcrashhandler.initialize(app_name="Strain Analyser")
43 by debianpkg at org
Reorganise tree; add livecoding module to aid debugging
24
25
parser = OptionParser()
45 by Malcolm Scott
Override livecoding to have a sane module namespace; destroy the window on cancel by default
26
parser.add_option("-l", "--livecoding",
27
		dest="livecoding", action="store_true", default=False,
28
		help="use livecoding module to aid code editing and debugging")
43 by debianpkg at org
Reorganise tree; add livecoding module to aid debugging
29
30
(options, args) = parser.parse_args()
31
32
if options.livecoding:
45 by Malcolm Scott
Override livecoding to have a sane module namespace; destroy the window on cancel by default
33
	import logging
51 by Malcolm Scott
Add an 'enabled' column to exclude some data files from a dataset
34
	logging.basicConfig(level=logging.DEBUG)
45 by Malcolm Scott
Override livecoding to have a sane module namespace; destroy the window on cancel by default
35
36
	logger = logging.getLogger("strainanalyser")
37
	logger.warning("loading livecoding in strainanalyser/")
38
43 by debianpkg at org
Reorganise tree; add livecoding module to aid debugging
39
	from livecoding import reloader
45 by Malcolm Scott
Override livecoding to have a sane module namespace; destroy the window on cancel by default
40
41
	import sys, os
42
	# don't want to accidentally import strainanalyser directly, rather than through livecoding!
43
	sys.path.remove(os.getcwd())
46 by Malcolm Scott
Demote a debug message
44
	logger.debug("sys.path = %s" % sys.path)
45 by Malcolm Scott
Override livecoding to have a sane module namespace; destroy the window on cancel by default
45
43 by debianpkg at org
Reorganise tree; add livecoding module to aid debugging
46
	cr = reloader.CodeReloader()
45 by Malcolm Scott
Override livecoding to have a sane module namespace; destroy the window on cancel by default
47
48
49
	# livecoding defaults to putting all files' contents in the same module,
50
	# rather than Python's default of one-module-per-file; override this to
51
	# match Python's default to avoid having two sets of import statements
52
53
	class MyReloadableScriptFile(reloader.ReloadableScriptFile):
54
55
		def __init__(self, filePath, namespacePath, implicitLoad=True):
48 by Malcolm Scott
Figure out namespace from scratch, so it still works when reloading scripts
56
			# ignore whatever namespace livecoding has come up with; figure it out ourselves
57
			# (and assume that namespace = path...)
58
			myNamespace = os.path.splitext(filePath)[0].replace(os.sep, ".")
45 by Malcolm Scott
Override livecoding to have a sane module namespace; destroy the window on cancel by default
59
			super(MyReloadableScriptFile, self).__init__(filePath, myNamespace, implicitLoad)
60
61
		def Run(self):
62
			# also it misses out magic globals...
63
			self.scriptGlobals = {"__file__": self.filePath}
64
			# the following code copied from namespace.ScriptFile.Run...
65
			try:
66
				eval(self.codeObject, self.scriptGlobals, self.scriptGlobals)
67
			except (ImportError, AttributeError):
68
				import sys, traceback
69
				self.lastError = traceback.format_exception(*sys.exc_info())
70
				return False
71
			return True
72
73
74
	class MyReloadableScriptDirectory(reloader.ReloadableScriptDirectory):
75
76
		scriptFileClass = MyReloadableScriptFile
77
78
		def SetModuleAttributes(self, scriptFile, namespace, overwritableAttributes=set()):
79
			# the code already sets __file__ but too late, and then complains about the one I added above!
80
			# => allow my one to override the default one
81
			overwritableAttributes.add("__file__")
82
			super(MyReloadableScriptDirectory, self).SetModuleAttributes(scriptFile, namespace, overwritableAttributes)
83
84
85
	cr.scriptDirectoryClass = MyReloadableScriptDirectory
86
43 by debianpkg at org
Reorganise tree; add livecoding module to aid debugging
87
	cr.AddDirectory("strainanalyser", "strainanalyser")
88
89
from strainanalyser.datasetdiff import run
90
run()
91