#!/usr/bin/env python
"""Executable Python script for scanning source code for compliance.

   This script checks some (simple) conventions:
   - no symlinks
   - no tabs
   - no trailing whitespace
   - files end with EOL
   - valid license headers in source files (where applicable)
   - general regex. string search

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
"""
import argparse
import collections
try:
    import configparser
except ImportError:
    import ConfigParser as configparser
import fnmatch
import itertools
import os
import platform
import re
import sys
import textwrap

VERBOSE = False

# Terminal colors
BLUE = '\033[94m'
CYAN = '\033[36m'
GREEN = '\033[92m'
RED = '\033[91m'
YELLOW = '\033[33m'

# Translatable messages (error and general)
ERR_REGEX = "file contains a forbidden string. string=[%s], regex=[%s]"
ERR_GENERAL = "an unspecified error was detected."
ERR_INVALID_CONFIG_FILE = "Invalid configuration file [%s]: %s.\n"
ERR_INVALID_SCAN_FUNCTION = "Config. file filter [%s] lists invalid " \
                            "function [%s]."
ERR_LICENSE = "file does not include required license header."
ERR_LICENSE_FILE_NOT_FOUND = "License file [%s] could not be found."
ERR_NO_EOL_AT_EOF = "file does not end with EOL."
ERR_PATH_IS_NOT_DIRECTORY = "%s: [%s] is not a valid directory.\n"
ERR_REQUIRED_SECTION = "Configuration file missing required section: [%s]"
ERR_SYMBOLIC_LINK = "file is a symbolic link."
ERR_TABS = "line contains tabs."
ERR_TRAILING_WHITESPACE = "line has trailing whitespaces."

HELP_CONFIG_FILE = "provide custom configuration file"
HELP_DISPLAY_EXCLUSIONS = "display path exclusion information"
HELP_ROOT_DIR = "starting directory for the scan"
HELP_VERBOSE = "enable verbose output"

MSG_CHECKING_FILE = "  [%s]..."
MSG_CHECKS_PASSED = "All checks passed."
MSG_CONFIG_ADDING_LICENSE_FILE = "Adding valid license from: [%s], value:\n%s"
MSG_ERROR_SUMMARY = "Scan detected %d error(s) in %d file(s):"
MSG_READING_CONFIGURATION = "Reading configuration file [%s]..."
MSG_READING_LICENSE_FILE = "Reading license file [%s]..."
MSG_RUNNING_FILE_CHECKS = "    Running File Check [%s]"
MSG_RUNNING_LINE_CHECKS = "    Running Line Check [%s]"
MSG_SCANNING_FILTER = "Scanning files with filter: [%s]:"
MSG_SCANNING_STARTED = "Scanning files starting at [%s]..."

WARN_CONFIG_SECTION_NOT_FOUND = "Configuration file section [%s] not found."
WARN_SCAN_EXCLUDED_PATH_SUMMARY = "Scan excluded (%s) directories:"
WARN_SCAN_EXCLUDED_FILE_SUMMARY = "Scan excluded (%s) files:"
WARN_SCAN_EXCLUDED_FILE = "  Excluded file: %s"
WARN_SCAN_EXCLUDED_PATH = "  Excluded path: %s"

MSG_DESCRIPTION = "Scans all source code under specified directory for " \
                  "project compliance using provided configuration."

# Default values for command line arguments
DEFAULT_ROOT_DIR = "."
DEFAULT_PROGRAM_PATH = "./"
DEFAULT_CONFIG_FILE = "scanCode.cfg"
DEFAULT_LICENSE_SEARCH_SLACK = 500

# Configuration file sections
SECTION_LICENSE = "Licenses"
SECTION_EXCLUDE = "Excludes"
SECTION_INCLUDE = "Includes"
SECTION_OPTIONS = "Options"
SECTION_REGEX = "Regex"

# Configuration Options known keys
OPT_LICENSE_SLACK_LEN = "license_slack_length"

# Globals
"""Hold valid license headers within an array strings."""
valid_licenses = []

"""Paths to exclude from directory search."""
exclusion_paths = []

"""Regex. patterns to search for."""
regex_patterns = []

"""globals."""
exclusion_files_set = set()
license_search_slack_len = DEFAULT_LICENSE_SEARCH_SLACK
FILE_CHECK_FUNCTIONS = dict()
LINE_CHECK_FUNCTIONS = dict()
FILTERS_WITH_CHECK_FUNCTIONS = []

def print_error(msg):
    """Print error message to stderr."""
    sys.stderr.write(col.red(msg) + "\n")

def print_warning(msg):
    """Print warning message to stdout."""
    print(col.yellow(msg))

def print_status(msg):
    """Print status message to stdout."""
    print(msg)

def print_success(msg):
    """Print success message to stdout."""
    print(col.green(msg))

def print_highlight(msg):
    """Print highlighted message to stdout."""
    print(col.cyan(msg))

def vprint(s):
    """Conditional print (stdout)."""
    if VERBOSE:
        print_status(s)

def get_config_section_dict(config, section):
    """Retrieve key-value(s) for requested section of a config. file."""
    dict1 = {}
    try:
        options = config.options(section)
        # print_warning("options for section: %s\n%s" % (section, options))
        for option in options:
            try:
                dict1[option] = config.get(section, option)
            except:
                dict1[option] = None
    except:
        print_warning(WARN_CONFIG_SECTION_NOT_FOUND % section)
        return None
    return dict1

def find_license_on_path(filename, path):
    """Find the specified filename in path; return it or raise error."""
    filename = os.path.join(path, filename)

    if not os.path.exists(filename):
        raise Exception(ERR_LICENSE_FILE_NOT_FOUND %
                        filename)
    return filename

def read_license_files(config):
    """Read the license files to use when scanning source files."""
    file_dict = get_config_section_dict(config, SECTION_LICENSE)
    # vprint("file_dict: " + str(file_dict))
    if file_dict is not None:
        # for each key (license filename) in license section
        for license_filename in file_dict:
            # Read and append text of each license (header) to a global array.
            # Each 'key' should be a filename containing license text.
            try:

                # if the file is not in current directory, try to find
                # it in the path this script is being executed from.
                if not os.path.exists(license_filename):
                    license_filename = find_license_on_path(
                        license_filename,
                        DEFAULT_PROGRAM_PATH)

                with open(license_filename, 'r') as temp_file:
                    vprint(MSG_READING_LICENSE_FILE % license_filename)
                    str1 = str(temp_file.read())
                    valid_licenses.append(str(str1))
                    vprint(MSG_CONFIG_ADDING_LICENSE_FILE % (license_filename,
                                                             str1))
            except Exception as e:
                raise e
    else:
        raise Exception(ERR_REQUIRED_SECTION % SECTION_LICENSE)

def read_path_exclusions(config):
    """Read the list of paths to exclude from the scan."""
    path_dict = get_config_section_dict(config, SECTION_EXCLUDE)
    # vprint("path_dict: " + str(path_dict))
    if path_dict is not None:
        # each 'key' is an exclusion path
        for key in path_dict:
            if key is not None:
                exclusion_paths.append(key)
    else:
        raise Exception(ERR_REQUIRED_SECTION % SECTION_LICENSE)

def read_scan_options(config):
    """Read the Options from the configuration file."""
    options_dict = get_config_section_dict(config, SECTION_OPTIONS)
    # vprint("options_dict: " + str(options_dict))
    if options_dict is not None:
        # Check for license scan slack length option
        # Set global variable to value found in config.
        if OPT_LICENSE_SLACK_LEN in options_dict:
            global license_search_slack_len
            license_search_slack_len = int(options_dict[OPT_LICENSE_SLACK_LEN])
    else:
        raise Exception(ERR_REQUIRED_SECTION % SECTION_OPTIONS)

def read_regex(config):
    """Read the Regular Expressions from the configuration file."""
    options_dict = get_config_section_dict(config, SECTION_REGEX)
    # vprint("options_dict: " + str(options_dict))
    if options_dict is not None:
        # each key is a regex string
        for pattern in options_dict:
            if pattern is not None:
                regex_patterns.append(pattern)
    else:
        raise Exception(ERR_REQUIRED_SECTION % SECTION_REGEX)

def read_config_file(file):
    """Read in and validate configuration file."""
    try:
        print_highlight(MSG_READING_CONFIGURATION % file.name)
        # Provide for sections that have simply values (not key=value)
        config = configparser.ConfigParser(allow_no_value=True)
        # This option prevents options from being normalized to lowercase
        # by allowing the raw string in the config. to be passed through
        config.optionxform = str
        config.readfp(file)
        read_license_files(config)
        read_path_inclusions(config)
        read_path_exclusions(config)
        read_scan_options(config)
        read_regex(config)
    except Exception as e:
        print_error(e)
        return -1
    return 0

def no_tabs(line):
    """Assert line does not contains a TAB character."""
    if re.match("\t", line):
        return ERR_TABS
    else:
        return None

def no_trailing_spaces(line):
    """Assert line does not have trailing whitespace."""
    if len(line) > 0 and line[-1] == '\n':
        line = line[:-1]

    if re.match("""^.*\s$""", line):
        return ERR_TRAILING_WHITESPACE
    else:
        return None

def eol_at_eof(line):
    """Assert line at End of File is an End of Line character."""
    if len(line) == 0 or line[-1] != '\n':
        return ERR_NO_EOL_AT_EOF
    else:
        return None

def has_block_license(path):
    """Open file and verify it contains a valid license header."""
    with open(path) as fp:
        for license in valid_licenses:
            # Assure license string is normalized to remove indentations
            # caused by declaration (above) as a string literal.
            normalized_license = textwrap.dedent(license)
            # Search for license at start of file,
            # allowing for some "slack" length
            file_head = fp.read(len(normalized_license) +
                                license_search_slack_len)

            if file_head is None:
                return [(1, ERR_LICENSE)]
            elif normalized_license in file_head:
                return []
            # reset and try finding the next license
            fp.seek(0)
    return [(1, ERR_LICENSE)]

def is_not_symlink(path):
    """Assert a file is not a symbolic link."""
    if os.path.islink(path):
        return [(0, ERR_SYMBOLIC_LINK)]
    else:
        return None

def regex_check(line):
    """Assert line does not contain strings matching regex. expressions."""
    # vprint("regex pattern: " + str(regex_patterns))
    for pattern in regex_patterns:
        if re.search(pattern, line):
            return ERR_REGEX
        else:
            return None

# Note: this function must appear after all "check" functions are defined
def read_path_inclusions(config):
    """Read the list of paths to include in scan tests."""
    inclusion_dict = get_config_section_dict(config, SECTION_INCLUDE)
    # vprint("inclusion_dict: " + str(inclusion_dict))

    for key in inclusion_dict:
        all_checks = inclusion_dict[key]
        # strip off all whitespace, regardless of index
        all_checks = all_checks.replace(' ', '')
        # retrieve the names of all functions to scan for
        # the respective filename (wildcards allowed)
        function_names = all_checks.split(',')
        file_check_fxs = []
        line_check_fxs = []
        for fname in function_names:
            try:
                fx = globals()[fname]
                if fname in FILE_CHECK_FUNCTIONS:
                    file_check_fxs.append(fx)
                elif fname in LINE_CHECK_FUNCTIONS:
                    line_check_fxs.append(fx)
            except Exception:
                print_error(ERR_INVALID_SCAN_FUNCTION % (key, fname))
                sys.exit(1)

        a_tuple = (key, file_check_fxs, line_check_fxs)
        FILTERS_WITH_CHECK_FUNCTIONS.append(a_tuple)
    # vprint("filters(checks):" + str(FILTERS_WITH_CHECK_FUNCTIONS))

def run_file_checks(file_path, checks):
    """Run a series of file-by-file checks."""
    errors = []
    # if VERBOSE (True) then print filename being checked
    vprint(MSG_CHECKING_FILE % file_path)
    for check in checks:
        vprint(col.cyan(MSG_RUNNING_FILE_CHECKS % check.__name__))
        errs = check(file_path)
        if errs:
            errors += errs
    return errors

def run_line_checks(file_path, checks):
    """."""
    errors = []
    line_number = 0
    # For each line in the file, run all "line checks"
    with open(file_path) as fp:
        for line in fp:
            line_number += 1
            for check in checks:
                if line_number == 1:
                    vprint(col.cyan(MSG_RUNNING_LINE_CHECKS %
                                    check.__name__))
                err = check(line)
                if err is not None:
                    errors.append((line_number, err))
    return errors

