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

Commit e0eb323

Browse files
committed
restored extras= config file functions
1 parent 2ec542b commit e0eb323

3 files changed

Lines changed: 57 additions & 40 deletions

File tree

CloudFlare/api_extras.py

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,61 @@
22

33
import re
44

5+
from .exceptions import CloudFlareAPIError
6+
57
def api_extras(self, extras=None):
68
""" API extras for Cloudflare API"""
79

10+
count = 0;
811
for extra in extras:
9-
if extra == '':
10-
continue
1112
extra = re.sub(r"^.*/client/v4/", '/', extra)
1213
extra = re.sub(r"^.*/v4/", '/', extra)
1314
extra = re.sub(r"^/", '', extra)
15+
if extra == '':
16+
continue
1417

1518
# build parts of the extra command
1619
parts = []
17-
nn = 0
20+
part = None
1821
for element in extra.split('/'):
1922
if element[0] == ':':
20-
nn += 1
21-
continue
22-
try:
23-
parts[nn]
24-
except IndexError:
25-
parts.append([])
26-
parts[nn].append(element)
27-
28-
# insert extra command into class
29-
element_path = []
30-
current = self
31-
for element in parts[0]:
32-
element_path.append(element)
33-
try:
34-
m = getattr(current, element)
35-
# exists - but still add it there's a second part
36-
if element == parts[0][-1] and len(parts) > 1:
37-
api_call_part1 = '/'.join(element_path)
38-
api_call_part2 = '/'.join(parts[1])
39-
setattr(m, parts[1][0],
40-
self._AddWithAuth(self._base, api_call_part1, api_call_part2))
41-
current = m
23+
parts.append(part)
24+
part = None
4225
continue
43-
except:
44-
pass
45-
# does not exist
46-
if element == parts[0][-1] and len(parts) > 1:
47-
# last element
48-
api_call_part1 = '/'.join(element_path)
49-
api_call_part2 = '/'.join(parts[1])
50-
setattr(current, element,
51-
self._AddWithAuth(self._base, api_call_part1, api_call_part2))
26+
if part:
27+
part += '/' + element
5228
else:
53-
api_call_part1 = '/'.join(element_path)
54-
setattr(current, element,
55-
self._AddWithAuth(self._base, api_call_part1))
56-
current = getattr(current, element)
29+
part = element
30+
if part:
31+
parts.append(part)
32+
33+
if len(parts) > 1:
34+
p = parts[1].split('/')
35+
for nn in range(0, len(p)):
36+
try:
37+
self.add('VOID', parts[0], '/'.join(p[0:nn]))
38+
except CloudFlareAPIError:
39+
# already exists - this is ok
40+
pass
41+
42+
if len(parts) > 2:
43+
p = parts[2].split('/')
44+
for nn in range(0, len(p)):
45+
try:
46+
self.add('VOID', parts[0], parts[1], '/'.join(p[0:nn]))
47+
except CloudFlareAPIError:
48+
# already exists - this is ok
49+
pass
50+
51+
while len(parts) < 3:
52+
parts.append(None)
53+
54+
# we can only add AUTH elements presently
55+
try:
56+
self.add('AUTH', parts[0], parts[1], parts[2])
57+
count += 1
58+
except CloudFlareAPIError:
59+
# this is silently dropped - however, that could change
60+
pass
61+
62+
return count

CloudFlare/cloudflare.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,14 @@ def add(self, t, p1, p2=None, p3=None):
814814
raise CloudFlareAPIError(0, 'api load name failed')
815815
name = a[-1]
816816

817+
try:
818+
f = getattr(branch, name)
819+
# already exists - don't let it overwrite
820+
raise CloudFlareAPIError(0, 'api duplicate name found: %s/**%s**' % ('/'.join(a[0:-1]), name))
821+
except AttributeError:
822+
# this is the required behavior - i.e. it's a new node to create
823+
pass
824+
817825
if t == 'VOID':
818826
f = self._AddUnused(self._base, p1, p2, p3)
819827
elif t == 'OPEN':

CloudFlare/read_configs.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ def read_configs(profile=None):
4545
for option in ['email', 'token', 'certtoken', 'extras']:
4646
if option not in config or config[option] is None:
4747
try:
48-
config[option] = re.sub(r"\s+", '', section.get(option))
48+
if option == 'extras':
49+
config[option] = re.sub(r"\s+", ' ', section.get(option))
50+
else:
51+
config[option] = re.sub(r"\s+", '', section.get(option))
4952
if config[option] == '':
5053
config.pop(option)
5154
except (configparser.NoOptionError, configparser.NoSectionError):
@@ -66,7 +69,7 @@ def read_configs(profile=None):
6669

6770
# do any final cleanup - only needed for extras (which are multiline)
6871
if 'extras' in config and config['extras'] is not None:
69-
config['extras'] = config['extras'].split(' ')
72+
config['extras'] = config['extras'].strip().split(' ')
7073

7174
# remove blank entries
7275
for x in sorted(config.keys()):

0 commit comments

Comments
 (0)