Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

Commit 6e840b8

Browse files
committed
move static methods to own class to streamline imports
1 parent d8138d7 commit 6e840b8

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

Source/Profiles.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json as Json
22
import os as Os
3-
from StreamlabsEvent import StreamlabsEvent
3+
from StreamlabsEvent import StreamlabsEventUtils
44

55

66
class Profiles:
@@ -64,9 +64,9 @@ def __init__(self, reactionData, profile):
6464
if "platform" in reactionData:
6565
self.platform = reactionData["platform"]
6666
self.type = reactionData["type"]
67-
self.handlerName = StreamlabsEvent.MakeHandlerString(
67+
self.handlerName = StreamlabsEventUtils.MakeHandlerString(
6868
self.platform, self.type)
69-
if self.handlerName not in StreamlabsEvent.handledEventTypes.keys():
69+
if self.handlerName not in StreamlabsEventUtils.handledEventTypes.keys():
7070
self.logging.LogQuit(
7171
"invalid event handler type: " + self.handlerName)
7272
else:
@@ -106,23 +106,23 @@ def __init__(self, filteredActionData, reaction):
106106
if self.condition == "":
107107
self.logging.LogQuit("'" + self.reaction.GetPrintHandlerType() +
108108
"' condition can not be blank")
109-
eventAttributeCheckResult = StreamlabsEvent.IsBadEventAttritubeUsed(
109+
eventAttributeCheckResult = StreamlabsEventUtils.IsBadEventAttritubeUsed(
110110
self.reaction.handlerName, self.condition, False)
111111
if eventAttributeCheckResult != "":
112112
self.logging.LogQuit("'" + self.reaction.GetPrintHandlerType() +
113113
"' condition error: " + eventAttributeCheckResult)
114-
scriptParseCheck = StreamlabsEvent.IsScriptValid(self.condition)
114+
scriptParseCheck = StreamlabsEventUtils.IsScriptValid(self.condition)
115115
if scriptParseCheck != "":
116116
self.logging.LogQuit("'" + self.reaction.GetPrintHandlerType() +
117117
"' has an invalid condition script:\n" + scriptParseCheck)
118118

119119
self.manipulator = filteredActionData["manipulator"]
120-
eventAttributeCheckResult = StreamlabsEvent.IsBadEventAttritubeUsed(
120+
eventAttributeCheckResult = StreamlabsEventUtils.IsBadEventAttritubeUsed(
121121
self.reaction.handlerName, self.manipulator, False)
122122
if eventAttributeCheckResult != "":
123123
self.logging.LogQuit("'" + self.reaction.GetPrintHandlerType() +
124124
"' manipulator error: " + eventAttributeCheckResult)
125-
scriptParseCheck = StreamlabsEvent.IsScriptValid(self.manipulator)
125+
scriptParseCheck = StreamlabsEventUtils.IsScriptValid(self.manipulator)
126126
if scriptParseCheck != "":
127127
self.logging.LogQuit("'" + self.reaction.GetPrintHandlerType() +
128128
"' has an invalid manipulator script:\n" + scriptParseCheck)
@@ -134,7 +134,7 @@ def __init__(self, filteredActionData, reaction):
134134
actionName = action[8:-1]
135135
if actionName in self.reaction.profile.actions.keys():
136136
self.action = self.reaction.profile.actions[actionName]
137-
eventAttributeCheckResult = StreamlabsEvent.IsBadEventAttritubeUsed(
137+
eventAttributeCheckResult = StreamlabsEventUtils.IsBadEventAttritubeUsed(
138138
self.reaction.handlerName, self.action.effect, True)
139139
if eventAttributeCheckResult != "":
140140
self.logging.LogQuit("'" + self.reaction.GetPrintHandlerType() + "' referenced action " +
@@ -147,7 +147,7 @@ def __init__(self, filteredActionData, reaction):
147147
if self.actionText == "":
148148
self.logging.LogQuit("'" + self.reaction.GetPrintHandlerType() +
149149
"' action can not be blank")
150-
eventAttributeCheckResult = StreamlabsEvent.IsBadEventAttritubeUsed(
150+
eventAttributeCheckResult = StreamlabsEventUtils.IsBadEventAttritubeUsed(
151151
self.reaction.handlerName, self.actionText, True)
152152
if eventAttributeCheckResult != "":
153153
self.logging.LogQuit("'" + self.reaction.GetPrintHandlerType() +

Source/Streamlabs Rcon Integration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from Streamlabs import Streamlabs
33
from Gui import Gui, GuiWindow
44
from Currency import Currency
5-
from StreamlabsEvent import StreamlabsEvent
5+
from StreamlabsEvent import StreamlabsEvent, StreamlabsEventUtils
66
import json as Json
77
from Config import Config
88
from Profiles import Profiles
@@ -21,7 +21,7 @@ def Setup(self):
2121
self.donationsIdsProcessed = {}
2222
self.translations = Translations(self)
2323
self.currency = Currency(self)
24-
StreamlabsEvent.LoadEventDefinitions()
24+
StreamlabsEventUtils.LoadEventDefinitions()
2525
self.profiles = Profiles(self)
2626
self.streamlabs = Streamlabs(self)
2727
self.rcon = Rcon(self)

Source/StreamlabsEvent.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55

66
class StreamlabsEvent():
7-
handledEventTypes = {}
8-
97
def __init__(self, state, data):
108
self.state = state
119
self.logging = state.logging
@@ -62,7 +60,7 @@ def __init__(self, state, data):
6260
self.platform = "streamlabs"
6361
elif self.platform == "twitch_account" and self.type == "subscription" and "gifter" in self.rawMessage and self.rawMessage["gifter"] != None:
6462
self.type = "subscription_gift"
65-
self.handlerName = self.MakeHandlerString(
63+
self.handlerName = StreamlabsEventUtils.MakeHandlerString(
6664
self.platform, self.type)
6765

6866
@property
@@ -96,15 +94,11 @@ def __str__(self):
9694
return ''.join(str_list)
9795

9896
def IsHandledEvent(self):
99-
if self.handlerName in self.handledEventTypes.keys():
97+
if self.handlerName in StreamlabsEventUtils.handledEventTypes.keys():
10098
return True
10199
else:
102100
return False
103101

104-
@staticmethod
105-
def MakeHandlerString(platform, type):
106-
return platform + "-" + type
107-
108102
def ShouldIgnoreEvent(self):
109103
if (self.type == "streamlabels") or (self.type == "streamlabels.underlying") or (self.type == "alertPlaying") or (self.type == "subscription-playing") or (self.type == "rollEndCredits") or (self.type == "subMysteryGift"):
110104
return True
@@ -174,7 +168,7 @@ def GetEventRawTitlesAsPrettyString(self):
174168
return eventDesc
175169

176170
def SubstituteEventDataIntoString(self, string, modValue="''"):
177-
instances = StreamlabsEvent.FindAttributeTagsInString(string)
171+
instances = StreamlabsEventUtils.FindAttributeTagsInString(string)
178172
for instance in instances:
179173
dataKeyName = instance[1:-1]
180174
dataKeyValue = "''"
@@ -197,9 +191,24 @@ def SubstituteEventDataIntoString(self, string, modValue="''"):
197191
elif dataKeyName in self.rawMessage:
198192
dataKeyValue = self.rawMessage[dataKeyName]
199193
string = string.replace(
200-
instance, StreamlabsEvent.EspaceStringForRcon(str(dataKeyValue)))
194+
instance, StreamlabsEventUtils.EspaceStringForRcon(str(dataKeyValue)))
201195
return string
202196

197+
198+
class StreamlabsEventUtils():
199+
handledEventTypes = {}
200+
201+
@staticmethod
202+
def MakeHandlerString(platform, type):
203+
return platform + "-" + type
204+
205+
@staticmethod
206+
def EspaceStringForRcon(text):
207+
text = text.replace("\\", "\\\\")
208+
text = text.replace("'", "\\'")
209+
text = text.replace('"', '\\"')
210+
return text
211+
203212
@staticmethod
204213
def FindAttributeTagsInString(string):
205214
return Regex.findall(r"\[[a-z_A-Z0-9]+\]", string)
@@ -209,28 +218,29 @@ def LoadEventDefinitions():
209218
with open("eventDefinitions.json", "r") as file:
210219
data = Json.load(file)
211220
file.closed
212-
StreamlabsEvent.handledEventTypes = data
221+
StreamlabsEventUtils.handledEventTypes = data
213222

214223
@staticmethod
215224
def IsBadEventAttritubeUsed(eventType, string, modValueAllowed):
216225
if string in ["", "[ALL]", "[NOTHING]"]:
217226
return ""
218-
instances = StreamlabsEvent.FindAttributeTagsInString(string)
227+
instances = StreamlabsEventUtils.FindAttributeTagsInString(string)
219228
for instance in instances:
220229
dataKeyName = instance[1:-1]
221230
if dataKeyName == "MODVALUE" and not modValueAllowed:
222231
return "[MODVALUE] used when not allowed"
223-
if dataKeyName in StreamlabsEvent.handledEventTypes['[ALL]'].keys():
232+
if dataKeyName in StreamlabsEventUtils.handledEventTypes['[ALL]'].keys():
224233
continue
225-
if eventType == "" or dataKeyName not in StreamlabsEvent.handledEventTypes[eventType].keys():
234+
if eventType == "" or dataKeyName not in StreamlabsEventUtils.handledEventTypes[eventType].keys():
226235
return instance + " not a valid attribute for this event"
227236
return ""
228237

229238
@staticmethod
230239
def IsScriptValid(scriptString):
231240
if scriptString in ["", "[ALL]", "[NOTHING]"]:
232241
return ""
233-
instances = StreamlabsEvent.FindAttributeTagsInString(scriptString)
242+
instances = StreamlabsEventUtils.FindAttributeTagsInString(
243+
scriptString)
234244
testScriptString = scriptString
235245
for instance in instances:
236246
testScriptString = testScriptString.replace(instance, str(1))
@@ -239,10 +249,3 @@ def IsScriptValid(scriptString):
239249
except Exception:
240250
return "config value: " + scriptString + "\n" + Traceback.format_exc(limit=0, chain=False)
241251
return ""
242-
243-
@staticmethod
244-
def EspaceStringForRcon(text):
245-
text = text.replace("\\", "\\\\")
246-
text = text.replace("'", "\\'")
247-
text = text.replace('"', '\\"')
248-
return text

0 commit comments

Comments
 (0)