Skip to content

Commit

Permalink
bump version t.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
synodriver authored Jul 6, 2022
1 parent 5324939 commit 5162600
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 97 deletions.
9 changes: 6 additions & 3 deletions changename.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os

for f in os.listdir('dist'):
if 'linux' in f:
os.rename(os.path.join('dist', f), os.path.join('dist', f.replace('linux', 'manylinux2014')))
for f in os.listdir("dist"):
if "linux" in f:
os.rename(
os.path.join("dist", f),
os.path.join("dist", f.replace("linux", "manylinux2014")),
)
4 changes: 2 additions & 2 deletions pysproto/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from pysproto._sproto import *
from pysproto.sprotoparser import parse
from pysproto.sprotodump import dump, parse_ast
from pysproto.sprotoparser import parse

__version__ = "0.1.0.rc1"
__version__ = "0.1.0"
25 changes: 14 additions & 11 deletions pysproto/_sproto.pyx
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
# cython: language_level=3
cimport cython
from libc.stdint cimport uint8_t, int64_t, int32_t
from libc.string cimport memcpy

from cpython.object cimport PyObject
from cpython.mem cimport PyMem_Malloc, PyMem_Realloc, PyMem_Free
from cpython.long cimport PyLong_AsLong, PyLong_FromLong,PyLong_FromLongLong, PyLong_AsLongLong, PyLong_Check
from cpython.float cimport PyFloat_Check, PyFloat_AsDouble, PyFloat_FromDouble
from cpython.bool cimport PyBool_FromLong
from cpython.bytes cimport (PyBytes_AsStringAndSize, PyBytes_Check,
PyBytes_FromStringAndSize)
from cpython.dict cimport (PyDict_Check, PyDict_GetItemString, PyDict_New,
PyDict_SetItem, PyDict_SetItemString)
from cpython.exc cimport PyErr_Occurred, PyErr_Print
from cpython.float cimport PyFloat_AsDouble, PyFloat_Check, PyFloat_FromDouble
from cpython.list cimport PyList_Append, PyList_Check, PyList_New, PyList_Size
from cpython.long cimport (PyLong_AsLong, PyLong_AsLongLong, PyLong_Check,
PyLong_FromLong, PyLong_FromLongLong)
from cpython.mem cimport PyMem_Free, PyMem_Malloc, PyMem_Realloc
from cpython.object cimport PyObject
from cpython.ref cimport Py_TYPE
from cpython.dict cimport PyDict_GetItemString, PyDict_Check, PyDict_SetItemString, PyDict_SetItem, PyDict_New
from cpython.list cimport PyList_Check, PyList_Size, PyList_New, PyList_Append
from cpython.bytes cimport PyBytes_Check, PyBytes_AsStringAndSize, PyBytes_FromStringAndSize
from cpython.unicode cimport PyUnicode_DecodeUTF8
from cpython.exc cimport PyErr_Occurred, PyErr_Print
from libc.stdint cimport int32_t, int64_t, uint8_t
from libc.string cimport memcpy

from pysproto cimport sproto


cdef extern from "Python.h":
char* PyUnicode_AsUTF8AndSize(object data, Py_ssize_t* l)

Expand Down
40 changes: 29 additions & 11 deletions pysproto/sprotodump.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# coding:utf-8
from typing import Union
import struct
from io import BytesIO
import sys
import argparse
import os
import struct
import sys
from io import BytesIO
from typing import Union

import pysproto.sprotoparser as sprotoparser

Expand All @@ -26,7 +26,7 @@ def packvalue(v: int) -> bytes:


def packfield(f):
strtbl = BytesIO() # todo L289
strtbl = BytesIO() # todo L289
if f["array"]:
if f["key"]: # if has no "key" already set to f["key"] = None
if f.get("map", None):
Expand Down Expand Up @@ -91,7 +91,10 @@ def packtype(name, t, alltypes):
if t["tag"] < min_t:
min_t = t["tag"]
f["key"] = n
assert c == 2, "Invalid map definition: %s, must only have two fields" % tmp["name"]
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"])
Expand All @@ -118,7 +121,10 @@ def packtype(name, t, alltypes):
def packproto(name, p, alltypes) -> bytes:
if "request" in p:
request = alltypes[p["request"]]
assert request != None, "Protocol %s request types not found" % (name, p["request"])
assert request != None, "Protocol %s request types not found" % (
name,
p["request"],
)
request = request["id"]

