1 |
jonathan |
696 |
# Copyright (c) 2003 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Jonathan Coles <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
import os |
9 |
frank |
1132 |
import os.path |
10 |
|
|
from tempfile import mktemp |
11 |
|
|
|
12 |
jonathan |
696 |
import Thuban |
13 |
jonathan |
875 |
from Thuban import _ |
14 |
|
|
|
15 |
jonathan |
709 |
from Thuban.Model.load import XMLReader |
16 |
|
|
from Thuban.Model.save import XMLWriter |
17 |
jonathan |
696 |
from Thuban.Model.proj import Projection, ProjFile |
18 |
jonathan |
727 |
from xml.sax import SAXParseException |
19 |
jonathan |
696 |
|
20 |
|
|
projdir = \ |
21 |
|
|
os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Projections") |
22 |
|
|
|
23 |
frank |
1132 |
if os.name == 'nt': |
24 |
|
|
# This should result in something like the user directory ... |
25 |
|
|
guess = os.path.dirname(os.path.dirname(os.path.dirname(mktemp()))) |
26 |
|
|
usrdir = os.path.join(guess, ".thuban") |
27 |
|
|
if not os.path.isdir(usrdir): |
28 |
|
|
os.mkdir(usrdir) |
29 |
|
|
else: |
30 |
|
|
usrdir = os.path.expanduser("~/.thuban") |
31 |
jonathan |
696 |
|
32 |
|
|
PROJ_EXT = ".proj" |
33 |
|
|
|
34 |
|
|
def ReadProjFile(filename): |
35 |
jonathan |
718 |
"""Read a single .proj file and return a ProjFile object. |
36 |
|
|
|
37 |
|
|
Raises IOError if the file cannot be opened. |
38 |
|
|
Raises OSError if the file cannot be read. |
39 |
|
|
Raises SAXParseException if the file is not valid XML. |
40 |
|
|
""" |
41 |
jonathan |
696 |
|
42 |
jonathan |
718 |
handler = ProjFileReader() |
43 |
|
|
handler.read(filename) |
44 |
|
|
return handler.GetProjFile() |
45 |
jonathan |
696 |
|
46 |
|
|
def WriteProjFile(pf): |
47 |
jonathan |
727 |
"""Write a single .proj file |
48 |
|
|
|
49 |
|
|
Raises IOError if the file cannot be written. |
50 |
|
|
""" |
51 |
|
|
|
52 |
jonathan |
696 |
saver = ProjFileSaver(pf) |
53 |
jonathan |
739 |
saver.write(pf.GetFilename()) |
54 |
jonathan |
696 |
|
55 |
jonathan |
709 |
def GetProjFiles(dir): |
56 |
jonathan |
696 |
"""Read all .proj files in the given directory and |
57 |
|
|
return a list of ProjFile objects. |
58 |
|
|
""" |
59 |
|
|
|
60 |
|
|
list = [] |
61 |
|
|
try: |
62 |
jonathan |
718 |
dirlist = os.listdir(dir) |
63 |
jonathan |
696 |
except OSError: |
64 |
jonathan |
718 |
pass # if we can't get a directory listing just return [] |
65 |
|
|
else: |
66 |
|
|
for file in filter(lambda s: s.endswith(PROJ_EXT), dirlist): |
67 |
|
|
try: |
68 |
|
|
filename = os.path.join(dir, file) |
69 |
|
|
list.append(ReadProjFile(filename)) |
70 |
|
|
except (OSError, IOError, SAXParseException): |
71 |
|
|
pass # just move onto the next file |
72 |
jonathan |
696 |
|
73 |
|
|
return list |
74 |
|
|
|
75 |
jonathan |
709 |
def GetSystemProjFiles(): |
76 |
jonathan |
696 |
"""Return a list of ProjFile objects from files that are |
77 |
|
|
supplied by Thuban. |
78 |
jonathan |
727 |
|
79 |
|
|
If no files could not be opened return a list with one |
80 |
|
|
empty projection file set to store data in the default file. |
81 |
jonathan |
696 |
""" |
82 |
jonathan |
727 |
filename = os.path.join(projdir, "defaults.proj") |
83 |
|
|
try: |
84 |
|
|
return [ReadProjFile(filename)] |
85 |
|
|
except (OSError, IOError, SAXParseException): |
86 |
|
|
return [ProjFile(filename)] |
87 |
jonathan |
696 |
|
88 |
jonathan |
709 |
def GetUserProjFiles(): |
89 |
jonathan |
727 |
"""Return a list of ProjFile objects from files that are user-defined. |
90 |
jonathan |
696 |
|
91 |
jonathan |
727 |
If no files could not be opened return a list with one |
92 |
|
|
empty projection file set to store data in the default file. |
93 |
|
|
""" |
94 |
|
|
|
95 |
|
|
filename = os.path.join(usrdir, "user.proj") |
96 |
|
|
try: |
97 |
|
|
return [ReadProjFile(filename)] |
98 |
|
|
except (OSError, IOError, SAXParseException): |
99 |
|
|
return [ProjFile(filename)] |
100 |
|
|
|
101 |
jonathan |
709 |
class ProjFileReader(XMLReader): |
102 |
jonathan |
696 |
|
103 |
jonathan |
709 |
def __init__(self): |
104 |
|
|
XMLReader.__init__(self) |
105 |
|
|
self.__pf = ProjFile("") |
106 |
|
|
|
107 |
|
|
XMLReader.AddDispatchers(self, |
108 |
|
|
{'projection': ("start_projection", "end_projection"), |
109 |
|
|
'parameter': ("start_parameter", None)}) |
110 |
|
|
|
111 |
|
|
def read(self, file_or_filename): |
112 |
|
|
XMLReader.read(self, file_or_filename) |
113 |
|
|
|
114 |
jonathan |
739 |
self.__pf.SetFilename(XMLReader.GetFilename(self)) |
115 |
jonathan |
696 |
|
116 |
|
|
def start_projection(self, name, qname, attrs): |
117 |
|
|
self.params = [] |
118 |
jonathan |
875 |
self.name = self.encode(attrs.get((None, 'name'), _("Unknown"))) |
119 |
jonathan |
696 |
|
120 |
|
|
def end_projection(self, name, qname): |
121 |
jonathan |
709 |
self.__pf.Add(Projection(self.params, self.name)) |
122 |
jonathan |
696 |
|
123 |
|
|
def start_parameter(self, name, qname, attrs): |
124 |
|
|
s = attrs.get((None, 'value')) |
125 |
|
|
s = str(s) # we can't handle unicode in proj |
126 |
|
|
self.params.append(s) |
127 |
|
|
|
128 |
|
|
def GetProjFile(self): |
129 |
jonathan |
709 |
return self.__pf |
130 |
jonathan |
696 |
|
131 |
|
|
|
132 |
jonathan |
709 |
class ProjFileSaver(XMLWriter): |
133 |
jonathan |
696 |
|
134 |
|
|
def __init__(self, pf): |
135 |
jonathan |
709 |
XMLWriter.__init__(self) |
136 |
|
|
self.__pf = pf |
137 |
jonathan |
696 |
|
138 |
|
|
def write(self, file_or_filename): |
139 |
jonathan |
709 |
XMLWriter.write(self, file_or_filename) |
140 |
jonathan |
696 |
|
141 |
jonathan |
875 |
self.write_header("projfile", "projfile.dtd") |
142 |
jonathan |
709 |
self.write_projfile(self.__pf) |
143 |
jonathan |
696 |
self.close() |
144 |
|
|
|
145 |
|
|
def write_projfile(self, pf): |
146 |
|
|
|
147 |
|
|
self.open_element("projectionlist") |
148 |
|
|
|
149 |
|
|
for p in pf.GetProjections(): |
150 |
|
|
self.open_element("projection", {"name": p.GetName()}) |
151 |
|
|
|
152 |
jonathan |
709 |
for param in p.GetAllParameters(): |
153 |
jonathan |
696 |
self.write_element("parameter", {"value": param}) |
154 |
|
|
|
155 |
|
|
self.close_element("projection") |
156 |
|
|
|
157 |
|
|
self.close_element("projectionlist") |
158 |
|
|
|
159 |
|
|
|