8000 WIP - Feature/custom codec by ekampf · Pull Request #16 · graphql-python/gql-next · GitHub
[go: up one dir, main page]

Skip to content

WIP - Feature/custom codec #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
4cab090
Fix error message
ekampf Jan 2, 2019
34536d2
Initial codec implementation
ekampf Jan 2, 2019
d815eeb
Add pth file to auto register the codec
ekampf Jan 2, 2019
1be6b75
reorg code
ekampf Jan 2, 2019
0d44000
Wrap queries in their own namespace
ekampf Jan 2, 2019
30250c0
Refactor
ekampf Jan 2, 2019
aa987b9
Sync client shouldnt be async
ekampf Jan 2, 2019
5b46ae9
Codec example
ekampf Jan 2, 2019
14b7a6f
Merge branch 'master' into feature/custom_codec
ekampf Jan 2, 2019
0623997
Merge branch 'master' into feature/custom_codec
ekampf Jan 2, 2019
bd8029e
Added encpoint to example
ekampf Jan 2, 2019
09156f8
Fix scripts section in project
ekampf Jan 3, 2019
2d0dd2e
Merge branch 'master' into feature/custom_codec
ekampf Jan 3, 2019
1746bdb
lint
ekampf Jan 3, 2019
ac6f8de
Merge branch 'master' into feature/custom_codec
ekampf Jan 3, 2019
221a87a
Fix tests
ekampf Jan 3, 2019
ce546d1
nicer code
ekampf Jan 3, 2019
0d0f6d4
Unnecessary
ekampf Jan 3, 2019
995c2b5
TODOs
ekampf Jan 3, 2019
a6aa951
Merge branch 'master' into feature/custom_codec
ekampf Jan 3, 2019
6d80401
Missing space
ekampf Jan 7, 2019
cab9b6b
Codec synonyms
ekampf Jan 7, 2019
0713f64
Fix usage text
ekampf Jan 7, 2019
c125c2e
More tests
ekampf Jan 8, 2019
2896860
faster register function
ekampf Feb 6, 2019
f7c7d0d
Updated dataclasses-json
ekampf Feb 6, 2019
f056aaa
Support “”” string literals too
ekampf Feb 6, 2019
f827c6b
Errors should be optional
ekampf Feb 6, 2019
52229dd
Ignore .venv
ekampf Feb 6, 2019
81686c6
Simple example showing embedded query
ekampf Feb 6, 2019
6482195
poetry update
ekampf Feb 6, 2019
56f55b8
pylint needs to import codec to work
ekampf Feb 6, 2019
402bf32
nicer code
ekampf Feb 6, 2019
fddcff2
ending newline
ekampf Feb 6, 2019
db3afb2
lint bootstrap.py
ekampf Feb 6, 2019
6787a86
Wrap autogenerated code with comments
ekampf Feb 7, 2019
c342f04
lint
ekampf Feb 7, 2019
c29268c
Decode works
ekampf Feb 7, 2019
eeacb51
fixing gql_decode
ekampf Feb 7, 2019
5885b9a
Fixed example
ekampf Feb 7, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
lint
  • Loading branch information
ekampf committed Jan 3, 2019
commit 1746bdb737c9ccbbcb2b7fd3a382e59af3b657a6
6 changes: 3 additions & 3 deletions gql/clients/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def __init__(self, endpoint, headers=None):
}

def call(self, query,
variables=None,
return_json=False,
on_before_callback: Callable[[Mapping[str, str], Mapping[str, str]], None] = None) -> Union[dict, str]:
variables=None,
return_json=False,
on_before_callback: Callable[[Mapping[str, str], Mapping[str, str]], None] = None) -> Union[dict, str]:

headers = self.__headers.copy()

Expand Down
30 changes: 20 additions & 10 deletions gql/codec/register.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
#!/usr/bin/env python
import sys
import codecs
import encodings
import sys
from encodings import utf_8
from io import BytesIO

from gql.codec.transform import gql_transform, gql_transform_string
from gql.codec.transform import gql_transform


def gql_decode(input, errors='strict'):
return utf_8.decode(input)
# return utf_8.decode(gql_transform_string(input), errors)
def gql_decode(value, **_):
return utf_8.decode(value)


class GQLIncrementalDecoder(utf_8.IncrementalDecoder):
def decode(self, input: bytes, final: bool=False):
def decode(self, input: bytes, final: bool = False): # pylint:disable=redefined-builtin
self.buffer += input
if final:
buff = self.buffer
self.buffer = ''
return gql_transform(BytesIO(buff))

return None


class GQLStreamReader(utf_8.StreamReader):
# pylint:disable=abstract-method

def __init__(self, *args, **kwargs):
super().__init__(self, *args, **kwargs)
self.stream = BytesIO(gql_decode(self.stream))
Expand All @@ -41,7 +44,8 @@ def search_function(encoding):
incrementalencoder=utf8.incrementalencoder,
incrementaldecoder=GQLIncrementalDecoder,
streamreader=GQLStreamReader,
streamwriter=utf8.streamwriter)
streamwriter=utf8.streamwriter
)


codecs.register(search_function)
Expand All @@ -55,7 +59,9 @@ def search_function(encoding):
python -m gql.codec.register path/to/script.py [args...]
"""

if __name__ == '__main__':

def main():
# pylint:disable=exec-used,redefined-builtin,global-statement
script = None
if len(sys.argv) >= 3 and sys.argv[1] == '-m':
mode = 'module'
Expand All @@ -73,12 +79,16 @@ def search_function(encoding):
import runpy
runpy.run_module(module, run_name='__main__', alter_sys=True)
elif mode == 'script':
with open(script, encoding='gql') as f:
with open(script) as file:
global __file__
__file__ = script
# Use globals as our "locals" dictionary so that something
# that tries to import __main__ (e.g. the unittest module)
# will see the right things.
code = f.read()
code = file.read()
print(code)
exec(code, globals())


if __name__ == '__main__':
main()
4 changes: 2 additions & 2 deletions gql/codec/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def gql_transform(stream: BytesIO):
return result.rstrip()


def gql_transform_string(input: str):
stream = BytesIO(input.encode('utf-8'))
def gql_transform_string(value: str):
stream = BytesIO(value.encode('utf-8'))
return gql_transform(stream)


Expand Down
3 changes: 2 additions & 1 deletion gql/renderer_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def render(self, parsed_query: ParsedQuery):

return str(buffer)

def get_operation_class_name(self, parsed_query: ParsedQuery):
@staticmethod
def get_operation_class_name(parsed_query: ParsedQuery):
for obj in parsed_query.objects[::-1]:
if isinstance(obj, ParsedOperation):
return obj.name
Expand Down
0