No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

208 líneas
4.8 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_openstack(f):
  83. return f.lower()
  84. cleaners = {
  85. "onprem": cleaner_onprem,
  86. "aws": cleaner_aws,
  87. "azure": cleaner_azure,
  88. "gcp": cleaner_gcp,
  89. "firebase": cleaner_firebase,
  90. "k8s": cleaner_k8s,
  91. "alibabacloud": cleaner_alibabacloud,
  92. "oci": cleaner_oci,
  93. "programming": cleaner_programming,
  94. "saas": cleaner_saas,
  95. "elastic": cleaner_elastic,
  96. "generic": cleaner_generic,
  97. "openstack": cleaner_openstack,
  98. }
  99. def clean_png(pvd: str) -> None:
  100. """Refine the resources files names."""
  101. def _rename(base: str, png: str):
  102. new = cleaners[pvd](png)
  103. old_path = os.path.join(base, png)
  104. new_path = os.path.join(base, new)
  105. os.rename(old_path, new_path)
  106. for root, _, files in os.walk(resource_dir(pvd)):
  107. pngs = filter(lambda f: f.endswith(".png"), files)
  108. [_rename(root, png) for png in pngs]
  109. def round_png(pvd: str) -> None:
  110. """Round the images."""
  111. def _round(base: str, path: str):
  112. path = os.path.join(base, path)
  113. subprocess.call([cfg.CMD_ROUND, *cfg.CMD_ROUND_OPTS, path])
  114. for root, _, files in os.walk(resource_dir(pvd)):
  115. pngs = filter(lambda f: f.endswith(".png"), files)
  116. paths = filter(lambda f: "rounded" not in f, pngs)
  117. [_round(root, path) for path in paths]
  118. def svg2png(pvd: str) -> None:
  119. """Convert the svg into png"""
  120. def _convert(base: str, path: str):
  121. path = os.path.join(base, path)
  122. subprocess.call([cfg.CMD_SVG2PNG, *cfg.CMD_SVG2PNG_OPTS, path])
  123. subprocess.call(["rm", path])
  124. for root, _, files in os.walk(resource_dir(pvd)):
  125. svgs = filter(lambda f: f.endswith(".svg"), files)
  126. [_convert(root, path) for path in svgs]
  127. def svg2png2(pvd: str) -> None:
  128. """Convert the svg into png using image magick"""
  129. def _convert(base: str, path: str):
  130. path_src = os.path.join(base, path)
  131. path_dest = path_src.replace(".svg", ".png")
  132. subprocess.call([cfg.CMD_SVG2PNG_IM, *cfg.CMD_SVG2PNG_IM_OPTS, path_src, path_dest])
  133. subprocess.call(["rm", path_src])
  134. for root, _, files in os.walk(resource_dir(pvd)):
  135. svgs = filter(lambda f: f.endswith(".svg"), files)
  136. [_convert(root, path) for path in svgs]
  137. # fmt: off
  138. commands = {
  139. "clean": clean_png,
  140. "round": round_png,
  141. "svg2png": svg2png,
  142. "svg2png2": svg2png2,
  143. }
  144. # fmt: on
  145. if __name__ == "__main__":
  146. if len(sys.argv) < 3:
  147. print(_usage)
  148. sys.exit()
  149. cmd = sys.argv[1]
  150. pvd = sys.argv[2]
  151. if cmd not in commands:
  152. sys.exit()
  153. if pvd not in cfg.PROVIDERS:
  154. sys.exit()
  155. commands[cmd](pvd)