#!/usr/bin/python3
from sys import stdin
import argparse
import logging
from pathlib import Path
from rich import print
from rich . console import Console
from rich . markdown import Markdown
from rich . logging import RichHandler
from rich . traceback import install
# Global Variables (Making things easy for myself)
# ====================================================================
__version__ = " 1.0.0 "
logging . basicConfig ( level = " NOTSET " , format = " %(message)s " ,
datefmt = " [ %X ] " , handlers = [ RichHandler ( ) ] )
log = logging . getLogger ( " rich " )
# install()
console = Console ( )
data_location = Path . joinpath ( Path . home ( ) , " shortcut-learner.md " )
# ====================================================================
def parse_arguments ( ) :
parser = argparse . ArgumentParser (
" Shortcut Learner " )
parser . add_argument ( " -v " , " --version " , action = " version " ,
version = ' %(prog)s : {version} ' . format ( version = __version__ ) )
parser . add_argument ( " -a " , " --append " ,
help = " Append a shortcut to your collection using Markdown. " )
parser . add_argument ( " -d " , " --delete " , action = " store_true " ,
help = " Deletes file storing your shortcuts list. " )
args = parser . parse_args ( )
return args
def create_new_file ( ) :
print ( " Create new file [b](Y/n)[/b]? " , end = " " )
create = stdin . readline ( 1 ) . strip ( )
if create == " y " or not create :
log . info ( f " Creating new data file at: { data_location } " )
try :
with open ( f " { data_location } " , " w+ " ) as new_file :
new_file . write ( " # Shortcuts \n \n " )
new_file . write (
" The shortcuts I intend to learn are as follows: \n \n " )
except IOError :
log . critical ( " Data file could not be created. " )
console . print_exception ( )
except Exception as e :
log . critical ( " Unrecoverable system error. " )
console . print_exception ( )
elif create == " n " :
log . info ( " No data file created. Exiting program. " )
else :
log . error ( " Invalid argument. Please enter ' y ' or ' n ' . Exiting program. " )
def load_file ( ) :
try :
with open ( data_location ) as data_file :
markdown = Markdown ( data_file . read ( ) )
console . print ( markdown )
except IOError :
log . warning ( " Data file cannot be found. " )
create_new_file ( )
except Exception as e :
log . critical ( " Unrecoverable system error. " )
console . print_exception ( )
def append_to_file ( data ) :
log . info ( f " \" { data } \" added to file. " )
log . info ( " Opening data file... " )
try :
with open ( data_location , " a " ) as data_file :
data_file . write ( f " 1. { data } \n \n " )
except IOError :
log . warning ( " Data file cannot be found. " )
create_new_file ( )
except Exception as e :
log . critical ( " Unrecoverable system error. " )
console . print_exception ( )
def delete_file ( ) :
try :
log . warning ( f " Deleting file { data_location } ... " )
Path . unlink ( data_location )
log . info ( " File deleted. " )
except IOError :
log . info ( " No file was found. Nothing deleted. " )
except Exception as e :
log . critical ( " Unrecoverable system error. " )
console . print_exception ( )
def main ( ) :
args = parse_arguments ( )
if args . delete is False :
if args . append is None :
load_file ( )
else :
append_to_file ( args . append )
load_file ( )
else :
delete_file ( )
if __name__ == " __main__ " :
main ( )