Skip to content

Commit b21ac81

Browse files
Refactor __netjson_redirect() to reduce complexity
1 parent 9aff7a1 commit b21ac81

1 file changed

Lines changed: 45 additions & 28 deletions

File tree

netjsonconfig/backends/openwrt/converters/firewall.py

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -199,38 +199,17 @@ def __netjson_forwarding(self, forwarding):
199199

200200
def __netjson_redirect(self, redirect):
201201
if "proto" in redirect:
202-
proto = redirect.pop("proto")
203-
if not isinstance(proto, list):
204-
if proto == "tcpudp":
205-
redirect["proto"] = ["tcp", "udp"]
206-
else:
207-
redirect["proto"] = [proto]
202+
redirect["proto"] = self.__netjson_redirect_proto(redirect["proto"])
208203

209204
if "weekdays" in redirect:
210-
weekdays = redirect["weekdays"]
211-
if not isinstance(weekdays, list):
212-
weekdays = weekdays.split()
213-
# UCI allows the first entry to be "!" which means negate the remaining
214-
# entries
215-
if weekdays[0] == "!":
216-
all_days = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
217-
wd = set([x for x in weekdays[1:]])
218-
redirect["weekdays"] = list(set(all_days) - wd)
219-
# Sort the days for predictability when testing
220-
redirect["weekdays"].sort(key=lambda v: all_days.index(v))
205+
redirect["weekdays"] = self.__netjson_redirect_weekdays(
206+
redirect["weekdays"]
207+
)
221208

222209
if "monthdays" in redirect:
223-
monthdays = redirect["monthdays"]
224-
if not isinstance(monthdays, list):
225-
monthdays = monthdays.split()
226-
# UCI allows the first entry to be "!" which means negate the remaining
227-
# entries
228-
if monthdays[0] == "!":
229-
all_days = set(range(1, 32))
230-
md = set([int(x) for x in monthdays[1:]])
231-
redirect["monthdays"] = list(all_days - md)
232-
else:
233-
redirect["monthdays"] = [int(x) for x in monthdays]
210+
redirect["monthdays"] = self.__netjson_redirect_monthdays(
211+
redirect["monthdays"]
212+
)
234213

235214
if "utc_time" in redirect:
236215
redirect["utc_time"] = redirect["utc_time"] == "1"
@@ -245,3 +224,41 @@ def __netjson_redirect(self, redirect):
245224
redirect["enabled"] = redirect["enabled"] == "1"
246225

247226
return self.type_cast(redirect)
227+
228+
def __netjson_redirect_proto(self, proto):
229+
if isinstance(proto, list):
230+
return proto.copy()
231+
else:
232+
if proto == "tcpudp":
233+
return ["tcp", "udp"]
234+
else:
235+
return proto.split()
236+
237+
def __netjson_redirect_weekdays(self, weekdays):
238+
if not isinstance(weekdays, list):
239+
wd = weekdays.split()
240+
else:
241+
wd = weekdays.copy()
242+
243+
# UCI allows the first entry to be "!" which means negate the remaining entries
244+
if wd[0] == "!":
245+
all_days = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
246+
wd = [day for day in all_days if day not in wd[1:]]
247+
248+
return wd
249+
250+
def __netjson_redirect_monthdays(self, monthdays):
251+
if not isinstance(monthdays, list):
252+
md = monthdays.split()
253+
else:
254+
md = monthdays.copy()
255+
256+
# UCI allows the first entry to be "!" which means negate the remaining entries
257+
if md[0] == "!":
258+
md = [int(day) for day in md[1:]]
259+
all_days = range(1, 32)
260+
md = [day for day in all_days if day not in md]
261+
else:
262+
md = [int(day) for day in md]
263+
264+
return md

0 commit comments

Comments
 (0)