import json
import os
from pprint import pprint


CFG_F_NAME = 'key.json'
KEY_APPID = 'AppID'
KEY_APPKEY = 'AppKey'
KEY_MASTERKEY = 'MasterKey'

key_set = None

def read_key():

    file_path = os.path.join(os.getcwd(), CFG_F_NAME)
    print(file_path)
    with open(CFG_F_NAME) as data_file:
        key = json.load(data_file)
        id = key[KEY_APPID]
        appkey = key[KEY_APPKEY]
        mkey = key[KEY_MASTERKEY]
        # pprint(key)
        print(mkey)
        key_set = key
    return key

def get_app_key():
    return read_key()[KEY_APPKEY]


def get_app_id():
    return read_key()[KEY_APPID]


def get_master_key():
    return key_set[KEY_MASTERKEY]


def write_cfg_file():
    cfg_fname = os.path.join(os.getcwd(), CFG_F_NAME)
    if os.path.isfile(cfg_fname):
        return
    cfg = {KEY_APPID: '', KEY_APPKEY: '', KEY_MASTERKEY: ''}
    with open(cfg_fname, 'w') as f:
        json.dump(cfg, f)


# read_key()
@@ -18,35 +18,17 @@
 t os
-%0Afrom pprint import pprint%0A
+, inspect
 %0A%0ACF
@@ -124,23 +124,8 @@
 y'%0A%0A
-key_set = None%0A
 %0Adef
@@ -141,16 +141,18 @@
 ():%0A%0A   
+ #
  file_pa
@@ -202,258 +202,375 @@
     
-print(file_path)%0A    with open(CFG_F_NAME) as data_file:%0A        key = json.load(data_file)%0A        id = key%5BKEY_APPID%5D%0A        appkey = key%5BKEY_APPKEY%5D%0A        mkey = key%5BKEY_MASTERKEY%5D%0A        # pprint(key)%0A        print(mkey)%0A        key_set = key
+# the os.getcwd return the path of the scrip of the execute%0A    dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))%0A    print('dir is --%3E', dir)%0A    file_path = os.path.join(dir, CFG_F_NAME)%0A    print(file_path)%0A    with open(file_path) as data_file:%0A        key = json.load(data_file)%0A        mkey = key%5BKEY_MASTERKEY%5D%0A        print(mkey)
 %0A   
@@ -582,16 +582,17 @@
 rn key%0A%0A
+%0A
 def get_
@@ -728,15 +728,18 @@
 urn 
-key_set
+read_key()
 %5BKEY
@@ -1006,16 +1006,4 @@
 )%0A%0A%0A
-# read_key()

optimize codes of cfg and get the right patch