|
1 | 1 | # valve-keyvalues-python |
2 | | -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 format. |
| 2 | +Python class for manipulation with Valve KeyValues files ([VDF format](https://developer.valvesoftware.com/wiki/KeyValues)). 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. |
3 | 3 |
|
4 | 4 | # Installation |
5 | 5 | Just copy the `valve-keyvalues-python` folder 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 import KeyValues` |
6 | 6 |
|
7 | 7 | **Requires Python 3!** |
8 | 8 |
|
9 | 9 | # Usage |
| 10 | +### Instantiation |
10 | 11 |
|
11 | | -TODO |
| 12 | +To create **empty** `KeyValues` instance: |
| 13 | + |
| 14 | +```python |
| 15 | +kv = KeyValues() |
| 16 | +``` |
| 17 | + |
| 18 | +To create `KeyValues` instance **from VDF file**: |
| 19 | + |
| 20 | +```python |
| 21 | +kv = KeyValues(filename="") |
| 22 | +``` |
| 23 | + |
| 24 | +Now you can access KeyValues with `dict` interface, i.e. with `kv[key] = value` operations. |
| 25 | + |
| 26 | +When you create `KeyValues` instance from VDF file you can specify these optional parameters: |
| 27 | + |
| 28 | +* `encoding=""` - input VDF file encoding. Default: `utf-8` |
| 29 | +* `mapper_type=` - mapper type for storing KeyValues. It must have the `dict` interface, i.e. allow to do `mapper[key] = value` operations. For example you can use the `dict` type. **Instance's attribute**. Default: `collections.OrderedDict` (stores the keys in the order they have been added) |
| 30 | +* `key_modifier=` - functions for modifying the key before its additions. For example `key_modifier=str.lower` will make all the keys to be lowercase. **Instance's attribute**. Default: `None` |
| 31 | + |
| 32 | +To create `KeyValues` instance **from your own object with `dict` interface**: |
| 33 | + |
| 34 | +```python |
| 35 | +kv = KeyValues(mapper=) |
| 36 | +``` |
| 37 | + |
| 38 | +Instance's attribute `mapper_type` will be set to type of passed `mapper=` object. |
| 39 | + |
| 40 | +All these instantiation variants have common optional parameter `key_sorter=`. It's a sorting function which will be applied to keys 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` |
| 41 | + |
| 42 | +### Methods |
| 43 | +* `parse(filename)` - parses the VDF file to `dict` interface representation, i.e. KeyValues can be accessed and modified by `kv[key] = value` operations. Optional arguments: |
| 44 | + * `encoding=""` - input VDF file encoding. Default: `utf-8` |
| 45 | + * `mapper_type=` - [see Instantiation section](README.md#instantiation). This will override the instance's attribute `mapper_type`. Default: `collections.OrderedDict` (stores the keys in the order they have been added) |
| 46 | + * `key_modifier=` - [see Instantiation section](README.md#instantiation). This will override the instance's attribute `key_modifier`. Default: `None` |
| 47 | + |
| 48 | +* `dump()` - returns the string representation of stored KeyValues, i.e. string in correct VDF format. Optional parameters: |
| 49 | + * `mapper=` - passed object will be dumped (it must have the `dict` interface!). Default: `None` |
| 50 | + * `key_sorter=` - [see Instantiation section](README.md#instantiation). This will override the instance's attribute `key_sorter`. Default: `None` |
| 51 | + |
| 52 | +* `write(filename)` - writes the dump to file. Optional parameters: |
| 53 | + * `encoding=""` - output file encoding. Default: `utf-8` |
| 54 | + * `mapper=` - writes the dump of passed object (it must have the `dict` interface!). Default: `None` |
| 55 | + * `key_sorter=` - [see Instantiation section](README.md#instantiation). This will override the instance's attribute `key_sorter`. Default: `None` |
| 56 | + |
| 57 | +Of course the class KeyValues also provides the standard `dict` interface methods like `keys()` or `key in d`. [See dict](https://docs.python.org/3.5/library/stdtypes.html#dict) |
| 58 | + |
| 59 | +*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). |
| 60 | + |
| 61 | +# [Examples - here](examples/) |
| 62 | + |
| 63 | +*example_01.py* - basic operations |
| 64 | + |
| 65 | +*example_02.py* - using the optional functions |
| 66 | + |
| 67 | +*example_03.py* - "advanced" uses |
| 68 | + |
| 69 | +# What is missing |
| 70 | + |
| 71 | +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 `{`. |
0 commit comments