parse_config.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. from __future__ import print_function
  15. import os
  16. import re
  17. from os.path import expanduser
  18. def check4_config():
  19. """checks to see if the config file is located in the users home dir"""
  20. folder = expanduser("~/")
  21. config_file = folder+".fluxStyle.rc"
  22. w_ok = os.access(folder, os.W_OK)
  23. f_ok = os.path.isfile(config_file)
  24. if f_ok:
  25. return True
  26. elif not f_ok and w_ok:
  27. write_config()
  28. return 2
  29. #file isnt there and we dont have permission to make it.
  30. elif not w_ok and not f_ok:
  31. return 3
  32. def write_config():
  33. """writes the basic config file in the users home dir"""
  34. config_file_text = """
  35. # No need to add ~/.fluxbox/styles it is the default location and if it is listed it will
  36. # be ignored. Currently the only option supported right now is STYLES_DIRS
  37. # to choose the name that will display in the view menu use the following syntax
  38. # Name,/location:Foo,/other/location:Bar,/another/location
  39. # If the name identifier is left off "Extra Styles" will be used.
  40. # The following line is an example of what to use if you have styles installed in these places
  41. #STYLES_DIRS:Global,/usr/share/fluxbox/styles:Tenners,/usr/share/tenr-de-styles-pkg-1.0/styles/
  42. """
  43. config_file = expanduser("~/.fluxStyle.rc")
  44. config_file = open(config_file, "w")
  45. config_file.write(config_file_text)
  46. config_file.close()
  47. # return 2
  48. def parse_file(config_file):
  49. """read config file place results into a
  50. dict file location provided by caller.
  51. keys = options (USEICONS, ICONPATHS, etc)
  52. values = values from options
  53. config file should be in the form of:
  54. OPTION:values:moreValuse:evenMore
  55. do not end with ":" Comments are "#" as
  56. the first char.
  57. #OPTION:comment
  58. OPTION:notComment #this is not valid comment
  59. """
  60. config_file = expanduser(config_file)
  61. opts = {}
  62. if os.path.isfile(config_file):
  63. match = re.compile(r"^[^#^\n]")
  64. file_handle = open(config_file)
  65. info = file_handle.readlines()
  66. file_handle.close()
  67. keys = []
  68. for lines in info:
  69. if match.findall(lines):
  70. keys.append(lines.strip().split(":"))
  71. if len(keys) == 0:
  72. return False
  73. for i in range(len(keys)):
  74. opts[keys[i][0]] = keys[i][1:]
  75. return opts
  76. else:
  77. return False
  78. if __name__ == "__main__":
  79. CFG = parse_file("~/.fluxStyle.rc")
  80. if not CFG:
  81. write_config()
  82. raise SystemExit("You need to edit the file ~/fluxStyle.rc")
  83. ITEMS = []
  84. for key, value in CFG.iteritems():
  85. if key == "STYLES_DIRS":
  86. for file_location in value:
  87. ITEMS.append(file_location.strip().split(","))
  88. for item in ITEMS:
  89. if len(item) <= 1:
  90. print("default {0}".format(item[0]))
  91. else:
  92. print("{0} {1}".format(item[0], item[1]))