config.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright © 2012 Pierre Raybaut
  4. # Licensed under the terms of the MIT License
  5. # (see winpython/__init__.py for details)
  6. """
  7. WinPython utilities configuration
  8. Created on Wed Aug 29 12:23:19 2012
  9. """
  10. import sys
  11. import os.path as osp
  12. def get_module_path(modname):
  13. """Return module *modname* base path"""
  14. return osp.abspath(
  15. osp.dirname(sys.modules[modname].__file__)
  16. )
  17. def get_module_data_path(
  18. modname, relpath=None, attr_name='DATAPATH'
  19. ):
  20. """Return module *modname* data path
  21. Note: relpath is ignored if module has an attribute named *attr_name*
  22. Handles py2exe/cx_Freeze distributions"""
  23. datapath = getattr(sys.modules[modname], attr_name, '')
  24. if datapath:
  25. return datapath
  26. else:
  27. datapath = get_module_path(modname)
  28. parentdir = osp.join(datapath, osp.pardir)
  29. if osp.isfile(parentdir):
  30. # Parent directory is not a directory but the 'library.zip' file:
  31. # this is either a py2exe or a cx_Freeze distribution
  32. datapath = osp.abspath(
  33. osp.join(
  34. osp.join(parentdir, osp.pardir), modname
  35. )
  36. )
  37. if relpath is not None:
  38. datapath = osp.abspath(
  39. osp.join(datapath, relpath)
  40. )
  41. return datapath
  42. DATA_PATH = get_module_data_path(
  43. 'winpython', relpath='data'
  44. )
  45. IMAGE_PATH = get_module_data_path(
  46. 'winpython', relpath='images'
  47. )