Review of domain_to_idna()
authorfunilrys <redacted>
Fri, 2 Mar 2018 21:07:11 +0000 (22:07 +0100)
committerfunilrys <redacted>
Fri, 2 Mar 2018 21:07:11 +0000 (22:07 +0100)
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.

updateHostsFile.py

index 0c78c3e2f5df92c615115e60faf4c57f4de74c30..29fa4e5b1b994fd7a9fa86b704fdb691a5ac3f6e 100644 (file)
@@ -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")
git clone https://git.99rst.org/PROJECT