Пример #1
0
 def test_getsetattr(self):
     cfg = Config()
     cfg["a"] = 1
     self.assertEquals(cfg.a, 1)
     cfg.a = 2
     self.assertEquals(cfg.a, 2)
     cfg.b = Config()
     cfg.b.a = 3
     self.assertEquals(cfg.b.a, 3)
Пример #2
0
 def test_getsetattr(self):
     cfg = Config()
     cfg["a"] = 1
     self.assertEquals(cfg.a, 1)
     cfg.a = 2
     self.assertEquals(cfg.a, 2)
     cfg.b = Config()
     cfg.b.a = 3
     self.assertEquals(cfg.b.a, 3)
Пример #3
0
 def setUp(self):
     self.test_db_file = "/tmp/gftest.db"
     connection_url = "sqlite:///{}".format(self.test_db_file)
     self.config = Config({
         "db_test": {
             "connect_url": "sqlite:///:memory:",
             "pool_policy": "test"
         },
         "db_test2": {
             "connect_url": connection_url
         },
         "dbpool_test": {
             "poolclass": "QueuePool",
             "pool_size": 10,
             "pool_recycle": 3600,
             "pool_timeout": 20
         }
     })
     conn = sqlite3.connect(self.test_db_file)
     create_table_sql = """
     create table user (
         id integer primary key,
         name varchar(10) unique,
         grade int not null,
         description text not null
     )
     """
     conn.execute(create_table_sql)
     conn.execute(("insert into user (id, name, grade, description) values "
                   "(1, 'SamChi', 1, 'I am SamChi')"))
     conn.commit()
Пример #4
0
    def test_validate(self):
        engine_manager = EngineManager()
        engine_manager.validate_config(self.config)

        # 是否已经设置默认参数
        self.assertEquals(self.config["db_test"]["connect_args"], "{}")
        self.assertEquals(self.config["db_test"]["encoding"], "utf-8")

        bad_config = Config({
            "db_test": {
                "connect_url": "hehe"  # url format error
            }
        })

        # 因为已经验证过配置了,所以同一个manager对象不会进行重复验证
        engine_manager.validate_config(bad_config)  # No Exception happened

        engine_manager = EngineManager()
        self.failUnlessException(InvalidArgumentException,
                                 engine_manager.validate_config, bad_config)

        bad_config["db_test"]["connect_url"] = "sqlite:///:memory:"
        bad_config["db_test"]["connect_args"] = "{h"
        engine_manager = EngineManager()
        self.failUnlessException(InvalidArgumentException,
                                 engine_manager.validate_config, bad_config)
Пример #5
0
def main():
    global TOOLS_OPTIONS
    config = Config.load_by_name(TOOLS_OPTIONS.config)
    _add_python_path(config)
    workflow_module = _load_module()

    workflow_parser = getattr(workflow_module, "cmd_parser", None)

    # 展示工作流模块所需要的参数说明
    if TOOLS_OPTIONS.show_args:
        _show_args(workflow_parser)

    # 解析workflow所需参数
    workflow_options = None
    if workflow_parser is not None:
        workflow_options = workflow_parser.parse_known_args(sys.argv[1:])[0]

    # 获取当前运行环境
    current_env = _get_current_env(workflow_module)

    # 更新配置信息,使用模块的配置项覆盖配置文件中的配置项
    _update_config(config, current_env, workflow_module, workflow_options)

    # 保存pid到文件
    if TOOLS_OPTIONS.pid:
        _save_pid_file(TOOLS_OPTIONS.pid)

    # 执行工作流
    _execute_workflow(config, workflow_module, workflow_options, current_env)
Пример #6
0
    def setUp(self):
        config = Config({
            "db_test": {
                "connect_url": "sqlite:///:memory:",
                "pool_policy": "test"
            },
            "dbpool_test": {
                "poolclass": "QueuePool",
                "pool_size": 10,
            }
        })
        _engine_manager.validate_config(config)
        _engine_manager.init_all(config)
        global Base
        engine_container = _engine_manager.engine("test")
        Base.metadata.create_all(engine_container.engine)

        student_sam = Student(id=1, name="Sam", fullname="SamChi", grade=1)
        student_jack = Student(id=2, name="Jack", fullname="JackMa", grade=2)
        student_betty = Student(id=3,
                                name="Betty",
                                fullname="Betty Smith",
                                grade=1)
        grade1 = Grade(id=1, name="Grade One")
        grade2 = Grade(id=2, name="Grade Two")

        session = engine_container.session()
        session.add_all(
            [student_sam, student_jack, student_betty, grade1, grade2])
        session.commit()
        self.config = config
