d
This commit is contained in:
39
Utilities/platform/2/parsing/CoreService.py
Normal file
39
Utilities/platform/2/parsing/CoreService.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from parsing.readKeyValue import *
|
||||
from parsing.valueArray import *
|
||||
|
||||
class CoreService:
|
||||
def __init__(self):
|
||||
self.actions = {}
|
||||
self.isPresent = False
|
||||
self.pipes = {}
|
||||
|
||||
def parseAction(self, line):
|
||||
kv = readKeyValue(line)
|
||||
|
||||
# В действии всегда должен быть хотя бы ключ.
|
||||
if kv is None:
|
||||
return
|
||||
|
||||
# Лишь ключ.
|
||||
if kv[1] is None:
|
||||
self.actions[kv[0]] = ""
|
||||
return
|
||||
|
||||
# Ключ и значение.
|
||||
self.actions[kv[0]] = kv[1]
|
||||
|
||||
def parsePipe(self, line):
|
||||
kv = readKeyValue(line)
|
||||
# В канале всегда должны быть и ключ, и значение.
|
||||
if (
|
||||
kv is None or
|
||||
kv[1] is None
|
||||
):
|
||||
return
|
||||
|
||||
values = valueArray(kv[1])
|
||||
# В канале каждое значение должно быть массивом.
|
||||
if values is None:
|
||||
return
|
||||
|
||||
self.pipes[kv[0]] = values
|
||||
31
Utilities/platform/2/parsing/Mode.py
Normal file
31
Utilities/platform/2/parsing/Mode.py
Normal file
@@ -0,0 +1,31 @@
|
||||
class Mode:
|
||||
def __init__(self):
|
||||
self.reset()
|
||||
|
||||
def parseLine(self, line):
|
||||
if line.startswith("model:"):
|
||||
self.reset()
|
||||
self.isModel = True
|
||||
elif line.startswith("core:"):
|
||||
self.reset()
|
||||
self.isCore = True
|
||||
elif line.startswith("service:"):
|
||||
self.reset()
|
||||
self.isService = True
|
||||
elif line.startswith("world:"):
|
||||
self.reset()
|
||||
self.isWorld = True
|
||||
elif line.startswith(" actions:"):
|
||||
self.isAction = True
|
||||
self.isPipe = False
|
||||
elif line.startswith(" pipes:"):
|
||||
self.isAction = False
|
||||
self.isPipe = True
|
||||
|
||||
def reset(self):
|
||||
self.isAction = False
|
||||
self.isCore = False
|
||||
self.isModel = False
|
||||
self.isPipe = False
|
||||
self.isService = False
|
||||
self.isWorld = False
|
||||
24
Utilities/platform/2/parsing/ModelWorld.py
Normal file
24
Utilities/platform/2/parsing/ModelWorld.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from parsing.readKeyValue import *
|
||||
from parsing.valueArray import *
|
||||
|
||||
class ModelWorld:
|
||||
def __init__(self):
|
||||
self.fields = {}
|
||||
|
||||
def parseLine(self, line):
|
||||
kv = readKeyValue(line)
|
||||
# В модели/мире всегда должен быть хотя бы ключ.
|
||||
if kv is None:
|
||||
return
|
||||
|
||||
# Лишь ключ.
|
||||
if kv[1] is None:
|
||||
self.fields[kv[0]] = []
|
||||
return
|
||||
|
||||
values = valueArray(kv[1])
|
||||
# В модели/мире указанное значение должно быть массивом.
|
||||
if values is None:
|
||||
return
|
||||
|
||||
self.fields[kv[0]] = values
|
||||
9
Utilities/platform/2/parsing/Structure.py
Normal file
9
Utilities/platform/2/parsing/Structure.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from parsing.CoreService import *
|
||||
from parsing.ModelWorld import *
|
||||
|
||||
class Structure:
|
||||
def __init__(self):
|
||||
self.core = CoreService()
|
||||
self.model = ModelWorld()
|
||||
self.service = CoreService()
|
||||
self.world = ModelWorld()
|
||||
42
Utilities/platform/2/parsing/parseLines.py
Normal file
42
Utilities/platform/2/parsing/parseLines.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from parsing.CoreService import *
|
||||
from parsing.Mode import *
|
||||
from parsing.ModelWorld import *
|
||||
|
||||
def parseLines(lines, structure):
|
||||
mode = Mode()
|
||||
for line in lines:
|
||||
# Определяем режим строки.
|
||||
mode.parseLine(line)
|
||||
ln = line.strip()
|
||||
|
||||
# Игнорируем пустую строку.
|
||||
if len(ln) == 0:
|
||||
continue
|
||||
|
||||
# Игнорируем комментарий.
|
||||
if ln.startswith("#"):
|
||||
continue
|
||||
|
||||
# model.
|
||||
if mode.isModel:
|
||||
structure.model.parseLine(ln)
|
||||
|
||||
# core.
|
||||
elif mode.isCore:
|
||||
structure.core.isPresent = True
|
||||
if mode.isAction:
|
||||
structure.core.parseAction(ln)
|
||||
elif mode.isPipe:
|
||||
structure.core.parsePipe(ln)
|
||||
|
||||
# service.
|
||||
elif mode.isService:
|
||||
structure.service.isPresent = True
|
||||
if mode.isAction:
|
||||
structure.service.parseAction(ln)
|
||||
elif mode.isPipe:
|
||||
structure.service.parsePipe(ln)
|
||||
|
||||
# world.
|
||||
elif mode.isWorld:
|
||||
structure.world.parseLine(ln)
|
||||
15
Utilities/platform/2/parsing/readKeyValue.py
Normal file
15
Utilities/platform/2/parsing/readKeyValue.py
Normal file
@@ -0,0 +1,15 @@
|
||||
def readKeyValue(line):
|
||||
parts = line.split(": ")
|
||||
# Ключ со значением.
|
||||
if len(parts) == 2:
|
||||
return (parts[0], parts[1])
|
||||
|
||||
# Ключ без значения.
|
||||
if (
|
||||
line.endswith(":") == False and
|
||||
len(parts) == 1
|
||||
):
|
||||
return (parts[0], None)
|
||||
|
||||
# Не является ключом со значением.
|
||||
return None
|
||||
12
Utilities/platform/2/parsing/valueArray.py
Normal file
12
Utilities/platform/2/parsing/valueArray.py
Normal file
@@ -0,0 +1,12 @@
|
||||
def valueArray(value):
|
||||
# Удостоверяем наличие скобок.
|
||||
if not (
|
||||
value.startswith("[") and
|
||||
value.endswith("]")
|
||||
):
|
||||
return None
|
||||
|
||||
# Исключаем скобки.
|
||||
noBrackets = value[1:-1]
|
||||
parts = noBrackets.split(", ")
|
||||
return parts
|
||||
Reference in New Issue
Block a user