add a parameter to list existing tags

This patch adds support for "-l" or "--list" or "-list" parameters to
read and list unique tags from tags.csv
This commit is contained in:
MrOtherGuy 2022-02-04 19:30:18 +02:00
parent f0caa6d5ae
commit ef7a9c73f7

View file

@ -3,14 +3,22 @@
# #
# usage: # usage:
# add_style.py <file_name> <tags> # add_style.py <file_name> <tags>
# where <tags> is a space-separated list of tags to apply for that style (1 minimum) #
# where <tags> is a space-separated list of tags to apply
# for that style (1 minimum). Creates a new file to
# chrome/ folder and updates tags.csv and docs/tagmap.json
#
# OR # OR
# add_style-py --update-only
# #
# When called with <file_name> <tags> it will create a new file to chrome/ folder and # add_style.py --update-only
# update tags.csv and docs/tagmap.json
# #
# With --update-only it will only update docs/tagmap.json based on tags.csv # updates docs/tagmap.json based on tags.csv without creating anything
#
# OR
#
# add_style.py --list
#
# shows a list of unique tags used in tags.csv
import sys, os import sys, os
@ -79,15 +87,40 @@ def createNewFile(name,folder):
print("Created file: {}".format(filesuffix)) print("Created file: {}".format(filesuffix))
return True return True
def isUpdateOnly(argv):
return (argv[1] == "--update-only") or (argv[1] == "-update-only") or (argv[1] == "-u") class TaskMode:
def __init__(self,args):
self.show_help = len(args) < 2 or (args[1] == "-h")
if self.show_help:
self.update_only = False
self.list_tags = False
self.normal = False
else:
self.update_only = (args[1] == "--update-only") or (args[1] == "-update-only") or (args[1] == "-u")
self.list_tags = (args[1] == "--list") or (args[1] == "-list") or (args[1] == "-l")
self.normal = not self.update_only and (not self.list_tags)
self.min_arg_length = (3 if self.normal else 2)
def printCurrentTags(filecontent):
tags = [];
for line in filecontent:
tokens = line.rsplit(",")
filterEmpty(tokens)
if len(tokens) < 2:
continue
for t in range(1,len(tokens)):
if tags.count(tokens[t]) == 0:
tags.append(tokens[t])
tags.sort()
print("\n".join(tags))
if __name__ == "__main__": if __name__ == "__main__":
update_only = isUpdateOnly(sys.argv) runmode = TaskMode(sys.argv)
if len(sys.argv) < (2 + (0 if update_only else 1)): if runmode.show_help or len(sys.argv) < runmode.min_arg_length:
print("usage: add_file.py <filename> <list_of_tags>") print("usage: add_file.py <filename> <list_of_tags>")
exit() exit()
@ -97,12 +130,12 @@ if __name__ == "__main__":
name = sys.argv[1] name = sys.argv[1]
if not(update_only) and not(name.endswith(".css")): if not(runmode.update_only) and not(name.endswith(".css")):
name += ".css" name += ".css"
# For the moment files in content/ are not tagged, but the script can still create the css files # For the moment files in content/ are not tagged, but the script can still create the css files
folder = "content" if ("-content" in sys.argv) else "chrome" folder = "content" if ("-content" in sys.argv) else "chrome"
if(folder == "content") and not(update_only): if(folder == "content") and not(runmode.update_only):
createNewFile(name,folder) createNewFile(name,folder)
print("Done") print("Done")
exit(0) exit(0)
@ -111,16 +144,22 @@ if __name__ == "__main__":
tagmap = tagfile.read().lstrip().splitlines() tagmap = tagfile.read().lstrip().splitlines()
tagfile.close() tagfile.close()
if not(update_only) and searchFile(tagmap,name): if runmode.normal and searchFile(tagmap,name):
print(name + "exist already") print(name + "exist already")
else: else:
if not(update_only): if runmode.normal:
exists = createNewFile(name,folder) exists = createNewFile(name,folder)
tagfile = open("tags.csv","a") tagfile = open("tags.csv","a")
tagfile.write(name+","+",".join(args)+"\n") tagfile.write(name+","+",".join(args)+"\n")
tagfile.close() tagfile.close()
else: createJSON(tagmap,name,args,runmode.update_only)
elif runmode.update_only:
print("Only update json") print("Only update json")
createJSON(tagmap,name,args,update_only) createJSON(tagmap,name,args,runmode.update_only)
print("Done") elif runmode.list_tags:
print("listing tags...")
printCurrentTags(tagmap)
else:
print("this shouldn't happen!")
# print("Done")
exit(0) exit(0)