Пример #7
0
    def test_validate(self):
        rules = (Rule("smtp.host", type=str, required=True),
                 Rule("smtp.port", type=int, required=False, min=25,
                      max=65535),
                 Rule("smtp.user",
                      type=str,
                      required=True,
                      min=1,
                      max=10,
                      regex=r"^\w+$"),
                 Rule("smtp.password", type=str, required=True, min=6, max=20),
                 Rule("db.host",
                      type=str,
                      required=True,
                      logic=lambda value: None
                      if value.startswith("192.168") else "Invalid!"))
        validator = DefaultConfigValidator(rules)

        cfg = Config({
            "smtp": {
                "host": "192.168.100.2",
                "port": 25,
                "user": "******",
                "password": "******"
            },
            "db": {
                "host": "192.168.1.1"
            }
        })
        validator(cfg)

        cfg["db"]["host"] = "localhost"
        self.failUnlessException(InvalidArgumentException, validator, cfg)
Пример #8
0
def main():
    global TOOLS_OPTIONS
    config = Config.load_by_name(TOOLS_OPTIONS.config)
    _add_python_path(config)
    workflow_module = _load_module()

    workflow_parser = getattr(workflow_module, "cmd_parser", None)

    # 展示工作流模块所需要的参数说明
    if TOOLS_OPTIONS.show_args:
        _show_args(workflow_parser)

    # 解析workflow所需参数
    workflow_options = None
    if workflow_parser is not None:
        workflow_options = workflow_parser.parse_known_args(sys.argv[1:])[0]

    # 获取当前运行环境
    current_env = _get_current_env(workflow_module)

    # 更新配置信息,使用模块的配置项覆盖配置文件中的配置项
    _update_config(config, current_env, workflow_module, workflow_options)

    # 保存pid到文件
    if TOOLS_OPTIONS.pid:
        _save_pid_file(TOOLS_OPTIONS.pid)

    # 执行工作流
    _execute_workflow(config, workflow_module, workflow_options, current_env)
Пример #9
0
 def __init__(self):
     self._clazz = Workflow
     self._options = ObjDictModel()
     self._units = tuple()
     self._plugin_mgr = plugin_mgr
     self._config = Config()
     self._context_factory = Context
     self._listeners = tuple()
     self._logger = None
Пример #10
0
    def test_wrap_function(self):
        """测试wrap_function类方法
        """

        # 只包装一个execute函数的时候
        plugin = Plugin.wrap_function(
            "testing", "testing plugin",
            lambda ctx, arg: "execute {}".format(arg))
        self.failUnlessException(PluginUnPreparedException, Plugin.execute,
                                 plugin, None, "cat")
        plugin.sys_prepare(None)
        self.assertEquals(plugin.execute(None, "cat"), "execute cat")
        plugin.sys_cleanup(None)
        self.failUnlessException(PluginAlreadyDeadException, Plugin.execute,
                                 plugin, None)

        # 包装错误的函数对象
        self.failUnlessException(InvalidPluginException, Plugin.wrap_function,
                                 "testing", "testing plugin", lambda: "hello")

        self.failUnlessException(InvalidPluginException, Plugin.wrap_function,
                                 "testing", "testing plugin",
                                 lambda ctx: "execute",
                                 lambda config, args: "sys_prepare")

        plugin = Plugin.wrap_function(
            "testing", "testing plugin",
            lambda ctx, arg: "execute {}".format(arg),
            lambda config: "sys_prepare a = {}".format(config["plugin"]["a"]),
            lambda config: "sys_cleanup",
            (Rule("arg", required=True, type=str, min=3, max=10), ),
            (Rule("plugin.a", required=True, type=int, min=1, max=3), ))

        cfg = Config({"plugin": {"a": -1}})
        self.failUnlessException(InvalidArgumentException, plugin.sys_prepare,
                                 cfg)
        cfg = Config({"plugin": {"a": 1}})
        self.assertEquals(plugin.sys_prepare(cfg), "sys_prepare a = 1")
        self.assertEquals(plugin.execute(None, "cat"), "execute cat")
        self.failUnlessException(InvalidArgumentException, plugin.execute,
                                 None, "c")
        self.assertEquals(plugin.sys_cleanup(None), "sys_cleanup")
Пример #11
0
 def setUp(self):
     self.config = Config({
         "smtp_test": {
             "host": "smtp.163.com",
             "port": 465,
             "account": "*****@*****.**",
             "password": "******",
             "ssl": "true"
         }
     })
     self.send_mail_plugin = SendMailPlugin()
     self.send_mail_plugin.config_validator(self.config)
