Skip to content

Commit

Permalink
churn: use async where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
netomi committed Feb 10, 2024
1 parent 8ffb1ea commit 40090b2
Show file tree
Hide file tree
Showing 28 changed files with 232 additions and 311 deletions.
6 changes: 3 additions & 3 deletions otterdog/operations/delete_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# SPDX-License-Identifier: EPL-2.0
# *******************************************************************************

import os
import aiofiles.ospath

from otterdog.config import OrganizationConfig
from otterdog.models.github_organization import GitHubOrganization
Expand Down Expand Up @@ -53,7 +53,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:
try:
org_file_name = jsonnet_config.org_config_file

if not os.path.exists(org_file_name):
if not await aiofiles.ospath.exists(org_file_name):
self.printer.print_error(
f"configuration file '{org_file_name}' does not yet exist, run 'fetch-config' or 'import' first."
)
Expand All @@ -71,7 +71,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:
self.printer.print_error(f"invalid credentials\n{str(e)}")
return 1

with GitHubProvider(credentials) as provider:
async with GitHubProvider(credentials) as provider:
rest_api = provider.rest_api

repositories_by_name = associate_by_key(organization.repositories, lambda r: r.name)
Expand Down
7 changes: 4 additions & 3 deletions otterdog/operations/diff_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

from __future__ import annotations

import os
from abc import abstractmethod
from typing import Any, Optional, Protocol

import aiofiles.ospath

from otterdog.config import OrganizationConfig, OtterdogConfig
from otterdog.jsonnet import JsonnetConfig
from otterdog.models import LivePatch, LivePatchContext, LivePatchType, ModelObject
Expand Down Expand Up @@ -88,7 +89,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:
return await self._generate_diff(org_config)
finally:
self.printer.level_down()
self._gh_client.close()
await self._gh_client.close()

def setup_github_client(self, org_config: OrganizationConfig) -> GitHubProvider:
return GitHubProvider(self.config.get_credentials(org_config, only_token=self.no_web_ui))
Expand All @@ -115,7 +116,7 @@ async def _generate_diff(self, org_config: OrganizationConfig) -> int:

org_file_name = jsonnet_config.org_config_file

if not os.path.exists(org_file_name):
if not await aiofiles.ospath.exists(org_file_name):
self.printer.print_error(
f"configuration file '{org_file_name}' does not yet exist, run fetch-config or import first."
)
Expand Down
2 changes: 1 addition & 1 deletion otterdog/operations/dispatch_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:
self.printer.print_error(f"invalid credentials\n{str(e)}")
return 1

with GitHubProvider(credentials) as provider:
async with GitHubProvider(credentials) as provider:
success = await provider.dispatch_workflow(github_id, self.repo_name, self.workflow_name)
if success is True:
self.printer.println(f"workflow '{self.workflow_name}' dispatched for repo '{self.repo_name}'")
Expand Down
16 changes: 9 additions & 7 deletions otterdog/operations/fetch_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
# SPDX-License-Identifier: EPL-2.0
# *******************************************************************************

import os
import aiofiles
import aiofiles.os
import aiofiles.ospath

from otterdog.config import OrganizationConfig
from otterdog.providers.github import GitHubProvider
Expand Down Expand Up @@ -36,7 +38,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:

org_file_name = jsonnet_config.org_config_file

if os.path.exists(org_file_name) and not self.force_processing:
if await aiofiles.ospath.exists(org_file_name) and not self.force_processing:
self.printer.println()
self.printer.println(style("Definition already exists", bright=True) + f" at '{org_file_name}'.")
self.printer.println(" Performing this action will overwrite its contents.")
Expand All @@ -56,7 +58,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:
self.printer.print_error(f"invalid credentials\n{str(e)}")
return 1

