Package pyctags :: Module tag_file
[hide private]
[frames] | no frames]

Source Code for Module pyctags.tag_file

  1  ## Copyright (C) 2008 Ben Smith <benjamin.coder.smith@gmail.com> 
  2   
  3  ##    This file is part of pyctags. 
  4   
  5  ##    pyctags is free software: you can redistribute it and/or modify 
  6  ##    it under the terms of the GNU Lesser General Public License as published 
  7  ##    by the Free Software Foundation, either version 3 of the License, or 
  8  ##    (at your option) any later version. 
  9   
 10  ##    pyctags is distributed in the hope that it will be useful, 
 11  ##    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  ##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  ##    GNU General Public License for more details. 
 14   
 15  ##    You should have received a copy of the GNU Lesser General Public License 
 16  ##    and the GNU Lesser General Public Licens along with pyctags.  If not,  
 17  ##    see <http://www.gnu.org/licenses/>. 
 18   
 19  """ 
 20  Python representation of ctags format file. 
 21  """ 
 22   
 23  import os 
 24  try: 
 25      # do relative imports for tests 
 26      # try this first in case pyctags is already installed, since we want to be testing the source bundled in the distribution 
 27      from kwargs_validator import the_validator as validator 
 28      from tag_entry import ctags_entry, _PYTHON_3000_ 
 29  except ImportError: 
 30      from pyctags.kwargs_validator import the_validator as validator 
 31      from pyctags.tag_entry import ctags_entry, _PYTHON_3000_ 
 32   
 33   