def all_paths(root_dir):
    """Generator that returns files with known extensions that can be scanned.

    Iteration is recursive beginning at the passed root directory and
    skipping directories that are listed as exception paths.
    """
    # For every file in every directory (path) starting at "root_dir"
    for dir_path, dir_names, files in os.walk(root_dir):
        for f in files:
            # Map will contain a boolean for each exclusion path tested
            # as input to the lambda function.
            # only if all() values in the Map are "True" (meaning the file is
            # not excluded) then it should yield the filename to run checks on.
            # not dir_path.endswith(p) and
            if all(map(lambda p: p not in dir_path,
                       exclusion_paths)):
                yield os.path.join(dir_path, f)
            else:
                exclusion_files_set.add(os.path.join(dir_path, f))

def colors():
    """Create a collection of helper functions to colorize strings."""
    ansi = hasattr(sys.stderr, "isatty") and platform.system() != "Windows"

    def colorize(code, string):
        # Enable ANSI terminal color only around string provided (if valid)
        return "%s%s%s" % (code, string, '\033[0m') if ansi else string

    def cyan(s):
        return colorize(CYAN, s)

    def green(s):
        return colorize(GREEN, s)

    def red(s):
        return colorize(RED, s)

    def yellow(s):
        return colorize(YELLOW, s)

    return collections.namedtuple(
        "Colorizer",
        "cyan green red yellow")(cyan, green, red, yellow)

# Script entrypoint.
if __name__ == "__main__":

    # Prepare message colorization methods
    col = colors()

    # Parser helpers
    def is_dir(path):
        """Check if path is a directory."""
        return os.path.isdir(root_dir)

    # identify the path (directory) where scanCode.py is located
    # Use this as default for finding default configuration
    DEFAULT_PROGRAM_PATH = os.path.split(os.path.abspath(__file__))[0]
    # vprintf("DEFAULT_PROGRAM_PATH: =[%s]" % DEFAULT_PROGRAM_PATH)
    DEFAULT_CONFIG_FILE = os.path.join(DEFAULT_PROGRAM_PATH,
                                       DEFAULT_CONFIG_FILE)

    # create / configure our argument parser
    # Note: ArgumentParser catches all errors and outputs a message
    # to override this behavior you would need to subclass it.
    parser = argparse.ArgumentParser(description=MSG_DESCRIPTION)
    parser.add_argument("-v", "--verbose",
                        action="store_true",
                        dest="verbose",
                        default=False,
                        help=HELP_VERBOSE)
    parser.add_argument("-x",
                        action="store_true",
                        dest="display_exclusions",
                        default=False,
                        help=HELP_DISPLAY_EXCLUSIONS)
    parser.add_argument("--config",
                        type=argparse.FileType('r'),
                        action="store",
                        dest="config",
                        default=DEFAULT_CONFIG_FILE,
                        help=HELP_CONFIG_FILE)
    parser.add_argument("root_directory",
                        type=str,
                        default=DEFAULT_ROOT_DIR,
                        help=HELP_ROOT_DIR)

    # Invoke parser, assign argument values to locals
    args = parser.parse_args()
    root_dir = args.root_directory
    VERBOSE = args.verbose

    # Config file at this point is an actual file object
    config_file = args.config

    # Assign supported scan functions to either file or line globals
    # These checks run once per-file
    FILE_CHECK_FUNCTIONS.update({
        "is_not_symlink": is_not_symlink,
        "has_block_license": has_block_license
    })

    # These checks run once per-line, per-file
    LINE_CHECK_FUNCTIONS.update({
        "no_tabs": no_tabs,
        "no_trailing_spaces": no_trailing_spaces,
        "eol_at_eof": eol_at_eof,
        "regex_check": regex_check
    })

    # Read / load configuration file from file (pointer)
    if read_config_file(config_file) == -1:
        exit(1)

    # Verify starting path parameter is valid
    if not is_dir(root_dir):
        print_error(ERR_PATH_IS_NOT_DIRECTORY % (sys.argv[0], root_dir))
        parser.print_help()
        exit(1)

    # Positive feedback to caller that scanning has started
    print_highlight(MSG_SCANNING_STARTED % root_dir)

    # Runs all listed checks on all relevant files.
    all_errors = []

    for fltr, chks1, chks2 in FILTERS_WITH_CHECK_FUNCTIONS:
        # print_error(col.cyan(MSG_SCANNING_FILTER % fltr))
        # print_error("chks1=" + str(chks1))
        # print_error("chks2=" + str(chks2))
        for path in fnmatch.filter(all_paths(root_dir), fltr):
            errors = run_file_checks(path, chks1)
            errors += run_line_checks(path, chks2)
            all_errors += map(lambda p: (path, p[0], p[1]), errors)

    # Display path and file exclusion details
    if args.display_exclusions or VERBOSE:
        print_warning(WARN_SCAN_EXCLUDED_PATH_SUMMARY % len(exclusion_paths))
        # Display all paths that were excluded (by configuration)
        for excluded_path in exclusion_paths:
            print_warning(WARN_SCAN_EXCLUDED_PATH % excluded_path)

        # Inform caller which files where excluded from these paths
        print_warning(WARN_SCAN_EXCLUDED_FILE_SUMMARY %
                      len(exclusion_files_set))
        for excluded_file in exclusion_files_set:
            print_warning(WARN_SCAN_EXCLUDED_FILE % excluded_file)

    def sort_key(p):
        """Define sort key for error listing as the filename."""
        # Filename is the 0th entry in tuple
        return p[0]

    if all_errors:
        # Group / sort errors by filename
        error_listing = ""
        files_with_errors = 0
        for path, triples in itertools.groupby(sorted(all_errors,
                                                      key=sort_key),
                                               key=sort_key):
            files_with_errors += 1
            error_listing += "  [%s]:\n" % path

            pairs = sorted(map(lambda t: (t[1], t[2]), triples),
                           key=lambda p: p[0])
            for line, msg in pairs:
                error_listing += col.red("    %4d: %s\n" % (line, msg))

        # Summarize errors
        summary = MSG_ERROR_SUMMARY % (len(all_errors), files_with_errors)
        print_highlight(summary)
        print(error_listing)
        print_error(summary)
        sys.exit(1)
    else:
        print_success(MSG_CHECKS_PASSED)
sys.exit(0)


# -*- coding: utf-8 -*-
"""
Created on Thu August 10 23:52:55 2022

@author: jmmauricio
"""

import numpy as np
import sympy as sym

def i_vsg_ll(grid,name,bus_name,data_dict):
    """
    # auxiliar equations
    Omega_b = 2*np.pi*F_n
    omega_s = omega_coi
    v_D = V*sin(theta)  # e^(-j)
    v_Q = V*cos(theta)
    v_d = v_D * cos(delta) - v_Q * sin(delta)
    v_q = v_D * sin(delta) + v_Q * cos(delta)

    Domega = x_v + K_p * (p_ref - p)
    e_dv = 0.0
    epsilon_v = v_ref - V
    i_d = i_d_ref
    i_q = i_q_ref
    omega_v = Domega + 1.0
    q_ref_0 = K_p_v * epsilon_v + K_i_v * xi_v

    # dynamical equations
    ddelta   = Omega_b*(omega_v - omega_s) - K_delta*delta
    dx_v = K_i*(p_ref - p) - K_g*(omega_v - 1.0)
    de_qm = 1.0/T_q * (q - q_ref_0 - q_ref - e_qm)
    dxi_v = epsilon_v # PI agregado

    # algebraic equations
    g_i_d_ref  = e_dv - R_v * i_d_ref - X_v * i_q_ref - v_d
    g_i_q_ref  = e_qv - R_v * i_q_ref + X_v * i_d_ref - v_q
    g_p  = v_d*i_d + v_q*i_q - p
    g_q  = v_d*i_q - v_q*i_d - q
    g_e_qv = 1.0 - e_qv - K_q*e_qm

    {"bus":"1","type":"vsg_ll",'S_n':10e6,'F_n':50,'K_delta':0.0,
    'R_v':0.01,'X_v':0.1,'K_p':1.0,'K_i':0.1,'K_g':0.0,'K_q':20.0,
    'T_q':0.1,'K_p_v':1e-6,'K_i_v':1e-6}

    """

    sin = sym.sin
    cos = sym.cos

    ctrl_data = data_dict['ctrl']

    buses = grid.data['buses']
    buses_list = [bus['name'] for bus in buses]

    idx_bus = buses_list.index(bus_name) # get the number of the bus where the syn is connected
    if not 'idx_powers' in buses[idx_bus]: buses[idx_bus].update({'idx_powers':0})
    buses[idx_bus]['idx_powers'] += 1

    # inputs:
    q_l,q_r,v_ref = sym.symbols(f'q_l_{name},q_r_{name},v_ref_{name}', real=True)
    p_l,p_r = sym.symbols(f'p_l_{name},p_r_{name}', real=True)
    v_dc = sym.Symbol(f"v_dc_{name}", real=True)
    Ddelta_ff,Domega_ff = sym.symbols(f'Ddelta_ff_{name},Domega_ff_{name}', real=True)

    # dynamical states:
    delta,x_p1,x_p2,e_qm,xi_v = sym.symbols(f'delta_{name},x_p1_{name},x_p2_{name},e_qm_{name},xi_v_{name}', real=True)

    # algebraic states:
    Domega = sym.Symbol(f'Domega_{name}', real=True)
    p_s = sym.Symbol(f'p_s_{name}', real=True)
    q_s = sym.Symbol(f'q_s_{name}', real=True)
    e_vq = sym.Symbol(f"e_vq_{name}", real=True)
    v_td_ref = sym.Symbol(f"v_td_ref_{name}", real=True)
    v_tq_ref = sym.Symbol(f"v_tq_ref_{name}", real=True)

    # params:
    S_base = sym.Symbol('S_base', real = True) # S_base = global power base, S_n = machine power base
    S_n,F_n,K_delta = sym.symbols(f'S_n_{name},F_n_{name},K_delta_{name}', real=True)
    K_p,K_i,K_g,K_i_q = sym.symbols(f'K_p_{name},K_i_{name},K_g_{name},K_i_q_{name}', real=True)
    R_v,X_v = sym.symbols(f'R_v_{name},X_v_{name}', real=True)
    K_q,T_q = sym.symbols(f'K_q_{name},T_q_{name}', real=True)
    K_p_v,K_i_v = sym.symbols(f'K_p_v_{name},K_i_v_{name}', real=True)
    params_list = ['F_n','K_delta','K_p','K_i','K_g','R_v','X_v','K_q','T_q','K_p_v','K_i_v']

    # auxiliar variables and constants
    omega_coi = sym.Symbol("omega_coi", real=True) # from global system
    V_s = sym.Symbol(f"V_{bus_name}", real=True) # from global system
    theta_s = sym.Symbol(f"theta_{bus_name}", real=True) # from global system
    i_si = sym.Symbol(f"i_si_{name}", real=True)
    i_sr = sym.Symbol(f"i_sr_{name}", real=True)
    m = sym.Symbol(f"m_{name}", real=True)
    theta_t = sym.Symbol(f"theta_t_{name}", real=True)

    # auxiliar equations
    Omega_b = 2*np.pi*F_n
    omega_s = omega_coi

    i_sD = i_si  # e^(-j)
    i_sQ = i_sr

    delta_ff =  delta + Ddelta_ff
    i_sd = i_sD * cos(delta_ff) - i_sQ * sin(delta_ff)
    i_sq = i_sD * sin(delta_ff) + i_sQ * cos(delta_ff)

    p_ref = p_l + p_r
    q_ref = q_l + q_r
    e_vd = 0.0
    epsilon_v = v_ref - V_s
    omega_v = Domega + 1.0
    q_ref_0 = K_p_v * epsilon_v + K_i_v * xi_v

    v_tD_ref =  v_td_ref * cos(delta_ff) + v_tq_ref * sin(delta_ff)
    v_tQ_ref = -v_td_ref * sin(delta_ff) + v_tq_ref * cos(delta_ff)

    v_ti_ref = v_tD_ref
    v_tr_ref = v_tQ_ref
    m_ref = sym.sqrt(v_tr_ref**2 + v_ti_ref**2)/v_dc
    theta_t_ref = sym.atan2(v_ti_ref,v_tr_ref)
    p_meas = p_s

    # dynamical equations
    ddelta   = Omega_b*(omega_v - omega_s) - K_delta*delta
    dx_p1 = K_i*(p_ref - p_meas) - K_g*x_p1
    dx_p2 = K_p*(p_ref - p_meas) - K_g*x_p2
    de_qm = 1.0/T_q * (q_s - q_ref_0 - q_ref - e_qm)
    dxi_v = epsilon_v # PI agregado

    # algebraic equations
    g_Domega = -Domega +  dx_p2 + x_p1
    g_v_td_ref  = e_vd - R_v * i_sd - X_v * i_sq - v_td_ref
    g_v_tq_ref  = e_vq - R_v * i_sq + X_v * i_sd - v_tq_ref

    g_e_vq = 1.0 - e_vq - K_q*e_qm
    g_m  = m-m_ref
    g_theta_t  = theta_t-theta_t_ref

    # DAE system update
    grid.dae['f'] += [ddelta,dx_p1,dx_p2,de_qm,dxi_v]
    grid.dae['x'] += [ delta, x_p1, x_p2, e_qm, xi_v]
    grid.dae['g'] +=     [g_Domega,g_v_td_ref,g_v_tq_ref,g_e_vq,g_m,g_theta_t]
    grid.dae['y_ini'] += [  Domega,v_td_ref,  v_tq_ref,  e_vq,  m,  theta_t]
    grid.dae['y_run'] += [  Domega,v_td_ref,  v_tq_ref,  e_vq,  m,  theta_t]

    # default inputs
    grid.dae['u_ini_dict'].update({f'p_l_{name}':0.0})
    grid.dae['u_ini_dict'].update({f'q_l_{name}':0.0})
    grid.dae['u_ini_dict'].update({f'p_r_{name}':0.0})
    grid.dae['u_ini_dict'].update({f'q_r_{name}':0.0})
    grid.dae['u_ini_dict'].update({f'v_ref_{name}':1.0})
    grid.dae['u_ini_dict'].update({f'Ddelta_ff_{name}':0.0})
    grid.dae['u_ini_dict'].update({f'Domega_ff_{name}':0.0})

    grid.dae['u_run_dict'].update({f'p_l_{name}':0.0})
    grid.dae['u_run_dict'].update({f'q_l_{name}':0.0})
    grid.dae['u_run_dict'].update({f'p_r_{name}':0.0})
    grid.dae['u_run_dict'].update({f'q_r_{name}':0.0})
    grid.dae['u_run_dict'].update({f'v_ref_{name}':1.0})
    grid.dae['u_run_dict'].update({f'Ddelta_ff_{name}':0.0})
    grid.dae['u_run_dict'].update({f'Domega_ff_{name}':0.0})

    # default parameters
    for item in params_list:
        grid.dae['params_dict'].update({item + f'_{name}':ctrl_data[item]})

    # add speed*H term to global for COI speed computing
    H = 4.0
    grid.H_total += H
    grid.omega_coi_numerator += omega_v*H*S_n
    grid.omega_coi_denominator += H*S_n

    # DAE outputs update
    grid.dae['h_dict'].update({f"omega_v_{name}":omega_v})
    grid.dae['h_dict'].update({f"p_ref_{name}":p_ref})
    grid.dae['h_dict'].update({f"q_ref_{name}":q_ref})
    grid.dae['h_dict'].update({f"v_ref_{name}":v_ref})
    grid.dae['h_dict'].update({f"i_sd_{name}":i_sd})
    grid.dae['h_dict'].update({f"i_sq_{name}":i_sq})
    grid.dae['h_dict'].update({f"delta_ff_{name}":delta_ff})

    grid.dae['xy_0_dict'].update({str(e_vq):1.0})
    grid.dae['xy_0_dict'].update({str(v_tq_ref):1.0})

    p_W   = p_s * S_n
    q_var = q_s * S_n

    return p_W,q_var

