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.
 
 
 
 
 

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