find_styles.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright 2005-2014 Michael Rice <michael@michaelrice.org>
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Module to install, remove, and set styles for fluxbox"""
  15. import tarfile
  16. import re
  17. import os
  18. from os.path import expanduser
  19. from os import system
  20. from shutil import rmtree, copyfile
  21. from sys import stdout
  22. def set_style(style, location):
  23. """Select style and create entry in init file to reflect,
  24. then restart flux for change to take place
  25. """
  26. if location == "default":
  27. location = "~/.fluxbox/styles"
  28. location = expanduser(location)
  29. new_style_name = "session.styleFile:\t{0}/{1}\n".format(location, style)
  30. old_style_name = ""
  31. local_init = expanduser("~/.fluxbox/init")
  32. backup_init = expanduser("~/.fluxbox/init.bckp")
  33. copyfile(local_init, backup_init)
  34. init_file = open(backup_init,"r")
  35. init_file_lines = init_file.readlines()
  36. init_file.close()
  37. init_file_backup = open(backup_init, "r")
  38. style_line = re.compile(r"session.styleFile")
  39. for line in init_file_lines:
  40. if style_line.search(line):
  41. old_style_name = line
  42. output = stdout
  43. output = open(local_init, "w")
  44. for line in init_file_backup.readlines():
  45. output.write(line.replace(old_style_name, new_style_name))
  46. output.close()
  47. init_file_backup.close()
  48. # attempt to not have to make a seperate fedora package for odd name
  49. # 'fluxbox-bin'
  50. #~ system('kill -s USR1 `xprop -root _BLACKBOX_PID | awk \'{print $3}\'`')
  51. # some styles (font colours mostly) don't apply properly after USR2
  52. # so USR1 is required = fluxbox restart
  53. system('kill -s USR1 `xprop -root _BLACKBOX_PID | awk \'{print $3}\'`')
  54. return
  55. def install_style(style_file):
  56. """Install a valid tar.gz or tar.bz2 style foo.tar.gz/foo.tar.bz2 we
  57. check to see if it was packaged as styleFoo/ or
  58. as ~/.fluxbox/styles/styleFoo people package both ways
  59. """
  60. for i in style_file:
  61. ins_dir = expanduser("~/.fluxbox/styles")
  62. if tarfile.is_tarfile(i) == True:
  63. # try first for bz2
  64. try:
  65. tar = tarfile.open(i, "r:bz2")
  66. #maybe its tar.gz
  67. except tarfile.ReadError:
  68. try:
  69. tar = tarfile.open(i, "r:gz")
  70. #this isnt a bz2 or gz, so wtf is it?
  71. except tarfile.ReadError:
  72. #now return 2 to say weird file type..
  73. #TODO: create exception class and rasie that
  74. return False
  75. #we need to find out how the style was packaged
  76. #if it is ~/.fluxbox/styles/styleName then we need a new
  77. #install dir. otherwise use default.
  78. check = tar.getnames()
  79. pat = re.compile('^\.fluxbox/styles/.+')
  80. if pat.match(check[0]) == None:
  81. for i in tar:
  82. tar.extract(i, ins_dir)
  83. else:
  84. ins_dir = expanduser("~/")
  85. for i in tar:
  86. tar.extract(i, ins_dir)
  87. else:
  88. # 2 == it wasnt even a tar file at all. This is a double check,
  89. # we filter the file types in the file chooser to allow only
  90. # tar.gz and tar.bz2
  91. #TODO: raise an exception instead
  92. return 2
  93. return
  94. def remove_style(style_file, location):
  95. """This can be used to remove a style"""
  96. if location == "default":
  97. location = "~/.fluxbox/styles"
  98. location = expanduser(location)
  99. if os.access(location + "/" + style_file, os.W_OK):
  100. rmtree(location + "/" + style_file)
  101. return True
  102. else:
  103. #TODO: raise exception here
  104. return False