Skip to content

Commit

Permalink
py lint
Browse files Browse the repository at this point in the history
  • Loading branch information
alakendu committed Sep 27, 2022
1 parent 2b623d9 commit 16fea70
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
18 changes: 12 additions & 6 deletions openapiart/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,23 @@
except ImportError:
from typing_extensions import Literal

#grpc-begin
# grpc-begin
import grpc
from google.protobuf import json_format
import sanity_pb2_grpc as pb2_grpc
import sanity_pb2 as pb2
#grpc-end

# grpc-end
if sys.version_info[0] == 3:
unicode = str

openapi_warnings = []
#grpc-begin
# grpc-begin
class Transport:
HTTP = "http"
GRPC = "grpc"


def api(
location=None,
transport=None,
Expand Down Expand Up @@ -87,8 +89,10 @@ class send_recv method to communicate with a REST based server.
except ImportError as err:
msg = "Extension %s is not installed or invalid: %s"
raise Exception(msg % (ext, err))
#grpc-end
#http-begin


# grpc-end
# http-begin
def api(
location=None,
verify=True,
Expand Down Expand Up @@ -132,7 +136,9 @@ class send_recv method to communicate with a REST based server.
except ImportError as err:
msg = "Extension %s is not installed or invalid: %s"
raise Exception(msg % (ext, err))
#http-end


# http-end
class HttpTransport(object):
def __init__(self, **kwargs):
"""Use args from api() method to instantiate an HTTP transport"""
Expand Down
26 changes: 15 additions & 11 deletions openapiart/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(
protobuf_package_name,
output_dir=None,
extension_prefix=None,
py_generate_grpc=True
py_generate_grpc=True,
):
self._parsers = {}
self._base_url = ""
Expand Down Expand Up @@ -145,28 +145,32 @@ def generate(self):
) as fp:
common_content = fp.read()
if self._py_generate_grpc:
grpc_chop = re.compile('#grpc-begin|#grpc-end', re.DOTALL)
common_content = grpc_chop.sub('', common_content)
http_chop = re.compile('#http-begin.*?#http-end', re.DOTALL)
common_content = http_chop.sub('', common_content)
grpc_chop = re.compile("# grpc-begin|# grpc-end", re.DOTALL)
common_content = grpc_chop.sub("", common_content)
http_chop = re.compile("# http-begin.*?# http-end", re.DOTALL)
common_content = http_chop.sub("", common_content)
cnf_text = "import sanity_pb2_grpc as pb2_grpc"
modify_text = "try:\n from {pkg_name} {text}\nexcept ImportError:\n {text}".format(
pkg_name=self._package_name,
text=cnf_text.replace("sanity", self._protobuf_package_name),
text=cnf_text.replace(
"sanity", self._protobuf_package_name
),
)
common_content = common_content.replace(cnf_text, modify_text)

cnf_text = "import sanity_pb2 as pb2"
modify_text = "try:\n from {pkg_name} {text}\nexcept ImportError:\n {text}".format(
pkg_name=self._package_name,
text=cnf_text.replace("sanity", self._protobuf_package_name),
text=cnf_text.replace(
"sanity", self._protobuf_package_name
),
)
common_content = common_content.replace(cnf_text, modify_text)
else:
grpc_chop = re.compile('#grpc-begin.*?#grpc-end', re.DOTALL)
common_content = grpc_chop.sub('', common_content)
http_chop = re.compile('#http-begin|#http-end', re.DOTALL)
common_content = http_chop.sub('', common_content)
grpc_chop = re.compile("# grpc-begin.*?# grpc-end", re.DOTALL)
common_content = grpc_chop.sub("", common_content)
http_chop = re.compile("# http-begin|# http-end", re.DOTALL)
common_content = http_chop.sub("", common_content)

if re.search(r"def[\s+]api\(", common_content) is not None:
self._generated_top_level_factories.append("api")
Expand Down

0 comments on commit 16fea70

Please sign in to comment.