# Path: phievo/AnalysisTools/main_functions.py
# def download_tools(run_evolution="run_evolution.py",AnalyseRun="AnalyseRun.ipynb",ProjectCreator="ProjectCreator.ipynb"):
#     url_runevo = "https://raw.githubusercontent.com/phievo/phievo/master/run_evolution.py"
#     url_jpnb = "https://github.com/phievo/phievo/raw/master/AnalyzeRun.ipynb"
#     url_confnb = "https://github.com/phievo/phievo/raw/master/ProjectCreator.ipynb"
#     urlretrieve(url_runevo,run_evolution)
#     print("run_evolution.py ... downloaded.")
#     urlretrieve(url_jpnb,AnalyseRun)
#     print("AnalyseRun.ipynb ... downloaded.")
#     urlretrieve(url_confnb,ProjectCreator)
#     print("ProjectCreator.ipynb ... downloaded.")
# 
# def download_example(example_name,directory=None):
#     """
#     Download an example seed or project.
#     """
# 
#     #server_address = "http://www.physics.mcgill.ca/~henrya/seeds_phievo/{}"
#     server_examples = "https://github.com/phievo/phievo/blob/master/Examples/{}?raw=true"
#     
#     existing_examples = {
#         "adaptation":"adaptation.zip",
#         "somite":"Somites.zip",
#         "hox":"StaticHox.zip",
#         "hox_pareto":"StaticHox_pareto.zip",
#         "lac_operon":"lac_operon.zip",
#         "immune":"immune.zip",
#         "minimal_project":"minimal_project.zip",
#     }
#     server_seed = "https://github.com/phievo/simulation_examples/blob/master/{}?raw=true"
#     existing_seeds = {
#         "seed_adaptation":"adaptation.zip",
#         "seed_adaptation_pruning":"adaptation_pruning.zip",
#         "seed_lacOperon":"lacOperon.zip",
#         "seed_lacOperon_pruning":"lacOperon_pruning.zip",
#         "seed_somite":"somite.zip",
#         "seed_somite_pruning":"somite_pruning.zip",
#         "seed_hox_pareto_light":"hox_pareto_light.zip",
#     }
#     
#     with_seed = False
#     if "seed" in example_name:
#         with_seed = True
#         try:            
#             zip_name = existing_seeds[example_name]
#             example_name = example_name[5:]
#             url = server_seed.format(zip_name)
#         except KeyError:
#             print("Example {} is not available.".format(example_name))
#             print("Only the following examples are available:\n\t- "+"\n\t- ".join(list(existing_examples.keys())+list(existing_seeds.keys())))
#             return None
#     else:
#         try:            
#             zip_name = existing_examples[example_name]
#             url = server_examples.format(zip_name)
#         except KeyError:
#             print("Example {} is not available.".format(example_name))
#             print("Only the following examples are available:\n\t- "+"\n\t- ".join(list(existing_examples.keys())+list(existing_seeds.keys())))
#             return None
#     if not directory:    
#         directory = "example_{}".format(example_name)
#     res = download_zip(directory,url)
#     if not res:
#         return None
#     
#     if with_seed:
#         seed_name = os.path.join(directory,"Seed{}".format(example_name))
#         
#         os.makedirs(seed_name)
#         files = glob.glob(os.path.join(directory,"*"))
#         files.remove(seed_name)
#         for filename in files :
#             try:
#                 os.rename(filename, filename.replace(directory,seed_name))
#             except OSError:
#                 import pdb;pdb.set_trace()
#         print("recovering log files...",end="\r")
#         for log_f in glob.glob(os.path.join(seed_name,"log_*")):
#             f_name = log_f.split(os.sep)[-1]
#             f_name = f_name.replace("log_","")
#             os.rename(log_f, os.path.join(directory,f_name))
#         with open(os.path.join(directory,"init_file.py"),"r") as init_file:
#             init_text = init_file.read()
#             init_text = re.sub("(cfile\[[\'\"](\w+)[\'\"]]\s*=\s*).+",r"\1'\2.c'",init_text)
#             init_text = re.sub("(pfile\[[\'\"](\w+)[\'\"]]\s*=\s*).+",r"\1'\2.py'",init_text)
#         with open(os.path.join(directory,"init_file.py"),"w") as init_file:
#             init_file.write(init_text)
#         print("recovering log files...   done.",end="\n")
#     print("Project saved in {}.".format(directory))


from phievo.AnalysisTools.main_functions import download_tools,download_example
import unittest
import shutil,os,glob

class TestDownloadFunctions(unittest.TestCase):
    def test_download_tools(self):
        AnalyseRun="test_AnalyseRun.py"
        run_evolution = "test_run_evolution.py"
        download_tools(run_evolution=run_evolution,AnalyseRun=AnalyseRun)
        self.assertTrue(os.path.isfile(run_evolution))
        self.assertTrue(os.path.isfile(AnalyseRun))
        os.remove(run_evolution)
        os.remove(AnalyseRun)
    def test_download_example(self):
        directory = "test_directory_adaptation"

