<s> from __future__ import print_function <EOL> import os <EOL> import base64 <EOL> NOIPY_CONFIG = "<STR_LIT>" <EOL> DEFAULT_CONFIG_DIR = os . path . expanduser ( "<STR_LIT>" ) <EOL> class ApiAuth ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , usertoken , password = "<STR_LIT>" ) : <EOL> self . _usertoken = usertoken <EOL> self . _password = password <EOL> @ property <EOL> def token ( self ) : <EOL> if self . _password != "<STR_LIT>" : <EOL> raise NotImplementedError <EOL> return self . _usertoken <EOL> @ property <EOL> def base64key ( self ) : <EOL> auth_str = '<STR_LIT>' % ( self . _usertoken , self . _password ) <EOL> return base64 . b64encode ( auth_str . encode ( '<STR_LIT:utf-8>' ) ) <EOL> @ classmethod <EOL> def get_instance ( cls , encoded_key ) : <EOL> """<STR_LIT>""" <EOL> login_str = base64 . b64decode ( encoded_key ) . decode ( '<STR_LIT:utf-8>' ) <EOL> usertoken , password = login_str . strip ( ) . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> instance = cls ( usertoken , password ) <EOL> return instance <EOL> def __str__ ( self ) : <EOL> return self . base64key . decode ( '<STR_LIT:utf-8>' ) <EOL> def __eq__ ( self , other ) : <EOL> return str ( self ) == str ( other ) <EOL> def store ( auth , provider , config_location = DEFAULT_CONFIG_DIR ) : <EOL> """<STR_LIT>""" <EOL> auth_file = None <EOL> try : <EOL> if not os . path . exists ( config_location ) : <EOL> print ( "<STR_LIT>" <EOL> % config_location , end = "<STR_LIT>" ) <EOL> os . mkdir ( config_location ) <EOL> print ( "<STR_LIT>" ) <EOL> config_dir = os . path . join ( config_location , NOIPY_CONFIG ) <EOL> if not os . path . exists ( config_dir ) : <EOL> print ( "<STR_LIT>" % config_dir , end = "<STR_LIT>" ) <EOL> os . mkdir ( config_dir ) <EOL> print ( "<STR_LIT>" ) <EOL> auth_file = os . path . join ( config_dir , provider ) <EOL> print ( "<STR_LIT>" % auth_file , end = "<STR_LIT>" ) <EOL> with open ( auth_file , '<STR_LIT:w>' ) as f : <EOL> buff = auth . base64key . decode ( '<STR_LIT:utf-8>' ) <EOL> f . write ( buff ) <EOL> print ( "<STR_LIT>" ) <EOL> except IOError as e : <EOL> print ( '<STR_LIT>' . format ( e . strerror , auth_file ) ) <EOL> raise e <EOL> def load ( provider , config_location = DEFAULT_CONFIG_DIR ) : <EOL> """<STR_LIT>""" <EOL> auth = None <EOL> auth_file = None <EOL> try : <EOL> config_dir = os . path . join ( config_location , NOIPY_CONFIG ) <EOL> print ( "<STR_LIT>" % config_dir , end = "<STR_LIT>" ) <EOL> auth_file = os . path . join ( config_dir , provider ) <EOL> with open ( auth_file ) as f : <EOL> auth_key = f . read ( ) <EOL> auth = ApiAuth . get_instance ( auth_key . encode ( '<STR_LIT:utf-8>' ) ) <EOL> print ( "<STR_LIT>" ) <EOL> except IOError as e : <EOL> print ( '<STR_LIT>' . format ( e . strerror , auth_file ) ) <EOL> raise e <EOL> return auth <EOL> def exists ( provider , config_location = DEFAULT_CONFIG_DIR ) : <EOL> """<STR_LIT>""" <EOL> config_dir = os . path . join ( config_location , NOIPY_CONFIG ) <EOL> auth_file = os . path . join ( config_dir , provider ) <EOL> return os . path . exists ( auth_file ) </s>