if __name__ == "__main__":
    pass


"""
Helpers to track the evolution of a node through transformations,
notably source code location.

Consider:

    with About(original_node, 'my-transform'):
        return f(original_node)

Any node (as defined in myia.stx.nodes) created while the ``with``
block is active, in this case during the execution of ``f``, will have
its ``about`` field set to the ``About`` instance. This will mark it
as being "about" ``original_node``, with the tag ``my-transform``. The
``about`` field can thus form a chain going all the way back to the
original node made during parsing.

All code transforms should use ``with About(...)`` in their implementation
in order to preserve this chain, with as fine a granularity as possible.
"""

from typing import Any
import threading
import textwrap

class MyiaSyntaxError(Exception):
    """
    Class for syntax errors in Myia. This exception type should be
    raised for any feature that is not supported.

    Attributes:
        message: A precise assessment of the problem.
        location: The error's location in the original source. If not
            provided, it will be extracted from the node we are currently
            About.
    """
    def __init__(self, message: str, location: 'Location' = None) -> None:
        self.location = location or current_location()
        self.message = message
        super().__init__(self.message, self.location)

# We use per-thread storage for the about stack.
_about = threading.local()
_about.stack = [None]

def top():
    """
    Return the currently active ``About`` instance.
    """
    return _about.stack[-1]

def current_location():
    """
    Follow the about chain of the node we are currently about in order
    to find where in the source code it ultimately came from.
    """
    abt = top()
    while abt:
        node = abt.node
        if getattr(node, 'about', None):
            abt = node.about
        else:
            abt = None
    if isinstance(node, Location):
        return node
    return None

class About:
    """
    Any code executed during the ``About`` context manager is understood
    to be "about" the node given in the constructor. It is up to
    compliant object constructors to consult what node they are about and
    to store that information. This is done by ``MyiaASTNode`` instances.

    ``About`` instances are pushed on a per-thread stack, therefore
    ``with`` calls to ``About`` can be nested.
    """
    def __init__(self, node, transform):
        self.node = node
        self.transform = transform

    def __enter__(self):
        _about.stack.append(self)

    def __exit__(self, etype, evalue, etraceback):
        _about.stack.pop()

class AboutPrinter:
    """
    Through its ``__hrepr__`` representation, an AboutPrinter can be
    printed out to Buche and will display a chain of about nodes that
    can be navigated, along with stack traces for the creation of each
    node when they are available.
    """
    def __init__(self, node):
        self.node = node

    def node_hrepr(self, node, H, hrepr):
        if hasattr(node, 'trace'):
            views = H.tabbedView()
            views = views(H.view(H.tab('node'), H.pane(hrepr(node))))
            views = views(H.view(H.tab('trace'), H.pane(hrepr(node.trace))))
            return views
        else:
            return hrepr(node)

    def __hrepr__(self, H, hrepr):
        views = H.tabbedView()
        node = self.node

        nodes = [self.node_hrepr(node, H, hrepr)]
        transforms = []

        while node and getattr(node, 'about', None):
            about = node.about
            node = about.node
            nodes.append(self.node_hrepr(node, H, hrepr))
            transforms.append(about.transform)
        transforms.append('orig')

        for transform, node in reversed(list(zip(transforms, nodes))):
            tab = H.tab(transform)
            pane = H.pane(node)
            views = views(H.view(tab, pane))

        return views

class Location:
    """
    Represents a source code location for an AST node.

    Attributes:
        url (str): The path of the code file.
        line (int): The line number in that file.
        column (int): The column number in that file.
    """

    def __init__(self,
                 url: str,
                 line: int,
                 column: int,
                 node: Any = None) -> None:
        self.url = url
        self.line = line
        self.column = column
        self.node = node

    def traceback(self) -> str:
        """
        Print out a "traceback" that corresponds to this location,
        with the line printed out and a caret at the right column.
        Basically:

        >>> loc.traceback()
          File {url}, line {line}, column {column}
            x = f(y)
                ^

        This is mostly meant for printing out ``MyiaSyntaxError``s.
        """
        try:
            with open(self.url) as file:
                raw_code = file.readlines()[self.line - 1].rstrip("\n")
                raw_caret = ' ' * self.column + '^'
                code, caret = textwrap.dedent(
                    raw_code + '\n' + raw_caret
                ).split("\n")
            return '  File "{}", line {}, column {}\n    {}\n    {}'.format(
                self.url, self.line, self.column, code, caret)
        except FileNotFoundError:
            return '  File "{}", line {}, column {}'.format(
                self.url, self.line, self.column)

    def __str__(self) -> str:
        return '{}@{}:{}'.format(self.url, self.line, self.column)

    def __hrepr__(self, H, hrepr):
        return H.codeSnippet(
            src = self.url,
            language = "python",
            line = self.line,
            column = self.column + 1,
            context = 4
        )


import matplotlib.pyplot as plt
import math

def analitica(A,B,t):
    k = 1
    m = 1
    a = -(math.sqrt(k/m))

    return A * math.cos(a*t) + B * math.sin(a*t)

def euler():
    aproxER = []
    real = []

    t = 0
    atualV = 0
    atualX = 1
    k = m = 1
    a = -(k/m)
    dt = 0.1
    res = 0.1

    for i in range(0,100):
        proxV = atualV + a*atualX*dt - res*atualX
        proxX = atualX + atualV*dt
        ana = analitica(1,1,dt*i)
        real.append(ana)
        aproxER.append(proxX)
        atualV = proxV
        atualX = proxX

    return aproxER

def main():
    aproxE = []
    real = []

    t = 0
    atualV = 0
    atualX = 1
    k = m = 1
    a = -(k/m)
    dt = 0.1
    res = 0.1

    for i in range(0,100):
        print(atualX)
        proxV = atualV + a*atualX*dt - res*atualX
        proxX = atualX + atualV*dt
        ana = analitica(1,1,dt*i)
        real.append(ana)
        print(proxX)
        aproxE.append(proxX)
        atualV = proxV
        atualX = proxX

    plt.plot(real, color = 'red', label = 'Analitica')
    plt.plot(aproxE, color = 'blue', label = 'Método de Euler')
    plt.grid(True)
    plt.xlabel('x = Tempo (s)')
    plt.ylabel('y = Posição (m)')
    plt.legend(loc = 'upper left')
    plt.show()

main()


import sqlite3, ast

current_path= '../results.db'
def getRows():
	'''
	Returns all the constituencies with current cases and other details
	'''
	conn = sqlite3.connect(current_path) #change path here
	c = conn.cursor()
	c.execute(f'''select * from main;''')
	rows=c.fetchall()
	c.execute(f'''PRAGMA table_info(main);''')
	column_names=[t[1] for t in c.fetchall()]
	c.close()
	return rows, column_names


"""
This file contains functions required to perform different
operations on customer database
"""

import logging
from customer_model import  Customer, peewee, DB

CUSTOMER_LIST = [('Andrew', 'peterson', '344 james ave' \
                  , 6308153728, 'a_peteerson@mail.com', True, 4500), \
                  ('Wang', 'Wou', '103 spring ave', \
                   2223334456, 'wang_wou@gmail.com', False, 22000)]
NAME = 0
LASTNAME = 1
ADDRESS = 2
PHONE = 3
EMAIL = 4
STATUS = 5
LIMIT = 6

def my_logger():
    """logger function to setup logging"""

    log_format = "<<%(asctime)s %(filename)s:" \
                 "Line:%(lineno)-3d %(levelname)s>> %(message)s"

    #define log file name format
    log_file_name = 'db.log'

    #assign log_format to formatter of logging
    formatter = logging.Formatter(log_format)

    f_handler = logging.FileHandler(log_file_name)
    f_handler.setFormatter(formatter)
    f_handler.setLevel('DEBUG')

    c_handler = logging.StreamHandler()
    c_handler.setFormatter(formatter)
    c_handler.setLevel('INFO')

    #create logger
    logger = logging.getLogger()
    logger.addHandler(f_handler)
    logger.addHandler(c_handler)
    logger.setLevel('DEBUG')

    return logger

def db_init():
    """
    Function to initialize DB, create/add
    tables and add customer data to the DB
    """
    DB.init('customer.db')
    DB.create_tables([Customer])
    [add_customer(customer) for customer in CUSTOMER_LIST if customer is not None]

def add_customer(customer):

    """
    This function will add new custome to Customer table
    in customer.db
    """
    try:
        with DB.transaction():
            logging.info('Adding \"%s\" as new customer', (customer[NAME], customer[LASTNAME]))
            new_customer = Customer.create(
                name=customer[NAME],
                lastname=customer[LASTNAME],
                home_address=customer[ADDRESS],
                phone_number=customer[PHONE],
                email_address=customer[EMAIL],
                status=customer[STATUS],
                credit_limit=customer[LIMIT])
            logging.debug('New Customer : \"%s \" added to the database',\
                        (customer[NAME], customer[LASTNAME]))
            new_customer.save()
    except peewee.IntegrityError as exp:
        logging.debug('IntegrityError, Error adding customer %s', exp)

