def check_options(opts, args): if opts.hostdef and (opts.hostres or opts.hostcat): raise OptionError("Host resources or categories cannot be " "specified when specifyin" "a host definition document.") if (opts.standalone and not ((len(opts.agents) == 1 and opts.agent_id is None) or (len(opts.agents) == 0 and opts.agent_id is not None))): raise OptionError("Running standalone agent requires passing the " "information about what agent to run. You can " "either specify one '--agent AGENT_TYPE' paremeter " "or '--agent-id AGENT_ID'") if opts.agent_id and not opts.standalone: raise OptionError("--agent-id options should only be used for " "the standalone agent.") if opts.standalone_kwargs: if not opts.standalone: raise OptionError("Passing kwargs for standalone makes sense" " only combined with --standalone option.") try: opts.standalone_kwargs = json.unserialize( opts.standalone_kwargs) except (TypeError, ValueError): raise OptionError("Error unserializing json dictionary: %s " % opts.standalone_kwargs), None, sys.exc_info()[2] if args: raise OptionError("Unexpected arguments: %r" % args) return opts, args
def check_options(opts, args): if opts.hostdef and (opts.hostres or opts.hostcat): raise OptionError("Host resources or categories cannot be " "specified when specifyin" "a host definition document.") if opts.standalone and len(opts.agents) != 1: raise OptionError("Running standalone agency requires passing " "run host_id with --agent option.") if opts.standalone_kwargs: if not opts.standalone: raise OptionError("Passing kwargs for standalone makes sense" " only combined with --standalone option.") try: opts.standalone_kwargs = json.unserialize( opts.standalone_kwargs) except TypeError: raise OptionError("Error unserializing json dictionary: %s " % opts.standalone_kwargs) if args: raise OptionError("Unexpected arguments: %r" % args) return opts, args
def testDefaultWriter(self): # some json implemention gives non-unicode empty strings yield self.check("", u"", (unicode, str)) yield self.check(u"", u"", (unicode, str)) yield self.check(u"áéíóú", u"áéíóú", unicode) yield self.check("\xc3\xa1\xc3\xa9\xc3\xad\xc3\xb3\xc3\xba", u"áéíóú", unicode) yield self.check(42, 42, int) yield self.check(-33, -33, int) yield self.check(0, 0, int) yield self.check(3.14, 3.14, float) yield self.check(True, True, bool) yield self.check(False, False, bool) yield self.check(None, None, types.NoneType) yield self.check([], [], list) yield self.check({}, {}, dict) yield self.check((), [], list) yield self.check([1, True, None], [1, True, None]) yield self.check({"toto": "\xc3\xa1\xc3\xa9\xc3\xad\xc3\xb3\xc3\xba", "tata": u"áéíóú"}, {u"toto": u"áéíóú", u"tata": u"áéíóú"}) yield self.check(DummySnapshotable(), {u"tata": u"", u"titi": None, u"toto": 3.14, u"tutu": u"áéíóú"}) obj = DummySerializable() data = yield self.check(obj, {u".type": u"json-dummy", u"tata": u"spam", u"titi": True, u"toto": 1, u"tutu": u"áéíóú", u"tete": u""}) obj2 = feat_json.unserialize(data) self.assertTrue(isinstance(obj2, DummySerializable)) self.assertFalse(obj2 is obj) self.assertEqual(obj, obj2) yield self.check([1, 2, DummySnapshotable()], [1, 2, {u"tata": u"", u"titi": None, u"toto": 3.14, u"tutu": u"áéíóú"}]) yield self.check({"toto": DummySerializable()}, {u"toto": {u".type": u"json-dummy", u"tata": u"spam", u"titi": True, u"toto": 1, u"tutu": u"áéíóú", u"tete": u""}}) yield self.check((1, DummyParent(), 2, DummyParent(), 3), [1, {u"a": {u"value": 1}, u"b": {u"value": 2}}, 2, {u"a": {u"value": 1}, u"b": {u"value": 2}}, 3])
def load(self, env, options=None): ''' Loads config from environment. Environment values can be overridden by specified options. ''' # First load from env matcher = re.compile('\AFEAT_([^_]+)_(.+)\Z') for key in env: res = matcher.search(key) if res: c_key = res.group(1).lower() c_kkey = res.group(2).lower() if not hasattr(self, c_key): continue try: value = json.unserialize(env[key]) except ValueError: self.error("Environment variable does not unserialize" "to json. Variable: %s, Value: %s", key, env[key]) else: self.log("Setting %s.%s to %r", c_key, c_kkey, value) setattr(getattr(self, c_key), c_kkey, value) #Then override with options if options: # for group_key, conf_group in self.config.items(): for field in self._fields: conf_group = getattr(self, field.name) for group_field in conf_group._fields: attr = "%s_%s" % (field.name, group_field.name) if hasattr(options, attr): new_value = getattr(options, attr) old_value = getattr(conf_group, group_field.name) if new_value is not None and (old_value != new_value): if old_value is None: self.log("Setting %s.%s to %r", field.name, group_field.name, new_value) else: self.log("Overriding %s.%s to %r", field.name, group_field.name, new_value) setattr(conf_group, group_field.name, new_value) #set default for tunnel host if self.tunnel.host is None: self.tunnel.host = self.agency.full_hostname _absolutize_path(self.agency, 'socket_path', self.agency.rundir) _absolutize_path(self.agency, 'lock_path', self.agency.rundir)