home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / xml2po / docbook.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  6.2 KB  |  202 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import re
  5. import libxml2
  6. import os
  7. import md5
  8. import sys
  9.  
  10. class docbookXmlMode:
  11.     '''Class for special handling of DocBook document types.
  12.  
  13.     It sets lang attribute on article elements, and adds translators
  14.     to articleinfo/copyright.'''
  15.     
  16.     def __init__(self):
  17.         self.lists = [
  18.             'itemizedlist',
  19.             'orderedlist',
  20.             'variablelist',
  21.             'segmentedlist',
  22.             'simplelist',
  23.             'calloutlist',
  24.             'varlistentry']
  25.         self.objects = [
  26.             'figure',
  27.             'textobject',
  28.             'imageobject',
  29.             'mediaobject',
  30.             'screenshot']
  31.  
  32.     
  33.     def getIgnoredTags(self):
  34.         '''Returns array of tags to be ignored.'''
  35.         return self.objects + self.lists
  36.  
  37.     
  38.     def getFinalTags(self):
  39.         """Returns array of tags to be considered 'final'."""
  40.         return [
  41.             'para',
  42.             'formalpara',
  43.             'simpara',
  44.             'releaseinfo',
  45.             'revnumber',
  46.             'title',
  47.             'date',
  48.             'term',
  49.             'programlisting'] + self.objects + self.lists
  50.  
  51.     
  52.     def getSpacePreserveTags(self):
  53.         '''Returns array of tags in which spaces are to be preserved.'''
  54.         return [
  55.             'classsynopsisinfo',
  56.             'computeroutput',
  57.             'funcsynopsisinfo',
  58.             'literallayout',
  59.             'programlisting',
  60.             'screen',
  61.             'synopsis',
  62.             'userinput']
  63.  
  64.     
  65.     def getTreatedAttributes(self):
  66.         '''Returns array of tag attributes which content is to be translated'''
  67.         return []
  68.  
  69.     
  70.     def getStringForTranslators(self):
  71.         '''Returns string which will be used to credit translators.'''
  72.         return 'translator-credits'
  73.  
  74.     
  75.     def getCommentForTranslators(self):
  76.         '''Returns a comment to be added next to string for crediting translators.'''
  77.         return 'Put one translator per line, in the form of NAME <EMAIL>, YEAR1, YEAR2.'
  78.  
  79.     
  80.     def _find_articleinfo(self, node):
  81.         if node.name == 'articleinfo' or node.name == 'bookinfo':
  82.             return node
  83.         
  84.         child = node.children
  85.         while child:
  86.             ret = self._find_articleinfo(child)
  87.             if ret:
  88.                 return ret
  89.             
  90.             child = child.next
  91.  
  92.     
  93.     def _find_lastcopyright(self, node):
  94.         if not node.children:
  95.             return None
  96.         
  97.         last = node.lastChild()
  98.         tmp = last
  99.         while tmp:
  100.             if tmp.name == 'copyright':
  101.                 last = tmp
  102.                 break
  103.             
  104.             tmp = tmp.prev
  105.         return last
  106.  
  107.     
  108.     def _md5_for_file(self, filename):
  109.         hash = md5.new()
  110.         input = open(filename, 'rb')
  111.         read = input.read(4096)
  112.         while read:
  113.             hash.update(read)
  114.             read = input.read(4096)
  115.         input.close()
  116.         return hash.hexdigest()
  117.  
  118.     
  119.     def _output_images(self, node, msg):
  120.         if node and node.type == 'element' and node.name == 'imagedata':
  121.             attr = node.prop('fileref')
  122.             if attr:
  123.                 dir = os.path.dirname(msg.filename)
  124.                 fullpath = os.path.join(dir, attr)
  125.                 if os.path.exists(fullpath):
  126.                     hash = self._md5_for_file(fullpath)
  127.                 else:
  128.                     hash = "THIS FILE DOESN'T EXIST"
  129.                     print >>sys.stderr, "Warning: image file '%s' not found." % fullpath
  130.                 msg.outputMessage("@@image: '%s'; md5=%s" % (attr, hash), node.lineNo(), 'When image changes, this message will be marked fuzzy or untranslated for you.\n' + "It doesn't matter what you translate it to: it's not used at all.")
  131.             
  132.         elif node and node.children:
  133.             child = node.children
  134.             while child:
  135.                 self._output_images(child, msg)
  136.                 child = child.next
  137.         
  138.  
  139.     
  140.     def preProcessXml(self, doc, msg):
  141.         '''Add additional messages of interest here.'''
  142.         root = doc.getRootElement()
  143.         self._output_images(root, msg)
  144.  
  145.     
  146.     def postProcessXmlTranslation(self, doc, language, translators):
  147.         '''Sets a language and translators in "doc" tree.
  148.         
  149.         "translators" is a string consisted of "Name <email>, years" pairs
  150.         of each translator, separated by newlines.'''
  151.         root = doc.getRootElement()
  152.         while root and root.name != 'article' and root.name != 'book':
  153.             root = root.next
  154.         if root:
  155.             if root.name == 'article' or root.name == 'book':
  156.                 root.setProp('lang', language)
  157.             else:
  158.                 return None
  159.         if translators == self.getStringForTranslators():
  160.             return None
  161.         elif translators:
  162.             ai = self._find_articleinfo(root)
  163.             if not ai:
  164.                 return None
  165.             
  166.             lines = translators.split('\n')
  167.             for line in lines:
  168.                 line = line.strip()
  169.                 match = re.match('^([^<,]+)\\s*(?:<([^>,]+)>)?,\\s*(.*)$', line)
  170.                 if match:
  171.                     last = self._find_lastcopyright(ai)
  172.                     copy = libxml2.newNode('copyright')
  173.                     if last:
  174.                         copy = last.addNextSibling(copy)
  175.                     else:
  176.                         ai.addChild(copy)
  177.                     if match.group(3):
  178.                         copy.newChild(None, 'year', match.group(3).encode('utf-8'))
  179.                     
  180.                     if match.group(1) and match.group(2):
  181.                         holder = match.group(1) + '(%s)' % match.group(2)
  182.                     elif match.group(1):
  183.                         holder = match.group(1)
  184.                     elif match.group(2):
  185.                         holder = match.group(2)
  186.                     else:
  187.                         holder = '???'
  188.                     copy.newChild(None, 'holder', holder.encode('utf-8'))
  189.                     continue
  190.             
  191.         
  192.  
  193.  
  194. if __name__ == '__main__':
  195.     test = docbookXmlMode()
  196.     print 'Ignored tags       : ' + repr(test.getIgnoredTags())
  197.     print 'Final tags         : ' + repr(test.getFinalTags())
  198.     print 'Space-preserve tags: ' + repr(test.getSpacePreserveTags())
  199.     print "Credits from string: '%s'" % test.getStringForTranslators()
  200.     print "Explanation for credits:\n\t'%s'" % test.getCommentForTranslators()
  201.  
  202.