Skip to content

Commit

Permalink
better names
Browse files Browse the repository at this point in the history
  • Loading branch information
synodriver committed Mar 1, 2025
1 parent 29785cd commit d7f6220
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
39 changes: 21 additions & 18 deletions pysproto/sprotodump.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def packfield(f):
return packbytes(strtbl.getvalue())


def packtype(name, t, alltypes):
def packtype(name, types_, alltypes):
fields = []
tmp = {}
for f in t: # type: dict
for f in types_: # type: dict
tmp["array"] = f["array"]
tmp["name"] = f["name"]
tmp["tag"] = f["tag"]
Expand All @@ -86,19 +86,22 @@ def packtype(name, t, alltypes):
tmp["map"] = 1
c = 0
min_t = sys.maxsize
for n, t in enumerate(subtype["fields"]):
for n, types_ in enumerate(subtype["fields"]):
c += 1
if t["tag"] < min_t:
min_t = t["tag"]
if types_["tag"] < min_t:
min_t = types_["tag"]
f["key"] = n
assert c == 2, (
"Invalid map definition: %s, must only have two fields"
% tmp["name"]
)
stfield = subtype["fields"].get(f.get("key", None), None)
if not stfield or not stfield.get("buildin", None):
raise AssertionError("Invalid map index :" + f["key"])
tmp["key"] = stfield.get("tag", None)
try:
if not stfield or not stfield.get("buildin", None):
raise AssertionError("Invalid map index :" + str(f["key"]))
tmp["key"] = stfield.get("tag", None)
except AttributeError:
tmp["key"] = stfield

# tmp["key"] = subtype["fields"][f["key"]["name"]]
# assert tmp["key"], "Invalid map index %d" % f["key"]["name"]
Expand Down Expand Up @@ -148,25 +151,25 @@ def packproto(name, p, alltypes) -> bytes:
return packbytes(tmp.getvalue())


def packgroup(t, p) -> bytes:
def packgroup(types_, protocols) -> bytes:
"""
:param t: Type
:param p: Protocol
:param types_: Type
:param protocols: Protocol
:return:
"""
if not t:
assert p
if not types_:
assert protocols
return b"\0\0"
tp = None
alltypes = {}
alltype_names = []
for name in t:
for name in types_:
alltype_names.append(name)
alltype_names.sort()
for idx, name in enumerate(alltype_names):
fields = {}
for type_fields in t[name]:
for type_fields in types_[name]:
if (
type_fields["typename"] in sprotoparser.builtin_types
): # todo add key too nested
Expand All @@ -176,13 +179,13 @@ def packgroup(t, p) -> bytes:
tt = BytesIO()
for name in alltype_names:
tt.write(
packtype(name, t[name], alltypes)
packtype(name, types_[name], alltypes)
) # alltypes["Person"]["fields"]["key"]

tt = packbytes(tt.getvalue())
if p:
if protocols:
tmp = []
for name, tbl in p.items():
for name, tbl in protocols.items():
tmp.append(tbl)
tbl["name"] = name
tmp = sorted(tmp, key=lambda k: k["tag"])
Expand Down
44 changes: 22 additions & 22 deletions pysproto/sprotoparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class TypeName(object):
grammar = flag("is_arr", "*"), attr("fullname", fullname)


class Filed(List):
class Field(List):
grammar = (
attr("filed", word),
attr("field", word),
attr("tag", tag),
":",
attr("typename", TypeName),
Expand All @@ -42,7 +42,7 @@ class Type(List):
pass


Struct.grammar = "{", nomeaning, attr("fileds", maybe_some([Filed, Type])), "}"
Struct.grammar = "{", nomeaning, attr("fields", maybe_some([Field, Type])), "}"
Type.grammar = nomeaning, ".", name(), attr("struct", Struct), nomeaning


Expand All @@ -53,7 +53,7 @@ class Sub_pro_type(Keyword):
class Subprotocol(List):
grammar = (
attr("subpro_type", Sub_pro_type),
attr("pro_filed", [TypeName, Struct]),
attr("pro_field", [TypeName, Struct]),
nomeaning,
)

Expand All @@ -65,7 +65,7 @@ class Protocol(List):
attr("tag", tag),
"{",
nomeaning,
attr("fileds", maybe_some(Subprotocol)),
attr("fields", maybe_some(Subprotocol)),
"}",
nomeaning,
)
Expand Down Expand Up @@ -154,25 +154,25 @@ def convert_type(obj, parent_name=""): # todo add mainkey
@staticmethod
def convert_struct(obj, name=""): # todo 你 加入decimal
struct = []
for filed in obj.fileds:
if type(filed) == Filed:
filed_typename = filed.typename.fullname
for field in obj.fields:
if type(field) == Field:
filed_typename = field.typename.fullname
filed_type = Convert.get_typename(filed_typename)
filed_info = {}
filed_info["name"] = filed.filed
filed_info["tag"] = int(filed.tag)
filed_info["array"] = filed.typename.is_arr
filed_info["name"] = field.field
filed_info["tag"] = int(field.tag)
filed_info["array"] = field.typename.is_arr
filed_info["typename"] = filed_typename
filed_info["type"] = filed_type
if len(filed) > 0:
if len(field) > 0:
if filed_typename == "integer":
filed_info["decimal"] = filed[0]
filed_info["decimal"] = field[0]
else:
filed_info["key"] = filed[0] # todo 解决冲突
filed_info["key"] = field[0] # todo 解决冲突

struct.append(filed_info)
elif type(filed) == Type:
Convert.convert_type(filed, name)
elif type(field) == Type:
Convert.convert_type(field, name)
return struct

@staticmethod
Expand All @@ -186,18 +186,18 @@ def convert_protocol(obj):
protocol = {}
protocol["tag"] = int(obj.tag)
protocol["name"] = obj.name
for fi in obj.fileds:
if type(fi.pro_filed) == TypeName:
assert fi.pro_filed.is_arr == False, "syntax error at %s.%s" % (
for fi in obj.fields:
if type(fi.pro_field) == TypeName:
assert fi.pro_field.is_arr == False, "syntax error at %s.%s" % (
obj.name,
fi.subpro_type,
)
newtype_name = str("".join(fi.pro_filed.fullname))
newtype_name = str("".join(fi.pro_field.fullname))
protocol[fi.subpro_type] = newtype_name
elif type(fi.pro_filed) == Struct:
elif type(fi.pro_field) == Struct:
newtype_name = obj.name + "." + fi.subpro_type
Convert.type_dict[newtype_name] = Convert.convert_struct(
fi.pro_filed, newtype_name
fi.pro_field, newtype_name
)
protocol[fi.subpro_type] = newtype_name

Expand Down

0 comments on commit d7f6220

Please sign in to comment.