forked from Source-Python-Dev-Team/Source.Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyvalues.py
More file actions
91 lines (64 loc) · 2.6 KB
/
keyvalues.py
File metadata and controls
91 lines (64 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# ../entities/keyvalues.py
# =============================================================================
# >> IMPORTS
# =============================================================================
# Site Package Imports
# ConfigObj
from configobj import ConfigObj
# Source.Python Imports
from core import GAME_NAME
from paths import SP_DATA_PATH
# =============================================================================
# >> ALL DECLARATION
# =============================================================================
# Set all to an empty list
__all__ = []
# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# Store the base "keyvalues" path
_basepath = SP_DATA_PATH.joinpath('keyvalues')
# =============================================================================
# >> CLASSES
# =============================================================================
class _KeyValues(dict):
'''Dictionary that stores all entities with their keyvalues'''
def __missing__(self, item):
'''Called the first time an entity is added to the dictionary'''
# Get all keyvalues for the given entity
value = self[item] = _get_all_entity_keyvalues(item)
# Return the keyvalues
return value
def get_entity_keyvalues(self, args):
'''Returns all keyvalues for the given entities'''
# Create an empty dictionary
values = dict()
# Loop through all given entities
for arg in args:
# Add the entities to the dictionary
values.update(self[arg])
# Return all keyvalues for the given entities
return values
# Get the _KeyValues instance
KeyValues = _KeyValues()
# =============================================================================
# >> FUNCTIONS
# =============================================================================
def _get_all_entity_keyvalues(entity):
'''Retrieves all keyvalues for the given entity'''
# Create an empty dictionary to pass
game_keyvalues = {}
# Get the path to the entity's keyvalue file
inifile = _basepath.joinpath(entity, GAME_NAME + '.ini')
# Does the file exist?
if not inifile.isfile():
# Return the empty dictionary
return game_keyvalues
# Get the file's contents
ini = ConfigObj(inifile)
# Loop through all items in the file
for key in ini:
# Add the item to the dictionary
game_keyvalues[key] = ini[key]
# Return the dictionary
return game_keyvalues