pie_time.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2014-2016 Tomek Wójcik <tomek@bthlabs.pl>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21. #
  22. import ConfigParser
  23. import imp
  24. import os
  25. import sys
  26. import traceback
  27. RET_OK = 0
  28. RET_NO_ARGS = 1
  29. RET_ERROR = 99
  30. CONFIG_SECTION_PIE_TIME = 'PieTime'
  31. CONFIG_SECTION_SDL = 'SDL'
  32. SDL_DEFAULTS = {
  33. 'VIDEODRIVER': None
  34. }
  35. def _find_module(name, search_path):
  36. import_path = name.split('.', 1)
  37. mod_f, mod_path, mod_desc = imp.find_module(import_path[0], search_path)
  38. if mod_desc[2] == imp.PKG_DIRECTORY:
  39. return _find_module(
  40. import_path[1], [os.path.abspath(mod_path)]
  41. )
  42. else:
  43. return mod_f, mod_path, mod_desc
  44. def main():
  45. try:
  46. config_file_path = sys.argv[1]
  47. except IndexError:
  48. print 'usage: %s [CONFIG_FILE]' % sys.argv[0]
  49. return RET_NO_ARGS
  50. config = ConfigParser.SafeConfigParser()
  51. config.optionxform = str
  52. config.read(config_file_path)
  53. app_spec = config.get(CONFIG_SECTION_PIE_TIME, 'app_module', True)
  54. try:
  55. app_module, app_obj = app_spec.split(':')
  56. except ValueError:
  57. print "%s: failed to find application '%s'" % (
  58. sys.argv[0], app_spec
  59. )
  60. return RET_ERROR
  61. mod_f = None
  62. result = RET_OK
  63. try:
  64. mod_search_path = [os.getcwd()] + sys.path
  65. mod_f, mod_path, mod_desc = _find_module(app_module, mod_search_path)
  66. mod = imp.load_module(app_module, mod_f, mod_path, mod_desc)
  67. app = getattr(mod, app_obj)
  68. if config.has_option(CONFIG_SECTION_PIE_TIME, 'log_path'):
  69. app.log_path = config.get(CONFIG_SECTION_PIE_TIME, 'log_path')
  70. sdl_config = dict(SDL_DEFAULTS)
  71. if config.has_section(CONFIG_SECTION_SDL):
  72. sdl_config.update({
  73. x[0]: x[1] for x in config.items(CONFIG_SECTION_SDL)
  74. })
  75. for k, v in sdl_config.iteritems():
  76. if v:
  77. os.environ['SDL_%s' % k] = v
  78. result = app.run(standalone=False)
  79. except:
  80. traceback.print_exc()
  81. result = RET_ERROR
  82. finally:
  83. if mod_f:
  84. mod_f.close()
  85. return result
  86. if __name__ == '__main__':
  87. sys.exit(main())