22# ****************** CANADIAN ASTRONOMY DATA CENTRE *******************
33# ************* CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES **************
44#
5- # (c) 2022 . (c) 2022 .
5+ # (c) 2024 . (c) 2024 .
66# Government of Canada Gouvernement du Canada
77# National Research Council Conseil national de recherches
88# Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6
7171
7272The tag system is meant to allow tags, in addition to the standard node
7373properties. """
74+ import logging
7475import pprint
76+ import sys
7577from ..commonparser import CommonParser , set_logging_level_from_args , \
7678 exit_on_exception , URI_DESCRIPTION
7779from .. import vos
@@ -107,14 +109,15 @@ def vtag():
107109 nargs = "*" )
108110 parser .add_option ('--remove' , action = "store_true" ,
109111 help = 'remove the listed property' )
112+ parser .add_option ('-R' , '--recursive' , action = "store_true" ,
113+ help = 'perform the operation recursively on all the descendants' )
110114
111- opt = parser .parse_args ()
112- args = opt
115+ args = parser .parse_args ()
113116 set_logging_level_from_args (args )
114117
115118 # the node should be the first argument, the rest should contain
116119 # the key/val pairs
117- node = args .node
120+ node_arg = args .node
118121
119122 props = []
120123 if args .remove :
@@ -128,35 +131,53 @@ def vtag():
128131
129132 try :
130133 client = vos .Client (
131- vospace_certfile = opt .certfile ,
132- vospace_token = opt .token ,
133- insecure = opt .insecure )
134- node = client .get_node (node )
134+ vospace_certfile = args .certfile ,
135+ vospace_token = args .token ,
136+ insecure = args .insecure )
137+ node = client .get_node (node_arg )
135138 if len (props ) == 0 :
136139 # print all properties
137140 pprint .pprint (node .props )
138-
139- changed = False
140- for prop in props :
141- prop = prop .split ('=' )
142- if len (prop ) == 1 :
143- # get one property
144- pprint .pprint (node .props .get (prop [0 ], None ))
145- elif len (prop ) == 2 :
146- # update properties
147- key , value = prop
141+ if args .recursive :
142+ node .props .clear ()
143+ node .clear_properties ()
144+ for prop in props :
145+ key , value = prop .split ('=' )
148146 if len (value ) == 0 :
149147 value = None
150- if value != node .props .get (key , None ):
151- node .props [key ] = value
152- changed = True
153- else :
154- raise ValueError (
155- "Illegal keyword of value character ('=') used: %s" % (
156- '=' .join (prop )))
157-
158- if changed :
159- client .add_props (node )
148+ node .props [key ] = value
149+ successes , failures = client .add_props (node , recursive = True )
150+ if args .recursive :
151+ if failures :
152+ logging .error (
153+ 'WARN. updated count: {}, failed count: {}\n ' .
154+ format (successes , failures ))
155+ sys .exit (- 1 )
156+ else :
157+ logging .info (
158+ 'DONE. updated count: {}\n ' .format (successes ))
159+ else :
160+ changed = False
161+ for prop in props :
162+ prop = prop .split ('=' )
163+ if len (prop ) == 1 :
164+ # get one property
165+ pprint .pprint (node .props .get (prop [0 ], None ))
166+ elif len (prop ) == 2 :
167+ # update properties
168+ key , value = prop
169+ if len (value ) == 0 :
170+ value = None
171+ if value != node .props .get (key , None ):
172+ node .props [key ] = value
173+ changed = True
174+ else :
175+ raise ValueError (
176+ "Illegal keyword of value character ('=') used: %s" % (
177+ '=' .join (prop )))
178+
179+ if changed :
180+ client .add_props (node )
160181 except Exception as ex :
161182 exit_on_exception (ex )
162183
0 commit comments