daemon: refactoring to remove usage of os.path where possible and pathlib.Path instead

This commit is contained in:
Blake Harnden 2021-03-19 16:54:24 -07:00
parent d0a55dd471
commit 1c970bbe00
38 changed files with 520 additions and 606 deletions

View file

@ -1,4 +1,5 @@
import logging
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, Generic, List, Optional, Type, TypeVar
from lxml import etree
@ -25,7 +26,7 @@ T = TypeVar("T")
def write_xml_file(
xml_element: etree.Element, file_path: str, doctype: str = None
xml_element: etree.Element, file_path: Path, doctype: str = None
) -> None:
xml_data = etree.tostring(
xml_element,
@ -34,8 +35,8 @@ def write_xml_file(
encoding="UTF-8",
doctype=doctype,
)
with open(file_path, "wb") as xml_file:
xml_file.write(xml_data)
with file_path.open("wb") as f:
f.write(xml_data)
def get_type(element: etree.Element, name: str, _type: Generic[T]) -> Optional[T]:
@ -293,13 +294,12 @@ class CoreXmlWriter:
self.write_session_metadata()
self.write_default_services()
def write(self, file_name: str) -> None:
self.scenario.set("name", file_name)
def write(self, path: Path) -> None:
self.scenario.set("name", str(path))
# write out generated xml
xml_tree = etree.ElementTree(self.scenario)
xml_tree.write(
file_name, xml_declaration=True, pretty_print=True, encoding="UTF-8"
str(path), xml_declaration=True, pretty_print=True, encoding="UTF-8"
)
def write_session_origin(self) -> None:
@ -580,8 +580,8 @@ class CoreXmlReader:
self.session: "Session" = session
self.scenario: Optional[etree.ElementTree] = None
def read(self, file_name: str) -> None:
xml_tree = etree.parse(file_name)
def read(self, file_path: Path) -> None:
xml_tree = etree.parse(str(file_path))
self.scenario = xml_tree.getroot()
# read xml session content

View file

@ -1,5 +1,5 @@
import logging
import os
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple
@ -54,7 +54,7 @@ def _value_to_params(value: str) -> Optional[Tuple[str]]:
def create_file(
xml_element: etree.Element,
doc_name: str,
file_path: str,
file_path: Path,
server: DistributedServer = None,
) -> None:
"""
@ -71,10 +71,11 @@ def create_file(
)
if server:
temp = NamedTemporaryFile(delete=False)
corexml.write_xml_file(xml_element, temp.name, doctype=doctype)
temp_path = Path(temp.name)
corexml.write_xml_file(xml_element, temp_path, doctype=doctype)
temp.close()
server.remote_put(temp.name, file_path)
os.unlink(temp.name)
server.remote_put(temp_path, file_path)
temp_path.unlink()
else:
corexml.write_xml_file(xml_element, file_path, doctype=doctype)
@ -92,9 +93,9 @@ def create_node_file(
:return:
"""
if isinstance(node, CoreNode):
file_path = os.path.join(node.nodedir, file_name)
file_path = node.nodedir / file_name
else:
file_path = os.path.join(node.session.session_dir, file_name)
file_path = node.session.session_dir / file_name
create_file(xml_element, doc_name, file_path, node.server)
@ -316,7 +317,7 @@ def create_event_service_xml(
group: str,
port: str,
device: str,
file_directory: str,
file_directory: Path,
server: DistributedServer = None,
) -> None:
"""
@ -340,8 +341,7 @@ def create_event_service_xml(
):
sub_element = etree.SubElement(event_element, name)
sub_element.text = value
file_name = "libemaneeventservice.xml"
file_path = os.path.join(file_directory, file_name)
file_path = file_directory / "libemaneeventservice.xml"
create_file(event_element, "emaneeventmsgsvc", file_path, server)