Skip to content

Commit

Permalink
Merge pull request #162 from kurusugawa-computer/159-set-endpoint-url
Browse files Browse the repository at this point in the history
159 set endpoint url
  • Loading branch information
yuji38kwmt authored Jan 24, 2020
2 parents 7509ed8 + aa79a7c commit 22d3db1
Show file tree
Hide file tree
Showing 60 changed files with 226 additions and 131 deletions.
12 changes: 6 additions & 6 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion annofabcli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def main(arguments: Optional[Sequence[str]] = None):
annofabcli.organization_member.subcommand_organization_member.add_parser(subparsers)
annofabcli.project.subcommand_project.add_parser(subparsers)
annofabcli.project_member.subcommand_project_member.add_parser(subparsers)
annofabcli.task.subcommand_task.add_parser(subparsers)
annofabcli.statistics.subcommand_statistics.add_parser(subparsers)
annofabcli.supplementary.subcommand_supplementary.add_parser(subparsers)
annofabcli.task.subcommand_task.add_parser(subparsers)

annofabcli.filesystem.subcommand_filesystem.add_parser(subparsers)
annofabcli.experimental.subcommand_experimental.add_parser(subparsers)
Expand All @@ -61,6 +61,7 @@ def main(arguments: Optional[Sequence[str]] = None):

if hasattr(args, "subcommand_func"):
try:
annofabcli.cli.load_logging_config_from_args(args)
args.subcommand_func(args)
except Exception as e:
logger.exception(e)
Expand Down
2 changes: 1 addition & 1 deletion annofabcli/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.18.2"
__version__ = "1.19.0"
2 changes: 1 addition & 1 deletion annofabcli/annotation/list_annotation_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def main(self):


def main(args):
service = build_annofabapi_resource_and_login()
service = build_annofabapi_resource_and_login(args)
facade = AnnofabApiFacade(service)
ListAnnotationCount(service, facade, args).main()

Expand Down
4 changes: 3 additions & 1 deletion annofabcli/annotation/subcommand_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ def add_parser(subparsers: argparse._SubParsersAction):
subcommand_help = "アノテーション関係のサブコマンド"
description = "アノテーション関係のサブコマンド"

parser = annofabcli.common.cli.add_parser(subparsers, subcommand_name, subcommand_help, description)
parser = annofabcli.common.cli.add_parser(
subparsers, subcommand_name, subcommand_help, description, is_subcommand=False
)
parse_args(parser)
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def main(self):


def main(args):
service = build_annofabapi_resource_and_login()
service = build_annofabapi_resource_and_login(args)
facade = AnnofabApiFacade(service)
AnnotationSpecsHistories(service, facade, args).main()

Expand Down
2 changes: 1 addition & 1 deletion annofabcli/annotation_specs/list_annotation_specs_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def parse_args(parser: argparse.ArgumentParser):


def main(args):
service = build_annofabapi_resource_and_login()
service = build_annofabapi_resource_and_login(args)
facade = AnnofabApiFacade(service)
PrintAnnotationSpecsLabel(service, facade, args).main()

Expand Down
2 changes: 1 addition & 1 deletion annofabcli/annotation_specs/print_label_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def parse_args(parser: argparse.ArgumentParser):


def main(args):
service = build_annofabapi_resource_and_login()
service = build_annofabapi_resource_and_login(args)
facade = AnnofabApiFacade(service)
PrintLabelColor(service, facade, args).main()

Expand Down
4 changes: 3 additions & 1 deletion annofabcli/annotation_specs/subcommand_annotation_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ def add_parser(subparsers: argparse._SubParsersAction):
subcommand_help = "アノテーション仕様関係のサブコマンド"
description = "アノテーション仕様関係のサブコマンド"

parser = annofabcli.common.cli.add_parser(subparsers, subcommand_name, subcommand_help, description)
parser = annofabcli.common.cli.add_parser(
subparsers, subcommand_name, subcommand_help, description, is_subcommand=False
)
parse_args(parser)
117 changes: 91 additions & 26 deletions annofabcli/common/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import getpass
import json
import logging
import os
from typing import Any, Dict, List, Optional, Tuple

import annofabapi
import jmespath
import pandas
import requests
from annofabapi.api import DEFAULT_ENDPOINT_URL
from annofabapi.exceptions import AnnofabApiException
from annofabapi.models import OrganizationMemberRole, ProjectMemberRole

Expand All @@ -27,16 +29,19 @@
logger = logging.getLogger(__name__)


def build_annofabapi_resource_and_login() -> annofabapi.Resource:
def build_annofabapi_resource_and_login(args: argparse.Namespace) -> annofabapi.Resource:
"""
annofabapi.Resourceインスタンスを生成する。
annofabapi.Resourceインスタンスを生成したあと、ログインする。
Args:
args: コマンドライン引数の情報
Returns:
annofabapi.Resourceインスタンス
"""

service = build_annofabapi_resource()
service = build_annofabapi_resource(args)

