Module comments |
[hide private]
[frames] | no frames]

# Source Code for Module comments

      1  #============================================================================
      2  #Name        : comments.py
      3  #Part of     : Helium
      4
      5  #Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
      6  #All rights reserved.
      7  #This component and the accompanying materials are made available
      8  #under the terms of the License "Eclipse Public License v1.0"
      9  #which accompanies this distribution, and is available
     10  #at the URL "http://www.eclipse.org/legal/epl-v10.html".
     11  #
     12  #Initial Contributors:
     13  #Nokia Corporation - initial contribution.
     14  #
     15  #Contributors:
     16  #
     17  #Description:
     18  #===============================================================================
     19
     20  """ Helper to parse branch information.
     21  """
     22  import amara
     23  import logging
     24  import os
     25  import re
     26  import string
     27
     28
     29  COMMENT_SYMBOLS = {
     30      '.java': ['//'],
     31      '.hrh': ['//'],
     32      '.cpp': ['//'],
     33      '.h': ['//'],
     34      '.inf': ['//'],
     35      '.mmp': ['//'],
     36      '.iby': ['//'],
     37      '.pl':['#'],
     38      '.py':['#'],
     39      '.mk':['#'],
     40      '.bat':['REM'],
     41      '.xml':['<!--'],
     42      '.txt':['//'],
     43      '.cmd':['#','REM']
     44      }
     45
     46
     47  # Uncomment this line to enable logging in this module, or configure logging elsewhere
     48  #logging.basicConfig(level=logging.DEBUG)
     49  _logger = logging.getLogger("comments")
     50
     51
     52 -class CommentParser(object):
     53      """ Parse branch information. """
     54 -    def __init__(self, files, element_name):
     55          self.files = files
     56          self.element_name = element_name
     57
     58 -    def scan(self):
     59          """ This method goes processes the input files.
     60
     61          It returns an xml document.  """
     62          doc = amara.create_document(u"commentLog")
     63          for path in self.files:
     64              open_file = open(path)
     65              CommentParser.scan_content(path, open_file.read(), self.element_name, doc)
     66              open_file.close()
     67
     68          #print doc.xml()
     69          return doc
     70
     71      @staticmethod
     72 -    def scan_content(filename, content, element_name, doc=None):
     73          """ This method scan the defined content to find any custom comment tags.
     74
     75          It returns an xml document.
     76          """
     77          # Creating a doc if not defined
     78          if not doc:
     79              doc = amara.create_document(u"commentLog")
     80
     81          # Search the file for any XML elements matching the given element name
     82          regex = string.Template(r"<${element_name}.*</${element_name}>").substitute(element_name=element_name)
     83          comment_elements = re.findall(regex, content, re.DOTALL)
     84          for comment in comment_elements:
     85              (_, file_type) = os.path.splitext(filename)
     86              file_type = file_type.lower()
     87              if COMMENT_SYMBOLS.has_key(file_type):
     88                  for i in range(len(COMMENT_SYMBOLS[file_type])):
     89                      comment = comment.replace(COMMENT_SYMBOLS[file_type][i], "")
     90              try:
     91                  doc.commentLog.xml_append_fragment(comment)
     92                  # Add a generic file attribute to the comment to label which file it comes from
     93                  doc.commentLog.xml_children[-1].xml_set_attribute(u'file', unicode(filename))
     94              except Exception:
     95                  _logger.warning("A comment in '%s' is not valid XML." % filename)
     96
     97          #print doc.xml()
     98          return doc
     99