-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
169 lines (137 loc) · 4.05 KB
/
Copy pathutil.py
File metadata and controls
169 lines (137 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import socket
class NetInfo:
TEST_SITE = 'www.google.com'
TEST_PORT = 80
@staticmethod
def GetLocalIP():
""" Get the external IP address of the local machine
Test connection to a test site
Then retrieve the local IP address through the socket name
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect((NetInfo.TEST_SITE, NetInfo.TEST_PORT))
local_ip = s.getsockname()[0]
except socket.error:
local_ip = None
finally:
del s
return local_ip
@staticmethod
def GetLocalDomainName():
""" Get the domain name of the local machine
First the external IP address is obtained through GetLocalIP() function
Then the domain name is obtained through the socket.gethostbyaddr() function
"""
myip = NetInfo.GetLocalIP()
try:
name = socket.gethostbyaddr(myip)[0]
except socket.herror:
return None
return name
from twisted.python import log
class Log:
""" Log class for logging
Message, Error, Data, StartLogging
"""
@staticmethod
def Msg(*msg):
text = '[MSG] ' + ' '.join(msg)
log.msg(text)
@staticmethod
def Err(*msg):
text = '[ERR] ' + ' '.join(msg)
log.msg(text)
@staticmethod
def Data(*msg):
text = '[DATA] ' + ' '.join(msg)
log.msg(text)
@staticmethod
def StartLogging(fd):
log.startLogging(fd)
from subscription import Subscription
class SubFile:
""" Subscription data file
Format:
[subscription source] [subscription]
...
Each subscription source corresponds to one subscription
Each peer records multiple subscriptions for each subscription source
"""
def __init__(self, fname):
self.fname = fname
def Open(self, fname):
self.fname = fname
def Get(self):
lines = []
try:
f = open(self.fname, 'r')
except IOError:
Log.Err('Cannot open file', self.fname)
else:
lines = [line.strip() for line in f if line.strip() != '']
f.close()
res = {}
for line in lines:
dname, sub = line.split()
if res.has_key(dname):
res[dname] = [Subscription(sub)]
else:
res[dname].append(Subscription(sub))
return res
def Append(self, src, sub):
# write data into subscription file
f = open(self.fname, 'a')
f.write(' '.join([src, sub]))
f.write('\n')
f.close()
def AppendMultiple(self, pairs):
# write multiple (src, sub) to the file
f = open(self.fname, 'a')
for pair in pairs:
f.write(' '.join(pair))
f.write('\n')
f.close()
class PeerListFile:
""" Process peer list file
A file has the format:
[peer domain name]
"""
def __init__(self, fname):
self.fname = fname
def Open(self, fname):
self.fname = fname
def Get(self):
lines = []
try:
f = open(self.fname, 'r')
except IOError:
Log.Err('Cannot open file', self.fname)
else:
lines = [line.strip() for line in f if line.strip() != '']
f.close()
return lines
from time import time
def AppendTimeStamp(data, now = None):
if now is None:
now = repr(time())
return data + '|' + now
def ComputeDelay(data):
time_stamp = float(data.split('|', 2)[1])
now = time()
return now - time_stamp
if __name__ == '__main__':
print NetInfo.GetLocalIP()
print NetInfo.GetLocalDomainName()
print NetInfo.TEST_SITE
print NetInfo.TEST_PORT
import sys
Log.StartLogging(sys.stdout)
Log.Msg('test')
Log.Err('test')
plfile = PeerListFile('nodes')
print 'nodes file content'
print plfile.Get()
subfile = SubFile('nodes')
subfile.Append('aaa', 'bbb')
subfile.AppendMultiple([('1','11'), ('2', '22')])