3
\$\begingroup\$

I try to get into Python and thought I build together my own IDE. Most of it consists of different addons for vim and changes in the vimrc (which of course I simply copied of google), but I also needed a way of executing the main.py file, even when I am working on some other file of the same project. So, since I learned bash scripting fairly recently, I thought I hack together a script doing that for me.

To be honest, it grew a lot longer than I'd have expected (and took much longer, too). I think I tried to give it a little more of a serious look (minus the one or other 'bloody' in error messages that should never show).

So, main question: Where can I streamline it and did I use totally 'hacky' solutions which I should definitely not use again?

### runpy ###

# Config #

# Config holds 4 options. Set them to one to activate them and set them 0 to deactivate them

# use Python Version 
# Choose 2 for python2 
# Choose 3 for python3
usePython=3

# Same Terminal 
# This starts python in the very terminal window where you run the command
# If you want to change this to an external terminal emulator, please also
# add the fitting attribute to pass along a command. E.g.: 'xterm -e
# Important: Leave the final apostrophe open
sameTerminal=1
useTerminal="osascript -e 'tell application \"Terminal\" to do script "

# Interactive Mode
# Adds the "-i" option to the python command.
interactiveMode=0

# This file
# When activated, the file you're specifing in your working path will be chosen. If not the application
# will look for a 'main.py' file. Hereby is assumed that the file is exactly named 'main.py' and 
# that it can be found in the same or the parent director. Sibling and subdirectories are ignored.
thisFile=0

# Declaring Variables
# Some Variables have already been declared in the config above.
workPath=""
usePython2=0
usePython3=0
mainPyFile=()
running=""

# Functions #

# Show Usage
showUsage () {
    printf "%s\n" "This script is desinged to be part of a vim based IDE for Python. It will have a few base assumptions." "Assumptions:" "  1. This script is started from within vim." "  2. You're sorting your scripts like this '/differentFolderWithProjects/mainFolderWithMainFile/SubFolderForAdditionalRessources'. If that's not your system, this script won't work." "" "Usage: runpy [args] {path}" "Options:" "  -2 Use Python2" "  -3 Use Python3" "  -t (thisFile) Run the file I am working on rather than the main.py file" "  -s Use the same Terminal rather than a new window. This option is broken right now, same window is set on standard. Don't use the -s switch, you will only get an error message and the script will end without running anything." "  -h Display this help screen and ends the script no matter what other attributes were given." 
}

# Switch on or off attributes function
switch () {
    switch=$1
    if [[ "$switch" -eq 0 ]]; then
            return 1
        elif [[ "$switch" -eq 1 ]]; then
        return 0
    else
        printf "%s\n" "How the fuck did you screw up this part of the script?" "Error in function Switch when passing attribute $switch."
    fi      
}

# Parsing for the main.py file
parseMainFile () {
    while read -r file; do
        if [[ -f $file ]]; then
            mainPyFile[${#mainPyFile[@]}]="$file"
        fi
    done <<< $(find $(dirname $workPath) -name *main.py -maxdepth 1)

    while read -r file; do
        if [[ -f $file ]]; then
            mainPyFile[${#mainPyFile[@]}]="$file"
        fi
    done <<< $(find $(dirname $workPath)/.. -name *main.py -maxdepth 1)

    if [[ "${#mainPyFile[@]}" -gt 1 ]]; then
        printf "%s\n" "We found ${#mainPyFile[@]} main.py files. Only one allowed." "main.py file locations:"
        while read -r file; do 
            printf "%s\n" "$file"
        done <<< ${mainPyFile[@]}
        exit 1
    elif [[ "${#mainPyFile[@]}" -eq 1 ]]; then
        workPath=${mainPyFile[0]}
    fi
}

# Running the file
run() {
    case $usePython in
        2)  running="python"
            ;;
        3)  running="python3"
            ;;
        *)  printf "%s\n" "How the bloody hell did you fuck this up right here?" "Error in function run"
            exit 1
            ;;  
    esac

    if (( $interactiveMode )); then
        running="$running -i"   
    fi

    running="$running $workPath"

    case $sameTerminal in
        0)  ### running="$useTerminal\"$running\"'"
            ### $running
            printf "%s\n" "The -s option is broken at the moment and might be fixed to a later point" "Might be not fixed at all and simply removed due to me finding http://mywiki.wooledge.org/BashFAQ/050." "Might be turned into different functions for the most common terminals." 
            exit 1
            ;;
        1)  $running
            ;;
    esac    
}
# Script #

while (( $# )); do
    attr=$1
    if [[ "$attr" == "-"* ]]; then
        while read -n1 char; do
            case $char in
                -|" "|"")   continue
                    ;;
                t)  switch "thisFile"
                    thisFile=$?
                    ;;
                i)  switch "interactiveMode"
                    interactiveMode=$?
                    ;;
                2)  switch "usePython2"
                    usePython2=$?
                    ;;
                3)  switch "usePython3"
                    usePython3=$?
                    ;;
                s)  switch "sameTerminal"
                    sameTerminal=$?
                    ;;
                h)  showUsage
                    exit 0
                    ;;
                *)  printf "%s\n" "" "Unknown Attribute: $char"
                    showUsage
                    exit 1
                    ;;
            esac            
        done <<< $attr

    elif [[ "$attr" == "/"*".py" ]] || [[ "$attr" == "./"*".py" ]]; then
        workPath="$attr"
        echo $workPath
    fi
    shift
done

# Check for Python Version
if [[ "$usePython2" -eq 1 ]] && [[ "$usePython3" -eq 1 ]]; then
    printf "%s\n" "You cannot use Python2 and Python3 simultaneously. Either use the -2 or the -3 option"
    showUsage
    exit 1
elif [[ "$usePython2" -eq 1 ]]; then
    usePython=2
elif [[ "$usePython3" -eq 1 ]]; then
    usePython=3
fi

# Check for if workPath is provided
    if [[ "$workPath" == "" ]]; then
        printf "%s\n" "No Python file found. Please make sure your python file is provided to the application and your python file has the suffix .py." "The easiest way of providing a the file and file path is by giving the % option while editing the file via vim (intended use of this script)"
        exit 1
    fi      

# Parse for the main.py file if the -t option is not given 
# It is assumed that the main.py file is in the same or the parent directory. Sibling or sub directories
# will be ignored
if [[ "$thisFile" -eq 0 ]]; then
    parseMainFile
fi
run

exit 0
\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.