# AUTOGENERATED! DO NOT EDIT! File to edit: ../notebooks/10_console-command.ipynb.

# %% auto 0
__all__ = ['find_links', 'clean_imdir', 'squeeze']

# %% ../notebooks/10_console-command.ipynb 6
import glob 
from . import squeeze_this_nb, make_imdir 
import os
import re 

# %% ../notebooks/10_console-command.ipynb 7
def find_links(nb_path):
    '''Find existing links to png images in notebook `nb_path`.'''
    
    if nb_path is None: 
        nb_path = ipynb_path.get()

    with open(nb_path) as fh: 
        lines = fh.readlines()

    links = [] 

    for line in lines:     
        m = re.match(r'.*src=\\"(.*\.png)\\"', line) 

        if m: 
            links.append(m.groups()[0]) 
            
    return links


def clean_imdir(nb_path, verbose=False): 
    '''Remove orphant image files corresponding to notebook `nb_path` that are not linked. '''

    imdir_path = make_imdir(nb_path)

    png_filenames = os.listdir(imdir_path) 

    # first select only (png) files that match the notebook prefix 
    prefix = re.sub('\.ipynb', '', os.path.basename(nb_path))

    nb_pngs = [png for png in png_filenames if prefix in png]

    links = find_links(nb_path)
    link_filenames = [re.sub('\./images/', '', link) for link in links]

    orphants = list(set(nb_pngs) - set(link_filenames))

    orphant_fpaths = [os.path.join(imdir_path, orph) for orph in orphants]
    
    for fp in orphant_fpaths: 
        os.remove(fp) 
        
    if verbose:  
        if len(orphants) > 0: 
            print(f'    Removing {len(orphants)} orphant image files:')
            print(orphants)
        else: 
            print(f'  (No orphant image files found)')

            
def squeeze(glob_ptrn='n*b*s/*.ipynb', verbose=False, overwrite=True): 
    '''Extract all embedded images from all notebooks in notebooks subdirectory.
    
    '''
    # locate notebooks 
    
    notebooks = glob.glob('n*b*s/*.ipynb')
    n = len(notebooks) 
    
    # clean and squeeze each notebook 
        
    for i, nb in enumerate(notebooks): 
        
        if verbose:
            print('-'*50) 
            print(f'Processing notebook {i+1}/{n}: {nb}')
            
        # clean orphant images 
        if verbose: 
            print(f'- Cleaning images folder...') 
        # first get rid of all orphant images 
        clean_imdir(nb, verbose=verbose) 
        
        # do the actual squeezing 
        if verbose: 
            print(f'- Extracting inline images...')
        
        squeeze_this_nb(nb, overwrite=overwrite, verbose=verbose) 
                
