Skip to content

Commit ea05210

Browse files
committed
Moved weapons.manager._Weapon to new weapons.instance module and made the class public.
Made listeners.tick.delays._Delay public and added a 'cancel' method to more easily cancel an instance. Removed '__all__' declaration from listeners.tick.delays and listeners.tick.repeat as the objects are exposed via listeners.tick.__init__
1 parent aaac813 commit ea05210

File tree

5 files changed

+123
-108
lines changed

5 files changed

+123
-108
lines changed

Diff for: addons/source-python/packages/source-python/listeners/tick/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# Source.Python Imports
99
# Listeners
1010
from listeners import listeners_logger
11+
from listeners.tick.delays import Delay
1112
from listeners.tick.delays import tick_delays
1213
from listeners.tick.repeat import TickRepeat
1314
from listeners.tick.repeat import TickRepeatStatus
@@ -16,7 +17,8 @@
1617
# =============================================================================
1718
# >> ALL DECLARATION
1819
# =============================================================================
19-
__all__ = ('TickRepeat',
20+
__all__ = ('Delay',
21+
'TickRepeat',
2022
'TickRepeatStatus',
2123
'tick_delays',
2224
)

Diff for: addons/source-python/packages/source-python/listeners/tick/delays.py

+27-25
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@
1717
from listeners import listeners_logger
1818

1919

20-
# =============================================================================
21-
# >> ALL DECLARATION
22-
# =============================================================================
23-
__all__ = ('tick_delays',
24-
)
25-
26-
2720
# =============================================================================
2821
# >> GLOBAL VARIABLES
2922
# =============================================================================
@@ -34,19 +27,19 @@
3427
# =============================================================================
3528
# >> CLASSES
3629
# =============================================================================
37-
class _Delay(object):
30+
class Delay(object):
3831

3932
"""Stores a callback to be called at a later time."""
4033

4134
def __init__(self, seconds, callback, *args, **kwargs):
4235
"""Called when an instance is instantiated."""
4336
# Log the init message
4437
listeners_tick_delays_logger.log_info(
45-
'_Delay.__init__ <{0}> <{1}> <{2}> <{3}>'.format(
38+
'Delay.__init__ <{0}> <{1}> <{2}> <{3}>'.format(
4639
seconds, callback, args, kwargs))
4740

4841
# Store the time to execute the callback
49-
self.exec_time = time.time() + seconds
42+
self._exec_time = time.time() + seconds
5043

5144
# Store the callback, arguments, and keywords
5245
self.callback = callback
@@ -57,7 +50,7 @@ def __call__(self):
5750
"""Call the delay with the proper arguments and keywords."""
5851
# Log the call message
5952
listeners_tick_delays_logger.log_info(
60-
'_Delay.__call__ - Try to call - <{0}> <{1}> <{2}>'.format(
53+
'Delay.__call__ - Try to call - <{0}> <{1}> <{2}>'.format(
6154
self.callback, self.args, self.kwargs))
6255

6356
# Use try/except in case an error is encountered
@@ -72,6 +65,15 @@ def __call__(self):
7265
# Print the exception to the console
7366
except_hooks.print_exception()
7467

68+
@property
69+
def exec_time(self):
70+
"""Return the time to execute the delayed function."""
71+
return self._exec_time
72+
73+
def cancel(self):
74+
"""Cancel the delay."""
75+
tick_delays.cancel_delay(self)
76+
7577

7678
class _Times(list):
7779

@@ -157,11 +159,11 @@ def __delitem__(self, item):
157159

158160
def delay(self, seconds, callback, *args, **kwargs):
159161
"""Create a delay."""
160-
# Get the _Delay instance for the given arguments
161-
delay_object = _Delay(seconds, callback, *args, **kwargs)
162+
# Get the Delay instance for the given arguments
163+
delay_object = Delay(seconds, callback, *args, **kwargs)
162164

163-
# Add the _Delay instance to the dictionary using its execution time
164-
self[delay_object.exec_time].append(delay_object)
165+
# Add the Delay instance to the dictionary using its execution time
166+
self[delay_object._exec_time].append(delay_object)
165167

166168
# Return the object
167169
return delay_object
@@ -199,37 +201,37 @@ def cancel_delay(self, delay_object):
199201
listeners_tick_delays_logger.log_info(
200202
'tick_delays.cancel_delay <{0}>'.format(delay_object))
201203

202-
# Is the given argument a _Delay object?
203-
if not isinstance(delay_object, _Delay):
204+
# Is the given argument a Delay object?
205+
if not isinstance(delay_object, Delay):
204206

205207
# If not, raise an error
206208
raise TypeError(
207-
'tick_delays.cancel_delay requires a _Delay instance.')
209+
'tick_delays.cancel_delay requires a Delay instance.')
208210

209-
# Is the given _Delay object's time no longer in the dictionary?
210-
if delay_object.exec_time not in self:
211+
# Is the given Delay object's time no longer in the dictionary?
212+
if delay_object._exec_time not in self:
211213

212214
# If not, raise an error
213215
raise KeyError('Object is no longer registered.')
214216

215217
# Log the removing from list message
216218
listeners_tick_delays_logger.log_info(
217219
'tick_delays.cancel_delay - Removing from '
218-
'<{0}>'.format(delay_object.exec_time))
220+
'<{0}>'.format(delay_object._exec_time))
219221

220222
# Remove the delay from its time
221-
self[delay_object.exec_time].remove(delay_object)
223+
self[delay_object._exec_time].remove(delay_object)
222224

223225
# Does the delay's time have any remaining objects?
224-
if not self[delay_object.exec_time]:
226+
if not self[delay_object._exec_time]:
225227

226228
# Log the deletion of the time from the dictionary message
227229
listeners_tick_delays_logger.log_info(
228230
'tick_delays.cancel_delay - Removing <{0}> '
229-
'from dictionary'.format(delay_object.exec_time))
231+
'from dictionary'.format(delay_object._exec_time))
230232

