Skip to content

Commit

Permalink
- allow for change of output encoding with default utf-8
Browse files Browse the repository at this point in the history
- avoid code duplicates by moving print and write functions to main helpers module
- remove dedicated helpers modules for xodr and xosc
- adapt imports to main helper module
  • Loading branch information
jakobkaths committed Nov 23, 2021
1 parent b91bb11 commit a1d1e16
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 96 deletions.
65 changes: 58 additions & 7 deletions scenariogeneration/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import os

from .scenario_generator import ScenarioGenerator
from .xodr import OpenDrive
from .xosc import Scenario


def esmini(generator,esminipath='esmini',
Expand Down Expand Up @@ -159,22 +157,75 @@ def esmini(generator,esminipath='esmini',
if run_with_replayer:
os.system(replay_executable + ' --file ' + record +' --res_path ' + os.path.join(resource_path,os.pardir) + ' --window '+ window_size)



def prettyprint(element):
def prettify(element, encoding='utf-8'):
""" prints the element to the commandline
Parameters
----------
element (Element, or any generation class of scenariogeneration): element to print
encoding (str): specifies the output encoding
Default: 'utf-8'
"""
if not isinstance(element,ET.Element):
element = element.get_element()
rough = ET.tostring(element, 'utf-8')
rough = ET.tostring(element, 'utf-8').replace(b'\n', b'').replace(b'\t', b'').replace(b' ', b'')
reparsed = mini.parseString(rough)
print(reparsed.toprettyxml(indent=" "))
return reparsed.toprettyxml(indent=" ", encoding=encoding)

def prettyprint(element, encoding='utf-8'):
""" returns the element prettyfied for writing to file or printing to the commandline
Parameters
----------
element (Element, or any generation class of scenariogeneration): element to print
encoding (str): specify the output encoding
Default: 'utf-8'
"""
print (prettify(element, encoding=encoding))


def printToFile(element, filename, prettyprint=True, encoding='utf-8'):
""" prints the element to a xml file
Parameters
----------
element (Element): element to print
filename (str): file to save to
prettyprint (bool): pretty or "ugly" print
encoding (str): specify the output encoding
Default: 'utf-8'
"""
if prettyprint:
try:
with open(filename,'wb') as file_handle:
file_handle.write(prettify(element, encoding=encoding))
except LookupError:
print ("%s is not a valid encoding option." % encoding)

else:
tree = ET.ElementTree(element)
try:
tree.write(filename, encoding=encoding)
except LookupError:
print ("%s is not a valid encoding option." % encoding)

def enum2str(enum):
""" helper to create strings from enums that should contain space but have to have _
Parameters
----------
enum (Enum): a enum of pyodrx
Returns
-------
enumstr (str): the enum as a string replacing _ with ' '
"""
return enum.name.replace('_',' ')
3 changes: 1 addition & 2 deletions scenariogeneration/xodr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# __init__.py
""" the xosc module contains the classes used to generate OpenDRIVE (xml) files.
""" the xodr module contains the classes used to generate OpenDRIVE (xml) files.
"""
from .geometry import *
from .helpers import *
from .opendrive import *
from .lane import *
from .enumerations import *
Expand Down
43 changes: 0 additions & 43 deletions scenariogeneration/xodr/helpers.py

This file was deleted.

2 changes: 1 addition & 1 deletion scenariogeneration/xodr/lane.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import xml.etree.ElementTree as ET
from .helpers import enum2str
from ..helpers import enum2str
from .enumerations import LaneType, LaneChange, RoadMarkWeight, RoadMarkColor, RoadMarkType, MarkRule
from .links import _Links,_Link

Expand Down
2 changes: 1 addition & 1 deletion scenariogeneration/xodr/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
import xml.etree.ElementTree as ET
from .helpers import enum2str
from ..helpers import enum2str
from .enumerations import ElementType, JunctionGroupType

from .exceptions import NotSameAmountOfLanesError
Expand Down
11 changes: 7 additions & 4 deletions scenariogeneration/xodr/opendrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import xml.etree.ElementTree as ET

from .helpers import printToFile,enum2str
from ..helpers import printToFile,enum2str
from .links import _Link, _Links, create_lane_links
from .enumerations import ElementType, ContactPoint, RoadSide, TrafficRule
from .exceptions import UndefinedRoadNetwork, RoadsAndLanesNotAdjusted
Expand Down Expand Up @@ -763,8 +763,8 @@ def get_element(self):
return element


def write_xml(self,filename=None,prettyprint = True):
""" writeXml writes the open scenario xml file
def write_xml(self, filename = None, prettyprint = True, encoding = 'utf-8'):
""" write_xml writes the OpenDRIVE xml file
Parameters
----------
Expand All @@ -773,11 +773,14 @@ def write_xml(self,filename=None,prettyprint = True):
prettyprint (bool): pretty print or ugly print?
Default: True
encoding (str): specifies the output encoding
Default: 'utf-8'
"""
if filename == None:
filename = self.name + '.xodr'
printToFile(self.get_element(),filename,prettyprint)
printToFile(self.get_element(),filename,prettyprint,encoding)

class _Type():
""" class to generate the type element of a road, (not the Enumeration it self).
Expand Down
2 changes: 1 addition & 1 deletion scenariogeneration/xodr/signals_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
import xml.etree.ElementTree as ET
from .helpers import enum2str
from ..helpers import enum2str
from .enumerations import ObjectType, Orientation, Dynamic

class _SignalObjectBase():
Expand Down
1 change: 0 additions & 1 deletion scenariogeneration/xosc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""
from .utils import *
from .actions import *
from .helpers import *
from .position import *
from .triggers import *
from .scenario import *
Expand Down
30 changes: 0 additions & 30 deletions scenariogeneration/xosc/helpers.py

This file was deleted.

4 changes: 2 additions & 2 deletions scenariogeneration/xosc/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import xml.etree.ElementTree as ET

from .enumerations import VersionBase, XMLNS, XSI
from .utils import _StochasticDistributionType, FileHeader, VersionBase, printToFile, ParameterAssignment, Rule
from .utils import _StochasticDistributionType, FileHeader, VersionBase, ParameterAssignment, Rule
from .exceptions import NotEnoughInputArguments, OpenSCENARIOVersionError

from ..helpers import printToFile

class _HistogramBin():
""" the _HistogramBin is used by Histogram to define the bins
Expand Down
9 changes: 6 additions & 3 deletions scenariogeneration/xosc/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import xml.dom.minidom as mini


from .helpers import printToFile
from ..helpers import printToFile
from .utils import FileHeader, ParameterDeclarations, Catalog, TrafficSignalController
from .enumerations import VersionBase, XMLNS, XSI
from .entities import Entities
Expand Down Expand Up @@ -108,15 +108,18 @@ def get_element(self):

return element

def write_xml(self,filename,prettyprint = True):
""" writeXml writes the open scenario xml file
def write_xml(self, filename, prettyprint = True, encoding = 'utf-8'):
""" write_xml writes the OpenSCENARIO xml file
Parameters
----------
filename (str): path and filename of the wanted xml file
prettyprint (bool): pretty print or ugly print?
Default: True
encoding (str): specifies the output encoding
Default: 'utf-8'
"""
printToFile(self.get_element(),filename,prettyprint)
Expand Down
2 changes: 1 addition & 1 deletion scenariogeneration/xosc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from numpy.lib.arraysetops import isin
from .exceptions import OpenSCENARIOVersionError
import xml.etree.ElementTree as ET
from .helpers import printToFile
from ..helpers import printToFile

from .enumerations import ParameterType, Rule, ReferenceContext, DynamicsShapes, DynamicsDimension, RouteStrategy,XSI,XMLNS, VehicleCategory,PrecipitationType,CloudState, VersionBase, SpeedTargetValueType
import datetime as dt
Expand Down

0 comments on commit a1d1e16

Please sign in to comment.