Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengliw committed Jun 13, 2022
1 parent 7da33ae commit 2481506
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions mte
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/python3

# MakeThingsEasier - mte
# A command line utility, more in the README

###
# Importing needed libs
###

from os import system # for command running
from sys import argv # for command-line arguments

###
# The APIs
###

def runAction(keyconfig, action): # function for running the action
'''Runs the action defined by keyconfig.
Returns True if action is defined in keyconfig and being run with system(),
returns False if action is not defined and not being run with system()'''

if action != '': # only do something if the user really typed something
try:
system(keyconfig[action]) # do the action
except KeyError: # if the action is not defined
print("The action is not defined.")
return False

return True # everything worked lol

def readConfig(filename, config): # config reading function
'''Modifies a dict to override the config key values defined before
or creates new ones if the keys don't exist'''

configFile = open(filename, "r") # Open the config file in read-only mode

for line in configFile: # for each line in configFile
if line[0] != '#' and '=' in line: # only read key and value if the line is not a comment line
# or trash line (no equal sign in line)

configLine = line.split('=') # configLine[0] = the key
# configLine[1] = the value

configLine[1] = configLine[1].strip() # remove whitespaces when saving config

config[configLine[0]] = configLine[1] # set key to value

configFile.close()

###
# Defining the variables that require pre-definining for the use case
###

runFromArgvSuccess = False # store if running the action from argv succeeded

###
# Defining default configuration
# or defining the dictionaries
###

generalconfig = {
'showWarnings': '1',
'exitAfterArgvSuccess': '1'
}

keyconfig = {}

###
# Reading the configurations
# Overrides default config defined above
###

readConfig("keyconfig.txt", keyconfig)
readConfig("config.txt", generalconfig)

###
# "Actual code"
###

if len(argv) != 1: # if there is a command line argument
runFromArgvSuccess = runAction(keyconfig, argv[1]) # try to use the command line argument

try:
# if running action from argv failed or no argv passed at all
# so runFromArgvSuccess stays False
# AND the config.txt says exitAfterArgvSuccess is 0
while not runFromArgvSuccess and generalconfig['exitAfterArgvSuccess'] == '0':
action = input(">>> ") # get user input to know what the user wants to do
runAction(keyconfig, action)


except KeyboardInterrupt: # if user wants to leave
print("\nBye bye :)") # bye guy

0 comments on commit 2481506

Please sign in to comment.