88class IODict (dict ):
99
1010 def __init__ (self , * args , ** kwargs ):
11- # if first argument is data-string,
12- # try to decode it using all decoders .
11+ # if first argument is data-string try to decode it.
12+ # use 'format' kwarg to specify the decoder to use, default 'json' .
1313 if len (args ) and isinstance (args [0 ], string_types ):
14- d = IODict ._from_any_data_string (args [0 ], ** kwargs )
14+ s = args [0 ]
15+ format = kwargs .pop ('format' , 'json' ).lower ()
16+ if format in ['b64' , 'base64' ]:
17+ kwargs .setdefault ('subformat' , 'json' )
18+ # decode data-string and initialize with dict data.
19+ d = IODict ._decode (s , format , ** kwargs )
1520 if d and isinstance (d , dict ):
16- args = list (args )
17- args [0 ] = d
18- args = tuple (args )
21+ super (IODict , self ).__init__ (d )
1922 else :
2023 raise ValueError ('Invalid string data input.' )
21- super (IODict , self ).__init__ (* args , ** kwargs )
24+ else :
25+ super (IODict , self ).__init__ (* args , ** kwargs )
2226
2327 @staticmethod
24- def _decode (s , decoder , ** kwargs ):
28+ def _decode (s , format , ** kwargs ):
2529 d = None
2630 try :
2731 content = io_util .read_content (s )
28- # decode content using the given decoder
29- data = decoder (content , ** kwargs )
32+ # decode content using the given format
33+ data = io_util . decode (content , format , ** kwargs )
3034 if isinstance (data , dict ):
3135 d = data
3236 elif isinstance (data , list ):
3337 # force list to dict
34- d = { 'values' :data }
38+ d = { 'values' : data }
3539 else :
3640 raise ValueError (
3741 'Invalid data type: {}, expected dict or list.' .format (type (data )))
@@ -41,90 +45,66 @@ def _decode(s, decoder, **kwargs):
4145 return d
4246
4347 @staticmethod
44- def _encode (d , encoder , filepath = None , ** kwargs ):
45- s = encoder (d , ** kwargs )
48+ def _encode (d , format , ** kwargs ):
49+ filepath = kwargs .pop ('filepath' , None )
50+ s = io_util .encode (d , format , ** kwargs )
4651 if filepath :
4752 io_util .write_file (filepath , s )
4853 return s
4954
5055 @staticmethod
51- def _from_any_data_string (s , ** kwargs ):
52- funcs = [
53- IODict .from_base64 ,
54- IODict .from_json ,
55- IODict .from_query_string ,
56- IODict .from_toml ,
57- IODict .from_xml ,
58- IODict .from_yaml ,
59- ]
60- for f in funcs :
61- try :
62- options = kwargs .copy ()
63- d = f (s , ** options )
64- return d
65- except ValueError :
66- pass
56+ def from_base64 (s , subformat = 'json' , encoding = 'utf-8' , ** kwargs ):
57+ kwargs ['subformat' ] = subformat
58+ kwargs ['encoding' ] = encoding
59+ return IODict ._decode (s , 'base64' , ** kwargs )
6760
6861 @staticmethod
69- def from_base64 (s , format = 'json' , encoding = 'utf-8' , ** kwargs ):
70- kwargs ['format' ] = format
71- kwargs ['encoding' ] = encoding
72- return IODict ._decode (s ,
73- decoder = io_util .decode_base64 , ** kwargs )
62+ def from_csv (s , columns = None , columns_row = True , ** kwargs ):
63+ kwargs ['columns' ] = columns
64+ kwargs ['columns_row' ] = columns_row
65+ return IODict ._decode (s , 'csv' , ** kwargs )
7466
7567 @staticmethod
7668 def from_json (s , ** kwargs ):
77- return IODict ._decode (s ,
78- decoder = io_util .decode_json , ** kwargs )
69+ return IODict ._decode (s , 'json' , ** kwargs )
7970
8071 @staticmethod
8172 def from_query_string (s , ** kwargs ):
82- return IODict ._decode (s ,
83- decoder = io_util .decode_query_string , ** kwargs )
73+ return IODict ._decode (s , 'query_string' , ** kwargs )
8474
8575 @staticmethod
8676 def from_toml (s , ** kwargs ):
87- return IODict ._decode (s ,
88- decoder = io_util .decode_toml , ** kwargs )
77+ return IODict ._decode (s , 'toml' , ** kwargs )
8978
9079 @staticmethod
9180 def from_xml (s , ** kwargs ):
92- return IODict ._decode (s ,
93- decoder = io_util .decode_xml , ** kwargs )
81+ return IODict ._decode (s , 'xml' , ** kwargs )
9482
9583 @staticmethod
9684 def from_yaml (s , ** kwargs ):
97- return IODict ._decode (s ,
98- decoder = io_util .decode_yaml , ** kwargs )
85+ return IODict ._decode (s , 'yaml' , ** kwargs )
9986
100- def to_base64 (self , filepath = None , format = 'json' , encoding = 'utf-8' , ** kwargs ):
101- kwargs ['format ' ] = format
87+ def to_base64 (self , subformat = 'json' , encoding = 'utf-8' , ** kwargs ):
88+ kwargs ['subformat ' ] = subformat
10289 kwargs ['encoding' ] = encoding
103- return IODict ._encode (self ,
104- encoder = io_util .encode_base64 ,
105- filepath = filepath , ** kwargs )
106-
107- def to_json (self , filepath = None , ** kwargs ):
108- return IODict ._encode (self ,
109- encoder = io_util .encode_json ,
110- filepath = filepath , ** kwargs )
111-
112- def to_query_string (self , filepath = None , ** kwargs ):
113- return IODict ._encode (self ,
114- encoder = io_util .encode_query_string ,
115- filepath = filepath , ** kwargs )
116-
117- def to_toml (self , filepath = None , ** kwargs ):
118- return IODict ._encode (self ,
119- encoder = io_util .encode_toml ,
120- filepath = filepath , ** kwargs )
121-
122- def to_xml (self , filepath = None , ** kwargs ):
123- return IODict ._encode (self ,
124- encoder = io_util .encode_xml ,
125- filepath = filepath , ** kwargs )
126-
127- def to_yaml (self , filepath = None , ** kwargs ):
128- return IODict ._encode (self ,
129- encoder = io_util .encode_yaml ,
130- filepath = filepath , ** kwargs )
90+ return IODict ._encode (self , 'base64' , ** kwargs )
91+
92+ def to_csv (self , key = 'values' , columns = None , columns_row = True , ** kwargs ):
93+ kwargs ['columns' ] = columns
94+ kwargs ['columns_row' ] = columns_row
95+ return IODict ._encode (self [key ], 'csv' , ** kwargs )
96+
97+ def to_json (self , ** kwargs ):
98+ return IODict ._encode (self , 'json' , ** kwargs )
99+
100+ def to_query_string (self , ** kwargs ):
101+ return IODict ._encode (self , 'query_string' , ** kwargs )
102+
103+ def to_toml (self , ** kwargs ):
104+ return IODict ._encode (self , 'toml' , ** kwargs )
105+
106+ def to_xml (self , ** kwargs ):
107+ return IODict ._encode (self , 'xml' , ** kwargs )
108+
109+ def to_yaml (self , ** kwargs ):
110+ return IODict ._encode (self , 'yaml' , ** kwargs )
0 commit comments