supports.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- coding: utf-8 -*-
  2. __author__ = 'wanger'
  3. __date__ = '2024-08-20'
  4. __copyright__ = '(C) 2024 by siwei'
  5. __revision__ = '1.0'
  6. import os
  7. import re
  8. from tempfile import mkstemp
  9. from typing import Dict
  10. from zipfile import ZipFile
  11. import xml.etree.ElementTree as ET
  12. def prepare_zip_file(name: str, data: Dict) -> str:
  13. fd, path = mkstemp()
  14. zip_file = ZipFile(path, "w", allowZip64=True)
  15. print(fd, path, zip_file, data)
  16. for ext, stream in data.items():
  17. fname = "{}.{}".format(name, ext)
  18. if isinstance(stream, str):
  19. zip_file.write(stream, fname)
  20. else:
  21. zip_file.writestr(fname, stream.read())
  22. zip_file.close()
  23. os.close(fd)
  24. return path
  25. def is_valid_xml(xml_string: str) -> bool:
  26. try:
  27. # Attempt to parse the XML string
  28. ET.fromstring(xml_string)
  29. return True
  30. except ET.ParseError:
  31. return False
  32. def is_surrounded_by_quotes(text, param):
  33. # The regex pattern searches for '%foo%' surrounded by single quotes.
  34. # It uses \'%foo%\' to match '%foo%' literally, including the single quotes.
  35. pattern = rf"\'%{param}%\'"
  36. # re.search() searches the string for the first location where the regex pattern produces a match.
  37. # If a match is found, re.search() returns a match object. Otherwise, it returns None.
  38. match = re.search(pattern, text)
  39. # Return True if a match is found, False otherwise.
  40. return bool(match)