This commit is contained in:
Anish Lakhwara 2021-12-22 17:29:08 +10:00
parent c972d1f15e
commit 2d2c686da8
2 changed files with 92 additions and 11 deletions

View file

@ -59,10 +59,13 @@ def makeDictFromTags(tagFile, key='name', value='file'):
def getAllFiles():
# Returns a set containing all the files we wish to make public
return makeSetFromTags(loadTagFile(tagFile))
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())
if __name__ == '__main__':
print(getAllTags().keys())

View file

@ -1,6 +1,5 @@
#! /usr/bin/env python3
import ctags
from ctags import CTags, TagEntry
import sys
from icecream import ic
@ -8,7 +7,7 @@ import os
import shutil
import fileinput
def loadTagFile(path='.vimwiki_tags'):
def loadTagFile(path='/home/anish/vimwiki/.vimwiki_tags'):
# Available file information keys:
# opened - was the tag file successfully opened?
# error_number - errno value when 'opened' is false
@ -22,9 +21,28 @@ def loadTagFile(path='.vimwiki_tags'):
tagFile = CTags(path)
return tagFile
except:
print("No tagfile at {}".farmat(path))
print("No tagfile at {}".format(path))
sys.exit(1)
bad_tags = [('Public', 'public'), ('Travel', 'travel'), ('jrl', 'jrnl'), ('Kitaab', 'kitaab'), ('Dream', 'dream'), ('Story', 'story'), ('Review', 'review'), ('private', 'priv'), ('ideas', 'idea'), ('quotes', 'quote'), ('rocks', 'rock'), ('sec', 'cybsec'), ('sealight.services', 'sealight')]
bad_tags_only = [x[0] for x in bad_tags]
def replaceBadTags(tagFile):
# no higher order function sadness
# this does the same thing as loopThrough but runs a different function
# (loop through looks for single character tags)
# We look for any tag in the list and replace it
entry = TagEntry()
tag = tagFile.next(entry)
tagErrors = set()
print(bad_tags_only)
while tag:
tag = entry['name'].decode('utf-8')
if tag in bad_tags_only:
tagErrors.add(entry['file'])
tag = tagFile.next(entry)
return tagErrors
def loopThrough(tagFile):
# Available TagEntry keys:
# name - name of tag
@ -46,12 +64,36 @@ def loopThrough(tagFile):
if len(entry['name']) <= 1:
tagErrors.add(entry['file'])
errorTagNames.add(entry['name'])
else:
print("Good tag found: {}".format(entry['name']))
tag = tagFile.next(entry)
ic(errorTagNames)
return tagErrors
def fixBadTags(errors):
''' More lack of higher order function sadness
same as fixErrors, but not really
'''
fileBase = '/home/anish/vimwiki/'
for v in errors:
print("Fixing {}...".format(v))
fileName = fileBase + getFileName(v)
newFile = ""
with open(fileName, 'r') as f:
newFile = newFile + f.readline()
line = f.readline()
# bad and ungly
for bad_tag in bad_tags:
line = line.replace(bad_tag[0], bad_tag[1])
newFile = newFile + line
for line in f.readlines():
newFile = newFile + line
with open(fileName, 'w') as f:
for line in newFile:
f.write(line)
print("Written new file...")
print()
print(newFile)
def fixErrors(errors):
''' Replaces :: with :, which seems to be the error for many of the tags '''
fileBase = '/home/anish/vimwiki/'
@ -61,6 +103,19 @@ def fixErrors(errors):
newFile = ""
with open(fileName, 'r') as f:
for line in f.readlines():
line = line.replace(":]:]:", "")
line = line.replace(":[:[:", "")
line = line.replace("G:r:a:t:i:t:u:d:e:", ":grat:")
line = line.replace(":J:o:u:r:n:a:l:", ":jrnl:")
line = line.replace(":R:e:v:i:e:w:", ":review:")
line = line.replace(":T:r:a:v:e:l:", ":travel:")
line = line.replace(":D:r:e:a:m:", ":dream:")
line = line.replace("%date", "\n%date")
line = line.replace("{date}", createDateStringFromFile(v)+"\n\n")
line = line.replace("{title}", "{title}\n")
line = line.replace(" :", ":")
line = line.replace("::", ":")
line = line.replace(" :", ":")
line = line.replace("::", ":")
newFile = newFile + line
with open(fileName, 'w') as f:
@ -70,15 +125,37 @@ def fixErrors(errors):
print()
print(newFile)
def printErrors(errors):
def createDateStringFromFile(error):
file = getFileName(error).replace("jrnl/", "").replace(".wiki", "")
dateConverted = file.split('-') # create date and time
dateConvertedSplit = list(dateConverted[0]) # splite date
dateConvertedSplit.insert(2, '-') # add - between year and month
dateConvertedSplit.insert(5, '-') # add - between month and day
dateConvertedSplit.insert(0, '20') # add 20 to year
dateConverted[0] = "".join(dateConvertedSplit) # make date a string
dateConverted[1] = dateConverted[1][:2] + ":" + dateConverted[1][2:] # add colon in between hour and minute
dateConverted = " ".join(dateConverted) # make date and time a string
return dateConverted
def printErrorFiles(errors):
fileBase = '/home/anish/vimwiki/'
for v in errors:
fileName = fileBase + getFileName(v)
print("Reading {}...".format(v))
with open(fileName, 'r') as f:
f.readline()
#f.readline()
ic(f.readline())
def listTags(tagFile):
entry = TagEntry()
tag = tagFile.next(entry)
allTags = set()
while tag:
tag = entry['name'].decode('utf-8')
allTags.add(tag)
tag = tagFile.next(entry)
return allTags
def getFileName(f):
return str(f, encoding="utf-8")
@ -93,8 +170,9 @@ def getBackupFiles(errors):
if __name__=='__main__':
tagFile = loadTagFile()
errors = loopThrough(tagFile)
printErrors(errors)
#ic(tagFile)
#errors = replaceBadTags(tagFile)
#fixBadTags(errors)
print(sorted(listTags(tagFile)))
#ic(errors)
#getBackupFiles(errors)
#fixErrors(errors)