translate.storage.txt

Package translate :: Package storage :: Module txt |
[hide private]
[frames] | no frames]

# Source Code for Module translate.storage.txt

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2007 Zuza Software Foundation
      5  #
      6  # This file is part of translate.
      7  #
      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
     10  # the Free Software Foundation; either version 2 of the License, or
     11  # (at your option) any later version.
     12  #
     13  # translate is distributed in the hope that it will be useful,
     14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  # GNU General Public License for more details.
     17  #
     18  # You should have received a copy of the GNU General Public License
     19  # along with translate; if not, write to the Free Software
     20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     21
     22  """This class implements the functionality for handling plain text files, or
     23  similar wiki type files.
     24
     25  Supported formats are
     26    - Plain text
     27    - dokuwiki
     28    - MediaWiki
     29  """
     30
     31  from translate.storage import base
     32  import re
     33
     34  dokuwiki = []
     35  dokuwiki.append(("Dokuwiki heading", re.compile(r"( ?={2,6}[\s]*)(.+)"), re.compile("([\s]*={2,6}[\s]*)$")))
     36  dokuwiki.append(("Dokuwiki bullet", re.compile(r"([\s]{2,}\*[\s]*)(.+)"), re.compile("[\s]+$")))
     37  dokuwiki.append(("Dokuwiki numbered item", re.compile(r"([\s]{2,}-[\s]*)(.+)"), re.compile("[\s]+$")))
     38
     39  mediawiki = []
     40  mediawiki.append(("MediaWiki heading", re.compile(r"(={2,5}[\s]*)(.+)"), re.compile("([\s]*={2,5}[\s]*)$")))
     41  mediawiki.append(("MediaWiki bullet", re.compile(r"(\*+[\s]*)(.+)"), re.compile("[\s]+$")))
     42  mediawiki.append(("MediaWiki numbered item", re.compile(r"(#+[\s]*)(.+)"), re.compile("[\s]+$")))
     43
     44  flavours = {
     45  "dokuwiki": dokuwiki,
     46  "mediawiki": mediawiki,
     47  None: [],
     48  "plain": []
     49  }
     50
     51 -class TxtUnit(base.TranslationUnit):
     52      """This class represents a block of text from a text file"""
     53 -    def __init__(self, source="", encoding="utf-8"):
     54          """Construct the txtunit"""
     55          self.encoding = encoding
     56          super(TxtUnit, self).__init__(source)
     57          self.source = source
     58          self.pretext = ""
     59          self.posttext = ""
     60          self.location = []
     61
     62 -    def __str__(self):
     63          """Convert a txt unit to a string"""
     64          string = u"".join([self.pretext, self.source, self.posttext])
     65          if isinstance(string, unicode):
     66              return string.encode(self.encoding)
     67          return string
     68
     69      # Note that source and target are equivalent for monolingual units
     70 -    def setsource(self, source):
     71          """Sets the definition to the quoted value of source"""
     72          if isinstance(source, str):
     73              source = source.decode(self.encoding)
     74          self._rich_source = None
     75          self._source = source
     76
     77 -    def getsource(self):
     78          """gets the unquoted source string"""
     79          return self._source
     80      source = property(getsource, setsource)
     81
     82 -    def settarget(self, target):
     83          """Sets the definition to the quoted value of target"""
     84          self._rich_target = None
     85          self.source = target
     86
     87 -    def gettarget(self):
     88          """gets the unquoted target string"""
     89          return self.source
     90      target = property(gettarget, settarget)
     91
     92 -    def addlocation(self, location):
     93          self.location.append(location)
     94
     95 -    def getlocations(self):
     96          return self.location
     97
     98 -class TxtFile(base.TranslationStore):
     99      """This class represents a text file, made up of txtunits"""
    100      UnitClass = TxtUnit
    101 -    def __init__(self, inputfile=None, flavour=None, encoding="utf-8"):
    102          base.TranslationStore.__init__(self, unitclass=self.UnitClass)
    103          self.filename = getattr(inputfile, 'name', '')
    104          self.flavour = flavours.get(flavour, [])
    105          if inputfile is not None:
    106              txtsrc = inputfile.readlines()
    107              self.parse(txtsrc)
    108          self.encoding = encoding
    109
    110 -    def parse(self, lines):
    111          """Read in text lines and create txtunits from the blocks of text"""
    112          block = []
    113          startline = 0
    114          pretext = ""
    115          posttext = ""
    116          if not isinstance(lines, list):
    117              lines = lines.split("\n")
    118          for linenum in range(len(lines)):
    119              line = lines[linenum].rstrip("\n").rstrip("\r")
    120              for rule, prere, postre in self.flavour:
    121                  match = prere.match(line)
    122                  if match:
    123                      pretext, source = match.groups()
    124                      postmatch = postre.search(source)
    125                      if postmatch:
    126                          posttext = postmatch.group()
    127                          source = source[:postmatch.start()]
    128                      block.append(source)
    129                      isbreak = True
    130                      break
    131              else:
    132                  isbreak = not line.strip()
    133              if isbreak and block:
    134                  unit = self.addsourceunit("\n".join(block))
    135                  unit.addlocation("%s:%d" % (self.filename, startline + 1))
    136                  unit.pretext = pretext
    137                  unit.posttext = posttext
    138                  pretext = ""
    139                  posttext = ""
    140                  block = []
    141              elif not isbreak:
    142                  if not block:
    143                      startline = linenum
    144                  block.append(line)
    145          if block:
    146              unit = self.addsourceunit("\n".join(block))
    147              unit.addlocation("%s:%d" % (self.filename, startline + 1))
    148
    149 -    def __str__(self):
    150          source = self.getoutput()
    151          if isinstance(source, unicode):
    152              return source.encode(getattr(self, "encoding", "UTF-8"))
    153          return source
    154
    155 -    def getoutput(self):
    156          """Convert the units back to blocks"""
    157          blocks = [str(unit) for unit in self.units]
    158          string = "\n\n".join(blocks)
    159          return string
    160