1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
import pprint
from contextlib import contextmanager
from stone.backend import Backend, CodeBackend
from stone.backends.helpers import (
fmt_pascal,
fmt_underscores,
)
from stone.ir import (
AnnotationType,
Boolean,
Bytes,
Float32,
Float64,
Int32,
Int64,
List,
String,
Timestamp,
UInt32,
UInt64,
is_user_defined_type,
is_alias,
)
from stone.ir import ApiNamespace
_MYPY = False
if _MYPY:
import typing # noqa: F401 # pylint: disable=import-error,unused-import,useless-suppression
_type_table = {
Boolean: 'bool',
Bytes: 'bytes',
Float32: 'float',
Float64: 'float',
Int32: 'int',
Int64: 'int',
List: 'list',
String: 'str',
Timestamp: 'datetime',
UInt32: 'int',
UInt64: 'int',
}
_reserved_keywords = {
'break',
'class',
'continue',
'for',
'pass',
'while',
'async',
}
@contextmanager
def emit_pass_if_nothing_emitted(codegen):
# type: (CodeBackend) -> typing.Iterator[None]
starting_lineno = codegen.lineno
yield
ending_lineno = codegen.lineno
if starting_lineno == ending_lineno:
codegen.emit("pass")
codegen.emit()
def _rename_if_reserved(s):
if s in _reserved_keywords:
return s + '_'
else:
return s
def fmt_class(name, check_reserved=False):
s = fmt_pascal(name)
return _rename_if_reserved(s) if check_reserved else s
def fmt_func(name, check_reserved=False, version=1):
name = fmt_underscores(name)
if check_reserved:
name = _rename_if_reserved(name)
if version > 1:
name = '{}_v{}'.format(name, version)
return name
def fmt_obj(o):
return pprint.pformat(o, width=1)
def fmt_type(data_type):
return _type_table.get(data_type.__class__, fmt_class(data_type.name))
def fmt_var(name, check_reserved=False):
s = fmt_underscores(name)
return _rename_if_reserved(s) if check_reserved else s
def fmt_namespaced_var(ns_name, data_type_name, field_name):
return ".".join([ns_name, data_type_name, fmt_var(field_name)])
def fmt_namespace(name):
return _rename_if_reserved(name)
def check_route_name_conflict(namespace):
"""
Check name conflicts among generated route definitions. Raise a runtime exception when a
conflict is encountered.
"""
route_by_name = {}
for route in namespace.routes:
route_name = fmt_func(route.name, version=route.version)
if route_name in route_by_name:
other_route = route_by_name[route_name]
raise RuntimeError(
'There is a name conflict between {!r} and {!r}'.format(other_route, route))
route_by_name[route_name] = route
TYPE_IGNORE_COMMENT = " # type: ignore"
def generate_imports_for_referenced_namespaces(
backend, namespace, package, insert_type_ignore=False):
# type: (Backend, ApiNamespace, typing.Text, bool) -> None
"""
Both the true Python backend and the Python PEP 484 Type Stub backend have
to perform the same imports.
:param insert_type_ignore: add a MyPy type-ignore comment to the imports in
the except: clause.
"""
imported_namespaces = namespace.get_imported_namespaces(consider_annotation_types=True)
if not imported_namespaces:
return
type_ignore_comment = TYPE_IGNORE_COMMENT if insert_type_ignore else ""
for ns in imported_namespaces:
backend.emit('from {package} import {namespace_name}{type_ignore_comment}'.format(
package=package,
namespace_name=fmt_namespace(ns.name),
type_ignore_comment=type_ignore_comment
))
backend.emit()
def generate_module_header(backend):
backend.emit('# -*- coding: utf-8 -*-')
backend.emit('# Auto-generated by Stone, do not modify.')
# Silly way to not type ATgenerated in our code to avoid having this
# file marked as auto-generated by our code review tool.
backend.emit('# @{}'.format('generated'))
backend.emit('# flake8: noqa')
backend.emit('# pylint: skip-file')
# This will be at the top of every generated file.
_validators_import_template = """\
from stone.backends.python_rsrc import stone_base as bb{type_ignore_comment}
from stone.backends.python_rsrc import stone_validators as bv{type_ignore_comment}
"""
validators_import = _validators_import_template.format(type_ignore_comment="")
validators_import_with_type_ignore = _validators_import_template.format(
type_ignore_comment=TYPE_IGNORE_COMMENT
)
def prefix_with_ns_if_necessary(name, name_ns, source_ns):
# type: (typing.Text, ApiNamespace, ApiNamespace) -> typing.Text
"""
Returns a name that can be used to reference `name` in namespace `name_ns`
from `source_ns`.
If `source_ns` and `name_ns` are the same, that's just `name`. Otherwise
it's `name_ns`.`name`.
"""
if source_ns == name_ns:
return name
return '{}.{}'.format(fmt_namespace(name_ns.name), name)
def class_name_for_data_type(data_type, ns=None):
"""
Returns the name of the Python class that maps to a user-defined type.
The name is identical to the name in the spec.
If ``ns`` is set to a Namespace and the namespace of `data_type` does
not match, then a namespace prefix is added to the returned name.
For example, ``foreign_ns.TypeName``.
"""
assert is_user_defined_type(data_type) or is_alias(data_type), \
'Expected composite type, got %r' % type(data_type)
name = fmt_class(data_type.name)
if ns:
return prefix_with_ns_if_necessary(name, data_type.namespace, ns)
return name
def class_name_for_annotation_type(annotation_type, ns=None):
"""
Same as class_name_for_data_type, but works with annotation types.
"""
assert isinstance(annotation_type, AnnotationType)
name = fmt_class(annotation_type.name)
if ns:
return prefix_with_ns_if_necessary(name, annotation_type.namespace, ns)
return name
|