Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(8)

Side by Side Diff: translate/storage/factory.py

Issue 4: Format knowledge in format classes SVN Base: https://translate.svn.sourceforge.net/svnroot/translate/src/trunk/
Patch Set: Second try Created 1 year, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 # 3 #
4 # Copyright 2006 Zuza Software Foundation 4 # Copyright 2006 Zuza Software Foundation
5 # 5 #
6 # This file is part of translate. 6 # This file is part of translate.
7 # 7 #
8 # translate is free software; you can redistribute it and/or modify 8 # translate is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by 9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or 10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version. 11 # (at your option) any later version.
12 # 12 #
13 # translate is distributed in the hope that it will be useful, 13 # translate is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details. 16 # GNU General Public License for more details.
17 # 17 #
18 # You should have received a copy of the GNU General Public License 18 # You should have received a copy of the GNU General Public License
19 # along with translate; if not, write to the Free Software 19 # along with translate; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 21
22 """factory methods to build real storage objects that conform to base.py""" 22 """factory methods to build real storage objects that conform to base.py"""
23 23
24 import os 24 import os
25 from gzip import GzipFile 25 from gzip import GzipFile
26 try: 26 try:
27 # bz2 is not available on python 2.3 27 # bz2 is not available on python 2.3
28 from bz2 import BZ2File 28 from bz2 import BZ2File
29 except ImportError: 29 except ImportError:
30 BZ2File = None 30 BZ2File = None
31 import sys 31 import sys
32 32
33 from translate.storage import base 33 from translate.storage import base
34 from translate.storage import csvl10n 34 from translate.storage import csvl10n
35 from translate.storage import mo 35 from translate.storage import mo
36 from translate.storage import po 36 from translate.storage import po
37 from translate.storage import qm 37 from translate.storage import qm
38 from translate.storage import wordfast 38 from translate.storage import wordfast
39 #Let's try to import the XML formats carefully. They might fail if the user 39 #Let's try to import the XML formats carefully. They might fail if the user
40 #doesn't have lxml installed. Let's try to continue gracefully, but print an 40 #doesn't have lxml installed. Let's try to continue gracefully, but print an
41 #informative warning. 41 #informative warning.
42 try: 42 try:
43 #Although poxliff is unused in this module, it is referenced in test_factory 43 #Although poxliff is unused in this module, it is referenced in test_factory
44 from translate.storage import poxliff 44 from translate.storage import poxliff
45 from translate.storage import tbx 45 from translate.storage import tbx
46 from translate.storage import tmx 46 from translate.storage import tmx
47 from translate.storage import xliff 47 from translate.storage import xliff
48 support_xml = True 48 support_xml = True
49 except ImportError, e: 49 except ImportError, e:
50 print >> sys.stderr, str(e) 50 print >> sys.stderr, str(e)
(...skipping 86 matching lines...) Show 10 above Show 10 below
137 Specify ignore to ignore some part at the back of the name (like .gz). """ 137 Specify ignore to ignore some part at the back of the name (like .gz). """
138 storefilename = _getname(storefile) 138 storefilename = _getname(storefile)
139 if ignore and storefilename.endswith(ignore): 139 if ignore and storefilename.endswith(ignore):
140 storefilename = storefilename[:-len(ignore)] 140 storefilename = storefilename[:-len(ignore)]
141 root, ext = os.path.splitext(storefilename) 141 root, ext = os.path.splitext(storefilename)
142 ext = ext[len(os.path.extsep):].lower() 142 ext = ext[len(os.path.extsep):].lower()
143 decomp = None 143 decomp = None
144 if ext in decompressclass: 144 if ext in decompressclass:
145 decomp = ext 145 decomp = ext
146 root, ext = os.path.splitext(root) 146 root, ext = os.path.splitext(root)
147 ext = ext[len(os.path.extsep):].lower() 147 ext = ext[len(os.path.extsep):].lower()
148 if ext in hiddenclasses: 148 if ext in hiddenclasses:
149 guesserfn = hiddenclasses[ext] 149 guesserfn = hiddenclasses[ext]
150 if decomp: 150 if decomp:
151 ext = guesserfn(decompressclass[decomp](storefile)) 151 ext = guesserfn(decompressclass[decomp](storefile))
152 else: 152 else:
153 ext = guesserfn(storefile) 153 ext = guesserfn(storefile)
154 try: 154 try:
155 storeclass = classes[ext] 155 storeclass = classes[ext]
156 except KeyError: 156 except KeyError:
157 raise ValueError("Unknown filetype (%s)" % storefilename) 157 raise ValueError("Unknown filetype (%s)" % storefilename)
158 return storeclass 158 return storeclass
159 159
160 def getobject(storefile, ignore=None): 160 def getobject(storefile, ignore=None):
161 """Factory that returns a usable object for the type of file presented. 161 """Factory that returns a usable object for the type of file presented.
162 162
163 @type storefile: file or str 163 @type storefile: file or str
164 @param storefile: File object or file name. 164 @param storefile: File object or file name.
165 165
166 Specify ignore to ignore some part at the back of the name (like .gz). 166 Specify ignore to ignore some part at the back of the name (like .gz).
167 """ 167 """
168 168
169 if isinstance(storefile, base.TranslationStore): 169 if isinstance(storefile, base.TranslationStore):
170 return storefile 170 return storefile
171 if isinstance(storefile, basestring): 171 if isinstance(storefile, basestring):
172 if os.path.isdir(storefile) or storefile.endswith(os.path.sep): 172 if os.path.isdir(storefile) or storefile.endswith(os.path.sep):
173 from translate.storage import directory 173 from translate.storage import directory
174 return directory.Directory(storefile) 174 return directory.Directory(storefile)
175 storefilename = _getname(storefile) 175 storefilename = _getname(storefile)
176 storeclass = getclass(storefile, ignore) 176 storeclass = getclass(storefile, ignore)
177 if os.path.exists(storefilename) or not getattr(storefile, "closed", True): 177 if os.path.exists(storefilename) or not getattr(storefile, "closed", True):
178 name, ext = os.path.splitext(storefilename) 178 name, ext = os.path.splitext(storefilename)
179 ext = ext[len(os.path.extsep):].lower() 179 ext = ext[len(os.path.extsep):].lower()
180 if ext in decompressclass: 180 if ext in decompressclass:
181 storefile = decompressclass[ext](storefilename) 181 storefile = decompressclass[ext](storefilename)
182 store = storeclass.parsefile(storefile) 182 store = storeclass.parsefile(storefile)
183 else: 183 else:
184 store = storeclass() 184 store = storeclass()
185 return store 185 return store
186 186
187 def supported_files():
188 """Returns data about all supported files
189
190 @return: list of type that include (name, extension, mimetypes)
191 @rtype: list
192 """
193
194 supported = []
195 processed = []
196 for supported_class in classes.itervalues():
197 name = getattr(supported_class, "name", None)
198 if name is None or name in processed:
199 continue
200 processed.append(name)
201 extension = getattr(supported_class, "extension", None)
202 mimetype = getattr(supported_class, "mimetype", None)
203 supported.extend([(name, extension, mimetype)])
jean.jordaan 2008/07/17 06:33:20 Why the one-element list? Why not 'supported.appe
204 return supported
OLDNEW

Powered by Google App Engine
This is Rietveld r159