mirror of
https://github.com/StevenBlack/hosts.git
synced 2026-07-01 02:36:52 +00: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:
@@ -1161,27 +1161,25 @@ def domain_to_idna(line):
|
||||
"""
|
||||
|
||||
if not line.startswith('#'):
|
||||
for separator in [' ', '\t']:
|
||||
comment_to_append = ''
|
||||
for separator in ['\t', ' ']:
|
||||
comment = ''
|
||||
|
||||
if separator in line:
|
||||
splited_line = line.split(separator)
|
||||
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] \
|
||||
.split(comment_to_append)[0] \
|
||||
.split(comment)[0] \
|
||||
.encode("IDNA").decode("UTF-8") + \
|
||||
'#' + comment_to_append[1]
|
||||
else:
|
||||
splited_line[1] = splited_line[1] \
|
||||
.encode("IDNA") \
|
||||
.decode("UTF-8")
|
||||
else:
|
||||
splited_line[1] = splited_line[1] \
|
||||
.encode("IDNA") \
|
||||
.decode("UTF-8")
|
||||
comment
|
||||
|
||||
splited_line[1] = splited_line[1] \
|
||||
.encode("IDNA") \
|
||||
.decode("UTF-8")
|
||||
return separator.join(splited_line)
|
||||
return line.encode("IDNA").decode("UTF-8")
|
||||
return line.encode("UTF-8").decode("UTF-8")
|
||||
|
||||
Reference in New Issue
Block a user