Loading settings.

★★☆

Many applications read and write settings into an initialisation (ini) file. Typically the data in the file is structured into blocks identified with square brackets. Each setting has a name, followed by an equal sign, followed by the data. E.g. in the example below, port is a setting and the data is 143. ``` [modified by] name=Dave Hillyard [database] ; use IP address in case network name resolution is not working server=192.0.2.62 port=143 ```

ini file

Make

Write a program that reads the contents of a text file as shown above and outputs the data contained within it in the format:

Last modified by: Dave Hillyard

IP address: 192.0.2.62

Port number: 143

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called input_settings that:

  1. Attempts to open a file called settings.ini. If the file is not found the message, "settings.ini file not found." is output and no further processing takes place. Note the file is included in the Trinket above for you to use as source data.
  2. Reads in each line of data in the file.
  3. Separates the line of data into two parts. The part before an equal sign (the attribute) and a part after an equal sign (data). If the line is not a data attribute, it will only have one part and is ignored.
  4. Stores the values for modified by, server/ip address and port settings in three variables and output their values.

Note the data attributes can be anywhere in the file, not necessarily in the order presented above.

Complete the main program so that:

  1. The input_settings function is called.

Typical inputs and outputs from the program would be:

File contents for settings.ini:

[modified by]
name=Dave Hillyard

[database]
; use IP address in case network name resolution is not working
server=192.0.2.62     
port=143

Output:

Last modified by: Dave Hillyard
IP address: 192.0.2.62
Port number: 143

Restricted automated feedback

Automated feedback for this assignment is still under construction. Submitted programs are checked for syntax errors and their source code is checked for potential errors, bugs, stylistic issues, and suspicious constructs. However, no checks are performed yet to see if the program correctly implements the behaviour specified in the assignment.

🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
# ini file program

# -------------------------
# Subprograms
# -------------------------
---
def input_settings():
---
    # Check file exists
    try:
---
        file = open("settings.ini", "r")
---
    # No file found
    except FileNotFoundError:
---
        print("settings.ini file not found.")
---
    # File found
    else:
---
        # Process each line in the file
        for line in file:
---
            line = line.strip()
---
            # Split data into a list, separated by an equal sign
            data = line.split("=")
---
            # If there are two items in the list, match the first item
            if len(data) > 1:
---
                # Set the variable to be the second item
                match data[0]:
---
                    case "name":
---
                        modified = data[1]
---
                    case "server":
---
                        ip = data[1]
---
                    case "port":
---
                        port = data[1]
---
        file.close()
---
        print("Last modified by:", modified)
        print("IP address:", ip)
        print("Port number:", port)
---


# -------------------------
# Main program
# -------------------------
input_settings()