34 -class ctags_file:
35 """ 36 Class that parses ctags generated files contains resulting ctags_entry objects. 37 """ 38
39 - def __init__(self, tags=None, **kwargs):
40 """ 41 Initializes instances of ctags_file. 42 - B{Keyword Arguments:} 43 - B{harvesters:} (list) list of harvester classes 44 @param tags: If I{tags} is a sequence, it will automatically be parsed. If it is a filename or path, it will be opened and parsed. 45 @type tags: sequence or str 46 """ 47 48 valid_kwargs = ['harvesters'] 49 validator.validate(kwargs.keys(), valid_kwargs) 50 51 self._clear_variables() 52 53 if tags: 54 if type(tags) == str: 55 tags = open(tags).readlines() 56 self.parse(tags, **kwargs)
57
58 - def _clear_variables(self):
59 """ 60 Sets internal maps to initial values. 61 """ 62 self.format = None 63 """ Format from the header.""" 64 self.format_comment = None 65 """ Format header comment.""" 66 self.sorted = None 67 """ Sorting type.""" 68 self.sorted_comment = None 69 """ Sorting type comment.""" 70 self.author = None 71 """ Ctags author.""" 72 self.author_comment = None 73 """ Ctags author comment.""" 74 self.name = None 75 """ Tag program name.""" 76 self.name_comment = None 77 """ Tag program comment.""" 78 self.url = None 79 """ Tag program url.""" 80 self.url_comment = None 81 """ Tag program url comment.""" 82 self.version = None 83 """ Tag program version.""" 84 self.version_comment = None 85 """ Tag program version comment.""" 86 87 self.tags = list() 88 """ List of ctags_entry elements.""" 89 90 self.__feed_harvesters = list() 91 """ List of harvesters used when parsing ctags output on the fly."""
92
93 - def __header_format(self, line):
94 """ Processes !_ctags_file_FORMAT ctags header.""" 95 if not self.format: 96 self.format = int(line[0]) 97 self.format_comment = line[1].strip('/')
98
99 - def __header_sorted(self, line):
100 """ Processes !_ctags_file_SORTED ctags header.""" 101 self.sorted = int(line[0]) 102 self.sorted_comment = line[1].strip('/')
103
104 - def __header_author(self, line):
105 """ Processes !_TAG_PROGRAM_AUTHOR ctags header.""" 106 self.author = line[0] 107 self.author_comment = line[1].strip('/')
108
109 - def __header_name(self, line):
110 """ Processes !_TAG_PROGRAM_NAME ctags header.""" 111 self.name = line[0] 112 self.name_comment = line[1].strip('/')
113
114 - def __header_url(self, line):
115 """ Processes !_TAG_PROGRAM_URL ctags header.""" 116 self.url = line[0] 117 self.url_comment = line[1].strip('/')
118
119 - def __header_version(self, line):
120 """ Processes !_TAG_PROGRAM_VERSION ctags header.""" 121 self.version = line[0] 122 self.version_comment = line[1].strip('/')
123 124 __HEADER_ITEMS = { 125 '!_TAG_FILE_FORMAT' : __header_format, 126 '!_TAG_FILE_SORTED' : __header_sorted, 127 '!_TAG_PROGRAM_AUTHOR' : __header_author, 128 '!_TAG_PROGRAM_NAME' : __header_name, 129 '!_TAG_PROGRAM_URL' : __header_url, 130 '!_TAG_PROGRAM_VERSION' : __header_version 131 } 132
133 - def parse(self, tags, **kwargs):
134 """ 135 Parses ctags file and constructs ctags_entry list. 136 - B{Keyword Arguments:} 137 - B{harvesters:} (list) list of harvester classes 138 @param tags: Filename or sequence of tag strings to parse. 139 @type tags: sequence or str 140 @raises ValueError: parsing error 141 """ 142 143 if type(tags) == str: 144 # we can iterate over the file, it doesn't have to be in a list first 145 tags = open(tags) 146 147 self.feed_init(**kwargs) 148 149 for line in tags: 150 if not _PYTHON_3000_ and type(line) is not unicode: 151 line = line.decode("utf-8") 152 if line[0] == '!': 153 # this is part of the file information header 154 line = line.strip() 155 elements = line.split('\t') 156 try: 157 self.__HEADER_ITEMS[elements[0]](self, elements[1:]) 158 except KeyError: 159 print ("Unknown header comment element " + elements[0] + " at line " + line_number + ".") 160 else: 161 self.feed_line(line) 162 163 self.feed_finish()
164
165 - def harvest(self, harvesters):
166 """ 167 Used to perform new data harvesters with already processed tags. 168 @param harvesters: harvester classes to apply to existing tags. 169 @type harvesters: list 170 @raises ValueError: if no tag data is available to process. 171 """ 172 173 if not len(self.tags): 174 raise ValueError("No tag data to harvest from.") 175 176 for h in harvesters: 177 h.do_before() 178 179 for tag in self.tags: 180 # order n^2 181 for h in harvesters: 182 h.feed(tag) 183 184 for h in harvesters: 185 h.do_after()
186 187
188 - def feed_init(self, **kwargs):
189 """ 190 Initializes ctags_file data members and possible data harvesters. 191 - B{Keyword Arguments:} 192 - B{harvesters:} (list) list of harvester classes 193 @raises ValueError: parsing error 194 """ 195 196 valid_kwargs = ['harvesters'] 197 validator.validate(kwargs.keys(), valid_kwargs) 198 199 self._clear_variables() 200 201 self.__feed_harvesters = list() 202 if 'harvesters' in kwargs: 203 self.__feed_harvesters = kwargs['harvesters'] 204 205 for h in self.__feed_harvesters: 206 h.do_before()
207
208 - def feed_line(self, tagline):
209 """ 210 Used to parse new ctags formatted output and new tags to the end of the tags list. 211 @param tagline: line from ctags output file 212 @type tagline: unicode str 213 """ 214 215 entry = ctags_entry(tagline) 216 self.tags.append(entry) 217 for h in self.__feed_harvesters: 218 h.feed(entry)
219
220 - def feed_finish(self):
221 """ Finalizes data harvesters from tag line feed. Drops references to harvesters.""" 222 for h in self.__feed_harvesters: 223 h.do_after() 224 # drop the references to the harvesters 225 self.__feed_harvesters = list()
226