Skip to content

Commit 37e5071

Browse files
committed
CQ: Show CLI warning on schema version mismatch (#50).
1 parent 5261571 commit 37e5071

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

tools/competency-query/competency_query.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def print_table(header: list[str], rows: Iterable[Iterable[str]]):
5959
QUERY_ENDPOINT = '/competency/query'
6060

6161
QUERY_DEFINITIONS_ENDPOINT = '/competency/queries'
62+
QUERY_SCHEMA_VERSION_ENDPOINT = '/competency/schema-version'
6263

6364
#===============================================================================
6465

@@ -81,7 +82,7 @@ class CompetencyQueryService:
8182
def __init__(self, map_server: str):
8283
self.__map_server = map_server
8384

84-
def request_json(self, method: str, endpoint: str, **kwds) -> dict|list:
85+
def request_json(self, method: str, endpoint: str, quiet: bool=False, **kwds) -> dict|list:
8586
#=======================================================================
8687
endpoint = self.__map_server + endpoint
8788
try:
@@ -102,15 +103,16 @@ def request_json(self, method: str, endpoint: str, **kwds) -> dict|list:
102103
error = f'HTTP error for request: {response.status_code} {response.reason}'
103104
except requests.exceptions.RequestException as exception:
104105
error = f'Exception: {exception}'
105-
print_formatted_text(FormattedText([('class:error', error),]),
106+
if not quiet:
107+
print_formatted_text(FormattedText([('class:error', error),]),
106108
style=Style.from_dict({'error': '#ff0000 bold'}))
107109
return []
108110

109-
def get_json(self, endpoint: str, param: Optional[str]=None) -> dict|list:
111+
def get_json(self, endpoint: str, param: Optional[str]=None, quiet: bool=False) -> dict|list:
110112
#=========================================================================
111113
if param is not None:
112114
endpoint += f'/{param}'
113-
return self.request_json('GET', endpoint)
115+
return self.request_json('GET', endpoint, quiet=quiet)
114116

115117
def post_query(self, request: QueryRequest) -> dict|list:
116118
#========================================================
@@ -123,13 +125,31 @@ class CompetencyQueryShell:
123125

124126
def __init__(self, map_server: str):
125127
self.__query_service = CompetencyQueryService(map_server)
128+
self.__warn_if_schema_mismatch()
126129
self.__queries: dict[str, str] = { str(query['id']): str(query['label'])
127130
for query in self.__query_service.get_json(QUERY_DEFINITIONS_ENDPOINT)
128131
if 'id' in query }
129132
self.__cmd_session = PromptSession(message=HTML('<p fg="ansiwhite"><b>cq> </b></p>'),
130133
style=Style.from_dict({'': COMMAND_INPUT_STYLE}))
131134
self.__input_session = PromptSession()
132135

136+
def __warn_if_schema_mismatch(self):
137+
#===================================
138+
schema_info = self.__query_service.get_json(QUERY_SCHEMA_VERSION_ENDPOINT, quiet=True)
139+
if isinstance(schema_info, dict):
140+
server_schema = schema_info.get('version')
141+
expected_schema = schema_info.get('expected')
142+
if expected_schema is not None and server_schema != expected_schema:
143+
warning = (
144+
'WARNING: Competency schema version mismatch. '
145+
f'Expected {expected_schema}, server has {server_schema}. '
146+
'A schema upgrade may be required.'
147+
)
148+
print_formatted_text(
149+
FormattedText([('class:warning', warning)]),
150+
style=Style.from_dict({'warning': '#ffaf00 bold'})
151+
)
152+
133153
def __list_queries(self):
134154
#========================
135155
print_table(['ID', 'Name'], list(self.__queries.items()))

0 commit comments

Comments
 (0)