Beispiel #1
0
def ParsePythonEnum(string: str,
                    enumType: typing.Type[enum_lib.Enum]) -> typing.Any:
    if not isinstance(string, str):
        raise Exceptions.IncorrectTypeException(string, "string", (str, ))

    if not isinstance(enumType, type):
        raise Exceptions.IncorrectTypeException(enumType, "enumType", (type, ))

    if not issubclass(enumType, enum_lib.Enum):
        raise Exceptions.DoesNotInheritException("enumType", (enum_lib.Enum, ))

    if "." in string:
        stringSplit = string.split(".")  # type: typing.List[str]

        if len(stringSplit) == 2:
            if stringSplit[0] == enumType.__name__:
                string = stringSplit[1]
            else:
                raise ValueError("Cannot parse '" + string + "' to '" +
                                 Types.GetFullName(enumType) + "'.")
        else:
            raise ValueError("Cannot parse '" + string + "' to '" +
                             Types.GetFullName(enumType) + "'.")

    try:
        return enumType[string]
    except KeyError:
        pass

    raise ValueError("'" + string + "' is not an attribute of '" +
                     Types.GetFullName(enumType) + "'.")
Beispiel #2
0
def _SortLevels(levels: typing.List[Level]) -> typing.List[Level]:
    """
	:param levels: A list of levels needing to be sorted.
	:type levels: typing.List[Level]
	:return: A new list containing the input levels in order from lowest level to highest level.
	:rtype: typing.List[Level]
	"""

    if not isinstance(levels, list):
        raise Exceptions.IncorrectTypeException(levels, "levels", (list, ))

    for levelIndex in range(len(levels)):  # type: int
        if not isinstance(levels[levelIndex], Level):
            raise Exceptions.IncorrectTypeException(levels[levelIndex],
                                                    "levels[%d]" % levelIndex,
                                                    (Level, ))

    levels = levels.copy()
    sortedLevels = list()

    while len(levels) != 0:
        selectedIndex = -1  # type: int

        for checkingIndex in range(len(levels)):  # type: int
            if selectedIndex == -1:
                selectedIndex = checkingIndex

            if levels[selectedIndex].Level > levels[checkingIndex].Level:
                selectedIndex = checkingIndex
                continue

        sortedLevels.append(levels[selectedIndex])
        levels.pop(selectedIndex)

    return sortedLevels
Beispiel #3
0
def ParseS4Enum(string: str, enumType: enum.Metaclass) -> typing.Any:
    if not isinstance(string, str):
        raise Exceptions.IncorrectTypeException(string, "string", (str, ))

    if not isinstance(enumType, enum.Metaclass):
        raise Exceptions.IncorrectTypeException(enumType, "enumType",
                                                (enum.Metaclass, ))

    if "." in string:
        stringSplit = string.split(".")  # type: typing.List[str]

        if len(stringSplit) == 2:
            if stringSplit[0] == enumType.__name__:
                string = stringSplit[1]
            else:
                raise ValueError("Cannot parse '" + string + "' to '" +
                                 Types.GetFullName(enumType) + "'.")
        else:
            raise ValueError("Cannot parse '" + string + "' to '" +
                             Types.GetFullName(enumType) + "'.")

    try:
        return enumType[string]
    except KeyError:
        pass

    raise ValueError("'" + string + "' is not an attribute of '" +
                     Types.GetFullName(enumType) + "'.")
Beispiel #4
0
    def __init__(self, settingSystem: typing.Any,
                 settings: typing.List[SettingStandardWrapper],
                 save: typing.Callable[[],
                                       None], update: typing.Callable[[],
                                                                      None]):
        """
		A wrapper for the standard settings system used by NeonOcean
		"""

        super().__init__(settingSystem)

        if not isinstance(save, typing.Callable):
            raise Exceptions.IncorrectTypeException(save, "save",
                                                    ("Callable", ))

        if not isinstance(update, typing.Callable):
            raise Exceptions.IncorrectTypeException(update, "update",
                                                    ("Callable", ))

        if not isinstance(settings, list):
            raise Exceptions.IncorrectTypeException(settings, "settings",
                                                    (list, ))

        for settingIndex in range(len(settings)):  # type: int
            setting = settings[settingIndex]  # type: SettingStandardWrapper

            if not isinstance(setting, SettingStandardWrapper):
                raise Exceptions.IncorrectTypeException(
                    setting, "settings[%s]" % settingIndex,
                    (SettingStandardWrapper, ))

        self._settings = settings  # type: typing.List[SettingStandardWrapper]
        self._save = save  # type: typing.Callable[[], None]
        self._update = update  # type: typing.Callable[[], None]
