Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

117 righe
2.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_aws(f):
  14. f = f.replace("_", "-")
  15. f = f.replace("@4x", "")
  16. for p in cfg.FILE_PREFIXES["aws"]:
  17. if f.startswith(p):
  18. f = f[len(p):]
  19. break
  20. return f.lower()
  21. def cleaner_azure(f):
  22. f = f.replace("_", "-")
  23. f = f.replace("(", "").replace(")", "")
  24. f = "-".join(f.split())
  25. for p in cfg.FILE_PREFIXES["azure"]:
  26. if f.startswith(p):
  27. f = f[len(p):]
  28. break
  29. return f.lower()
  30. def cleaner_gcp(f):
  31. f = f.replace("_", "-")
  32. f = "-".join(f.split())
  33. for p in cfg.FILE_PREFIXES["gcp"]:
  34. if f.startswith(p):
  35. f = f[len(p):]
  36. break
  37. return f.lower()
  38. cleaners = {
  39. "aws": cleaner_aws,
  40. "azure": cleaner_azure,
  41. "gcp": cleaner_gcp,
  42. }
  43. def clean_png(pvd: str) -> None:
  44. """Refine the resources files names."""
  45. def _rename(base: str, png: str):
  46. new = cleaners[pvd](png)
  47. old_path = os.path.join(base, png)
  48. new_path = os.path.join(base, new)
  49. os.rename(old_path, new_path)
  50. for root, _, files in os.walk(resource_dir(pvd)):
  51. pngs = filter(lambda f: f.endswith(".png"), files)
  52. [_rename(root, png) for png in pngs]
  53. def round_png(pvd: str) -> None:
  54. """Round the images."""
  55. def _round(base: str, path: str):
  56. path = os.path.join(base, path)
  57. subprocess.call([cfg.CMD_ROUND, *cfg.CMD_ROUND_OPTS, path])
  58. for root, _, files in os.walk(resource_dir(pvd)):
  59. pngs = filter(lambda f: f.endswith(".png"), files)
  60. paths = filter(lambda f: "rounded" not in f, pngs)
  61. [_round(root, path) for path in paths]
  62. def svg2png(pvd: str) -> None:
  63. """Convert the svg into png"""
  64. def _convert(base: str, path: str):
  65. path = os.path.join(base, path)
  66. subprocess.call([cfg.CMD_SVG2PNG, *cfg.CMD_SVG2PNG_OPTS, path])
  67. subprocess.call(['rm', path])
  68. for root, _, files in os.walk(resource_dir(pvd)):
  69. svgs = filter(lambda f: f.endswith(".svg"), files)
  70. [_convert(root, path) for path in svgs]
  71. # fmt: off
  72. commands = {
  73. "clean": clean_png,
  74. "round": round_png,
  75. "svg2png": svg2png,
  76. }
  77. # fmt: on
  78. if __name__ == "__main__":
  79. if len(sys.argv) < 3:
  80. print(_usage)
  81. sys.exit()
  82. cmd = sys.argv[1]
  83. pvd = sys.argv[2]
  84. if cmd not in commands:
  85. sys.exit()
  86. if pvd not in cfg.PROVIDERS:
  87. sys.exit()
  88. commands[cmd](pvd)