# Path: phievo/AnalysisTools/main_functions.py
# def download_tools(run_evolution="run_evolution.py",AnalyseRun="AnalyseRun.ipynb",ProjectCreator="ProjectCreator.ipynb"):
#     url_runevo = "https://raw.githubusercontent.com/phievo/phievo/master/run_evolution.py"
#     url_jpnb = "https://github.com/phievo/phievo/raw/master/AnalyzeRun.ipynb"
#     url_confnb = "https://github.com/phievo/phievo/raw/master/ProjectCreator.ipynb"
#     urlretrieve(url_runevo,run_evolution)
#     print("run_evolution.py ... downloaded.")
#     urlretrieve(url_jpnb,AnalyseRun)
#     print("AnalyseRun.ipynb ... downloaded.")
#     urlretrieve(url_confnb,ProjectCreator)
#     print("ProjectCreator.ipynb ... downloaded.")
# 
# def download_example(example_name,directory=None):
#     """
#     Download an example seed or project.
#     """
# 
#     #server_address = "http://www.physics.mcgill.ca/~henrya/seeds_phievo/{}"
#     server_examples = "https://github.com/phievo/phievo/blob/master/Examples/{}?raw=true"
#     
#     existing_examples = {
#         "adaptation":"adaptation.zip",
#         "somite":"Somites.zip",
#         "hox":"StaticHox.zip",
#         "hox_pareto":"StaticHox_pareto.zip",
#         "lac_operon":"lac_operon.zip",
#         "immune":"immune.zip",
#         "minimal_project":"minimal_project.zip",
#     }
#     server_seed = "https://github.com/phievo/simulation_examples/blob/master/{}?raw=true"
#     existing_seeds = {
#         "seed_adaptation":"adaptation.zip",
#         "seed_adaptation_pruning":"adaptation_pruning.zip",
#         "seed_lacOperon":"lacOperon.zip",
#         "seed_lacOperon_pruning":"lacOperon_pruning.zip",
#         "seed_somite":"somite.zip",
#         "seed_somite_pruning":"somite_pruning.zip",
#         "seed_hox_pareto_light":"hox_pareto_light.zip",
#     }
#     
#     with_seed = False
#     if "seed" in example_name:
#         with_seed = True
#         try:            
#             zip_name = existing_seeds[example_name]
#             example_name = example_name[5:]
#             url = server_seed.format(zip_name)
#         except KeyError:
#             print("Example {} is not available.".format(example_name))
#             print("Only the following examples are available:\n\t- "+"\n\t- ".join(list(existing_examples.keys())+list(existing_seeds.keys())))
#             return None
#     else:
#         try:            
#             zip_name = existing_examples[example_name]
#             url = server_examples.format(zip_name)
#         except KeyError:
#             print("Example {} is not available.".format(example_name))
#             print("Only the following examples are available:\n\t- "+"\n\t- ".join(list(existing_examples.keys())+list(existing_seeds.keys())))
#             return None
#     if not directory:    
#         directory = "example_{}".format(example_name)
#     res = download_zip(directory,url)
#     if not res:
#         return None
#     
#     if with_seed:
#         seed_name = os.path.join(directory,"Seed{}".format(example_name))
#         
#         os.makedirs(seed_name)
#         files = glob.glob(os.path.join(directory,"*"))
#         files.remove(seed_name)
#         for filename in files :
#             try:
#                 os.rename(filename, filename.replace(directory,seed_name))
#             except OSError:
#                 import pdb;pdb.set_trace()
#         print("recovering log files...",end="\r")
#         for log_f in glob.glob(os.path.join(seed_name,"log_*")):
#             f_name = log_f.split(os.sep)[-1]
#             f_name = f_name.replace("log_","")
#             os.rename(log_f, os.path.join(directory,f_name))
#         with open(os.path.join(directory,"init_file.py"),"r") as init_file:
#             init_text = init_file.read()
#             init_text = re.sub("(cfile\[[\'\"](\w+)[\'\"]]\s*=\s*).+",r"\1'\2.c'",init_text)
#             init_text = re.sub("(pfile\[[\'\"](\w+)[\'\"]]\s*=\s*).+",r"\1'\2.py'",init_text)
#         with open(os.path.join(directory,"init_file.py"),"w") as init_file:
#             init_file.write(init_text)
#         print("recovering log files...   done.",end="\n")
#     print("Project saved in {}.".format(directory))


# Path: tests/test_downloads.py
from phievo.AnalysisTools.main_functions import download_tools,download_example
import unittest
import shutil,os,glob


class TestDownloadFunctions(unittest.TestCase):
    def test_download_tools(self):
        AnalyseRun="test_AnalyseRun.py"
        run_evolution = "test_run_evolution.py"
        download_tools(run_evolution=run_evolution,AnalyseRun=AnalyseRun)
        self.assertTrue(os.path.isfile(run_evolution))
        self.assertTrue(os.path.isfile(AnalyseRun))
        os.remove(run_evolution)
        os.remove(AnalyseRun)
    def test_download_example(self):
        directory = "test_directory_adaptation"

        download_example("adaptation",directory=directory)