1   
  2   
  3   
  4  from translate.storage import factory 
  5  from translate.storage.directory import Directory 
  6  from translate.misc import wStringIO 
  7   
  8  from gzip import GzipFile 
  9  try: 
 10       
 11      from bz2 import BZ2File 
 12  except ImportError: 
 13      BZ2File = None 
 14  import os 
 15   
 17      """returns the classname to ease testing""" 
 18      classinstance = factory.getclass(filename) 
 19      return str(classinstance.__name__).lower() 
  20   
 26   
 29          """sets up a test directory""" 
 30          self.testdir = "%s_testdir" % (self.__class__.__name__) 
 31          self.cleardir(self.testdir) 
 32          os.mkdir(self.testdir) 
  33   
 35          """removes the attributes set up by setup_method""" 
 36          self.cleardir(self.testdir) 
  37   
 39          """removes the given directory""" 
 40          if os.path.exists(dirname): 
 41              for dirpath, subdirs, filenames in os.walk(dirname, topdown=False): 
 42                  for name in filenames: 
 43                      os.remove(os.path.join(dirpath, name)) 
 44                  for name in subdirs: 
 45                      os.rmdir(os.path.join(dirpath, name)) 
 46          if os.path.exists(dirname): os.rmdir(dirname) 
 47          assert not os.path.exists(dirname) 
  48      cleardir = classmethod(cleardir) 
 49   
 51          assert classname("file.po") == "pofile" 
 52          assert classname("file.pot") == "pofile" 
 53          assert classname("file.dtd.po") == "pofile" 
 54   
 55          assert classname("file.tmx") == "tmxfile" 
 56          assert classname("file.af.tmx") == "tmxfile" 
 57          assert classname("file.tbx") == "tbxfile" 
 58          assert classname("file.po.xliff") == "xlifffile" 
 59   
 60          assert not classname("file.po") == "tmxfile" 
 61          assert not classname("file.po") == "xlifffile" 
 62   
 63          assert classname("file.po.gz") == "pofile" 
 64          assert classname("file.pot.gz") == "pofile" 
 65          assert classname("file.dtd.po.gz") == "pofile" 
 66   
 67          assert classname("file.tmx.gz") == "tmxfile" 
 68          assert classname("file.af.tmx.gz") == "tmxfile" 
 69          assert classname("file.tbx.gz") == "tbxfile" 
 70          assert classname("file.po.xliff.gz") == "xlifffile" 
 71   
 72          assert not classname("file.po.gz") == "tmxfile" 
 73          assert not classname("file.po.gz") == "xlifffile" 
 74   
 75          assert classname("file.po.bz2") == "pofile" 
 76          assert classname("file.pot.bz2") == "pofile" 
 77          assert classname("file.dtd.po.bz2") == "pofile" 
 78   
 79          assert classname("file.tmx.bz2") == "tmxfile" 
 80          assert classname("file.af.tmx.bz2") == "tmxfile" 
 81          assert classname("file.tbx.bz2") == "tbxfile" 
 82          assert classname("file.po.xliff.bz2") == "xlifffile" 
 83   
 84          assert not classname("file.po.bz2") == "tmxfile" 
 85          assert not classname("file.po.bz2") == "xlifffile" 
  86   
 92   
 94          """Tests that we get a valid object from a file object without a name.""" 
 95          fileobj = wStringIO.StringIO(self.file_content) 
 96          assert not hasattr(fileobj, 'name') 
 97          store = factory.getobject(fileobj) 
 98          assert isinstance(store, self.expected_instance) 
  99   
108   
119   
121          """Test that a directory is correctly detected.""" 
122          object = factory.getobject(self.testdir) 
123          assert isinstance(object, Directory) 
 131      expected_instance = factory.xliff.xlifffile 
132      filename = 'dummy.xliff' 
133      file_content = '''<?xml version="1.0" encoding="utf-8"?> 
134  <xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1"> 
135  <file> 
136  <body> 
137    <trans-unit> 
138      <source>test</source> 
139      <target>rest</target> 
140    </trans-unit> 
141  </body> 
142  </file> 
143  </xliff>''' 
 144   
146      expected_instance = factory.poxliff.PoXliffFile 
147      filename = 'dummy.xliff' 
148      file_content = '''<?xml version="1.0" encoding="utf-8"?> 
149  <xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1"> 
150  <file datatype="po" original="file.po" source-language="en-US"><body><trans-unit approved="no" id="1" restype="x-gettext-domain-header" xml:space="preserve"> 
151  <source>MIME-Version: 1.0 
152  Content-Type: text/plain; charset=UTF-8 
153  Content-Transfer-Encoding: 8bit 
154  </source> 
155  <target>MIME-Version: 1.0 
156  Content-Type: text/plain; charset=UTF-8 
157  Content-Transfer-Encoding: 8bit 
158  </target> 
159  </trans-unit></body></file></xliff>''' 
 160   
162      expected_instance = factory.wordfast.WordfastTMFile 
163      filename = 'dummy.txt' 
164      file_content = '''%20070801~103212  %User ID,S,S SMURRAY,SMS Samuel Murray-Smit,SM Samuel Murray-Smit,MW Mary White,DS Deepak Shota,MT! Machine translation (15),AL! Alignment (10),SM Samuel Murray,       %TU=00000075    %AF-ZA  %Wordfast TM v.5.51r/00 %EN-ZA  %---80597535    Subject (5),EL,EL Electronics,AC Accounting,LE Legal,ME Mechanics,MD Medical,LT Literary,AG Agriculture,CO Commercial   Client (5),LS,LS LionSoft Corp,ST SuperTron Inc,CA CompArt Ltd                   
165  20070801~103248 SM      0       AF-ZA   Langeraad en duimpie    EN-ZA   Big Ben and Little John EL      LS''' 
 166