Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

169 Zeilen
4.1 KiB

  1. """
  2. resources.py provides useful tools for resources processing.
  3. There are 2 commands available.
  4. - clean: clean and unify the resources file names with some rules.
  5. - round: generate the rounded images from the original squared images.
  6. """
  7. import os
  8. import subprocess
  9. import sys
  10. import config as cfg
  11. from . import resource_dir
  12. _usage = "Usage: resources.py <cmd> <pvd>"
  13. def cleaner_onprem(f):
  14. f = f.replace("_", "-")
  15. return f.lower()
  16. def cleaner_aws(f):
  17. f = f.replace("_", "-")
  18. f = f.replace("@4x", "")
  19. f = f.replace("-light-bg", "")
  20. for p in cfg.FILE_PREFIXES["aws"]:
  21. if f.startswith(p):
  22. f = f[len(p):]
  23. break
  24. return f.lower()
  25. def cleaner_azure(f):
  26. f = f.replace("_", "-")
  27. f = f.replace("(", "").replace(")", "")
  28. f = "-".join(f.split())
  29. for p in cfg.FILE_PREFIXES["azure"]:
  30. if f.startswith(p):
  31. f = f[len(p):]
  32. break
  33. return f.lower()
  34. def cleaner_gcp(f):
  35. f = f.replace("_", "-")
  36. f = "-".join(f.split())
  37. for p in cfg.FILE_PREFIXES["gcp"]:
  38. if f.startswith(p):
  39. f = f[len(p):]
  40. break
  41. return f.lower()
  42. def cleaner_k8s(f):
  43. f = f.replace("-256", "")
  44. for p in cfg.FILE_PREFIXES["k8s"]:
  45. if f.startswith(p):
  46. f = f[len(p):]
  47. break
  48. return f.lower()
  49. def cleaner_alibabacloud(f):
  50. for p in cfg.FILE_PREFIXES["alibabacloud"]:
  51. if f.startswith(p):
  52. f = f[len(p):]
  53. break
  54. return f.lower()
  55. def cleaner_oci(f):
  56. f = f.replace("_", "-")
  57. f = f.replace("-grey", "")
  58. for p in cfg.FILE_PREFIXES["oci"]:
  59. if f.startswith(p):
  60. f = f[len(p):]
  61. break
  62. return f.lower()
  63. cleaners = {
  64. "onprem": cleaner_onprem,
  65. "aws": cleaner_aws,
  66. "azure": cleaner_azure,
  67. "gcp": cleaner_gcp,
  68. "k8s": cleaner_k8s,
  69. "alibabacloud": cleaner_alibabacloud,
  70. "oci": cleaner_oci,
  71. }
  72. def clean_png(pvd: str) -> None:
  73. """Refine the resources files names."""
  74. def _rename(base: str, png: str):
  75. new = cleaners[pvd](png)
  76. old_path = os.path.join(base, png)
  77. new_path = os.path.join(base, new)
  78. os.rename(old_path, new_path)
  79. for root, _, files in os.walk(resource_dir(pvd)):
  80. pngs = filter(lambda f: f.endswith(".png"), files)
  81. [_rename(root, png) for png in pngs]
  82. def round_png(pvd: str) -> None:
  83. """Round the images."""
  84. def _round(base: str, path: str):
  85. path = os.path.join(base, path)
  86. subprocess.call([cfg.CMD_ROUND, *cfg.CMD_ROUND_OPTS, path])
  87. for root, _, files in os.walk(resource_dir(pvd)):
  88. pngs = filter(lambda f: f.endswith(".png"), files)
  89. paths = filter(lambda f: "rounded" not in f, pngs)
  90. [_round(root, path) for path in paths]
  91. def svg2png(pvd: str) -> None:
  92. """Convert the svg into png"""
  93. def _convert(base: str, path: str):
  94. path = os.path.join(base, path)
  95. subprocess.call([cfg.CMD_SVG2PNG, *cfg.CMD_SVG2PNG_OPTS, path])
  96. subprocess.call(['rm', path])
  97. for root, _, files in os.walk(resource_dir(pvd)):
  98. svgs = filter(lambda f: f.endswith(".svg"), files)
  99. [_convert(root, path) for path in svgs]
  100. def svg2png2(pvd: str) -> None:
  101. """Convert the svg into png using image magick"""
  102. def _convert(base: str, path: str):
  103. path_src = os.path.join(base, path)
  104. path_dest = path_src.replace(".svg", ".png")
  105. subprocess.call([cfg.CMD_SVG2PNG_IM, *cfg.CMD_SVG2PNG_IM_OPTS, path_src, path_dest])
  106. subprocess.call(['rm', path_src])
  107. for root, _, files in os.walk(resource_dir(pvd)):
  108. svgs = filter(lambda f: f.endswith(".svg"), files)
  109. [_convert(root, path) for path in svgs]
  110. # fmt: off
  111. commands = {
  112. "clean": clean_png,
  113. "round": round_png,
  114. "svg2png": svg2png,
  115. "svg2png2": svg2png2,
  116. }
  117. # fmt: on
  118. if __name__ == "__main__":
  119. if len(sys.argv) < 3:
  120. print(_usage)
  121. sys.exit()
  122. cmd = sys.argv[1]
  123. pvd = sys.argv[2]
  124. if cmd not in commands:
  125. sys.exit()
  126. if pvd not in cfg.PROVIDERS:
  127. sys.exit()
  128. commands[cmd](pvd)