Beispiel #5
0
def ShowOkCancelDialog (callback: typing.Callable = None, queue: bool = True, **dialogArguments) -> None:
	"""
	:param callback: Called after the dialog gets a response from the user. This will never be called it the dialog has no responses.
	 				 The callback function will receive one argument; a reference to the dialog.
	:type callback: typing.Callable
	:param queue: When true and the UI dialog service is not running the dialog will be put in queue until it is. Otherwise the dialog will be ignored.
				  The ui dialog service will only run while a zone is loaded.
	:type queue: bool
	"""

	global _dialogDisplayable

	if not isinstance(callback, typing.Callable) and callback is not None:
		raise Exceptions.IncorrectTypeException(callback, "callback", ("Callable",))

	if not isinstance(queue, bool):
		raise Exceptions.IncorrectTypeException(queue, "queue", (bool,))

	if not "owner" in dialogArguments:
		dialogArguments["owner"] = None

	dialog = ui_dialog.UiDialogOkCancel.TunableFactory().default(**dialogArguments)  # type: ui_dialog.UiDialogOkCancel

	if callback is not None:
		dialog.add_listener(callback)

	if services.current_zone() is not None and _dialogDisplayable:
		dialog.show_dialog()
	else:
		_dialogDisplayable = False

		if queue:
			_queue.append(dialog)
Beispiel #6
0
def _Setup() -> None:
    global _shownPromotions

    if os.path.exists(_shownPromotionsFilePath):
        try:
            with open(_shownPromotionsFilePath) as shownPromotionsFile:
                shownPromotions = json.JSONDecoder().decode(
                    shownPromotionsFile.read())

                if not isinstance(shownPromotions, list):
                    raise Exceptions.IncorrectTypeException(
                        shownPromotions, "Root", (list, ))

                for shownPromotionIndex in range(
                        len(shownPromotions)):  # type: int
                    if not isinstance(shownPromotions[shownPromotionIndex],
                                      str):
                        raise Exceptions.IncorrectTypeException(
                            shownPromotions[shownPromotionIndex],
                            "Root[%d]" % shownPromotionIndex, (str, ))

                _shownPromotions = shownPromotions
        except Exception as e:
            Debug.Log("Failed to read shown promotions file.",
                      This.Mod.Namespace,
                      Debug.LogLevels.Warning,
                      group=This.Mod.Namespace,
                      owner=__name__,
                      exception=e)
