comparison orpg/dirpath/dirpath_tools.py @ 0:4385a7d0efd1 grumpy-goblin

Deleted and repushed it with the 'grumpy-goblin' branch. I forgot a y
author sirebral
date Tue, 14 Jul 2009 16:41:58 -0500
parents
children 0b8b7e3ed78d
comparison
equal deleted inserted replaced
-1:000000000000 0:4385a7d0efd1
1 import sys
2 import os
3 import errno
4 from orpg.orpg_wx import *
5
6 if WXLOADED:
7 class tmpApp(wx.App):
8 def OnInit(self):
9 return True
10
11
12 #-------------------------------------------------------
13 # void load_paths( dir_struct_reference )
14 # moved structure loading from dirpath.py by Snowdog 3-8-05
15 #-------------------------------------------------------
16 def load_paths(dir_struct, root_dir):
17 dir_struct["home"] = root_dir + os.sep
18 dir_struct["core"] = dir_struct["home"] + "orpg"+ os.sep
19 dir_struct["data"] = dir_struct["home"] + "data"+ os.sep
20 dir_struct["d20"] = dir_struct["data"] + "d20" + os.sep
21 dir_struct["dnd3e"] = dir_struct["data"] + "dnd3e" + os.sep
22 dir_struct["dnd35"] = dir_struct["data"] + "dnd35" + os.sep
23 dir_struct["SWd20"] = dir_struct["data"] + "SWd20" + os.sep
24 dir_struct["icon"] = dir_struct["home"] + "images" + os.sep
25 dir_struct["template"] = dir_struct["core"] + "templates" + os.sep
26
27 dir_struct["plugins"] = dir_struct["home"] + "plugins" + os.sep
28 dir_struct["nodes"] = dir_struct["template"] + "nodes" + os.sep
29 dir_struct["rollers"] = dir_struct["core"] + "dieroller" + os.sep + "rollers" + os.sep
30
31
32 _userbase_dir = _userbase_dir = os.environ['OPENRPG_BASE']
33 _user_dir = _userbase_dir + os.sep + "myfiles" + os.sep
34
35
36 try:
37 os.makedirs(_user_dir)
38 os.makedirs(_user_dir + "runlogs" + os.sep);
39 os.makedirs(_user_dir + "logs" + os.sep);
40 os.makedirs(_user_dir + "webfiles" + os.sep);
41 except OSError, e:
42 if e.errno != errno.EEXIST:
43 raise
44
45 dir_struct["user"] = _user_dir
46
47 dir_struct["logs"] = dir_struct["user"] + "logs" + os.sep
48
49
50
51 #-------------------------------------------------------
52 # int verify_home_path( directory_name )
53 # added by Snowdog 3-8-05
54 # updated with bailout code. Snowdog 7-25-05
55 #-------------------------------------------------------
56 def verify_home_path( path ):
57 """checks for key ORPG files in the openrpg tree
58 and askes for user intervention if their is a problem"""
59
60 try:
61 #verify that the root dir (as supplied) exists
62 if not verify_file(path): return 0
63
64 #These checks require that 'path' have a separator at the end.
65 #Check and temporarily add one if needed
66 if (path[(len(path)-len(os.sep)):] != os.sep):
67 path = path + os.sep
68
69 # These files should always exist at the root orpg dir
70 check_files = ["orpg","data","images"]
71 for n in range(len(check_files)):
72 if not verify_file(path + check_files[n]): return 0
73
74 except:
75 # an error occured while verifying the directory structure
76 # bail out with error signal
77 return 0
78
79 #all files and directories exist.
80 write_approot(path)
81 return 1
82
83
84
85 #-------------------------------------------------------
86 # int verify_file( absolute_path )
87 # added by Snowdog 3-8-05
88 #-------------------------------------------------------
89 def verify_file(abs_path):
90 """Returns True if file or directory exists"""
91 try:
92 os.stat(abs_path)
93 return 1
94 except OSError:
95 #this exception signifies the file or dir doesn't exist
96 return 0
97
98
99 #-------------------------------------------------------
100 # pathname get_user_help()
101 # added by Snowdog 3-8-05
102 # bug fix (SF #1242456) and updated with bailout code. Snowdog 7-25-05
103 #-------------------------------------------------------
104 def get_user_located_root():
105 """Notify the user of directory problems
106 and show directory selection dialog """
107
108 if WXLOADED:
109 app = tmpApp(0)
110 app.MainLoop()
111
112 dir = None
113
114 try:
115 msg = "OpenRPG cannot locate critical files.\nPlease locate the openrpg1 directory in the following window"
116 alert= wx.MessageDialog(None,msg,"Warning",wx.OK|wx.ICON_ERROR)
117 alert.Show()
118 if alert.ShowModal() == wx.OK:
119 alert.Destroy()
120 dlg = wx.DirDialog(None, "Locate the openrpg1 directory:",style=wx.DD_DEFAULT_STYLE)
121 if dlg.ShowModal() == wx.ID_OK:
122 dir = dlg.GetPath()
123 dlg.Destroy()
124 app.Destroy()
125 return dir
126 except Exception, e:
127 print e
128 print "OpenRPG encountered a problem while attempting to load file dialog to locate the OpenRPG root directory."
129 print "please delete the files ./openrpg/orpg/dirpath/aproot.py and ./openrpg/orpg/dirpath/aproot.pyc and try again."
130 else:
131 dir = raw_input("Enter the full path to your openrpg folder: ")
132 return dir
133
134
135
136 #-------------------------------------------------------
137 # void write_approot( orpg_root_path )
138 # added by snowdog 3-10-05
139 #-------------------------------------------------------
140
141 def write_approot( orpg_root_path):
142 try:
143 #if a trailing path separator is on the path string remove it.
144 if (orpg_root_path[(len(orpg_root_path)-len(os.sep)):] == os.sep):
145 orpg_root_path = orpg_root_path[:(len(orpg_root_path)-len(os.sep))]
146 fn = orpg_root_path + os.sep + "orpg" + os.sep + "dirpath" + os.sep + "approot.py"
147 f = open(fn, "w")
148 #trim off the appended os.sep character(s) to avoid duplicating them on re-loading of path
149 code = 'basedir = "' + orpg_root_path + '"' + "\n"
150 #fix path string for windows boxes that lamely use an escape character for a path separator
151 code = str.replace(code,'\\','\\\\')
152 f.write(code)
153 f.close()
154 except IOError:
155 print "[WARNING] Could not create approot file."
156 print "[WARNING] Automatic directory resolution not configured."
157 return