2024-04-15 20:40:15 +02:00
from csv import reader as csv_reader
2024-04-17 15:33:10 +02:00
from pprint import pformat
from pprint import pp as pprint
2024-04-15 20:40:15 +02:00
from sys import argv , stderr
2024-04-17 15:33:10 +02:00
from xml . dom . minidom import parseString as xml_parser
from xml . etree import ElementTree
2024-04-15 20:40:15 +02:00
class System :
""" Represents an L-System """
def __init__ (
self ,
name : str ,
2024-04-17 15:37:25 +02:00
base : list [ str ] ,
2024-04-15 20:40:15 +02:00
axiom : str ,
substitutions : list [ tuple [ str , str ] ] ,
interpretations : list [ tuple [ str , str ] ] ,
) :
self . name = name
""" System name """
self . base = base
""" Set used """
self . axiom = axiom
""" Axiom used at the start of iterations """
self . substitutions = substitutions
"""
Substitution for each member of the base ,
represented as a couple ( member , substitution )
"""
self . interpretations = interpretations
"""
Interpretation for each member of the base ,
represented as a couple ( member , interpretation )
"""
# TODO: Est-ce que on garde ça ??
# Interpretation of extra symbols added if necessary
extra_symbols = [ " [ " , " ] " ]
for _ , substitution in substitutions :
if any ( symbol in substitution for symbol in extra_symbols ) :
self . interpretations . extend (
[ ( extra_symbols [ 0 ] , " STORE " ) , ( extra_symbols [ 1 ] , " RESTORE " ) ]
)
break
def __repr__ ( self ) :
return pformat ( self . __dict__ , compact = True , width = 120 , sort_dicts = False )
2024-04-17 15:33:10 +02:00
def to_xml ( self ) - > ElementTree . Element :
""" Convert the current system into an XML element """
system = ElementTree . Element ( " lsystem " )
base = ElementTree . SubElement ( system , " base " )
base . text = " " . join ( self . base )
axiom = ElementTree . SubElement ( system , " axiom " )
axiom . text = self . axiom
substitutions = ElementTree . SubElement ( system , " substitutions " )
for member , substitution in self . substitutions :
sub_element = ElementTree . SubElement ( substitutions , " substitution " )
sub_element . set ( " member " , member )
sub_element . text = substitution
interpretations = ElementTree . SubElement ( system , " interpretations " )
for member , interpretation in self . interpretations :
inter_element = ElementTree . SubElement ( interpretations , " interpretation " )
inter_element . set ( " member " , member )
inter_element . text = interpretation
return system
2024-04-15 20:40:15 +02:00
def data_reader ( path : str , delimiter : str = " , " ) :
""" Read a CSV file and returns a list of L-System """
res : list [ System ] = [ ]
with open ( path ) as csv_file :
data = csv_reader ( csv_file , delimiter = delimiter )
for system in data :
name = system [ 0 ]
2024-04-17 15:37:25 +02:00
base = list ( system [ 1 ] )
2024-04-15 20:40:15 +02:00
axiom = system [ 2 ]
substitutions = [ ( v , system [ 3 + i ] ) for i , v in enumerate ( base ) ]
interpretations = [
( v , system [ 3 + i + len ( substitutions ) ] ) for i , v in enumerate ( base )
]
res . append ( System ( name , base , axiom , substitutions , interpretations ) )
2024-04-17 15:37:25 +02:00
2024-04-15 20:40:15 +02:00
return res
2024-04-17 15:33:10 +02:00
def lsystems_xml ( systems : list [ System ] ) - > ElementTree . Element :
""" Convert list of L-system structure into XML """
root = ElementTree . Element ( " lsystems " )
for system in systems :
root . append ( system . to_xml ( ) )
return root
2024-04-15 20:40:15 +02:00
if __name__ == " __main__ " :
if len ( argv ) != 2 :
2024-04-17 15:33:10 +02:00
pprint ( f " Syntax error: \n \t python { argv [ 0 ] } file.csv " , stream = stderr )
2024-04-15 20:40:15 +02:00
exit ( 1 )
2024-04-17 15:33:10 +02:00
# TODO: Check if we have to dump to stdout or we write into a file
# use `argparse` for cli arguments
# Read data
lsystems = data_reader ( argv [ 1 ] )
2024-04-15 20:40:15 +02:00
2024-04-17 15:33:10 +02:00
# Generate XML
xml = lsystems_xml ( lsystems )
2024-04-15 20:45:09 +02:00
2024-04-17 15:33:10 +02:00
# Print XML
dom = xml_parser ( ElementTree . tostring ( xml ) )
print ( dom . toprettyxml ( encoding = " UTF-8 " ) . decode ( ) )