Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 0bc445a

Browse files
committed
read_config() now compatible with older and newer ConfigParser usage
1 parent 38ec20c commit 0bc445a

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

CloudFlare/read_configs.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,47 +31,50 @@ def read_configs(profile=None):
3131
except Exception as e:
3232
raise Exception("%s: configuration file error" % (profile))
3333

34+
if len(cp.sections()) == 0 and profile != None:
35+
# no config file and yet a config name provided - not acceptable!
36+
raise Exception("%s: configuration section provided however config file missing" % (profile))
37+
3438
# Is it CloudFlare or Cloudflare? (A legacy issue)
3539
if profile is None:
36-
if len(cp.sections()) > 0:
37-
if 'CloudFlare' in cp:
38-
profile = 'CloudFlare'
39-
if 'Cloudflare' in cp:
40-
profile = 'Cloudflare'
40+
if cp.has_section('CloudFlare'):
41+
profile = 'CloudFlare'
42+
if cp.has_section('Cloudflare'):
43+
profile = 'Cloudflare'
4144

4245
## still not found - then set to to CloudFlare for legacy reasons
4346
if profile == None:
4447
profile = "CloudFlare"
48+
4549
config['profile'] = profile
4650

4751
if len(cp.sections()) > 0:
4852
# we have a configuration file - lets use it
49-
try:
50-
# grab the section - as we will use it for all values
51-
section = cp[profile]
52-
except Exception as e:
53-
# however section name is missing - this is an error
54-
raise Exception("%s: configuration section missing" % (profile))
53+
54+
if not cp.has_section(profile):
55+
raise Exception("%s: configuration section missing - configuration file only has these sections: %s" % (profile, ','.join(cp.sections())))
5556

5657
for option in ['email', 'token', 'certtoken', 'extras', 'base_url']:
57-
if option not in config or config[option] is None:
58-
try:
59-
if option == 'extras':
60-
config[option] = re.sub(r"\s+", ' ', section.get(option))
61-
else:
62-
config[option] = re.sub(r"\s+", '', section.get(option))
63-
if config[option] == '':
64-
config.pop(option)
65-
except (configparser.NoOptionError, configparser.NoSectionError):
66-
pass
67-
except Exception as e:
68-
pass
58+
try:
59+
config_value = cp.get(profile, option)
60+
if option == 'extras':
61+
config[option] = re.sub(r"\s+", ' ', config_value)
62+
else:
63+
config[option] = re.sub(r"\s+", '', config_value)
64+
if config[option] == None or config[option] == '':
65+
config.pop(option)
66+
except (configparser.NoOptionError, configparser.NoSectionError):
67+
pass
68+
except Exception as e:
69+
pass
70+
6971
# do we have an override for specific calls? (i.e. token.post or email.get etc)
7072
for method in ['get', 'patch', 'post', 'put', 'delete']:
7173
option_for_method = option + '.' + method
7274
try:
73-
config[option_for_method] = re.sub(r"\s+", '', section.get(option_for_method))
74-
if config[option_for_method] == '':
75+
config_value = cp.get(profile, option_for_method)
76+
config[option_for_method] = re.sub(r"\s+", '', config_value)
77+
if config[option] == None or config[option] == '':
7578
config.pop(option_for_method)
7679
except (configparser.NoOptionError, configparser.NoSectionError) as e:
7780
pass

0 commit comments

Comments
 (0)