mirror of
https://github.com/StevenBlack/hosts.git
synced 2026-10-06 06:14:48 +01:00
Review of domain_to_idna()
This patch review the way we get the comment at the end of a line. I also did an application of DRY (Do not Repeat Yourself) and/or KISS (Keep It Simple, Stupid) by refactoring the 2 `else` statements into one line.
This commit is contained in:
+12
-14
@@ -1161,27 +1161,25 @@ def domain_to_idna(line):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
if not line.startswith('#'):
|
if not line.startswith('#'):
|
||||||
for separator in [' ', '\t']:
|
for separator in ['\t', ' ']:
|
||||||
comment_to_append = ''
|
comment = ''
|
||||||
|
|
||||||
if separator in line:
|
if separator in line:
|
||||||
splited_line = line.split(separator)
|
splited_line = line.split(separator)
|
||||||
if '#' in splited_line[1]:
|
if '#' in splited_line[1]:
|
||||||
comment_to_append = splited_line[1].split('#')[1]
|
index_comment = splited_line[1].find('#')
|
||||||
|
|
||||||
|
if index_comment > -1:
|
||||||
|
comment = splited_line[1][index_comment:]
|
||||||
|
|
||||||
if comment_to_append:
|
|
||||||
splited_line[1] = splited_line[1] \
|
splited_line[1] = splited_line[1] \
|
||||||
.split(comment_to_append)[0] \
|
.split(comment)[0] \
|
||||||
.encode("IDNA").decode("UTF-8") + \
|
.encode("IDNA").decode("UTF-8") + \
|
||||||
'#' + comment_to_append[1]
|
comment
|
||||||
else:
|
|
||||||
splited_line[1] = splited_line[1] \
|
splited_line[1] = splited_line[1] \
|
||||||
.encode("IDNA") \
|
.encode("IDNA") \
|
||||||
.decode("UTF-8")
|
.decode("UTF-8")
|
||||||
else:
|
|
||||||
splited_line[1] = splited_line[1] \
|
|
||||||
.encode("IDNA") \
|
|
||||||
.decode("UTF-8")
|
|
||||||
return separator.join(splited_line)
|
return separator.join(splited_line)
|
||||||
return line.encode("IDNA").decode("UTF-8")
|
return line.encode("IDNA").decode("UTF-8")
|
||||||
return line.encode("UTF-8").decode("UTF-8")
|
return line.encode("UTF-8").decode("UTF-8")
|
||||||
|
|||||||
Reference in New Issue
Block a user