<commit_before>#!/usr/bin/env python
import os
import platform
import sys
import re
import shutil
import urllib2


BLOCKHEAD = '###ClickBait HEAD###'
BLOCKTAIL = '###ClickBait TAIL###'
LINUX_HOSTPATH = '/etc/hosts'
WIN_HOSTPATH = '\\system32\\drivers\\etc\\hosts'
BASE_LIST = 'base.list'
SINKHOLE_IP = '127.0.1.1'
SPACER = '    '
SINKPREFIX = SINKHOLE_IP + SPACER
LIST_URL = 'https://raw.githubusercontent.com/EOA/nullclick/master/base.list'

local_os = platform.system()
host_file = ''
host_file_backup = ''

sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # Unbuffered IO for printing


def set_hostfile():
    """Set file to manipulate based off OS environment."""
    global host_file
    global host_file_backup
    if local_os.lower() == 'linux' or local_os.lower() == 'darwin':
        host_file = LINUX_HOSTPATH
        host_file_backup = '%s.backup' %  host_file
    elif local_os.lower() == 'windows':
        host_file = os.environ['WINDIR'] + WIN_HOSTPATH
        host_file_backup = '%s.backup' % host_file
    else:
        print "Unrecognized host OS"
        exit()


def menu_choice():
    """"Print options menu, take in user choice."""
    print """
1. Add site to block list.
2. Remove site from block list. 
3. Toggle site state.
4. Update block list.
5. Print all block list.
6. Exit

0. Install/Uninstall block list.
"""
    choice = ''
    valid = ('0','1', '2', '3', '4', '5', '6')
    while choice not in valid:
        try:
            choice = raw_input('#: ')
        except:
            choice = ''
        if choice not in valid:
            print "invalid choice \n"            
    return int(choice)


def launcher(choice):
    """"Take in int choice, use dict as switch to call function."""
    options = {0 : install_uninstall,
               1 : add_site,
               2 : remove_site,
               3 : toggle_site,
               4 : update_list,
               5 : print_list,
               6 : exit
               }
    options[choice]()


def install_uninstall(**kwargs):
    """Check for block list headers, if present uninstall. If not, install."""
    list_present = check_list_init()
    choice = ''
    while choice not in ['yes', 'no']:
        if list_present:
            print "\n* Uninstall block list?"
        else:
            print "\n* Install block list?"
        choice = raw_input('yes/no ?: ').lower()
    if list_present and choice == 'yes':
        remove_list()
    elif choice == 'yes':
        initialize_list()
    else:
        return


def check_list_init():
    try:
        list_state = BLOCKHEAD in open(host_file).read()
    except IOError as e:
        print e.args
        exit()
    return list_state


def backup_hostfile():
    if os.path.exists(host_file_backup):
        import filecmp
        cmp_result = filecmp.cmp(host_file, host_file_backup)
        list_installed = check_list_init()
        if not cmp_result and not list_installed:
            shutil.copyfile(host_file, host_file_backup)
    else:
        try:
            shutil.copy2(host_file, host_file_backup)
        except IOError as e:
            print 'Unable to back up hosts file: \n%s' % e.args
            exit()


def initialize_list():
    """Insert Block List header and footer into host file, propagate base list."""
    backup_hostfile()
    try:
        with open(host_file, 'a') as hostf:
            hostf.write(BLOCKHEAD + '\n' + BLOCKTAIL + '\n')
    except IOError as e:
        print e.args
        exit()
    print "\n* Block list headers installed"         
    print "* Initializing block list"
    push_site(file_to_list(BASE_LIST))        


def file_to_list(file_path):
    domain_list = []
    try:  
        with open(file_path, 'r') as listfile:    
            for site in listfile: 
                domain_list.append(site)
    except IOError as e:
            print e.args<commit_msg>host_file_backup global removed. Unneeded altered conditionals for readability / platform separation added OSX_HOSTFILE const docstring for backup_hostfile() docstring for check_list_init() check_list_init() return inside try block to eliminate construction of list_state var refactor check_list_init() and associated calls to bool naming: "is_list_present()"<commit_after>#!/usr/bin/env python
import os
import platform
import sys
import re
import shutil
import urllib2


