/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Model/resource.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/Thuban/Model/resource.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 718 - (show annotations)
Wed Apr 23 10:39:46 2003 UTC (21 years, 10 months ago) by jonathan
Original Path: trunk/thuban/Thuban/Model/resource.py
File MIME type: text/x-python
File size: 3472 byte(s)
Fixes RTbug #1813.
(ReadProjFile): Add documentation about which exceptions are raised.
    Always pass the exceptions up to the caller.
(GetProjFiles): If the directory can't be read return an empty list.
    If any of the proj files can't be read skip that file and go
    on to the next one.

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

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26