__author__ = 'Chameleon'

from Tkinter import *
import os

def gethostfile():
    hpath = os.environ['WINDIR']
    hpath = hpath + '\\System32\\drivers\\etc\\hosts'
    o = open(hpath,'r')
    data = o.readlines()
    for line in data:
        if line[0]=='#':
            pass
        else:
            List.insert(END,line)
    o.close()

def writeinhostfile():
    hpath = os.environ['WINDIR']
    hpath = hpath + '\\System32\\drivers\\etc\\hosts'
    o = open(hpath,'a')
    o.write('127.0.0.1 ' + HostE.get() + '\n')
    o.close()
    HostE.delete(0,END)
    List.delete(0,END)
    gethostfile()

main = Tk()
main.title('Host Blocker v1.0')
main.resizable(0,0)

HostL = Label(main,text='Host IP adress')
HostE = Entry(main)
AddB = Button(main,text='Block',height=1,width=10,command=lambda: writeinhostfile())

Line = Label(main,text='---------------------------\n---------------------------\n')

List = Listbox(main,width=48,height=10)

Line.grid(column=0,row=1,pady=10,columnspan=3)

gethostfile()

HostL.grid(column=0,row=0,padx=2)
HostE.grid(column=1,row=0,padx=8)
AddB.grid(column=2,row=0,padx=8)

List.grid(column=0,row=2,pady=10,columnspan=3)

main.mainloop()