tmp = BytesIO()
Expand Down Expand Up @@ -161,13 +167,17 @@ def packgroup(t, p) -> bytes:
for idx, name in enumerate(alltype_names):
fields = {}
for type_fields in t[name]:
if type_fields["typename"] in sprotoparser.builtin_types: # todo add key too nested
if (
type_fields["typename"] in sprotoparser.builtin_types
): # todo add key too nested
fields[type_fields["name"]] = type_fields["tag"]
alltypes[name] = {"id": idx, "fields": fields}

tt = BytesIO()
for name in alltype_names:
tt.write(packtype(name, t[name], alltypes)) # alltypes["Person"]["fields"]["key"]
tt.write(
packtype(name, t[name], alltypes)
) # alltypes["Person"]["fields"]["key"]

tt = packbytes(tt.getvalue())
if p:
Expand Down Expand Up @@ -215,8 +225,16 @@ def dump(build, outfile):
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--directory", dest="src_dir", help="sproto source files")
parser.add_argument("-f", "--file", dest="src_file", help="sproto single file")
parser.add_argument("-o", "--out", dest="outfile", default="sproto.spb", help="specific dump binary file")
parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", help="show more info")
parser.add_argument(
"-o",
"--out",
dest="outfile",
default="sproto.spb",
help="specific dump binary file",
)
parser.add_argument(
"-v", "--verbose", dest="verbose", action="store_true", help="show more info"
)
args = parser.parse_args()

build = None
Expand Down
66 changes: 52 additions & 14 deletions pysproto/sprotoparser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function
from __future__ import print_function, unicode_literals

from pypeg2 import *

pypeg2_parse = parse # rename it,for avoiding name conflict
Expand All @@ -21,8 +22,16 @@ class TypeName(object):


class Filed(List):
grammar = attr("filed", word), attr("tag", tag), ":", attr("typename", TypeName), \
optional(Decimal), optional(MainKey), nomeaning, endl
grammar = (
attr("filed", word),
attr("tag", tag),
":",
attr("typename", TypeName),
optional(Decimal),
optional(MainKey),
nomeaning,
endl,
)


class Struct(List):
Expand All @@ -42,12 +51,24 @@ class Sub_pro_type(Keyword):


class Subprotocol(List):
grammar = attr("subpro_type", Sub_pro_type), attr("pro_filed", [TypeName, Struct]), nomeaning
grammar = (
attr("subpro_type", Sub_pro_type),
attr("pro_filed", [TypeName, Struct]),
nomeaning,
)


class Protocol(List):
grammar = nomeaning, attr("name", word), attr("tag", tag), "{", nomeaning, attr("fileds", maybe_some(
Subprotocol)), "}", nomeaning
grammar = (
nomeaning,
attr("name", word),
attr("tag", tag),
"{",
nomeaning,
attr("fileds", maybe_some(Subprotocol)),
"}",
nomeaning,
)


class Sproto(List):
Expand All @@ -56,7 +77,13 @@ class Sproto(List):

# ====================================================================

builtin_types = {"integer": 0, "boolean": 1, "string": 2, "double": 3, "binary": 2} # add double and binary
builtin_types = {
"integer": 0,
"boolean": 1,
"string": 2,
"double": 3,
"binary": 2,
} # add double and binary

import re as rawre

Expand Down Expand Up @@ -161,12 +188,17 @@ def convert_protocol(obj):
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" % (obj.name, fi.subpro_type)
newtype_name = str(''.join(fi.pro_filed.fullname))
assert fi.pro_filed.is_arr == False, "syntax error at %s.%s" % (
obj.name,
fi.subpro_type,
)
newtype_name = str("".join(fi.pro_filed.fullname))
protocol[fi.subpro_type] = newtype_name
elif type(fi.pro_filed) == Struct:
newtype_name = obj.name + "." + fi.subpro_type
Convert.type_dict[newtype_name] = Convert.convert_struct(fi.pro_filed, newtype_name)
Convert.type_dict[newtype_name] = Convert.convert_struct(
fi.pro_filed, newtype_name
)
protocol[fi.subpro_type] = newtype_name

