Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XML Templates Generator. #90

Open
Tracked by #51
aladinoster opened this issue Nov 15, 2021 · 3 comments · May be fixed by #100
Open
Tracked by #51

XML Templates Generator. #90

aladinoster opened this issue Nov 15, 2021 · 3 comments · May be fixed by #100
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@aladinoster
Copy link
Contributor

No description provided.

@aladinoster aladinoster mentioned this issue Nov 15, 2021
37 tasks
@aladinoster aladinoster added this to the v1.2.0 milestone Nov 17, 2021
@aladinoster aladinoster added the enhancement New feature or request label Nov 17, 2021
@aladinoster
Copy link
Contributor Author

Idea:

  1. XML Object associated to a section of 1 XML
  2. Single Object to populate the XML.
  • Jinja2 vs. lxml for parsing?

@aladinoster aladinoster linked a pull request Nov 22, 2021 that will close this issue
@floriangc
Copy link
Contributor

floriangc commented Nov 24, 2021

For the first idea, there is a draft. We define a XMLObject as a base class and for each XML element we define a subclass with the right parameters. You can execute the code below to see the result.

class XMLObject(object):
    def __init__(self, args: dict):
        self.attrs = args
        self.childs = []
        self.level = 0

    def add_child(self, child):
        self.childs.append(child)
        child.level = self.level + 1

    def __str__(self):
        if self.attrs:
            attr = " "+" ".join([f'{key}="{val}"' for key, val in self.attrs.items()])
            balise = "\t"*self.level+f"<{self.__class__.__name__}{attr}"
        else:
            balise = "\t"*self.level+f"<{self.__class__.__name__}"
        if self.childs:
            res = "\n".join([balise+">"]+[child.__str__() for child in self.childs]+["\t"*self.level+f"</{self.__class__.__name__}>"])
        else:
            res = balise + "/>"
        return res


class ROOT_SYMUBRUIT(XMLObject):
    def __init__(self, xmlnsxsi="http://www.w3.org/2001/XMLSchema-instance", xsi_noNamespaceSchemaLocation="reseau.xsd", version="2.05"):
        super(ROOT_SYMUBRUIT, self).__init__({"xmlns:xsi":xmlnsxsi, "xsi:noNamespaceSchemaLocation":xsi_noNamespaceSchemaLocation, "version": version})


class PLAGES_TEMPORELLES(XMLObject):
    def __init__(self, debut, type):
        super(PLAGES_TEMPORELLES, self).__init__({"debut": debut, "type": type})


class PLAGE_TEMPORELLE(XMLObject):
    def __init__(self, id, debut, fin):
        super(PLAGE_TEMPORELLE, self).__init__({"id": id, "debut": debut, "fin": fin})


class SIMULATIONS(XMLObject):
    def __init__(self):
        super(SIMULATIONS, self).__init__({})


class SIMULATION(XMLObject):
    def __init__(self, id, pasdetemps, debut, fin, loipoursuite='exact', comportementflux="iti", date="1985-01-17", titre="", proc_decelation="false", seed="1"):
        super(SIMULATION, self).__init__(dict(id=id, pasdetemps=pasdetemps, debut=debut, fin=fin, loipoursuite=loipoursuite,
                                              comportementflux=comportementflux, date=date, titre=titre, proc_decelation=proc_decelation,
                                              seed=seed))


class RESTITUTION(XMLObject):
    def __init__(self, trace_route="false", trajectoires="true", debug="false", debug_matrice_OD="false", debug_SAS="false"):
        super(RESTITUTION, self).__init__(dict(trace_route=trace_route, trajectoires=trajectoires, debug=debug,
                                               debug_matrice_OD=debug_matrice_OD, debug_SAS=debug_SAS))


if __name__ == "__main__":

    root = ROOT_SYMUBRUIT()

    plage = PLAGES_TEMPORELLES("06:00:00", "horaire")
    root.add_child(plage)
    plage.add_child(PLAGE_TEMPORELLE("P01", "06:00:00", "07:00:00"))
    plage.add_child(PLAGE_TEMPORELLE("P02", "07:00:00", "08:00:00"))

    sims = SIMULATIONS()
    root.add_child(sims)

    sim = SIMULATION("simID", "1", "06:00:00", "07:00:00")
    sims.add_child(sim)
    sim.add_child(RESTITUTION())

    print(root)

Output:

<ROOT_SYMUBRUIT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="reseau.xsd" version="2.05">
	<PLAGES_TEMPORELLES debut="06:00:00" type="horaire">
		<PLAGE_TEMPORELLE id="P01" debut="06:00:00" fin="07:00:00"/>
		<PLAGE_TEMPORELLE id="P02" debut="07:00:00" fin="08:00:00"/>
	</PLAGES_TEMPORELLES>
	<SIMULATIONS>
		<SIMULATION id="simID" pasdetemps="1" debut="06:00:00" fin="07:00:00" loipoursuite="exact" comportementflux="iti" date="1985-01-17" titre="" proc_decelation="false" seed="1">
			<RESTITUTION trace_route="false" trajectoires="true" debug="false" debug_matrice_OD="false" debug_SAS="false"/>
		</SIMULATION>
	</SIMULATIONS>
</ROOT_SYMUBRUIT>

@aladinoster
Copy link
Contributor Author

Great proposal, I think this is a good approach. I've added this to #100. Inspired by this I'm going to work on an iteration protocol that should be able to create the file based on a tree simple structure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants