~pyneighborhood/pyneighborhood/0.5

« back to all changes in this revision

Viewing changes to addconfig.py

  • Committer: definer
  • Date: 2006-09-16 18:34:46 UTC
  • Revision ID: svn-v3-trunk0:918a6f1d-dd1c-0410-8c33-97c2c1a26c01:trunk:6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# writes data to the configuration files 'hosts' and 'options'
 
4
 
 
5
import os
 
6
import string
 
7
from os import system, environ, path, getenv, mkdir, getcwd
 
8
 
 
9
def addhost(hostname):
 
10
    if not path.exists(getenv("HOME")+"/.pyNeighborhood"): 
 
11
        mkdir(getenv("HOME")+"/.pyNeighborhood")
 
12
 
 
13
    if path.exists(getenv("HOME")+"/.pyNeighborhood"): 
 
14
        home = getenv("HOME")
 
15
        hostslocation = string.join([home, "/.pyNeighborhood"], '')
 
16
        if not hostname == None:
 
17
            os.chdir(hostslocation)
 
18
            hostfile = open('hosts', 'a')
 
19
            hostfile.write(hostname)
 
20
            hostfile.write("\n")
 
21
            hostfile.close()
 
22
        return
 
23
 
 
24
def rmhost(hostname):
 
25
    # check if ~/.pyNeighborhood exists. If not, create it
 
26
 
 
27
    if not path.exists(getenv("HOME")+"/.pyNeighborhood"): 
 
28
        mkdir(getenv("HOME")+"/.pyNeighborhood")
 
29
        
 
30
    if path.exists(getenv("HOME")+"/.pyNeighborhood"): 
 
31
        home = getenv("HOME")
 
32
        hostslocation = string.join([home, "/.pyNeighborhood"], '')
 
33
        os.chdir(hostslocation)
 
34
        print os.getcwd()
 
35
                
 
36
        #check if file containing a list of favourite hosts     exists. If not, create it
 
37
        hostfile = open('hosts', 'a')
 
38
        hostfile.close()
 
39
 
 
40
        #read hosts from file and create a standart list of them                
 
41
        hostfile = open("hosts", 'r')
 
42
        hostfile.seek(0)
 
43
        list = hostfile.readlines()
 
44
        print list
 
45
        hostlist = []
 
46
        i = 0
 
47
        for number in list:
 
48
            line = list[i]
 
49
            hostline = line.split("\n")
 
50
            hostlist.append(hostline[0])
 
51
            i = i + 1
 
52
 
 
53
        #remove host from list
 
54
        hostlist.remove(hostname)
 
55
        print hostlist
 
56
        hostfile.close()
 
57
 
 
58
        #Clear the hostfile contents
 
59
        hostfile = open('hosts', 'w')
 
60
        hostfile.close()
 
61
        i=0
 
62
        #Write edited contents
 
63
        for number in hostlist: 
 
64
            addhost(hostlist[i])
 
65
            i=i+1
 
66
 
 
67
        return
 
68