basant/TagHelper.py

69 lines
2.2 KiB
Python

import ctags
from ctags import CTags, TagEntry
import sys
from icecream import ic
import os
import shutil
import fileinput
def loadTagFile(path='.vimwiki_tags'):
# Available file information keys:
# opened - was the tag file successfully opened?
# error_number - errno value when 'opened' is false
# format - format of tag file (1 = original, 2 = extended)
# sort - how is the tag file sorted?
# author - name of author of generating program (may be empy string)
# name - name of program (may be empy string)
# url - URL of distribution (may be empy string)
# version - program version (may be empty string)
try:
tagFile = CTags(path)
return tagFile
except:
print("No tagfile at {}".farmat(path))
sys.exit(1)
def makeSetFromTags(tagFile, key='file'):
# Available TagEntry keys:
# name - name of tag
# file - path of source file containing definition of tag
# pattern - pattern for locating source line (None if no pattern)
# lineNumber - line number in source file of tag definition (may be zero if not known)
# kind - kind of tag (none if not known)
# fileScope - is tag of file-limited scope?
# Note: other keys will be assumed as an extension key and will
# return None if no such key is found
entry = TagEntry()
tag = tagFile.next(entry)
AllFileList = set()
while tag:
AllFileList.add(entry[key].decode())
tag = tagFile.next(entry)
return AllFileList
def makeDictFromTags(tagFile, key='name', value='file'):
entry = TagEntry()
tag = tagFile.next(entry)
ret = {}
while tag:
# Not sure why they're being read as bytes now?
strKey = entry[key].decode()
strValue = entry[value].decode()
if strKey not in ret.keys():
ret[strKey] = set(strValue)
else:
ret[strKey].add(strValue)
tag = tagFile.next(entry)
return ret
def getAllFiles():
# Returns a set containing all the files we wish to make public
return makeSetFromTags(loadTagFile())
def getAllTags():
# Returns a dictionary with:
# - key as tag titles
# - values as files that have the key in their tags
return makeDictFromTags(loadTagFile())