<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

import requests
import os
import sys
import json
import time
import importlib
import random
import threading
import argparse
import flask

def load_class_from_module(importstring):
    module_name, class_name = importstring.rsplit('.', 1)
    module = importlib.import_module(module_name)
    klass = getattr(module, class_name)
    return klass


class Client(object):

    def __init__(self, master, config_file):
        # Read from config.ini / pub- and priv-key files etc ??
        self.id = None
        self.terminate = False
        self.app = flask.Flask(__name__)
        self.unit_type = None
        self.checkin_frequency = None
        self.priv_key = ".ssh/id_rsa"
        self.certificate = None

        # setup url handlers
        self.configure_url_routes()

        # Replace with NFC discovery ??
        self.register_url = "http://%s/register-unit" % master
        self.checkin_url = None
        self.registry_url = None

        # Read config.ini
        with open(config_file) as config_fh:
            props = json.load(config_fh)

        self.unit_type = props['unit_type']
        self.checkin_frequency = props['checkin_frequency']

        self.actuators = {}
        for interface, actuator_class in props.get("actuators", {}).items():
            klass = load_class_from_module(actuator_class)
            self.actuators[interface] = klass(interface)

        self.sensors = {}
        for interface, sensor_class in props.get("sensors", {}).items():
            klass = load_class_from_module(sensor_class)
            self.sensors[interface] = klass(interface)


    def tear_down(self):
        pass


    def setup(self):
        with open(os.path.expanduser(os.path.join('~', '.ssh', 'id_rsa.csr'))) as csr_fh:
            csr = csr_fh.read()

        payload = {
            'unit_type': self.unit_type,
            'csr': csr,
            'sensors': [{
                'id': interface,
                'data': value.describe()
                } for interface, value in self.sensors.items()],
            'actuators': [{
                'id': interface,
                'data': value.describe()
                } for interface, value in self.actuators.items()]
        }

        headers = {'Content-Type': 'application/json'}
        try:
            r = requests.post(self.register_url, data=json.dumps(payload), headers=headers, timeout=1)
        except requests.exceptions.RequestException:
            print "[error]\tsomething went wrong while connecting to master, terminating..."
            sys.exit(1)
        if not r.ok:
            print "[error]\tsomething went wrong while connecting to master, terminating..."
            sys.exit(1)
<commit_msg>Propperly exit when swipe fails<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

import requests
import os
import sys
import json
import time
import importlib
import random
import threading
import argparse
import flask

def load_class_from_module(importstring):
    module_name, class_name = importstring.rsplit('.', 1)
    module = importlib.import_module(module_name)
    klass = getattr(module, class_name)
    return klass


class Client(object):

    def __init__(self, master, config_file):
        # Read from config.ini / pub- and priv-key files etc ??
        self.id = None
        self.terminate = False
        self.app = flask.Flask(__name__)
        self.unit_type = None
        self.checkin_frequency = None
        self.priv_key = ".ssh/id_rsa"
        self.certificate = None

        # setup url handlers
        self.configure_url_routes()

        # Replace with NFC discovery ??
        self.register_url = "http://%s/register-unit" % master
        self.checkin_url = None
        self.registry_url = None

        # Read config.ini
        with open(config_file) as config_fh:
            props = json.load(config_fh)

        self.unit_type = props['unit_type']
        self.checkin_frequency = props['checkin_frequency']

        self.actuators = {}
        for interface, actuator_class in props.get("actuators", {}).items():
            klass = load_class_from_module(actuator_class)
            self.actuators[interface] = klass(interface)

        self.sensors = {}
        for interface, sensor_class in props.get("sensors", {}).items():
            klass = load_class_from_module(sensor_class)
            self.sensors[interface] = klass(interface)


    def tear_down(self):
        pass


    def setup(self):
        with open(os.path.expanduser(os.path.join('~', '.ssh', 'id_rsa.csr'))) as csr_fh:
            csr = csr_fh.read()

        payload = {
            'unit_type': self.unit_type,
            'csr': csr,
            'sensors': [{
                'id': interface,
                'data': value.describe()
                } for interface, value in self.sensors.items()],
            'actuators': [{
                'id': interface,
                'data': value.describe()
                } for interface, value in self.actuators.items()]
        }

        headers = {'Content-Type': 'application/json'}
        try:
            r = requests.post(self.register_url, data=json.dumps(payload), headers=headers, timeout=1)
        except requests.exceptions.RequestException:
            r = None
        if not r or not r.ok:
            print "[error]\tsomething went wrong while connecting to master, terminating..."
            sys.exit(1)

{'fraction_non_alphanumeric': 0.11192526103681993, 'fraction_numerical': 0.001648653599560359, 'mean_word_length': 2.1744186046511627, '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': '2816249', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1552, 'n_words': 369}