util_funcs.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # -*- coding: utf-8 -*-
  2. import platform
  3. import pygame
  4. import os
  5. import subprocess
  6. #from libs import easing
  7. #from datetime import datetime
  8. #import base64
  9. #from beeprint import pp
  10. import string
  11. from Xlib import X,display
  12. import config
  13. from constants import Width,Height
  14. def get_git_revision_hash():
  15. return subprocess.check_output(['git', 'rev-parse', 'HEAD'])
  16. def get_git_revision_short_hash():
  17. return subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'])
  18. def X_center_mouse():
  19. d = display.Display()
  20. s = d.screen()
  21. root = s.root
  22. width = s.width_in_pixels
  23. height = s.height_in_pixels
  24. # print(width,height)
  25. root.warp_pointer(width/2,height/2)
  26. d.sync()
  27. def IsPythonPackage(self,dirname):
  28. files = os.listdir(dirname)
  29. for i in sorted(files):
  30. if i.endswith("__init__.py"):
  31. return True
  32. return False
  33. def IsExecutable(fpath):
  34. return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
  35. def MakeExecutable(path):
  36. mode = os.stat(path).st_mode
  37. mode |= (mode & 0o444) >> 2 # copy R bits to X
  38. os.chmod(path, mode)
  39. def GetExePath():# get self dir
  40. #dir_path = os.path.dirname(os.path.realpath(__file__))
  41. dir_path = os.getcwd()
  42. return dir_path
  43. def CmdClean(cmdpath):#escape spec chars
  44. spchars = "\\`$();|{}&'\"*?<>[]!^~-#\n\r "
  45. for i in spchars:
  46. cmdpath = string.replace(cmdpath,i,"\\"+i)
  47. return cmdpath
  48. def ReplaceSuffix(orig_file_str,new_ext):
  49. filename,ext = os.path.splitext(orig_file_str)
  50. ext = ext.strip()
  51. if ext != "":
  52. return "%s.%s"%(filename,new_ext)
  53. def FileExists(name): # both file and dir checked
  54. return os.path.exists(name)
  55. def ReadTheFileContent(filename):
  56. data = ""
  57. with open(filename, 'r') as myfile:
  58. data = myfile.read()
  59. return data
  60. def midRect(x,y,width,height,canWidth,canHeight):
  61. return pygame.Rect(min(canWidth,x-width/2),min(canHeight,y-height/2),width,height)
  62. #surface color change
  63. def color_surface(surface, color):
  64. red = color.r
  65. green = color.g
  66. blue = color.b
  67. arr = pygame.surfarray.pixels3d(surface)
  68. arr[:,:,0] = red
  69. arr[:,:,1] = green
  70. arr[:,:,2] = blue
  71. def DrawText(canvas,text, x,y,width,height,canWidth,canHeight,fontObj):# text for content,fontObj for pygame.font.Font
  72. _w = 0
  73. _tp = len(text)
  74. for idx,t in enumerate(fontObj.metrics(text)):
  75. _w = _w + t[1] - t[0]
  76. if _w > icon_width:
  77. _tp = idx
  78. break
  79. width = _w #recalc the width of text
  80. if width > icon_width: ##Label width max is icon width
  81. width = icon_width
  82. if _tp < len(text):##cut the text to fit width
  83. text = text[0:_tp]
  84. canvas.blit(fontObj.render(text,True,(83,83,83)),midRect(x,y,width,height,canWidth,canHeight))
  85. def SwapAndShow():
  86. screen = pygame.display.get_surface()
  87. if config.GlobalScale > 1:
  88. pygame.transform.scale(config.GlobalCanvas,(Width*config.GlobalScale,Height*config.GlobalScale),config.GlobalCanvas2)
  89. screen.blit(config.GlobalCanvas2,(0,0,Width*config.GlobalScale,Height*config.GlobalScale))
  90. elif config.GlobalScale == 1:
  91. screen.blit(config.GlobalCanvas,(0,0,Width,Height))
  92. pygame.display.update()
  93. def ArmSystem(cmd):
  94. if "arm" not in platform.machine():
  95. return
  96. os.system(cmd)
  97. def InGameShell():
  98. if "arm" not in platform.machine():
  99. return True
  100. else:
  101. return False