47 |
pass |
pass |
48 |
|
|
49 |
def read_proj_file(filename): |
def read_proj_file(filename): |
50 |
"""Read a single .proj file and return a ProjFile object. |
"""Read a .proj file and return a ProjFile object and warnings |
51 |
|
|
52 |
Raises IOError if the file cannot be opened. |
The return value is a tuple with the ProjFile object and a list of |
53 |
Raises OSError if the file cannot be read. |
strings with warnings messages that might have been generated by the |
54 |
Raises SAXParseException if the file is not valid XML. |
proj ifle parser. |
|
""" |
|
55 |
|
|
56 |
|
Raises IOError if the file cannot be opened, OSError if the file |
57 |
|
cannot be read and SAXParseException if the file is not valid XML. |
58 |
|
""" |
59 |
handler = ProjFileReader() |
handler = ProjFileReader() |
60 |
handler.read(filename) |
handler.read(filename) |
61 |
return handler.GetProjFile() |
return handler.GetProjFile(), handler.GetWarnings() |
62 |
|
|
63 |
def write_proj_file(pf): |
def write_proj_file(pf): |
64 |
"""Write a single .proj file |
"""Write a single .proj file |
69 |
saver = ProjFileSaver(pf) |
saver = ProjFileSaver(pf) |
70 |
saver.write(pf.GetFilename()) |
saver.write(pf.GetFilename()) |
71 |
|
|
|
def get_proj_files(dir): |
|
|
"""Read all .proj files in the given directory and |
|
|
return a list of ProjFile objects. |
|
|
""" |
|
72 |
|
|
73 |
list = [] |
def get_system_proj_file(): |
74 |
try: |
"""Return the standard projections and a list with warnings |
75 |
dirlist = os.listdir(dir) |
|
76 |
except OSError: |
The projections read from the default thuban projection file |
77 |
pass # if we can't get a directory listing just return [] |
(usually in Resources/Projections/defaults.proj). The return value |
78 |
else: |
is a tuple with the projections in a ProjFile object and a list of |
79 |
for file in filter(lambda s: s.endswith(PROJ_EXT), dirlist): |
strings with warning messages. The warnings list is usually empty |
80 |
try: |
but may contain messages about ignored errors. |
|
filename = os.path.join(dir, file) |
|
|
list.append(read_proj_file(filename)) |
|
|
except (OSError, IOError, SAXParseException): |
|
|
pass # just move onto the next file |
|
|
|
|
|
return list |
|
|
|
|
|
def get_system_proj_files(): |
|
|
"""Return a list of ProjFile objects from files that are |
|
|
supplied by Thuban. |
|
81 |
|
|
82 |
If no files could not be opened return a list with one |
If the file could could not be opened return an empty projection |
83 |
empty projection file set to store data in the default file. |
file object set to store data in the default file. |
84 |
""" |
""" |
85 |
filename = os.path.join(projdir, "defaults.proj") |
filename = os.path.join(projdir, "defaults.proj") |
86 |
try: |
try: |
87 |
return [read_proj_file(filename)] |
return read_proj_file(filename) |
88 |
except (OSError, IOError, SAXParseException): |
except (OSError, IOError, SAXParseException), val: |
89 |
return [ProjFile(filename)] |
msg = _('Could not read "%s": %s') % (filename, str(val)) |
90 |
|
return ProjFile(filename), [msg] |
91 |
|
|
92 |
|
def get_user_proj_file(): |
93 |
|
"""Return the user's projections and a list with warnings |
94 |
|
|
95 |
|
The projections read from the user's thuban projection file (usually |
96 |
|
in ~/.thuban/user.proj). The return value is a tuple with the |
97 |
|
projections in a ProjFile object and a list of strings with warning |
98 |
|
messages. The warnings list is usually empty but may contain |
99 |
|
messages about ignored errors. |
100 |
|
|
101 |
def get_user_proj_files(): |
If the file could could not be opened return an empty projection |
102 |
"""Return a list of ProjFile objects from files that are user-defined. |
file object set to store data in the default file. |
|
|
|
|
If no files could not be opened return a list with one |
|
|
empty projection file set to store data in the default file. |
|
103 |
""" |
""" |
|
|
|
104 |
usrdir = get_application_dir() |
usrdir = get_application_dir() |
105 |
filename = os.path.join(usrdir, "user.proj") |
filename = os.path.join(usrdir, "user.proj") |
106 |
try: |
try: |
107 |
return [read_proj_file(filename)] |
return read_proj_file(filename) |
108 |
except (OSError, IOError, SAXParseException): |
except (OSError, IOError, SAXParseException): |
109 |
return [ProjFile(filename)] |
msg = _('Could not read "%s": %s') % (filename, str(val)) |
110 |
|
return ProjFile(filename), [msg] |
111 |
|
|
112 |
|
|
113 |
class ProjFileReader(XMLReader): |
class ProjFileReader(XMLReader): |
114 |
|
|
115 |
def __init__(self): |
def __init__(self): |
116 |
XMLReader.__init__(self) |
XMLReader.__init__(self) |
117 |
self.__pf = ProjFile("") |
self.projfile = ProjFile("") |
118 |
|
self.warnings = [] |
119 |
|
|
120 |
XMLReader.AddDispatchers(self, |
XMLReader.AddDispatchers(self, |
121 |
{'projection': ("start_projection", "end_projection"), |
{'projection': ("start_projection", "end_projection"), |
122 |
'parameter': ("start_parameter", None)}) |
'parameter': ("start_parameter", None)}) |
123 |
|
|
124 |
def read(self, file_or_filename): |
def read(self, file_or_filename): |
125 |
XMLReader.read(self, file_or_filename) |
XMLReader.read(self, file_or_filename) |
126 |
|
|
127 |
self.__pf.SetFilename(XMLReader.GetFilename(self)) |
self.projfile.SetFilename(XMLReader.GetFilename(self)) |
128 |
|
|
129 |
def start_projection(self, name, qname, attrs): |
def start_projection(self, name, qname, attrs): |
130 |
self.params = [] |
self.params = [] |
131 |
self.name = self.encode(attrs.get((None, 'name'), _("Unknown"))) |
name = self.encode(attrs.get((None, 'name'))) |
132 |
|
if name is None: |
133 |
|
name = _("Unknown") |
134 |
|
self.name = name |
135 |
|
|
136 |
def end_projection(self, name, qname): |
def end_projection(self, name, qname): |
137 |
self.__pf.Add(Projection(self.params, self.name)) |
try: |
138 |
|
proj = Projection(self.params, self.name) |
139 |
|
except IOError, val: |
140 |
|
self.warnings.append(_('Error in projection "%s": %s') |
141 |
|
% (self.name, str(val))) |
142 |
|
else: |
143 |
|
self.projfile.Add(proj) |
144 |
|
|
145 |
def start_parameter(self, name, qname, attrs): |
def start_parameter(self, name, qname, attrs): |
146 |
s = attrs.get((None, 'value')) |
s = attrs.get((None, 'value')) |
148 |
self.params.append(s) |
self.params.append(s) |
149 |
|
|
150 |
def GetProjFile(self): |
def GetProjFile(self): |
151 |
return self.__pf |
return self.projfile |
152 |
|
|
153 |
|
def GetWarnings(self): |
154 |
|
"""Return the list of warning messages that may have been produced""" |
155 |
|
return self.warnings |
156 |
|
|
157 |
|
|
158 |
class ProjFileSaver(XMLWriter): |
class ProjFileSaver(XMLWriter): |
159 |
|
|