try:
service.api.login()
Expand All @@ -50,27 +55,30 @@ def build_annofabapi_resource_and_login() -> annofabapi.Resource:

def add_parser(
subparsers: argparse._SubParsersAction,
subcommand_name: str,
subcommand_help: str,
command_name: str,
command_help: str,
description: str,
is_subcommand: bool = True,
epilog: Optional[str] = None,
) -> argparse.ArgumentParser:
"""
サブコマンド用にparserを追加する
Args:
subparsers:
subcommand_name:
subcommand_help:
description:
epilog:
command_name:
command_help: 1階層上のコマンドヘルプに表示される コマンドの説明(簡易的な説明)
description: ヘルプ出力に表示される説明(詳細な説明)
is_subcommand: サブコマンドかどうか. `annofabcli project`はコマンド、`annofabcli project list`はサブコマンドとみなす。
epilog: ヘルプ出力後に表示される内容。デフォルトはNoneです。
Returns:
サブコマンドのparser
"""
parents = [create_parent_parser()] if is_subcommand else []
parser = subparsers.add_parser(
subcommand_name, parents=[create_parent_parser()], description=description, help=subcommand_help, epilog=epilog
command_name, parents=parents, description=description, help=command_help, epilog=epilog
)
parser.set_defaults(command_help=parser.print_help)
return parser
Expand All @@ -85,6 +93,19 @@ def create_parent_parser() -> argparse.ArgumentParser:

group.add_argument("--yes", action="store_true", help="処理中に現れる問い合わせに対して、常に'yes'と回答します。")

# EXAMPLE_CREDENTAILS = '{"user_id": "test_user", "password": "test_password"}'
# group.add_argument(
# "--credentials",
# type=str,
# help=f"AnnoFabにログインするユーザの認証情報をJSON形式で指定します。"
# f"(ex) `{EXAMPLE_CREDENTAILS}` ."
# f"`file://`を先頭に付けると、JSON形式のファイルを指定できます。",
# )

group.add_argument(
"--endpoint_url", type=str, help=f"AnnoFab WebAPIのエンドポイントを指定します。指定しない場合は'{DEFAULT_ENDPOINT_URL}'です。"
)

