Python Classroom Series – 16/Mar/2020

Play by Play – 1

  • Scenario: Write a Python command line application, which will help in launching applications.

  • Sample Inputs and Outputs:

    • Sample-1:
      • Input: python ProgramLauncher.py –launch chrome
      • Output: Chrome should be launched
    • Sample-2:
      • Input: python ProgramLauncher.py –launch atom
      • Output: Atom is not installed (Atom should not be installed on your laptop)
    • Sample-3:
      • Input: python ProgramLauncher.py –list-applications
      • Output: All the applications which Program Launcher is aware about
    • Sample-4:
      • Input python ProgramLauncher.py –alias "winword,word"
      • Output: Successful in mapping winword to new alias word
  • Requirements:

    • ProgramLauncher should have an option to scan all the applications in your laptop
      • python programlauncher.py --scan-all
      • Scan all should be done only once or on user demand not every time
  • Implementation:

    • Whenever we find any executable more than once we will not store it in map.
    • The map that is built will be stored in a file (Json)
  • Developer Thinking

    • We need to find a way to scan all the directories and build a mapping between name of the application and the full path (absolute path) for this we can use python dictionary
    {
        "7z": "C:\Program Files\7-Zip\7z.exe",
        "7zFM": "C:\Program Files\7-Zip\7zFM.exe",
        "7zG": "C:\Program Files\7-Zip\7zG.exe",
        "aws": "C:\Program Files\Amazon\AWSCLIV2\aws.exe",
        "aws_completer": "C:\Program Files\Amazon\AWSCLIV2\aws_completer.exe",
    }
    
    • So this mapping can be stored in RAM/memory using dictionary, so to save this in a file we will be using json format. We would be converting python dictionary to json.
  • Programming:

    • We have tried to build a dictionary with executables. The code programlauncher.py is looking as shown below so far
import os

def scan_all_directories(root_folder='C:\\'):
    """
    This function is used to scan all the directories in the root folder
    to identify executables. 

    Parameters:
      root_folder (str): root directory in which programs have to be scanned

    Returns: 
      exe_dict (dict): an executable dictionary

    """
    exe_dict = dict()
    for root, dirs, files in os.walk(root_folder):
        for name in files:
            (filename, ext) = os.path.splitext(name)
            if ext =='.exe':
                if filename not in exe_dict.keys():
                    exe_dict[filename] = os.path.join(root, name)
    return exe_dict

exe_map = scan_all_directories()
    
* In the next session, we will add the storing the dictionary to the file and parsing command line arguments.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner