comparison common/utils.py @ 2:06145a6ee387

Fixed resource path dependencies issue that caused PARPG to crash on start. * PARPG should now run without issue (system installation not tested). * Utilized FIFE's VFS module to remove path dependencies from most PARPG modules. * The new parpg.vfs module is a singleton with a single global variable, VFS, which is a reference to the global VFS instance. Although a singleton is not ideal it should be replaced once PARPG's core code is refactored. * The parpg.vfs singleton is initialized in the parpg.applicaiton.PARPGApplication class with the absolute path to the data directory via the parpg.settings module and corresponding configuration file. * A new DataPath entry was added to the default system configuration file template under the [parpg] section to support the new parpg.vfs module. * Updated the parpg-assets subrepo to revision 3 to fix some dialog file format issues (for details see commit message for parpg-assets). * Fixed a few bugs in the parpg.dialogueparsers.YAMLDialogueParser class related to exception handling.
author M. George Hansen <technopolitica@gmail.com>
date Mon, 06 Jun 2011 15:56:14 -1000
parents 7a89ea5404b1
children
comparison
equal deleted inserted replaced
1:4912a6f97c52 2:06145a6ee387
11 # You should have received a copy of the GNU General Public License 11 # You should have received a copy of the GNU General Public License
12 # along with this program. If not, see <http://www.gnu.org/licenses/>. 12 # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 13
14 # Miscellaneous game functions 14 # Miscellaneous game functions
15 15
16 import os, sys, fnmatch 16 import sys
17 import os
18 import fnmatch
19
17 from textwrap import dedent 20 from textwrap import dedent
21 from contextlib import contextmanager
22 from parpg import vfs
18 23
19 def addPaths (*paths): 24 def addPaths (*paths):
20 """Adds a list of paths to sys.path. Paths are expected to use forward 25 """Adds a list of paths to sys.path. Paths are expected to use forward
21 slashes, for example '../../engine/extensions'. Slashes are converted 26 slashes, for example '../../engine/extensions'. Slashes are converted
22 to the OS-specific equivalent. 27 to the OS-specific equivalent.
36 return False 41 return False
37 42
38 def locateFiles(pattern, root=os.curdir): 43 def locateFiles(pattern, root=os.curdir):
39 """Locate all files matching supplied filename pattern in and below 44 """Locate all files matching supplied filename pattern in and below
40 supplied root directory.""" 45 supplied root directory."""
41 for path, _, files in os.walk(os.path.abspath(root)): 46 filepaths = []
42 for filename in fnmatch.filter(files, pattern): 47 filenames = vfs.VFS.listFiles(root)
43 yield os.path.join(path, filename) 48 for filename in fnmatch.filter(filenames, pattern):
49 vfs_file_path = '/'.join([root, filename])
50 filepaths.append(vfs_file_path)
51 dirnames = vfs.VFS.listDirectories(root)
52 for dirname in dirnames:
53 subdir_filepaths = locateFiles(pattern, '/'.join([root, dirname]))
54 filepaths.extend(subdir_filepaths)
55 return filepaths
44 56
45 def dedent_chomp(string): 57 def dedent_chomp(string):
46 """Remove common leading whitespace and chomp each non-blank line.""" 58 """Remove common leading whitespace and chomp each non-blank line."""
47 dedented_string = dedent(string).strip() 59 dedented_string = dedent(string).strip()
48 lines = dedented_string.splitlines() 60 lines = dedented_string.splitlines()
57 else: 69 else:
58 line = ''.join([line, ' ']) 70 line = ''.join([line, ' '])
59 formatted_lines.append(line) 71 formatted_lines.append(line)
60 result = ''.join(formatted_lines) 72 result = ''.join(formatted_lines)
61 return result 73 return result
74
75 @contextmanager
76 def cwd(dirname):
77 cwd = os.getcwd()
78
79 try:
80 os.chdir(dirname)
81 yield
82 finally:
83 os.chdir(cwd)