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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 718 - (hide 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 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 jonathan 718 """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 jonathan 696
29 jonathan 718 handler = ProjFileReader()
30     handler.read(filename)
31     return handler.GetProjFile()
32 jonathan 696
33     def WriteProjFile(pf):
34     saver = ProjFileSaver(pf)
35     saver.write(pf.GetFileName())
36    
37 jonathan 709 def GetProjFiles(dir):
38 jonathan 696 """Read all .proj files in the given directory and
39     return a list of ProjFile objects.
40     """
41    
42     list = []
43     try:
44 jonathan 718 dirlist = os.listdir(dir)
45 jonathan 696 except OSError:
46 jonathan 718 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 jonathan 696
55     return list
56    
57 jonathan 709 def GetSystemProjFiles():
58 jonathan 696 """Return a list of ProjFile objects from files that are
59     supplied by Thuban.
60     """
61 jonathan 709 return GetProjFiles(projdir)
62 jonathan 696
63 jonathan 709 def GetUserProjFiles():
64 jonathan 696 """Return a list of ProjFile objects from files that are user-defined."""
65 jonathan 709 return GetProjFiles(usrdir)
66 jonathan 696
67 jonathan 709 class ProjFileReader(XMLReader):
68 jonathan 696
69 jonathan 709 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 jonathan 696
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 jonathan 709 self.__pf.Add(Projection(self.params, self.name))
88 jonathan 696
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 jonathan 709 return self.__pf
96 jonathan 696
97    
98 jonathan 709 class ProjFileSaver(XMLWriter):
99 jonathan 696
100     def __init__(self, pf):
101 jonathan 709 XMLWriter.__init__(self)
102     self.__pf = pf
103 jonathan 696
104     def write(self, file_or_filename):
105 jonathan 709 XMLWriter.write(self, file_or_filename)
106 jonathan 696
107     self.write_header("projfile", "thuban.dtd")
108 jonathan 709 self.write_projfile(self.__pf)
109 jonathan 696 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 jonathan 709 for param in p.GetAllParameters():
119 jonathan 696 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