def search_customer(c_id):
    """
    This function will return a dictionary object with name,
    lastname, email address and phone number of a customer or
    an empty dictionary object if no customer was found.
    """
    a_customer_dict = dict()
    try:
        a_customer = Customer.get(Customer.id == c_id)
        a_customer_dict = {'id': a_customer.id,
                           'name': a_customer.name,
                           'last_name': a_customer.lastname,
                           'phone_number': a_customer.phone_number,
                           'email_address': a_customer.email_address,}

        return a_customer_dict

    except Customer.DoesNotExist as exp:
        logging.debug('No customer with id=%s was found', c_id)
        logging.debug(exp)
    return a_customer_dict

def del_customer(c_id):
    """
    This function will delete a customer from the sqlite3 database.
    """
    try:
        logging.info('Searching for customer with id = %s', c_id)
        a_customer = Customer.get(Customer.id == c_id)
        a_customer.delete_instance()
        logging.info('customer %s with id = %s deleted', a_customer.name, c_id)

    except Customer.DoesNotExist as exp:
        logging.debug('No customer with id=%s was found', c_id)
        logging.debug(exp)

def update_customer_credit(c_id, credit_limit):
    """
    This function will search an existing customer by customer_id
    and update their credit limit or raise a ValueError exception if the customer does not exist.
    """

    try:
        logging.info('Searching for customer with id = %s', c_id)
        customer = search_customer(c_id)
        a_customer = Customer.get(Customer.id == c_id)
        logging.info('old credit limit = %s', a_customer.credit_limit)
        a_customer.credit_limit = credit_limit
        logging.info('New credit limit = %s', a_customer.credit_limit)
        a_customer.save()
        return customer
    except Customer.DoesNotExist:
        logging.info('No record found for customer_id %s', c_id)

def list_active_customers():
    """
    This function will return an integer with the number of
    customers whose status is currently active.
    """
    active_customer_count = Customer.select().where(Customer.status).count()
    return active_customer_count

# def query_all_customers():
#     """ Generator to return customer_id for all the
#     customers int the DB"""
#     query = Customer.select()
#     c_id = (customer.id for customer in query)
#     return c_id

def query_all_customers():
    """Return entries for all the customers
    in the DB """
    query = Customer.select()
    c_id = (customer.id for customer in query)
    all_customers = [search_customer(customer_id) for customer_id in c_id]
    return all_customers

if __name__ == '__main__':
    my_logger()
    db_init()


from __future__ import print_function
import sys
import os
import keras
from tensorflow.python.platform import gfile
import numpy as np
import tensorflow as tf
from tensorflow.python.layers.core import Dense
from utils.data_manager import load_data
from utils.vocab import load_vocab_all
from utils.bleu import moses_multi_bleu
from collections import defaultdict
from argparse import ArgumentParser
import utils.vocab as vocab
import sys
reload(sys)
sys.setdefaultencoding('utf8')
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

_PAD = vocab._PAD
_GO = vocab._GO
_END = vocab._END

def train(sess, env, X_data, y_data, epochs=10, load=False, shuffle=True, batch_size=128,
          name='model', base=0, model2load=''):
    """
    Train TF model by env.train_op
    """
    if load:
        print('\nLoading saved model')
        env.saver.restore(sess, model2load )

    print('\nTrain model')
    n_sample = X_data.shape[0]
    n_batch = int((n_sample+batch_size-1) / batch_size)
    for epoch in range(epochs):
        print('\nEpoch {0}/{1}'.format(epoch+1, epochs))
        sys.stdout.flush()
        if shuffle:
            print('\nShuffling data')
            ind = np.arange(n_sample)
            np.random.shuffle(ind)
            X_data = X_data[ind]
            y_data = y_data[ind]

        for batch in range(n_batch):
            print(' batch {0}/{1}'.format(batch+1, n_batch),end='\r')
            start = batch * batch_size
            end = min(n_sample, start+batch_size)
            sess.run(env.train_op, feed_dict={env.x: X_data[start:end],
                                              env.y: y_data[start:end],
                                              env.training: True})

        evaluate(sess, env, X_data, y_data, batch_size=batch_size)

        if (epoch+1) == epochs:
            print('\n Saving model')
            env.saver.save(sess, 'model/{0}-{1}'.format(name, base))
    return 'model/{0}-{1}'.format(name, base)

def evaluate(sess, env, X_data, y_data, batch_size=128):
    """
    Evaluate TF model by running env.loss and env.acc.
    """
    print('\nEvaluating')

    n_sample = X_data.shape[0]
    n_batch = int((n_sample+batch_size-1) / batch_size)
    loss, acc = 0, 0

    for batch in range(n_batch):
        print(' batch {0}/{1}'.format(batch+1, n_batch),end='\r')
        sys.stdout.flush()
        start = batch * batch_size
        end = min(n_sample, start+batch_size)
        cnt = end - start
        batch_loss, batch_acc = sess.run(
            [env.loss,env.acc],
            feed_dict={env.x: X_data[start:end],
                       env.y: y_data[start:end]})
        loss += batch_loss * cnt
        acc += batch_acc * cnt
    loss /= n_sample
    acc /= n_sample

    print(' loss: {0:.4f} acc: {1:.4f}'.format(loss, acc))
    return acc

def _decode_data(sess, env, X_data, batch_size, reverse_vocab_dict):
    print('\nDecoding')
    logics_all = []
    n_sample = X_data.shape[0]
    n_batch = int((n_sample+batch_size-1) / batch_size)
    for batch in range(n_batch):
        print(' batch {0}/{1}'.format(batch+1, n_batch),end='\r')
        sys.stdout.flush()
        start = batch * batch_size
        end = min(n_sample, start+batch_size)
        cnt = end - start
        ybar = sess.run(env.pred_ids,
                        feed_dict={env.x: X_data[start:end]})
        ybar = np.asarray(ybar)
        ybar = np.squeeze(ybar[:,0,:])  # pick top prediction
        for seq in ybar:
            seq = np.append(seq, _END)
            seq = seq[:list(seq).index(_END)]
            logic = " ".join([reverse_vocab_dict[idx] for idx in seq])
            logics_all.append(logic)
    return logics_all

def decode_data_recover(sess, env, args, X_data, y_data, s, batch_size=128):
    """
    Inference and calculate EM acc based on recovered SQL
    """
    annotation_path = args.annotation_path
    i, acc = 0, 0
    _, reverse_vocab_dict, _, _ = load_vocab_all(args)
    inf_logics = _decode_data(sess, env, X_data, batch_size, reverse_vocab_dict)
    xtru, ytru = X_data, y_data
    with gfile.GFile(annotation_path+'%s_infer.txt'%s, mode='w') as output, gfile.GFile(annotation_path+'%s_ground_truth.txt'%s, mode='r') as S_ori_file, \
        gfile.GFile(annotation_path+'%s_sym_pairs.txt'%s, mode='r') as sym_pair_file:

        sym_pairs = sym_pair_file.readlines()  # annotation pairs from question & table files
        S_oris = S_ori_file.readlines()  # SQL files before annotation
        for true_seq, logic, x, sym_pair, S_ori in zip(ytru, inf_logics, xtru, sym_pairs, S_oris):
            sym_pair = sym_pair.replace('<>\n','')
            S_ori = S_ori.replace('\n','')
            Qpairs = []
            for pair in sym_pair.split('<>'):
                Qpairs.append(pair.split('=>'))

            true_seq = true_seq[1:]    #delete <bos>
            x = x[1:]   #delete <bos>

            true_seq = true_seq[:list(true_seq).index(_END)]
            x = np.append(x, _END)
            x = x[:list(x).index(_END)]

            xseq = " ".join([reverse_vocab_dict[idx] for idx in x])
            true_logic = " ".join([reverse_vocab_dict[idx] for idx in true_seq])

            logic = logic.replace(' (','').replace(' )','')
            true_logic = true_logic.replace(' (','').replace(' )','')

            logic = _switch_cond(logic, true_logic)

            recover_S = logic
            for sym, word in Qpairs:
                recover_S = recover_S.replace(sym, word)

            acc += (recover_S==S_ori)
            output.write(recover_S + '\n')
            i += 1

    print('EM: %.4f'%(acc*1./len(y_data)))
    print('number of correct ones:%d'%acc)

    return acc*1./len(y_data)

def decode_data(sess, env, args, X_data, y_data, batch_size=128, filename='output.txt'):
    """
    Inference w/o recover annotation symbols
    """
    i, acc = 0, 0
    _, reverse_vocab_dict, _, _ = load_vocab_all(args)
    logics_all = _decode_data(sess, env, X_data, batch_size, reverse_vocab_dict)
    xtru, ytru = X_data, y_data

    for true_seq, logic, x in zip(ytru, logics_all, xtru):
        true_seq = true_seq[1:]
        x = x[1:]
        true_seq = np.append(true_seq, _END)
        x = np.append(x, _END)

        true_seq=true_seq[:list(true_seq).index(_END)]
        x=x[:list(x).index(_END)]

        xseq = " ".join([reverse_vocab_dict[idx] for idx in x ])
        true_logic = " ".join([reverse_vocab_dict[idx] for idx in true_seq ])

        logic = logic.replace(' (','').replace(' )','')
        true_logic = true_logic.replace(' (','').replace(' )','')
        logic = _switch_cond(logic, true_logic)

        acc += (logic==true_logic)
        i += 1
    print('EM: %.4f'%(acc*1./len(y_data)))
    print('number of correct ones:%d'%acc)

    return acc*1./len(y_data)

def _switch_cond(logic, true_logic):
    logic_tokens = logic.split()
    if len(logic_tokens) > 8 and logic_tokens[5] == 'and':
        newlogic = [x for x in logic_tokens]
        newlogic[2], newlogic[6], newlogic[4], newlogic[8] = logic_tokens[6], logic_tokens[2], logic_tokens[8], logic_tokens[4]
        newline = ' '.join(newlogic)
        if newline == true_logic:
            logic = newline
    elif len(logic_tokens) > 9 and logic_tokens[6] == 'and':
        newlogic = [x for x in logic_tokens]
        newlogic[3], newlogic[7], newlogic[5], newlogic[9] = logic_tokens[7], logic_tokens[3], logic_tokens[9], logic_tokens[5]
        newline = ' '.join(newlogic)
        if newline == true_logic:
            logic = newline
    return logic


import json
import codecs
import random
import multiprocessing
from multiprocessing import Process, Lock
from multiprocessing.dummy import Pool as ThreadPool
# from pprint import pprint
from char_level import CharLevel
from access.wikiclient_v2 import WikiClient

class generation(object):

    def __init__(self, from_language, to_language):
        self.client = WikiClient(from_language)
        self.to_language = to_language
        self.translits = dict()
        self._lock = Lock()

    def get_textfile(self):
        file_path = 'C:\\Users\\Administrator\\Desktop\\wiki_raw_100K_row.txt'
        self.client.extract_text(file_path, is_char=False, count=100000)

    def get_mapping(self, file):
        with codecs.open(file, encoding="utf-8-sig") as m:
            mapping = json.load(m)
        return mapping

    def translit(self,map_file):
        result = ''
        mapping = self.get_mapping(map_file)
        pool = ThreadPool(multiprocessing.cpu_count())

        rows =[]
        with codecs.open('C:\\Users\\Administrator\\Downloads\\data\\raw\\wiki_raw_100K_row.txt', 'r', encoding="utf-8") as text_file:
            rows = text_file.read().split("\n")

        per_batch_size = len(rows) // multiprocessing.cpu_count()

        to_be_processed = [(rows[i * per_batch_size:i * per_batch_size + per_batch_size],mapping,i) for i in range(multiprocessing.cpu_count())]
        results = pool.map_async(self.translit_async, to_be_processed,callback=self.aprove_finish)
        results.wait()
        print("Finished")

    def aprove_finish(self,status):
        print("Aproove finish started")
        with codecs.open('C:\\Users\\Administrator\\Downloads\\data\\raw\\translit_text_100K_row.txt', 'w', encoding="utf-8") as translit_file:
            for i in range(multiprocessing.cpu_count()):
                translit_file.write(self.translits[i])
        print("Aproove finish finished")

    def translit_async(self, batch):
        print("Aync translitaration started {}".format(batch[2]))
        thread_translits = ""

        try:
            for line in batch[0]:
                row = ""
                for char in line:
                    if char in batch[1].keys():
                        row += random.choice(batch[1][char])
                    else:
                        row += char
                thread_translits+= row.replace("\n"," ") + "\n"
        except Exception as ex:
            print(ex)
        with self._lock:
            self.translits[batch[2]] = thread_translits
        print("Aync translitaration finished {}".format(batch[2]))
        return 0