Beispiel #7
0
	def _UpdateScriptPaths (self, informationDictionary: dict) -> bool:
		informationKey = "ScriptPaths"  # type: str

		scriptPathRootKey = "Root"  # type: str
		scriptPathPathKey = "Path"  # type: str

		try:
			scriptPaths = informationDictionary.get(informationKey, list())  # type: typing.List[str]

			if not isinstance(scriptPaths, list):
				raise Exceptions.IncorrectTypeException(scriptPaths, "Root[%s]" % informationKey, (list,))

			for index, scriptPath in enumerate(scriptPaths):  # type: int, str
				if not isinstance(scriptPath, str) and not isinstance(scriptPath, dict):
					raise Exceptions.IncorrectTypeException(scriptPath, "Root[%s][%d]" % (informationKey, index), (str, dict))

				if isinstance(scriptPath, dict):
					if not scriptPathRootKey in scriptPath:
						raise Exception("Missing dictionary entry '%s' in 'Root[%s][%d]'." % (scriptPathRootKey, informationKey, index))

					if not scriptPathPathKey in scriptPath:
						raise Exception("Missing dictionary entry '%s' in 'Root[%s][%d]'." % (scriptPathPathKey, informationKey, index))

					scriptPathRoot = scriptPath[scriptPathRootKey]  # type: str

					if not isinstance(scriptPathRoot, str):
						raise Exceptions.IncorrectTypeException(scriptPathRoot, "Root[%s][%d][%s]" % (informationKey, index, scriptPathRootKey), (str,))

					scriptPathPath = scriptPath[scriptPathPathKey]  # type: str

					if not isinstance(scriptPathPath, str):
						raise Exceptions.IncorrectTypeException(scriptPathRoot, "Root[%s][%d][%s]" % (informationKey, index, scriptPathPathKey), (str,))

					scriptPathRootLower = scriptPathRoot.lower()

					if scriptPathRootLower == "mods":
						scriptPathRootValue = Paths.ModsPath
					elif scriptPathRootLower == "s4":
						scriptPathRootValue = Paths.UserDataPath
					elif scriptPathRootLower == "current":
						scriptPathRootValue = self.Mod.InformationFileDirectoryPath
					else:
						raise Exception("'" + scriptPathPath + "' is not a valid path root, valid roots are 'mods', 's4' and 'current'.")

					scriptPaths[index] = os.path.join(scriptPathRootValue, os.path.normpath(scriptPathPath))
				else:
					scriptPaths[index] = os.path.join(Paths.ModsPath, os.path.normpath(scriptPath))

				if not os.path.exists(scriptPaths[index]):
					raise Exception("'" + scriptPaths[index] + "' does not exist.")

			self.Mod.ScriptPaths = scriptPaths

			for scriptPath in self.Mod.ScriptPaths:
				self.Mod.Modules.extend(_GetArchiveModules(scriptPath))

			return True
		except Exception:
			Debug.Log("Failed to read mod information file value '%s' for '%s'." % (informationKey, self.Mod.Namespace), This.Mod.Namespace, Debug.LogLevels.Exception, group = This.Mod.Namespace, owner = __name__)
			return False
Beispiel #8
0
    def __init__(self,
                 loggingRootPath: str,
                 hostNamespace: str = This.Mod.Namespace):
        """
		An object for logging debug information.
		Logs will be written to a folder named either by the global NeonOcean debugging start time, or the time ChangeLogFile() was last called for this object.

		:param loggingRootPath: The root path all reports sent to this logger object will be written.
		:type loggingRootPath: str
		:param hostNamespace: Errors made by this logger object will show up under this namespace.
		:type hostNamespace: str
		"""

        if not isinstance(loggingRootPath, str):
            raise Exceptions.IncorrectTypeException(loggingRootPath,
                                                    "loggingRootPath", (str, ))

        if not isinstance(hostNamespace, str):
            raise Exceptions.IncorrectTypeException(hostNamespace,
                                                    "hostNamespace", (str, ))

        self.DebugGlobal = Global.GetModule("Debug")

        if not hasattr(self.DebugGlobal,
                       self._globalSessionID) or not isinstance(
                           getattr(self.DebugGlobal, self._globalSessionID),
                           uuid.UUID):
            setattr(self.DebugGlobal, self._globalSessionID, uuid.uuid4())

        if not hasattr(self.DebugGlobal,
                       self._globalSessionStartTime) or not isinstance(
                           getattr(self.DebugGlobal,
                                   self._globalSessionStartTime),
                           datetime.datetime):
            setattr(self.DebugGlobal, self._globalSessionStartTime,
                    datetime.datetime.now())

        if not hasattr(self.DebugGlobal,
                       self._globalShownWriteFailureNotification):
            setattr(self.DebugGlobal,
                    self._globalShownWriteFailureNotification, False)

        self.HostNamespace = hostNamespace  # type: str

        self._reportStorage = list()  # type: typing.List[Report]
        self._flushThread = None  # type: typing.Optional[threading.Thread]

        self._loggingRootPath = loggingRootPath  # type: str
        self._loggingDirectoryName = GetDateTimePathString(
            getattr(self.DebugGlobal,
                    self._globalSessionStartTime))  # type: str

        self._writeFailureCount = 0  # type: int
        self._writeFailureLimit = 2  # type: int
        self._isContinuation = False  # type: bool

        self._sessionInformation = self._CreateSessionInformation(
        )  # type: str
        self._modsDirectoryInformation = self._CreateModsDirectoryInformation(
        )  # type: str
