-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.py
38 lines (32 loc) · 1.13 KB
/
tools.py
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
from __future__ import print_function
import socket
import time
import re
#---------------------------------------------------
def findInFile(fname, split, *search):
found = []
search = list(search)
with open(fname, "r") as f:
for l in f.readlines():
if len(search) == 0: break
w = re.sub(' +', ' ', l).split(split)
if len(w) > 1:
w = map(lambda x: x.replace(' ', ''), w)
for s in search:
if s in w:
found.append(re.sub(r'[\n"\']', '', w[w.index(s) + 1]))
search.remove(s)
break
return found[0] if len(found)==1 else found
#---------------------------------------------------
def findServerAddr(callback):
hostname, port = findInFile('./platformio.ini', '=', 'otaname', 'otaport')
hostname = '%s.local' % hostname
port = int(port)
print ('seek %s...' % hostname, end='')
try:
print('found')
ip = socket.gethostbyname(hostname)
callback((ip, port))
except socket.gaierror:
time.sleep(1)