Пример #12
0
    def test_validate_config(self):
        smtp_manager = SMTPManager()
        config = Config({
            "smtp_test": {
                "host": "smtp.163.com",
                "account": "*****@*****.**",
                "password": "******"
            }
        })
        smtp_manager.validate_config(config)
        self.assertEquals(config["smtp_test"]["port"], 25)

        config["smtp_test"]["port"] = "25"
        smtp_manager = SMTPManager()
        smtp_manager.validate_config(config)
        self.assertEquals(config["smtp_test"]["port"], 25)
Пример #13
0
    def __init__(self, module, config=None, options=None):
        super(self, ModuleWorkflowBuilder).__init__()

        # 从模块中加载各属性
        self._clazz = getattr(module, "workflow_class", Workflow)
        self._units = getattr(module, "workflow")
        self._plugin_mgr = getattr(module, "plugin_manager", plugin_mgr)
        self._options = options if options else ObjDictModel()

        self._config = config if config is not None else Config()
        module_config = getattr(module, "config", None)
        # 使用模块中的配置去覆盖之前的配置
        if module_config is not None:
            if isinstance(module_config, types.FunctionType):
                module_config = module_config(options)
            self._config.update(module_config)

        self._logger = getattr(module, "logger", None)
        self._listeners = getattr(module, "listeners", tuple())  # 获取监听器列表
Пример #14
0
 def test_env(cls):
     return cls("test", {}, Config(), u"测试环境")
Пример #15
0
 def setUp(self):
     self.config = Config.load_by_name("default")
Пример #16
0
 def setUp(self):
     self.config = Config.load_by_name("default")
Пример #17
0
    def __init__(self,
                 workflow_list,
                 config=None,
                 plugin_mgr=plugin_manager,
                 context_factory=Context,
                 logger=None,
                 parrent_context=None,
                 thread_id=None):
        """
        :param workflow_list 工作单元列表
        :param config 配置数据
        :param plugin_mgr 插件管理器,默认是plugin自带的entry_points管理器
        :param context_factory 上下文工厂,必须具有config, args, plugin_mgr, parent
                               这四个约定的参数
        :param logger 日志对象
        """

        self._workflow_list = workflow_list
        self._config = config or Config()
        self._plugin_manager = plugin_mgr

        self._units = {}  # 以名称为Key的工作流单元引用字典
        for idx, unit in enumerate(self._workflow_list):
            if unit.name in self._units:
                raise WorkflowUnitExistedException(unit.name)
            self._units[unit.name] = unit
            if unit.unittype == "job" or unit.unittype == "join":
                # 如果未指定goto,那么goto的默认值是下一个节点
                if unit.goto is None:
                    if idx < len(self._workflow_list) - 1:
                        unit.goto = self._workflow_list[idx + 1].name
                    else:
                        unit.goto = "end"
            elif unit.unittype == "fork":
                # 自动设置起始节点
                if unit.start_point is None:
                    if idx < len(self._workflow_list) - 1:
                        unit.start_point = self._workflow_list[idx + 1].name
                    else:
                        raise InvalidArgumentException(
                            u"Fork单元 '{}' 必须指定一个有效的start_point参数")
                # 设置下一步运行的goto节点,如果未指定,则设置最近的join
                if unit.goto is None:
                    for i, next_unit in enumerate(self._workflow_list[idx +
                                                                      1:],
                                                  start=idx + 1):
                        if next_unit.unittype == "join":
                            unit.goto = next_unit.name
                            break
                    else:
                        raise InvalidArgumentException(
                            u"Fork单元 '{}' 必须使用一个有效的goto跳转到Join".format(
                                unit.name))
                # 自动设置结束节点
                if unit.end_point is None:
                    for i, next_unit in enumerate(self._workflow_list[idx +
                                                                      1:],
                                                  start=idx + 1):
                        if (next_unit.unittype == "join"
                                and next_unit.name == unit.goto):
                            # join unit前一个元素
                            unit.end_point = self._workflow_list[i - 1].name
                            break
                    else:
                        raise InvalidArgumentException(
                            u"Fork单元 '{}' 必须指定一个有效的end_point参数".format(
                                unit.name))

        self._context_factory = context_factory
        self._listeners = []

        # 创建logger
        if logger is None:
            self._logger = create_logger("girlfriend", (stdout_handler(), ))
        else:
            self._logger = logger

        self._parrent_context = parrent_context
        self._thread_id = thread_id