-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:jina-ai/jina
- Loading branch information
Showing
14 changed files
with
470 additions
and
6 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
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 |
---|---|---|
|
@@ -73,4 +73,4 @@ pytest-custom_exit_code: cicd, test | |
bs4: test | ||
aiostream: devel, cicd | ||
click: cicd | ||
|
||
jsonschema: cicd |
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,41 @@ | ||
def get_full_schema() -> dict: | ||
""" | ||
Return the full schema for Jina core as a dict. | ||
""" | ||
from .. import __version__ | ||
from ..importer import IMPORTED | ||
from .driver import schema_all_drivers | ||
from .executor import schema_all_executors | ||
from .flow import schema_flow | ||
from .meta import schema_metas | ||
from .request import schema_requests | ||
from .pod import schema_pod | ||
|
||
definitions = {} | ||
for s in [ | ||
schema_all_drivers, | ||
schema_all_executors, | ||
schema_flow, | ||
schema_metas, | ||
schema_requests, | ||
schema_pod, | ||
IMPORTED.schema_executors, | ||
IMPORTED.schema_drivers | ||
]: | ||
definitions.update(s) | ||
|
||
# fix CompoundExecutor | ||
definitions['Jina::Executors::CompoundExecutor']['properties']['components'] = { | ||
'$ref': '#/definitions/Jina::Executors::All' | ||
} | ||
|
||
return { | ||
'$id': f'https://api.jina.ai/schemas/{__version__}.json', | ||
'$schema': 'http://json-schema.org/draft-07/schema#', | ||
'description': 'The YAML schema of Jina objects (Flow, Executor, Drivers).', | ||
'type': 'object', | ||
'oneOf': | ||
[{'$ref': '#/definitions/Jina::Flow'}] + | ||
[{"$ref": f"#/definitions/{k}"} for k in IMPORTED.schema_executors.keys()], | ||
'definitions': definitions | ||
} |
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 ..importer import IMPORTED | ||
|
||
schema_all_drivers = { | ||
'Jina::Drivers::All': { | ||
'type': 'array', | ||
'items': { | ||
'oneOf': [ | ||
{'$ref': f'#/definitions/{k}'} for k in IMPORTED.schema_drivers.keys() | ||
] | ||
}, | ||
'minItems': 1 | ||
} | ||
} |
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 ..importer import IMPORTED | ||
|
||
schema_all_executors = { | ||
'Jina::Executors::All': { | ||
'type': 'array', | ||
'items': { | ||
'oneOf': [ | ||
{'$ref': f'#/definitions/{k}'} for k in IMPORTED.schema_executors.keys() | ||
] | ||
}, | ||
'minItems': 1 | ||
} | ||
} |
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,32 @@ | ||
from cli.export import api_to_dict | ||
from jina.schemas.helper import _cli_to_schema | ||
|
||
schema_flow = _cli_to_schema( | ||
api_to_dict(), | ||
'flow', | ||
extras={ | ||
'jtype': { | ||
'description': 'The type of Jina object (Flow, Executor, Driver).\n' | ||
'A Flow is made up of several sub-tasks, and it manages the states and context of these sub-tasks.\n' | ||
'The input and output data of Flows are Documents.', | ||
'type': 'string', | ||
'default': 'Flow', | ||
'enum': ['Flow', 'AsyncFlow'] | ||
}, | ||
'version': { | ||
'description': 'The YAML version of this Flow.', | ||
'type': 'string', | ||
'default': '\'1\'', | ||
}, | ||
'pods': { | ||
'description': 'Define the steps in the Flow.\n' | ||
'A Pod is a container and interface for one or multiple Peas that have the same properties.', | ||
'type': 'array', | ||
'items': { | ||
'$ref': '#/definitions/Jina::Pod' | ||
}, | ||
'minItems': 1 | ||
} | ||
}, | ||
allow_addition=False, | ||
required=['jtype', 'version', 'pods']) |
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,162 @@ | ||
import inspect | ||
import re | ||
import typing | ||
from functools import reduce | ||
|
||
|
||
def _python_type_to_schema_type(p): | ||
if p == 'str': | ||
dtype = 'string' | ||
elif p == 'int' or p == 'float': | ||
dtype = 'number' | ||
elif p in {'typing.List[str]', 'typing.Tuple[str]', 'list', 'tuple'}: | ||
dtype = 'array' | ||
elif p == 'bool': | ||
dtype = 'boolean' | ||
elif p == 'dict': | ||
dtype = 'object' | ||
else: | ||
dtype = None | ||
# raise TypeError(f'{p} is not supported') | ||
|
||
return dtype | ||
|
||
|
||
def _cli_to_schema(api_dict, target, | ||
extras=None, | ||
required=None, | ||
allow_addition=False, | ||
namespace='Jina'): | ||
pod_api = None | ||
|
||
for d in api_dict['methods']: | ||
if d['name'] == target: | ||
pod_api = d['options'] | ||
break | ||
|
||
_schema = { | ||
'properties': {}, | ||
'type': 'object', | ||
'required': [], | ||
'additionalProperties': allow_addition} | ||
|
||
for p in pod_api: | ||
dtype = _python_type_to_schema_type(p['type']) | ||
pv = { | ||
'description': p['help'].strip(), | ||
'type': dtype, | ||
'default': p['default'] | ||
} | ||
if p['choices']: | ||
pv['enum'] = p['choices'] | ||
if p['required']: | ||
_schema['required'].append(p['name']) | ||
if dtype == 'array': | ||
_schema['items'] = { | ||
'type': 'string', | ||
'minItems': 1, | ||
'uniqueItems': True | ||
} | ||
|
||
_schema['properties'][p['name']] = pv | ||
|
||
if extras: | ||
_schema['properties'].update(extras) | ||
if required: | ||
_schema['required'].extend(required) | ||
|
||
return { | ||
f'{namespace}::{target.capitalize()}': _schema | ||
} | ||
|
||
|
||
def _get_all_arguments(class_): | ||
def get_class_arguments(class_): | ||
""" | ||
:param class_: the class to check | ||
:return: a list containing the arguments from `class_` | ||
""" | ||
taboo = {'self', 'args', 'kwargs'} | ||
signature = inspect.signature(class_.__init__) | ||
|
||
reg = r'.*?:param.*?%s:(.*)' | ||
|
||
class_arguments = {} | ||
for p in signature.parameters.values(): | ||
if p.name in taboo: | ||
continue | ||
class_arguments[p.name] = {} | ||
if p.default != inspect._empty: | ||
class_arguments[p.name]['default'] = p.default | ||
else: | ||
class_arguments[p.name]['default'] = None | ||
if p.annotation != inspect._empty: | ||
dtype = None | ||
try: | ||
if hasattr(p.annotation, '__origin__') and p.annotation.__origin__ is typing.Union: | ||
dtype = p.annotation.__args__[0].__name__ | ||
else: | ||
dtype = getattr(p.annotation, '__origin__', p.annotation).__name__ | ||
except: | ||
pass | ||
dtype = _python_type_to_schema_type(dtype) | ||
if dtype: | ||
class_arguments[p.name]['type'] = dtype | ||
|
||
if class_.__init__.__doc__: | ||
m = re.search(reg % p.name, class_.__init__.__doc__) | ||
if m and m.group(1): | ||
class_arguments[p.name]['description'] = m.group(1).strip() | ||
|
||
return class_arguments | ||
|
||
def accumulate_classes(cls): | ||
""" | ||
:param cls: the class to check | ||
:return: all classes from which cls inherits from | ||
""" | ||
|
||
def _accumulate_classes(c, cs): | ||
cs.append(c) | ||
if cls == object: | ||
return cs | ||
for base in c.__bases__: | ||
_accumulate_classes(base, cs) | ||
return cs | ||
|
||
classes = [] | ||
_accumulate_classes(cls, classes) | ||
return set(classes) | ||
|
||
all_classes = accumulate_classes(class_) | ||
args = list(map(lambda x: get_class_arguments(x), all_classes)) | ||
return reduce(lambda x, y: {**x, **y}, args) | ||
|
||
|
||
def _jina_class_to_schema(cls): | ||
kwargs = _get_all_arguments(cls) | ||
|
||
return { | ||
'type': 'object', | ||
'description': cls.__doc__.strip() if cls.__doc__ else '', | ||
'properties': { | ||
'jtype': { | ||
'type': 'string', | ||
'const': cls.__name__, | ||
'description': cls.__doc__.strip().split('\n')[0] if cls.__doc__ else '' | ||
}, | ||
'with': { | ||
'type': 'object', | ||
'description': 'The arguments of this Jina Executor/Driver', | ||
'properties': kwargs, | ||
'additionalProperties': False | ||
}, | ||
'metas': { | ||
'$ref': '#/definitions/Jina::Metas' | ||
}, | ||
'requests': { | ||
'$ref': '#/definitions/Jina::Requests' | ||
} | ||
}, | ||
'additionalProperties': False, | ||
} |
Oops, something went wrong.