1
1
#!/usr/bin/env python
2
2
# PYTHON_ARGCOMPLETE_OK
3
3
4
+ from __future__ import print_function
5
+ import six
4
6
import re
5
7
import requests
6
8
import collections
@@ -45,7 +47,7 @@ def _prefix(cmd):
45
47
def echo (fn ):
46
48
def wrapped (* args , ** kw ):
47
49
ret = fn (* args , ** kw )
48
- print fn .__name__ , repr (ret )
50
+ print ( fn .__name__ , repr (ret ) )
49
51
return ret
50
52
return wrapped
51
53
@@ -56,7 +58,7 @@ def wrapped(*args, **kw):
56
58
start = time .time ()
57
59
ret = fn (* args , ** kw )
58
60
delta = time .time () - start
59
- print delta , args [1 ], fn .__name__
61
+ print ( delta , args [1 ], fn .__name__ )
60
62
return ret
61
63
else :
62
64
return fn (* args , ** kw )
@@ -78,7 +80,7 @@ def _as_table(self):
78
80
if not hasattr (self , 'type' ):
79
81
return str (self .__dict__ )
80
82
data = [('Type' , 'Id' , 'Name' , 'Value' )]
81
- for k , v in self .iteritems ():
83
+ for k , v in six .iteritems (self ):
82
84
if self ._is_public (k , v ):
83
85
if v is None :
84
86
v = 'null'
@@ -99,7 +101,7 @@ def _is_list(self):
99
101
100
102
def __repr__ (self ):
101
103
data = {}
102
- for k , v in self . __dict__ . iteritems ():
104
+ for k , v in six . iteritems (self . __dict__ ):
103
105
if self ._is_public (k , v ):
104
106
data [k ] = v
105
107
return repr (data )
@@ -204,7 +206,7 @@ def object_hook(self, obj):
204
206
if isinstance (obj , dict ):
205
207
result = RestObject ()
206
208
207
- for k , v in obj .iteritems ():
209
+ for k , v in six .iteritems (obj ):
208
210
setattr (result , k , self .object_hook (v ))
209
211
210
212
for link in ['next' , 'prev' ]:
@@ -216,9 +218,9 @@ def object_hook(self, obj):
216
218
pass
217
219
218
220
if hasattr (result , 'type' ) and isinstance (getattr (result , 'type' ),
219
- basestring ):
221
+ six . string_types ):
220
222
if hasattr (result , 'links' ):
221
- for link_name , link in result . links . iteritems ():
223
+ for link_name , link in six . iteritems (result . links ):
222
224
cb = lambda _link = link , ** kw : self ._get (_link ,
223
225
data = kw )
224
226
if hasattr (result , link_name ):
@@ -227,7 +229,7 @@ def object_hook(self, obj):
227
229
setattr (result , link_name , cb )
228
230
229
231
if hasattr (result , 'actions' ):
230
- for link_name , link in result . actions . iteritems ():
232
+ for link_name , link in six . iteritems (result . actions ):
231
233
cb = lambda _link_name = link_name , _result = result , \
232
234
* args , ** kw : self .action (_result , _link_name ,
233
235
* args , ** kw )
@@ -338,7 +340,7 @@ def by_id(self, type, id, **kw):
338
340
url = '/' .join ([url , id ])
339
341
try :
340
342
return self ._get (url , self ._to_dict (** kw ))
341
- except ApiError , e :
343
+ except ApiError as e :
342
344
if e .error .status == 404 :
343
345
return None
344
346
else :
@@ -367,7 +369,7 @@ def _validate_list(self, type, **kw):
367
369
if hasattr (collection_filters , k ):
368
370
return
369
371
370
- for filter_name , filter_value in collection_filters .iteritems ():
372
+ for filter_name , filter_value in six .iteritems (collection_filters ):
371
373
for m in filter_value .modifiers :
372
374
if k == '_' .join ([filter_name , m ]):
373
375
return
@@ -395,7 +397,7 @@ def delete(self, *args):
395
397
return self ._delete (i .links .self )
396
398
397
399
def action (self , obj , action_name , * args , ** kw ):
398
- url = obj .actions [ action_name ]
400
+ url = getattr ( obj .actions , action_name )
399
401
return self ._post (url , data = self ._to_dict (* args , ** kw ))
400
402
401
403
def _is_list (self , obj ):
@@ -411,7 +413,7 @@ def _is_list(self, obj):
411
413
def _to_value (self , value ):
412
414
if isinstance (value , dict ):
413
415
ret = {}
414
- for k , v in value .iteritems ():
416
+ for k , v in six .iteritems (value ):
415
417
ret [k ] = self ._to_value (v )
416
418
return ret
417
419
@@ -445,10 +447,10 @@ def _to_dict(self, *args, **kw):
445
447
for i in args :
446
448
value = self ._to_value (i )
447
449
if isinstance (value , dict ):
448
- for k , v in value .iteritems ():
450
+ for k , v in six .iteritems (value ):
449
451
ret [k ] = v
450
452
451
- for k , v in kw .iteritems ():
453
+ for k , v in six .iteritems (kw ):
452
454
ret [k ] = self ._to_value (v )
453
455
454
456
return ret
@@ -470,15 +472,14 @@ def _bind_methods(self, schema):
470
472
('create' , 'collectionMethods' , POST_METHOD , self .create )
471
473
]
472
474
473
- for type_name , type in schema . types . iteritems ():
475
+ for type_name , typ in six . iteritems (schema . types ):
474
476
for name_variant in self ._type_name_variants (type_name ):
475
477
for method_name , type_collection , test_method , m in bindings :
476
478
# double lambda for lexical binding hack, I'm sure there's
477
479
# a better way to do this
478
480
cb = lambda type_name = type_name , method = m : \
479
481
lambda * args , ** kw : method (type_name , * args , ** kw )
480
- if hasattr (type , type_collection ) and \
481
- test_method in type [type_collection ]:
482
+ if test_method in getattr (typ , type_collection , []):
482
483
setattr (self , '_' .join ([method_name , name_variant ]),
483
484
cb ())
484
485
@@ -537,14 +538,17 @@ def _print_cli(client, obj):
537
538
return
538
539
539
540
if JSON :
540
- print client ._marshall (obj , indent = 2 , sort_keys = True )
541
+ print ( client ._marshall (obj , indent = 2 , sort_keys = True ) )
541
542
elif callable (getattr (obj , '_as_table' )):
542
- print obj ._as_table ()
543
+ print ( obj ._as_table () )
543
544
else :
544
- print obj
545
+ print ( obj )
545
546
546
547
# {{{ http://code.activestate.com/recipes/267662/ (r7)
547
- import cStringIO
548
+ try :
549
+ from cStringIO import StringIO
550
+ except ImportError :
551
+ from io import StringIO
548
552
import operator
549
553
550
554
@@ -580,15 +584,16 @@ def rowWrapper(row):
580
584
len (delim )* (len (maxWidths )- 1 ))
581
585
# select the appropriate justify method
582
586
justify = {'center' : str .center , 'right' : str .rjust , 'left' : str .ljust }[justify .lower ()] # NOQA
583
- output = cStringIO . StringIO ()
587
+ output = StringIO ()
584
588
if separateRows :
585
- print >> output , rowSeparator
589
+ print ( rowSeparator , file = output )
586
590
for physicalRows in logicalRows :
587
591
for row in physicalRows :
588
- print >> output , prefix \
589
- + delim .join ([justify (str (item ), width ) for (item , width ) in zip (row , maxWidths )]) + postfix # NOQA
592
+ print (prefix
593
+ + delim .join ([justify (str (item ), width ) for (item , width ) in zip (row , maxWidths )]) + postfix , # NOQA
594
+ file = output )
590
595
if separateRows or hasHeader :
591
- print >> output , rowSeparator
596
+ print ( rowSeparator , file = output )
592
597
hasHeader = False
593
598
return output .getvalue ()
594
599
# End {{{ http://code.activestate.com/recipes/267662/ (r7)
@@ -610,7 +615,7 @@ def from_env(prefix=PREFIX + '_', factory=Client, **kw):
610
615
611
616
def _from_env (prefix = PREFIX + '_' , factory = Client , ** kw ):
612
617
result = dict (kw )
613
- for k , v in kw .iteritems ():
618
+ for k , v in six .iteritems (kw ):
614
619
if v is not None :
615
620
result [k ] = v
616
621
else :
@@ -651,7 +656,7 @@ def _general_args(help=True):
651
656
def _list_args (subparsers , client , type , schema ):
652
657
help_msg = LIST [0 :len (LIST )- 1 ].capitalize () + ' ' + type
653
658
subparser = subparsers .add_parser (LIST + type , help = help_msg )
654
- for name , filter in schema . collectionFilters . iteritems ():
659
+ for name , filter in six . iteritems (schema . collectionFilters ):
655
660
subparser .add_argument ('--' + name )
656
661
for m in filter .modifiers :
657
662
if m != 'eq' :
@@ -686,7 +691,7 @@ def _generic_args(subparsers, field_key, type, schema,
686
691
subparser = subparsers .add_parser (prefix , help = help_msg )
687
692
688
693
if schema is not None :
689
- for name , field in schema .iteritems ():
694
+ for name , field in six .iteritems (schema ):
690
695
if field .get (field_key ) is True :
691
696
if field .get ('type' ).startswith ('array' ):
692
697
subparser .add_argument ('--' + name , nargs = '*' )
@@ -701,7 +706,7 @@ def _generic_args(subparsers, field_key, type, schema,
701
706
def _full_args (client ):
702
707
parser = _general_args ()
703
708
subparsers = parser .add_subparsers (help = 'Sub-Command Help' )
704
- for type , schema in client .schema .types . iteritems ( ):
709
+ for type , schema in six . iteritems ( client .schema .types ):
705
710
if schema .listable :
706
711
subparser = _list_args (subparsers , client , type , schema )
707
712
subparser .set_defaults (_action = LIST , _type = type )
@@ -721,7 +726,7 @@ def _full_args(client):
721
726
subparser .set_defaults (_action = DELETE , _type = type )
722
727
723
728
try :
724
- for name , args in schema . resourceActions . iteritems ():
729
+ for name , args in six . iteritems (schema . resourceActions ):
725
730
action_schema = None
726
731
try :
727
732
action_schema = client .schema .types [args .input ]
@@ -783,7 +788,7 @@ def _run_cli(client, namespace):
783
788
obj = client .action (obj , command_type [len (ACTION ):], ** args )
784
789
if obj :
785
790
_print_cli (client , obj )
786
- except ApiError , e :
791
+ except ApiError as e :
787
792
import sys
788
793
789
794
sys .stderr .write ('Error : {}\n ' .format (e .error ))
0 commit comments