# -*- coding: utf-8 -*-

__author__ = 'wanger'
__date__ = '2024-08-20'
__copyright__ = '(C) 2024 by siwei'
__revision__ = '1.0'

import os
import re
from tempfile import mkstemp
from typing import Dict
from zipfile import ZipFile
import xml.etree.ElementTree as ET


def prepare_zip_file(name: str, data: Dict) -> str:
    fd, path = mkstemp()
    zip_file = ZipFile(path, "w", allowZip64=True)
    print(fd, path, zip_file, data)
    for ext, stream in data.items():
        fname = "{}.{}".format(name, ext)
        if isinstance(stream, str):
            zip_file.write(stream, fname)
        else:
            zip_file.writestr(fname, stream.read())
    zip_file.close()
    os.close(fd)
    return path


def is_valid_xml(xml_string: str) -> bool:
    try:
        # Attempt to parse the XML string
        ET.fromstring(xml_string)
        return True
    except ET.ParseError:
        return False


def is_surrounded_by_quotes(text, param):
    # The regex pattern searches for '%foo%' surrounded by single quotes.
    # It uses \'%foo%\' to match '%foo%' literally, including the single quotes.
    pattern = rf"\'%{param}%\'"

    # re.search() searches the string for the first location where the regex pattern produces a match.
    # If a match is found, re.search() returns a match object. Otherwise, it returns None.
    match = re.search(pattern, text)

    # Return True if a match is found, False otherwise.
    return bool(match)