lan1: str = 'hy'
lan2: str = 'en'
g = generation(lan1, lan2)

g.get_textfile()
map_file = 'C:\\Users\\Administrator\\Downloads\\data-master\\data-master\\mappings\\hy-Latn.json'
w = WikiClient(lan1)
#w.get_titles()
#w.extract_text(f,False,100000)
g.translit(map_file)

path_target_from = 'C:\\Users\\Administrator\\Downloads\\data\\raw\\translit_text_100K_row.txt'
path_target_to = 'C:\\Users\\Administrator\\Downloads\\data\\raw\\translit_text_100K_row_char.txt'

## call function on the files
cl = CharLevel()
cl.split_to_char(path_target_from, path_target_to)


#!/usr/bin/env python
import numpy as np
import cv2

from commonFunctions_v06 import get_info_from_logfile
from commonFunctions_v06 import flip_horizontally
from commonFunctions_v06 import visualize_loss_history

# History
# v01 : Start
# v02 : add nb_images to read parameter
# v03 : add normalization + mean centering data to 0
# v04 : data augmentation flip horizontally image + inverse measurements
# v05 : use left/right images + measurements with Steering error correction
# v06 : cropping images
# v07 : add a generator to load data and preprocess it on the fly, in batchsize portions
#        to feed into your Behavioral Cloning model .
# v08 : Adding loss viusalization tool
# v09 : Re-start from v06 as fit_generator and need to add generator obsolete.
#       Latest Keras.Model.fit integrates a generator in itself.
#       ie v09 : Visualize loss history

STEER_CORRECTION_FACTOR = 0.2 # to tune up for left and right images/measurements

# Set our batch size for fit generator
batch_size=32

# get images + steering angle measurements
images, measurements = get_info_from_logfile(STEER_CORRECTION_FACTOR,nb_images=100)

# data augmentation flip horizontally image + inverse measurements
augm_images, augm_measurements = flip_horizontally(images,measurements)
images.extend(augm_images)
measurements.extend(augm_measurements)

X_train = np.array(images)
y_train = np.array(measurements)

#print(f'X_train shape : {X_train.shape}')
#print(f'images shape : {im.shape}')

from keras.models import Sequential
from keras.layers import Flatten, Dense, Lambda, Cropping2D
from keras.callbacks import ModelCheckpoint,EarlyStopping

model = Sequential()
model.add(Lambda(lambda x: ((x/255) - 0.5),input_shape=(160,320,3)))
model.add(Cropping2D(cropping=((70,25),(0,0))))
model.add(Flatten())
model.add(Dense(1))

model.compile(loss='mse', optimizer='adam')
# Callbacks to save best model and prevent overfit by early stopping
checkpoint = ModelCheckpoint(filepath='bestModelFolder/model.{epoch:02d}-{val_loss:.2f}.h5', monitor='val_loss', save_best_only=True)
stopper = EarlyStopping(monitor='val_loss', min_delta=0.0003, patience=10)
# model.fit(callbacks=[checkpoint, stopper])
history_object = model.fit(X_train,y_train, batch_size, validation_split=0.2, shuffle = True, epochs=10, callbacks=[checkpoint, stopper])

'''
fit(
    x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None,
    validation_split=0.0, validation_data=None, shuffle=True, class_weight=None,
    sample_weight=None, initial_epoch=0, steps_per_epoch=None,
    validation_steps=None, validation_batch_size=None, validation_freq=1,
    max_queue_size=10, workers=1, use_multiprocessing=False
)
'''

model.save('model.h5')

# save picture lossHistory.png
visualize_loss_history(history_object)


# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <codecell>
# Fan Zhang 2015/10/14

import sys
import os
import numpy as np
import scipy as sp
from scipy.stats import beta
import matplotlib.pyplot as plt
import matplotlib
import h5py
import pdb
import rvd27
import rvd3

# Insert the src/python/directory at front of the path
rvddir = os.path.join('../../src/python/rvd27')
sys.path.insert(0, rvddir)

# Plot mu of true positive position (344, 84)
# Plot mu of false positive position (38, 263)
position = [299]
dsample = 10000

path_mcmc = "../2015-10-09_Run_rvd2_with_without_chi2_for_comparison_with_rvd3_synthetic_data/hdf5"
control_mcmc='%s/%d/Control.hdf5' %(path_mcmc, dsample)
case_mcmc='%s/%d/Case0_1.hdf5' %(path_mcmc, dsample)

path_var = "../2015-09-28_Run_rvd3_synthetic_data_set/hdf5"
control_var='%s/%d/Control.hdf5' %(path_var, dsample)
case_var='%s/%d/Case0_1.hdf5' %(path_var, dsample)

def main():
    ################### Read mu of MCMC (rvd2) ################################
    with h5py.File(control_mcmc, 'r') as f:
            muControl = f['mu'][...]
            locControl = f['loc'][...]
    with h5py.File(case_mcmc, 'r') as f:
            muCase = f['mu'][...]
            locCase = f['loc'][...]
    idx = []
    for pos in position:
            idx.append(pos)
            muControl1 = muControl[idx]
            muCase1 = muCase[idx]
            #N = 2000
            #(muZ,_,_) =rvd27.sample_post_diff(muCase1, muControl1, N) # sample Z

    ## plot histogram
    num_bins = 25
    for i in xrange(len(position)):
            fig = plt.figure(figsize=(12,8))

            ########### Plot mu of MCMC (rvd2) vs Variational (rvd3) ##################
            # normed=True, the integral of the histogram will sum to 1.
            plt.hist(muCase1[i,:].T, num_bins, normed=True, facecolor='r', alpha=0.5, label = 'Case (MCMC)')
            plt.hist(muControl1[i,:].T, num_bins, normed=True, facecolor='k', alpha=0.5, label = 'Control (MCMC)')

            ############# Plot mu of Variational (rvd3) ################################
            caseR, caseN, casephi, caseq, loc, refb = rvd3.load_model(case_var)
            casegam = caseq['gam']
            a = casegam[position, 0]
            b = casegam[position, 1]
            cov_case = int(np.median(caseN))
            x_case = np.linspace(beta.ppf(0.001, a, b), beta.ppf(0.999, a, b), 100)
            plt.plot(x_case, beta.pdf(x_case, a, b), 'r--', lw=4, alpha=1.0, label="Case (Variational)")
            r_case = beta.rvs(a, b, size=2000)
            plt.hist(r_case, num_bins, normed=True, histtype='stepfilled', alpha=0.2, facecolor='r')

            controlR, controlN, controlphi, controlq, _, _ = rvd3.load_model(control_var)
            controlgam = controlq['gam']
            a = controlgam[position, 0]
            b = controlgam[position, 1]
            x_control = np.linspace(beta.ppf(0.001, a, b), beta.ppf(0.999, a, b), 100)
            plt.plot(x_control, beta.pdf(x_control, a, b), 'k--', lw=4, alpha=1.0, label='Control (Variational)')
            r_control = beta.rvs(a, b, size=2000)
            plt.hist(r_control, num_bins, normed=True, histtype='stepfilled', alpha=0.2, facecolor='k')
            plt.xlim(0, 0.012)
            plt.legend(loc='best', frameon=False)
            plt.xlabel('$\hat{\mu} = \mu-\mu_0$', fontsize = 20)
            plt.xticks(rotation=25)
            plt.title('$\hat{\mu}$ at position %s when median depth is %d' %((position[i]+1), cov_case), fontsize = 18)
            plt.xticks(rotation=25)
            plt.savefig('position_%s_%d_mcmc_vs_var.png' %((position[i]+1), cov_case))
            plt.tight_layout()

if __name__ == "__main__":
     main()


from collections import Counter, namedtuple
import csv
import re

import requests

MARVEL_CSV = 'https://raw.githubusercontent.com/pybites/marvel_challenge/master/marvel-wikia-data.csv'  # noqa E501

Character = namedtuple('Character', 'pid name sid align sex appearances year')

# csv parsing code provided so this Bite can focus on the parsing

def _get_csv_data():
    """Download the marvel csv data and return its decoded content"""
    with requests.Session() as session:
        return session.get(MARVEL_CSV).content.decode('utf-8')

def load_data():
    """Converts marvel.csv into a sequence of Character namedtuples
       as defined above"""
    content = _get_csv_data()
    reader = csv.DictReader(content.splitlines(), delimiter=',')
    for row in reader:
        name = re.sub(r'(.*?)\(.*', r'\1', row['name']).strip()
        yield Character(pid=row['page_id'],
                        name=name,
                        sid=row['ID'],
                        align=row['ALIGN'],
                        sex=row['SEX'],
                        appearances=row['APPEARANCES'],
                        year=row['Year'])

characters = list(load_data())

# start coding

def most_popular_characters(characters=characters, top=5):
    """Get the most popular character by number of appearances,
       return top n characters (default 5)
    """

    def appearances(character):
        try:
            return int(character.appearances)
        except ValueError:
            return 0

    sorted_characters = sorted(
        characters,
        key=appearances,
        reverse=True
    )

    return [character.name for character in sorted_characters][:top]

def max_and_min_years_new_characters(characters=characters):
    """Get the year with most and least new characters introduced respectively,
       use either the 'FIRST APPEARANCE' or 'Year' column in the csv
       characters, or the 'year' attribute of the namedtuple, return a tuple
       of (max_year, min_year)
    """
    first_appearances_by_year = Counter(
        character.year
        for character in characters
        if character.year
    )

    [(max_year, _)] = first_appearances_by_year.most_common(1)
    min_year, _ = first_appearances_by_year.most_common()[-1]

    return max_year, min_year

def get_percentage_female_characters(characters=characters):
    """Get the percentage of female characters as percentage of all genders
       over all appearances.
       Ignore characters that don't have gender ('sex' attribue) set
       (in your characters data set you should only have Male, Female,
       Agender and Genderfluid Characters.
       Return the result rounded to 2 digits
    """

    def total_appearances(sex=None):
        def condition(character):
            if sex is None:
                return True
            else:
                return character.sex == sex

        return len([
            character
            for character in characters
            if character.sex and condition(character)
        ])

    female_count = total_appearances(sex="Female Characters")

    all_gender_count = total_appearances()

    return round(female_count/all_gender_count*100, 2)


#!/usr/bin/env python

# coding:utf-8
from __future__ import print_function
import numpy as np
import argparse

def parse_args():
    parser = argparse.ArgumentParser(description='prepare orbitalidx.def')
    parser.add_argument('-L',metavar='L',dest='L',type=int,default=6,help='set L')
    parser.add_argument('-output',metavar='output_file',dest='output_file',type=str,default='orbitalidx.def',help='set output file name')
    return parser.parse_args()

## output header
def print_header(output_file,L):
    f = open(output_file,'w')
    Nud = L
    Nuu = L//2
    Ndd = L//2
    Nidx = Nud + Nuu + Ndd
    f.write('========================\n')
    f.write('NOrbitalIdx           %d\n' % Nidx)
    f.write('ComplexType           %d\n' % 1)
    f.write('========================\n')
    f.write('========================\n')
    f.close()

def print_data(output_file,L):
    f = open(output_file,'a')
    Nud = L
    Nuu = L//2
    s = 0
    t = 1
    for i in range(L):
        for j in range(L):
            sgn = 1
            orb = (j-i+L)%L
            if i>j: sgn = -1
            f.write('{} {} {} {} {} {}\n'.format(i,s,j,t,orb,sgn))
    for spin in range(2):
        s = spin
        t = spin
        for i in range(L):
            for j in range(i+1,L):
                sgn = 1
#                orb = (j-i+L-1)%L
                orb = np.min([(j-i+L-1)%L,(i-j+L-1)%L])
                orb += Nud
                orb += Nuu*spin
                f.write('{} {} {} {} {} {}\n'.format(i,s,j,t,orb,sgn))
    f.close()

def print_on_off(output_file,L):
    f = open(output_file,'a')
    Nud = L
    Nuu = L//2
    Ndd = L//2
    for i in range(Nud):
        f.write('{} {}\n'.format(i,1))
    for i in range(Nud,Nud+Nuu):
        f.write('{} {}\n'.format(i,1))
    for i in range(Nud+Nuu,Nud+Nuu+Ndd):
        f.write('{} {}\n'.format(i,1))
    f.close()