Beispiel #9
0
	def __init__ (self, hostNamespace: str, settingsSystem: UISettingsShared.SettingSystemWrapper):
		if not isinstance(hostNamespace, str):
			raise Exceptions.IncorrectTypeException(hostNamespace, "hostNamespace", (str,))

		if not isinstance(settingsSystem, UISettingsShared.SettingSystemWrapper):
			raise Exceptions.IncorrectTypeException(settingsSystem, "settingsSystem", (UISettingsShared.SettingSystemWrapper, ))

		self.HostNamespace = hostNamespace  # type: str
		self._settingsSystem = settingsSystem  # type: UISettingsShared.SettingSystemWrapper
Beispiel #10
0
    def __init__(self, mod: Mods.Mod, exiting: bool):
        if not isinstance(mod, Mods.Mod):
            raise Exceptions.IncorrectTypeException(mod, "mod", (Mods.Mod, ))

        if not isinstance(exiting, bool):
            raise Exceptions.IncorrectTypeException(exiting, "exiting",
                                                    (bool, ))

        self.Mod = mod  # type: Mods.Mod
        self.Exiting = exiting  # type: bool
Beispiel #11
0
    def __init__(self, identifier: str, key: int = None):
        if not isinstance(identifier, str):
            raise Exceptions.IncorrectTypeException(identifier, "identifier",
                                                    (str, ))

        if key is not None:
            if not isinstance(key, int):
                raise Exceptions.IncorrectTypeException(key, "key", (int, ))

        self.Identifier = identifier  # type: str
        self.Key = key  # type: typing.Optional[int]

        self.Children = list()  # type: typing.List[_Identifier]
Beispiel #12
0
    def Verify(cls,
               value: bool,
               lastChangeVersion: Version.Version = None) -> bool:
        if not isinstance(value, bool):
            raise Exceptions.IncorrectTypeException(value, "value", (bool, ))

        if not isinstance(lastChangeVersion,
                          Version.Version) and lastChangeVersion is not None:
            raise Exceptions.IncorrectTypeException(lastChangeVersion,
                                                    "lastChangeVersion",
                                                    (Version.Version, "None"))

        return value
Beispiel #13
0
def SetupAnnouncer(announcer: typing.Type[Announcer]) -> None:
    if not isinstance(announcer, type):
        raise Exceptions.IncorrectTypeException(announcer, "announcer",
                                                (type, ))

    if not issubclass(announcer, Announcer):
        raise Exceptions.DoesNotInheritException("announcer", (Announcer, ))

    if announcer in _announcers:
        return

    _Register(announcer)

    _SortAnnouncer()
Beispiel #14
0
    def __init__(self, setting: typing.Type[AbstractSettings.SettingAbstract]):
        """
		A wrapper for the standard settings system used by NeonOcean
		"""

        if not isinstance(setting, type):
            raise Exceptions.IncorrectTypeException(setting, "setting",
                                                    (type, ))

        if not issubclass(setting, AbstractSettings.SettingAbstract):
            raise Exceptions.DoesNotInheritException(
                "setting", (AbstractSettings.SettingAbstract, ))

        super().__init__(setting)
Beispiel #15
0
	def Build (self, value: typing.Tuple[typing.Union[int, str], ...]) -> None:
		if not isinstance(value, tuple):
			raise Exceptions.IncorrectTypeException(value, "Build", (tuple,))

		for buildIdentifierIndex in range(len(value)):  # type: int
			buildIdentifier = value[buildIdentifierIndex]  # type: typing.Union[str, int]

			if not isinstance(buildIdentifier, (str, int)):
				raise Exceptions.IncorrectTypeException(value, "PreRelease[%s]" % buildIdentifierIndex, (str, int))

			if isinstance(buildIdentifier, str):
				if not self.ValidStringIdentifier(buildIdentifier):
					raise ValueError("'PreRelease[%s]' contains a character outside what is allowed (0-9, A-Z, a-z, or -).\nValue: %s" % (buildIdentifierIndex, buildIdentifier))

		self._build = value
