# ==============================================================================
from ezCLI import *
# ==============================================================================

# On reprend l'exercice B3_numplines pour :
# - lire le fichier
# - le séparer en tableau de ligne
# - parcourir le tableau pour ne garder que certaines lignes


def divide_file(name:str,name_result:str) -> bool:
  try :
    txt = read_txt(name)
    txt_line = txt.split('\n')

    # Version 1 : concaténation de chaine
    # txt_result = ""
    # for i in range(0,len(txt_line),2):
    #   txt_result += txt_line[i] + "\n"
    # write_txt(name_res,txt_result.strip())
    # return True

    # Version 2 : ajout des lignes à un tableau
    # txt_result = []
    # for i in range(0,len(txt_line),2):
    #   txt_result.append(txt_line[i])
    # write_txt(name_result,'\n'.join(txt_result))
    
    # Version 3 : création directe du tableau
    # txt_result = [txt_line[i] for i in range(0,len(txt_line),2)]
    # write_txt(name_result,'\n'.join(txt_result))

    # version 4 : slice
    txt_result = txt_line[::2]
    write_txt(name_result,'\n'.join(txt_result))

    return True
  
  except OSError:
    return False
    
print(divide_file('toto.txt','toto_divide.txt'))

def suppress_comment(name:str,name_res:str,comment_symbol:str) -> bool:
  try :
    txt = read_txt(name)
    txt_line = txt.split('\n')

    # Version 1 : concaténation de chaine
    # txt_result = ""
    # for line in txt_line:
    #   if line[0] != comment_symbol:
    #     txt_result += line + "\n"
    # write_txt(name_res,txt_result.strip())
    # return True

    # Version 2 : ajout des lignes à un tableau
    txt_result = []
    for line in txt_line:
      if line[0] != comment_symbol:
        txt_result.append(line)
    
    write_txt(name_res,'\n'.join(txt_result))

    return True
  
  except OSError:
    return False

print(suppress_comment('comment.txt','comment_res.txt','#'))

def suppress_comment2(name:str,name_res:str,comment_symbol:str) -> bool:
  try :
    txt = read_txt(name)
    txt_line = txt.split('\n')

    # Version 1 : concaténation de chaine
    # txt_result = ""
    # for line in txt_line:
    #   if line[0] != comment_symbol:
    #     txt_result += line + "\n"
    # write_txt(name_res,txt_result.strip())
    # return True

    # Version 2 : ajout des lignes à un tableau
    txt_result = []
    for line in txt_line:
      for c in line:
        if c == comment_symbol:
          break
        if c not in ' \t':
          txt_result.append(line)
          break
    
    write_txt(name_res,'\n'.join(txt_result))

    return True
  
  except OSError:
    return False

print(suppress_comment2('comment2.txt','comment_res2.txt','#'))