with GitHubProvider(credentials) as provider:
async with GitHubProvider(credentials) as provider:
try:
if self.pull_request is not None:
ref = await provider.get_ref_for_pull_request(
Expand All @@ -76,11 +78,11 @@ async def execute(self, org_config: OrganizationConfig) -> int:
return 1

output_dir = jsonnet_config.org_dir
if not os.path.exists(output_dir):
os.makedirs(output_dir)
if not await aiofiles.ospath.exists(output_dir):
await aiofiles.os.makedirs(output_dir)

with open(org_file_name, "w") as file:
file.write(definition)
async with aiofiles.open(org_file_name, "w") as file:
await file.write(definition)

if ref is not None:
self.printer.println(
Expand Down
19 changes: 11 additions & 8 deletions otterdog/operations/import_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
# SPDX-License-Identifier: EPL-2.0
# *******************************************************************************

import os
import shutil

import aiofiles
import aiofiles.os
import aiofiles.ospath

from otterdog.config import OrganizationConfig
from otterdog.models import PatchContext
from otterdog.models.github_organization import GitHubOrganization
Expand Down Expand Up @@ -48,7 +51,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:

org_file_name = jsonnet_config.org_config_file

if os.path.exists(org_file_name) and not self.force_processing:
if await aiofiles.ospath.exists(org_file_name) and not self.force_processing:
self.printer.println()
self.printer.println(style("Definition already exists", bright=True) + f" at '{org_file_name}'.")
self.printer.println(" Performing this action will overwrite its contents.")
Expand All @@ -59,7 +62,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:
self.printer.println("\nImport cancelled.")
return 1

if os.path.exists(org_file_name):
if await aiofiles.ospath.exists(org_file_name):
sync_secrets_from_previous_config = True
backup_file = f"{org_file_name}.bak"
shutil.copy(org_file_name, backup_file)
Expand All @@ -82,7 +85,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:
"the resulting config will be incomplete."
)

with GitHubProvider(credentials) as provider:
async with GitHubProvider(credentials) as provider:
organization = await GitHubOrganization.load_from_provider(
github_id, jsonnet_config, provider, self.no_web_ui, self.printer
)
Expand All @@ -97,11 +100,11 @@ async def execute(self, org_config: OrganizationConfig) -> int:
output = organization.to_jsonnet(jsonnet_config, context)

output_dir = jsonnet_config.org_dir
if not os.path.exists(output_dir):
os.makedirs(output_dir)
if not await aiofiles.ospath.exists(output_dir):
await aiofiles.os.makedirs(output_dir)

with open(org_file_name, "w") as file:
file.write(output)
async with aiofiles.open(org_file_name, "w") as file:
await file.write(output)

self.printer.println(f"Organization definition written to '{org_file_name}'.")

Expand Down
2 changes: 1 addition & 1 deletion otterdog/operations/list_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:
self.printer.print_error(f"invalid credentials\n{str(e)}")
return 1

with GitHubProvider(credentials) as provider:
async with GitHubProvider(credentials) as provider:
apps = await provider.rest_api.org.get_app_installations(github_id)

if not self.json_output:
Expand Down
6 changes: 3 additions & 3 deletions otterdog/operations/list_members.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# SPDX-License-Identifier: EPL-2.0
# *******************************************************************************

import os
import aiofiles.ospath

from otterdog.config import OrganizationConfig
from otterdog.models.github_organization import GitHubOrganization
Expand Down Expand Up @@ -51,7 +51,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:
try:
org_file_name = jsonnet_config.org_config_file

if not os.path.exists(org_file_name):
if not await aiofiles.ospath.exists(org_file_name):
self.printer.print_error(
f"configuration file '{org_file_name}' does not yet exist, run fetch-config or import first"
)
Expand All @@ -69,7 +69,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:
self.printer.print_error(f"invalid credentials\n{str(e)}")
return 1

with GitHubProvider(credentials) as provider:
async with GitHubProvider(credentials) as provider:
members = await provider.rest_api.org.list_members(github_id, self.two_factor_disabled)

if self.two_factor_disabled is True:
Expand Down
5 changes: 3 additions & 2 deletions otterdog/operations/local_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
# SPDX-License-Identifier: EPL-2.0
# *******************************************************************************

import os
from typing import Optional

import aiofiles.ospath

from otterdog.config import OrganizationConfig
from otterdog.jsonnet import JsonnetConfig
from otterdog.models.github_organization import GitHubOrganization
Expand Down Expand Up @@ -47,7 +48,7 @@ def setup_github_client(self, org_config: OrganizationConfig) -> GitHubProvider:
async def load_current_org(self, github_id: str, jsonnet_config: JsonnetConfig) -> GitHubOrganization:
other_org_file_name = jsonnet_config.org_config_file + self.suffix

if not os.path.exists(other_org_file_name):
if not await aiofiles.ospath.exists(other_org_file_name):
raise RuntimeError(f"configuration file '{other_org_file_name}' does not exist")

return GitHubOrganization.load_from_file(github_id, other_org_file_name, self.config)
16 changes: 9 additions & 7 deletions otterdog/operations/push_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
# SPDX-License-Identifier: EPL-2.0
# *******************************************************************************

import os.path
from typing import Optional

import aiofiles
import aiofiles.ospath

from otterdog.config import OrganizationConfig
from otterdog.providers.github import GitHubProvider
from otterdog.utils import style
Expand Down Expand Up @@ -41,7 +43,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:

org_file_name = jsonnet_config.org_config_file

if not os.path.exists(org_file_name):
if not await aiofiles.ospath.exists(org_file_name):
self.printer.print_error(
f"configuration file '{org_file_name}' does not yet exist, run fetch-config or import first"
)
Expand All @@ -56,13 +58,13 @@ async def execute(self, org_config: OrganizationConfig) -> int:
self.printer.print_error(f"invalid credentials\n{str(e)}")
return 1

with open(org_file_name, "r") as file:
content = file.read()
async with aiofiles.open(org_file_name, "r") as file:
content = await file.read()

with open(jsonnet_config.jsonnet_bundle_file, "r") as file:
bundle_content = file.read()
async with aiofiles.open(jsonnet_config.jsonnet_bundle_file, "r") as file:
bundle_content = await file.read()

with GitHubProvider(credentials) as provider:
async with GitHubProvider(credentials) as provider:
try:
updated_files = []
updated = False
Expand Down
17 changes: 10 additions & 7 deletions otterdog/operations/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import textwrap
from io import StringIO

import aiofiles.os
import aiofiles.ospath

from otterdog.config import OrganizationConfig
from otterdog.models import ModelObject
from otterdog.models.github_organization import GitHubOrganization
Expand Down Expand Up @@ -53,7 +56,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:
try:
org_file_name = jsonnet_config.org_config_file

if not os.path.exists(org_file_name):
if not await aiofiles.ospath.exists(org_file_name):
self.printer.print_error(
f"configuration file '{org_file_name}' does not yet exist, run 'fetch-config' or 'import first'"
)
Expand All @@ -68,7 +71,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:
if not self.markdown:
self._print_classic(organization)
else:
self._print_markdown(organization)
await self._print_markdown(organization)

return 0

Expand All @@ -82,9 +85,9 @@ def _print_classic(self, organization: GitHubOrganization) -> None:
model_header = model_object.get_model_header(parent_object)
self.print_dict(model_object.to_model_dict(), model_header, "", "black")

def _print_markdown(self, organization: GitHubOrganization) -> None:
if not os.path.exists(self.output_dir):
os.makedirs(self.output_dir, exist_ok=True)
async def _print_markdown(self, organization: GitHubOrganization) -> None:
if not await aiofiles.ospath.exists(self.output_dir):
await aiofiles.os.makedirs(self.output_dir, exist_ok=True)

writer = StringIO()
self.printer = IndentingPrinter(writer, spaces_per_level=4)
Expand Down Expand Up @@ -181,8 +184,8 @@ def _print_markdown(self, organization: GitHubOrganization) -> None:

self.printer.level_down()

with open(os.path.join(self.output_dir, "configuration.md"), "w") as file:
file.write(writer.getvalue())
async with aiofiles.open(os.path.join(self.output_dir, "configuration.md"), "w") as file:
await file.write(writer.getvalue())

for repo in organization.repositories:
self._print_repo_markdown(organization, repo)
Expand Down
2 changes: 1 addition & 1 deletion otterdog/operations/show_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:
self.printer.print_error(f"invalid credentials\n{str(e)}")
return 1

with GitHubProvider(credentials) as provider:
async with GitHubProvider(credentials) as provider:
if self.no_web_ui is True:
self.printer.print_warn(
"the Web UI will not be queried as '--no-web-ui' has been specified, "
Expand Down
6 changes: 3 additions & 3 deletions otterdog/operations/sync_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# SPDX-License-Identifier: EPL-2.0
# *******************************************************************************

import os
import aiofiles.ospath

from otterdog.config import OrganizationConfig
from otterdog.models.github_organization import GitHubOrganization
Expand Down Expand Up @@ -43,7 +43,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:
try:
org_file_name = jsonnet_config.org_config_file

if not os.path.exists(org_file_name):
if not await aiofiles.ospath.exists(org_file_name):
self.printer.print_error(
f"configuration file '{org_file_name}' does not yet exist, run fetch-config or import first."
)
Expand All @@ -61,7 +61,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:
self.printer.print_error(f"invalid credentials\n{str(e)}")
return 1

with GitHubProvider(credentials) as provider:
async with GitHubProvider(credentials) as provider:
rest_api = provider.rest_api

repositories_by_name = associate_by_key(organization.repositories, lambda r: r.name)
Expand Down
4 changes: 2 additions & 2 deletions otterdog/operations/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# SPDX-License-Identifier: EPL-2.0
# *******************************************************************************

import os
import aiofiles.ospath

from otterdog.config import OrganizationConfig
from otterdog.models import FailureType
Expand Down Expand Up @@ -38,7 +38,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:
try:
org_file_name = jsonnet_config.org_config_file

if not os.path.exists(org_file_name):
if not await aiofiles.ospath.exists(org_file_name):
self.printer.print_error(
f"configuration file '{org_file_name}' does not yet exist, run 'fetch-config' or 'import' first."
)
Expand Down
2 changes: 1 addition & 1 deletion otterdog/operations/web_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async def execute(self, org_config: OrganizationConfig) -> int:
self.printer.print_error(f"invalid credentials\n{str(e)}")
return 1

with GitHubProvider(credentials) as provider:
async with GitHubProvider(credentials) as provider:
await provider.web_client.open_browser_with_logged_in_user(github_id)

return 0
Expand Down
Loading

0 comments on commit 40090b2

Please sign in to comment.