Beispiel #16
0
    def Set(self,
            key: str,
            value,
            autoSave: bool = True,
            autoUpdate: bool = True) -> None:
        """
		Set the value of the persistent data specified by the key. The value is deep copied before being but into storage, modifying the value after setting
		it will not change the stored version.

		:param key: The name of the persistent data, is case sensitive.
		:type key: str
		:param value: The value the persistent data will be changing to. This must be of the type specified for the target persistent data during setup.
		:param autoSave: Whether or not to automatically save the persistent data after changing the value.
		 				 This can allow you to change multiple values at once without saving each time.
		:type autoSave: bool
		:param autoUpdate: Whether or not to automatically notify callbacks to the fact that a value has changed.
						   This can allow you to change multiple values at once without calling update callbacks each time.
		:type autoUpdate: bool
		:rtype: None
		"""

        if not isinstance(key, str):
            raise Exceptions.IncorrectTypeException(key, "key", (str, ))

        if not self.IsSetup(key):
            raise Exception("Persistent data '" + key + "' is not setup.")

        valueStorage = self._storage[key]

        if not isinstance(value, valueStorage.ValueType):
            raise Exceptions.IncorrectTypeException(value, "value",
                                                    (valueStorage.ValueType, ))

        if not isinstance(autoSave, bool):
            raise Exceptions.IncorrectTypeException(autoSave, "autoSave",
                                                    (bool, ))

        if not isinstance(autoUpdate, bool):
            raise Exceptions.IncorrectTypeException(autoUpdate, "autoUpdate",
                                                    (bool, ))

        valueStorage.Set(value, self.CurrentVersion)

        if autoSave:
            self.Save()

        if autoUpdate:
            self.Update()
Beispiel #17
0
	def ShowDialog (self,
					setting: UISettingsShared.SettingWrapper,
					returnCallback: typing.Callable[[], None] = None,
					**kwargs) -> None:

		if not isinstance(setting, UISettingsShared.SettingWrapper):
			raise Exceptions.IncorrectTypeException(setting, "setting", (UISettingsShared.SettingWrapper,))

		if not isinstance(returnCallback, typing.Callable) and returnCallback is not None:
			raise Exceptions.IncorrectTypeException(returnCallback, "returnCallback", ("Callable", None))

		if services.current_zone() is None:
			Debug.Log("Tried to show setting dialog before a zone was loaded\n" + str.join("", traceback.format_stack()), self.HostNamespace, Debug.LogLevels.Warning, group = self.HostNamespace, owner = __name__)
			return

		self._ShowDialogInternal(setting, setting.Get(ignoreOverride = True), kwargs, returnCallback = returnCallback)
Beispiel #18
0
    def __init__(self,
                 filePath: str,
                 currentVersion: Version.Version,
                 hostNamespace: str = This.Mod.Namespace,
                 alwaysSaveValues: bool = False):
        """
		:param filePath: The file path this persistence object will be written to and read from.
		:type filePath: str
		:param currentVersion: The current version of what ever will be controlling this persistence object.
							   This value can allow you to correct outdated persistent data.
		:type currentVersion: Version.Version
		:param hostNamespace: Errors made by this persistent object will show up under this namespace.
		:type hostNamespace: str
		:param alwaysSaveValues: If this value is true this persistence object will save all values. Otherwise this object will not save values that have not been
		set or were reset at some point.
		:type alwaysSaveValues: bool
		"""

        if not isinstance(filePath, str):
            raise Exceptions.IncorrectTypeException(filePath, "path", (str, ))

        super().__init__(currentVersion,
                         hostNamespace=hostNamespace,
                         alwaysSaveValues=alwaysSaveValues)

        self.FilePath = filePath  # type: str
Beispiel #19
0
    def Reset(self,
              key: str = None,
              autoSave: bool = True,
              autoUpdate: bool = True) -> None:
        """
		Resets persistent data to its default value.

		:param key: The name of the persistent data, is case sensitive. If the key is none, all values will be reset.
		:type key: str
		:param autoSave: Whether or not to automatically save the persistent data after resetting the values.
		:type autoSave: bool
		:param autoUpdate: Whether or not to automatically notify callbacks to the fact that the values have been reset.
		:type autoUpdate: bool
		:rtype: None
		"""

        if not isinstance(key, str) and key is not None:
            raise Exceptions.IncorrectTypeException(key, "key", (str, ))

        if key is None:
            for valueStorage in self._storage.values(
            ):  # type: str, Persistent.Value
                valueStorage.Reset()
        else:
            if not self.IsSetup(key):
                raise Exception("Persistent data '" + key + "' is not setup.")

            valueStorage = self._storage[key]  # type: Persistent.Value
            valueStorage.Reset()

        if autoSave:
            self.Save()

        if autoUpdate:
            self.Update()
