1+ # -------------------------------------------------------------------------
2+ # Copyright (c) PTC Inc. and/or all its affiliates. All rights reserved.
3+ # See License.txt in the project root for
4+ # license information.
5+ # --------------------------------------------------------------------------
6+
7+ r"""`link_tags` exposes an API to allow modifications (add, delete, modify) to
8+ link tag objects within the Kepware Configuration API
9+ """
10+
11+ from ..connection import server
12+ from ..error import KepError , KepHTTPError
13+ from ..utils import _url_parse_object
14+ from typing import Union
15+ from .. import adv_tags
16+
17+ LINK_TAGS_ROOT = '/link_tags'
18+
19+ def _get_link_tags_url (tag : str = None ) -> str :
20+ '''Creates url object for the "link_tags" branch of Kepware's project tree.
21+
22+ Returns the link tag specific url when a value is passed as the tag name.
23+ '''
24+ if tag is None :
25+ return LINK_TAGS_ROOT
26+ else :
27+ return f'{ LINK_TAGS_ROOT } /{ _url_parse_object (tag )} '
28+
29+ def add_link_tag (server : server , adv_tag_group_path : str , DATA : Union [dict , list ]) -> Union [bool , list ]:
30+ '''Add `"link_tag"` or multiple `"link_tag"` objects to a specific path in Kepware.
31+ Can be used to pass a list of link tags to be added at one path location.
32+
33+ :param server: instance of the `server` class
34+ :param adv_tag_group_path: path identifying where to add link tag(s). Standard Kepware address decimal
35+ notation string such as "_advancedtags.AdvTagGroup1" or "_advancedtags.AdvTagGroup1.AdvTagGroupChild"
36+ :param DATA: Dict or List of Dicts of the link tag(s) to add
37+
38+ :return: True - If a "HTTP 201 - Created" is received from Kepware server
39+ :return: If a "HTTP 207 - Multi-Status" is received from Kepware with a list of dict error responses for all
40+ link tags added that failed.
41+
42+ :raises KepHTTPError: If urllib provides an HTTPError
43+ :raises KepURLError: If urllib provides an URLError
44+ '''
45+ path_obj = adv_tags .adv_tag_path_split (adv_tag_group_path , isItem = False )
46+ url = adv_tags ._create_adv_tags_base_url (server .url , path_obj ) + _get_link_tags_url ()
47+
48+ r = server ._config_add (url , DATA )
49+ if r .code == 201 :
50+ return True
51+ elif r .code == 207 :
52+ errors = [item for item in r .payload if item ['code' ] != 201 ]
53+ return errors
54+ else :
55+ raise KepHTTPError (r .url , r .code , r .msg , r .hdrs , r .payload )
56+
57+ def modify_link_tag (server : server , link_tag_path : str , DATA : dict , force : bool = False ) -> bool :
58+ '''Modify a `"link_tag"` object and its properties in Kepware.
59+
60+ :param server: instance of the `server` class
61+ :param link_tag_path: path identifying location and link tag to modify. Standard Kepware address decimal
62+ notation string including the link tag such as "_advancedtags.AdvTagGroup1.LinkTag1"
63+ :param DATA: Dict of the `link_tag` properties to be modified
64+ :param force: *(optional)* if True, will force the configuration update to the Kepware server
65+
66+ :return: True - If a "HTTP 200 - OK" is received from Kepware server
67+
68+ :raises KepHTTPError: If urllib provides an HTTPError
69+ :raises KepURLError: If urllib provides an URLError
70+ '''
71+ link_tag_data = server ._force_update_check (force , DATA )
72+ path_obj = adv_tags .adv_tag_path_split (link_tag_path , isItem = True )
73+ url = adv_tags ._create_adv_tags_base_url (server .url , path_obj ) + _get_link_tags_url (path_obj ['item' ])
74+
75+ r = server ._config_update (url , link_tag_data )
76+ if r .code == 200 :
77+ return True
78+ else :
79+ raise KepHTTPError (r .url , r .code , r .msg , r .hdrs , r .payload )
80+
81+ def del_link_tag (server : server , link_tag_path : str ) -> bool :
82+ '''Delete `"link_tag"` object at a specific path in Kepware.
83+
84+ :param server: instance of the `server` class
85+ :param link_tag_path: path identifying location and link tag to delete. Standard Kepware address decimal
86+ notation string including the link tag such as "_advancedtags.AdvTagGroup1.LinkTag1"
87+
88+ :return: True - If a "HTTP 200 - OK" is received from Kepware server
89+
90+ :raises KepHTTPError: If urllib provides an HTTPError
91+ :raises KepURLError: If urllib provides an URLError
92+ '''
93+ path_obj = adv_tags .adv_tag_path_split (link_tag_path , isItem = True )
94+ url = adv_tags ._create_adv_tags_base_url (server .url , path_obj ) + _get_link_tags_url (path_obj ['item' ])
95+
96+ r = server ._config_del (url )
97+ if r .code == 200 :
98+ return True
99+ else :
100+ raise KepHTTPError (r .url , r .code , r .msg , r .hdrs , r .payload )
101+
102+ def get_link_tag (server : server , link_tag_path : str ) -> dict :
103+ '''Returns the properties of the `"link_tag"` object at a specific path in Kepware.
104+
105+ :param server: instance of the `server` class
106+ :param link_tag_path: path identifying location and link tag to retrieve. Standard Kepware address decimal
107+ notation string including the link tag such as "_advancedtags.AdvTagGroup1.LinkTag1"
108+
109+ :return: Dict of data for the link tag requested
110+
111+ :raises KepHTTPError: If urllib provides an HTTPError
112+ :raises KepURLError: If urllib provides an URLError
113+ '''
114+ path_obj = adv_tags .adv_tag_path_split (link_tag_path , isItem = True )
115+ url = adv_tags ._create_adv_tags_base_url (server .url , path_obj ) + _get_link_tags_url (path_obj ['item' ])
116+
117+ r = server ._config_get (url )
118+ return r .payload
119+
120+ def get_all_link_tags (server : server , adv_tag_group_path : str , * , options : dict = None ) -> list :
121+ '''Returns the properties of all `"link_tag"` objects at a specific path in Kepware.
122+
123+ :param server: instance of the `server` class
124+ :param adv_tag_group_path: path identifying location to retrieve link tag list. Standard Kepware address decimal
125+ notation string such as "_advancedtags.AdvTagGroup1" or "_advancedtags.AdvTagGroup1.AdvTagGroupChild"
126+ :param options: *(optional)* Dict of parameters to filter, sort or paginate the list of link tags. Options are `filter`,
127+ `sortOrder`, `sortProperty`, `pageNumber`, and `pageSize`
128+
129+ :return: List of data for all link tags
130+
131+ :raises KepHTTPError: If urllib provides an HTTPError
132+ :raises KepURLError: If urllib provides an URLError
133+ '''
134+ path_obj = adv_tags .adv_tag_path_split (adv_tag_group_path , isItem = False )
135+ url = adv_tags ._create_adv_tags_base_url (server .url , path_obj ) + _get_link_tags_url ()
136+
137+ r = server ._config_get (url , params = options )
138+ return r .payload
0 commit comments