from .input_gdsii import gds_input_gdsii
from .input_key import gds_input_key
from .output_gdsii import gds_output_gdsii
from .output_key import gds_output_key

## Easy-to-use Read/write functions ###
__all__ = ["read_library_from_file",
           "read_library_from_gdsii",
           "read_library_from_key",
           "write_library_to_file",
           "write_library_to_gdsii",
           "write_library_to_key"]

## Read
def read_library_from_file(filename, input_type):
    """ reads a library from a file using the input processor """
    f = open(filename,"rb")
    i = input_type(f)
    L = i.read()
    f.close()
    return L

def read_library_from_gdsii(filename):
    """ reads a library from a GDSII file """
    return read_library_from_file(filename, gds_input_gdsii)

def read_library_from_key(filename):
    """ reads a library from a KEY file """
    return read_library_from_file(filename, gds_input_key)

## Write

def write_library_to_file(library, filename, output_type):
    """ writes a library to a file using the given output processor """
    f = open(filename,"wb")
    o = output_type(f)
    o.write(library)
    f.close()

def write_library_to_gdsii(library, filename):
    """ writes a library to a GDSII file """
    return write_library_to_file(library, filename, gds_output_gdsii)

def write_library_to_key(library, filename):
    """ writes a library to a KEY file """
    return write_library_to_file(library, filename, gds_output_key)

