<commit_before>#! /usr/bin/env python
# Â encoding: utf-8
#
# Res andy

AppVersion = "0.1.1"

import base64, functools, hashlib, json, os, platform, re, select, socket, subprocess, sys, tempfile, threading, time, \
    tkFileDialog, tkMessageBox, urllib2, webbrowser, zlib
from Tkinter import *
from operator import itemgetter


class App:
    # from socket.h
    # define SO_BINDTODEVICE 25
    SO_BINDTODEVICE = 25

    def __init__(self, master):
        self.connected = {}
        self.camconfig = []
        self.ActualAction = ""
        self.defaultbg = master.cget('bg')
        self.camsettableconfig = {}
        self.zoom_level_value = ""
        self.zoom_level_old_value = ""
        self.thread_zoom = ""
        self.CamAddresses = {}
        self.CamSockets = {}
        self.CamTokens = {}
        self.JsonData = {}
        self.Jsondata = {}
        self.Jsoncounter = {}
        self.Jsonflip = {}
        self.initcounter = {}
        self.AllConnected = True

        self.DonateUrl = "http://sw.deltaflyer.cz/donate.html"
        self.GitUrl = "https://github.com/deltaflyer4747/Xiaomi_Yi"
        self.UpdateUrl = "https://raw.githubusercontent.com/deltaflyer4747/Xiaomi_Yi/master/Multi_version.txt"
        self.master = master
        self.master.geometry("445x250+300+75")
        self.master.wm_title("Xiaomi Yi Multitrigger by Andy_S | ver %s" % AppVersion)
        self.load_settings()

        # create a menu
        self.menu = Menu(self.master)
        root.config(menu=self.menu)

        self.Cameramenu = Menu(self.menu, tearoff=0)
        self.menu.add_cascade(label="Camera", menu=self.Cameramenu, underline=0)
        self.Cameramenu.add_separator()
        self.Cameramenu.add_command(label="Exit", command=self.quit, underline=1)

        self.helpmenu = Menu(self.menu, tearoff=0)
        self.menu.add_cascade(label="Help", menu=self.helpmenu, underline=0)
        self.helpmenu.add_command(label="Donate", command=lambda aurl=self.DonateUrl: webbrowser.open_new(aurl),
                                  underline=0)
        self.helpmenu.add_command(label="About...", command=self.show_about_info, underline=0)
        self.check_for_updates()
        self.build_conn_window()

    def read_camera_config(self):
        self.camconfig = []
        message_id = 3
        tosend = '{"msg_id":' + str(message_id) + '}'
        print("Reading camera configurations...")
        self.comm(tosend, True)

        for ix_cam in self.CamAddresses.keys():
            resp = self.JsonData[ix_cam][message_id]
            cc = {}
            for each in resp["param"]:
                cc.update(each)
            self.camconfig.append(cc)

    def load_settings(self, add="", rem=""):
        if add == "" and rem == "":  # nothing to add or remove = initial call
            try:  # open the settings file (if exists) and read the settings
                filek = open("Multisettings.cfg", "r")
                filet = filek.readlines()
                filek.close()
                cam_ix = 0
                for line in filet:
                    if not line.startswith("#"):
                        addr = line.split(" ")
                        if len(addr) > 2:
                            self.CamAddresses[cam_ix] = [addr[0], int(addr[1]), addr[2].strip()]
                        else:
                            self.CamAddresses[cam_ix] = [addr[0], int(addr[1])]
                        cam_ix += 1

            except Exception as e:  # no settings file yet or file structure mismatch - lets create default one & set defaults
                print e
                filek = open("Multisettings.cfg_example", "w")
                ConfigFile = '192.168.0.10 7878\n#192.168.0.11 7878'
                filek.write(ConfigFile)
                filek.close()
                tkMessageBox.showerror("Config file is broken",
                                       "Config file is broken.\n\n Please use format\n\nIP.address.of.cam port\n\nfor example\n192.168.0.10 7878")

    def build_conn_window(self):
        self.camconn = Frame(self.master)  # create connection window with buttons
        b = Button(self.camconn, text="Connect Multitrigger", width=20, command=self.connect_cameras)
        b.focus_set()
        b.grid(row=1, column=1, padx=10, pady=2)
        self.camconn.pack(side=TOP, fill=X)

    def check_for_updates(self):
        try:
            newversion = urllib2.urlopen(self.UpdateUrl, timeout=2).read()
        except Exception:
            newversion = "0"
        if newversion > AppVersion:
            if tkMessageBox.askyesno("New version found",
                                     "NEW VERSION FOUND (%s)\nYours is %s\n\nOpen download page?" % (
                                             newversion, AppVersion)):
                webbrowser.open_new(self.GitUrl)

    def show_about_info(self):
        tkMessageBox.showinfo("About",
                              "Multitrigger | ver. %s\nCreated by Andy_S, 2015\n\<EMAIL>" % AppVersion)

    def connect_cameras(self):
        try:
            socket.setdefaulttimeout(5)<commit_msg>MultiTrigger.pyw now upgraded to python3<commit_after>#! /usr/bin/env python
# Â encoding: utf-8
#
# Res andy

AppVersion = "0.1.1"

