Home

Awesome

valve-keyvalues-python

Python class for manipulation with Valve KeyValues files (VDF format). Provides the parsing of VDF files to objects with dict interface, editing of this object keys and values and writing of any object with dict interface to VDF file.

Installation

Just copy the valve_keyvalues_python folder or keyvalues.py file wherever you need, for example to your Python's site-packages: <Python>/Lib/site-packages/ and then inside your program import the class by from valve_keyvalues_python.keyvalues import KeyValues or from keyvalues import KeyValues

Requires Python 3!

Usage

Instantiation

To create empty KeyValues instance:

kv = KeyValues()

To create KeyValues instance from VDF file:

kv = KeyValues(filename="")

Now you can access KeyValues with dict interface, i.e. with kv[key] = value operations.

When you create KeyValues instance from VDF file you can specify these optional parameters:

To create KeyValues instance from your own object with dict interface:

kv = KeyValues(mapper=)

Instance's attribute mapper_type will be set to type of passed mapper= object.

All these instantiation variants have common optional parameter key_sorter=. It's a sorting function which will be applied to keys of every subpart when you use methods dump() or write() (or print(kv), which is in fact shortcut for print(kv.dump())). For example you can use key_sorter=sorted and keys will be represented in alphabetical ascending order; key_sorter=reversed for reverse order. Instance's attribute. Default: None

Methods

Of course the class KeyValues also provides the standard dict interface methods like keys() or key in d. See dict

Note: when you print(kv) your KeyValues instance, it's in fact a shortcut for print(kv.dump()) method (because magic method __str__() is defined that way).

Examples - here

example_01.py - basic operations

example_02.py - using the optional functions

example_03.py - "advanced" uses

What is missing

Generally the checking of VDF file syntax, i.e. if brackets are closed and so on. The only check is if after "key" is a starting bracket {. So it's expected that input VDF file will have a 'clean' and valid structure:

"key" "value"
"key" "val
ue"
"key"
{
  "key2" "value"
}
"key"
// some comment
{
  "key2" "value"
}
"key"
some comment
{ comment
  "key2" "value" comment
} comment

TODO