group.add_argument(
"--logdir", type=str, default=".log", help="ログファイルを保存するディレクトリを指定します。指定しない場合は`.log`ディレクトリ'にログファイルが保存されます。"
)
Expand Down Expand Up @@ -182,21 +203,56 @@ def get_wait_options_from_args(
return default_wait_options


def load_logging_config_from_args(args: argparse.Namespace):
def load_logging_config_from_args(args: argparse.Namespace) -> None:
"""
args情報から、logging設定ファイルを読み込む
args情報から、logging設定ファイルを読み込む.
以下のコマンドライン引数からlogging設定ファイルを読み込む。
``--disable_log`` が指定されている場合は、loggerを設定しない。
* --logdir
* --disable_log
* --logging_yaml
Args:
args: Command引数情報
"""
log_dir = args.logdir
logging_yaml_file = args.logging_yaml if hasattr(args, "logging_yaml") else None

annofabcli.utils.load_logging_config(log_dir, logging_yaml_file)
if args.disable_log:
return

annofabcli.utils.load_logging_config(args.logdir, args.logging_yaml)


def get_endpoint_url(args: argparse.Namespace) -> str:
"""
AnnoFab WebAPIのエンドポイントURLを、以下の優先順位で取得する。
1. コマンドライン引数 ``--endpoint_url``
2. 環境変数 ``ANNOFAB_ENDPOINT_URL``
取得できない場合は、デフォルトの ``https://annofab.com`` を返す。
Args:
args: コマンドライン引数情報
Returns:
AnnoFab WebAPIのエンドポイントURL
def build_annofabapi_resource() -> annofabapi.Resource:
"""
annofabapi.Resourceインスタナスを生成する。
endpoint_url = args.endpoint_url
if endpoint_url is not None:
return endpoint_url

endpoint_url = os.environ.get("ANNOFAB_ENDPOINT_URL")
if endpoint_url is not None:
return endpoint_url

return DEFAULT_ENDPOINT_URL


def build_annofabapi_resource(args: argparse.Namespace) -> annofabapi.Resource:
"""
annofabapi.Resourceインスタンスを生成する。
以下の順にAnnoFabの認証情報を読み込む。
1. `.netrc`ファイル
2. 環境変数`ANNOFAB_USER_ID` , `ANNOFAB_PASSWORD`
Expand All @@ -207,16 +263,28 @@ def build_annofabapi_resource() -> annofabapi.Resource:
annofabapi.Resourceインスタンス
"""

endpoint_url = get_endpoint_url(args)
if endpoint_url != DEFAULT_ENDPOINT_URL:
logger.info(f"AnnoFab WebAPIのエンドポイントURL: {endpoint_url}")

# # コマンドライン引数から認証情報を取得する
# dict_credentials = annofabcli.common.cli.get_json_from_args(args.credentials)
# if dict_credentials is not None:
# return annofabapi.build(
# dict_credentials.get("user_id"), dict_credentials.get("password"), endpoint_url=endpoint_url
# )

# '.netrc'ファイルから認証情報を取得する
try:
return annofabapi.build_from_netrc()
return annofabapi.build_from_netrc(endpoint_url)
except AnnofabApiException:
logger.info("`.netrc`ファイルにはAnnoFab認証情報が存在しなかった")
logger.debug("`.netrc`ファイルにはAnnoFab認証情報が存在しなかった")

# 環境変数から認証情報を取得する
try:
return annofabapi.build_from_env()
return annofabapi.build_from_env(endpoint_url)
except AnnofabApiException:
logger.info("`環境変数`ANNOFAB_USER_ID` or `ANNOFAB_PASSWORD`が空だった")
logger.debug("`環境変数`ANNOFAB_USER_ID` or `ANNOFAB_PASSWORD`が空だった")

# 標準入力から入力させる
login_user_id = ""
Expand All @@ -227,7 +295,7 @@ def build_annofabapi_resource() -> annofabapi.Resource:
while login_password == "":
login_password = getpass.getpass("Enter AnnoFab Password: ")

return annofabapi.build(login_user_id, login_password)
return annofabapi.build(login_user_id, login_password, endpoint_url=endpoint_url)


def prompt_yesno(msg: str) -> bool:
Expand Down Expand Up @@ -387,9 +455,6 @@ def process_common_args(self, args: argparse.Namespace):
Args:
args: コマンドライン引数
"""
if not args.disable_log:
load_logging_config_from_args(args)

self.all_yes = args.yes
if hasattr(args, "query"):
self.query = args.query
Expand Down
1 change: 0 additions & 1 deletion annofabcli/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def load_logging_config(log_dir: str, logging_yaml_file: Optional[str] = None):
Args:
log_dir: ログの出力先
log_filename: ログのファイル名
logging_yaml_file: ログ設定ファイル。指定しない場合、`data/logging.yaml`を読み込む. 指定した場合、log_dir, log_filenameは無視する。
"""
Expand Down
2 changes: 1 addition & 1 deletion annofabcli/deprecated_register_annotation_from_full_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def parse_args(parser: argparse.ArgumentParser):


def main(args):
service = build_annofabapi_resource_and_login()
service = build_annofabapi_resource_and_login(args)
facade = AnnofabApiFacade(service)
RegisterAnnotation(service, facade).main(args)

Expand Down
2 changes: 1 addition & 1 deletion annofabcli/experimental/find_break_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def _timedelta_to_HM(td: datetime.timedelta):


def main(args):
service = build_annofabapi_resource_and_login()
service = build_annofabapi_resource_and_login(args)
facade = AnnofabApiFacade(service)
FindBreakError(service, facade, args).main()

Expand Down
2 changes: 1 addition & 1 deletion annofabcli/experimental/list_labor_worktime.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def main(self):


def main(args):
service = build_annofabapi_resource_and_login()
service = build_annofabapi_resource_and_login(args)
facade = AnnofabApiFacade(service)
ListLaborWorktime(service, facade, args).main()

Expand Down
4 changes: 3 additions & 1 deletion annofabcli/experimental/subcommand_experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ def add_parser(subparsers: argparse._SubParsersAction):
subcommand_help = "アルファ版のサブコマンド"
description = "アルファ版のサブコマンド。予告なしに削除されたり、コマンドライン引数が変わったりします。"

parser = annofabcli.common.cli.add_parser(subparsers, subcommand_name, subcommand_help, description)
parser = annofabcli.common.cli.add_parser(
subparsers, subcommand_name, subcommand_help, description, is_subcommand=False
)
parse_args(parser)
4 changes: 3 additions & 1 deletion annofabcli/filesystem/subcommand_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ def add_parser(subparsers: argparse._SubParsersAction):
subcommand_help = "ファイル操作関係(Web APIにアクセスしない)のサブコマンド"
description = "ファイル操作関係(Web APIにアクセスしない)のサブコマンド"

parser = annofabcli.common.cli.add_parser(subparsers, subcommand_name, subcommand_help, description)
parser = annofabcli.common.cli.add_parser(
subparsers, subcommand_name, subcommand_help, description, is_subcommand=False
)
parse_args(parser)
1 change: 0 additions & 1 deletion annofabcli/filesystem/write_annotation_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def is_target_parser(parser: SimpleAnnotationParser) -> bool:
return is_target_parser

def main(self, args):
annofabcli.common.cli.load_logging_config_from_args(args)
logger.info(f"args: {args}")

image_size = annofabcli.common.cli.get_input_data_size(args.image_size)
Expand Down
2 changes: 1 addition & 1 deletion annofabcli/input_data/delete_input_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def main(self):


def main(args):
service = build_annofabapi_resource_and_login()
service = build_annofabapi_resource_and_login(args)
facade = AnnofabApiFacade(service)
DeleteInputData(service, facade, args).main()

Expand Down
Loading

0 comments on commit 22d3db1

Please sign in to comment.