"""
Handles connections to various serial objects.
This is to ensure that a single serial connection
can stay running and not interfere with other
threads.

| author     = "Seth Miers"
| copyright  = "Copyright, Xilinx Inc. All rights reserved."
| credits    = ["Seth Miers"]
| license    = "GPL"
| version    = "3.0"
| maintainer = "Seth Miers"
| email      = "sethm@xilinx.com"
| status     = "Development"


"""

##########################################
#               Imports                  #
##########################################

# --------------Packages---------------- #

# --------------Local------------------- #
import filecomm

##########################################
#              Main Code                 #
##########################################


class FileManager():
    '''
    A dictionary is created that keeps track of
    all the opened (non-volatile) pickled files.
    when a thread wants to change a variable in
    one of these files, the file manager handles
    it so we don't have multiple creations
    of the same object.

    Args:
        logger (logging): The logger that is used

    Attributes:
        file_comms (dict): A dictionary of all the
            files currently being accessed.
    '''
    # pylint: disable=too-few-public-methods
    # More methods to be added in the future
    def __init__(self, logger):
        self.logger = logger
        self.file_comms = dict()

    def get_file_comm(self, file=''):
        '''
        This method is used to request a single FileComm
        object. If the file has already been created as
        a FileComm object then this method will return that object.
        Otherwise it will create it, add it to the dictionary, and
        return that object.

        Args:
            file (string): The file that contains the pickled
                variable.
        '''
        if file not in self.file_comms.keys():
            self.file_comms.update({file: filecomm.FileComm(self.logger,
                                                            file)})
        return self.file_comms[file]
