Processing a “locale” file with Python (Copy)
Task description:
In this assignment, you will write a Python program vaguely inspired by Unix command locale. Your Python program will parse a file containing information about language “locales” and “charmaps” and will generate output depending on the command line.
Final score : 30/30
Here is my code:
import sys
import os
#Receive all the error messages and force program exiting
def print_error(message):
print(message)
sys.exit(1)
#Check the file existance and access
def check_file(file_path):
if not os.path.exists(file_path):
print_error(f"Error: File '{file_path}' does not exist.")
if not os.path.isfile(file_path):
print_error(f"Error: '{file_path}' is not a file.")
if not os.access(file_path, os.R_OK):
print_error(f"Error: File '{file_path}' is not readable.")
#Put the data into diffrent dictionary
def parse_file(file_path):
locales = {} #The dictionary that store locales
charmaps = {} #The dictionary that store charmaps
with open(file_path, 'r') as file:
for line in file:
line = line.strip()
if line:
parts = line.split(',') #Separate by ','
if len(parts) == 3:
what_kind, language, filename = parts #Tear parts into three different variables
if what_kind == 'locale':
locales.setdefault(language, []).append(filename)
elif what_kind == 'charmap':
charmaps.setdefault(language, []).append(filename)
return locales, charmaps
#Print all messege also check if there's data in dictionary or not
def print_available(all_kind, message):
if all_kind:
print(message)
for one in all_kind:
print(one)
else:#if no data in it, print no items
print("No items available")
def print_language_info(language, locales, charmaps):
locales_count = len(locales.get(language, []))
charmaps_count = len(charmaps.get(language, []))
if locales_count == 0 and charmaps_count == 0:
print(f"No locales or charmaps in language '{language}'")
else:
print(f"Language {language}:")
print(f"Total number of locales: {locales_count}")
print(f"Total number of charmaps: {charmaps_count}")
#Print my basic info
def print_author_info():
print("Author: Ming-You Chen")
print("Student ID: 24603704")
print("Date of Completion: 13.05.2024")
def main():
#Make sure user input correctness
if len(sys.argv) < 3 or len(sys.argv) > 4:
print_error("Error: Incorrect syntax")
#Get option and argument file
option = sys.argv[1]
file_path = sys.argv[-1]
#Check file vailadation
check_file(file_path)
#Put the data into diffrent dictionary
locales, charmaps = parse_file(file_path)
#Doing action with different option
if option == '-a':
print_available(locales.values(), "Available locales:")
elif option == '-m':
print_available(charmaps.values(), "Available charmaps:")
elif option == '-l':
if len(sys.argv) != 4:#-l option needs one more argument
print_error("Error: Missing language argument")
language = sys.argv[2]
print_language_info(language, locales, charmaps)
elif option == '-v':
print_author_info()
else:
print_error("Error: Invalid option")
# execute main() only if the script is run directly
if __name__ == "__main__":
main()