Browse files

Merge pull request #1 from toyg/patch-1

Generalized comment handling in the list of sites
  • Loading branch information...
2 parents 0c12cb2 + e43c40f commit f9915d117a5824cfebb47d856b8aaf66a0db7b09 @ParkerK committed Mar 14, 2012
Showing with 12 additions and 10 deletions.
  1. +12 −10 SelfRestraint.py
View
22 SelfRestraint.py
@@ -122,17 +122,19 @@ def startBlock(self):
hostsFile.write('# Block the following sites:\n')
blockedSites = list.tableView.toPlainText()
- blockedSites = blockedSites.split("\n")
-
+ # remove whitespace before and after
+ blockedSites = [ site.strip() for site in blockedSites.split("\n") ]
+ # filter out comments and empty rows
+ blockedSites = [ site for site in blockedSites if (not site.startsWith('#')) and site != '' ]
+ # write out
for sites in blockedSites:
- if sites != "# Add one website per line #" and len(sites)>2:
- hostsFile.write( "0.0.0.0\t"+sites+"\n" )
- temp = sites
- if sites.startsWith('www.'):
- temp = temp.split('www.')[1]
- hostsFile.write( "0.0.0.0\t"+temp+"\n" )
- else:
- hostsFile.write( "0.0.0.0\t"+"www."+sites+"\n" )
+ hostsFile.write( "0.0.0.0\t"+sites+"\n" )
+ temp = sites
+ if sites.startsWith('www.'):
+ temp = temp.split('www.')[1]
+ hostsFile.write( "0.0.0.0\t"+temp+"\n" )
+ else:
+ hostsFile.write( "0.0.0.0\t"+"www."+sites+"\n" )
hostsFile.write("# End Blocklist")
hostsFile.close()

0 comments on commit f9915d1

Please sign in to comment.