util_funcs.py 3.4 KB

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