Lyend

Untitled

Dec 19th, 2018
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. import os
  2. import logging
  3.  
  4. class Extension:
  5. ''' Dynamic object carrying for objects to hold extensions '''
  6. def __init__(self, parent, folder, load_func='setup', exclusions=[]):
  7. self.folder = folder
  8. self.parent = parent
  9. self.load_func = load_func
  10. self.loaded = set()
  11. self.available = list()
  12. self.exclusions = exclusions
  13.  
  14. def find_files(self):
  15. for root, dirs, files in os.walk(os.path.join(os.path.dirname(__file__), self.folder)):
  16. for x in files:
  17. if not x.startswith('_') and x.endswith('.py') and x not in self.exclusions \
  18. and not x.split('.', -1)[0] in self.exclusions:
  19. yield x.split('.')[0]
  20.  
  21. if not '__init__.py' in files:
  22. with open(os.path.join(root, '__init__.py'), 'w') as f:
  23. f.write('pass')
  24. break
  25.  
  26. def reload_setup_hooks(self):
  27. loaded = [x.__name__ for x in self.loaded]
  28. avail = [x.__name__ for x in self.available]
  29. for f in self.find_files():
  30. needs_containment = False
  31. if f in loaded+avail:
  32. mod = [x for x in self.loaded if x.__name__ == f][0]
  33. else:
  34. needs_containment = True
  35. mod = __import__(self.folder + '.' + f).__dict__[f]
  36.  
  37. if hasattr(mod, 'USE'):
  38. if mod.USE:
  39. if hasattr(mod, self.load_func):
  40. try:
  41. getattr(mod, self.load_func)(self.parent)
  42. except Exception as e:
  43. logging.exception(
  44. mod.__name__ + ' Has failed to load due to [' + e.__class__.__name__ + '] Being raised.')
  45. else:
  46. logging.warning(mod.__name__ + ' Has no setup function. Ignoring...')
  47. else:
  48. logging.warning(mod.__name__ + ' Has no USE flag, ignoring...')
  49.  
  50. if needs_containment:
  51. self.loaded.add(mod)
  52.  
  53.  
  54. def load_ext(self):
  55. ''' gimme moar stuff
  56. status protocol:
  57. 0 = cant use
  58. 1 = is using
  59. 2 = can use
  60.  
  61. '''
  62. fs = list(self.find_files())
  63.  
  64. for mod in fs:
  65. try:
  66. _mod = __import__(self.folder + '.' + mod).__dict__[mod]
  67. except Exception as e:
  68. logging.exception(mod + ' Has failed to load due to [' + e.__class__.__name__ + '] Being raised.')
  69. yield 0, mod, e
  70.  
  71. if hasattr(_mod, 'USE'):
  72. if _mod.USE:
  73. if hasattr(_mod, self.load_func):
  74. getattr(_mod, self.load_func)(self.parent)
  75. self.loaded.add(_mod)
  76. yield _mod, 1
  77. else:
  78. logging.warning(mod + ' Has no setup function. Ignoring...')
  79. yield _mod, 0
  80. else:
  81. yield _mod, 2
  82. else:
  83. logging.warning(mod + ' Has no USE flag, ignoring...')
  84. self.available.append(_mod)
  85. yield _mod, 2
Advertisement
Add Comment
Please, Sign In to add comment