<commit_before>#!/usr/bin/env python
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals
from future import standard_library
standard_library.install_aliases()
from builtins import object
import argparse
import sys
import requests
import time
from . import api
import os
import subprocess
from . import machdep
import urllib.request, urllib.parse, urllib.error
import socket

orig_getaddrinfo = socket.getaddrinfo
def ipv4_getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0):
    if family == 0:
        family = socket.AF_INET
    return orig_getaddrinfo(host, port, family, socktype, proto, flags)
    
class Main(object):
    """Main commandline program"""
    
    def __init__(self):
        self.wifi = None
        self.device = None
        self.cmdlist = []
        
    def __del__(self):
        self.close()
        
    def close(self):
        self.wifi = None
        if self.device:
            self.device.close()
        self.device = None
        
    def run(self):
        """Run the main commandline program"""
        self.parseArgs()
        self.cmdlist = self.args.command
        if type(self.cmdlist) != type([]):
            self.cmdlist = [self.cmdlist]
        try:
            while True:
                cmd = self._getcmd()
                if not cmd: break
                cmdName = 'cmd_' + cmd
                if not hasattr(self, cmdName):
                    print("%s: unknown command %s, help for help" % (sys.argv[0], cmd), file=sys.stderr)
                    sys.exit(1)
                handler = getattr(self, cmdName)
                try:
                    handler()
                except api.UserIntervention as arg:
                    print("%s: %s: user intervention required:" % (sys.argv[0], cmd), file=sys.stderr)
                    print("%s: %s" % (sys.argv[0], arg), file=sys.stderr)
                    sys.exit(2)
                except requests.exceptions.HTTPError as arg:
                    print("%s: %s: %s" % (sys.argv[0], cmd, arg), file=sys.stderr)
                    sys.exit(1)
                except api.CoapError as arg:
                    print("%s: %s: %s" % (sys.argv[0], cmd, arg), file=sys.stderr)
                    sys.exit(1)
        finally:
            self.close()

    def _configModeAndWait(self, mode):
        """Helper method to request a specific mode and wait until the user has enabled it"""
        self.loadDevice()
        if self.device.config.get('currentMode') == mode:
            print("%s: target already in mode %s" % (sys.argv[0], self.device.modeName(mode)))
            return
        self.device.config.set('requestedMode', mode)
        try:
            self.device.config.save()
        except api.UserIntervention as arg:
            print("%s: %s" % (sys.argv[0], arg), file=sys.stderr)
        while True:
            time.sleep(5)
            self.device.flush()
            if self.device.config.get('currentMode') == mode:
                break
            reqMode = self.device.config.get('requestedMode', 0)
            if self.device.config.get('requestedMode') != mode:
                print("%s: target now has requestedMode %s in stead of %s?" % (sys.argv[0], self.device.modeName(reqMode), self.device.modeName(mode)), file=sys.stderr)
            print("%s: Reboot %s within %s seconds to activate mode %s" % (sys.argv[0], self.device.ipAddress, self.device.config.get('requestedModeTimeout', '???'), self.device.modeName(reqMode)), file=sys.stderr)
        print("%s: target is now in %s mode" % (sys.argv[0], self.device.config.modeName(mode)))
        
    def parseArgs(self):
        """Command line argument handling"""
        parser = argparse.ArgumentParser(description="Access Igor home automation service and other http databases")
        parser.add_argument("--ssid", action="store", metavar="SSID", help="Connect to WiFi network named SSID")<commit_msg>Error in refactoring fixed<commit_after>#!/usr/bin/env python
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals
from future import standard_library
standard_library.install_aliases()
from builtins import object
import argparse
import sys
import requests
import time
from . import api
import os
import subprocess
from . import machdep
import urllib.request, urllib.parse, urllib.error
import socket

orig_getaddrinfo = socket.getaddrinfo
def ipv4_getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0):
    if family == 0:
        family = socket.AF_INET
    return orig_getaddrinfo(host, port, family, socktype, proto, flags)
    
class Main(object):
    """Main commandline program"""
    
    def __init__(self):
        self.wifi = None
        self.device = None
        self.cmdlist = []
        
    def __del__(self):
        self.close()
        
    def close(self):
        self.wifi = None
        if self.device:
            self.device.close()
        self.device = None
        
    def run(self):
        """Run the main commandline program"""
        self.parseArgs()
        self.cmdlist = self.args.command
        if type(self.cmdlist) != type([]):
            self.cmdlist = [self.cmdlist]
        try:
            while True:
                cmd = self._getcmd()
                if not cmd: break
                cmdName = 'cmd_' + cmd
                if not hasattr(self, cmdName):
                    print("%s: unknown command %s, help for help" % (sys.argv[0], cmd), file=sys.stderr)
                    sys.exit(1)
                handler = getattr(self, cmdName)
                try:
                    handler()
                except api.UserIntervention as arg:
                    print("%s: %s: user intervention required:" % (sys.argv[0], cmd), file=sys.stderr)
                    print("%s: %s" % (sys.argv[0], arg), file=sys.stderr)
                    sys.exit(2)
                except requests.exceptions.HTTPError as arg:
                    print("%s: %s: %s" % (sys.argv[0], cmd, arg), file=sys.stderr)
                    sys.exit(1)
                except api.CoapError as arg:
                    print("%s: %s: %s" % (sys.argv[0], cmd, arg), file=sys.stderr)
                    sys.exit(1)
        finally:
            self.close()

    def _configModeAndWait(self, mode):
        """Helper method to request a specific mode and wait until the user has enabled it"""
        self.loadDevice()
        if self.device.config.get('currentMode') == mode:
            print("%s: target already in mode %s" % (sys.argv[0], self.device.modeName(mode)))
            return
        self.device.config.set('requestedMode', mode)
        try:
            self.device.config.save()
        except api.UserIntervention as arg:
            print("%s: %s" % (sys.argv[0], arg), file=sys.stderr)
        while True:
            time.sleep(5)
            self.device.flush()
            if self.device.config.get('currentMode') == mode:
                break
            reqMode = self.device.config.get('requestedMode', 0)
            if self.device.config.get('requestedMode') != mode:
                print("%s: target now has requestedMode %s in stead of %s?" % (sys.argv[0], self.device.modeName(reqMode), self.device.modeName(mode)), file=sys.stderr)
            print("%s: Reboot %s within %s seconds to activate mode %s" % (sys.argv[0], self.device.ipAddress, self.device.config.get('requestedModeTimeout', '???'), self.device.modeName(reqMode)), file=sys.stderr)
        print("%s: target is now in %s mode" % (sys.argv[0], self.device.modeName(mode)))
        
    def parseArgs(self):
        """Command line argument handling"""
        parser = argparse.ArgumentParser(description="Access Igor home automation service and other http databases")
        parser.add_argument("--ssid", action="store", metavar="SSID", help="Connect to WiFi network named SSID")
{'fraction_non_alphanumeric': 0.12216909914443885, 'fraction_numerical': 0.0055359838953195776, 'mean_word_length': 2.143139580862001, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5986260', 'n_tokens_mistral': 2306, 'n_tokens_neox': 2253, 'n_words': 610}