Esempio n. 1
0
 def keytest3(self,x):
     es.keyploadfromfile(x, server_var['eventscripts_addondir'] + "/pyunittest/myfile.vdf")
     y = es.keypfindsubkey(x, "subkey", False)
     outcome = es.keypgetint(y, "hello")
     self.failUnless(outcome==99)
     z = es.keypfindsubkey(y, "test", True)
     name = es.keypgetname(z)
     self.failUnless(name =="test")
     pp = es.keypcreate()
     es.keyploadfromfile(pp, server_var['eventscripts_addondir'] + "/pyunittest/myfile.vdf")
     es.keyprecursivekeycopy(z, pp)
     name = es.keypgetname(z)
     self.failUnless(name == "justice2")
 def __init__(self, name=None, parent=None, existingid=None, filename=None):
     '''
     You can create a new KeyValues object by passing in different
     combinations of arguments:
     With only an existingid, KeyValues returns a wrapper around that
     existing id (i.e. C++ pointer). With only a parent, we'll create a
     new randomly named subkey. With a parent and name, we'll find/make a
     subkey of that name. With a name only, we'll create an unparented key
     with that name. With a filename, we'll load the KeyValues tree from
     disk and return it as the parent.
     '''
     if existingid is not None:
         #print "kv", existingid, parent
         self._id_ = existingid
         self._parent_ = parent
     else:
         if parent is None:
             self._id_ = es.keypcreate()
             if filename is not None:
                 self.load(filename)
             if name is not None:
                 es.keypsetname(self._id_, str(name))
         else:
             self._parent_ = parent
             if name is None:
                 self._id_ = es.keypcreatesubkey(parent._id_)
             else:
                 self._id_ = es.keypfindsubkey(parent._id_, str(name))
 def __delitem__(self, name):
     '''
     Delete a subkey from the keygroup.
     '''
     x = es.keypfindsubkey(self._id_, str(name), False)
     if x is None or x == 0:
         raise KeyError("%s not found in keyvalues object" % name)
     else:
         k = KeyValues(parent=self, existingid=x)
         k.detachParent()
 def keytest2(self, x):
     self.failIf(x <= 0)
     name = es.keypgetname(x)
     self.failIf(name != "justice")
     es.keypsetname(x, "justice2")
     name = es.keypgetname(x)
     self.failIf(name != "justice2")
     y = es.keypfindsubkey(x, "subkey", False)
     self.failUnless(y is None)
     y = es.keypfindsubkey(x, "subkey", True)
     subname = es.keypgetname(y)
     self.failUnless(subname == "subkey")
     r = es.keypfindsubkey(x, "subkey", True)
     self.failUnless(r == y)
     q = es.keypfindsubkey(x, "subkey2", True)
     subname = es.keypgetname(q)
     self.failIf(subname != "subkey2")
     w = es.keypfindsubkey(x, "subkey3", True)
     e = es.keypfindsubkey(x, "subkey3", True)
     p = es.keypgetfirstsubkey(x)
     n = 0
     while p:
         n += 1
         p = es.keypgetnextkey(p)
     self.failUnless(n == 3)
     self.failUnless(w == e)
     es.keypdetachsubkey(x, w)
     e = es.keypfindsubkey(x, "subkey3", False)
     self.failUnless(e is None)
     es.keypdelete(w)
     p = es.keypgetfirstsubkey(x)
     n = 0
     while p:
         n += 1
         p = es.keypgetnextkey(p)
     self.failUnless(n == 2)
     self.failUnless(es.keypisempty(r))
     # setstring
     es.keypsetstring(r, "hello", "1.3")
     self.failIf(es.keypisempty(r))
     outcome = es.keypgetstring(y, "hello")
     self.failUnless(outcome == "1.3")
     outcome = es.keypgetint(y, "hello")
     self.failUnless(outcome == 1)
     outcome = es.keypgetfloat(y, "hello")
     self.failUnless(round(outcome, 1) == 1.3)
     # setint
     es.keypsetint(y, "hello", 99)
     outcome = es.keypgetint(y, "hello")
     self.failUnless(outcome == 99)
     # setfloat
     es.keypsetint(y, "hello", 99)
     outcome = es.keypgetint(y, "hello")
     self.failUnless(outcome == 99)
     # save to file
     es.keypsavetofile(
         x, server_var['eventscripts_addondir'] + "/pyunittest/myfile.vdf")
