1616#
1717
1818TAGS_RE = re .compile (
19- r' (?P<symbol>[^\t]+)\t'
20- r' (?P<filename>[^\t]+)\t'
19+ r" (?P<symbol>[^\t]+)\t"
20+ r" (?P<filename>[^\t]+)\t"
2121 r'(?P<ex_command>(/.+/|\?.+\?|\d+));"\t'
22- r' (?P<type>[^\t\r\n]+)'
23- r' (?:\t(?P<fields>.*))?'
22+ r" (?P<type>[^\t\r\n]+)"
23+ r" (?:\t(?P<fields>.*))?"
2424)
2525
2626# column indexes
2727SYMBOL = 0
2828FILENAME = 1
2929
30- MATCHES_STARTWITH = ' starts_with'
30+ MATCHES_STARTWITH = " starts_with"
3131
3232PATH_ORDER = [
33- 'function' , 'class' , 'struct' ,
33+ "function" ,
34+ "class" ,
35+ "struct" ,
3436]
3537
36- PATH_IGNORE_FIELDS = (
37- 'file' , 'access' , 'signature' , 'language' , 'line' , 'inherits' )
38+ PATH_IGNORE_FIELDS = ("file" , "access" , "signature" , "language" , "line" , "inherits" )
3839
39- TAG_PATH_SPLITTERS = ('/' , '.' , '::' , ':' )
40+ TAG_PATH_SPLITTERS = ("/" , "." , "::" , ":" )
4041
4142#
4243# Functions
4344#
4445
4546# Helper functions
4647
48+
4749def splits (string , * splitters ):
4850 """
4951 Split a string on a number of splitters.
@@ -61,9 +63,11 @@ def splits(string, *splitters):
6163 if string :
6264 yield string
6365
66+
6467# Tag processing functions
6568
66- def parse_tag_lines (lines , order_by = 'symbol' , tag_class = None , filters = None ):
69+
70+ def parse_tag_lines (lines , order_by = "symbol" , tag_class = None , filters = None ):
6771 """
6872 Parse and sort a list of tags.
6973
@@ -88,7 +92,7 @@ def parse_tag_lines(lines, order_by='symbol', tag_class=None, filters=None):
8892 if isinstance (line , Tag ): # handle both text and tag objects
8993 line = line .line
9094
91- line = line .rstrip (' \r \n ' )
95+ line = line .rstrip (" \r \n " )
9296
9397 search_obj = TAGS_RE .search (line )
9498
@@ -116,6 +120,7 @@ def parse_tag_lines(lines, order_by='symbol', tag_class=None, filters=None):
116120
117121 return tags_lookup
118122
123+
119124def post_process_tag (tag ):
120125 """
121126 Process 'EX Command'-related elements of a tag.
@@ -160,12 +165,13 @@ def post_process_tag(tag):
160165 """
161166 tag .update (process_fields (tag ))
162167
163- tag [' ex_command' ] = process_ex_cmd (tag )
168+ tag [" ex_command" ] = process_ex_cmd (tag )
164169
165170 tag .update (create_tag_path (tag ))
166171
167172 return tag
168173
174+
169175def process_ex_cmd (tag ):
170176 """
171177 Process the 'ex_command' element of a tag dictionary.
@@ -177,12 +183,13 @@ def process_ex_cmd(tag):
177183
178184 :returns: updated 'ex_command' dictionary entry
179185 """
180- ex_cmd = tag .get (' ex_command' )
186+ ex_cmd = tag .get (" ex_command" )
181187
182188 if ex_cmd .isdigit (): # if a line number, do nothing
183189 return ex_cmd
184- else : # else a regex, so unescape
185- return re .sub (r"\\(\$|/|\^|\\)" , r'\1' , ex_cmd [2 :- 2 ]) # unescape regex
190+ else : # else a regex, so unescape
191+ return re .sub (r"\\(\$|/|\^|\\)" , r"\1" , ex_cmd [2 :- 2 ]) # unescape regex
192+
186193
187194def process_fields (tag ):
188195 """
@@ -197,19 +204,20 @@ def process_fields(tag):
197204 :returns: dict containing the key-value pairs from the field element, plus
198205 a list of keys for said pairs
199206 """
200- fields = tag .get (' fields' )
207+ fields = tag .get (" fields" )
201208
202209 if not fields : # do nothing
203210 return {}
204211
205212 # split the fields string into a dictionary of key-value pairs
206- result = dict (f .split (':' , 1 ) for f in fields .split (' \t ' ))
213+ result = dict (f .split (":" , 1 ) for f in fields .split (" \t " ))
207214
208215 # append all keys to the dictionary
209- result [' field_keys' ] = sorted (result .keys ())
216+ result [" field_keys" ] = sorted (result .keys ())
210217
211218 return result
212219
220+
213221def create_tag_path (tag ):
214222 """
215223 Create a tag path entry for a tag dictionary.
@@ -226,9 +234,9 @@ def create_tag_path(tag):
226234
227235 :returns: dict containing the 'tag_path' entry
228236 """
229- field_keys = tag .get (' field_keys' , [])[:]
237+ field_keys = tag .get (" field_keys" , [])[:]
230238 fields = []
231- tag_path = ''
239+ tag_path = ""
232240
233241 # sort field arguments related to path order in correct order
234242 for field in PATH_ORDER :
@@ -242,22 +250,23 @@ def create_tag_path(tag):
242250 # convert list of fields to dot-joined string, dropping any "ignore" fields
243251 for field in fields :
244252 if field not in PATH_IGNORE_FIELDS :
245- tag_path += ( tag .get (field ) + '.' )
253+ tag_path += tag .get (field ) + "."
246254
247255 # append symbol as last item in string
248- tag_path += tag .get (' symbol' )
256+ tag_path += tag .get (" symbol" )
249257
250258 # split string on seperators and append tag filename to resulting list
251- splitup = ([tag .get ('filename' )] +
252- list (splits (tag_path , * TAG_PATH_SPLITTERS )))
259+ splitup = [tag .get ("filename" )] + list (splits (tag_path , * TAG_PATH_SPLITTERS ))
253260
254261 # convert list to tuple
255- result = {' tag_path' : tuple (splitup )}
262+ result = {" tag_path" : tuple (splitup )}
256263
257264 return result
258265
266+
259267# Tag building/sorting functions
260268
269+
261270def build_ctags (path , cmd = None , tag_file = None , recursive = False , opts = None ):
262271 """
263272 Execute the ``ctags`` command using ``Popen``.
@@ -275,19 +284,20 @@ def build_ctags(path, cmd=None, tag_file=None, recursive=False, opts=None):
275284 if cmd :
276285 cmd = [cmd ]
277286 else :
278- cmd = [' ctags' ]
287+ cmd = [" ctags" ]
279288
280289 if not os .path .exists (path ):
281- raise IOError ('\' path\' is not at valid directory or file path, or '
282- 'is not accessible' )
290+ raise IOError (
291+ "'path' is not at valid directory or file path, or " "is not accessible"
292+ )
283293
284294 if os .path .isfile (path ):
285295 cwd = os .path .dirname (path )
286296 else :
287297 cwd = path
288298
289299 if tag_file :
290- cmd .append (' -f {0}' .format (tag_file ))
300+ cmd .append (" -f {0}" .format (tag_file ))
291301
292302 if opts :
293303 if type (opts ) == list :
@@ -296,24 +306,25 @@ def build_ctags(path, cmd=None, tag_file=None, recursive=False, opts=None):
296306 cmd .append (opts )
297307
298308 if recursive : # ignore any file specified in path if recursive set
299- cmd .append ('-R' )
309+ cmd .append ("-R" )
300310 elif os .path .isfile (path ):
301311 filename = os .path .basename (path )
302312 cmd .append (filename )
303313 else : # search all files in current directory
304- cmd .append (os .path .join (path , '*' ))
314+ cmd .append (os .path .join (path , "*" ))
305315
306316 # workaround for the issue described here:
307317 # http://bugs.python.org/issue6689
308- if os .name == ' posix' :
309- cmd = ' ' .join (cmd )
318+ if os .name == " posix" :
319+ cmd = " " .join (cmd )
310320
311321 # execute the command
312- check_output (cmd , cwd = cwd , shell = True , stdin = subprocess .PIPE ,
313- stderr = subprocess .STDOUT )
322+ check_output (
323+ cmd , cwd = cwd , shell = True , stdin = subprocess .PIPE , stderr = subprocess .STDOUT
324+ )
314325
315326 if not tag_file : # Exuberant ctags defaults to ``tags`` filename.
316- tag_file = os .path .join (cwd , ' tags' )
327+ tag_file = os .path .join (cwd , " tags" )
317328 else :
318329 if os .path .dirname (tag_file ) != cwd :
319330 tag_file = os .path .join (cwd , tag_file )
@@ -323,6 +334,7 @@ def build_ctags(path, cmd=None, tag_file=None, recursive=False, opts=None):
323334
324335 return tag_file
325336
337+
326338def resort_ctags (tag_file ):
327339 """
328340 Rearrange ctags file for speed.
@@ -351,45 +363,51 @@ def resort_ctags(tag_file):
351363 """
352364 groups = {}
353365
354- with codecs .open (tag_file , encoding = ' utf-8' , errors = ' replace' ) as file_ :
366+ with codecs .open (tag_file , encoding = " utf-8" , errors = " replace" ) as file_ :
355367 for line in file_ :
356368 # meta data not needed in sorted files
357- if line .startswith (' !_TAG' ):
369+ if line .startswith (" !_TAG" ):
358370 continue
359371
360- # read all valid symbol tags, which contain at least
372+ # read all valid symbol tags, which contain at least
361373 # symbol name and containing file and build a list of tuples
362- split = line .split (' \t ' , FILENAME + 1 )
374+ split = line .split (" \t " , FILENAME + 1 )
363375 if len (split ) > FILENAME :
364376 groups .setdefault (split [FILENAME ], []).append (line )
365377
366- with codecs .open (tag_file + '_sorted_by_file' , 'w' , encoding = 'utf-8' ,
367- errors = 'replace' ) as file_ :
378+ with codecs .open (
379+ tag_file + "_sorted_by_file" , "w" , encoding = "utf-8" , errors = "replace"
380+ ) as file_ :
368381 for group in sorted (groups ):
369382 file_ .writelines (groups [group ])
370383
384+
371385#
372386# Models
373387#
374388
389+
375390class TagElements (dict ):
376391 """
377392 Model the entries of a tag file.
378393 """
394+
379395 def __init__ (self , * args , ** kw ):
380396 """Initialise Tag object"""
381397 dict .__init__ (self , * args , ** kw )
382398 self .__dict__ = self
383399
400+
384401class Tag (object ):
385402 """
386403 Model a tag.
387404
388405 This exists mainly to enable different types of sorting.
389406 """
407+
390408 def __init__ (self , line , column = 0 ):
391409 if isinstance (line , bytes ): # python 3 compatibility
392- line = line .decode (' utf-8' , ' replace' )
410+ line = line .decode (" utf-8" , " replace" )
393411 self .line = line
394412 self .column = column
395413
@@ -406,15 +424,16 @@ def __gt__(self, other):
406424 return False
407425
408426 def __getitem__ (self , index ):
409- return self .line .split (' \t ' , self .column + 1 )[index ]
427+ return self .line .split (" \t " , self .column + 1 )[index ]
410428
411429 def __len__ (self ):
412- return self .line .count (' \t ' ) + 1
430+ return self .line .count (" \t " ) + 1
413431
414432 @property
415433 def key (self ):
416434 return self [self .column ]
417435
436+
418437class TagFile (object ):
419438 """
420439 Model a tag file.
@@ -425,6 +444,7 @@ class TagFile(object):
425444 searching for a retrieving tags, finding tags based on given criteria
426445 (prefix, suffix, exact), getting the directory of a tag and so forth.
427446 """
447+
428448 file_o = None
429449 mapped = None
430450
@@ -489,9 +509,8 @@ def open(self):
489509 """
490510 Open file.
491511 """
492- self .file_o = codecs .open (self .path , 'r+b' , encoding = 'utf-8' )
493- self .mapped = mmap .mmap (self .file_o .fileno (), 0 ,
494- access = mmap .ACCESS_READ )
512+ self .file_o = codecs .open (self .path , "r+b" , encoding = "utf-8" )
513+ self .mapped = mmap .mmap (self .file_o .fileno (), 0 , access = mmap .ACCESS_READ )
495514
496515 def close (self ):
497516 """
@@ -555,20 +574,22 @@ def tag_class(self):
555574 accessed as class variables (i.e. ``class.variable``, rather than
556575 ``dict['variable'])
557576 """
558- return type (' TagElements' , (TagElements ,), dict (root_dir = self .dir ))
577+ return type (" TagElements" , (TagElements ,), dict (root_dir = self .dir ))
559578
560579 def get_tags_dict (self , * tags , ** kw ):
561580 """
562581 Return the tags from a tag file as a dict.
563582 """
564- filters = kw .get ('filters' , [])
565- return parse_tag_lines (self .search (True , * tags ),
566- tag_class = self .tag_class (), filters = filters )
583+ filters = kw .get ("filters" , [])
584+ return parse_tag_lines (
585+ self .search (True , * tags ), tag_class = self .tag_class (), filters = filters
586+ )
567587
568588 def get_tags_dict_by_suffix (self , suffix , ** kw ):
569589 """
570590 Return the tags with the given suffix of a tag file as a dict.
571591 """
572- filters = kw .get ('filters' , [])
573- return parse_tag_lines (self .search_by_suffix (suffix ),
574- tag_class = self .tag_class (), filters = filters )
592+ filters = kw .get ("filters" , [])
593+ return parse_tag_lines (
594+ self .search_by_suffix (suffix ), tag_class = self .tag_class (), filters = filters
595+ )
0 commit comments