util_funcs.py 3.0 KB

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