@@ -59,14 +59,18 @@ class FileOrData(object):
59
59
content of obj[%file_key_name] and represent it as file or data.
60
60
Note that the data is preferred. The obj[%file_key_name] will be used iff
61
61
obj['%data_key_name'] is not set or empty. Assumption is file content is
62
- raw data and data field is base64 string."""
62
+ raw data and data field is base64 string. The assumption can be changed
63
+ with base64_file_content flag. If set to False, the content of the file
64
+ will assumed to be base64 and read as is. The default True value will
65
+ result in base64 encode of the file content after read."""
63
66
64
67
def __init__ (self , obj , file_key_name , data_key_name = None ,
65
- file_base_path = "" ):
68
+ file_base_path = "" , base64_file_content = True ):
66
69
if not data_key_name :
67
70
data_key_name = file_key_name + "-data"
68
71
self ._file = None
69
72
self ._data = None
73
+ self ._base64_file_content = base64_file_content
70
74
if data_key_name in obj :
71
75
self ._data = obj [data_key_name ]
72
76
elif file_key_name in obj :
@@ -78,8 +82,11 @@ def as_file(self):
78
82
decoded obj[%data_key_name] content otherwise obj[%file_key_name]."""
79
83
use_data_if_no_file = not self ._file and self ._data
80
84
if use_data_if_no_file :
81
- self ._file = _create_temp_file_with_content (
82
- base64 .decodestring (self ._data .encode ()))
85
+ if self ._base64_file_content :
86
+ self ._file = _create_temp_file_with_content (
87
+ base64 .decodestring (self ._data .encode ()))
88
+ else :
89
+ self ._file = _create_temp_file_with_content (self ._data )
83
90
if self ._file and not os .path .isfile (self ._file ):
84
91
raise ConfigException ("File does not exists: %s" % self ._file )
85
92
return self ._file
@@ -90,8 +97,11 @@ def as_data(self):
90
97
use_file_if_no_data = not self ._data and self ._file
91
98
if use_file_if_no_data :
92
99
with open (self ._file ) as f :
93
- self ._data = bytes .decode (
94
- base64 .encodestring (str .encode (f .read ())))
100
+ if self ._base64_file_content :
101
+ self ._data = bytes .decode (
102
+ base64 .encodestring (str .encode (f .read ())))
103
+ else :
104
+ self ._data = f .read ()
95
105
return self ._data
96
106
97
107
@@ -164,7 +174,8 @@ def _load_gcp_token(self):
164
174
def _load_user_token (self ):
165
175
token = FileOrData (
166
176
self ._user , 'tokenFile' , 'token' ,
167
- file_base_path = self ._config_base_path ).as_data ()
177
+ file_base_path = self ._config_base_path ,
178
+ base64_file_content = False ).as_data ()
168
179
if token :
169
180
self .token = "Bearer %s" % token
170
181
return True
0 commit comments