BLOCKHEAD = '###ClickBait HEAD###'
BLOCKTAIL = '###ClickBait TAIL###'
LINUX_HOSTPATH = '/etc/hosts'
WIN_HOSTPATH = '\\system32\\drivers\\etc\\hosts'
OSX_HOSTPATH = LINUX_HOSTPATH  
BASE_LIST = 'base.list'
SINKHOLE_IP = '127.0.1.1'
SPACER = '    '
SINKPREFIX = SINKHOLE_IP + SPACER
LIST_URL = 'https://raw.githubusercontent.com/EOA/nullclick/master/base.list'

local_os = platform.system()
host_file = ''
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # Unbuffered IO for printing


def set_hostfile():
    """Set file to manipulate based off OS environment."""
    global host_file
    if local_os.lower() == 'linux':
        host_file = LINUX_HOSTPATH
    elif local_os.lower() == 'darwin':
        host_file = OSX_HOSTPATH
    elif local_os.lower() == 'windows':
        host_file = os.environ['WINDIR'] + WIN_HOSTPATH
    else:
        print "Unrecognized host OS"
        exit()


def menu_choice():
    """"Print options menu, take in user choice."""
    print """
1. Add site to block list.
2. Remove site from block list. 
3. Toggle site state.
4. Update block list.
5. Print all block list.
6. Exit

0. Install/Uninstall block list.
"""
    choice = ''
    valid = ('0','1', '2', '3', '4', '5', '6')
    while choice not in valid:
        try:
            choice = raw_input('#: ')
        except:
            choice = ''
        if choice not in valid:
            print "invalid choice \n"            
    return int(choice)


def launcher(choice):
    """"Take in int choice, use dict as switch to call function."""
    options = {0 : install_uninstall,
               1 : add_site,
               2 : remove_site,
               3 : toggle_site,
               4 : update_list,
               5 : print_list,
               6 : exit
               }
    options[choice]()


def install_uninstall(**kwargs):
    """Check for block list headers, if present uninstall. If not, install."""
    list_present = is_list_present()
    choice = ''
    while choice not in ['yes', 'no']:
        if list_present:
            print "\n* Uninstall block list?"
        else:
            print "\n* Install block list?"
        choice = raw_input('yes/no ?: ').lower()
    if list_present and choice == 'yes':
        remove_list()
    elif choice == 'yes':
        initialize_list()
    else:
        return


def is_list_present():
    """Return Bool of list presence."""
    try:
        return BLOCKHEAD in open(host_file).read()
    except IOError as e:
        print e.args
        exit()


def backup_hostfile(): #TODO: Review and add call in initialize_list or 
    """Backups up host file before injection of block list. Called from initialize_list()"""
    host_file_backup = '%s.backup' % host_file
    if os.path.exists(host_file_backup):
        import filecmp
        cmp_result = filecmp.cmp(host_file, host_file_backup)
        list_installed = is_list_present()
        if not cmp_result and not list_installed:
            shutil.copyfile(host_file, host_file_backup)
    else:
        try:
            shutil.copy2(host_file, host_file_backup)
        except IOError as e:
            print 'Unable to back up hosts file: \n%s' % e.args
            exit()


def initialize_list():
    """Insert Block List header and footer into host file, propagate base list."""
    # backup_hostfile() TODO: Add user prompting for back up, append date to hostfile name
    try:
        with open(host_file, 'a') as hostf:
            hostf.write(BLOCKHEAD + '\n' + BLOCKTAIL + '\n')
    except IOError as e:
        print e.args
        exit()
    print "\n* Block list headers installed"         
    print "* Initializing block list"
    push_site(file_to_list(BASE_LIST))        


def file_to_list(file_path):
    domain_list = []
    try:  
        with open(file_path, 'r') as listfile:    
            for site in listfile: 
                domain_list.append(site)
    except IOError as e:
            print e.args
{'fraction_non_alphanumeric': 0.11108424996978121, 'fraction_numerical': 0.007736008703009791, 'mean_word_length': 2.3675213675213675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2581566', 'n_tokens_mistral': 2596, 'n_tokens_neox': 2511, 'n_words': 772}