Skip to content

Commit 796350f

Browse files
committed
[Junos] Make _load_candidate() support loading of XML files with headers that have encoding information
1 parent dde6a59 commit 796350f

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

napalm/junos/junos.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,21 @@ def _detect_config_format(self, config):
233233
return fmt
234234

235235
def _load_candidate(self, filename, config, overwrite):
236+
fmt = None
236237
if filename is None:
237238
configuration = config
238239
else:
239240
with open(filename) as f:
240-
configuration = f.read()
241+
try:
242+
configuration = etree.parse(f).getroot()
243+
fmt = "xml"
244+
except etree.XMLSyntaxError:
245+
configuration = f.read()
246+
247+
if not fmt:
248+
fmt = self._detect_config_format(configuration)
249+
if fmt == "xml":
250+
configuration = etree.XML(configuration)
241251

242252
if (
243253
not self.lock_disable
@@ -248,11 +258,6 @@ def _load_candidate(self, filename, config, overwrite):
248258
self._lock()
249259

250260
try:
251-
fmt = self._detect_config_format(configuration)
252-
253-
if fmt == "xml":
254-
configuration = etree.XML(configuration)
255-
256261
if self.config_private:
257262
try:
258263
self.device.rpc.open_configuration(private=True, normalize=True)

0 commit comments

Comments
 (0)