"""Module specifies functionality for parsing XML files."""

import urllib.request

from lxml import etree

from cve_connector.vendor_cve.implementation.parsers.general_and_format_parsers\
    .general_parser import GeneralParser


class XmlParser(GeneralParser):
    """
    Class responsible for parsing XML files.
    """
    def __init__(self, url, from_date=None, to_date=None):
        super().__init__(url, from_date, to_date)

    def load_content(self):
        """
        Loads content from URL and assignes to the atribute of class.

        :return: None
        """
        data = self.get_content_from_ulr()
        self.data = etree.XML(data)

    def parse(self):
        pass

    def get_content_from_ulr(self):
        """
        Downloads content from URL.

        :return: downloaded content
        """
        response = urllib.request.urlopen(self.url)
        if response.getcode() != 200:
            raise ConnectionError('Unable to load ', self.url)
        data = response.read()
        response.close()
        return data
