25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

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