-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add query-endpoints command. (#43)
Signed-off-by: Caroline Russell <[email protected]>
- Loading branch information
1 parent
f2e061d
commit 1f5e957
Showing
8 changed files
with
293 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ def _load() -> Command: | |
COMMANDS = [ | ||
'convert', | ||
'filter', | ||
'query-endpoints', | ||
'validate-lines', | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# pylint: disable=R0801 | ||
"""Query Command for the atom-tools CLI.""" | ||
import logging | ||
|
||
from cleo.helpers import option | ||
|
||
from atom_tools.cli.commands.command import Command | ||
from atom_tools.lib.converter import OpenAPI | ||
from atom_tools.lib.filtering import get_ln_range | ||
from atom_tools.lib.utils import output_endpoints | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class QueryEndpointsCommand(Command): | ||
""" | ||
This command handles the conversion of an atom slice to a specified | ||
destination format. | ||
Attributes: | ||
name (str): The name of the command. | ||
description (str): The description of the command. | ||
options (list): The list of options for the command. | ||
help (str): The help message for the command. | ||
Methods: | ||
handle: Executes the command and performs the conversion. | ||
""" | ||
|
||
name = 'query-endpoints' | ||
description = 'List elements to display in the console.' | ||
options = [ | ||
option( | ||
'input-slice', | ||
'i', | ||
'Slice file', | ||
flag=False, | ||
value_required=True, | ||
), | ||
option( | ||
'type', | ||
't', | ||
'Origin type of source on which the atom slice was generated.', | ||
flag=False, | ||
default='java', | ||
), | ||
option( | ||
'filter-lines', | ||
'f', | ||
'Filter endpoints by line number or range.', | ||
flag=False, | ||
), | ||
option( | ||
'sparse', | ||
's', | ||
'Only display names; do not include path and line numbers.', | ||
) | ||
] | ||
help = """The query command can be used to return results directly to the console. """ | ||
loggers = ['atom_tools.lib.converter', 'atom_tools.lib.regex_utils', 'atom_tools.lib.slices'] | ||
|
||
def handle(self): | ||
""" | ||
Executes the query command and performs the conversion. | ||
""" | ||
supported_types = {'java', 'jar', 'python', 'py', 'javascript', 'js', 'typescript', 'ts'} | ||
if self.option('type') not in supported_types: | ||
raise ValueError(f'Unknown origin type: {self.option("type")}') | ||
converter = OpenAPI( | ||
'openapi3.1.0', | ||
self.option('type'), | ||
self.option('input-slice'), | ||
) | ||
if not (result := converter.endpoints_to_openapi('')): | ||
logging.warning('No results produced!') | ||
line_filter = () | ||
if self.option('filter-lines'): | ||
line_filter = get_ln_range(self.option('filter-lines')) | ||
output_endpoints(result, self.option('sparse'), line_filter) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from atom_tools.lib.utils import add_params_to_cmd, remove_duplicates_list | ||
|
||
|
||
def test_add_params_to_cmd(): | ||
assert add_params_to_cmd('convert -i usages.json -f openapi3.1.0 -t java', 'test.json') == ('convert', '-i test.json -f openapi3.1.0 -t java') | ||
assert add_params_to_cmd('convert -f openapi3.1.0', 'usages.json', 'java') == ('convert', '-f openapi3.1.0 -t java -i usages.json') | ||
|
||
|
||
def test_remove_duplicates_list(): | ||
data = [ | ||
{'function_name': '', 'code': '', 'line_number': 1}, | ||
{'function_name': ':program', 'line_number': 1, 'code': None}, | ||
{'function_name': ':program', 'line_number': 1, 'code': None}, | ||
{'function_name': 'require', 'line_number': 6, 'code': 'app.ts::program:require'} | ||
] | ||
assert remove_duplicates_list(data) == [ | ||
{'code': '', 'function_name': '', 'line_number': 1}, | ||
{'code': None, 'function_name': ':program', 'line_number': 1}, | ||
{'code': 'app.ts::program:require', 'function_name': 'require', 'line_number': 6} | ||
] |