|
| 1 | +# coding=utf8 |
| 2 | + |
| 3 | +# Copyright 2018 JDCLOUD.COM |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http:#www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import os |
| 18 | +from configparser import SafeConfigParser |
| 19 | + |
| 20 | +CONF_AK = 'access_key' |
| 21 | +CONF_SK = 'secret_key' |
| 22 | +CONF_REGION = 'region_id' |
| 23 | +CONF_ENDPOINT = 'endpoint' |
| 24 | +CONF_SCHEME = 'scheme' |
| 25 | + |
| 26 | + |
| 27 | +class Config(object): |
| 28 | + |
| 29 | + def __init__(self, access_key, secret_key, region_id, endpoint, scheme): |
| 30 | + self.access_key = access_key |
| 31 | + self.secret_key = secret_key |
| 32 | + self.region_id = region_id |
| 33 | + self.endpoint = endpoint |
| 34 | + self.scheme = scheme |
| 35 | + |
| 36 | + |
| 37 | +class ProfileManager(object): |
| 38 | + |
| 39 | + def __new__(type): |
| 40 | + if '_instance' not in type.__dict__: |
| 41 | + type._instance = object.__new__(type) |
| 42 | + return type._instance |
| 43 | + |
| 44 | + def __init__(self): |
| 45 | + cli_dir = self.__make_config_dir() |
| 46 | + self.__config_file = cli_dir + '/config' |
| 47 | + self.__current_file = cli_dir + '/current' |
| 48 | + self.__current_profile_name = self.__get_current_profile_name() |
| 49 | + self.__parser = SafeConfigParser() |
| 50 | + self.__parser.read(self.__config_file) |
| 51 | + |
| 52 | + def load_current_profile(self): |
| 53 | + if self.__current_profile_name not in self.__parser.sections(): |
| 54 | + print 'Please use `jdc configure add` command to add cli configure first.' |
| 55 | + return None |
| 56 | + |
| 57 | + # notice that the type of config info which read from file is unicode |
| 58 | + # python SDK uses type str |
| 59 | + access_key = str(self.__parser.get(self.__current_profile_name, CONF_AK)) |
| 60 | + secret_key = str(self.__parser.get(self.__current_profile_name, CONF_SK)) |
| 61 | + region_id = str(self.__parser.get(self.__current_profile_name, CONF_REGION)) |
| 62 | + endpoint = str(self.__parser.get(self.__current_profile_name, CONF_ENDPOINT)) |
| 63 | + scheme = str(self.__parser.get(self.__current_profile_name, CONF_SCHEME)) |
| 64 | + return Config(access_key, secret_key, region_id, endpoint, scheme) |
| 65 | + |
| 66 | + def get_all_profiles(self): |
| 67 | + result = dict() |
| 68 | + sections = self.__parser.sections() |
| 69 | + for section in sections: |
| 70 | + result[section] = self.__parser.items(section) |
| 71 | + return result |
| 72 | + |
| 73 | + def get_current_profile(self): |
| 74 | + result = dict() |
| 75 | + if not self.__parser.has_section(self.__current_profile_name): |
| 76 | + return result |
| 77 | + |
| 78 | + result[self.__current_profile_name] = self.__parser.items(self.__current_profile_name) |
| 79 | + |
| 80 | + return result |
| 81 | + |
| 82 | + def add_profile(self, profile, config): |
| 83 | + if self.__parser.has_section(profile): |
| 84 | + return False, 'Profile ' + profile + ' has existed' |
| 85 | + |
| 86 | + self.__parser.add_section(profile) |
| 87 | + |
| 88 | + # use enums for the fields order |
| 89 | + for key in [CONF_AK, CONF_SK, CONF_REGION, CONF_ENDPOINT, CONF_SCHEME]: |
| 90 | + value = config.__dict__[key] |
| 91 | + self.__parser.set(profile, key, value) |
| 92 | + |
| 93 | + return self.__write_config_file() |
| 94 | + |
| 95 | + def delete_profile(self, profile): |
| 96 | + if not self.__parser.has_section(profile): |
| 97 | + return False, 'No profile ' + profile |
| 98 | + |
| 99 | + if profile == self.__current_profile_name: |
| 100 | + return False, 'Can not delete current profile: ' + profile |
| 101 | + |
| 102 | + self.__parser.remove_section(profile) |
| 103 | + return self.__write_config_file() |
| 104 | + |
| 105 | + def set_current_profile(self, name): |
| 106 | + if not self.__parser.has_section(name): |
| 107 | + return False, 'Profile %s do not exist! Configure failed!' % name |
| 108 | + try: |
| 109 | + with open(self.__current_file, 'w') as f: |
| 110 | + f.write(name) |
| 111 | + self.__current_profile_name = name |
| 112 | + except Exception as e: |
| 113 | + return False, e.message |
| 114 | + return True, '' |
| 115 | + |
| 116 | + def __write_config_file(self): |
| 117 | + try: |
| 118 | + with open(self.__config_file, 'w+') as f: |
| 119 | + self.__parser.write(f) |
| 120 | + except Exception as e: |
| 121 | + return False, e.message |
| 122 | + return True, '' |
| 123 | + |
| 124 | + def __make_config_dir(self): |
| 125 | + home = os.path.expanduser("~") |
| 126 | + cli_dir = home + "/.jdc" |
| 127 | + if not os.path.exists(cli_dir): |
| 128 | + os.makedirs(cli_dir) |
| 129 | + return cli_dir |
| 130 | + |
| 131 | + def __get_current_profile_name(self): |
| 132 | + if not os.path.exists(self.__current_file): |
| 133 | + with open(self.__current_file, 'w') as f: |
| 134 | + f.write('default') |
| 135 | + return 'default' |
| 136 | + |
| 137 | + with open(self.__current_file, 'r') as f: |
| 138 | + name = f.read() |
| 139 | + return name |
0 commit comments