231233
# If not, remove the delay's time from the dictionary
232-
del self[delay_object.exec_time]
234+
del self[delay_object._exec_time]
233235

234236
# Are there any remaining delays?
235237
if not self:

Diff for: addons/source-python/packages/source-python/listeners/tick/repeat.py

-8
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@
1919
from listeners.tick import listeners_logger
2020

2121

22-
# =============================================================================
23-
# >> ALL DECLARATION
24-
# =============================================================================
25-
__all__ = ('TickRepeat',
26-
'TickRepeatStatus',
27-
)
28-
29-
3022
# =============================================================================
3123
# >> GLOBAL VARIABLES
3224
# =============================================================================
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# ../weapons/instance.py
2+
3+
"""Provides a class to store information specific to each weapon."""
4+
5+
# =============================================================================
6+
# >> IMPORTS
7+
# =============================================================================
8+
# Source.Python Imports
9+
# Cvars
10+
from cvars import ConVar
11+
12+
13+
# =============================================================================
14+
# >> ALL DECLARATION
15+
# =============================================================================
16+
__all__ = ('Weapon',
17+
)
18+
19+
20+
# =============================================================================
21+
# >> CLASSES
22+
# =============================================================================
23+
class Weapon(object):
24+
25+
"""Class used to store information specific to the given weapon."""
26+
27+
def __init__(self, name, basename, properties):
28+
"""Store the base attributes for the weapon."""
29+
# Store the weapon's full name
30+
self._name = name
31+
32+
# Store the weapon's base name
33+
self._basename = basename
34+
35+
# Store the weapon's slot number
36+
self._slot = properties.get('slot', None)
37+
38+
# Store the weapon's max ammo amount
39+
self._maxammo = properties.get('maxammo', None)
40+
41+
# Store the weapon's ammo property
42+
self._ammoprop = properties.get('ammoprop', None)
43+
44+
# Store the weapon's clip amount
45+
self._clip = properties.get('clip', 0)
46+
47+
# Store the weapon's tags
48+
self._tags = properties.get('tags', 'all').split(',')
49+
50+
@property
51+
def name(self):
52+
"""Return the classname of the weapon."""
53+
return self._name
54+
55+
@property
56+
def basename(self):
57+
"""Return the basename of the weapon."""
58+
return self._basename
59+
60+
@property
61+
def slot(self):
62+
"""Return the slot of the weapon."""
63+
return self._slot
64+
65+
@property
66+
def maxammo(self):
67+
"""Return the maxammo amount for the weapon."""
68+
# Is the stored maxammo an integer?
69+
if isinstance(self._maxammo, int):
70+
71+
# Return the value
72+
return self._maxammo
73+
74+
# Get the cvar value of the maxammo
75+
return ConVar(self._maxammo).get_int()
76+
77+
@property
78+
def ammoprop(self):
79+
"""Return the ammoprop of the weapon."""
80+
return self._ammoprop
81+
82+
@property
83+
def clip(self):
84+
"""Return the clip value of the weapon."""
85+
return self._clip
86+
87+
@property
88+
def tags(self):
89+
"""Return the tags of the weapon."""
90+
return self._tags