Esempio n. 5
0
 def keytest2(self, x):
     self.failIf(x <= 0)
     name = es.keypgetname(x)
     self.failIf(name != "justice")
     es.keypsetname(x, "justice2")
     name = es.keypgetname(x)
     self.failIf(name != "justice2")
     y = es.keypfindsubkey(x, "subkey", False)
     self.failUnless(y is None)
     y = es.keypfindsubkey(x, "subkey", True)
     subname = es.keypgetname(y)
     self.failUnless(subname == "subkey")
     r = es.keypfindsubkey(x, "subkey", True)
     self.failUnless(r==y)
     q = es.keypfindsubkey(x, "subkey2", True)
     subname = es.keypgetname(q)
     self.failIf(subname != "subkey2")
     w = es.keypfindsubkey(x, "subkey3", True)
     e = es.keypfindsubkey(x, "subkey3", True)
     p = es.keypgetfirstsubkey(x)
     n = 0
     while p:
       n+=1
       p = es.keypgetnextkey(p)
     self.failUnless(n==3)
     self.failUnless(w==e)
     es.keypdetachsubkey(x, w)
     e = es.keypfindsubkey(x, "subkey3", False)
     self.failUnless(e is None)
     es.keypdelete(w)
     p = es.keypgetfirstsubkey(x)
     n = 0
     while p:
       n+=1
       p = es.keypgetnextkey(p)
     self.failUnless(n==2)
     self.failUnless(es.keypisempty(r))
     # setstring    
     es.keypsetstring(r, "hello", "1.3")
     self.failIf(es.keypisempty(r))
     outcome = es.keypgetstring(y, "hello")
     self.failUnless(outcome=="1.3")
     outcome = es.keypgetint(y, "hello")
     self.failUnless(outcome == 1)
     outcome = es.keypgetfloat(y, "hello")
     self.failUnless(round(outcome,1) == 1.3)
     # setint
     es.keypsetint(y, "hello", 99)
     outcome = es.keypgetint(y, "hello")
     self.failUnless(outcome==99)
     # setfloat
     es.keypsetint(y, "hello", 99)
     outcome = es.keypgetint(y, "hello")
     self.failUnless(outcome==99)
     # save to file
     es.keypsavetofile(x, server_var['eventscripts_addondir'] + "/pyunittest/myfile.vdf")
Esempio n. 6
0
 def __setitem__(self, name, value):
     '''
     Stores a name/value pair in the keygroup. Only ints, floats, strings,
     and KeyValues classes (as subkeys) can be stored.
     '''
     if (isinstance(value, KeyValues)):
         x = es.keypfindsubkey(self._id_, str(name), True)
         if x:
             es.keyprecursivekeycopy(x, value._id_)
     else:
         if (isinstance(value, int)):
             es.keypsetint(self._id_, str(name), value)
         elif (isinstance(value, float)):
             es.keypsetfloat(self._id_, str(name), value)
         else:
             es.keypsetstring(self._id_, str(name), str(value))
 def __setitem__(self, name, value):
     '''
     Stores a name/value pair in the keygroup. Only ints, floats, strings,
     and KeyValues classes (as subkeys) can be stored.
     '''
     if (isinstance(value, KeyValues)):
         x = es.keypfindsubkey(self._id_, str(name), True)
         if x:
             es.keyprecursivekeycopy(x, value._id_)
     else:
         if (isinstance(value, int)):
             es.keypsetint(self._id_, str(name), value)
         elif (isinstance(value, float)):
             es.keypsetfloat(self._id_, str(name), value)
         else:
             es.keypsetstring(self._id_, str(name), str(value))
    def __getitem__(self, name):
        '''
        Look up a value by name, a lot like a dictionary. Can return a value
        or a subkey, depending on what is returned.
        '''
        x = es.keypfindsubkey(self._id_, str(name), False)
        if x is None or x == 0:
            raise KeyError("%s not found in keyvalues object" % name)
        else:
            typ = es.keypgetdatatype(x)
            if types[typ] == "TYPE_NONE":
                return KeyValues(parent=self, existingid=x)
            elif types[typ] == "TYPE_STRING" or types[typ] == "TYPE_WSTRING":
                return es.keypgetstring(self._id_, str(name))
            elif types[typ] == "TYPE_INT":
                return es.keypgetint(self._id_, str(name))
            elif types[typ] == "TYPE_FLOAT":
                return es.keypgetfloat(self._id_, str(name))
##          types[4] = "TYPE_PTR"
##          types[6] = "TYPE_COLOR"
##          types[7] = "TYPE_NUMTYPES"
            else:
                raise NotImplemented("KeyValues does not yet support type: %s" % types[typ])
    def __getitem__(self, name):
        '''
        Look up a value by name, a lot like a dictionary. Can return a value
        or a subkey, depending on what is returned.
        '''
        x = es.keypfindsubkey(self._id_, str(name), False)
        if x is None or x == 0:
            raise KeyError(f"{name} not found in keyvalues object")
        else:
            typ = es.keypgetdatatype(x)
            if types[typ] == "TYPE_NONE":
                return KeyValues(parent=self, existingid=x)
            elif types[typ] == "TYPE_STRING" or types[typ] == "TYPE_WSTRING":
                return es.keypgetstring(self._id_, str(name))
            elif types[typ] == "TYPE_INT":
                return es.keypgetint(self._id_, str(name))
            elif types[typ] == "TYPE_FLOAT":
                return es.keypgetfloat(self._id_, str(name))
##          types[4] = "TYPE_PTR"
##          types[6] = "TYPE_COLOR"
##          types[7] = "TYPE_NUMTYPES"
            else:
                raise NotImplemented(f"KeyValues does not yet support type: {types[typ]}")