From 2481506d8f14742efcfe34c83a6a2cfa575833fa Mon Sep 17 00:00:00 2001 From: Zhengli Wang <96080426+zhengliw@users.noreply.github.com> Date: Mon, 13 Jun 2022 16:57:59 +0200 Subject: [PATCH] Add files via upload --- mte | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 mte diff --git a/mte b/mte new file mode 100644 index 0000000..d5d1406 --- /dev/null +++ b/mte @@ -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 \ No newline at end of file