diff options
author | Andrei Karas <akaras@inbox.ru> | 2017-07-19 22:30:42 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2017-07-20 17:21:53 +0300 |
commit | 19edcb923f184894f8d738ab1e7a8e50cdd642c0 (patch) | |
tree | f3f925e128ea4bb8d3672a44c7fc3e6f98b46e88 /hercules/code/servertoclient/groups.py | |
parent | 46a6f9869a2828c462895f149655edfdf897f55b (diff) | |
download | tools-19edcb923f184894f8d738ab1e7a8e50cdd642c0.tar.gz tools-19edcb923f184894f8d738ab1e7a8e50cdd642c0.tar.bz2 tools-19edcb923f184894f8d738ab1e7a8e50cdd642c0.tar.xz tools-19edcb923f184894f8d738ab1e7a8e50cdd642c0.zip |
hercules: add converter from groups.conf into groups.xml
Diffstat (limited to 'hercules/code/servertoclient/groups.py')
-rw-r--r-- | hercules/code/servertoclient/groups.py | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/hercules/code/servertoclient/groups.py b/hercules/code/servertoclient/groups.py new file mode 100644 index 0000000..dbe4c8e --- /dev/null +++ b/hercules/code/servertoclient/groups.py @@ -0,0 +1,184 @@ +# -*- coding: utf8 -*- +# +# Copyright (C) 2017 Evol Online +# Author: Andrei Karas (4144) + +import re + +from code.fileutils import makeDir, readFile, saveFile + +nameSplit = re.compile(":") +comaSplit = re.compile(",") + +class Groups: + pass + +def readLine(f): + for line in f: + if len(line) == 0: + continue + if line[-1:] == "\n": + line = line[:-1] + idx = line.find("/*") + if idx >= 0: + line = line[:idx] + line = line.strip() + yield line + +def skipFor(data, f): + for line in readLine(f): + if line == data or line == data + "\n": + return True + return False + +def readCommands(f): + commands = dict() + for line in readLine(f): + if line == "}": + return commands + parts = nameSplit.split(line) + if len(parts) != 2: + continue + name = parts[0].strip() + value = parts[1].strip() + idx1 = value.find("[") + if idx1 < 0: + commands[name] = (value, "false") + else: + idx2 = value.find("]") + value = value[idx1 + 1: idx2 - idx1] + parts = comaSplit.split(value) + commands[name] = (parts[0].strip(), parts[1].strip()) + print "command: {0}, {1}".format(name, commands[name]) + return commands + +def readPermissions(f): + permissions = dict() + for line in readLine(f): + if line == "}": + return permissions + parts = nameSplit.split(line) + if len(parts) != 2: + continue + name = parts[0].strip() + value = parts[1].strip() + permissions[name] = value + print "permission: {0}, {1}".format(name, value) + return permissions + +def readInherit(data): + inherit = set() + idx1 = data.find("(") + idx2 = data.find(")") + data = data[idx1 + 1: idx2 - idx1] + parts = comaSplit.split(data) + for field in parts: + field = field.strip() + if field == "": + continue + inherit.add(field) + return inherit + +def readGroup(groups, f): + fields = dict() + commands = dict() + permissions = dict() + for line in readLine(f): + if line == "}" or line == "},": + groups.nameToId[fields["name"]] = fields["id"] + groups.idToFields[fields["id"]] = fields + groups.idToCommands[fields["id"]] = commands + groups.idToPermissions[fields["id"]] = permissions + groups.idToInherit[fields["id"]] = inherit + groups.idToNames[fields["id"]] = fields["name"] + return + parts = nameSplit.split(line) + if len(parts) != 2: + continue + name = parts[0].strip() + value = parts[1].strip() + print "line: {0}, {1}".format(name, value) + if name == "id": + fields["id"] = value + elif name == "name": + fields["name"] = value + elif name == "commands": + commands = readCommands(f) + print "Commands: {0}".format(commands) + elif name == "permissions": + permissions = readPermissions(f) + print "Permissions: {0}".format(permissions) + elif name == "inherit": + inherit = readInherit(value) + print "Inherit: {0}".format(inherit) + +def getCommandType(data): + if data[0] == "false" and data[1] == "false": + return "false" + elif data[0] == "false" and data[1] == "true": + return "other"; + elif data[0] == "true" and data[1] == "false": + return "self" + elif data[0] == "true" and data[1] == "true": + return "both" + +def getCommandsStr(commands): + data = "" + commandFormat = "\n <command name=\"{0}\" use=\"{1}\" />" + for command in commands: + data = data + commandFormat.format( + command, + getCommandType(commands[command]) + ) + return data + +def getPermissionsStr(permissions): + data = "" + permissionFormat = "\n <permission name=\"{0}\" />" + for permission in permissions: + data = data + permissionFormat.format( + permission + ) + return data + +def getInheritStr(inherits, names): + data = "" + if len(inherits) == 0: + return "" + for name in inherits: + if data != "": + data = data + "," + data += names[name] + return "\n inherit=\"{0}\"".format(data) + +def convertGroups(): + destDir = "clientdata/" + templatesDir = "templates/" + groupsConfFile = "serverdata/conf/groups.conf" + makeDir(destDir) + tpl = readFile(templatesDir + "group.tpl") + groupsTpl = readFile(templatesDir + "groups.xml") + groups = Groups() + groups.nameToId = dict() + groups.idToFields = dict() + groups.idToCommands = dict() + groups.idToPermissions = dict() + groups.idToNames = dict() + groups.idToInherit = dict() + + with open(groupsConfFile, "r") as f: + skipFor("groups: (", f) + while (skipFor("{", f) == True): + readGroup(groups, f) + + data = "" + for ids in sorted(groups.idToFields): + data = data + tpl.format( + id = ids, + name = groups.idToNames[ids], + inherit = getInheritStr(groups.idToInherit[ids], groups.nameToId), + commands = getCommandsStr(groups.idToCommands[ids]), + permissions = getPermissionsStr(groups.idToPermissions[ids]), + ) + + saveFile(destDir + "groups.xml", groupsTpl.format(data)) |