1
0
mirror of https://github.com/mbirth/tcl_ota_check.git synced 2024-11-10 06:16:46 +00:00
tcl_ota_check/tcllib/requests/tclresult.py

92 lines
3.1 KiB
Python
Raw Normal View History

2018-02-11 01:38:38 +00:00
#!/usr/bin/env python3
2018-02-08 00:15:58 +00:00
# -*- coding: utf-8 -*-
2018-02-11 01:38:38 +00:00
"""Generic TCL API result handlers."""
2018-02-10 01:38:06 +00:00
import xml.dom.minidom
2018-02-08 00:15:58 +00:00
from defusedxml import ElementTree
2018-02-11 00:48:47 +00:00
from .. import dumpmgr
2018-02-08 23:17:08 +00:00
2018-02-08 00:15:58 +00:00
class TclResult:
2018-02-11 01:38:38 +00:00
"""Generic TCL API result."""
2018-02-11 00:56:56 +00:00
def __init__(self, xmlstr: str):
2018-02-11 01:38:38 +00:00
"""Populate variables."""
2018-02-11 00:56:56 +00:00
self.raw_xml = xmlstr
2018-02-08 23:17:08 +00:00
self.dumper = dumpmgr.DumpMgr()
2018-02-11 00:56:56 +00:00
self.dumper.write_dump(xmlstr)
2018-02-08 23:17:08 +00:00
def delete_dump(self):
2018-02-11 01:38:38 +00:00
"""Delete last dump."""
2018-02-08 23:17:08 +00:00
self.dumper.delete_last_dump()
2018-02-08 00:15:58 +00:00
2018-02-10 01:38:06 +00:00
def pretty_xml(self):
"""Return prettified input XML with ``xml.dom.minidom``."""
mdx = xml.dom.minidom.parseString(self.raw_xml)
return mdx.toprettyxml(indent=" ")
2018-02-11 01:38:38 +00:00
2018-02-08 00:15:58 +00:00
class CheckResult(TclResult):
2018-02-11 01:38:38 +00:00
"""Handle check request result."""
2018-02-11 00:56:56 +00:00
def __init__(self, xmlstr: str):
2018-02-11 01:38:38 +00:00
"""Extract data from check request result."""
2018-02-11 00:56:56 +00:00
super().__init__(xmlstr)
root = ElementTree.fromstring(xmlstr)
2018-02-08 00:15:58 +00:00
self.curef = root.find("CUREF").text
self.fvver = root.find("VERSION").find("FV").text
self.tvver = root.find("VERSION").find("TV").text
self.fw_id = root.find("FIRMWARE").find("FW_ID").text
fileinfo = root.find("FIRMWARE").find("FILESET").find("FILE")
self.fileid = fileinfo.find("FILE_ID").text
self.filename = fileinfo.find("FILENAME").text
self.filesize = fileinfo.find("SIZE").text
self.filehash = fileinfo.find("CHECKSUM").text
2018-02-10 01:38:27 +00:00
2018-02-11 01:38:38 +00:00
2018-02-10 01:38:27 +00:00
class DownloadResult(TclResult):
2018-02-11 01:38:38 +00:00
"""Handle download request result."""
2018-02-11 00:56:56 +00:00
def __init__(self, xmlstr: str):
2018-02-11 01:38:38 +00:00
"""Extract data from download request result."""
2018-02-11 00:56:56 +00:00
super().__init__(xmlstr)
root = ElementTree.fromstring(xmlstr)
2018-02-10 01:38:27 +00:00
file = root.find("FILE_LIST").find("FILE")
self.fileid = file.find("FILE_ID").text
self.fileurl = file.find("DOWNLOAD_URL").text
s3_fileurl_node = file.find("S3_DOWNLOAD_URL")
self.s3_fileurl = None
2018-02-10 02:11:53 +00:00
if s3_fileurl_node is not None:
2018-02-10 01:38:27 +00:00
self.s3_fileurl = s3_fileurl_node.text
slave_list = root.find("SLAVE_LIST").findall("SLAVE")
enc_list = root.find("SLAVE_LIST").findall("ENCRYPT_SLAVE")
s3_slave_list = root.find("SLAVE_LIST").findall("S3_SLAVE")
self.slaves = [s.text for s in slave_list]
self.encslaves = [s.text for s in enc_list]
self.s3_slaves = [s.text for s in s3_slave_list]
2018-02-11 01:38:38 +00:00
class ChecksumResult(TclResult):
2018-02-11 01:38:38 +00:00
"""Handle checksum request result."""
2018-02-11 00:56:56 +00:00
def __init__(self, xmlstr: str):
2018-02-11 01:38:38 +00:00
"""Extract data from checksum request result."""
2018-02-11 00:56:56 +00:00
super().__init__(xmlstr)
root = ElementTree.fromstring(xmlstr)
file = root.find("FILE_CHECKSUM_LIST").find("FILE")
self.file_addr = file.find("ADDRESS").text
self.sha1_enc_footer = file.find("ENCRYPT_FOOTER").text
self.sha1_footer = file.find("FOOTER").text
self.sha1_body = file.find("BODY").text
2018-02-11 01:38:38 +00:00
class EncryptHeaderResult(TclResult):
2018-02-11 01:38:38 +00:00
"""Handle encrypted header request result."""
def __init__(self, contents: str):
2018-02-11 01:38:38 +00:00
"""Extract data from encrypted header request result."""
self.rawdata = contents