Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

# -*- coding: utf-8 -*- 

 

# Licensed under the MIT license 

# http://opensource.org/licenses/mit-license.php 

 

# Copyright 2007, Frank Scholz <coherence@beebits.net> 

 

""" real simple plugin system 

    meant as a replacement when setuptools/pkg_resources 

    are not available 

""" 

 

import os 

import sys 

 

 

class Plugin(object): 

    """ a new style class that 

        betrays all its sub-classes 

    """ 

    pass 

 

 

class Reception(object): 

    """ singleton class which holds information 

        about known plugins 

 

        currently a singleton, and even a class, 

        seems to be overkill for this, but maybe 

        we'll add some more functionality later 

    """ 

 

    _instance_ = None  # Singleton 

 

    def __new__(cls, *args, **kwargs): 

        """ creates the singleton """ 

        obj = getattr(cls, '_instance_', None) 

        if obj is not None: 

            return obj 

        else: 

            obj = super(Reception, cls).__new__(cls) 

            cls._instance_ = obj 

            return obj 

 

    def __init__(self, plugin_path=None, log=None): 

        """ initializes the class and 

            checks in if a path is provided 

        """ 

        self.log = log 

        if plugin_path is not None: 

            self.checkin(plugin_path) 

 

    def checkin(self, plugin_path): 

        """ import all valid files from plugin_path """ 

        if plugin_path not in sys.path: 

            sys.path.insert(0, plugin_path) 

        for plugin in os.listdir(plugin_path): 

            p = os.path.join(plugin_path, plugin) 

            if plugin != '__init__.py' and os.path.isfile(p) and \ 

                    os.path.splitext(p)[1] == '.py': 

                try: 

                    __import__(os.path.splitext(plugin)[0], None, None, ['']) 

                except Exception as msg: 

                    if self.log is None: 

                        print("can't import %r - %s" % ( 

                            os.path.splitext(plugin)[0], msg)) 

                    else: 

                        self.log("can't import %r - %r" % ( 

                            os.path.splitext(plugin)[0], msg)) 

 

    def guestlist(self, plugin_class=Plugin): 

        """ returns a list of all Plugin subclasses """ 

        found = [] 

 

        def get_subclass(klass, subclasses): 

            if len(subclasses) == 0: 

                found.append(klass) 

            else: 

                for k in subclasses: 

                    get_subclass(k, k.__subclasses__()) 

 

        get_subclass(plugin_class, plugin_class.__subclasses__()) 

 

        return found