@@ -0,0 +1,1937 @@
+# Copyright 2018 James McCauley%0A#%0A# Licensed under the Apache License, Version 2.0 (the %22License%22);%0A# you may not use this file except in compliance with the License.%0A# You may obtain a copy of the License at:%0A#%0A#     http://www.apache.org/licenses/LICENSE-2.0%0A#%0A# Unless required by applicable law or agreed to in writing, software%0A# distributed under the License is distributed on an %22AS IS%22 BASIS,%0A# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A# See the License for the specific language governing permissions and%0A# limitations under the License.%0A%0A%22%22%22%0ATweak values%0A%0AThis component lets you tweak various values which otherwise you'd need%0Ato write code to do.  For example, lots of classes have default values%0Astored as class variables, and there isn't always an exposed way to%0Achange them from the commandline or a config file.  With tweak, you%0Ajust do:%0A  misc.tweak=some.thing.Somewhere.value --value=42%0A%22%22%22%0A%0Aimport sys%0Afrom pox.lib.config_eval import eval_one%0Afrom pox.core import core%0A%0Alog = core.getLogger()%0A%0A%0Adef launch (key, value=None):%0A  if %22=%22 in key:%0A    if value is not None:%0A      raise RuntimeError(%22Value specified twice%22)%0A    assert value is None%0A    key,value = key.split(%22=%22,1)%0A  elif value is None:%0A    raise RuntimeError(%22You must specify a value with --value=...%22)%0A%0A  def try_tweak (mod, obj):%0A    mod = %22.%22.join(mod)%0A    if mod not in sys.modules: return False%0A    m = sys.modules%5Bmod%5D%0A    o = m%0A    prev = None%0A    for oname in obj:%0A      prev = o%0A      if not hasattr(o, oname): return False%0A      o = getattr(o, oname)%0A%0A    core.getLogger()%0A    log.debug(%22Tweaking %25s in %25s from %25s to %25s%22, %22.%22.join(obj), mod,%0A              repr(getattr(prev,oname)), repr(value))%0A%0A    setattr(prev, oname, value)%0A    return True%0A%0A  value = eval_one(value)%0A%0A  modparts = key.split(%22.%22)%0A  for split_at in range(len(modparts)-1,0,-1):%0A    if try_tweak(modparts%5B:split_at%5D,modparts%5Bsplit_at:%5D): break%0A

Add new "tweak" component