-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from unmade/devel
Initial version
- Loading branch information
Showing
45 changed files
with
1,102 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ source = | |
parallel = true | ||
|
||
[report] | ||
fail_under = 100 | ||
show_missing = true | ||
precision = 2 | ||
omit = *__main__* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[flake8] | ||
max-line-length = 88 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
tests/stubs/actual | ||
|
||
*.egg-info | ||
pip-wheel-metadata | ||
dist | ||
|
||
.tox | ||
|
||
__pycache__ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Example app | ||
|
||
This example app shows how to use [generated .pyi files](app/interfaces) | ||
in order to have correct autocomplete and type checking for dynamically loaded thrift interfaces | ||
|
||
## Overview | ||
|
||
Stubs were created with command: | ||
|
||
thriftpyi example/interfaces --output example/app/interfaces | ||
|
||
Note, that [\_\_init__.py](app/interfaces/__init__.py) was created by hand and not by the script. | ||
This file is responsible for the actual access to thrift interfaces. | ||
|
||
## Consideration | ||
|
||
Normally you must do import like this: | ||
```python | ||
from example.app import interfaces | ||
``` | ||
|
||
Imports like this will no work: | ||
```python | ||
# DANGER!!! THIS WILL NOT WORK | ||
from example.app.interfaces.todo import Todo | ||
``` | ||
|
||
Although, if you still want do import things like shown above you can do it like this: | ||
```python | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from example.app.interfaces.todo import Todo | ||
``` |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
from example.app import interfaces | ||
from thriftpy2.rpc import make_client | ||
|
||
if TYPE_CHECKING: | ||
from example.app.interfaces.todo import Todo | ||
|
||
|
||
if __name__ == "__main__": | ||
client: "Todo" = make_client(interfaces.todo.Todo, "127.0.0.1", 6000) | ||
|
||
todo_id = client.create(text="item", type=interfaces.todo.TodoType.NOTE) | ||
print(f"CREATE = {todo_id}") | ||
print(f"GET = {client.get(id=todo_id)}") | ||
try: | ||
client.get(id=todo_id + 1) | ||
except interfaces.shared.NotFound: | ||
print(f"NOT FOUND") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from pathlib import Path | ||
from types import ModuleType | ||
from typing import Dict | ||
|
||
import thriftpy2 | ||
|
||
_interfaces_path = Path("example/interfaces") | ||
_interfaces: Dict[str, ModuleType] = {} | ||
|
||
|
||
def __getattr__(name): | ||
try: | ||
return _interfaces[name] | ||
except KeyError: | ||
interface = thriftpy2.load(str(_interfaces_path.joinpath(f"{name}.thrift"))) | ||
_interfaces[name] = interface | ||
return interface |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from . import dates | ||
from . import shared | ||
from . import todo | ||
from . import todo_v2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from dataclasses import dataclass | ||
from typing import * | ||
@dataclass | ||
class DateTime: | ||
year: int | ||
month: int | ||
day: int | ||
hour: int | ||
minute: int | ||
second: int | ||
microsecond: Optional[int] = None | ||
|
||
@dataclass | ||
class Date: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from dataclasses import dataclass | ||
from typing import * | ||
|
||
class NotFound(Exception): | ||
message: Optional[str] = None | ||
|
||
@dataclass | ||
class LimitOffset: | ||
limit: Optional[int] = None | ||
offset: Optional[int] = None | ||
|
||
class Service: | ||
def ping(self,) -> str: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from dataclasses import dataclass | ||
from enum import IntEnum | ||
from typing import * | ||
|
||
from . import shared | ||
from . import dates | ||
|
||
class TodoType(IntEnum): | ||
PLAIN = 1 | ||
NOTE = 2 | ||
CHECKBOXES = 3 | ||
|
||
@dataclass | ||
class TodoItem: | ||
id: int | ||
text: str | ||
type: int | ||
created: dates.DateTime | ||
is_deleted: bool | ||
picture: Optional[str] = None | ||
|
||
class Todo: | ||
def create(self, text: str, type: int) -> int: ... | ||
def update(self, id: int, text: str, type: int) -> None: ... | ||
def get(self, id: int) -> TodoItem: ... | ||
def all(self, pager: shared.LimitOffset) -> List[TodoItem]: ... | ||
def filter(self, ids: List[int]) -> List[TodoItem]: ... | ||
def stats(self,) -> Dict[int, float]: ... | ||
def types(self,) -> Set[int]: ... | ||
def groupby(self,) -> Dict[int, List[TodoItem]]: ... | ||
def ping(self,) -> str: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class Todo: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import datetime | ||
from typing import TYPE_CHECKING, Dict | ||
|
||
from example.app import interfaces | ||
from thriftpy2.rpc import make_server | ||
|
||
if TYPE_CHECKING: | ||
from example.app.interfaces.todo import TodoItem | ||
|
||
todos: Dict[int, "TodoItem"] = {} | ||
|
||
|
||
class Dispatcher(object): | ||
def create(self, text: str, type: int) -> int: | ||
todo_id = max(todos.keys() or [0]) + 1 | ||
created = datetime.datetime.now() | ||
todos[todo_id] = interfaces.todo.TodoItem( | ||
id=todo_id, | ||
text=text, | ||
type=type, | ||
created=interfaces.dates.DateTime( | ||
year=created.year, | ||
month=created.month, | ||
day=created.day, | ||
hour=created.hour, | ||
minute=created.minute, | ||
second=created.second, | ||
), | ||
is_deleted=False, | ||
) | ||
return todo_id | ||
|
||
def get(self, id: int) -> "TodoItem": | ||
try: | ||
return todos[id] | ||
except KeyError: | ||
raise interfaces.shared.NotFound | ||
|
||
def ping(self): | ||
return "pong" | ||
|
||
|
||
if __name__ == "__main__": | ||
server = make_server(interfaces.todo.Todo, Dispatcher(), "127.0.0.1", 6000) | ||
server.serve() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace * dates | ||
|
||
struct DateTime { | ||
1: required i16 year, | ||
2: required byte month, | ||
3: required byte day | ||
4: required i16 hour, | ||
5: required byte minute, | ||
6: required byte second, | ||
7: optional i64 microsecond | ||
} | ||
|
||
struct Date { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
exception NotFound { | ||
1: optional string message | ||
} | ||
|
||
|
||
struct LimitOffset { | ||
1: optional i32 limit | ||
2: optional i32 offset | ||
} | ||
|
||
|
||
service Service { | ||
string ping(); | ||
} |
Oops, something went wrong.