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 |
|
|
import Thuban |
10 |
jonathan |
709 |
from Thuban.Model.load import XMLReader |
11 |
|
|
from Thuban.Model.save import XMLWriter |
12 |
jonathan |
696 |
from Thuban.Model.proj import Projection, ProjFile |
13 |
|
|
|
14 |
|
|
projdir = \ |
15 |
|
|
os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Projections") |
16 |
|
|
|
17 |
|
|
usrdir = os.path.expanduser("~/.thuban") |
18 |
|
|
|
19 |
|
|
PROJ_EXT = ".proj" |
20 |
|
|
|
21 |
|
|
def ReadProjFile(filename): |
22 |
|
|
"""Read a single .proj file and return a ProjFile object.""" |
23 |
|
|
|
24 |
|
|
projs = [] |
25 |
|
|
try: |
26 |
jonathan |
709 |
handler = ProjFileReader() |
27 |
|
|
handler.read(filename) |
28 |
jonathan |
696 |
pf = handler.GetProjFile() |
29 |
|
|
except OSError: |
30 |
|
|
pass |
31 |
|
|
|
32 |
|
|
return pf |
33 |
|
|
|
34 |
|
|
def WriteProjFile(pf): |
35 |
|
|
saver = ProjFileSaver(pf) |
36 |
|
|
saver.write(pf.GetFileName()) |
37 |
|
|
|
38 |
jonathan |
709 |
def GetProjFiles(dir): |
39 |
jonathan |
696 |
"""Read all .proj files in the given directory and |
40 |
|
|
return a list of ProjFile objects. |
41 |
|
|
""" |
42 |
|
|
|
43 |
|
|
list = [] |
44 |
|
|
try: |
45 |
|
|
for file in filter(lambda s: s.endswith(PROJ_EXT), os.listdir(dir)): |
46 |
|
|
filename = os.path.join(dir, file) |
47 |
jonathan |
709 |
list.append(ReadProjFile(filename)) |
48 |
jonathan |
696 |
except OSError: |
49 |
|
|
pass |
50 |
|
|
|
51 |
|
|
return list |
52 |
|
|
|
53 |
jonathan |
709 |
def GetSystemProjFiles(): |
54 |
jonathan |
696 |
"""Return a list of ProjFile objects from files that are |
55 |
|
|
supplied by Thuban. |
56 |
|
|
""" |
57 |
jonathan |
709 |
return GetProjFiles(projdir) |
58 |
jonathan |
696 |
|
59 |
jonathan |
709 |
def GetUserProjFiles(): |
60 |
jonathan |
696 |
"""Return a list of ProjFile objects from files that are user-defined.""" |
61 |
jonathan |
709 |
return GetProjFiles(usrdir) |
62 |
jonathan |
696 |
|
63 |
jonathan |
709 |
class ProjFileReader(XMLReader): |
64 |
jonathan |
696 |
|
65 |
jonathan |
709 |
def __init__(self): |
66 |
|
|
XMLReader.__init__(self) |
67 |
|
|
self.__pf = ProjFile("") |
68 |
|
|
|
69 |
|
|
XMLReader.AddDispatchers(self, |
70 |
|
|
{'projection': ("start_projection", "end_projection"), |
71 |
|
|
'parameter': ("start_parameter", None)}) |
72 |
|
|
|
73 |
|
|
def read(self, file_or_filename): |
74 |
|
|
XMLReader.read(self, file_or_filename) |
75 |
|
|
|
76 |
|
|
self.__pf.SetFileName(XMLReader.GetFileName(self)) |
77 |
jonathan |
696 |
|
78 |
|
|
def start_projection(self, name, qname, attrs): |
79 |
|
|
self.params = [] |
80 |
|
|
self.name = attrs.get((None, 'name'), "Unknown") |
81 |
|
|
|
82 |
|
|
def end_projection(self, name, qname): |
83 |
jonathan |
709 |
self.__pf.Add(Projection(self.params, self.name)) |
84 |
jonathan |
696 |
|
85 |
|
|
def start_parameter(self, name, qname, attrs): |
86 |
|
|
s = attrs.get((None, 'value')) |
87 |
|
|
s = str(s) # we can't handle unicode in proj |
88 |
|
|
self.params.append(s) |
89 |
|
|
|
90 |
|
|
def GetProjFile(self): |
91 |
jonathan |
709 |
return self.__pf |
92 |
jonathan |
696 |
|
93 |
|
|
|
94 |
jonathan |
709 |
class ProjFileSaver(XMLWriter): |
95 |
jonathan |
696 |
|
96 |
|
|
def __init__(self, pf): |
97 |
jonathan |
709 |
XMLWriter.__init__(self) |
98 |
|
|
self.__pf = pf |
99 |
jonathan |
696 |
|
100 |
|
|
def write(self, file_or_filename): |
101 |
jonathan |
709 |
XMLWriter.write(self, file_or_filename) |
102 |
jonathan |
696 |
|
103 |
|
|
self.write_header("projfile", "thuban.dtd") |
104 |
jonathan |
709 |
self.write_projfile(self.__pf) |
105 |
jonathan |
696 |
self.close() |
106 |
|
|
|
107 |
|
|
def write_projfile(self, pf): |
108 |
|
|
|
109 |
|
|
self.open_element("projectionlist") |
110 |
|
|
|
111 |
|
|
for p in pf.GetProjections(): |
112 |
|
|
self.open_element("projection", {"name": p.GetName()}) |
113 |
|
|
|
114 |
jonathan |
709 |
for param in p.GetAllParameters(): |
115 |
jonathan |
696 |
self.write_element("parameter", {"value": param}) |
116 |
|
|
|
117 |
|
|
self.close_element("projection") |
118 |
|
|
|
119 |
|
|
self.close_element("projectionlist") |
120 |
|
|
|
121 |
|
|
|