import base64, functools, hashlib, json, os, platform, re, select, socket, subprocess, sys, tempfile, threading, time, \
    webbrowser, zlib
from tkinter import *
import tkinter.messagebox as tkMessageBox
from urllib.request import urlopen
from operator import itemgetter


class App:
    # from socket.h
    # define SO_BINDTODEVICE 25
    SO_BINDTODEVICE = 25

    def __init__(self, master):
        self.connected = {}
        self.camconfig = []
        self.ActualAction = ""
        self.defaultbg = master.cget('bg')
        self.camsettableconfig = {}
        self.zoom_level_value = ""
        self.zoom_level_old_value = ""
        self.thread_zoom = ""
        self.CamAddresses = {}
        self.CamSockets = {}
        self.CamTokens = {}
        self.JsonData = {}
        self.Jsondata = {}
        self.Jsoncounter = {}
        self.Jsonflip = {}
        self.initcounter = {}
        self.AllConnected = True

        self.DonateUrl = "http://sw.deltaflyer.cz/donate.html"
        self.GitUrl = "https://github.com/deltaflyer4747/Xiaomi_Yi"
        self.UpdateUrl = "https://raw.githubusercontent.com/deltaflyer4747/Xiaomi_Yi/master/Multi_version.txt"
        self.master = master
        self.master.geometry("445x250+300+75")
        self.master.wm_title("Xiaomi Yi Multitrigger by Andy_S | ver %s" % AppVersion)
        self.load_settings()

        # create a menu
        self.menu = Menu(self.master)
        root.config(menu=self.menu)

        self.Cameramenu = Menu(self.menu, tearoff=0)
        self.menu.add_cascade(label="Camera", menu=self.Cameramenu, underline=0)
        self.Cameramenu.add_separator()
        self.Cameramenu.add_command(label="Exit", command=self.quit, underline=1)

        self.helpmenu = Menu(self.menu, tearoff=0)
        self.menu.add_cascade(label="Help", menu=self.helpmenu, underline=0)
        self.helpmenu.add_command(label="Donate", command=lambda aurl=self.DonateUrl: webbrowser.open_new(aurl),
                                  underline=0)
        self.helpmenu.add_command(label="About...", command=self.show_about_info, underline=0)
        self.check_for_updates()
        self.build_conn_window()

    def read_camera_config(self):
        self.camconfig = []
        message_id = 3
        tosend = '{"msg_id":' + str(message_id) + '}'
        print("Reading camera configurations...")
        self.comm(tosend, True)

        for ix_cam in self.CamAddresses.keys():
            resp = self.JsonData[ix_cam][message_id]
            cc = {}
            for each in resp["param"]:
                cc.update(each)
            self.camconfig.append(cc)

    def load_settings(self, add="", rem=""):
        if add == "" and rem == "":  # nothing to add or remove = initial call
            try:  # open the settings file (if exists) and read the settings
                filek = open("Multisettings.cfg", "r")
                filet = filek.readlines()
                filek.close()
                cam_ix = 0
                for line in filet:
                    if not line.startswith("#"):
                        addr = line.split(" ")
                        if len(addr) > 2:
                            self.CamAddresses[cam_ix] = [addr[0], int(addr[1]), addr[2].strip()]
                        else:
                            self.CamAddresses[cam_ix] = [addr[0], int(addr[1])]
                        cam_ix += 1

            except Exception as e:  # no settings file yet or file structure mismatch - lets create default one & set defaults
                print(e)
                filek = open("Multisettings.cfg_example", "w")
                ConfigFile = '192.168.0.10 7878\n#192.168.0.11 7878'
                filek.write(ConfigFile)
                filek.close()
                tkMessageBox.showerror("Config file is broken",
                                       "Config file is broken.\n\n Please use format\n\nIP.address.of.cam port\n\nfor example\n192.168.0.10 7878")

    def build_conn_window(self):
        self.camconn = Frame(self.master)  # create connection window with buttons
        b = Button(self.camconn, text="Connect Multitrigger", width=20, command=self.connect_cameras)
        b.focus_set()
        b.grid(row=1, column=1, padx=10, pady=2)
        self.camconn.pack(side=TOP, fill=X)

    def check_for_updates(self):
        try:
            newversion = urlopen(self.UpdateUrl, timeout=2).read()
        except Exception:
            newversion = "0"
        if str(newversion)[1:] > str(AppVersion):
            if tkMessageBox.askyesno("New version found",
                                     "NEW VERSION FOUND (%s)\nYours is %s\n\nOpen download page?" % (
                                             newversion, AppVersion)):
                webbrowser.open_new(self.GitUrl)

    def show_about_info(self):
        tkMessageBox.showinfo("About",
                              "Multitrigger | ver. %s\nCreated by Andy_S, 2015\n\<EMAIL>" % AppVersion)

    def connect_cameras(self):
        try:
            socket.setdefaulttimeout(5)
{'fraction_non_alphanumeric': 0.11693861693861694, 'fraction_numerical': 0.019425019425019424, 'mean_word_length': 1.959758551307847, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5493657', 'n_tokens_mistral': 3300, 'n_tokens_neox': 3116, 'n_words': 621}