StringUtils.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import uuid
  2. import os
  3. import platform
  4. from datetime import datetime
  5. import base64
  6. def getConnectionStr(connstr, index):
  7. str = connstr[index]
  8. strlist = str.split("=")
  9. return strlist[1].replace("'", "")
  10. # 获取系统默认下载路径
  11. def get_download_dir():
  12. if platform.system() == "Windows":
  13. return os.path.join(os.path.expanduser("~"), "Downloads")
  14. elif platform.system() == "Darwin":
  15. return os.path.join(os.path.expanduser("~"), "Downloads")
  16. elif platform.system() == "Linux":
  17. return os.path.join(os.path.expanduser("~"), "Downloads")
  18. else:
  19. return None
  20. def get_temp_dir():
  21. return f'D:/temp'
  22. # dict转换str 并进行拼接
  23. def dict_to_str_with_equals(d):
  24. # 使用列表推导式生成键值对字符串列表
  25. pairs = [f"{key}='{value}'" for key, value in d.items()]
  26. # 使用 join 方法将列表中的字符串合并为一个字符串
  27. return ' '.join(pairs)
  28. # postgis数据库连接转字符串
  29. def sourceToDBConfig(source):
  30. result = {
  31. "mode": 2
  32. }
  33. arr = source.split(" ")
  34. for attr in arr:
  35. if attr.__contains__("srid") or attr.__contains__("sslmode"):
  36. continue
  37. if attr.__contains__("="):
  38. cur = attr.split("=")
  39. key = cur[0].replace("'", "")
  40. value = cur[1].replace("'", "").replace("\"", "")
  41. if key == "table":
  42. result[key] = value.split(".")[1]
  43. result["schema"] = value.split(".")[0]
  44. else:
  45. result[key] = value
  46. return result
  47. # 获取UUID
  48. def getUUID():
  49. id = uuid.uuid4().__str__()
  50. id = id.replace("-", "")
  51. return id
  52. # 获取当前时间
  53. def getNow():
  54. # 获取当前时间
  55. current_time = datetime.now()
  56. # 将当前时间转换为 'YYYY-MM-DD HH:MM:SS' 格式
  57. formatted_time = current_time.strftime('%Y-%m-%d %H:%M:%S')
  58. return formatted_time
  59. # base64加密
  60. def base64str(string):
  61. # 将字符串编码为字节
  62. string_bytes = string.encode('utf-8')
  63. # 使用 base64 编码
  64. base64_bytes = base64.b64encode(string_bytes)
  65. # 将编码后的字节转换为字符串
  66. base64_string = base64_bytes.decode('utf-8')
  67. # 输出结果
  68. return base64_string
  69. #base64解密
  70. def enbase64str(base64_string):
  71. # 将 Base64 字符串转换为字节
  72. base64_bytes = base64_string.encode('utf-8')
  73. # 使用 base64 解码
  74. decoded_bytes = base64.b64decode(base64_bytes)
  75. # 将字节解码为原始字符串
  76. decoded_string = decoded_bytes.decode('utf-8')
  77. return decoded_string