from os import system
from hcfg.exceptions import *

class hcfg():


    def __init__(self) -> None:
        pass
    

    @staticmethod
    def readFile(fileName):
        buffer = {}
        if not fileName.endswith(".hcfg"):
            raise Exception("[Errno 0] This is a not .hcfg file.")
            

        if not (open(fileName,"r")):
            raise Exception("[Errno 1] File does not exist.")

        with open(fileName,"r") as file:
            i = 0
            for line in file:
                i += 1
                object = line.split(":")
                if len(object) <= 1:
                    raise hypSyntaxError(f"[Errno 2] Syntax error [Line:{i}].")
                name = object[0].strip(" ")
                value = hcfg.assignValue(object[1])
                buffer[name] = value
            return buffer

    @staticmethod
    def assignValue(value):
        try:
            val = int(value)
            return val
        except:
            None
        try:
            val = str(value).strip().strip('"')
            return val
        except:
            None
        try:
            val = bool(value)
            return val
        except:
            None    

                