Diff for: addons/source-python/packages/source-python/weapons/manager.py

+3-74
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
# Source.Python Imports
1313
# Core
1414
from core import GAME_NAME
15-
# Cvars
16-
from cvars import ConVar
1715
# Paths
1816
from paths import SP_DATA_PATH
1917
# Weapons
2018
from weapons.default import NoWeaponManager
19+
from weapons.instance import Weapon
2120

2221

2322
# =============================================================================
@@ -74,13 +73,13 @@ def __init__(self, ini_file):
7473
name = self._format_name(basename)
7574

7675
# Add the weapon to the dictionary
77-
self[name] = _Weapon(name, basename, ini['weapons'][basename])
76+
self[name] = Weapon(name, basename, ini['weapons'][basename])
7877

7978
# Add the weapon's tags to the set of tags
8079
self._tags.update(self[name].tags)
8180

8281
def __getitem__(self, item):
83-
"""Format the given name."""
82+
"""Return the Weapon instance for the given weapon."""
8483
# Format the weapon's name
8584
name = self._format_name(item)
8685

@@ -151,76 +150,6 @@ def tags(self):
151150
"""Return the weapon tags for the server."""
152151
return self._tags
153152

154-
155-
class _Weapon(object):
156-
157-
"""Class used to store information specific to the given weapon."""
158-
159-
def __init__(self, name, basename, properties):
160-
"""Store the base attributes for the weapon."""
161-
# Store the weapon's full name
162-
self._name = name
163-
164-
# Store the weapon's base name
165-
self._basename = basename
166-
167-
# Store the weapon's slot number
168-
self._slot = properties.get('slot', None)
169-
170-
# Store the weapon's max ammo amount
171-
self._maxammo = properties.get('maxammo', None)
172-
173-
# Store the weapon's ammo property
174-
self._ammoprop = properties.get('ammoprop', None)
175-
176-
# Store the weapon's clip amount
177-
self._clip = properties.get('clip', 0)
178-
179-
# Store the weapon's tags
180-
self._tags = properties.get('tags', 'all').split(',')
181-
182-
@property
183-
def name(self):
184-
"""Return the classname of the weapon."""
185-
return self._name
186-
187-
@property
188-
def basename(self):
189-
"""Return the basename of the weapon."""
190-
return self._basename
191-
192-
@property
193-
def slot(self):
194-
"""Return the slot of the weapon."""
195-
return self._slot
196-
197-
@property
198-
def maxammo(self):
199-
"""Return the maxammo amount for the weapon."""
200-
# Is the stored maxammo an integer?
201-
if isinstance(self._maxammo, int):
202-
203-
# Return the value
204-
return self._maxammo
205-
206-
# Get the cvar value of the maxammo
207-
return ConVar(self._maxammo).get_int()
208-
209-
@property
210-
def ammoprop(self):
211-
"""Return the ammoprop of the weapon."""
212-
return self._ammoprop
213-
214-
@property
215-
def clip(self):
216-
"""Return the clip value of the weapon."""
217-
return self._clip
218-
219-
@property
220-
def tags(self):
221-
"""Return the tags of the weapon."""
222-
return self._tags
223-
224153
# Does the current game have an ini file?
225154
if _gamepath.isfile():
226155

0 commit comments

Comments
 (0)