/[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 875 - (hide annotations)
Fri May 9 16:31:39 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: 4207 byte(s)
Import _.  (ProjFileSaver.write): write header using projfile.dtd.

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

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26