## main
def main():
    args = parse_args()
    L = args.L
    output_file = args.output_file
    print('L',L)

    print_header(output_file,L)
    print_data(output_file,L)
    print_on_off(output_file,L)

if __name__ == "__main__":
    main()


"""
<Program>
  test_deptools.py

<Purpose>
  Some unit tests for the dep tools and resolvability assesser.
  (Can restructure to be more standard later.)

"""

import json
import os

import depresolve # __init__ for errors

import depresolve.deptools as deptools

from tests.testdata import *

def main():
  """
  """
  test_deptools()

  print("All tests in main() OK (:")

def test_deptools():
  """
  """
  assert 41 == len(DEPS_MODERATE), \
      "Set changed: should be len 41 but is len " + str(len(DEPS_MODERATE)) + \
      " - reconfigure tests"

  json.dump(DEPS_MODERATE, open('data/test_deps_set.json', 'w'))

  deps = deptools.load_raw_deps_from_json('data/test_deps_set.json')

  assert DEPS_MODERATE == deps, \
      "JSON write and load via load_raw_deps_from_json is breaking!"

  versions_by_package = deptools.generate_dict_versions_by_package(deps)

  assert 10 == len(versions_by_package) # different package names

  total_package_versions = \
      sum([len(versions_by_package[p]) for p in versions_by_package])

  assert 41 == total_package_versions, \
      "Wrong number of versions: " + str(total_package_versions) + "instead" \
      + "of 41."

  (edeps, packs_wout_avail_version_info, dists_w_missing_dependencies) = \
      deptools.elaborate_dependencies(deps, versions_by_package)

  #print(str(len(edeps)))
  from pprint import pprint
  #pprint(edeps)

  # 1 entry in the edeps dict
  assert 41 == len(edeps), \
      "Wrong number of dists in elaborate_dependencies output. " + \
      str(len(edeps)) + "instead of 41."

  # 1 version listed for every possible satisfying dependency
  n_dependencies_elaborated = 0
  for distkey in edeps:
    for satisfying_package_entry in edeps[distkey]:
      list_of_satisfying_versions = satisfying_package_entry[1]
      n_dependencies_elaborated += len(list_of_satisfying_versions)
      #print(distkey + " -> " + str(list_of_satisfying_versions))

  assert 34 == n_dependencies_elaborated, \
      "Expecting 34 satisfying versions (1 for every [depending dist]" + \
      ",[satisfying_version] pair. Instead, got " + \
      str(n_dependencies_elaborated)

  assert deptools.are_deps_valid(DEPS_MODERATE) and \
      deptools.are_deps_valid(DEPS_SIMPLE), \
      'The test dependencies are coming up as invalid for some reason....'

  # Clear any pre-existing test database.
  deptools.sqli.initialize(db_fname='data/test_dependencies.db')
  deptools.sqli.delete_all_tables()

  deptools.populate_sql_with_full_dependency_info(
      edeps, versions_by_package, packs_wout_avail_version_info,
      dists_w_missing_dependencies, db_fname='data/test_dependencies.db')

  print("test_deptools(): All tests OK.")

if __name__ == '__main__':
  main()


from pubnub import Pubnub
import time
import random
from nose.tools import with_setup

pubnub = Pubnub("ds", "ds")
pubnub_enc = Pubnub(publish_key="ds", subscribe_key="ds", cipher_key="enigma")
pubnub_pam = Pubnub(publish_key="pam", subscribe_key="pam", secret_key="pam")

def rand(msg):
    return "rand-" + str(random.random()) + "-" + msg

channel = rand("channel")
channel_enc = rand("channel_enc")
channel_pam = rand("channel_pam")

messages = []

def setup_func():
    pubnub_pam.grant(channel=channel_pam, read=True, write=True, ttl=144000)

    for i in range(0, 20):
        msg = rand("message-" + str(i))
        messages.append(msg)
        print(pubnub.publish(channel=channel, message=msg))
        # Fails with Python 3
        # print(pubnub_enc.publish(channel=channel_enc, message=msg))
        print(pubnub_pam.publish(channel=channel_pam, message=msg))

@with_setup(setup_func)
def test_1():
    time.sleep(3)
    hresp = pubnub.history(channel=channel, count=20)
    # Fails with Python 3
    # hresp2 = pubnub_enc.history(channel=channel_enc, count=20)
    hresp3 = pubnub_pam.history(channel=channel_pam, count=20)
    hresp4 = pubnub_pam.history(channel=channel_pam + "no_rw", count=20)
    assert hresp[0] == messages
    # Fails with Python 3
    # assert hresp2[0] == messages
    assert hresp3[0] == messages
    assert hresp4['message'] == 'Forbidden'
    assert channel_pam + "no_rw" in hresp4['payload']['channels']

def test_2():
    time.sleep(3)
    hresp = pubnub.history(channel=channel, count=20, include_token=True)
    # Fails with Python 3
    # hresp2 = pubnub_enc.history(channel=channel_enc, count=20, include_token=True)
    hresp3 = pubnub_pam.history(channel=channel_pam, count=20, include_token=True)
    hresp4 = pubnub_pam.history(channel=channel_pam + "no_rw", count=20, include_token=True)
    assert len(hresp[0]) == len(messages)
    assert hresp[0][0]['timetoken']
    # Fails with Python 3
    # assert len(hresp2[0]) == len(messages)
    # assert hresp2[0][0]['timetoken']
    assert len(hresp3[0]) == len(messages)
    assert hresp3[0][0]['timetoken']
    assert hresp4['message'] == 'Forbidden'
    assert channel_pam + "no_rw" in hresp4['payload']['channels']


"""news_website URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
#View All
from django.conf.urls import url,include
from django.conf.urls.static import static
from django.conf import settings
from django.contrib import admin
from swnews_app import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^accounts/', include('django.contrib.auth.urls')),
    url(r'^i18n/', include('django.conf.urls.i18n')),
    url(r'^captcha/', include('captcha.urls')),
    url(r'^accounts/new_user$', views.upload_newUser),
    url(r'^$', views.NewsListHome.as_view(),name = 'home'),
    url(r'^submit_new/$', views.upload_new,name = 'submit_new'),
    url(r'^thanks/$', views.thank_you,name = 'thanks'),
    url(r'^terms/$', views.terms_f,name = 'terms'),
    url(r'^privacy/$', views.privacy_f,name = 'privacy'),
    url(r'^about/$', views.about_f,name = 'about'),
    url(r'^search/$', views.search_view,name = 'search_html'),
    url(r'^news/(?P<pk>\d+)$', views.NewDetailView.as_view(), name='new-detail'),
    url(r'^reporters/(?P<pk>\d+)$', views.ReporterDetailView.as_view(), name='user-detail'),
    url(r'^allNews/$', views.AllNews.as_view(), name='all-news'),
    url(r'^allNews/subs/$', views.AllNewsFollow.as_view(), name='all-subs'),
    url(r'^ajax/unfollows_reporter/$',views.unfollow_reporter,name ='unfollow_reporter'),
    url(r'^ajax/follows_reporter/$',views.follow_reporter,name ='follow_reporter'),
    url(r'^ajax/unfollows_userpage/$',views.unfollow_userpage,name ='unfollow_userpage'),
    url(r'^ajax/deletes_userpage/$',views.delete_userpage,name ='delete_userpage'),
    url(r'^ajax/edits_new/$',views.edit_new,name ='edit_new'),
    url(r'^ajax/deletes_new/$',views.delete_new,name ='delete_new'),
    url(r'^ckeditor/', include('ckeditor_uploader.urls')),
    url(r'^es/$',  views.home_view_es,name = 'eshome'),
    url(r'^en/$',  views.home_view_en,name = 'enhome'),
    #url(r'^home/$',  views.AllNewsFront.as_view(),name = 'secondhome'),
    # url(r'^setLocation/$', views.set_local,name = 'set_location'),
    #url(r'^allNews/world/$', views.AllNewsWorld.as_view(), name='all-news-world'),
    #url(r'^allNews/local/$', views.AllNewsLocal.as_view(), name='all-news-local'),
    #url(r'^allNews/sports/$', views.AllNewsSport.as_view(), name='all-news-sports'),
    #url(r'^allNews/economy/$', views.AllNewsEconomy.as_view(), name='all-news-economy'),
    #url(r'^allNews/weather/$', views.AllNewsWeather.as_view(), name='all-news-weather'),
    #url(r'^allNews/culture/$', views.AllNewsCulture.as_view(), name='all-news-culture'),
    #url(r'^allNews/science/$', views.AllNewsScience.as_view(), name='all-news-science'),
    #url(r'^allNews/oped/$', views.AllNewsEditorial.as_view(), name='all-news-op-ed'),

]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

#url(r'^allNews/(?P
\[a-z][^/]+)/$', views.AllNews1.as_view(), name='all-news'),
'''
url(r'^allNews/$', views.AllNews.as_view(), name='all-news-world'),
url(r'^allNews/world/$', views.AllNewsWorld.as_view(), name='all-news-world'),
    url(r'^allNews/local/$', views.AllNewsLocal.as_view(), name='all-news-local'),
    url(r'^allNews/sports/$', views.AllNewsSport.as_view(), name='all-news-sports'),
    url(r'^allNews/economy/$', views.AllNewsEconomy.as_view(), name='all-news-economy'),
    url(r'^allNews/weather/$', views.AllNewsWeather.as_view(), name='all-news-weather'),
    url(r'^allNews/culture/$', views.AllNewsCulture.as_view(), name='all-news-culture'),
    url(r'^allNews/science/$', views.AllNewsScience.as_view(), name='all-news-science'),
'''


import os
from glob import glob
import re
import sys
from TTS.tts.utils.generic_utils import split_dataset

def load_meta_data(datasets):
	meta_data_train_all = []
	meta_data_eval_all = []
	for dataset in datasets:
		name = dataset['name']
		root_path = dataset['path']
		meta_file_train = dataset['meta_file_train']
		meta_file_val = dataset['meta_file_val']
		preprocessor = get_preprocessor_by_name(name)

		meta_data_train = preprocessor(root_path, meta_file_train)
		if meta_file_val is None:
			meta_data_eval, meta_data_train = split_dataset(meta_data_train)
		else:
			meta_data_eval = preprocessor(root_path, meta_file_val)
		meta_data_train_all += meta_data_train
		meta_data_eval_all += meta_data_eval
	return meta_data_train_all, meta_data_eval_all

def get_preprocessor_by_name(name):
	"""Returns the respective preprocessing function."""
	thismodule = sys.modules[__name__]
	return getattr(thismodule, name.lower())

def tweb(root_path, meta_file):
	"""Normalize TWEB dataset.
	https://www.kaggle.com/bryanpark/the-world-english-bible-speech-dataset
	"""
	txt_file = os.path.join(root_path, meta_file)
	items = []
	speaker_name = "tweb"
	with open(txt_file, 'r') as ttf:
		for line in ttf:
			cols = line.split('\t')
			wav_file = os.path.join(root_path, cols[0] + '.wav')
			text = cols[1]
			items.append([text, wav_file, speaker_name])
	return items

# def kusal(root_path, meta_file):
#     txt_file = os.path.join(root_path, meta_file)
#     texts = []
#     wavs = []
#     with open(txt_file, "r", encoding="utf8") as f:
#         frames = [
#             line.split('\t') for line in f
#             if line.split('\t')[0] in self.wav_files_dict.keys()
#         ]
#     # TODO: code the rest
#     return  {'text': texts, 'wavs': wavs}

def mozilla(root_path, meta_file):
	"""Normalizes Mozilla meta data files to TTS format"""
	txt_file = os.path.join(root_path, meta_file)
	items = []
	speaker_name = "mozilla"
	with open(txt_file, 'r') as ttf:
		for line in ttf:
			cols = line.split('|')
			wav_file = cols[1].strip()
			text = cols[0].strip()
			wav_file = os.path.join(root_path, "wavs", wav_file)
			items.append([text, wav_file, speaker_name])
	return items

def mozilla_de(root_path, meta_file):
	"""Normalizes Mozilla meta data files to TTS format"""
	txt_file = os.path.join(root_path, meta_file)
	items = []
	speaker_name = "mozilla"
	with open(txt_file, 'r', encoding="ISO 8859-1") as ttf:
		for line in ttf:
			cols = line.strip().split('|')
			wav_file = cols[0].strip()
			text = cols[1].strip()
			folder_name = f"BATCH_{wav_file.split('_')[0]}_FINAL"
			wav_file = os.path.join(root_path, folder_name, wav_file)
			items.append([text, wav_file, speaker_name])
	return items

def mailabs(root_path, meta_files=None):
	"""Normalizes M-AI-Labs meta data files to TTS format"""
	speaker_regex = re.compile("by_book/(male|female)/(?P<speaker_name>[^/]+)/")
	if meta_files is None:
		csv_files = glob(root_path + "/**/metadata.csv", recursive=True)
	else:
		csv_files = meta_files
	# meta_files = [f.strip() for f in meta_files.split(",")]
	items = []
	for csv_file in csv_files:
		txt_file = os.path.join(root_path, csv_file)
		folder = os.path.dirname(txt_file)
		# determine speaker based on folder structure...
		speaker_name_match = speaker_regex.search(txt_file)
		if speaker_name_match is None:
			continue
		speaker_name = speaker_name_match.group("speaker_name")
		print(" | > {}".format(csv_file))
		with open(txt_file, 'r') as ttf:
			for line in ttf:
				cols = line.split('|')
				if meta_files is None:
					wav_file = os.path.join(folder, 'wavs', cols[0] + '.wav')
				else:
					wav_file = os.path.join(root_path, folder.replace("metadata.csv", ""), 'wavs', cols[0] + '.wav')
				if os.path.isfile(wav_file):
					text = cols[1].strip()
					items.append([text, wav_file, speaker_name])
				else:
					raise RuntimeError("> File %s does not exist!" % (wav_file))
	return items

def ljspeech(root_path, meta_file):
	"""Normalizes the Nancy meta data file to TTS format"""
	txt_file = os.path.join(root_path, meta_file)
	items = []
	speaker_name = "ljspeech"
	with open(txt_file, 'r') as ttf:
		for line in ttf:
			cols = line.split('|')
			wav_file = os.path.join(root_path, 'wavs', cols[0] + '.wav')
			text = cols[1]
			items.append([text, wav_file, speaker_name])
	return items

def nancy(root_path, meta_file):
	"""Normalizes the Nancy meta data file to TTS format"""
	txt_file = os.path.join(root_path, meta_file)
	items = []
	speaker_name = "nancy"
	with open(txt_file, 'r') as ttf:
		for line in ttf:
			utt_id = line.split()[1]
			text = line[line.find('"') + 1:line.rfind('"') - 1]
			wav_file = os.path.join(root_path, "wavn", utt_id + ".wav")
			items.append([text, wav_file, speaker_name])
	return items

def common_voice(root_path, meta_file):
	"""Normalize the common voice meta data file to TTS format."""
	txt_file = os.path.join(root_path, meta_file)
	items = []
	with open(txt_file, 'r') as ttf:
		for line in ttf:
			if line.startswith("client_id"):
				continue
			cols = line.split("\t")
			text = cols[2]
			speaker_name = cols[0]
			wav_file = os.path.join(root_path, "clips", cols[1] + ".wav")
			items.append([text, wav_file, speaker_name])
	return items

def libri_tts(root_path, meta_files=None):
	"""https://ai.google/tools/datasets/libri-tts/"""
	items = []
	if meta_files is None:
		meta_files = glob(f"{root_path}/**/*trans.tsv", recursive=True)
	for meta_file in meta_files:
		_meta_file = os.path.basename(meta_file).split('.')[0]
		speaker_name = _meta_file.split('_')[0]
		chapter_id = _meta_file.split('_')[1]
		_root_path = os.path.join(root_path, f"{speaker_name}/{chapter_id}")
		with open(meta_file, 'r') as ttf:
			for line in ttf:
				cols = line.split('\t')
				wav_file = os.path.join(_root_path, cols[0] + '.wav')
				text = cols[1]
				items.append([text, wav_file, speaker_name])
	for item in items:
		assert os.path.exists(item[1]), f" [!] wav files don't exist - {item[1]}"
	return items

def aishell_3(root_path, meta_file):
	items = []
	with open(os.path.join(root_path, meta_file), "r", encoding="utf-8") as f:
		while 1:
			line = f.readline()
			if not line:
				break
			filename, text = line.split("|")
			speaker_name = filename[:7]
			wav_file = os.path.join(root_path, meta_file.split("/")[0], "wav", speaker_name, filename + ".wav")
			items.append([text, wav_file, 'AISHELL_' + speaker_name])
	return items

def private_data(root_path, meta_file):
	items = []
	with open(os.path.join(root_path, meta_file), "r", encoding="utf-8") as f:
		while 1:
			line = f.readline()
			if not line:
				break
			filename, text = line.split("|")
			speaker_name = filename.split("_")[0]
			wav_file = os.path.join(root_path, "wavs", speaker_name, filename + ".wav")
			items.append([text, wav_file, 'private_' + speaker_name])
	return items

def m2voc(root_path, meta_file):
	items = []
	speakers = os.listdir(root_path)
	for speaker in speakers:
		with open(os.path.join(root_path, speaker, meta_file), "r", encoding="utf-8") as f:
			while 1:
				line = f.readline()
				if not line:
					break
				filename, text = line.split("|")
				speaker_name, _ = filename.split("_")
				assert speaker == speaker_name
				wav_file = os.path.join(root_path, speaker, "wavs", filename + ".wav")
				items.append([text, wav_file, 'm2voc_' + speaker_name])
	return items

def jiang(root_path, meta_file):
	items = []
	with open(os.path.join(root_path, meta_file), "r", encoding="utf-8") as f:
		while 1:
			line = f.readline()
			if not line:
				break
			filename, text = line.split("|")
			speaker_name = "jiang"
			wav_file = os.path.join(root_path, "wav", speaker_name, filename + ".wav")
			items.append([text, wav_file, speaker_name])
	return items

def baker(root_path, meta_file):
	txt_file = os.path.join(root_path, meta_file)
	items = []
	speaker_name = "baker"
	with open(txt_file, 'r') as ttf:
		for line in ttf:
			cols = line.split('|')
			wav_file = os.path.join(root_path, 'wavs', cols[0] + '.wav')
			text = cols[1]
			items.append([text, wav_file, speaker_name])
	return items

def niuman(root_path, meta_file):
	txt_file = os.path.join(root_path, meta_file)
	items = []
	speaker_name = "niuman"
	with open(txt_file, 'r') as ttf:
		for line in ttf:
			cols = line.split('|')
			wav_file = os.path.join(root_path, 'wavs', cols[0] + '.wav')
			text = cols[1]
			items.append([text, wav_file, speaker_name])
	return items

def haitian(root_path, meta_file):
	txt_file = os.path.join(root_path, meta_file)
	items = []
	speaker_name = "haitian031"
	with open(txt_file, 'r') as ttf:
		for line in ttf:
			cols = line.split('|')
			wav_file = os.path.join(root_path, 'wavs_multi', cols[0] + '.wav')
			text = cols[1]
			items.append([text, wav_file, speaker_name])
	return items

def custom_turkish(root_path, meta_file):
	txt_file = os.path.join(root_path, meta_file)
	items = []
	speaker_name = "turkish-female"
	skipped_files = []
	with open(txt_file, 'r', encoding='utf-8') as ttf:
		for line in ttf:
			cols = line.split('|')
			wav_file = os.path.join(root_path, 'wavs', cols[0].strip() + '.wav')
			if not os.path.exists(wav_file):
				skipped_files.append(wav_file)
				continue
			text = cols[1].strip()
			items.append([text, wav_file, speaker_name])
	print(f" [!] {len(skipped_files)} files skipped. They don't exist...")
	return items

# ToDo: add the dataset link when the dataset is released publicly
def brspeech(root_path, meta_file):
	'''BRSpeech 3.0 beta'''
	txt_file = os.path.join(root_path, meta_file)
	items = []
	with open(txt_file, 'r') as ttf:
		for line in ttf:
			if line.startswith("wav_filename"):
				continue
			cols = line.split('|')
			# print(cols)
			wav_file = os.path.join(root_path, cols[0])
			text = cols[2]
			speaker_name = cols[3]
			items.append([text, wav_file, speaker_name])
	return items

def vctk(root_path, meta_files=None, wavs_path='wav48'):
	"""homepages.inf.ed.ac.uk/jyamagis/release/VCTK-Corpus.tar.gz"""
	test_speakers = meta_files
	items = []
	meta_files = glob(f"{os.path.join(root_path, 'txt')}/**/*.txt", recursive=True)
	for meta_file in meta_files:
		_, speaker_id, txt_file = os.path.relpath(meta_file, root_path).split(os.sep)
		file_id = txt_file.split('.')[0]
		if isinstance(test_speakers, list):  # if is list ignore this speakers ids
			if speaker_id in test_speakers:
				continue
		with open(meta_file) as file_text:
			text = file_text.readlines()[0]
		wav_file = os.path.join(root_path, wavs_path, speaker_id, file_id + '.wav')
		items.append([text, wav_file, speaker_id])

	return items


#PARA VIGILAR LAS IP DE LOS SERVIDORES DE MORGAN MEDIA
#PARA INCLUIR UNA GUI EN ESTE PROGRAMA

from bs4 import BeautifulSoup
from selenium import webdriver
from time import sleep
import datetime, json

#PARA HACER HEADLESS EL NAVEGADOR
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True

#FUNCION PARA MODIFICAR EL LISTADO DE IP
def modificar():
    with open("./listado.json", "r") as read_file:
        dct_servers = json.load(read_file)

    if any(dct_servers):
        print('IP ya incluidas:')
        for x in dct_servers:
            sleep(1)
            print(f'{x}: {dct_servers[x]}')
    else:
        print('No hay IP')

    incluir_borrar = input('Quieres incluir o borrar una IP? (incluir/borrar): ')
    if incluir_borrar == "incluir":
        incluir_ip(dct_servers)
    elif incluir_borrar == "borrar":
        borrar_ip(dct_servers)
    else:
        print("No se hace nada")

#FUNCION PARA INCLUIR UNA IP
def incluir_ip(lista_ip):
    sleep(1)
    resultado = True

    while resultado:
        nombre = input("Nombre del servidor a incluir: ")
        ip = input("IP: ")
        resultado = comprobar_ip(ip)

    lista_ip[nombre] = ip
    with open("./listado.json", "w") as write_file:
        write_file.write(json.dumps(lista_ip))
    print("Incluido")

#FUNCION PARA BORRAR IP
def borrar_ip(lista_ip):
    sleep(1)
    comprobacion = True

    while comprobacion:
        nombre = input('Nombre del servidor a borrar:')
        comprobacion = revisar_srv(lista_ip,nombre)

    with open("./listado.json", "w") as write_file:
        write_file.write(json.dumps(lista_ip))
    print("Borrado")

#FUNCION PARA COMPROBAR QUE LA IP ES CORRECTA
def comprobar_ip (ip):
    numeros_ip = ip.split('.')
    if len(numeros_ip) ==4:
        for x in numeros_ip:
            try:
                int(x)
                if (int(x)<0 or int(x)>255):
                    print('ERROR EN LA IP: numero fuera de rango')
                    return True
            except:
                print('ERROR EN LA IP: no es un numero')
                return True
    else:
        print('ERROR EN LA IP: no tiene 4 campos')

#FUNCION PARA COMPROBAR EL SERVIDOR PARA BORRAR
def revisar_srv(ip,nombre):
    try:
        del ip[nombre]
    except:
        print('No se encuentra ese nombre de servidor')
        return True

