1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """factory methods to build real storage objects that conform to base.py"""
23
24 import os
25 from gzip import GzipFile
26 try:
27
28 from bz2 import BZ2File
29 except ImportError:
30 BZ2File = None
31 import sys
32
33 from translate.storage import base
34 from translate.storage import csvl10n
35 from translate.storage import mo
36 from translate.storage import po
37 from translate.storage import qm
38 from translate.storage import wordfast
39
40
41
42 try:
43
44 from translate.storage import poxliff
45 from translate.storage import tbx
46 from translate.storage import tmx
47 from translate.storage import xliff
48 support_xml = True
49 except ImportError, e:
50 print >> sys.stderr, str(e)
51 support_xml = False
52
53
54
55
56 classes = {
57 "csv": csvl10n.csvfile,
58 "po": po.pofile, "pot": po.pofile,
59 "mo": mo.mofile, "gmo": mo.mofile,
60 "qm": qm.qmfile,
61 "_wftm": wordfast.WordfastTMFile,
62 }
63 """Dictionary of file extensions and their associated class.
64
65 _ext is a pseudo extension, that is their is no real extension by that name."""
66
67 if support_xml:
68 classes.update({
69 "tbx": tbx.tbxfile,
70 "tmx": tmx.tmxfile,
71 "xliff": xliff.xlifffile, "xlf": xliff.xlifffile,
72 })
73
74 decompressclass = {
75 'gz': GzipFile,
76 }
77 if BZ2File:
78 decompressclass['bz2'] = BZ2File
79
81 """Determine the true filetype for a .txt file"""
82 if isinstance(storefile, basestring) and os.path.exists(storefile):
83 storefile = open(storefile)
84 try:
85 start = storefile.read(600).strip()
86 except AttributeError:
87 raise ValueError("Need to read object to determine type")
88
89 if wordfast.TAB_UTF16 in start.split("\n")[0]:
90 encoding = 'utf-16'
91 else:
92 encoding = 'iso-8859-1'
93 start = start.decode(encoding).encode('utf-8')
94 if '%Wordfast TM' in start:
95 pseudo_extension = '_wftm'
96 else:
97 raise ValueError("Failed to guess file type.")
98 storefile.seek(0)
99 return pseudo_extension
100
101 hiddenclasses = {"txt": _examine_txt}
102
104 """Guesses the type of a file object by looking at the first few characters.
105 The return value is a file extention ."""
106 start = storefile.read(300).strip()
107 if '<xliff ' in start:
108 extention = 'xlf'
109 elif 'msgid "' in start:
110 extention = 'po'
111 elif '%Wordfast TM' in start:
112 extention = 'txt'
113 else:
114 raise ValueError("Failed to guess file type.")
115 storefile.seek(0)
116 return extention
117
119 """Provides a dummy name for a file object without a name attribute, by guessing the file type."""
120 return 'dummy.' + _guessextention(storefile)
121
123 """returns the filename"""
124 if storefile is None:
125 raise ValueError("This method cannot magically produce a filename when given None as input.")
126 if not isinstance(storefile, basestring):
127 if not hasattr(storefile, "name"):
128 storefilename = _getdummyname(storefile)
129 else:
130 storefilename = storefile.name
131 else:
132 storefilename = storefile
133 return storefilename
134
136 """Factory that returns the applicable class for the type of file presented.
137 Specify ignore to ignore some part at the back of the name (like .gz). """
138 storefilename = _getname(storefile)
139 if ignore and storefilename.endswith(ignore):
140 storefilename = storefilename[:-len(ignore)]
141 root, ext = os.path.splitext(storefilename)
142 ext = ext[len(os.path.extsep):].lower()
143 decomp = None
144 if ext in decompressclass:
145 decomp = ext
146 root, ext = os.path.splitext(root)
147 ext = ext[len(os.path.extsep):].lower()
148 if ext in hiddenclasses:
149 guesserfn = hiddenclasses[ext]
150 if decomp:
151 ext = guesserfn(decompressclass[decomp](storefile))
152 else:
153 ext = guesserfn(storefile)
154 try:
155 storeclass = classes[ext]
156 except KeyError:
157 raise ValueError("Unknown filetype (%s)" % storefilename)
158 return storeclass
159
161 """Factory that returns a usable object for the type of file presented.
162
163 @type storefile: file or str
164 @param storefile: File object or file name.
165
166 Specify ignore to ignore some part at the back of the name (like .gz).
167 """
168
169 if isinstance(storefile, base.TranslationStore):
170 return storefile
171 if isinstance(storefile, basestring):
172 if os.path.isdir(storefile) or storefile.endswith(os.path.sep):
173 from translate.storage import directory
174 return directory.Directory(storefile)
175 storefilename = _getname(storefile)
176 storeclass = getclass(storefile, ignore)
177 if os.path.exists(storefilename) or not getattr(storefile, "closed", True):
178 name, ext = os.path.splitext(storefilename)
179 ext = ext[len(os.path.extsep):].lower()
180 if ext in decompressclass:
181 storefile = decompressclass[ext](storefilename)
182 store = storeclass.parsefile(storefile)
183 else:
184 store = storeclass()
185 return store
186