Convert.protocol_dict[obj.name] = protocol
Expand Down Expand Up @@ -200,14 +232,20 @@ def parse_list(sproto_list):

# merge type
for stname, stype in ast["type"].iteritems():
assert stname not in build["type"], "redifine type %s in %s" % (stname, v[1])
assert stname not in build["type"], "redifine type %s in %s" % (
stname,
v[1],
)
build["type"][stname] = stype
# merge protocol
for spname, sp in ast["protocol"].iteritems():
assert spname not in build["protocol"], "redifine protocol name %s in %s" % (spname, v[1])
assert (
spname not in build["protocol"]
), "redifine protocol name %s in %s" % (spname, v[1])
for proto in build["protocol"]:
assert sp["tag"] != build["protocol"][proto]["tag"], "redifine protocol tag %d in %s with %s" % (
sp["tag"], proto, spname)
assert (
sp["tag"] != build["protocol"][proto]["tag"]
), "redifine protocol tag %d in %s with %s" % (sp["tag"], proto, spname)
build["protocol"][spname] = sp

flattypename(build)
Expand Down
50 changes: 30 additions & 20 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
from collections import defaultdict

from Cython.Build import cythonize
from setuptools import Extension, setup, find_packages
from setuptools import Extension, find_packages, setup
from setuptools.command.build_ext import build_ext

BUILD_ARGS = defaultdict(lambda: ['-O3', '-g0'])
BUILD_ARGS = defaultdict(lambda: ["-O3", "-g0"])

for compiler, args in [
('msvc', ['/EHsc', '/DHUNSPELL_STATIC', "/Oi", "/O2", "/Ot"]),
('gcc', ['-O3', '-g0'])]:
("msvc", ["/EHsc", "/DHUNSPELL_STATIC", "/Oi", "/O2", "/Ot"]),
("gcc", ["-O3", "-g0"]),
]:
BUILD_ARGS[compiler] = args


Expand All @@ -25,10 +26,12 @@ def build_extensions(self):


extensions = [
Extension("pysproto._sproto", ["pysproto/_sproto.pyx", f'sproto/sproto.c'],
include_dirs=[f"./sproto"],
library_dirs=[f"./sproto"],
),
Extension(
"pysproto._sproto",
["pysproto/_sproto.pyx", f"sproto/sproto.c"],
include_dirs=[f"./sproto"],
library_dirs=[f"./sproto"],
),
]


Expand All @@ -38,14 +41,16 @@ def get_dis():


def get_version() -> str:
path = os.path.join(os.path.abspath(os.path.dirname(__file__)), "pysproto", "__init__.py")
path = os.path.join(
os.path.abspath(os.path.dirname(__file__)), "pysproto", "__init__.py"
)
with open(path, "r", encoding="utf-8") as f:
data = f.read()
result = re.findall(r"(?<=__version__ = \")\S+(?=\")", data)
return result[0]


packages = find_packages(exclude=('test', 'tests.*', "test*"))
packages = find_packages(exclude=("test", "tests.*", "test*"))


def main():
Expand All @@ -63,12 +68,13 @@ def main():
author="synodriver",
author_email="[email protected]",
python_requires=">=3.6",
install_requires=["cython"],
license='BSD',
setup_requires=["cython"],
install_requires=["pypeg2"],
license="BSD",
classifiers=[
"Development Status :: 4 - Beta",
"Operating System :: OS Independent",
'License :: OSI Approved :: BSD License',
"License :: OSI Approved :: BSD License",
"Programming Language :: C",
"Programming Language :: Cython",
"Programming Language :: Python",
Expand All @@ -77,16 +83,20 @@ def main():
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: Implementation :: CPython"
"Programming Language :: Python :: Implementation :: CPython",
],
include_package_data=True,
zip_safe=False,
cmdclass={'build_ext': build_ext_compiler_check},
ext_modules=cythonize(extensions,
compiler_directives={"cdivision": True,
"embedsignature": True,
"boundscheck": False,
"wraparound": False}),
cmdclass={"build_ext": build_ext_compiler_check},
ext_modules=cythonize(
extensions,
compiler_directives={
"cdivision": True,
"embedsignature": True,
"boundscheck": False,
"wraparound": False,
},
),
)


Expand Down
Loading

0 comments on commit 5162600

Please sign in to comment.