/[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 1149 - (hide annotations)
Wed Jun 11 10:45:34 2003 UTC (21 years, 9 months ago) by frank
Original Path: trunk/thuban/Thuban/Model/resource.py
File MIME type: text/x-python
File size: 4266 byte(s)
Use get_application_dir

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

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26