# Extracts code from the latex documents.
# Python 3 code. Full documentation at http://artint.info/code/python/aipython.pdf

# Artificial Intelligence: Foundations of Computational Agents
# http://artint.info
# Copyright David L Poole and Alan K Mackworth 2016.
# This work is licensed under a Creative Commons
# Attribution-NonCommercial-ShareAlike 4.0 International License.
# See: http://creativecommons.org/licenses/by-nc-sa/4.0/deed.en_US.

import re

all_files = ['python.tex', 'agents.tex', 'search.tex', 'csp.tex', 'propositions.tex',
                 'planning.tex', 'learning.tex', 'learnDeep.tex', 'uncertainty.tex', 'planUncertainty.tex',
                 'learnUncertainty.tex', 'mas.tex', 'rl.tex', 'relational.tex']
base_folder = 'aipython/'
base_folder_alt = 'aipython_322/'
base_folder_dev = 'aipython_dev/'
files_alt = ['python.tex', 'search.tex','csp.tex' #, 'propositions.tex'
                 ,'planning.tex', 'uncertainty.tex', 'planUncertainty.tex']
files_dev =  ['python.tex', 'agents.tex', 'search.tex', 'csp.tex', 'propositions.tex',
                 'planning.tex', 'learning.tex', 'learnDeep.tex', 'uncertainty.tex', 'planUncertainty.tex',
                 'learnUncertainty.tex', 'mas.tex', 'rl.tex', 'relational.tex']
##base_folder_alt = 'aifca_new/'
##files_alt = ['python.tex', 'search.tex','csp.tex', 'propositions.tex', 'planning.tex', 'uncertainty.tex', 'planUncertainty.tex']
header = 'header.py'


def extract_code(files,base_folder):
    output_files = {}    # name:file dictionary
    headerfile = open(header,'r')
    headertext = headerfile.readlines()
    headerfile.close()
    for file_name in files:
        #print('Opening file:',file_name)
        file = open(file_name,'r')
        output = None
        for line in file:
            #print(line,end="")
            if output:
                if line.startswith("\\end{python"):
                    #print("found",line,end="")
                    output = None
                else:
                    output.write(line)
            elif line.startswith("\\begin{python}"):
                #print("found",line,end="")
                py,name,desc = re.findall('\{[^\}]*\}',line)
                name = name[1:-1]
                if name in output_files:
                    print("Warning overwriting file",name,"-",desc[1:-1])
                output = open(base_folder+name+'.py','w')
                output_files[name] = output
                output.write("# "+name+".py - "+desc[1:-1]+"\n")
                for headl in headertext:
                    output.write(headl)
            elif line.startswith("\\begin{pythoncont}"):
                #print("found",line,end="")
                py,name= re.findall('\{[^\}]*\}',line)
                name = name[1:-1]
                output = output_files[name]
        file.close()
    for f in output_files.values():
        f.close()
        
                                 
        
extract_code(all_files,base_folder)
#extract_code(files_alt,base_folder_alt)
extract_code(files_dev,base_folder_dev)
quit()
