In the style of my var.lua replacement, a similar dict-like object that proxies world.GetOption/world.GetAlphaOption/...
# ---------------------------------------------------------
# Python world.GetOption proxy dictionary
# ---------------------------------------------------------
import UserDict # if you don't have it already
class gopt(object, UserDict.DictMixin):
__repr__ = UserDict.DictMixin.__repr__
def canonize_name(self, name):
# Global Options don't seem to have a canonical form.
# In particular, "DefaultInputFontItalic " <-- trailing space
# And "Tray Icon", "Icon Placement",
#name = name.split()
#if len(name) > 1:
# world.Note(repr(name))
# name = name[0] + [n.lower().capitalize() for n in name[1:]]
#name = u"".join(name)
## Why, oh why!?
#if name == u"DefaultInputFontItalic":
# name += " "
return name
def getopt(self, name):
return world.GetGlobalOption(name)
def setopt(self, name, value):
raise TypeError("Cannot modify global options")
def __getitem__(self, name):
name = self.canonize_name(name)
r = self.getopt(name)
if None == r:
raise KeyError(name)
def __setitem__(self, name, value):
assert value != None
name = self.canonize_name(name)
r = self.setopt(name, value)
if eUnknownOption == r:
raise KeyError(name)
def __delitem__(self, name):
raise TypeError("Cannot delete MUSHclient options")
def keys(self):
return world.GetGlobalOptionList
def __getattr__(self, name):
try:
return self.__getitem__(name)
except KeyError:
raise AttributeError("MUSHclient has no option '%s'" % name)
def __setattr__(self, name, value):
try:
self.__setitem__(name, value)
except KeyError:
raise AttributeError("MUSHclient has no option '%s'" % name)
def __delattr__(self, name):
raise TypeError("Cannot delete MUSHclient options")
class wopt(gopt):
def canonize_name(self, name):
name = name.lower()
name = name.split()
name = '_'.join(name)
return name
def getopt(self, name):
return world.GetCurrentValue(name)
def setopt(self, name, value):
if name in world.GetAlphaOptionList:
if isinstance(value, str):
value = value.decode('utf8')
assert isinstance(value, unicode)
return world.SetAlphaOption(name, value)
if name in world.GetOptionList:
if isinstance(value, bool):
value = int(value)
assert isinstance(value, int)
return world.SetOption(name, value)
return eUnknownOption
def keys(self):
return sorted(list(world.GetAlphaOptionList) + list(world.GetOptionList))
class lwopt(wopt):
def getopt(self, name):
return world.GetLoadedValue(name)
def setopt(self, name, value):
raise TypeError("Cannot modify loadtime option values")
class dwopt(wopt):
def getopt(self, name):
return world.GetDefaultValue(name)
def setopt(self, name, value):
raise TypeError("Cannot modify default option values")
gopt = gopt()
wopt = wopt()
lwopt = lwopt()
dwopt = dwopt()
Hmm. I wonder if I should make a full OO over all the MUSHclient functions so you never need to explicitly reference world... |