Files now stored in ~/.Indexes

main
Graham Helton 2 years ago
parent 60888fe323
commit 5ec6daad56

@ -22,7 +22,7 @@ Currently the indexer has the following features
- ~~Only show CSVs when asking which index you want to work on~~ - ~~Only show CSVs when asking which index you want to work on~~
- ~~Add "Create new" option to csv selection option~~ - ~~Add "Create new" option to csv selection option~~
![](createCsv.gif) ![](createCsv.gif)
- Create a .sansindex file in home directory that stores indexes - ~~Create a .index file in home directory that stores indexes~~
- Add delete option for index files - Add delete option for index files
- ~~Add "ask for book" prompt on startup~~ - ~~Add "ask for book" prompt on startup~~
- ~~Allow for input with no definition (term, pagenumber)~~ - ~~Allow for input with no definition (term, pagenumber)~~

@ -1,6 +1,7 @@
#! /bin/python3 #! /bin/python3
import csv import csv
import os import os
from os.path import expanduser
import signal import signal
import pandas as pd import pandas as pd
from colorama import Fore, Style from colorama import Fore, Style
@ -8,7 +9,7 @@ from colorama import Fore, Style
headers = ['Title', 'Description','Page','book'] headers = ['Title', 'Description','Page','book']
index = [] index = []
row = [] row = []
dic = {} dictionary = {}
# Define formatting # Define formatting
reset = Style.RESET_ALL reset = Style.RESET_ALL
red = Fore.RED red = Fore.RED
@ -25,6 +26,14 @@ tickInfo = Fore.BLUE + "[" + yellow + "!" + Fore.BLUE + "]" + reset
print(tick, "Welcome to Sans Terminal Indexer") print(tick, "Welcome to Sans Terminal Indexer")
print(tick, "Enter" , green, "\"new book\"" , reset, "to change to the next book") print(tick, "Enter" , green, "\"new book\"" , reset, "to change to the next book")
book = input("Which book are you currently working on?\n") book = input("Which book are you currently working on?\n")
def path():
home = expanduser("~")
path = '/.Indexes/'
if not os.path.exists(home+path):
print("~/.Indexes Does not exist! Creating .Index directory in your home folder!")
os.mkdir(home+path)
os.chdir(home+path)
# Handle control+c # Handle control+c
def handler(signum, frame): def handler(signum, frame):
@ -34,53 +43,63 @@ signal.signal(signal.SIGINT, handler)
# Kick off program, check if any .csv files are in directory, if not, prompt user to create one # Kick off program, check if any .csv files are in directory, if not, prompt user to create one
def init(): def init():
path()
count = 0 count = 0
csvCheck = os.listdir() csvCheck = os.listdir()
for i in csvCheck:
if any(".csv" in c for c in csvCheck): # If a .csv file exists in the current directory (~.Indexes) then prompt user for which one they would like to use
print(tickInfo,"CSV Exists in current directory, which would you like to use? ") if any(".csv" in c for c in csvCheck):
print(tickInfo,"CSV Exists in current directory, which would you like to use? ")
for x in os.listdir(): # For each file that ends in .csv, add to dictionary, then for each value, assign a key from 1-n.
if x.endswith((".csv")): # This will allow the user to select a number that corresponds to the csv file.
count+=1 for x in os.listdir():
dic[count]=x if x.endswith((".csv")):
print(sep) count+=1
dictionary[count]=x
# For each value, assign a key from 1-n. This will allow the user to select a number print(sep)
# that corresponds to the csv file. # For each key and value, in the dictionary, print key, value for user to select from
for key, value in dic.items(): for key, value in dictionary.items():
print(green, key, red,'-->', cyan , value, reset) print(green, key, red,'-->', cyan , value, reset)
print(sep) print(sep)
print(green, "0", red, "-->" + cyan + " New index") print(green, "0", red, "-->" + cyan + " New index")
selection = input(tick + green + " Type the number of the file you would like\n" + reset)
# If selecing existing csv, exit function # Ask the user which file they would like
if selection != "0": selection = input(tick + green + " Type the number of the file you would like\n" + reset)
csvName = dic[int(selection)]
print(tick, "You chose: ", csvName) # If selecing existing csv, set csvName to choice and exit function
return csvName if selection != "0":
# If selection is to create a new CSV, carry on with creating it csvName = dictionary[int(selection)]
else: print(tick, "You chose: ", csvName)
csvName = input(tick + green + "Creating a new index, what do you want to name your CSV file?\n" + reset) return csvName
if not csvName.endswith(".csv"):
csvName += ".csv" # If selection is not to select a current CSV, create a new one
with open(csvName, 'a') as f: else:
write = csv.writer(f) csvName = input(tick + green + "Creating a new index, what do you want to name your CSV file?\n" + reset)
write.writerow(headers)
if not csvName.endswith(".csv"):
print(csvName) csvName += ".csv"
return csvName
with open(csvName, 'a') as f:
write = csv.writer(f)
write.writerow(headers)
print(csvName)
return csvName
def writeCsv(): def writeCsv():
with open(csvName, 'a') as f: with open(csvName, 'a') as f:
write = csv.writer(f) write = csv.writer(f)
write.writerows(index) write.writerows(index)
def readIndex(csvName): def readIndex(csvName):
print(tick, "Displaying abbreviated entries for",cyan, csvName,reset) print(tick, "Displaying abbreviated entries for",cyan, csvName,reset)
df = pd.read_csv(csvName) df = pd.read_csv(csvName)
#bottom = df.tail() #bottom = df.tail()
print(df) print(df)
# Call init function and assign csvName to a string
csvName = str(init()) csvName = str(init())
# Get user input # Get user input
while True: while True:
# Set index and row to null # Set index and row to null

Loading…
Cancel
Save