Skip to content

Commit

Permalink
grpc: adjustment to have save xml provide the file as byte data vs st…
Browse files Browse the repository at this point in the history
…ring, updated grpc server to save data directly vs using a tmp file
  • Loading branch information
bharnden committed Sep 9, 2023
1 parent c68b74e commit bfbf1e6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion daemon/core/api/grpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ def save_xml(self, session_id: int, file_path: str) -> None:
"""
request = core_pb2.SaveXmlRequest(session_id=session_id)
response = self.stub.SaveXml(request)
with open(file_path, "w") as xml_file:
with open(file_path, "wb") as xml_file:
xml_file.write(response.data)

def open_xml(self, file_path: Path, start: bool = False) -> tuple[bool, int]:
Expand Down
7 changes: 3 additions & 4 deletions daemon/core/api/grpc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
from core.nodes.network import CoreNetwork, WlanNode
from core.nodes.wireless import WirelessNode
from core.services.coreservices import ServiceManager
from core.xml.corexml import CoreXmlWriter

logger = logging.getLogger(__name__)
_ONE_DAY_IN_SECONDS: int = 60 * 60 * 24
Expand Down Expand Up @@ -1163,10 +1164,8 @@ def SaveXml(
"""
logger.debug("save xml: %s", request)
session = self.get_session(request.session_id, context)
_, temp_path = tempfile.mkstemp()
session.save_xml(Path(temp_path))
with open(temp_path, "r") as xml_file:
data = xml_file.read()
xml_writer = CoreXmlWriter(session)
data = xml_writer.get_data()
return core_pb2.SaveXmlResponse(data=data)

def OpenXml(
Expand Down
12 changes: 7 additions & 5 deletions daemon/core/xml/corexml.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,16 @@ def write_session(self) -> None:
self.write_session_metadata()
self.write_default_services()

def write(self, path: Path) -> None:
self.scenario.set("name", str(path))
# write out generated xml
def get_data(self) -> bytes:
xml_tree = etree.ElementTree(self.scenario)
xml_tree.write(
str(path), xml_declaration=True, pretty_print=True, encoding="UTF-8"
return etree.tostring(
xml_tree, xml_declaration=True, pretty_print=True, encoding="UTF-8"
)

def write(self, path: Path) -> None:
data = self.get_data()
path.write_bytes(data)

def write_session_origin(self) -> None:
# origin: geolocation of cartesian coordinate 0,0,0
lat, lon, alt = self.session.location.refgeo
Expand Down
2 changes: 1 addition & 1 deletion daemon/proto/core/api/grpc/core.proto
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ message SaveXmlRequest {
}

message SaveXmlResponse {
string data = 1;
bytes data = 1;
}

message OpenXmlRequest {
Expand Down

0 comments on commit bfbf1e6

Please sign in to comment.