now verifies initialization before running commands. plus minor changes.

This commit is contained in:
= 2022-05-30 02:22:25 +05:30
parent 237f027c1b
commit fa77564b44
1 changed files with 23 additions and 3 deletions

26
gempost
View File

@ -11,8 +11,6 @@ from sys import argv
from hashlib import sha1
from random import random
import curses
# colored output functions
def prRed(skk): print("\033[91m {}\033[00m" .format(skk))
def prGreen(skk): print("\033[92m {}\033[00m" .format(skk))
@ -29,6 +27,25 @@ postDir = wdir + "postdir/" # all post files along with the archive.gmi are stor
indexFile = wdir + "postIndex" # the index is maintained in this file
editor = "nano"
def checkInit():
""" Checks if the public_gemini/ directory has been initialized for use with gempost or is missing some files. """
contents = listdir(wdir)
valid_contents = ["postIndex","trash","postdir"]
validity = 0
missing = []
for i in contents:
if i in valid_contents:
validity += 1
else:
missing.append(i)
if (validity != len(valid_contents)):
if (validity == 0):
prRed(f'\n Your "public_gemini/" directory has not been initialized yet. Use "gempost qs" to read the quickstart.')
else:
prRed(f'\n Your "public_gemini/" directory is missing some required files and directories / has not been properly initialized.\n\n The following files/directories are missing: {missing}\n Validity score: {validity}/{len(valid_contents)}')
exit()
def rebuildReferences(filename = None, title = None, callingFrom = "default"):
""" reads {wdir}postIndex and rebuilds the {wdir}posts.gmi and {postDir}archive.gmi files """
customized = False
@ -170,7 +187,7 @@ def manage():
exec(f"sed -i '/{postsList[which][1]}.gmi/d' {indexFile}")
print(f"postIndex updated")
rebuildReferences(None, None, "delete")
print("rebuilt post.gmi, archive.gmi")
print("rebuilt posts.gmi, archive.gmi")
prGreen("All done.")
elif (mode == 3):
print("\nCancelled.")
@ -270,6 +287,7 @@ elif (arg == "qs"):
print(quickStart) # a simple 2-step quick start for new users
elif (arg == "post"):
checkInit()
if (len(argv) == 3): # checking if more than 1 arg (except python) to see if source file has been supplied
filePath = argv[2] # storing that file's path in a var
print(f"Using file {filePath}") # tell the user that the program is using a source file
@ -300,9 +318,11 @@ elif (arg == "post"):
newpost(title)
elif (arg == "manage"):
checkInit()
manage()
elif (arg == "purge"): # permanently delete trashed posts
checkInit()
if (len(listdir(f"{wdir}trash/")) == 0):
prYellow("Trash is already empty.")
else: