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

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

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

import os 

import pkgutil 

from runmacs.processor.product import Product, DerrivedProduct, ImportedProduct 

import runmacs.processor.product as product_module 

 

 

class ProductRegistry(object): 

def __init__(self): 

self.products = set() 

 

def add_product(self, new_product): 

self.products.add(new_product) 

 

def __getitem__(self, key): 

16 ↛ 27line 16 didn't jump to line 27, because the condition on line 16 was never false if isinstance(key, (str, unicode)): 

candidates = [] 

for product in self.products: 

if key in product.productClassName(): 

candidates.append(product) 

if len(candidates) == 1: 

return candidates[0] 

23 ↛ 26line 23 didn't jump to line 26, because the condition on line 23 was never false elif len(candidates) == 0: 

raise KeyError('Product "{}" not found!'.format(key)) 

else: 

raise KeyError('Multiple Products matching "{}" found!'.format(key)) 

raise KeyError("Unknown key format!") 

 

def __contains__(self, key): 

try: 

self[key] 

except KeyError: 

return key in self.products 

else: 

return True 

 

def __iter__(self): 

return iter(self.products) 

 

def __len__(self): 

return len(self.products) 

 

def find_subclasses(self, baseclass): 

return {subclass for subclass in self if issubclass(subclass, baseclass)} 

 

def find_derived_products(self): 

return self.find_subclasses(DerrivedProduct) 

 

def find_imported_products(self): 

return self.find_subclasses(ImportedProduct) 

 

 

_default_registry = [] 

 

def get_module_importers(path, basePackage, package): 

path = os.path.join(path, package) 

pkg = basePackage + [package] 

for importer, name, ispkg in pkgutil.iter_modules([path]): 

yield importer, name, '.'.join(pkg + [name]) 

if ispkg: 

for importer, name, fullname in get_module_importers(path, pkg, name): 

yield importer, name, fullname 

 

 

def find_products_in_module(module): 

for name in dir(module): 

obj = getattr(module, name) 

try: 

if issubclass(obj, Product): 

yield obj 

except TypeError: 

pass 

 

 

def find_products_in_folder(folder): 

special_products = list(find_products_in_module(product_module)) 

 

package = __name__.split('.')[:-1] 

importers = get_module_importers(folder, package, 'products') 

 

for importer, name, fullname in importers: 

module = importer.find_module(name).load_module(fullname) 

for product in find_products_in_module(module): 

if not any(issubclass(sp, product) for sp in special_products): 

yield product 

 

 

def create_registry_from_folder(folder): 

registry = ProductRegistry() 

products = set(find_products_in_folder(folder)) 

for product in products: 

registry.add_product(product) 

return registry 

 

 

def get_default_registry(): 

if not _default_registry: 

_default_registry.append(create_registry_from_folder(os.path.dirname(__file__))) 

return _default_registry[0]