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.

пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. cleaners = {
  102. "onprem": cleaner_onprem,
  103. "aws": cleaner_aws,
  104. "azure": cleaner_azure,
  105. "digitalocean": cleaner_digitalocean,
  106. "gcp": cleaner_gcp,
  107. "ibm": cleaner_ibm,
  108. "firebase": cleaner_firebase,
  109. "k8s": cleaner_k8s,
  110. "alibabacloud": cleaner_alibabacloud,
  111. "oci": cleaner_oci,
  112. "programming": cleaner_programming,
  113. "saas": cleaner_saas,
  114. "elastic": cleaner_elastic,
  115. "outscale": cleaner_outscale,
  116. "generic": cleaner_generic,
  117. "openstack": cleaner_openstack,
  118. }
  119. def clean_png(pvd: str) -> None:
  120. """Refine the resources files names."""
  121. def _rename(base: str, png: str):
  122. new = cleaners[pvd](png)
  123. old_path = os.path.join(base, png)
  124. new_path = os.path.join(base, new)
  125. os.rename(old_path, new_path)
  126. for root, _, files in os.walk(resource_dir(pvd)):
  127. pngs = filter(lambda f: f.endswith(".png"), files)
  128. [_rename(root, png) for png in pngs]
  129. def round_png(pvd: str) -> None:
  130. """Round the images."""
  131. def _round(base: str, path: str):
  132. path = os.path.join(base, path)
  133. subprocess.run([cfg.CMD_ROUND, *cfg.CMD_ROUND_OPTS, path])
  134. for root, _, files in os.walk(resource_dir(pvd)):
  135. pngs = filter(lambda f: f.endswith(".png"), files)
  136. paths = filter(lambda f: "rounded" not in f, pngs)
  137. [_round(root, path) for path in paths]
  138. def svg2png(pvd: str) -> None:
  139. """Convert the svg into png"""
  140. def _convert(base: str, path: str):
  141. path = os.path.join(base, path)
  142. subprocess.run([cfg.CMD_SVG2PNG, *cfg.CMD_SVG2PNG_OPTS, path])
  143. subprocess.run(["rm", path])
  144. for root, _, files in os.walk(resource_dir(pvd)):
  145. svgs = filter(lambda f: f.endswith(".svg"), files)
  146. [_convert(root, path) for path in svgs]
  147. def svg2png2(pvd: str) -> None:
  148. """Convert the svg into png using image magick"""
  149. def _convert(base: str, path: str):
  150. path_src = os.path.join(base, path)
  151. path_dest = path_src.replace(".svg", ".png")
  152. subprocess.run([cfg.CMD_SVG2PNG_IM, *cfg.CMD_SVG2PNG_IM_OPTS, path_src, path_dest])
  153. subprocess.run(["rm", path_src])
  154. for root, _, files in os.walk(resource_dir(pvd)):
  155. svgs = filter(lambda f: f.endswith(".svg"), files)
  156. [_convert(root, path) for path in svgs]
  157. # fmt: off
  158. commands = {
  159. "clean": clean_png,
  160. "round": round_png,
  161. "svg2png": svg2png,
  162. "svg2png2": svg2png2,
  163. }
  164. # fmt: on
  165. if __name__ == "__main__":
  166. if len(sys.argv) < 3:
  167. print(_usage)
  168. sys.exit()
  169. cmd = sys.argv[1]
  170. pvd = sys.argv[2]
  171. if cmd not in commands:
  172. sys.exit()
  173. if pvd not in cfg.PROVIDERS:
  174. sys.exit()
  175. commands[cmd](pvd)