import pathlib


def new_module(source_dir: str, prepend_text=None):
    """Creates a new module file consisting of all functions that reside in a given folder.  This tool is intended to receive the path of a given folder where the individual function .py files reside, and it will retrieve the content of each of those .py files, and will put all of the content together in a single file in the "modules" directory (hard-coded within this script).  The new single 'module' file will have the same name as the given "source_dir" folder + the ".py" extension.  Additionally, the 'prepend_text' parameter can be used to add notes or import statements to the top of the module file (underneath the header, but before the functions are defined).

    Reference:
        https://stackoverflow.com/questions/47518669/create-new-folder-with-pathlib-and-write-files-into-it

    Args:
        source_dir (str): Reference the path of the directory where the function .py files reside
        prepend_text (str, optional): Use this parameter in order to add text underneath the banner but before all of the functions, e.g. "from scapy.all import *". Defaults to None.
    """
    
    source_dir_pathobj = pathlib.Path(source_dir).resolve()
    if not source_dir_pathobj.is_dir():
        print('The given source_dir is not a directory.')
        return
    
    dest_dir_pathobj = pathlib.Path().home() / "Temp/pyplay/IMPORT_functions/Python_3.8_Tools/modules/"
    
    def new_module_header(source_dir_name: str):
        
        def format_header_block_plus(string: str):
            """Returns a header for use with my function files.

            Example:
                #######################################\n
                ########### ARRAY FUNCTIONS ###########\n
                #######################################\n

            """
            newstring = ""
            newstring += "{0:#<39}".format("") + "\n"
            newstring += "{0:#^39}".format(f" {string} ") + "\n"
            newstring += "{0:#<39}".format("") + "\n\n"
            
            return newstring
        
        header_name = source_dir_name.replace('_', ' ').upper()
        new_header = format_header_block_plus(header_name)
        return new_header
    
    header_content = new_module_header(source_dir_pathobj.name)
    all_funcs_content_from_source_dir = ''.join([ e.read_text() for e in source_dir_pathobj.glob('*') if e.is_file() and e.name.endswith('.py')])
    
    if prepend_text:
        full_content = header_content + prepend_text + all_funcs_content_from_source_dir
    else:
        full_content = header_content + all_funcs_content_from_source_dir
    
    new_module_name = source_dir_pathobj.name + '.py'
    
    module_filepath = dest_dir_pathobj / new_module_name
    
    with module_filepath.open('w') as f:
        f.write(full_content)