Beispiel #20
0
	def ShowDialog (self,
					listPath: str,
					returnCallback: typing.Callable[[], None] = None,
					**kwargs) -> None:

		if not isinstance(listPath, str):
			raise Exceptions.IncorrectTypeException(listPath, "listPath", (str,))

		if not isinstance(returnCallback, typing.Callable) and returnCallback is not None:
			raise Exceptions.IncorrectTypeException(returnCallback, "returnCallback", ("Callable", None))

		if services.current_zone() is None:
			Debug.Log("Tried to show setting dialog before a zone was loaded\n" + str.join("", traceback.format_stack()), self.HostNamespace, Debug.LogLevels.Warning, group = self.HostNamespace, owner = __name__)
			return

		self._ShowDialogInternal(listPath, kwargs, returnCallback = returnCallback)
Beispiel #21
0
def _ShowURL(urlHex: str, _connection: int = None) -> None:
    try:
        if not isinstance(urlHex, str):
            raise Exceptions.IncorrectTypeException(urlHex, "urlHex", (str, ))
    except Exception as e:
        Debug.Log("Incorrect types for command.",
                  This.Mod.Namespace,
                  Debug.LogLevels.Exception,
                  group=This.Mod.Namespace,
                  owner=__name__,
                  exception=e)
        return

    try:
        url = codecs.decode(urlHex, "hex").decode("utf-8")
        Generic.ShowOpenBrowserDialog(url)
    except Exception as e:
        Debug.Log("Failed to show distribution url.\nURL hex '" + str(urlHex) +
                  "'.",
                  This.Mod.Namespace,
                  Debug.LogLevels.Exception,
                  group=This.Mod.Namespace,
                  owner=__name__,
                  exception=e)
        return
Beispiel #22
0
def ShowNotification(queue: bool = False, **notificationArguments) -> None:
    """
	:param queue: When true and the UI dialog service is not running the dialog will be put in queue until it is. Otherwise the dialog will be ignored.
				  The ui dialog service will only run while a zone is loaded.
	:type queue: bool
	"""

    global _dialogDisplayable

    if not isinstance(queue, bool):
        raise Exceptions.IncorrectTypeException(queue, "queue", (bool, ))

    if not "owner" in notificationArguments:
        notificationArguments["owner"] = None

    dialog = ui_dialog_notification.UiDialogNotification.TunableFactory(
    ).default(**notificationArguments
              )  # type: ui_dialog_notification.UiDialogNotification

    if services.current_zone() is not None and _dialogDisplayable:
        dialog.show_dialog()
    else:
        _dialogDisplayable = False

        if queue:
            _queue.append(dialog)
Beispiel #23
0
	def _UpdateDistribution (self, informationDictionary: dict) -> bool:
		informationKey = "Distribution"  # type: str

		try:
			distributionData = informationDictionary.get(informationKey, None)  # type: typing.Optional[typing.Dict[str, dict]]

			if distributionData is None:
				return True

			if not isinstance(distributionData, dict) and distributionData is not None:
				raise Exceptions.IncorrectTypeException(distributionData, "Root[%s]" % informationKey, (dict, None))

			def GetStringValue (targetKey: str) -> typing.Optional[str]:
				targetValue = distributionData.get(targetKey, None)  # type: typing.Optional[str]

				if not isinstance(targetValue, str) and targetValue is not None:
					raise Exceptions.IncorrectTypeException(targetValue, "Root[%s][%s]" % (informationKey, targetKey), (str, None))

				return targetValue

			updatesController = GetStringValue("UpdatesController")  # type: typing.Optional[str]
			updatesFileURL = GetStringValue("UpdatesFileURL")  # type: typing.Optional[str]
			downloadURL = GetStringValue("DownloadURL")  # type: typing.Optional[str]
			previewDownloadURL = GetStringValue("PreviewDownloadURL")  # type: typing.Optional[str]

			self.Mod.Distribution = Mods.Distribution(updatesController, updatesFileURL, downloadURL, previewDownloadURL)
			return True
		except Exception:
			Debug.Log("Failed to read mod information file value '%s' for '%s'." % (informationKey, self.Mod.Namespace), This.Mod.Namespace, Debug.LogLevels.Exception, group = This.Mod.Namespace, owner = __name__)
			return False
