Page 1 of 1

How do I use the KeyValues class?

Posted: Tue Apr 11, 2017 11:11 am
by Doldol
It isn't very clear to me how to best use this class.

Let's say I'm trying to write this to a file (I don't care about newlines/whitespace), how would I construct this best using the KeyValues class? (Or is there a better way?)

Code: Select all

"Admins"
{
   "Admin1"
   {
      "auth"      "steam"
      "identity"      "STEAM_0:1:1337"
      "flags"      "z"
      "group"      "Group1"
      "immunity"   "100"
   }
   "Admin2"
   {
      "auth"      "steam"
      "identity"      "STEAM_0:1:13372"
      "flags"      "a"
      "group"      "Group2"
      "immunity"   "1"
   }
}

Re: How do I use the KeyValues class?

Posted: Tue Apr 11, 2017 4:50 pm
by Ayuto

Syntax: Select all

from keyvalues import KeyValues

root = KeyValues('Admins')

admin1 = root.find_key('Admin1', True)
admin1.set_string('auth', 'steam')
admin1.set_string('identity', 'STEAM_0:1:1337')
admin1.set_string('flags', 'z')
admin1.set_string('group', 'Group1')
admin1.set_string('immunity', '100')

admin2 = root.find_key('Admin2', True)
admin2.set_string('auth', 'steam')
admin2.set_string('identity', 'STEAM_0:1:13372')
admin2.set_string('flags', 'a')
admin2.set_string('group', 'Group2')
admin2.set_string('immunity', '1')

# Will create a file called 'admins.txt' in the game directory (e.g. cstrike)
root.save_to_file('admins.txt')
I think it makes sense to add a 'from_dict' method to construct a KeyValues class from a dictionary.

Re: How do I use the KeyValues class?

Posted: Tue Apr 11, 2017 6:17 pm
by Doldol
Thanks! I understand how it works now.

Ayuto wrote:I think it makes sense to add a 'from_dict' method to construct a KeyValues class from a dictionary.


Yes please!

Re: How do I use the KeyValues class?

Posted: Tue Apr 11, 2017 6:49 pm
by Ayuto
There you go:
https://github.com/Source-Python-Dev-Te ... 28fa4af32c

Test code:

Syntax: Select all

from pprint import pprint

from colors import RED
from events.manager import game_event_manager
from memory import get_object_pointer

d = {
'key1': '0',
'key2': 2,
'key3': 2.3,
'key4': {
'subkey1': '4',
'subkey2': 5,
'subkey3': 6.7,
'subkey4': {
'subsubkey1': '4',
'subsubkey2': 5,
'subsubkey3': 6.7,
'subsubkey4': RED,
'subsubkey5': 2<<32,
'subsubkey6': get_object_pointer(game_event_manager),
'subsubkey7': False,
'subsubkey8': 0
}
},
'key5': 1,
'key6': True,
'key7': -3,
'key8': -(2<<30)
}

pprint(d)

new_root = KeyValues.from_dict('Admins', d)
pprint(new_root.as_dict())

Re: How do I use the KeyValues class?

Posted: Tue Apr 11, 2017 8:19 pm
by Doldol
That's fast. & Supporting nested dicts out of the box, I like the implementation.

Re: How do I use the KeyValues class?

Posted: Tue May 02, 2017 12:17 am
by decompile
Would there be any support for file paths instead of the filename in the game directory?

Or should we move the file afterwards?

Re: How do I use the KeyValues class?

Posted: Tue May 02, 2017 5:07 am
by Ayuto
Both, relative and absolute paths, are supported.