| Conditions | 14 |
| Total Lines | 75 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like CLIConfigParser.parse() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
| 137 | def parse(self): |
||
| 138 | """ |
||
| 139 | Parse the config and return a dict with the parsed values. |
||
| 140 | |||
| 141 | :rtype: ``dict`` |
||
| 142 | """ |
||
| 143 | result = defaultdict(dict) |
||
| 144 | |||
| 145 | if not os.path.isfile(self.config_file_path): |
||
| 146 | # Config doesn't exist, return the default values |
||
| 147 | return CONFIG_DEFAULT_VALUES |
||
| 148 | |||
| 149 | config_dir_path = os.path.dirname(self.config_file_path) |
||
| 150 | |||
| 151 | if self.validate_config_permissions: |
||
| 152 | # Make sure the directory permissions == 0o770 |
||
| 153 | if bool(os.stat(config_dir_path).st_mode & 0o777 ^ 0o770): |
||
| 154 | self.LOG.warn( |
||
| 155 | # TODO: Perfect place for an f-string |
||
| 156 | "The StackStorm configuration directory permissions are " |
||
| 157 | "insecure (too permissive)." |
||
| 158 | "\n\n" |
||
| 159 | "You can fix this by running:" |
||
| 160 | "\n\n" |
||
| 161 | " chmod 770 {config_dir}\n".format(config_dir=config_dir_path)) |
||
| 162 | |||
| 163 | # Make sure the setgid bit is set on the directory |
||
| 164 | if not bool(os.stat(config_dir_path).st_mode & 0o2000): |
||
| 165 | self.LOG.info( |
||
| 166 | # TODO: Perfect place for an f-string |
||
| 167 | "The SGID bit is not set on the StackStorm configuration " |
||
| 168 | "directory." |
||
| 169 | "\n\n" |
||
| 170 | "You can fix this by running:" |
||
| 171 | "\n\n" |
||
| 172 | " chmod g+s {config_dir}\n".format(config_dir=config_dir_path)) |
||
| 173 | |||
| 174 | # Make sure the file permissions == 0o660 |
||
| 175 | if bool(os.stat(self.config_file_path).st_mode & 0o777 ^ 0o660): |
||
| 176 | self.LOG.warn( |
||
| 177 | # TODO: Another perfect place for an f-string |
||
| 178 | "The StackStorm configuration file permissions are insecure." |
||
| 179 | "\n\n" |
||
| 180 | "You can fix this by running:" |
||
| 181 | "\n\n" |
||
| 182 | " chmod 660 {config_file}\n".format(config_file=self.config_file_path)) |
||
| 183 | |||
| 184 | config = ConfigParser() |
||
| 185 | with io.open(self.config_file_path, 'r', encoding='utf8') as fp: |
||
| 186 | config.readfp(fp) |
||
| 187 | |||
| 188 | for section, keys in six.iteritems(CONFIG_FILE_OPTIONS): |
||
| 189 | for key, options in six.iteritems(keys): |
||
| 190 | key_type = options['type'] |
||
| 191 | key_default_value = options['default'] |
||
| 192 | |||
| 193 | if config.has_option(section, key): |
||
| 194 | if key_type in ['str', 'string']: |
||
| 195 | get_func = config.get |
||
| 196 | elif key_type in ['int', 'integer']: |
||
| 197 | get_func = config.getint |
||
| 198 | elif key_type in ['float']: |
||
| 199 | get_func = config.getfloat |
||
| 200 | elif key_type in ['bool', 'boolean']: |
||
| 201 | get_func = config.getboolean |
||
| 202 | else: |
||
| 203 | msg = 'Invalid type "%s" for option "%s"' % (key_type, key) |
||
| 204 | raise ValueError(msg) |
||
| 205 | |||
| 206 | value = get_func(section, key) |
||
| 207 | result[section][key] = value |
||
| 208 | else: |
||
| 209 | result[section][key] = key_default_value |
||
| 210 | |||
| 211 | return dict(result) |
||
| 212 |
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: