<commit_before>from os import path
import socket
import subprocess
import re
import Onaeri.settings as settings
from Onaeri import helper

print("Getting network data from gateway: ", end="", flush=True)

from pytradfri import Gateway, error
from pytradfri.api.libcoap_api import APIFactory




def activeIp(ip):
    proc = subprocess.Popen("fping %s -r 2 2> /dev/null" % ip, stdout=subprocess.PIPE, shell=True)
    (ipState, err) = proc.communicate()
    ipState = str(ipState)

    if 'alive' in ipState:
        return True
    else:
        return False


def findGatewayIp():
    helper.printWarning("Attempting to find Gateway on local network.", end=" ", flush=True)

    def hostIp():
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect(("8.8.8.8", 80))
            ip = s.getsockname()[0]
            s.close()
            return ip
        except OSError:
            helper.printError("\nCouldn't find the host IP. Are you connected to the network?")
            exit()
    ip = hostIp()


    keyword = 'tradfri-gateway'

    # Update arp table with full network on current subnet
    subprocess.call("fping -c 1 -g %s/24 2> /dev/null | grep %s" % (ip, keyword), shell=True)

    # Read arp table and look for keyword
    proc = subprocess.Popen("arp -a | grep %s --ignore-case" % keyword, stdout=subprocess.PIPE, shell=True)
    (arpRecord, err) = proc.communicate()
    arpRecord = str(arpRecord)

    # Find ip4 address in arp record
    regex = re.compile('([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})')
    match = regex.search(arpRecord)
    if match is None:
        return None

    return arpRecord[match.start():match.end()]




filePath = path.dirname(path.abspath(__file__))

with open('%s/gateway_ip.txt' % filePath, 'r+') as file:
    file.seek(0)<commit_msg>Create gateway_ip.txt when not present<commit_after>from os import path
import socket
import subprocess
import re
import Onaeri.settings as settings
from Onaeri import helper

print("Getting network data from gateway: ", end="", flush=True)

from pytradfri import Gateway, error
from pytradfri.api.libcoap_api import APIFactory




def activeIp(ip):
    proc = subprocess.Popen("fping %s -r 2 2> /dev/null" % ip, stdout=subprocess.PIPE, shell=True)
    (ipState, err) = proc.communicate()
    ipState = str(ipState)

    if 'alive' in ipState:
        return True
    else:
        return False


def findGatewayIp():
    helper.printWarning("Attempting to find Gateway on local network.", end=" ", flush=True)

    def hostIp():
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect(("8.8.8.8", 80))
            ip = s.getsockname()[0]
            s.close()
            return ip
        except OSError:
            helper.printError("\nCouldn't find the host IP. Are you connected to the network?")
            exit()
    ip = hostIp()


    keyword = 'tradfri-gateway'

    # Update arp table with full network on current subnet
    subprocess.call("fping -c 1 -g %s/24 2> /dev/null | grep %s" % (ip, keyword), shell=True)

    # Read arp table and look for keyword
    proc = subprocess.Popen("arp -a | grep %s --ignore-case" % keyword, stdout=subprocess.PIPE, shell=True)
    (arpRecord, err) = proc.communicate()
    arpRecord = str(arpRecord)

    # Find ip4 address in arp record
    regex = re.compile('([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})')
    match = regex.search(arpRecord)
    if match is None:
        return None

    return arpRecord[match.start():match.end()]




filePath = path.dirname(path.abspath(__file__))

with open('%s/gateway_ip.txt' % filePath, 'a') as file:
    pass
with open('%s/gateway_ip.txt' % filePath, 'r+') as file:
    file.seek(0)
{'fraction_non_alphanumeric': 0.13257173219978746, 'fraction_numerical': 0.01647183846971307, 'mean_word_length': 2.8496932515337425, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '24945', 'n_tokens_mistral': 1321, 'n_tokens_neox': 1256, 'n_words': 352}