Beispiel #24
0
			def GetStringValue (targetKey: str) -> typing.Optional[str]:
				targetValue = distributionData.get(targetKey, None)  # type: typing.Optional[str]

				if not isinstance(targetValue, str) and targetValue is not None:
					raise Exceptions.IncorrectTypeException(targetValue, "Root[%s][%s]" % (informationKey, targetKey), (str, None))

				return targetValue
Beispiel #25
0
def GetLocalizationStringByIdentifier(
        identifier: str,
        *tokens,
        fallbackText: str = None) -> localization.LocalizedString:
    """
	Find localized string by identifier. No identifiers will be loaded until a zone is loaded.
	:param identifier: This function will look through the list or registered entries to find one with this identifier.
					   Case will be ignored while searching for the identifier.
					   If the corresponding identifier object cannot be found a localization string will be created containing the identifier.
	:type identifier: str
	:param tokens: Valid tokens include any object with a function called "populate_localization_token", numbers, strings, LocalizedString objects, and arrays.
	 			   Array tokens seem to be considered lists of sims and all objects inside them require the "populate_localization_token" function.

	:param fallbackText: The text that will be fallen back to if the specified identifier isn't registered. If this is None the identifier will be used instead.
	:type fallbackText: str

	:rtype: localization.LocalizedString
	"""

    if not isinstance(identifier, str):
        raise Exceptions.IncorrectTypeException(identifier, "identifier",
                                                (str, ))

    identifierObject = _GetIdentifierObject(_SplitIdentifier(identifier))

    if identifierObject.Key is not None:
        return GetLocalizationStringByKey(identifierObject.Key, *tokens)
    else:
        if fallbackText is None:
            return CreateLocalizationString(identifier)
        else:
            return CreateLocalizationString(fallbackText)
Beispiel #26
0
	def _UpdateCompatibility (self, informationDictionary: dict) -> bool:
		informationKey = "Compatibility"  # type: str

		compatibilityLowestVersionKey = "LowestVersion"  # type: str
		compatibilityHighestVersionKey = "HighestVersion"  # type: str

		try:
			compatibility = informationDictionary.get(informationKey, dict())  # type: typing.Dict[str, dict]

			if not isinstance(compatibility, dict):
				raise Exceptions.IncorrectTypeException(compatibility, "Root[%s]" % informationKey, (dict,))

			for namespace, modDictionary in compatibility.items():  # type: str, typing.Dict[str, str]
				if not isinstance(namespace, str):
					raise Exceptions.IncorrectTypeException(namespace, "Root[%s]<Key>" % informationKey, (str,))

				if namespace == self.Mod.Namespace:
					raise Exception("A mod cannot have compatibility information for its self.")

				if not isinstance(modDictionary, dict):
					raise Exceptions.IncorrectTypeException(modDictionary, "Root[%s][%s]" % (informationKey, namespace), (dict,))

				if not compatibilityLowestVersionKey in modDictionary:
					lowestVersion = None
				else:
					lowestVersion = modDictionary[compatibilityLowestVersionKey]

				if not isinstance(lowestVersion, str) and lowestVersion is not None:
					raise Exceptions.IncorrectTypeException(lowestVersion, "Root[%s][%s][%s]" % (informationKey, namespace, compatibilityLowestVersionKey), (str, "None"))

				if not compatibilityHighestVersionKey in modDictionary:
					highestVersion = None
				else:
					highestVersion = modDictionary[compatibilityHighestVersionKey]

				if not isinstance(highestVersion, str) and highestVersion is not None:
					raise Exceptions.IncorrectTypeException(highestVersion, "Root[%s][%s][%s]" % (informationKey, namespace, compatibilityHighestVersionKey), (str, "None"))

				lowestVersionObject = Version.Version(versionString = lowestVersion) if lowestVersion is not None else None  # type: typing.Optional[Version.Version]
				highestVersionObject = Version.Version(versionString = highestVersion) if highestVersion is not None else None  # type: typing.Optional[Version.Version]

				self.Mod.Compatibility.append(Mods.Compatibility(namespace, lowestVersionObject, highestVersionObject))

			return True
		except Exception:
			Debug.Log("Failed to read mod information file value '%s' for '%s'." % (informationKey, self.Mod.Namespace), This.Mod.Namespace, Debug.LogLevels.Exception, group = This.Mod.Namespace, owner = __name__)
			return False
