Skip to content

Commit

Permalink
Fix issue with base64 and python2/3
Browse files Browse the repository at this point in the history
  • Loading branch information
jrrodri committed Feb 23, 2019
1 parent a7ae2c8 commit 51845b6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 13 additions & 1 deletion abraia/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import base64

API_URL = 'https://api.abraia.me'
Expand All @@ -15,6 +16,17 @@
'psd': 'image/vnd.adobe.photoshop'}


def base64encode(str):
str = str.encode('utf-8') if sys.version_info[0] == 3 else str
str = base64.b64encode(str)
return str.decode('ascii') if isinstance(str, bytes) else str


def base64decode(str):
str = base64.b64decode(str)
return str.decode('ascii') if isinstance(str, bytes) else str


def load_auth():
abraia_key = os.environ.get('ABRAIA_KEY')
if os.path.isfile(CONFIG_FILE) and (abraia_key is None):
Expand All @@ -25,7 +37,7 @@ def load_auth():
config[key] = value
return config['abraia_api_key'], config['abraia_api_secret']
elif abraia_key:
api_key, api_secret = base64.b64decode(abraia_key).split(':')
api_key, api_secret = base64decode(abraia_key).split(':')
return api_key, api_secret
return '', ''

Expand Down
8 changes: 5 additions & 3 deletions scripts/abraia
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ from builtins import input
import os
import sys
import shutil
import base64
import argparse
from tqdm import tqdm
from datetime import datetime

from abraia.config import base64encode, base64decode

import abraia

IMAGE_EXTS = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg', '.psd']
Expand All @@ -21,10 +22,11 @@ def process_info():

def process_configure():
api_key, api_secret = abraia.config.load_auth()
abraia_key = '{}:{}'.format(api_key, api_secret).encode('base64') if api_key and api_secret else ''
abraia_key = base64encode(
'{}:{}'.format(api_key, api_secret)) if api_key and api_secret else ''
key = input('Abraia Key [{}]: '.format(abraia_key))
abraia_key = abraia_key if key == '' else key
api_key, api_secret = base64.b64decode(abraia_key).split(':')
api_key, api_secret = base64decode(abraia_key).split(':')
abraia.config.save_auth(api_key, api_secret)


Expand Down

0 comments on commit 51845b6

Please sign in to comment.