You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

201 lines
4.7 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-bg4x", "")
  20. f = f.replace("-light-bg", "")
  21. for p in cfg.FILE_PREFIXES["aws"]:
  22. if f.startswith(p):
  23. f = f[len(p) :]
  24. break
  25. return f.lower()
  26. def cleaner_azure(f):
  27. f = f.replace("_", "-")
  28. f = f.replace("(", "").replace(")", "")
  29. f = "-".join(f.split())
  30. for p in cfg.FILE_PREFIXES["azure"]:
  31. if f.startswith(p):
  32. f = f[len(p) :]
  33. break
  34. return f.lower()
  35. def cleaner_gcp(f):
  36. f = f.replace("_", "-")
  37. f = "-".join(f.split())
  38. for p in cfg.FILE_PREFIXES["gcp"]:
  39. if f.startswith(p):
  40. f = f[len(p) :]
  41. break
  42. return f.lower()
  43. def cleaner_firebase(f):
  44. f = f.replace("_", "-")
  45. f = "-".join(f.split())
  46. for p in cfg.FILE_PREFIXES["firebase"]:
  47. if f.startswith(p):
  48. f = f[len(p) :]
  49. break
  50. return f.lower()
  51. def cleaner_k8s(f):
  52. f = f.replace("-256", "")
  53. for p in cfg.FILE_PREFIXES["k8s"]:
  54. if f.startswith(p):
  55. f = f[len(p) :]
  56. break
  57. return f.lower()
  58. def cleaner_alibabacloud(f):
  59. for p in cfg.FILE_PREFIXES["alibabacloud"]:
  60. if f.startswith(p):
  61. f = f[len(p) :]
  62. break
  63. return f.lower()
  64. def cleaner_oci(f):
  65. f = f.replace("_", "-")
  66. f = f.replace("-red", "")
  67. for p in cfg.FILE_PREFIXES["oci"]:
  68. if f.startswith(p):
  69. f = f[len(p) :]
  70. break
  71. return f.lower()
  72. def cleaner_programming(f):
  73. return f.lower()
  74. def cleaner_generic(f):
  75. return f.lower()
  76. def cleaner_saas(f):
  77. return f.lower()
  78. def cleaner_elastic(f):
  79. return f.lower()
  80. cleaners = {
  81. "onprem": cleaner_onprem,
  82. "aws": cleaner_aws,
  83. "azure": cleaner_azure,
  84. "gcp": cleaner_gcp,
  85. "firebase": cleaner_firebase,
  86. "k8s": cleaner_k8s,
  87. "alibabacloud": cleaner_alibabacloud,
  88. "oci": cleaner_oci,
  89. "programming": cleaner_programming,
  90. "saas": cleaner_saas,
  91. "elastic": cleaner_elastic,
  92. "generic": cleaner_generic,
  93. }
  94. def clean_png(pvd: str) -> None:
  95. """Refine the resources files names."""
  96. def _rename(base: str, png: str):
  97. new = cleaners[pvd](png)
  98. old_path = os.path.join(base, png)
  99. new_path = os.path.join(base, new)
  100. os.rename(old_path, new_path)
  101. for root, _, files in os.walk(resource_dir(pvd)):
  102. pngs = filter(lambda f: f.endswith(".png"), files)
  103. [_rename(root, png) for png in pngs]
  104. def round_png(pvd: str) -> None:
  105. """Round the images."""
  106. def _round(base: str, path: str):
  107. path = os.path.join(base, path)
  108. subprocess.call([cfg.CMD_ROUND, *cfg.CMD_ROUND_OPTS, path])
  109. for root, _, files in os.walk(resource_dir(pvd)):
  110. pngs = filter(lambda f: f.endswith(".png"), files)
  111. paths = filter(lambda f: "rounded" not in f, pngs)
  112. [_round(root, path) for path in paths]
  113. def svg2png(pvd: str) -> None:
  114. """Convert the svg into png"""
  115. def _convert(base: str, path: str):
  116. path = os.path.join(base, path)
  117. subprocess.call([cfg.CMD_SVG2PNG, *cfg.CMD_SVG2PNG_OPTS, path])
  118. subprocess.call(["rm", path])
  119. for root, _, files in os.walk(resource_dir(pvd)):
  120. svgs = filter(lambda f: f.endswith(".svg"), files)
  121. [_convert(root, path) for path in svgs]
  122. def svg2png2(pvd: str) -> None:
  123. """Convert the svg into png using image magick"""
  124. def _convert(base: str, path: str):
  125. path_src = os.path.join(base, path)
  126. path_dest = path_src.replace(".svg", ".png")
  127. subprocess.call([cfg.CMD_SVG2PNG_IM, *cfg.CMD_SVG2PNG_IM_OPTS, path_src, path_dest])
  128. subprocess.call(["rm", path_src])
  129. for root, _, files in os.walk(resource_dir(pvd)):
  130. svgs = filter(lambda f: f.endswith(".svg"), files)
  131. [_convert(root, path) for path in svgs]
  132. # fmt: off
  133. commands = {
  134. "clean": clean_png,
  135. "round": round_png,
  136. "svg2png": svg2png,
  137. "svg2png2": svg2png2,
  138. }
  139. # fmt: on
  140. if __name__ == "__main__":
  141. if len(sys.argv) < 3:
  142. print(_usage)
  143. sys.exit()
  144. cmd = sys.argv[1]
  145. pvd = sys.argv[2]
  146. if cmd not in commands:
  147. sys.exit()
  148. if pvd not in cfg.PROVIDERS:
  149. sys.exit()
  150. commands[cmd](pvd)