File: factory.py

package info (click to toggle)
w3af 1.0-rc3svn3489-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 59,908 kB
  • ctags: 16,916
  • sloc: python: 136,990; xml: 63,472; sh: 153; ruby: 94; makefile: 40; asm: 35; jsp: 32; perl: 18; php: 5
file content (41 lines) | stat: -rw-r--r-- 1,201 bytes parent folder | download
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
# -*- coding: utf-8 -*-
# Copyright © 2007-2008 Stockholm TreeAligner Project
# Author: Torsten Marek <shlomme@gmx.net>
# Licensed under the GNU GPLv2
class MissingClassException(Exception):
    pass

class FactoryBase(object):
    __classes__ = []
    
    def __init__(self):
        self._switch = {}
        if isinstance(self.__classes__, dict):
            self._switch = self.__classes__
        else:
            for cls in self.__classes__:
                for switch in self._get_class_switch(cls):
                    self._switch[switch] = cls
    
    def create(self, *args):
        switch_value = self._get_switch(*args)
        try:
            cls = self._switch[switch_value]
        except KeyError, e:
            self.raise_error(e.args[0])
        return self._create_instance(cls, *args)

    def raise_error(self, switch_name):
        raise MissingClassException, switch_name
    
    def _create_instance(self, cls, *args):
        return cls(*args)

    def _get_class_switch(self, cls):
        raise NotImplementedError
    
    def _get_switch(self, *args):
        raise NotImplementedError
    
    def __iter__(self):
        return iter(set(self._switch.values()))