/[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 739 - (hide annotations)
Fri Apr 25 09:13:48 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: 4167 byte(s)
Rename calls to ProjFile.GetFileName to ProjFile.GetFilename.

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

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26