Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 5 additions & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Change Log

### 2020-xx-xx - 5.5.1
### 2020-xx-xx - 5.6.0
Autorest core version: 3.0.6318

Modelerfour version: 4.15.421

**New Features**

- Add support for [`black`](https://pypi.org/project/black/) formatting of your generated files. Pass flag `--black` when generating to get this behavior. #836

**Bug Fixes**

- Wrap individual enum descriptions #844
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,24 @@ scope-multiapiscript/emitter:

output-artifact: python-files
```

# Black script pipeline

``` yaml $(black)

pipeline:
python/black:
scope: black
input: python/codegen
output-artifact: python-files

python/black/emitter:
input: black
scope: scope-black/emitter

scope-black/emitter:
input-artifact: python-files
output-uri-expr: $key

output-artifact: python-files
```
38 changes: 38 additions & 0 deletions autorest/black/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import logging
from pathlib import Path
import black

from .. import Plugin

_LOGGER = logging.getLogger(__name__)

_BLACK_MODE = black.Mode()
_BLACK_MODE.line_length = 120

class BlackScriptPlugin(Plugin):

def __init__(self, autorestapi):
super(BlackScriptPlugin, self).__init__(autorestapi)
self.output_folder: Path = Path(self._autorestapi.get_value("output-folder")).resolve()

def process(self) -> bool:
# apply format_file on every file in the output folder
list(map(self.format_file, [f for f in self.output_folder.glob('**/*') if f.is_file()]))
return True

def format_file(self, full_path) -> None:
file = full_path.relative_to(self.output_folder)
file_content = self._autorestapi.read_file(file)
if not file.suffix == ".py":
self._autorestapi.write_file(file, file_content)
return
try:
file_content = black.format_file_contents(file_content, fast=True, mode=_BLACK_MODE)
except black.NothingChanged:
pass
self._autorestapi.write_file(file, file_content)
4 changes: 3 additions & 1 deletion autorest/jsonrpc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@dispatcher.add_method
def GetPluginNames():
return ["codegen", "m2r", "namer", "multiapiscript"]
return ["codegen", "m2r", "namer", "black", "multiapiscript"]


@dispatcher.add_method
Expand All @@ -37,6 +37,8 @@ def Process(plugin_name: str, session_id: str) -> bool:
from ..namer import Namer as PluginToLoad # type: ignore
elif plugin_name == "codegen":
from ..codegen import CodeGenerator as PluginToLoad # type: ignore
elif plugin_name == "black":
from ..black import BlackScriptPlugin as PluginToLoad # type: ignore
elif plugin_name == "multiapiscript":
from ..multiapi import MultiApiScriptPlugin as PluginToLoad # type: ignore
else:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@
"Jinja2 >= 2.11", # I need "include" and auto-context + blank line are not indented by default
"pyyaml",
"m2r",
"black",
],
)
3 changes: 2 additions & 1 deletion tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ def _build_flags(
"payload-flattening-threshold": 1,
"keep-version-file": True,
"namespace": _OVERWRITE_DEFAULT_NAMESPACE.get(package_name, package_name.lower()),
"client-side-validation": package_name in _PACKAGES_WITH_CLIENT_SIDE_VALIDATION
"client-side-validation": package_name in _PACKAGES_WITH_CLIENT_SIDE_VALIDATION,
"black": True,
}
if override_flags:
flags.update(override_flags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
from ._version import VERSION

__version__ = VERSION
__all__ = ['AutoRestDurationTestService']
__all__ = ["AutoRestDurationTestService"]

try:
from ._patch import patch_sdk # type: ignore

patch_sdk()
except ImportError:
pass
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(
):
# type: (...) -> None
if not base_url:
base_url = 'http://localhost:3000'
base_url = "http://localhost:3000"
self._config = AutoRestDurationTestServiceConfiguration(**kwargs)
self._client = PipelineClient(base_url=base_url, config=self._config, **kwargs)

Expand All @@ -44,8 +44,7 @@ def __init__(
self._serialize.client_side_validation = False
self._deserialize = Deserializer(client_models)

self.duration = DurationOperations(
self._client, self._config, self._serialize, self._deserialize)
self.duration = DurationOperations(self._client, self._config, self._serialize, self._deserialize)

def close(self):
# type: () -> None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,24 @@ class AutoRestDurationTestServiceConfiguration(Configuration):
"""

def __init__(
self,
**kwargs # type: Any
self, **kwargs # type: Any
):
# type: (...) -> None
super(AutoRestDurationTestServiceConfiguration, self).__init__(**kwargs)

kwargs.setdefault('sdk_moniker', 'autorestdurationtestservice/{}'.format(VERSION))
kwargs.setdefault("sdk_moniker", "autorestdurationtestservice/{}".format(VERSION))
self._configure(**kwargs)

def _configure(
self,
**kwargs # type: Any
self, **kwargs # type: Any
):
# type: (...) -> None
self.user_agent_policy = kwargs.get('user_agent_policy') or policies.UserAgentPolicy(**kwargs)
self.headers_policy = kwargs.get('headers_policy') or policies.HeadersPolicy(**kwargs)
self.proxy_policy = kwargs.get('proxy_policy') or policies.ProxyPolicy(**kwargs)
self.logging_policy = kwargs.get('logging_policy') or policies.NetworkTraceLoggingPolicy(**kwargs)
self.http_logging_policy = kwargs.get('http_logging_policy') or policies.HttpLoggingPolicy(**kwargs)
self.retry_policy = kwargs.get('retry_policy') or policies.RetryPolicy(**kwargs)
self.custom_hook_policy = kwargs.get('custom_hook_policy') or policies.CustomHookPolicy(**kwargs)
self.redirect_policy = kwargs.get('redirect_policy') or policies.RedirectPolicy(**kwargs)
self.authentication_policy = kwargs.get('authentication_policy')
self.user_agent_policy = kwargs.get("user_agent_policy") or policies.UserAgentPolicy(**kwargs)
self.headers_policy = kwargs.get("headers_policy") or policies.HeadersPolicy(**kwargs)
self.proxy_policy = kwargs.get("proxy_policy") or policies.ProxyPolicy(**kwargs)
self.logging_policy = kwargs.get("logging_policy") or policies.NetworkTraceLoggingPolicy(**kwargs)
self.http_logging_policy = kwargs.get("http_logging_policy") or policies.HttpLoggingPolicy(**kwargs)
self.retry_policy = kwargs.get("retry_policy") or policies.RetryPolicy(**kwargs)
self.custom_hook_policy = kwargs.get("custom_hook_policy") or policies.CustomHookPolicy(**kwargs)
self.redirect_policy = kwargs.get("redirect_policy") or policies.RedirectPolicy(**kwargs)
self.authentication_policy = kwargs.get("authentication_policy")
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
# --------------------------------------------------------------------------

from ._auto_rest_duration_test_service import AutoRestDurationTestService
__all__ = ['AutoRestDurationTestService']

__all__ = ["AutoRestDurationTestService"]
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@ class AutoRestDurationTestService(object):
:param str base_url: Service URL
"""

def __init__(
self,
base_url: Optional[str] = None,
**kwargs: Any
) -> None:
def __init__(self, base_url: Optional[str] = None, **kwargs: Any) -> None:
if not base_url:
base_url = 'http://localhost:3000'
base_url = "http://localhost:3000"
self._config = AutoRestDurationTestServiceConfiguration(**kwargs)
self._client = AsyncPipelineClient(base_url=base_url, config=self._config, **kwargs)

Expand All @@ -39,8 +35,7 @@ def __init__(
self._serialize.client_side_validation = False
self._deserialize = Deserializer(client_models)

self.duration = DurationOperations(
self._client, self._config, self._serialize, self._deserialize)
self.duration = DurationOperations(self._client, self._config, self._serialize, self._deserialize)

async def close(self) -> None:
await self._client.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,19 @@ class AutoRestDurationTestServiceConfiguration(Configuration):
attributes.
"""

def __init__(
self,
**kwargs: Any
) -> None:
def __init__(self, **kwargs: Any) -> None:
super(AutoRestDurationTestServiceConfiguration, self).__init__(**kwargs)

kwargs.setdefault('sdk_moniker', 'autorestdurationtestservice/{}'.format(VERSION))
kwargs.setdefault("sdk_moniker", "autorestdurationtestservice/{}".format(VERSION))
self._configure(**kwargs)

def _configure(
self,
**kwargs: Any
) -> None:
self.user_agent_policy = kwargs.get('user_agent_policy') or policies.UserAgentPolicy(**kwargs)
self.headers_policy = kwargs.get('headers_policy') or policies.HeadersPolicy(**kwargs)
self.proxy_policy = kwargs.get('proxy_policy') or policies.ProxyPolicy(**kwargs)
self.logging_policy = kwargs.get('logging_policy') or policies.NetworkTraceLoggingPolicy(**kwargs)
self.http_logging_policy = kwargs.get('http_logging_policy') or policies.HttpLoggingPolicy(**kwargs)
self.retry_policy = kwargs.get('retry_policy') or policies.AsyncRetryPolicy(**kwargs)
self.custom_hook_policy = kwargs.get('custom_hook_policy') or policies.CustomHookPolicy(**kwargs)
self.redirect_policy = kwargs.get('redirect_policy') or policies.AsyncRedirectPolicy(**kwargs)
self.authentication_policy = kwargs.get('authentication_policy')
def _configure(self, **kwargs: Any) -> None:
self.user_agent_policy = kwargs.get("user_agent_policy") or policies.UserAgentPolicy(**kwargs)
self.headers_policy = kwargs.get("headers_policy") or policies.HeadersPolicy(**kwargs)
self.proxy_policy = kwargs.get("proxy_policy") or policies.ProxyPolicy(**kwargs)
self.logging_policy = kwargs.get("logging_policy") or policies.NetworkTraceLoggingPolicy(**kwargs)
self.http_logging_policy = kwargs.get("http_logging_policy") or policies.HttpLoggingPolicy(**kwargs)
self.retry_policy = kwargs.get("retry_policy") or policies.AsyncRetryPolicy(**kwargs)
self.custom_hook_policy = kwargs.get("custom_hook_policy") or policies.CustomHookPolicy(**kwargs)
self.redirect_policy = kwargs.get("redirect_policy") or policies.AsyncRedirectPolicy(**kwargs)
self.authentication_policy = kwargs.get("authentication_policy")
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
from ._duration_operations import DurationOperations

__all__ = [
'DurationOperations',
"DurationOperations",
]
Loading