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.
 
 
 
 
 

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