#FUNCION PARA EJECUTAR EL ESCANEO
def escanear(options):
    with open("./listado.json", "r") as read_file:
        dct_servers = json.load(read_file)
    print('Estas monitorizando estas IP:')
    for x in dct_servers:
        sleep(1)
        print(f'{x}: {dct_servers[x]}')

    #FECHA
    today = datetime.date.today()

    sleep(1)
    print("Iniciando scrapeo")

    #COMENZAR EL ARCHIVO
    file = open("./codigo.html", "w", encoding="utf-8")
    file.write(f"
Listado de IP localizadas en blacklist (blacklistmaster.com) - {today}

He sido generado con python
")

    #PARA COMPROBAR CADA ELEMENTO DE LA LISTA
    for srv in dct_servers:
        #PARA HACER VISIBLE EL NAVEGADOR
        #browser = webdriver.Firefox()
        #PARA HACER INVISIBLE EL NAVEGADOR
        browser = webdriver.Firefox(options=options)

        browser.get("https://www.blacklistmaster.com/check")
        search_form = browser.find_element_by_name('ip')
        search_form.send_keys(dct_servers[srv])
        boton = browser.find_element_by_name('Submit')
        boton.click()

        # HACER LA ESPERA DE 15 SEGUNDOS FUNCIONA PARA ESTA WEB https://www.blacklistmaster.com/check
        sleep(15)

        page = BeautifulSoup(browser.page_source,"html.parser")
        browser.close()

        #------------------------------ AQUI LOCALIZAMOS LA TABLA COMPLETA DE RESULTADOS

        links = page.select('.myip800 a')
        print(srv,' - IP: ', dct_servers[srv])

        if links:
            for link in links:
                file.write("
")
                file.write(dct_servers[srv])
                file.write(": encontrado en ")
                file.write(str(link))
                file.write("
")
                print(f'{dct_servers[srv]}: encontrado en {link}')
        else:
            file.write("
")
            file.write(dct_servers[srv])
            file.write(": Limpio")
            file.write("
")
            print(f'{dct_servers[srv]}: limpio')

        print(f"{srv} terminado")

    file.write("")
    file.close

    print('Fin del proceso')

#------------------------------------ #FUNCION PARA VER EL ARCHIVO DE RESULTADOS
#---------------------------------------------------------- def ver_resultado():
    #--------------------------------------------- browser = webdriver.Firefox()
    # browser.get("file:///C:/Users/escri/OneDrive/Programacion/python/ip-blacklist-scrap-python/codigo.html")

#RAMA PRINCIPAL DEL PROGRAMA
programa = True

print('Hola!')
while programa:
    iniciar_incluir = input("Quieres modificar el listado o ejecutar el escaneo? (modificar/ejecutar): ")

    if iniciar_incluir == "modificar":
        print('Vamos a modificar el listado de IP')
        sleep(1)
        modificar()
    elif iniciar_incluir == "ejecutar":
        print('Ejecutando el escaneo')
        sleep(1)
        escanear(options)
        #------ ver_archivo = input ("Ver resultado en el navegador? (si/no): ")
        #----------------------------------------------- if ver_archivo == "si":
            #--------------------------------------------------- ver_resultado()
    elif iniciar_incluir == "salir" or iniciar_incluir == "exit":
        print('Saliendo')
        programa = False
    else:
        print('No he entendido que quieres hacer')

quit()


import torch
import numpy as np
from collections import Counter

def Dn_fun(omega_vals, U, nk):
    U = U.reshape((-1, 1))
    n = torch.sum(nk)
    # max_U = torch.max(U)

    log_vals = torch.log(torch.mm(omega_vals, torch.exp(U)))
    main_term = torch.mean(log_vals)
    # log_vals = torch.log(torch.mm(omega_vals, torch.exp(U-max_U)))
    # main_term = max_U + torch.mean(log_vals)
    other_term = torch.dot(nk.float(), U.flatten()) / n
    Dn = main_term - other_term
    return Dn

def U_from_Omegas(Omegas, nk):
    Omegas = Omegas.flatten()
    nk = nk.flatten()
    n = torch.sum(nk)
    U = torch.log(nk / (n * Omegas)).float()
    return U.reshape((-1, 1))

def Omegas_from_U(U, nk):
    U = U.flatten()
    nk = nk.flatten()
    n = torch.sum(nk)
    W = (nk / (torch.exp(U) * n)).float()
    return W.reshape((-1, 1))

def solve_Ws(
    omegas,
    source_datasets,
    n_iter=1000,
    batch_size=100,
    lr=0.01,
    momentum=0.9,
    verbose=True,
):
    """
    * omegas is a n x K numpy matrix with the values of the omega functions,
    i.e. (omegas)_{i,j} = \omega_j(Z_{k, l}) where (k,l) \in \Lambda is
    the ith observation.
    """
    K = omegas.shape[1]
    U = torch.normal(0, 1, (K, 1), requires_grad=True)
    nks = dict()
    for k in source_datasets:
        nks[k] = nks.get(k, 0) + 1
    nk_vec = torch.tensor([nks[k] for k in range(0, K)])

    # Solve the minimization of D_n by gradient descent:
    omegas = torch.tensor(omegas).float()
    data_loader = torch.utils.data.DataLoader(
        omegas, shuffle=True, batch_size=batch_size
    )
    optimizer = torch.optim.SGD([U], lr=lr, momentum=momentum)

    i = 0
    while i < n_iter:
        for batch in data_loader:
            optimizer.zero_grad()
            Dn = Dn_fun(omegas, U, nk_vec)
            Dn.backward()
            optimizer.step()
            if verbose and i % 100 == 0:
                print(
                    ("i = {}/{}, Dn = {:.5f} - " "- grad_norm = {:.2f}").format(
                        i, n_iter, Dn.item(), torch.norm(U.grad)
                    ),
                    flush=True,
                )
                # print("{:.5f} - {:.5f}".format(U[0].item(), U[1].item()))
            i += 1
            if i >= n_iter:
                break

    # Recover the W_s
    W = Omegas_from_U(U, nk_vec).detach().numpy().ravel()
    return W

def get_weights(omegas, source_datasets, W):
    K = omegas.shape[1]
    nks = dict()
    for k in source_datasets:
        nks[k] = nks.get(k, 0) + 1
    nk_vec = torch.tensor([nks[k] for k in range(0, K)])
    n = torch.sum(nk_vec)
    tmp = torch.mm(
        torch.tensor(omegas).float(), (nk_vec / (n * W)).reshape((-1, 1)).float()
    )
    weights = 1 / tmp
    tot_weights = torch.sum(weights)
    res = (weights / tot_weights).detach().numpy()
    return res

def main():
    print("Hello world")
    # How to use
    # W = solve_Ws(omegas, source_datasets, n_iter=500, batch_size=100, lr=0.001)

    # sample_weights = get_weights(omegas, source_datasets, W)
    # ideal_weights = get_weights(omegas, source_datasets, Omegas)

if __name__ == "__main__":
    main()


######################################################################
#
# File: test/unit/v0/test_raw_api.py
#
# Copyright 2019 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################
from __future__ import annotations

import pytest

from ..test_base import TestBase
from .deps import (
    B2Http,
    B2RawHTTPApi,
    BucketRetentionSetting,
    EncryptionAlgorithm,
    EncryptionKey,
    EncryptionMode,
    EncryptionSetting,
    RetentionMode,
    RetentionPeriod,
)
from .deps_exception import UnusableFileName, WrongEncryptionModeForBucketDefault

# Unicode characters for testing filenames.  (0x0394 is a letter Delta.)
TWO_BYTE_UNICHR = chr(0x0394)
CHAR_UNDER_32 = chr(31)
DEL_CHAR = chr(127)

class TestRawAPIFilenames(TestBase):
    """Test that the filename checker passes conforming names and rejects those that don't."""

    def setUp(self):
        self.raw_api = B2RawHTTPApi(B2Http())

    def _should_be_ok(self, filename):
        """Call with test filenames that follow the filename rules.

        :param filename: unicode (or str) that follows the rules
        """
        print(f"Filename \"{filename}\" should be OK")
        self.assertTrue(self.raw_api.check_b2_filename(filename) is None)

    def _should_raise(self, filename, exception_message):
        """Call with filenames that don't follow the rules (so the rule checker should raise).

        :param filename: unicode (or str) that doesn't follow the rules
        :param exception_message: regexp that matches the exception's detailed message
        """
        print(
            "Filename \"{}\" should raise UnusableFileName(\".*{}.*\").".format(
                filename, exception_message
            )
        )
        with self.assertRaisesRegex(UnusableFileName, exception_message):
            self.raw_api.check_b2_filename(filename)

    def test_b2_filename_checker(self):
        """Test a conforming and non-conforming filename for each rule.

        From the B2 docs (https://www.backblaze.com/b2/docs/files.html):
        - Names can be pretty much any UTF-8 string up to 1024 bytes long.
        - No character codes below 32 are allowed.
        - Backslashes are not allowed.
        - DEL characters (127) are not allowed.
        - File names cannot start with "/", end with "/", or contain "//".
        - Maximum of 250 bytes of UTF-8 in each segment (part between slashes) of a file name.
        """
        print("test b2 filename rules")

        # Examples from doc:
        self._should_be_ok('Kitten Videos')
        self._should_be_ok('\u81ea\u7531.txt')

        # Check length
        # 1024 bytes is ok if the segments are at most 250 chars.
        s_1024 = 4 * (250 * 'x' + '/') + 20 * 'y'
        self._should_be_ok(s_1024)
        # 1025 is too long.
        self._should_raise(s_1024 + 'x', "too long")
        # 1024 bytes with two byte characters should also work.
        s_1024_two_byte = 4 * (125 * TWO_BYTE_UNICHR + '/') + 20 * 'y'
        self._should_be_ok(s_1024_two_byte)
        # But 1025 bytes is too long.
        self._should_raise(s_1024_two_byte + 'x', "too long")

        # Names with unicode values < 32, and DEL aren't allowed.
        self._should_raise('hey' + CHAR_UNDER_32, "contains code.*less than 32")
        # Unicode in the filename shouldn't break the exception message.
        self._should_raise(TWO_BYTE_UNICHR + CHAR_UNDER_32, "contains code.*less than 32")
        self._should_raise(DEL_CHAR, "DEL.*not allowed")

        # Names can't start or end with '/' or contain '//'
        self._should_raise('/hey', "not start.*/")
        self._should_raise('hey/', "not .*end.*/")
        self._should_raise('not//allowed', "contain.*//")

        # Reject segments longer than 250 bytes
        self._should_raise('foo/' + 251 * 'x', "segment too long")
        # So a segment of 125 two-byte chars plus one should also fail.
        self._should_raise('foo/' + 125 * TWO_BYTE_UNICHR + 'x', "segment too long")

class BucketTestBase:
    @pytest.fixture(autouse=True)
    def init(self, mocker):
        b2_http = mocker.MagicMock()
        self.raw_api = B2RawHTTPApi(b2_http)

class TestUpdateBucket(BucketTestBase):
    """Test updating bucket."""

    @pytest.fixture(autouse=True)
    def init(self, mocker):
        b2_http = mocker.MagicMock()
        self.raw_api = B2RawHTTPApi(b2_http)

    def test_assertion_raises(self):
        with pytest.raises(AssertionError):
            self.raw_api.update_bucket('test', 'account_auth_token', 'account_id', 'bucket_id')

    @pytest.mark.parametrize(
        'bucket_type,bucket_info,default_retention', (
            (None, {}, None),
            (
                'allPublic', None,
                BucketRetentionSetting(RetentionMode.COMPLIANCE, RetentionPeriod(years=1))
            ),
        )
    )
    def test_assertion_not_raises(self, bucket_type, bucket_info, default_retention):
        self.raw_api.update_bucket(
            'test',
            'account_auth_token',
            'account_id',
            'bucket_id',
            bucket_type=bucket_type,
            bucket_info=bucket_info,
            default_retention=default_retention,
        )

    @pytest.mark.parametrize(
        'encryption_setting,', (
            EncryptionSetting(
                mode=EncryptionMode.SSE_C,
                algorithm=EncryptionAlgorithm.AES256,
                key=EncryptionKey(b'key', 'key-id')
            ),
            EncryptionSetting(mode=EncryptionMode.UNKNOWN,),
        )
    )
    def test_update_bucket_wrong_encryption(self, encryption_setting):
        with pytest.raises(WrongEncryptionModeForBucketDefault):
            self.raw_api.update_bucket(
                'test',
                'account_auth_token',
                'account_id',
                'bucket_id',
                default_server_side_encryption=encryption_setting,
                bucket_type='allPublic',
            )

class TestCreateBucket(BucketTestBase):
    """Test creating bucket."""

    @pytest.mark.parametrize(
        'encryption_setting,', (
            EncryptionSetting(
                mode=EncryptionMode.SSE_C,
                algorithm=EncryptionAlgorithm.AES256,
                key=EncryptionKey(b'key', 'key-id')
            ),
            EncryptionSetting(mode=EncryptionMode.UNKNOWN,),
        )
    )
    def test_create_bucket_wrong_encryption(self, encryption_setting):

        with pytest.raises(WrongEncryptionModeForBucketDefault):
            self.raw_api.create_bucket(
                'test',
                'account_auth_token',
                'account_id',
                'bucket_id',
                bucket_type='allPrivate',
                bucket_info={},
                default_server_side_encryption=encryption_setting,
            )