Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[one-cmds] Implement get_arg_parser #13169

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions compiler/one-cmds/onelib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
import subprocess
import sys

from typing import Union
from typing import Union, Optional

import onelib.constant as _constant
from onelib.argumentparse import ArgumentParser


def add_default_arg(parser):
Expand Down Expand Up @@ -96,20 +97,27 @@ def get_config_parser() -> configparser.ConfigParser:
return parser


def parse_cfg(config_path: Union[str, None], section_to_parse: str, args):
def parse_cfg(config_path: Union[str, None],
section_to_parse: str,
args,
quiet: bool = False):
"""
parse configuration file and store the information to args

:param config_path: path to configuration file
:param section_to_parse: section name to parse
:param args: object to store the parsed information
:param quiet: raise no error when given section doesn't exist
"""
if config_path is None:
return

parser = get_config_parser()
parser.read(config_path)

if not parser.has_section(section_to_parse) and quiet:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quiet arg is added because introducing [backend] section is not always exist for the backward compatability.

return

if not parser.has_section(section_to_parse):
raise AssertionError('configuration file must have \'' + section_to_parse +
'\' section')
Expand Down Expand Up @@ -231,6 +239,31 @@ def get_optimization_list(get_name=False):
return opt_list


def get_arg_parser(target: Optional[str], cmd: str) -> Optional[ArgumentParser]:
if not target:
return None

dir_path = os.path.dirname(os.path.realpath(__file__))
# for python module naming convention
target_name = target.replace('-', '_')
command_schema_path = dir_path + f'/../../backends/command/{cmd}/{target_name}.py'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(optional) if there some kind of path provider then we can ask that provider for this kind of information.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's good point. I'll revise that later.

if not os.path.isfile(command_schema_path):
return None

# https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
spec = importlib.util.spec_from_file_location(target_name, command_schema_path)
module = importlib.util.module_from_spec(spec)
sys.modules[target_name] = module
spec.loader.exec_module(module)

if not hasattr(module, "command_schema"):
raise RuntimeError('You must implement "command_schema" function')

parser: ArgumentParser = module.command_schema()
parser.target = target
return parser


def detect_one_import_drivers(search_path):
"""Looks for import drivers in given directory

Expand Down
Loading