<commit_before># Copyright 2016 Mycroft AI, Inc.
#
# This file is part of Mycroft Core.
#
# Mycroft Core is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Mycroft Core is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mycroft Core.  If not, see <http://www.gnu.org/licenses/>.

"""
This module implements a mechanism that allows the wifi connection of
a Linux system to be selected by end users.  This is achieved by:
  * creating a websocket for communication between the pieces of this
    mechanism
  * temporarilly creating a virtual access point
  * directing the end user to connect to that access point with another device
    (phone or tablet or laptop)
  * having them open a captive portal in that device's web browser
  * selecting the desired wifi within that browser
  * configuring this device based on that selection
"""

import os
import sys
import threading
import time
import traceback
from SimpleHTTPServer import SimpleHTTPRequestHandler
from SocketServer import TCPServer
from os.path import dirname, realpath
from shutil import copyfile
from subprocess import Popen, PIPE, call
from threading import Thread
from time import sleep

from pyric import pyw
from wifi import Cell

from mycroft.client.enclosure.api import EnclosureAPI
from mycroft.configuration import ConfigurationManager
from mycroft.messagebus.client.ws import WebsocketClient
from mycroft.messagebus.message import Message
from mycroft.util import connected
from mycroft.util.log import getLogger

__author__ = 'aatchison <NAME>'

LOG = getLogger("WiFiClient")

SCRIPT_DIR = dirname(realpath(__file__))

WPA_SUPPLICANT = """# mycroft_p2p_start
 ctrl_interface = /var / run / wpa_supplicant
 driver_param = p2p_device = 1
 update_config = 1
 device_name = mycroft - holmes - i
 device_type = 1 - 0050F204 - 1
 p2p_go_intent = 10
 p2p_go_ht40 = 1

 network = {
     ssid = "MYCROFT"
     psk = "MYCROFT1"
     proto = RSN
     key_mgmt = WPA - PSK
     pairwise = CCMP
     auth_alg = OPEN
     mode = 3
     disabled = 2
 }

 network = {
     ssid = "MYCROFT"
     psk = "MYCROFT1"
     proto = RSN
     key_mgmt = WPA - PSK
     pairwise = CCMP
     auth_alg = OPEN
     mode = 3
     disabled = 2
 }
 # mycroft_p2p_end"""

RM_SKILLS = """mkdir /opt/mycroft/safety &&
 mv /opt/mycroft/skills/skill-pairing /opt/mycroft/safety &&
 rm -rf /opt/mycroft/skills/* &&
 mv /opt/mycroft/safety/skill-pairing /opt/mycroft/skills &&
 rm -rf /opt/mycroft/safety"""


def cli_no_output(*args):
    ''' Invoke a command line and return result '''
    LOG.info("Command: %s" % list(args))
    proc = Popen(args=args, stdout=PIPE, stderr=PIPE)
    stdout, stderr = proc.communicate()
    return {'code': proc.returncode, 'stdout': stdout, 'stderr': stderr}


def cli(*args):
    ''' Invoke a command line, then log and return result '''
    LOG.info("Command: %s" % list(args))
    proc = Popen(args=args, stdout=PIPE, stderr=PIPE)
    stdout, stderr = proc.communicate()
    result = {'code': proc.returncode, 'stdout': stdout, 'stderr': stderr}
    LOG.info("Command result: %s" % result)<commit_msg>	Fixes error with wpa_supplicant string 	modified:   mycroft/client/wifisetup/main.py<commit_after># Copyright 2016 Mycroft AI, Inc.
#
# This file is part of Mycroft Core.
#
# Mycroft Core is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Mycroft Core is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mycroft Core.  If not, see <http://www.gnu.org/licenses/>.

"""
This module implements a mechanism that allows the wifi connection of
a Linux system to be selected by end users.  This is achieved by:
  * creating a websocket for communication between the pieces of this
    mechanism
  * temporarilly creating a virtual access point
  * directing the end user to connect to that access point with another device
    (phone or tablet or laptop)
  * having them open a captive portal in that device's web browser
  * selecting the desired wifi within that browser
  * configuring this device based on that selection
"""

import os
import sys
import threading
import time
import traceback
from SimpleHTTPServer import SimpleHTTPRequestHandler
from SocketServer import TCPServer
from os.path import dirname, realpath
from shutil import copyfile
from subprocess import Popen, PIPE, call
from threading import Thread
from time import sleep

from pyric import pyw
from wifi import Cell

from mycroft.client.enclosure.api import EnclosureAPI
from mycroft.configuration import ConfigurationManager
from mycroft.messagebus.client.ws import WebsocketClient
from mycroft.messagebus.message import Message
from mycroft.util import connected
from mycroft.util.log import getLogger

__author__ = 'a<NAME> penrods'

LOG = getLogger("WiFiClient")

SCRIPT_DIR = dirname(realpath(__file__))

WPA_SUPPLICANT = """#mycroft_p2p_start
ctrl_interface=/var/run/wpa_supplicant
driver_param=p2p_device=1
update_config=1
device_name=mycroft-holmes-i
device_type=1-0050F204-1
p2p_go_intent=10
p2p_go_ht40=1

network={
    ssid="MYCROFT"
    psk="MYCROFT1"
    proto=RSN
    key_mgmt=WPA-PSK
    pairwise=CCMP
    auth_alg=OPEN
    mode=3
    disabled=2
}

network={
    ssid="MYCROFT"
    psk="MYCROFT1"
    proto=RSN
    key_mgmt=WPA-PSK
    pairwise=CCMP
    auth_alg=OPEN
    mode=3
    disabled=2
}
#mycroft_p2p_end"""

RM_SKILLS = """mkdir /opt/mycroft/safety &&
 mv /opt/mycroft/skills/skill-pairing /opt/mycroft/safety &&
 rm -rf /opt/mycroft/skills/* &&
 mv /opt/mycroft/safety/skill-pairing /opt/mycroft/skills &&
 rm -rf /opt/mycroft/safety"""


def cli_no_output(*args):
    ''' Invoke a command line and return result '''
    LOG.info("Command: %s" % list(args))
    proc = Popen(args=args, stdout=PIPE, stderr=PIPE)
    stdout, stderr = proc.communicate()
    return {'code': proc.returncode, 'stdout': stdout, 'stderr': stderr}


def cli(*args):
    ''' Invoke a command line, then log and return result '''
    LOG.info("Command: %s" % list(args))
    proc = Popen(args=args, stdout=PIPE, stderr=PIPE)
    stdout, stderr = proc.communicate()
    result = {'code': proc.returncode, 'stdout': stdout, 'stderr': stderr}
    LOG.info("Command result: %s" % result)
{'fraction_non_alphanumeric': 0.08536411571896822, 'fraction_numerical': 0.009120706854781245, 'mean_word_length': 4.009279086366881, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, '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': '3495964', 'n_tokens_mistral': 2281, 'n_tokens_neox': 2168, 'n_words': 792}