Beispiel #27
0
def ParseNumber(string: str) -> typing.Union[float, int]:
    if not isinstance(string, str):
        raise Exceptions.IncorrectTypeException(string, "string", (str, ))

    try:
        return int(string)
    except Exception:
        return float(string)
Beispiel #28
0
def PatchDirectly(originalCallable: typing.Callable,
                  targetFunction: typing.Callable,
                  patchType: PatchTypes = PatchTypes.After,
                  permanent: bool = False) -> typing.Callable:
    """
	Combine a function with another function or method, the patched function will be returned upon completion.
	:param originalCallable: The original callable object to be patched.
	:param targetFunction: The function object that will be combined with the original. This can only be a function or a built in function.
	:type targetFunction: typing.Callable
	:param patchType: Controls when the original callable is called. A value of PatchTypes.Custom requires that the target function take an extra argument
					  in the first argument position. The extra argument will be a reference to the original callable.
	:type patchType: PatchTypes
	:param permanent: Whether or not the patch will be disabled if the module the patching function resides in is unloaded.
	:type permanent: bool
	:return: Returns the patched function.
	:rtype: typing.Callable
	"""

    if not isinstance(
            originalCallable, types.BuiltinFunctionType) and not isinstance(
                originalCallable, types.FunctionType) and not isinstance(
                    originalCallable, types.MethodType):
        raise Exception(
            Types.GetFullName(originalCallable) +
            " is not a function, built-in function or a method.")

    if not isinstance(targetFunction, types.FunctionType) and not isinstance(
            targetFunction, types.BuiltinFunctionType):
        raise Exceptions.IncorrectTypeException(
            targetFunction, "targetFunction",
            (types.FunctionType, types.BuiltinFunctionType))

    if not isinstance(patchType, PatchTypes):
        raise Exceptions.IncorrectTypeException(patchType, "patchType",
                                                (PatchTypes, ))

    if not isinstance(permanent, bool):
        raise Exceptions.IncorrectTypeException(permanent, "permanent",
                                                (bool, ))

    information = _Information(originalCallable, targetFunction, patchType,
                               permanent)  # type: _Information
    patchedFunction = _Wrapper(information)
    _storage.append(information)

    return patchedFunction
Beispiel #29
0
def ShowOpenBrowserDialog(
        url: str,
        returnCallback: typing.Optional[typing.Callable[[],
                                                        None]] = None) -> None:
    """
	Show a dialog asking the user to confirm their intention to open a website in their browser.
	:param url: The url to be opened when the user clicks the ok button.
	:type url: str
	:param returnCallback: Called after the dialog has closed.
	:type returnCallback: typing.Optional[typing.Callable[[], None]]
	"""

    if not isinstance(url, str):
        raise Exceptions.IncorrectTypeException(url, "url", (str, ))

    if not isinstance(returnCallback,
                      typing.Callable) and returnCallback is not None:
        raise Exceptions.IncorrectTypeException(returnCallback,
                                                "returnCallback",
                                                ("Callable", None))

    dialogArguments = {
        "text": OpenBrowserDialogText.GetCallableLocalizationString(url),
        "text_ok": OpenBrowserDialogYesButton.GetCallableLocalizationString(),
        "text_cancel":
        OpenBrowserDialogNoButton.GetCallableLocalizationString()
    }

    def DialogCallback(dialogReference: ui_dialog.UiDialogOkCancel) -> None:
        try:
            if dialogReference.response == ui_dialog.ButtonType.DIALOG_RESPONSE_OK:
                webbrowser.open(url, new=2)

            if returnCallback is not None:
                returnCallback()
        except Exception:
            Debug.Log(
                "Failed to run the callback for the open browser dialog.",
                This.Mod.Namespace,
                Debug.LogLevels.Exception,
                group=This.Mod.Namespace,
                owner=__name__)

    Dialogs.ShowOkCancelDialog(callback=DialogCallback,
                               queue=False,
                               **dialogArguments)
Beispiel #30
0
    def ShowDialog(self,
                   returnCallback: typing.Callable[[], None] = None) -> None:
        if not isinstance(returnCallback,
                          typing.Callable) and returnCallback is not None:
            raise Exceptions.IncorrectTypeException(returnCallback,
                                                    "returnCallback",
                                                    ("Callable", None))

        return self.Setting.ShowDialog(returnCallback=returnCallback)