diff options
| author | Fulgen301 <tokmajigeorge@gmailc.om> | 2017-06-18 14:27:01 +0200 |
|---|---|---|
| committer | Fulgen301 <tokmajigeorge@gmail.com> | 2017-07-11 11:10:32 +0200 |
| commit | 16dbb3ee022fdf504d10d423dd5ed55bafe5aa85 (patch) | |
| tree | 4ce78551f47b712d79a55c9dd787c68623aab938 | |
| parent | eb3d7298fda73b82cbbf1beef6568c335aaf5714 (diff) | |
| download | pycrctrl-16dbb3ee022fdf504d10d423dd5ed55bafe5aa85.tar.gz pycrctrl-16dbb3ee022fdf504d10d423dd5ed55bafe5aa85.zip | |
Major fixes
* Output is now given as string
* Remove capability code
| -rw-r--r-- | pycrctrl/pycrctrl.py | 96 |
1 files changed, 16 insertions, 80 deletions
diff --git a/pycrctrl/pycrctrl.py b/pycrctrl/pycrctrl.py index 6c86940..37c95b5 100644 --- a/pycrctrl/pycrctrl.py +++ b/pycrctrl/pycrctrl.py @@ -123,12 +123,6 @@ class CmdResult(IntEnum): RightsFail = 2 RuntimeError = 3 -class Capability(IntEnum): - Unknown = -1 - User = 0 - Admin = 1 - Moderator = 2 - class Updater(object): parent = None __current_revision = "" @@ -229,7 +223,6 @@ class PyCRCtrl(object): scenario = "" commands = {} - codec = None path = QString() scenlist = QStringList() @@ -241,9 +234,6 @@ class PyCRCtrl(object): __state = "Lobby" __ingamechat = "aktiviert" - capabilities = {} - _capa_old = {} - updater = None def __init__(self, irc=None, path=None, config="pycrctrl.conf"): @@ -253,7 +243,6 @@ class PyCRCtrl(object): self.irc = irc self.path = path self.loadConfigFile(config) - self.loadCapabilities() self.loadScenarioList() self.queue = queue.Queue(5) @@ -295,16 +284,6 @@ class PyCRCtrl(object): self.league_scenlist.list = Unpickler(fobj).load() return True - - def setConfigEntry(self, *args): - pass - - def loadCapabilities(self) -> bool: - if self.path == None: - return False - - self._capa_old = json.load(open(os.path.join(self.path, "capabilities.conf"), "r")) - return True def loadConfigFile(self, config) -> bool: @@ -376,8 +355,6 @@ class PyCRCtrl(object): return True def setupConfig(self, config): - #codec - self.codec = QTextCodec.codecForName(config["Clonk"]["encoding"]) #commandline sep = ":" if config["Clonk"]["commandlinePrefix"] == "/" else "=" config["Clonk"]["commandline"] = " ".join("{}{}".format(config["Clonk"]["commandlinePrefix"], (sep.join(str(entry)) if isinstance(entry, list) else str(entry))) for entry in config["Clonk"]["commandline"]) @@ -392,14 +369,14 @@ class PyCRCtrl(object): return config def decodeRegExp(self, regexp): - return self.codec.toUnicode(base64.b64decode(regexp)) + return base64.b64decode(regexp).decode(self.config["Clonk"]["encoding"]) def host(self, scenario=None, user=None) -> str: if scenario == None: return (CmdResult.SyntaxFail, "Bitte gib einen Szenarionamen an!") if type(scenario) in [bytes, QByteArray]: try: - scenario = self.codec.toUnicode(scenario) + scenario = scenario.decode(self.config["Clonk"]["encoding"]) except: return (CmdResult.SyntaxFail, "Unbekannter Datentyp!") @@ -431,7 +408,6 @@ class PyCRCtrl(object): else: self.scenario = random.choice(self.scenlist) - self.capabilities = deepcopy(self._capa_old) self.clonk = subprocess.Popen( './{} {} "{}"'.format(self.config["Clonk"]["engine"], self.config["Clonk"]["commandline"] + self.config["Clonk"]["commandlinePrefix"] + ("league" if self.scenario in self.league_scenlist() else "noleague"),self.scenario), 0, @@ -440,7 +416,8 @@ class PyCRCtrl(object): subprocess.PIPE, subprocess.STDOUT, shell=True, - cwd=self.path + cwd=self.path, + encoding=self.config["Clonk"]["encoding"] ) self.state = "Lobby" self.readServerOutput() @@ -460,9 +437,9 @@ class PyCRCtrl(object): try: output = self.clonk.stdout.readline() - if self.config["Clonk"]["RegExps"]["shutdownExp"].match(self.codec.toUnicode(output)): + if self.config["Clonk"]["RegExps"]["shutdownExp"].match(output): self.clonk.stdin.close() - elif output == b"" and self.clonk.poll() is not None: + elif output == "" and self.clonk.poll() is not None: if self.clonk: self.clonk.stdin.close() self.clonk = None @@ -470,7 +447,7 @@ class PyCRCtrl(object): return elif output: - output = self.codec.toUnicode(output).splitlines()[0] + output = output.splitlines()[0] #output = output.decode("utf-8").splitlines()[0] output = output[(output.find("] ") if output.find("] ") != -1 else -2)+len("] "):] @@ -538,14 +515,14 @@ class PyCRCtrl(object): def writeToServer(self, text=None): if text == None and self.clonk == None: return (CmdResult.RuntimeError, "") - elif type(text) not in [bytes, QByteArray]: + elif type(text) not in [str, QString]: try: - text = self.codec.fromUnicode(text) + text = text.decode(self.config["Clonk"]["encoding"] except: raise IOError("Cannot write anything else to the server except the following data types: QString, bytes, str, QByteArray (got {})".format(type(text).__name__)) if self.clonk and self.clonk.stdin: - self.clonk.stdin.write(bytes(text) + b"\n") + self.clonk.stdin.write(text + "\n") self.clonk.stdin.flush() return (CmdResult.Success, "") @@ -555,7 +532,7 @@ class PyCRCtrl(object): if type(text) not in [str, QString]: try: - text = self.codec.toUnicode(text) + text = text.decode(self.config["Clonk"]["encoding"] except: raise TypeError("text must be a string!") @@ -564,37 +541,11 @@ class PyCRCtrl(object): else: self.topic = text - return bool(self.irc.sendMsg(ircmsgs.topic(self.config.get("Channels")["ingame"], text))) - - def admin(self, prm=None, user=""): - """Setzt den Rundenadmin.""" - try: - if not prm: - raise IndexError - prm = " ".join(prm) - - if (self.capabilities[prm], self.capabilities[user]) > (Capability.Admin, Capability.Admin): - return (CmdResult.RuntimeError, "Du hast bereits mehr Rechte als ein Rundenadmin!") - - - except KeyError: - pass - - except IndexError: - return (CmdResult.SyntaxFail, "Nicht genügend Argumente!") - - for u, c in self.capabilities.items(): - if c == Capability.Admin: - return (CmdResult.RuntimeError, "{} ist bereits Rundenadmin!".format(u)) - - - self.capabilities[prm] = self.capabilities[user] = Capability.Admin - return (CmdResult.Success, "{} (Spielername: {}) wurde als Rundenadmin eingetragen!".format(user, prm)) + return bool(self.irc.sendMsg(ircmsgs.topic(self.config.get["Clonk"]["IRC"]["Channels"]["ingame"], text))) def start(self, time=None, user=""): """Startet das Spiel.""" - if not self.checkCapability(Capability.Admin, user): return (CmdResult.RightsFail, "Du hast nicht die nötige Berechtigung.".encode("utf-8")) self.stopped = False if time: try: @@ -603,14 +554,13 @@ class PyCRCtrl(object): time = 5 self.writeToServer("/start {}".format(time)) else: - self.writeToServer(b"/start") + self.writeToServer("/start") return (CmdResult.Success, "") def stop(self, prm=None, user=""): """Stoppt den Countdown.""" - if not self.checkCapability(Capability.Admin, user): return (CmdResult.RightsFail, "Du hast nicht die nötige Berechtigung.".encode("utf-8")) def stopping(self): while self.clonk and self.stopped: self.writeToServer("/start 60000") @@ -627,7 +577,7 @@ class PyCRCtrl(object): def help(self, prm=None, user=""): """Gibt die Hilfe aus.""" - self.writeToServer("Verfügbare Befehle:".encode("utf-8")) + self.writeToServer("Verfügbare Befehle:") for text, function in self.commands.items(): self.writeToServer("{} -- {}".format(text, function.__doc__)) @@ -636,7 +586,7 @@ class PyCRCtrl(object): def displayQueue(self, prm=None, user=""): """Gibt die Warteschlange aus.""" - self.writeToServer(b"Warteschlange:") + self.writeToServer("Warteschlange:") for i,scen in enumerate(self.queue.queue): self.writeToServer("{}. {}".format(i+1, scen)) @@ -646,7 +596,7 @@ class PyCRCtrl(object): def list(self, prm=None, user=""): """Zeigt die Szenarioliste an.""" - self.writeToServer(b"List:\n-------------") + self.writeToServer("List:\n-------------") for scen in self.scenlist: self.writeToServer(scen) @@ -654,7 +604,6 @@ class PyCRCtrl(object): def ircCommands(self, prm=None, user=""): """Enthält Befehle zur Steuerung der IRC-Funktionen.""" - if not self.checkCapability(Capability.Admin, user): return (CmdResult.RightsFail, "Du hast nicht die nötige Berechtigung.".encode("utf-8")) if not prm: return (CmdResult.SyntaxFail, "") @@ -694,19 +643,6 @@ class PyCRCtrl(object): self.scenlist().append(name) Pickler(open(os.path.join(self.path, "scenlist"), "wb")).dump(self.scenlist()) return self - - def checkCapability(self, capability, user=""): - if not user: - return False - - try: - return (True if self.capabilities[user] >= capability else False) - - except KeyError: - return False - - def registerHook(self, hook, function, replace=False): - pass # # |
