From b0c90fcc073246531f7f915caee8778d59506a13 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Wed, 5 Apr 2023 23:39:07 +0800 Subject: [PATCH] Support to get all tags --- api/__init__.py | 2 +- api/s8_tags.py | 15 +++++++++++++-- test_tjnetwork.py | 18 ++++++++++++++++++ tjnetwork.py | 3 +++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index e12dd91..548830b 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -53,7 +53,7 @@ from .s7_valves import get_valve_schema, add_valve, get_valve, set_valve from .del_cmd import delete_valve_cascade from .s8_tags import TAG_TYPE_NODE, TAG_TYPE_LINK -from .s8_tags import get_tag_schema, get_tag, set_tag +from .s8_tags import get_tag_schema, get_tags, get_tag, set_tag from .s9_demands import get_demand_schema, get_demand, set_demand diff --git a/api/s8_tags.py b/api/s8_tags.py index 18fc1cd..aa2ed7a 100644 --- a/api/s8_tags.py +++ b/api/s8_tags.py @@ -9,14 +9,25 @@ def get_tag_schema(name: str) -> dict[str, dict[str, Any]]: 'tag' : {'type': 'str' , 'optional': True , 'readonly': False},} +def get_tags(name: str) -> list[dict[str, Any]]: + results: list[dict[str, Any]] = [] + rows = read_all(name, f"select * from tags_node") + for row in rows: + tag = str(row['tag']) if row['tag'] != None else None + results.append({ 't_type': TAG_TYPE_NODE, 'id': str(row['id']), 'tag': tag }) + rows = read_all(name, f"select * from tags_link") + for row in rows: + tag = str(row['tag']) if row['tag'] != None else None + results.append({ 't_type': TAG_TYPE_LINK, 'id': str(row['id']), 'tag': tag }) + return results + + def get_tag(name: str, t_type: str, id: str) -> dict[str, Any]: t = None if t_type == TAG_TYPE_NODE: t = try_read(name, f"select * from tags_node where id = '{id}'") elif t_type == TAG_TYPE_LINK: t = try_read(name, f"select * from tags_link where id = '{id}'") - else: - raise Exception('Only support NODE and Link') if t == None: return { 't_type': t_type, 'id': id, 'tag': None } d = {} diff --git a/test_tjnetwork.py b/test_tjnetwork.py index a4df208..e7c4d92 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -1998,10 +1998,19 @@ class TestApi: t = get_tag(p, TAG_TYPE_NODE, 'j1') assert t['tag'] == 'j1t' + tags = get_tags(p) + assert len(tags) == 1 + assert tags[0]['t_type'] == TAG_TYPE_NODE + assert tags[0]['id'] == 'j1' + assert tags[0]['tag'] == 'j1t' + set_tag(p, ChangeSet({'t_type': TAG_TYPE_NODE, 'id': 'j1', 'tag': None })) t = get_tag(p, TAG_TYPE_NODE, 'j1') assert t['tag'] == None + tags = get_tags(p) + assert len(tags) == 0 + t = get_tag(p, TAG_TYPE_NODE, 'j2') assert t['tag'] == None set_tag(p, ChangeSet({'t_type': TAG_TYPE_NODE, 'id': 'j2', 'tag': 'j2t' })) @@ -2014,6 +2023,15 @@ class TestApi: t = get_tag(p, TAG_TYPE_LINK, 'p0') assert t['tag'] == 'p0t' + tags = get_tags(p) + assert len(tags) == 2 + assert tags[0]['t_type'] == TAG_TYPE_NODE + assert tags[0]['id'] == 'j2' + assert tags[0]['tag'] == 'j2t' + assert tags[1]['t_type'] == TAG_TYPE_LINK + assert tags[1]['id'] == 'p0' + assert tags[1]['tag'] == 'p0t' + self.leave(p) diff --git a/tjnetwork.py b/tjnetwork.py index 8890429..af9c61d 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -492,6 +492,9 @@ def delete_valve(name: str, cs: ChangeSet) -> ChangeSet: def get_tag_schema(name: str) -> dict[str, dict[str, Any]]: return api.get_tag_schema(name) +def get_tags(name: str) -> list[dict[str, Any]]: + return api.get_tags(name) + def get_tag(name: str, t_type: str, id: str) -> dict[str, Any]: return api.get_tag(name, t_type, id)