#@+leo-ver=5-thin
#@+node:ekr.20211209153303.1: * @file ../plugins/importers/python.py
"""The new, tokenize based, @auto importer for Python."""
import re
import tokenize
import token
from typing import Any, Callable, Dict, Generator, List, Optional, Tuple
from collections import defaultdict, namedtuple
import leo.core.leoGlobals as g
from leo.core.leoCommands import Commands as Cmdr
from leo.core.leoNodes import Position
from leo.plugins.importers.linescanner import Importer, block_tuple
#@+<< define def_tuple >>
#@+node:ekr.20220724060054.1: ** << define def_tuple >>
# For the new token-based python importer.
def_tuple = namedtuple('def_tuple', [
    'body_indent',  # Indentation of body.
    'body_line9',  # Line number of the first line after the definition.
    'decl_indent',  # Indentation of the class or def line.
    'decl_line1',  # Line number of the first line of this node.
                   # This line may be a comment or decorator.
    'kind',  # 'def' or 'class'.
    'name',  # name of the function, class or method.
])
#@-<< define def_tuple >>
#@+others
#@+node:ekr.20220720043557.1: ** class Python_Importer(Importer)
class Python_Importer(Importer):
    """
    A Python importer for eventual use by leoJS.

    Also, the base class of other importers.
    """

    def __init__(self, c: Cmdr, language: str = 'python') -> None:
        """Py_Importer.ctor."""
        super().__init__(c, language=language, strict=True)
        self.string_list = ['"""', "'''", '"', "'"]  # longest first.
        self.add_class_to_headlines = c.config.getBool('put-class-in-imported-headlines')

    #@+others
    #@+node:ekr.20220805071145.1: *3* python_i.compute_headline
    def compute_headline(self, s: str) -> str:
        """
        Python_Importer.compute_headline.

        Return the cleaned version headline s.
        """
        s = s.strip()
        # Remove '(' and ':' and everything after.
        for ch in '(:':
            i = s.find(ch)
            if i > -1:
                s = s[:i]
        # Remove leading 'def'
        if s.startswith('def '):  # pragma: no cover
            s = s[len('def') :]
        elif s.startswith('async def '):  # pragma: no cover (missing test)
            s = s[len('async def') :]
        # Remove leading 'class'.
        elif (
            s.startswith('class ')
            and not g.unitTesting
            and not self.add_class_to_headlines
        ):
            s = s[5:]  # pragma: no cover (missing test).
        return s.strip()
    #@+node:ekr.20220720060831.3: *3* python_i.declaration_headline
    def declaration_headline(self, body: str) -> str:  # #2500
        """
        Return an informative headline for body, a group of declarations.
        """
        for s in g.splitLines(body):
            strip_s = s.strip()
            if strip_s:
                if strip_s.startswith('#'):
                    strip_comment = strip_s[1:].strip()
                    if strip_comment:
                        # A non-trivial comment: Return the comment w/o the leading '#'.
                        return strip_comment
                else:
                    # A non-trivial non-comment.
                    return strip_s
        # Return legacy headline.
        return "...some declarations"  # pragma: no cover (missing test)
    #@+node:ekr.20220720050740.1: *3* python_i.get_block
    def get_block(self, i: int) -> block_tuple:
        """
        Python_Importer.get_block, based on Vitalije's getdefn function.

        Look for a def or class at lines[i].

        Return None or a block_tuple describing the class or def.
        """
        line, state = self.lines[i], self.line_states[i]
        if state.context or not line.strip():
            return None
        i2 = self.new_starts_block(i)
        if i2 is None:
            return None

        # Compute declaration data.
        decl_line = i
        decl_indent = self.get_int_lws(line)
        decl_level = decl_indent

        # Set body_indent to the indentation of the first non-blank line of the body.
        # Test for a single-line class or def.
        i = i2
        while i < len(self.lines):
            line = self.lines[i]
            if line.isspace():
                i += 1
            else:
                body_indent = self.get_int_lws(line)
                single_line = body_indent == decl_indent
                break
        else:  # pragma: no cover (mysterious)
            single_line = True
            body_indent = decl_indent

        # Multi-line bodies end at the next non-blank with less indentation than body_indent.
        # This is tricky because of underindented strings and docstrings.
        if not single_line:
            last_state = None
            while i < len(self.lines):
                line = self.lines[i]
                this_state = self.line_states[i]
                last_context = last_state.context if last_state else ''
                this_context = this_state.context if this_state else ''
                if (
                    not line.isspace()
                    and this_context not in ("'''", '"""', "'", '"')
                    and last_context not in ("'''", '"""', "'", '"')
                    and self.get_int_lws(line) < body_indent
                ):
                    break
                last_state = this_state
                i += 1

        # Include all following blank lines.
        while i < len(self.lines) and self.lines[i].isspace():  # pragma: no cover (mysterious).
            i += 1

        # Return the description of the block.
        return block_tuple(
            body_indent=body_indent,
            body_line9=i,
            decl_indent=decl_indent,
            decl_line1=decl_line - self.get_intro(decl_line, decl_indent),
            decl_level=decl_level,
            name=self.compute_headline(self.lines[decl_line])
        )
    #@+node:ekr.20220729081229.1: *3* python_i.is_intro_line
    def is_intro_line(self, n: int, col: int) -> bool:
        """
        Return True if line n is a comment line or decorator that starts at the give column.
        """
        line = self.lines[n]
        return (
            line.strip().startswith(('#', '@'))
            and col == g.computeLeadingWhitespaceWidth(line, self.tab_width)
        )
    #@+node:ekr.20220806085448.1: *3* python_i.new_starts_block
    # Optional base classes.
    class_pat_s = r'\s*(class|async class)\s+([\w_]+)\s*(\(.*?\))?(.*?):'
    class_pat = re.compile(class_pat_s, re.MULTILINE)

    # Requred argument list.
    def_pat_s = r'\s*(async def|def)\s+([\w_]+)\s*(\(.*?\))(.*?):'
    def_pat = re.compile(def_pat_s, re.MULTILINE)

    def new_starts_block(self, i: int) -> Optional[int]:
        """
        Return None if lines[i] does not start a class, function or method.

        Otherwise, return the index of the first line of the body.
        """
        line = self.lines[i]
        m = self.class_pat.match(line) or self.def_pat.match(line)
        if not m:
            return None
        newlines = m.group(0).count('\n')
        return i + newlines + 1
    #@-others
#@+node:vitalije.20211201230203.1: ** function: token_based_python_importer
SPLIT_THRESHOLD = 10  # Don't split blocks shorter than this threshold.

def token_based_python_importer(c: Cmdr, root: Position, s: str) -> None:
    """
    An importer that uses python's tokenizer module to analyze program structure.
    By Виталије Милошевић, (Vitalije Milosevic).

    Create direct children of root for all top level function definitions and class definitions.

    For longer class nodes, create separate child nodes for each method.

    Helpers use a token-oriented "parse" of the lines.
    Tokens are named 5-tuples, but this code uses only three fields:

    t.type:   token type
    t.string: the token string;
    t.start:  a tuple (srow, scol) of starting row/column numbers.
    """
    trace = False
    add_class_to_headlines = g.unitTesting or c.config.getBool('put-class-in-imported-headlines')
    lines: List[str] = s.splitlines(True)
    rawtokens: List

    #@+others
    #@+node:vitalije.20211208092910.1: *3* function: getdefn & helper
    def getdefn(start: int) -> def_tuple:
        """
        Look for an 'async', 'def' or `class` token at rawtokens[start].
        Return None or a def_tuple describing the def or class.
        """
        nonlocal add_class_to_headlines
        nonlocal lines  # 'lines' is a kwarg to split_root.
        nonlocal rawtokens

        # pylint: disable=undefined-loop-variable
        # tok will never be empty, but pylint doesn't know that.

        # Ignore all tokens except 'async', 'def', 'class'
        tok = rawtokens[start]
        if tok.type != token.NAME or tok.string not in ('async', 'def', 'class'):
            return None

        # Compute 'kind' and 'name'.
        if tok.string == 'async':
            kind = rawtokens[start + 1][1]
            name = rawtokens[start + 2][1]
        else:
            kind = tok.string
            name = rawtokens[start + 1][1]

        # Don't include 'async def' twice.
        if kind == 'def' and rawtokens[start - 1][1] == 'async':
            return None

        decl_line, decl_indent = tok.start

        # Find the end of the definition line, ending in a NEWLINE token.
        # This one logical line may span several physical lines.
        i, t = find_token(start + 1, token.NEWLINE)
        body_line9 = t.start[0] + 1

        # Look ahead to see if we have a one-line definition (INDENT comes after the NEWLINE).
        i1, t = find_token(i + 1, token.INDENT)  # t is used below.
        i2, t2 = find_token(i + 1, token.NEWLINE)
        if t is None or t2 is None:
            return None
        oneliner = i1 > i2

        # Find the end of this definition
        if oneliner:
            # The entire decl is on the same line.
            body_indent = decl_indent
        else:
            body_indent = len(t.string) + decl_indent
            # Skip the INDENT token.
            assert t.type == token.INDENT, t.type
            i += 1
            # The body ends at the next DEDENT or COMMENT token with less indentation.
            for i, t in itoks(i + 1):
                col2 = t.start[1]
                if col2 <= decl_indent and t.type in (token.DEDENT, token.COMMENT):
                    body_line9 = t.start[0]
                    break

        # Increase body_line9 to include all following blank lines.
        for j in range(body_line9, len(lines) + 1):
            if lines[j - 1].isspace():
                body_line9 = j + 1
            else:
                break

        if add_class_to_headlines:
            name = f"class {name}" if kind == 'class' else name

        # This is the only instantiation of def_tuple.
        return def_tuple(
            body_indent=body_indent,
            body_line9=body_line9,
            decl_indent=decl_indent,
            decl_line1=decl_line - get_intro(decl_line, decl_indent),
            kind=kind,
            name=name,
        )
    #@+node:vitalije.20211208084231.1: *4* function: get_intro (Vitalije's importer)
    def get_intro(row: int, col: int) -> int:
        """
        Return the number of preceeding lines that should be added to this class or def.
        """
        last = row
        for i in range(row - 1, 0, -1):
            if is_intro_line(i, col):
                last = i
            else:
                break
        # Remove blank lines from the start of the intro.
        # Leading blank lines should be added to the end of the preceeding node.
        for i in range(last, row):
            if lines[i - 1].isspace():
                last = i + 1
        return row - last
    #@+node:vitalije.20211208183603.1: *4* function: is_intro_line (Vitalije's importer)
    def is_intro_line(n: int, col: int) -> bool:
        """
        Return True if line n is either:
        - a comment line that starts at the same column as the def/class line,
        - a decorator line
        """
        # Filter out all whitespace tokens.
        xs = [z for z in lntokens[n] if z[0] not in (token.DEDENT, token.INDENT, token.NL)]
        if not xs:  # A blank line.
            return True  # Allow blank lines in a block of comments.
        t = xs[0]  # The first non blank token in line n.
        if t.start[1] != col:
            # Not the same indentation as the definition.
            return False
        if t.type == token.OP and t.string == '@':
            # A decorator.
            return True
        if t.type == token.COMMENT:
            # A comment at the same indentation as the definition.
            return True
        return False
    #@+node:vitalije.20211208104408.1: *3* mknode & helpers
    def mknode(
        p: Position,  # The starting (local root) position.
        start: int,  # The first line to allocate.
        end: int,  # The last line to allocate.
        others_indent: int,  # @others indentation (to be stripped from left).
        inner_indent: int,  # The indentation of all of the inner definitions.
        definitions: List[def_tuple],  # The definitions occuring within lines[start : end].
    ) -> None:
        """
        Allocate lines[start : end] to p.b or descendants of p.
        """
        nonlocal lines

        # Find all defs with the given inner indentation.
        inner_defs = [z for z in definitions if z.decl_indent == inner_indent]

        # Don't use the threshold for unit tests. It's too confusing.
        if not inner_defs or (not g.unitTesting and end - start < SPLIT_THRESHOLD):
            # Don't split the body.
            p.b = body(start, end, others_indent)
            return

        # Calculate head, the lines preceding the @others.
        decl_line1 = inner_defs[0].decl_line1
        head = body(start, decl_line1, others_indent) if decl_line1 > start else ''
        others_line = ' ' * max(0, inner_indent - others_indent) + '@others\n'

        # Calculate tail, the lines following the @others line.
        last_tail_line = inner_defs[-1].body_line9
        tail = body(last_tail_line, end, others_indent) if last_tail_line < end else ''
        p.b = f'{head}{others_line}{tail}'

        # Generate (allocate to body text) all @others lines, that is, all lines from:
        # - the first line of the first inner definition to
        # - the last line of the last inner definition.
        # *including* lines *between* inner defitions.

        last = decl_line1  # The last @other line that has been allocated.

        for inner_def in inner_defs:

            # Add a child for in-between (declaration) lines.
            if inner_def.decl_line1 > last:
                new_body = body(last, inner_def.decl_line1, inner_indent)  # #2500.
                child1 = p.insertAsLastChild()
                child1.h = declaration_headline(new_body)  # #2500
                child1.b = new_body
                last = inner_def.decl_line1

            # Add a child holding the inner definition.
            child = p.insertAsLastChild()
            child.h = inner_def.name

            # Compute the inner definitions of *this* inner definition.
            # Note: The calculation uses only the the position of each definition.
            #       The calculation *ignores* indentation!
            inner_inner_defs = [z for z in definitions if
                z.decl_line1 > inner_def.decl_line1 and z.body_line9 <= inner_def.body_line9
            ]
            if inner_inner_defs:
                # Recursively allocate all lines of all inner inner defs.
                # This will set child.b to include the head lines, @others lines, and tail lines.
                mknode(
                    p=child,
                    start=inner_def.decl_line1,
                    end=inner_def.body_line9,
                    others_indent=others_indent + inner_indent,
                    inner_indent=inner_def.body_indent,
                    definitions=inner_inner_defs,
                )
            else:
                # There are no inner defs, so this node will contain no @others directive.
                child.b = body(inner_def.decl_line1, inner_def.body_line9, inner_indent)

            # Remember the last allocated line.
            last = inner_def.body_line9
    #@+node:vitalije.20211208101750.1: *4* body & bodyLine
    def bodyLine(s: str, i: int) -> str:
        """Massage line s, adding the underindent string if necessary."""
        # Vitalije's legacy importer generated underindented escape strings.
        # However, this seems unlikely to be useful.
        legacy = False
        if i == 0 or s[:i].isspace():
            return s[i:] or '\n'
        # An underindented string.
        n = len(s) - len(s.lstrip())
        return f"\\\\-{i-n}.{s[n:]}" if legacy else s[n:]

    def body(a: int, b: Optional[int], i: int) -> str:
        """Return the (massaged) concatentation of lines[a: b]"""
        nonlocal lines  # 'lines' is a kwarg to split_root.
        return ''.join(bodyLine(s, i) for s in lines[a - 1 : b and (b - 1)])
    #@+node:ekr.20220320055103.1: *4* declaration_headline
    def declaration_headline(body_string: str) -> str:  # #2500
        """
        Return an informative headline for s, a group of declarations.
        """
        for s1 in g.splitLines(body_string):
            s = s1.strip()
            if s.startswith('#') and len(s.replace('#', '').strip()) > 1:
                # A non-trivial comment: Return the comment w/o the leading '#'.
                return s[1:].strip()
            if s and not s.startswith('#'):
                # A non-trivial non-comment.
                return s
        # Return legacy headline.
        return "...some declarations"  # pragma: no cover (missing tests)
    #@+node:ekr.20220717080934.1: *3* utils
    #@+node:vitalije.20211208092833.1: *4* find_token
    def find_token(i: int, k: int) -> Tuple[int, int]:
        """
        Return (j, t), the first token in "rawtokens[i:] with t.type == k.
        Return (None, None) if there is no such token.
        """
        for j, t in itoks(i):
            if t.type == k:
                return j, t
        return None, None  # pragma: no cover (missing test)
    #@+node:vitalije.20211208092828.1: *4* itoks
    def itoks(i: int) -> Generator:
        """Generate (n, rawtokens[n]) starting with i."""
        nonlocal rawtokens

        # Same as `enumerate(rawtokens[i:], start=i)` without allocating substrings.
        while i < len(rawtokens):
            yield(i, rawtokens[i])
            i += 1
    #@+node:vitalije.20211206182505.1: *4* mkreadline
    def mkreadline(lines: List[str]) -> Callable:
        """Return an readline-like interface for tokenize."""
        itlines = iter(lines)

        def nextline() -> str:
            try:
                return next(itlines)
            except StopIteration:
                return ''

        return nextline
    #@-others

    # Create rawtokens: a list of all tokens found in input lines
    rawtokens = list(tokenize.generate_tokens(mkreadline(lines)))

    # lntokens groups tokens by line number.
    lntokens: Dict[int, Any] = defaultdict(list)
    for t in rawtokens:
        row = t.start[0]
        lntokens[row].append(t)

    # Make a list of *all* definitions.
    aList = [getdefn(i) for i, z in enumerate(rawtokens)]
    all_definitions = [z for z in aList if z]

    if trace:  # pragma: no cover
        # trace results.
        for i, z in enumerate(all_definitions):
            g.trace(i, repr(z))

    # Start the recursion.
    root.deleteAllChildren()
    mknode(
        p=root, start=1, end=len(lines) + 1,
        others_indent=0, inner_indent=0, definitions=all_definitions)

    # Add *trailing* lines, just like the Importer class.
    root.b += '@language python\n@tabwidth -4\n'
#@-others

# Define this top-level for unit/coverage tests
USE_PYTHON_TOKENS = True

def do_import(c: Cmdr, parent: Position, s: str) -> None:
    """The importer callback for python."""
    if USE_PYTHON_TOKENS:  # For desktop Leo: use an importer based on python tokens.
        token_based_python_importer(c, parent, s)
    else:  # For leoJS: use a subclass of the Importer class.
        Python_Importer(c).import_from_string(parent, s)

importer_dict = {
    'extensions': ['.py', '.pyw', '.pyi'],  # mypy uses .pyi extension.
    'func': do_import,
}
#@@language python
#@@tabwidth -4
#@-leo


import sublime, sublime_plugin

# Related reading:
#     http://stackoverflow.com/questions/39606221/how-to-create-sublime-text-3-build-system-which-reads-shebang

# Normally in a Sublime build system you need to specify the program to run to
# execute the build directly. This is an example of how you could modify a build
# system so that it gets the name of the executable to use to build the current
# file from the current file itself.
#
# In order to use this, you would need a build file that looks something like
# the following example. This build is python specific but it doesn't need to
# be.
#
# {
#     // WindowCommand to execute for this build
#     "target": "shebanger",
#
#     // Use this when there is no shebang
#     "interpreter_default": "python",
#
#     // Args to pass to the interpreter
#     "interpreter_args": ["-u"],
#
#     "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
#     "selector": "source.python",
#
#     "env": {"PYTHONIOENCODING": "utf-8"},
#
#     "variants":
#     [
#         {
#             "name": "Syntax Check",
#             "interpreter_args": ["-m py_compile"],
#         }
#     ]
# }

class ShebangerCommand(sublime_plugin.WindowCommand):
    """
    Command to be used as the "target" option in a build system. Based on a
    customized build system, this will modify the version of python used to
    the one listed in the shebang line at the start of the script (if any).
    """
    def parseShebang (self, filename):
        with open(filename, 'r') as handle:
            shebang = handle.readline ().strip ().split (' ', 1)[0]
        if shebang.startswith ("#!"):
            return shebang[2:]
        return None

    def createExecDict(self, sourceDict):
        current_file = self.window.active_view ().file_name()
        args = dict (sourceDict)

        interpreter = args.pop ("interpreter_default", "python")
        exec_args = args.pop ("interpreter_args", ["-u"])
        shebang = self.parseShebang (current_file)

        args["shell_cmd"] = "{} {} \"{}\"".format (shebang or interpreter,
                                                   " ".join (exec_args),
                                                   current_file)

        return args

    def run(self, **kwargs):
        self.window.run_command ("exec", self.createExecDict (kwargs))


# -*- coding: utf-8 -*-
"""
Spyder Editor
By Christoph Martin Hell
This is a temporary script file.
"""

from tkinter import *
from CBED_Thickness import make_si, get_thickness_plot
import matplotlib.pyplot as plt

###################################################
###################################################
########### Predefined Constants###################
###################################################
#wavelength: https://advanced-microscopy.utah.edu/education/electron-micro/
wavelength_TEM_200kV = 0.0251*10**-10 #m

#Latice spacing pure AL taken from Williams and Carter
#d_Al_200 = 2.021e-10 #m Lattice spacing
d_Al_220 = 1.432e-10
##################################################

def run_CBEDAnalysis(event):
    theta1_variable = theta1.get()
    theta2_variable = theta2.get()
    theta3_variable = theta3.get()
    #theta4_variable = theta4.get()
    d_kik_variable = float(d_kik.get())
    version = version_variable.get()

    theta_list = [theta1_variable,theta2_variable,theta3_variable]
    theta_list= [float(entry) for entry in theta_list]

    wavelengthTEM_variable = float(wavelengthTEM.get())
    d_latticePlane_variable = float(d_latticePlane.get())

    si_list = make_si(theta_list , d_kik_variable, d_latticePlane_variable , wavelengthTEM_variable)
    thickness_fig, rsquares, thickness = get_thickness_plot(si_list, kStart = 1, kstop = 5 ,version= version)
    print('##################'+ '\n'+'##################'+ '\n'+'Thickness: '+ str(round(thickness,3))+
          '\n'+'##################'+'\n'+'##################')

root = Tk() #creates empty Window
root.title('CBED- Sicknessmeasurements')
root.geometry('400x300')
caption = Label(root, text = 'CBED- Sicknessmeasurements ', bg= 'black', fg = 'white', font = ('arial italic',20))
caption.grid(row = 0, column=0, columnspan = 2, pady = 10)

theta1_label = Label(root, text = 'Theta1:')
theta1_label.grid(row =3, column=0)
theta1 = Entry(root, width = 20)
theta1.grid(row =4, column=0)

theta2_label = Label(root, text = 'Theta2:')
theta2_label.grid(row =5, column=0)
theta2 = Entry(root, width = 20)
theta2.grid(row =6, column=0)

theta3_label = Label(root, text = 'Theta3:')
theta3_label.grid(row =7, column=0)
theta3 = Entry(root, width = 20)
theta3.bind('<Return>',run_CBEDAnalysis)
theta3.grid(row =8, column=0)

"""
theta4_label = Label(root, text = 'Theta4:')
theta4_label.pack()
theta4 = Entry(root, width = 20)
theta4.pack()
"""

d_kik_label = Label(root, text = 'Kikuchi Distance:')
d_kik_label.grid(row =1, column=0)
d_kik = Entry(root, width = 20)
d_kik.bind('<Return>',run_CBEDAnalysis)
d_kik.grid(row =2, column=0,pady = 15)

button1 = Button(root, text="Calculate Sickness", command = run_CBEDAnalysis)
button1.bind("<Button-1>", run_CBEDAnalysis)

button1.grid(row = 8, column = 1, columnspan = 1)

wavelengthTEM_label = Label(root, text = 'Wavelength [m]: ')
wavelengthTEM_label.grid(row =1, column=1)
wavelengthTEM = Entry(root, width = 20)
wavelengthTEM.insert(END, wavelength_TEM_200kV)
wavelengthTEM.grid(row =2, column=1)

d_latticePlane_label = Label(root, text = 'Latticeplane distance [m]:')
d_latticePlane_label.grid(row =3, column=1)
d_latticePlane = Entry(root, width = 20)
d_latticePlane.insert(END, d_Al_220)
d_latticePlane.grid(row =4, column=1)

version_variable= StringVar(root)
version_variable.set('3 Plots')
version = OptionMenu(root, version_variable, '3 Plots', 'Std-Error')
version.grid(row =7, column=1)

root.mainloop() #shows Window continously


from .decorators import UseFor
from ..pyasn1.type import base, tag, constraint, namedtype, namedval, tagmap, univ, char
from .. import decoder
#from ..pyasn1.codec.der import decoder

from ..pyasn1_modules import rfc5280

# This translator class convertes ASN1-Structures into new
# parse trees to dive deeper into classes of the type Any.
class Asn1Translators:
    @UseFor(univ.Any)
    def translateAny(self, record):
        # Parse the content of the Any-Tag if there is any content and return the result
        if (record):
            return decoder.decode(record)[0]
        else:
            return None

class X509Translators:
    ceOIDClassMap = {
        rfc5280.id_ce_authorityKeyIdentifier: rfc5280.AuthorityKeyIdentifier,
        rfc5280.id_ce_basicConstraints: rfc5280.BasicConstraints,
        rfc5280.id_ce_certificateIssuer: rfc5280.CertificateIssuer,
        rfc5280.id_ce_certificatePolicies: rfc5280.CertificatePolicies,
        rfc5280.id_ce_cRLDistributionPoints: rfc5280.CRLDistPointsSyntax,
        #rfc5280.id_ce_cRLNumber - Handled by auto value decoding feature. Decoding the integer directly fails.
        rfc5280.id_ce_cRLReasons: rfc5280.CRLReason,
        rfc5280.id_ce_extKeyUsage: rfc5280.ExtKeyUsageSyntax,
        rfc5280.id_ce_freshestCRL: rfc5280.FreshestCRL,
        rfc5280.id_ce_holdInstructionCode: rfc5280.HoldInstructionCode,
        rfc5280.id_ce_invalidityDate: rfc5280.InvalidityDate,
        rfc5280.id_ce_issuerAltName: rfc5280.IssuerAltName,
        rfc5280.id_ce_issuingDistributionPoint: rfc5280.IssuingDistributionPoint,
        rfc5280.id_ce_keyUsage: rfc5280.KeyUsage,
        rfc5280.id_ce_nameConstraints: rfc5280.NameConstraints,
        rfc5280.id_ce_policyConstraints: rfc5280.PolicyConstraints,
        rfc5280.id_ce_policyMappings: rfc5280.PolicyMappings,
        rfc5280.id_ce_privateKeyUsagePeriod: rfc5280.PrivateKeyUsagePeriod,
        rfc5280.id_ce_subjectAltName: rfc5280.SubjectAltName,
        rfc5280.id_ce_subjectDirectoryAttributes: rfc5280.SubjectDirectoryAttributes,
        rfc5280.id_ce_subjectKeyIdentifier: rfc5280.SubjectKeyIdentifier,
        rfc5280.id_pe_authorityInfoAccess: rfc5280.AuthorityInfoAccessSyntax
    }

    @UseFor(rfc5280.Extension)
    def translateExtension(self, record):
        extensionOID = record["extnID"]

        # The extension is stored as an instance of the class any.
        # This instance must be decoded first to get the internal structure of the extension
        extensionValue = decoder.decode(record["extnValue"].asOctets())[0]

        # Iterate over all registered Extensions and decode the subelements.
        for oidClassMapEntry in self.ceOIDClassMap:
            if (extensionOID == oidClassMapEntry):
                try:
                    record["extnValue"].override = decoder.decode(extensionValue, asn1Spec=self.ceOIDClassMap[oidClassMapEntry]())[0]
                    return record;
                except:
                    pass

        # Start a universal decoding try on the extensionValue if it was not found within the list.
        # This is called: Auto value decoding
        try:
            record["extnValue"].override = decoder.decode(extensionValue)[0]
            return record
        except:
            pass

        # Just retorn the internaly decoded any.
        record["extnValue"].override = extensionValue
        return record


"""Test the performance of basic API calls."""

# pip install pympler
import gc
from pympler import asizeof
import time
import uuid
import unittest2 as unittest

try:
    from osp.core.namespaces import city
except ImportError:
    from osp.core.ontology import Parser
    from osp.core.namespaces import _namespace_registry
    Parser(_namespace_registry._graph).parse("city")
    _namespace_registry.update_namespaces()
    city = _namespace_registry.city

RUN_PERFORMANCE_TEST = False

class TestPerformance(unittest.TestCase):
    """Test the performance of basic API calls."""

    def setUp(self):
        """Start the timer."""
        if not RUN_PERFORMANCE_TEST:
            return
        self.iterations = 100000
        self.c = city.City(name="A big city")
        for i in range(10):
            j = i * 9
            self.c.add(city.Citizen(uid=j + 0), rel=city.hasInhabitant)
            self.c.add(city.Citizen(uid=j + 1), rel=city.encloses)
            self.c.add(city.Citizen(uid=j + 2), rel=city.hasPart)
            self.c.add(city.Neighborhood(name="", uid=j + 3),
                       rel=city.hasInhabitant)
            self.c.add(city.Neighborhood(name="", uid=j + 4),
                       rel=city.encloses)
            self.c.add(city.Neighborhood(name="", uid=j + 5),
                       rel=city.hasPart)
            self.c.add(city.Street(name="", uid=j + 6),
                       rel=city.hasInhabitant)
            self.c.add(city.Street(name="", uid=j + 7), rel=city.encloses)
            self.c.add(city.Street(name="", uid=j + 8), rel=city.hasPart)
        self.start = time.time()

    def tearDown(self):
        """Stop the timer and print the results."""
        if not RUN_PERFORMANCE_TEST:
            return
        self.stop = time.time()
        mem_bytes = asizeof.asizeof(self.c)
        mem_mb = mem_bytes / (1024 * 1024.0)
        print('Memory usage: ' + str(mem_mb) + ' MegaBytes.')
        total = self.stop - self.start
        if total > 60:
            print('Total time: ' + str(total / 60) + ' minutes.')
        else:
            print('Total time: ' + str(total) + ' seconds.')
        self.c._session = None
        gc.collect()

    def test_creation(self):
        """Test the instantiation and type of the objects."""
        if not RUN_PERFORMANCE_TEST:
            return
        print("Test cuds object creation")
        for i in range(self.iterations):
            city.Citizen(name='citizen ' + str(i))

    def test_add_default(self):
        """Test the instantiation and type of the objects."""
        if not RUN_PERFORMANCE_TEST:
            return
        print("Test adding with the default relationship")
        for i in range(self.iterations):
            self.c.add(city.Neighborhood(
                name='neighborhood ' + str(i)))

    def test_add_rel(self):
        """Test the instantiation and type of the objects."""
        if not RUN_PERFORMANCE_TEST:
            return
        print("Test adding with a general relationship")
        for i in range(self.iterations):
            self.c.add(city.Citizen(name='citizen ' + str(i)),
                       rel=city.hasInhabitant)

    def test_get_by_oclass(self):
        """Test performance of getting by oclass."""
        if not RUN_PERFORMANCE_TEST:
            return
        print("Test get by oclass")
        for i in range(self.iterations):
            self.c.get(oclass=city.Street)

    def test_get_by_uid(self):
        """Test the performance of getting by UUID."""
        if not RUN_PERFORMANCE_TEST:
            return
        print("Test get by uid")
        uids = list(map(lambda x: uuid.UUID(int=x), range(10)))
        for i in range(self.iterations):
            self.c.get(*uids)

    def test_get_by_rel(self):
        """Test the performance of getting by relationship."""
        if not RUN_PERFORMANCE_TEST:
            return
        print("Test get by relationship")
        for i in range(self.iterations):
            self.c.get(rel=city.hasInhabitant)

if __name__ == '__main__':
    RUN_PERFORMANCE_TEST = True
    unittest.main()


# 문1) 1 ~ 100 사이의 숫자 중 3의 배수이나 2의 배수가 아닌 수를 출력하고, 합을 출력
i = 0; total = 0
while i <= 100:
    i += 1
    if i % 3 == 0:
        if i % 2 != 0:
            print(i, end = ", ")
            total += i
print("총합 : " + str(total))

# 문2) 2 ~ 5 까지의 구구단 출력
i = 2
while i <= 5:
    j = 1
    while j <= 9:
         print(str(i) + 'X' + str(j) + '=' + str(int(i) * int(j)), end = "\t")
         j = j + 1
    print()
    i = i + 1

# 문3) -1, 3, -5, 7, -9, 11 ~ 99 까지의 합을 출력
i = 1
odd = 0
even = 0
while i <= 100:
    if i % 2 != 0:
        odd = odd - i  # 홀수번째 홀수는 음수
    else:
        even = even + i # 짝수번째 홀수는 양수
    i += 1
print("총합 : " + str(odd+even))

# 문4) 1 ~ 1000 사이의 소수(1보다 크며 1과 자신의 수 이외에는 나눌 수 없는 수)와 그 개수를 출력
num = 2
while num <= 1000:
    count = 0  # 약수의 개수를 세어줄 변수  (자신을 포함해 나눠지는수가 2개면 소수)
    i = 1  # 1 ~ num 까지 증가할 변수
    while i <= num:
        if num % i == 0:  # 나누어지면 약수
            count += 1
        i += 1  # 1증가
    if count == 2:  # 약수의 개수가 2개면 출력
        print('{0}의 약수가 {1}개이므로 "소수"입니다.'.format(num, count))
    num += 1  # 1000까지 증가


"""
#------------------------------------------------------------------------------
# parse_datafile.py
#
# Get meaningful data from large CDF results
#
# Created: 2/7/16 - Daniel Newman -- dmn3669@louisiana.edu
#
# Modified:
#   * 2/7/18 - DMN -- dmn3669@louisiana.edu
#			- Added documentation for this script
#------------------------------------------------------------------------------
"""

import numpy as np
import glob
import os
import sys
import pdb

data_folder = 'Mesh Data/Steady Flow Rate Data/'

# grab all csv files in the given directory
#files = glob.glob(data_folder + '/*.csv')

file = data_folder + 'WaterGlycerin MeshVelocity_10Lmin.txt'

# Overwrite the previous file
new_file = open(data_folder + '10LPM.txt','w')
new_file.write('X [m]\tY [m]\tZ [m]\tVolume[m^3]\tSurface[m^2]\tVelocity (X) [m/s]\tVelocity (Y) [m/s]\tVelocity(Z) [m/s]\n')

# Grab data.  Specify the delimiter and header rows to skip
data = np.genfromtxt(file,delimiter='\t',skip_header=1)

surf_column = 4
vel_column = np.array([5,6,7])
vol_column = 3

notsurf_index = np.argwhere(np.isnan(data[:,4]))
notsurfnotflow_index = np.argwhere(np.all(np.isnan(data[:,4:7]),axis=1))
issurf_index = np.argwhere(np.isfinite(data[:,4]))

isvalid_index = np.argwhere(np.all(np.isfinite(data[:,5:7]),axis=1))

data = np.nan_to_num(data)

for i in isvalid_index:
	new_file.write('{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n'.format(
			float(data[i,0]),float(data[i,1]),float(data[i,2]),float(data[i,3]),float(data[i,4]),float(data[i,5]),float(data[i,6]),float(data[i,7])))

new_file.close()


import speech_recognition as sr
import time
import os

# DEFINITIONS
file_limit = 20 # @param:  the maximum amount of wav files we want

# @ param is the number of files created before overwriting old files starting from 1.
def m2S(file_limit):
    print("m2S");
    r = sr.Recognizer()
    i = 1
    while True:
        if i >= file_limit:
            i = 1

        with sr.Microphone() as source:
            print("Say Something:")
            audio = r.record(source, duration = 4)

            file_name = "test" + str(i) +".wav"
            with open(file_name, "wb") as f:
                f.write(audio.get_wav_data())
        i+=1

# convert speech to wav file ordered from one to file_limit
# @ param is the number of files created before overwriting old files starting from 1.
def s2T(file_limit):
    # print("s2T is working")
    r = sr.Recognizer()
    i = 1
    while True:
        # reset i counter to i if we go beyond the limit of wav files we want
        if i >= file_limit:
            i = 1

        try:
            hello = sr.AudioFile('test' + str(i) + '.wav')
            with hello as source:
                audio = r.record(source)
            try:
                google_text = r.recognize_google(audio)
                convertTxt(google_text, i)

            # this exception is if there is nothing in the audio file
            except Exception as e:
                print("ERROR NO SOUND")

            os.remove('test' + str(i) + '.wav')
            i+=1

        # this exception is for if we dont have a audio file by that name
        except:
            print("...");
            time.sleep(3);

#@param is the translated google text file using the google speech API and
#the variable 'i' is from the counter to indicate the txt file per wav file
def convertTxt(google_text, i):
    print("changing to txt now!")
    file = open("txt" + str(i) + ".txt", "w")
    file.write(google_text)
    file.close()


"""integration testing of formats"""
import cStringIO
import json
import pytest
from ijson.backends.python import UnexpectedSymbol
from workspan.formats import JSONFormat

def test_json_format_process():
    """test json format"""
    obj = {
        'entity_id': 1,
        'name': 'Entity'
    }
    output = cStringIO.StringIO(json.dumps(obj))
    processor = JSONFormat(output)
    processor.register_prefix("")
    items = processor.process()
    _, entity = next(items)
    assert entity == obj
    output.close()

def test_json_incorrect_structure():
    """raise exception while incorrect structure"""
    content = '{\"entity_id\': 1, \"name\": \"Entity\"}'
    output = cStringIO.StringIO(content)
    processor = JSONFormat(output)
    processor.register_prefix("")
    with pytest.raises(UnexpectedSymbol):
        next(processor.process())
    output.close()


import ply.lex as lex
import ply.yacc as yacc

import pyramid

# pyramid of token names
tokens = (
    'NUMBER',
    'PRINT'
)

# A string containing ignored characters (spaces and tabs)
t_ignore = ' \t'
t_PRINT = 'p'

def t_NUMBER(t):
    r'\d+'
    t.value = int(t.value)
    return t

# Error handling rule
def t_error(t):
    print("Illegal character '%s'" % t.value[0])
    t.lexer.skip(1)

# YACC
def p_expression_pyramid(p):
    'expression : pyramid'
    p[0] = p[1]

def p_pyramid_number(p):
    'pyramid : NUMBER'
    p[0] = pyramid.Pyramid(p[1])

def p_pyramid_pyramid_number(p):
    'pyramid : pyramid NUMBER'
    p[0] = p[1]
    p[0].add(p[2])

def p_intermediate_print(p):
    'pyramid : pyramid PRINT'
    p[0] = p[1]
    print(p[0])

# Error rule for syntax errors
def p_error(p):
    print("Syntax error in input!")
    print("Token was:", p)

lexer = lex.lex()
parser = yacc.yacc()


import logging
from sqlalchemy import MetaData, create_engine, exc, and_, or_
from sqlalchemy import asc, desc, select, exists
# from config import DATABASE_URL
from sqlalchemy.sql import text
logger = logging.getLogger(__name__)

class AlchemyDB:
    engine = None
    _table = dict()
    metadata = None

    def __init__(self):
        self.conn = AlchemyDB.get_connection()

    def __del__(self):
        self.conn.close()

    @classmethod
    def init(cls):
        try:
            # from lib.utils import get_agent_id

            cls.engine = create_engine(
                    'mysql+pymysql://root:root@localhost/crawler?charset=utf8mb4',
                    paramstyle='format',
                    pool_recycle=3600,
                    isolation_level='REPEATABLE_READ',
                    convert_unicode=True
            )

            cls.metadata = MetaData()

        except exc.SQLAlchemyError as err:
            logger.error(err, exc_info=True)
        except Exception as err:
            logger.error(err, exc_info=True)

    @classmethod
    def get_connection(cls):
        return cls.engine.connect()

    @classmethod
    def get_raw_connection(cls):
        return cls.engine.raw_connection()

    @classmethod
    def get_table(cls, name):
        return cls._table[name]

    def begin(self):
        self.trans = self.conn.begin()

    def commit(self):
        self.trans.commit()

    def rollback(self):
        self.trans.rollback()

    @staticmethod
    def args_to_where(table, args):
        clause = []
        for k, v in args.items():
            if isinstance(v, (list, tuple)):
                clause.append(table.c[k].in_(v))
            else:
                clause.append(table.c[k] == v)
        return and_(*clause)

    @staticmethod
    def args_to_where_or(table, args):
        clause = []
        for k, v in args.items():
            if isinstance(v, (list, tuple)):
                clause.append(table.c[k].in_(v))
            else:
                clause.append(table.c[k] == v)
        return or_(*clause)

    def insert_row(self, table_name, **values):
        table = self.get_table(table_name)
        insert = table.insert().values(values)
        self.conn.execute(insert)

    def insert_row_batch(self, table_name, values):
        table = self.get_table(table_name)
        self.conn.execute(table.insert(), values)

    def update_row(self, table_name, *keys, **row):
        table = self.get_table(table_name)
        if not isinstance(keys, (list, tuple)):
            keys = [keys]
        if not keys or len(keys) == len(row):
            return False
        clause = dict()
        for k in keys:
            clause[k] = row[k]
        clean_row = row.copy()
        for key in keys:
            if key in clean_row.keys():
                del clean_row[key]
        clauses = self.args_to_where(table, clause)
        update = table.update(clauses, clean_row)
        self.conn.execute(update)
        return True

    def update_row_new(self, table_name, where=None, val=None):
        if not where:
            where = {}
        if not val:
            val = {}
        try:
            table = self.get_table(table_name)
            clauses = self.args_to_where(table, where)
            update = table.update(clauses, val)
            self.conn.execute(update)
            return True
        except Exception as err:
            logger.error(err, exc_info=True)(err, True)
            return False

    def delete_row(self, table_name, **where):
        table = self.get_table(table_name)
        try:
            delete = table.delete().where(self.args_to_where(table, where))
            self.conn.execute(delete)
        except exc.SQLAlchemyError as err:
            logger.error(err, exc_info=True)(err, True)
            return False

    def delete_row_in_transaction(self, table_name, **where):
        table = self.get_table(table_name)
        delete = table.delete().where(self.args_to_where(table, where))
        self.conn.execute(delete)

    def find_one(self,table_name, **where):
        table = self.get_table(table_name)
        sel = select([table]).where(self.args_to_where(table, where))
        row = self.conn.execute(sel)
        tup = row.fetchone()
        if tup:
            return dict(tup)
        return False

    def exists_row(self, table_name, **where):
        table = self.get_table(table_name)
        try:
            sel = select([table]).where(self.args_to_where(table, where))
            sel = select([exists(sel)])
            row = self.conn.execute(sel).scalar()
            if row:
                return True
        except exc.SQLAlchemyError as err:
            logger.error(err, exc_info=True)
        return False

    def find(self, table_name, order_by="id", _limit=None, **where):
        table = self.get_table(table_name)
        try:
            func = asc
            if order_by and order_by.startswith('_'):
                order_by = order_by[1:]
                func = desc
            if _limit:
                sel = select([table]).where(self.args_to_where(table, where)).order_by(func(order_by)).limit(_limit)
            else:
                sel = select([table]).where(self.args_to_where(table, where)).order_by(func(order_by))
            row = self.conn.execute(sel)
            tup = row.fetchall()
            l = [dict(r) for r in tup]
            return l
        except exc.SQLAlchemyError as err:
            logger.error(err, exc_info=True)
            return False

    def find_or(self, table_name, order_by="id", _limit=None, **where):
        table = self.get_table(table_name)
        try:
            func = asc
            if order_by and order_by.startswith('_'):
                order_by = order_by[1:]
                func = desc
            if _limit:
                sel = select([table]).where(self.args_to_where_or(table, where)).order_by(func(order_by)).limit(_limit)
            else:
                sel = select([table]).where(self.args_to_where_or(table, where)).order_by(func(order_by))
            row = self.conn.execute(sel)
            tup = row.fetchall()
            l = [dict(r) for r in tup]
            return l
        except exc.SQLAlchemyError as err:
            logger.error(err, exc_info=True)
            return False

    @staticmethod
    def args_to_join(table1, table2, args):
        clause = []
        for k, v in args.items():
            clause.append(table1.c[k] == table2.c[v])
        return and_(*clause)

    def select_outer_join(self, table_names, foreign_key, where):
        table = [self.get_table(t) for t in table_names]
        try:
            fclause = self.args_to_join(table[0], table[1], foreign_key[0])
            logger.debug(fclause)
            clause = self.args_to_where_join(where)
            logger.debug(clause)
            # clause = and_(fclause,clause)
            logger.debug(clause)
            j = table[0].outerjoin(table[1], fclause)
            for i in range(1, len(foreign_key)):
                fclause = self.args_to_join(table[i], table[i+1], foreign_key[i])
                j = j.join(table[i+1], fclause)
            sel = select(table, use_labels=True).select_from(j).where(clause)
            row = self.conn.execute(sel)
            tup = row.fetchall()
            l = [dict(r) for r in tup]
            return l
        except Exception as e:
            logger.exception(e)
            return False

    @classmethod
    def args_to_where_join(cls, where):
        # where = [({"SocialContact.Email": email}), ({"AppUserId": appid})]
        logger.debug(where)
        or_list = []
        for tup in where:
            and_list = []
            for r in tup:
                if r:
                    logger.debug(r)
                    tab, col = r.keys()[0].split('.')
                    table = cls.get_table(tab)
                    v = r.values()[0]
                    if isinstance(v, (list, tuple)):
                        and_list.append(table.c[col].in_(v))
                    else:
                        and_list.append(table.c[col] == v)
                or_list.append(and_(*and_list))
        return or_(*or_list)

    def count(self, table_name, **where):
        table = self.get_table(table_name)
        try:
            sel = select([table.c.id]).where(self.args_to_where(table, where))
            row = self.conn.execute(sel)
            count = row.rowcount
            return count
        except exc.SQLAlchemyError as err:
            logger.error(err, exc_info=True)
            return False

    def execute_raw_sql(self, sql, kwargs):
        try:
            row = self.conn.execute(text(sql), kwargs)
            tup = row.fetchall()
            l = [dict(r) for r in tup]
            return l
        except Exception as e:
            logger.exception(e)
            return False


import vagrant
from subprocess import Popen, PIPE, STDOUT
import logging
from logging import warn, error, info

def clean():
   """Starts the specified machine using vagrant"""
   count=1
   shell_commands = ['vagrant halt', 'vagrant destroy', 'rm Vagrantfile']
   for shell_command in shell_commands:
      if (count == 1): info("Halting Vagrant cell...")
      elif (count == 2): info("Destroying Vagrant cell...")
      elif (count == 3): info("Removing Vagrantfile...")
      event = Popen(shell_command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
      output = event.communicate()
      count += 1

l = logging.getLogger()
l.setLevel('DEBUG')
clean()


from sqlalchemy import *
from migrate import *

from migrate.changeset import schema
pre_meta = MetaData()
post_meta = MetaData()
user = Table('user', pre_meta,
    Column('id', INTEGER(), primary_key=True, nullable=False),
    Column('nickname', VARCHAR(length=256)),
    Column('email', VARCHAR(length=256)),
    Column('confirmed_at', DATETIME),
    Column('password', VARCHAR(length=55)),
    Column('calendar_credentials', TEXT),
    Column('first_name', VARCHAR(length=256)),
    Column('last_name', VARCHAR(length=256)),
    Column('status', VARCHAR(length=15)),
    Column('calendar_access', SMALLINT),
    Column('active', INTEGER()),
)

therapist = Table('therapist', pre_meta,
    Column('id', INTEGER(), primary_key=True, nullable=False),
    Column('first_name', VARCHAR(length=55)),
    Column('last_name', VARCHAR(length=55)),
    Column('company_id', INTEGER()),
)

therapist = Table('therapist', post_meta,
    Column('id', INTEGER, primary_key=True, nullable=False),
    Column('user_id', INTEGER),
    Column('company_id', INTEGER),
)

def upgrade(migrate_engine):
    # Upgrade operations go here. Don't create your own engine; bind
    # migrate_engine to your metadata
    pre_meta.bind = migrate_engine
    post_meta.bind = migrate_engine
    pre_meta.tables['user'].columns['active'].drop()
    pre_meta.tables['user'].columns['nickname'].drop()
    pre_meta.tables['therapist'].columns['first_name'].drop()
    pre_meta.tables['therapist'].columns['last_name'].drop()
    post_meta.tables['therapist'].columns['user_id'].create()

def downgrade(migrate_engine):
    # Operations to reverse the above upgrade go here.
    pre_meta.bind = migrate_engine
    post_meta.bind = migrate_engine
    pre_meta.tables['user'].columns['active'].create()
    pre_meta.tables['user'].columns['nickname'].create()
    pre_meta.tables['therapist'].columns['first_name'].create()
    pre_meta.tables['therapist'].columns['last_name'].create()
    post_meta.tables['therapist'].columns['user_id'].drop()


'''Creates boxplot of fragment length statistics. Data should be a single column of integers with no header. Usage: python makeBoxplot.py *.len -- second argument is string indicating the pattern to match when searching for files
'''

import matplotlib.pyplot as plt
import sys
import glob

dataToPlot = []
fileList = glob.glob('*len*')
#print(fileList)

for fileName in fileList:
	lines = [line.rstrip('\n') for line in open(fileName)]
	converted = [int(i) for i in lines]
	dataToPlot.append(converted)

#print(dataToPlot)

fig = plt.figure(1, figsize=(10,10))
ax = fig.add_subplot(111)
bp = ax.boxplot(dataToPlot, sym='', showfliers=False)
#with outliers
#bp = ax.boxplot(dataToPlot)

ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
ax.set_ylim([0, 200])
xnames = [i.replace('*len*', '') for i in fileList]
ax.set_xticklabels(xnames, fontsize=10)
plt.xticks(rotation=90)

fig.savefig('lenbp.png', bbox_inches='tight')
fig.savefig('lenbp.pdf', format='pdf', bbox_inches='tight')
fig.savefig('lenbp.svg', format='svg', bbox_inches='tight')

#with outliers
#fig.savefig('lenbp_outliers.png', bbox_inches='tight')
#fig.savefig('lenbp_outliers.pdf', format='pdf', bbox_inches='tight')


#Embedded file name: e:\jenkins\workspace\client_SERENITY\branches\release\SERENITY\eve\client\script\ui\services\menuAction.py
import types
from carbonui.control.menu import ClearMenuLayer
from eve.client.script.ui.control.glowSprite import GlowSprite
import evecamera
import util
import crimewatchConst
import uthread
import carbonui.const as uiconst
import uicontrols
import uiprimitives
import uix
import localization
import math
import base
import blue
import state
import uiutil
import log
import sys
from eve.client.script.ui.services.menuSvcExtras.movementFunctions import SetDefaultDist
from eve.client.script.ui.services.menuSvcExtras.movementFunctions import GetGlobalActiveItemKeyName
from eve.client.script.ui.services.menuSvcExtras.movementFunctions import DefaultWarpToLabel

class ActionMenu(uiprimitives.Container):
    __guid__ = 'xtriui.ActionMenu'

    def ApplyAttributes(self, attributes):
        uiprimitives.Container.ApplyAttributes(self, attributes)
        self.actionMenuOptions = {'UI/Commands/ShowInfo': ('ui_44_32_24', 0, 0, 0, 0),
         'UI/Inflight/LockTarget': ('ui_44_32_17', 0, 0, 0, 0),
         'UI/Inflight/UnlockTarget': ('ui_44_32_17', 0, 0, 0, 1),
         'UI/Inflight/ApproachObject': ('ui_44_32_23', 0, 0, 0, 0),
         'UI/Inflight/LookAtObject': ('ui_44_32_20', 0, 0, 0, 0),
         'UI/Inflight/SetAsCameraInterest': ('ui_44_32_65', 0, 0, 0, 0),
         'UI/Inflight/ResetCamera': ('ui_44_32_20', 0, 0, 0, 1),
         'UI/Inflight/Submenus/KeepAtRange': ('ui_44_32_22', 0, 0, 1, 0),
         'UI/Inflight/OrbitObject': ('ui_44_32_21', 0, 0, 1, 0),
         'UI/Inflight/DockInStation': ('ui_44_32_9', 0, 0, 0, 0),
         'UI/Chat/StartConversation': ('ui_44_32_33', 0, 0, 0, 0),
         'UI/Commands/OpenCargo': ('ui_44_32_35', 0, 0, 0, 0),
         'UI/Commands/OpenMyCargo': ('ui_44_32_35', 0, 0, 0, 0),
         'UI/PI/Common/AccessCustomOffice': ('ui_44_32_35', 0, 0, 0, 0),
         'UI/Inflight/StopMyShip': ('ui_44_32_38', 0, 0, 0, 0),
         'UI/Inflight/StopMyCapsule': ('ui_44_32_38', 0, 0, 0, 0),
         'UI/Inflight/ActivateAutopilot': ('ui_44_32_12', 0, 0, 0, 0),
         'UI/Inflight/DeactivateAutopilot': ('ui_44_32_12', 0, 0, 0, 1),
         'UI/Inflight/EjectFromShip': ('ui_44_32_36', 0, 0, 0, 0),
         'UI/Inflight/SelfDestructShipOrPod': ('ui_44_32_37', 0, 0, 0, 0),
         'UI/Inflight/BoardShip': ('ui_44_32_40', 0, 0, 0, 0),
         'UI/Inflight/Jump': ('ui_44_32_39', 0, 0, 0, 0),
         'UI/Inflight/EnterWormhole': ('ui_44_32_39', 0, 0, 0, 0),
         'UI/Inflight/ActivateGate': ('ui_44_32_39', 0, 0, 0, 0),
         'UI/Drones/ScoopDroneToBay': ('ui_44_32_1', 0, 0, 0, 0),
         'UI/Commands/ReadNews': ('ui_44_32_47', 0, 0, 0, 0)}
        self.lastActionSerial = None
        self.sr.actionTimer = None
        self.itemID = None
        self.width = 134
        self.height = 134
        self.pickRadius = -1
        self.oldx = self.oldy = None
        uicore.event.RegisterForTriuiEvents(uiconst.UI_MOUSEUP, self.OnGlobalUp)
        self.mouseMoveCookie = uicore.event.RegisterForTriuiEvents(uiconst.UI_MOUSEMOVE, self.OnGlobalMove)

    def Load(self, slimItem, centerItem = None, setposition = 1):
        if not (uicore.uilib.leftbtn or uicore.uilib.midbtn):
            return
        actions = sm.StartService('menu').CelestialMenu(slimItem.itemID, slimItem=slimItem, ignoreTypeCheck=1)
        reasonsWhyNotAvailable = getattr(actions, 'reasonsWhyNotAvailable', {})
        if not (uicore.uilib.leftbtn or uicore.uilib.midbtn):
            return
        self.itemID = slimItem.itemID
        warptoLabel = DefaultWarpToLabel()[0]
        warpops = {warptoLabel: ('ui_44_32_18', 0, 0, 1, 0)}
        self.actionMenuOptions.update(warpops)
        serial = ''
        valid = {}
        inactive = None
        for each in actions:
            if each:
                if isinstance(each[0], tuple):
                    name = each[0][0]
                else:
                    name = each[0]
                if name in self.actionMenuOptions:
                    valid[name] = each
                    if type(each[1]) not in (str, unicode):
                        serial += '%s_' % name

        if not (uicore.uilib.leftbtn or uicore.uilib.midbtn):
            return
        if serial != self.lastActionSerial:
            uix.Flush(self)
            i = 0
            order = ['UI/Commands/ShowInfo',
             ['UI/Inflight/LockTarget', 'UI/Inflight/UnlockTarget'],
             ['UI/Inflight/ApproachObject', warptoLabel],
             'UI/Inflight/OrbitObject',
             'UI/Inflight/Submenus/KeepAtRange']
            default = [None, ['UI/Inflight/LookAtObject', 'UI/Inflight/ResetCamera'], None]
            lookAtString = 'UI/Inflight/LookAtObject'
            resetCameraString = 'UI/Inflight/ResetCamera'
            openCargoString = 'UI/Commands/OpenCargo'
            groups = {const.groupStation: [None, [lookAtString, resetCameraString], 'UI/Inflight/DockInStation'],
             const.groupCargoContainer: [None, [lookAtString, resetCameraString], openCargoString],
             const.groupMissionContainer: [None, [lookAtString, resetCameraString], openCargoString],
             const.groupSecureCargoContainer: [None, [lookAtString, resetCameraString], openCargoString],
             const.groupAuditLogSecureContainer: [None, [lookAtString, resetCameraString], openCargoString],
             const.groupFreightContainer: [None, [lookAtString, resetCameraString], openCargoString],
             const.groupSpawnContainer: [None, [lookAtString, resetCameraString], openCargoString],
             const.groupSpewContainer: [None, [lookAtString, resetCameraString], openCargoString],
             const.groupDeadspaceOverseersBelongings: [None, [lookAtString, resetCameraString], openCargoString],
             const.groupWreck: [None, [lookAtString, resetCameraString], openCargoString],
             const.groupStargate: [None, [lookAtString, resetCameraString], 'UI/Inflight/Jump'],
             const.groupWormhole: [None, [lookAtString, resetCameraString], 'UI/Inflight/EnterWormhole'],
             const.groupWarpGate: [None, [lookAtString, resetCameraString], 'UI/Inflight/ActivateGate'],
             const.groupBillboard: [None, [lookAtString, resetCameraString], 'UI/Commands/ReadNews'],
             const.groupAgentsinSpace: ['UI/Chat/StartConversation', [lookAtString, resetCameraString], None],
             const.groupDestructibleAgentsInSpace: ['UI/Chat/StartConversation', [lookAtString, resetCameraString], None],
             const.groupPlanetaryCustomsOffices: [None, [lookAtString, resetCameraString], 'UI/PI/Common/AccessCustomOffice']}
            categories = {const.categoryShip: ['UI/Chat/StartConversation', [lookAtString, resetCameraString], 'UI/Inflight/BoardShip'],
             const.categoryDrone: [None, [lookAtString, resetCameraString], 'UI/Drones/ScoopDroneToBay']}
            if slimItem.itemID == session.shipid:
                order = ['UI/Commands/ShowInfo',
                 'UI/Inflight/EjectFromShip',
                 ['UI/Inflight/StopMyShip', 'UI/Inflight/StopMyCapsule'],
                 ['UI/Inflight/ActivateAutopilot', 'UI/Inflight/DeactivateAutopilot'],
                 [lookAtString, resetCameraString]]
            elif slimItem.groupID in groups:
                order += groups[slimItem.groupID]
            elif slimItem.categoryID in categories:
                order += categories[slimItem.categoryID]
            else:
                order += default
            step = 360.0 / 8
            rad = 48
            angle = 180.0
            for actionName in order:
                if actionName is None:
                    angle += step
                    i += 1
                    continue
                if type(actionName) == list:
                    action = None
                    for each in actionName:
                        tryaction = valid.get(each, None)
                        if tryaction and type(tryaction[1]) not in (str, unicode):
                            actionName = each
                            action = tryaction
                            break

                    if action is None:
                        actionName = actionName[0]
                        if actionName in valid:
                            action = valid.get(actionName)
                        elif actionName in reasonsWhyNotAvailable:
                            action = (actionName, reasonsWhyNotAvailable.get(actionName))
                        else:
                            action = (actionName, localization.GetByLabel('UI/Menusvc/MenuHints/NoReasonGiven'))
                elif actionName in valid:
                    action = valid.get(actionName)
                elif actionName in reasonsWhyNotAvailable:
                    action = (actionName, reasonsWhyNotAvailable.get(actionName))
                else:
                    action = (actionName, localization.GetByLabel('UI/Menusvc/MenuHints/NoReasonGiven'))
                disabled = type(action[1]) in (str, unicode)
                props = self.actionMenuOptions[actionName]
                btnpar = uiprimitives.Container(parent=self, align=uiconst.TOPLEFT, width=40, height=40, state=uiconst.UI_NORMAL)
                btnpar.left = int(rad * math.cos(angle * math.pi / 180.0)) + (self.width - btnpar.width) / 2
                btnpar.top = int(rad * math.sin(angle * math.pi / 180.0)) + (self.height - btnpar.height) / 2
                btn = uiprimitives.Sprite(parent=btnpar, name='hudBtn', pos=(0, 0, 40, 40), state=uiconst.UI_DISABLED, texturePath='res:/UI/Texture/Shared/actionMenuBtn.png')
                btnpar.actionID = actionName
                btnpar.name = actionName
                btnpar.action = action
                btnpar.itemIDs = [slimItem.itemID]
                btnpar.killsub = props[3]
                btnpar.pickRadius = -1
                icon = uicontrols.Icon(icon=props[0], parent=btnpar, pos=(4, 4, 0, 0), align=uiconst.TOPLEFT, state=uiconst.UI_DISABLED, idx=0)
                if disabled:
                    icon.color.a = 0.5
                    btn.color.a = 0.1
                if props[4]:
                    icon = uicontrols.Icon(icon='ui_44_32_8', parent=btnpar, pos=(5, 5, 0, 0), align=uiconst.TOPLEFT, state=uiconst.UI_DISABLED, idx=0)
                angle += step
                i += 1

            self.lastActionSerial = serial
            if self.sr.actionTimer is None:
                self.sr.actionTimer = base.AutoTimer(1000, self.Load, slimItem, None, 0)
        if centerItem:
            self.left = max(0, min(uicore.desktop.width - self.width, centerItem.absoluteLeft - (self.width - centerItem.width) / 2))
            self.top = max(0, min(uicore.desktop.height - self.height, centerItem.absoluteTop - (self.height - centerItem.height) / 2))
        elif setposition:
            self.left = max(0, min(uicore.desktop.width - self.width, uicore.uilib.x - self.width / 2))
            self.top = max(0, min(uicore.desktop.height - self.height, uicore.uilib.y - self.height / 2))

    def OnGlobalUp(self, *args):
        if not self or self.destroyed:
            return
        if self.itemID and blue.os.TimeDiffInMs(self.expandTime, blue.os.GetWallclockTime()) < 100:
            sm.StartService('state').SetState(self.itemID, state.selected, 1)
        self.sr.actionTimer = None
        self.sr.updateAngle = None
        mo = uicore.uilib.mouseOver
        self.state = uiconst.UI_HIDDEN
        self.lastActionSerial = None
        if mo in self.children:
            uthread.new(self.OnBtnparClicked, mo)
        else:
            ClearMenuLayer()
        if not self.destroyed:
            uicore.event.UnregisterForTriuiEvents(self.mouseMoveCookie)

    def OnBtnparClicked(self, btnpar):
        sm.StartService('ui').StopBlink(btnpar)
        if btnpar.destroyed:
            ClearMenuLayer()
            return
        if btnpar.killsub and isinstance(btnpar.action[1], list):
            uthread.new(btnpar.action[1][0][2][0][0], btnpar.action[1][0][2][0][1][0])
            ClearMenuLayer()
            return
        if isinstance(btnpar.action[1], basestring):
            sm.StartService('gameui').Say(btnpar.action[1])
        else:
            try:
                apply(*btnpar.action[1:])
            except Exception as e:
                log.LogError(e, 'Failed executing action:', btnpar.action)
                log.LogException()
                sys.exc_clear()

        ClearMenuLayer()

    def OnGlobalMove(self, *args):
        mo = uicore.uilib.mouseOver
        lib = uicore.uilib
        for c in self.children:
            c.opacity = 1.0

        if mo in self.children:
            mo.opacity = 0.7
        if not lib.leftbtn:
            self.oldx = self.oldy = None
            return
        if self.oldx and self.oldy:
            dx, dy = self.oldx - lib.x, self.oldy - lib.y
            camera = sm.GetService('sceneManager').GetRegisteredCamera(evecamera.CAM_SPACE_PRIMARY)
            if mo.name == 'blocker' and not lib.rightbtn:
                fov = camera.fov
                camera.OrbitParent(dx * fov * 0.2, -dy * fov * 0.2)
            elif lib.rightbtn:
                uicore.layer.inflight.zoomlooking = 1
                ClearMenuLayer()
        if hasattr(self, 'oldx') and hasattr(self, 'oldy'):
            self.oldx, self.oldy = lib.x, lib.y
        return 1

    def OnMouseWheel(self, *args):
        camera = sm.GetService('sceneManager').GetActiveCamera()
        if camera.__typename__ == 'EveCamera':
            camera.Dolly(uicore.uilib.dz * 0.001 * abs(camera.translationFromParent))
            camera.translationFromParent = camera.CheckTranslationFromParent(camera.translationFromParent)
        return 1

class Action(uiprimitives.Container):
    __guid__ = 'xtriui.Action'
    default_state = uiconst.UI_NORMAL

    def ApplyAttributes(self, attributes):
        uiprimitives.Container.ApplyAttributes(self, attributes)
        self.actionID = None
        self.disabled = attributes.get('disabled', False)
        self.Prepare_(icon=attributes.icon)

    def Prepare_(self, icon = None):
        opacity = 0.2 if self.disabled else 1.0
        self.icon = GlowSprite(parent=self, align=uiconst.TOALL, texturePath=icon, state=uiconst.UI_DISABLED, opacity=opacity, iconOpacity=1.0)
        self.sr.fill = uiprimitives.Fill(parent=self, state=uiconst.UI_HIDDEN)

    def LoadTooltipPanel(self, tooltipPanel, *args):
        tooltipPanel.LoadGeneric2ColumnTemplate()
        shortcutString = None
        reasonString = None
        distString = None
        keywords = {}
        if isinstance(self.action[1], basestring):
            reasonString = self.action[1]
            if self.actionID == 'UI/Inflight/WarpToWithinDistance':
                keywords['distance'] = sm.GetService('menu').GetDefaultActionDistance('WarpTo')
        else:
            if isinstance(self.action[0], uiutil.MenuLabel):
                actionNamePath, keywords = self.action[0]
            if self.actionID in ('UI/Inflight/OrbitObject', 'UI/Inflight/Submenus/KeepAtRange'):
                key = GetGlobalActiveItemKeyName(actionNamePath)
                current = sm.GetService('menu').GetDefaultActionDistance(key)
                if current is not None:
                    distString = util.FmtDist(current)
                else:
                    distString = localization.GetByLabel('UI/Menusvc/MenuHints/NoDistanceSet')
        if hasattr(self, 'cmdName'):
            shortcutString = uicore.cmd.GetShortcutStringByFuncName(self.cmdName)
        actionName = localization.GetByLabel(self.actionID, **keywords)
        if distString:
            hint = localization.GetByLabel('UI/Menusvc/MenuHints/SelectedItemActionWithDist', actionName=actionName, distanceString=distString)
        else:
            hint = actionName
        tooltipPanel.AddLabelShortcut(hint, shortcutString)
        if reasonString:
            tooltipPanel.AddLabelMedium(text=reasonString, colSpan=tooltipPanel.columns)

    def GetTooltipPointer(self):
        return uiconst.POINT_TOP_2

    def GetMenu(self):
        m = []
        label = ''
        key = GetGlobalActiveItemKeyName(self.actionID)
        if key == 'Orbit':
            label = uiutil.MenuLabel('UI/Inflight/SetDefaultOrbitDistance', {'typeName': self.actionID})
        elif key == 'KeepAtRange':
            label = uiutil.MenuLabel('UI/Inflight/SetDefaultKeepAtRangeDistance', {'typeName': self.actionID})
        elif key == 'WarpTo':
            label = uiutil.MenuLabel('UI/Inflight/SetDefaultWarpWithinDistance', {'typeName': self.actionID})
        if len(label) > 0:
            m.append((label, SetDefaultDist, (key,)))
        return m

    def OnMouseEnter(self, *args):
        if self.disabled:
            return
        if self.sr.Get('fill', None):
            if hasattr(self, 'action'):
                if 'EngageTarget' in self.action[0][0]:
                    crimewatchSvc = sm.GetService('crimewatchSvc')
                    droneInfo = self.action[2]
                    if len(self.itemIDs) > 1 and len(droneInfo) > 1 and isinstance(droneInfo[0], (types.MethodType, types.LambdaType)):
                        droneIDs = droneInfo[1]
                    else:
                        droneIDs = droneInfo
                    targetID = sm.GetService('target').GetActiveTargetID()
                    requiredSafetyLevel = crimewatchSvc.GetRequiredSafetyLevelForEngagingDrones(droneIDs, targetID)
                    if crimewatchSvc.CheckUnsafe(requiredSafetyLevel):
                        if requiredSafetyLevel == const.shipSafetyLevelNone:
                            color = crimewatchConst.Colors.Criminal.GetRGBA()
                        else:
                            color = crimewatchConst.Colors.Suspect.GetRGBA()
                        self.sr.fill.color.SetRGB(*color[:3])
            self.sr.fill.state = uiconst.UI_DISABLED
            self.sr.fill.opacity = 0.25
            uicore.animations.MorphScalar(self.icon, 'glowAmount', self.icon.glowAmount, 1.0, duration=uiconst.TIME_ENTRY)

    def OnMouseExit(self, *args):
        if self.sr.Get('fill', None):
            self.sr.fill.color.SetRGBA(1, 1, 1, 1)
            self.sr.fill.state = uiconst.UI_HIDDEN
            uicore.animations.MorphScalar(self.icon, 'glowAmount', self.icon.glowAmount, 0.0, duration=uiconst.TIME_ENTRY)

    def OnMouseDown(self, *args):
        if self.sr.Get('fill', None):
            self.sr.fill.color.a = 0.5
            uicore.animations.MorphScalar(self.icon, 'glowAmount', self.icon.glowAmount, 1.3, duration=uiconst.TIME_ENTRY)

    def OnMouseUp(self, *args):
        if self.sr.Get('fill', None):
            self.sr.fill.color.a = 0.25
            uicore.animations.MorphScalar(self.icon, 'glowAmount', self.icon.glowAmount, 1.0, duration=uiconst.TIME_ENTRY)

    def OnClick(self, *args):
        sm.StartService('ui').StopBlink(self)
        if self.destroyed:
            ClearMenuLayer()
            return
        if self.killsub and isinstance(self.action[1], list):
            uthread.new(self.action[1][0][1], self.action[1][0][2][0])
            ClearMenuLayer()
            return
        if isinstance(self.action[1], basestring):
            sm.StartService('gameui').Say(self.action[1])
        else:
            try:
                actionMenuLabel = self.action[0]
                labelPath = actionMenuLabel[0]
                if len(self.action) > 2 and self.action[2]:
                    funcArgs = self.action[2]
                    if sm.GetService('menu').CaptionIsInMultiFunctions(labelPath) and not isinstance(funcArgs[0], types.MethodType):
                        funcArgs = (funcArgs,)
                else:
                    funcArgs = ()
                apply(self.action[1], funcArgs)
            except Exception as e:
                log.LogError(e, 'Failed executing action:', self.action)
                log.LogException()
                sys.exc_clear()

        ClearMenuLayer()


"""
Type annotations for license-manager service client paginators.

[Open documentation](https://vemel.github.io/boto3_stubs_docs/mypy_boto3_license_manager/paginators.html)

Usage::

    ```python
    import boto3

    from mypy_boto3_license_manager import LicenseManagerClient
    from mypy_boto3_license_manager.paginator import (
        ListAssociationsForLicenseConfigurationPaginator,
        ListLicenseConfigurationsPaginator,
        ListLicenseSpecificationsForResourcePaginator,
        ListResourceInventoryPaginator,
        ListUsageForLicenseConfigurationPaginator,
    )

    client: LicenseManagerClient = boto3.client("license-manager")

    list_associations_for_license_configuration_paginator: ListAssociationsForLicenseConfigurationPaginator = client.get_paginator("list_associations_for_license_configuration")
    list_license_configurations_paginator: ListLicenseConfigurationsPaginator = client.get_paginator("list_license_configurations")
    list_license_specifications_for_resource_paginator: ListLicenseSpecificationsForResourcePaginator = client.get_paginator("list_license_specifications_for_resource")
    list_resource_inventory_paginator: ListResourceInventoryPaginator = client.get_paginator("list_resource_inventory")
    list_usage_for_license_configuration_paginator: ListUsageForLicenseConfigurationPaginator = client.get_paginator("list_usage_for_license_configuration")
    ```
"""
from typing import Iterator, List

from botocore.paginate import Paginator as Boto3Paginator

from .type_defs import (
    FilterTypeDef,
    InventoryFilterTypeDef,
    ListAssociationsForLicenseConfigurationResponseTypeDef,
    ListLicenseConfigurationsResponseTypeDef,
    ListLicenseSpecificationsForResourceResponseTypeDef,
    ListResourceInventoryResponseTypeDef,
    ListUsageForLicenseConfigurationResponseTypeDef,
    PaginatorConfigTypeDef,
)

__all__ = (
    "ListAssociationsForLicenseConfigurationPaginator",
    "ListLicenseConfigurationsPaginator",
    "ListLicenseSpecificationsForResourcePaginator",
    "ListResourceInventoryPaginator",
    "ListUsageForLicenseConfigurationPaginator",
)

class ListAssociationsForLicenseConfigurationPaginator(Boto3Paginator):
    """
    [Show boto3 documentation](https://boto3.amazonaws.com/v1/documentation/api/1.26.45/reference/services/license-manager.html#LicenseManager.Paginator.ListAssociationsForLicenseConfiguration)
    [Show boto3-stubs documentation](https://vemel.github.io/boto3_stubs_docs/mypy_boto3_license_manager/paginators.html#listassociationsforlicenseconfigurationpaginator)
    """

    def paginate(
        self, *, LicenseConfigurationArn: str, PaginationConfig: PaginatorConfigTypeDef = None
    ) -> Iterator[ListAssociationsForLicenseConfigurationResponseTypeDef]:
        """
        [Show boto3 documentation](https://boto3.amazonaws.com/v1/documentation/api/1.26.45/reference/services/license-manager.html#LicenseManager.Paginator.ListAssociationsForLicenseConfiguration.paginate)
        [Show boto3-stubs documentation](https://vemel.github.io/boto3_stubs_docs/mypy_boto3_license_manager/paginators.html#listassociationsforlicenseconfigurationpaginator)
        """

class ListLicenseConfigurationsPaginator(Boto3Paginator):
    """
    [Show boto3 documentation](https://boto3.amazonaws.com/v1/documentation/api/1.26.45/reference/services/license-manager.html#LicenseManager.Paginator.ListLicenseConfigurations)
    [Show boto3-stubs documentation](https://vemel.github.io/boto3_stubs_docs/mypy_boto3_license_manager/paginators.html#listlicenseconfigurationspaginator)
    """

    def paginate(
        self,
        *,
        LicenseConfigurationArns: List[str] = None,
        Filters: List["FilterTypeDef"] = None,
        PaginationConfig: PaginatorConfigTypeDef = None
    ) -> Iterator[ListLicenseConfigurationsResponseTypeDef]:
        """
        [Show boto3 documentation](https://boto3.amazonaws.com/v1/documentation/api/1.26.45/reference/services/license-manager.html#LicenseManager.Paginator.ListLicenseConfigurations.paginate)
        [Show boto3-stubs documentation](https://vemel.github.io/boto3_stubs_docs/mypy_boto3_license_manager/paginators.html#listlicenseconfigurationspaginator)
        """

class ListLicenseSpecificationsForResourcePaginator(Boto3Paginator):
    """
    [Show boto3 documentation](https://boto3.amazonaws.com/v1/documentation/api/1.26.45/reference/services/license-manager.html#LicenseManager.Paginator.ListLicenseSpecificationsForResource)
    [Show boto3-stubs documentation](https://vemel.github.io/boto3_stubs_docs/mypy_boto3_license_manager/paginators.html#listlicensespecificationsforresourcepaginator)
    """

    def paginate(
        self, *, ResourceArn: str, PaginationConfig: PaginatorConfigTypeDef = None
    ) -> Iterator[ListLicenseSpecificationsForResourceResponseTypeDef]:
        """
        [Show boto3 documentation](https://boto3.amazonaws.com/v1/documentation/api/1.26.45/reference/services/license-manager.html#LicenseManager.Paginator.ListLicenseSpecificationsForResource.paginate)
        [Show boto3-stubs documentation](https://vemel.github.io/boto3_stubs_docs/mypy_boto3_license_manager/paginators.html#listlicensespecificationsforresourcepaginator)
        """

class ListResourceInventoryPaginator(Boto3Paginator):
    """
    [Show boto3 documentation](https://boto3.amazonaws.com/v1/documentation/api/1.26.45/reference/services/license-manager.html#LicenseManager.Paginator.ListResourceInventory)
    [Show boto3-stubs documentation](https://vemel.github.io/boto3_stubs_docs/mypy_boto3_license_manager/paginators.html#listresourceinventorypaginator)
    """

    def paginate(
        self,
        *,
        Filters: List["InventoryFilterTypeDef"] = None,
        PaginationConfig: PaginatorConfigTypeDef = None
    ) -> Iterator[ListResourceInventoryResponseTypeDef]:
        """
        [Show boto3 documentation](https://boto3.amazonaws.com/v1/documentation/api/1.26.45/reference/services/license-manager.html#LicenseManager.Paginator.ListResourceInventory.paginate)
        [Show boto3-stubs documentation](https://vemel.github.io/boto3_stubs_docs/mypy_boto3_license_manager/paginators.html#listresourceinventorypaginator)
        """

class ListUsageForLicenseConfigurationPaginator(Boto3Paginator):
    """
    [Show boto3 documentation](https://boto3.amazonaws.com/v1/documentation/api/1.26.45/reference/services/license-manager.html#LicenseManager.Paginator.ListUsageForLicenseConfiguration)
    [Show boto3-stubs documentation](https://vemel.github.io/boto3_stubs_docs/mypy_boto3_license_manager/paginators.html#listusageforlicenseconfigurationpaginator)
    """

    def paginate(
        self,
        *,
        LicenseConfigurationArn: str,
        Filters: List["FilterTypeDef"] = None,
        PaginationConfig: PaginatorConfigTypeDef = None
    ) -> Iterator[ListUsageForLicenseConfigurationResponseTypeDef]:
        """
        [Show boto3 documentation](https://boto3.amazonaws.com/v1/documentation/api/1.26.45/reference/services/license-manager.html#LicenseManager.Paginator.ListUsageForLicenseConfiguration.paginate)
        [Show boto3-stubs documentation](https://vemel.github.io/boto3_stubs_docs/mypy_boto3_license_manager/paginators.html#listusageforlicenseconfigurationpaginator)
        """


import matplotlib
matplotlib.use('Agg')

import numpy as np
import netCDF4
from datetime import datetime
import pyroms
import pyroms_toolbox
import sys

# get HYCOM Northeast Pacific data from 2007 to 2011

invarname = 'temperature'
outvarname = 'temp'

#read grid and variable attributes from the first file
url='http://tds.hycom.org/thredds/dodsC/datasets/global/GLBa0.08_analysis/data/temp/archv.2010_342_00_3zt.nc'
dataset = netCDF4.Dataset(url)
lon = dataset.variables['Longitude'][2100-1:,550-1:4040+1]
lat = dataset.variables['Latitude'][2100-1:,550-1:4040+1]
z = dataset.variables['Depth'][:]
#spval = dataset.variables[invarname]._FillValue
var = dataset.variables[invarname][0,:,2100-1:,550-1:4040+1]
spval = var.get_fill_value()
units = dataset.variables[invarname].units
long_name = dataset.variables[invarname].long_name
dataset.close()

year = 2011
day = 1

#create netCDF file
outfile = 'HYCOM_GLBa0.08_North_grid.nc'
nc = netCDF4.Dataset(outfile, 'w', format='NETCDF3_64BIT')
nc.Created = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
nc.title = 'HYCOM + NCODA Global 1/12 Analysis (GLBa0.08)'

#create dimensions
Mp, Lp = lon.shape
N = len(z)
nc.createDimension('lon', Lp)
nc.createDimension('lat', Mp)
nc.createDimension('z', N)
nc.createDimension('ocean_time', None)

#create variables
nc.createVariable('lon', 'f', ('lat', 'lon'))
nc.variables['lon'].long_name = 'longitude'
nc.variables['lon'].units = 'degrees_east'
nc.variables['lon'][:] = lon

nc.createVariable('lat', 'f', ('lat', 'lon'))
nc.variables['lat'].long_name = 'latitude'
nc.variables['lat'].units = 'degrees_north'
nc.variables['lat'][:] = lat

nc.createVariable('z', 'f', ('z'))
nc.variables['z'].long_name = 'depth'
nc.variables['z'].units = 'meter'
nc.variables['z'][:] = z

nc.createVariable('ocean_time', 'f', ('ocean_time'))
nc.variables['ocean_time'].units = 'days since 1900-01-01 00:00:00'
jday = pyroms_toolbox.date2jday(datetime(year, 1, 1)) + day - 1
nc.variables['ocean_time'][0] = jday

nc.createVariable(outvarname, 'f', ('ocean_time', 'z', 'lat', 'lon'), fill_value=spval)
nc.variables[outvarname].long_name = long_name
nc.variables[outvarname].units = units
nc.variables[outvarname][0] = var

nc.close()


from __future__ import annotations
import argparse
from copy import copy

import maidenhead

def main(
    loc: str | tuple[float, float],
    precision: int = 3,
    url: bool = False,
    center: bool = False,
) -> str | tuple[float, float]:

    if isinstance(loc, str):  # maidenhead
        maiden = copy(loc)
        loc = maidenhead.to_location(loc, center)
        print(f"{loc[0]:.4f} {loc[1]:.4f}")
    elif len(loc) == 2:  # lat lon
        if isinstance(loc[0], str):
            loc = (float(loc[0]), float(loc[1]))
        maiden = maidenhead.to_maiden(*loc, precision=precision)
        print(maiden)
        loc = maiden
    else:
        raise ValueError("specify Maidenhead grid (single string) or lat lon (with space between)")

    if url:
        uri = maidenhead.google_maps(maiden, center)
        print(uri)
        return uri

    return loc

p = argparse.ArgumentParser(description="convert to / from Maidenhead locator")
p.add_argument(
    "loc", help="Maidenhead grid (single string) or lat lon (with space between)", nargs="+"
)
p.add_argument("-p", "--precision", help="maidenhead precision", type=int, default=3)
p.add_argument("--url", help="also output Google Maps URL", action="store_true")
p.add_argument(
    "-c",
    "--center",
    help="output lat lon of the center of provided maidenhead grid square "
    "(default output: lat lon of it's south-west corner)",
    action="store_true",
)
args = p.parse_args()

if len(args.loc) == 1:
    loc = args.loc[0]
else:
    loc = args.loc

main(loc, args.precision, args.url, args.center)


import tweepy
import config
import json
import pandas as pd
from urllib3.exceptions import ProtocolError

# authorization tokens
consumer_key = config.api_key
consumer_secret = config.api_secret
access_key = config.access_token
access_secret = config.token_secret

class StreamListener(tweepy.StreamListener):
    def on_status(self, status):
        print(status)
        try:
            data = status._json
            # decode the json object from twitter
            # collect geo-tagged data

            # check if place name is available, in case not the entry is named
            # 'no place'
            place=""
            try:
                place = data['place']['name'].replace(',', '')
            except TypeError:
                place = 'no place'
            place_type=""

            try:
                place_type = str(data['place']['place_type']).replace(',', '')
            except TypeError:
                place_type = 'na'

            bbx=""
            try:
                bbx = str(data['place']['bounding_box']['coordinates']).replace(',', '')
            except TypeError:
                bbx = 'na'

            # check if coordinates is available, in case not tentry is named
            # 'no coordinates'
            try:
                coord = str(data['coordinates']['coordinates']).replace(',', '')
            except TypeError:
                coord = 'no coordinates'

            # checks valid location, in case there is not enter named 'no
            # coordinates'
            try:
                loc = str(data['user']['location']).replace(',', '')
            except TypeError:
                loc = 'no location'
                print(status)
            # remove any characters that might cause problems with csv encoding
            # write to file of your choice
            with open("united_arab_emirates.csv", "a", encoding='utf-8') as f:
                    f.write("%s,%s,%s,%s,%s,%s\n" % (data['created_at'],str(data['user']['screen_name']).replace(',', ''), coord, str(data['text']).replace(',', '').replace('\n',''), place, bbx))

        except Exception as e:
            print(e)
    def on_error(self, status_code):
        print("Encountered streaming error (", status_code, ")")

if __name__ == "__main__":

    # complete authorization and initialize the API endpoint
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_key, access_secret)
    api = tweepy.API(auth)

    # initialize stream
    streamListener = StreamListener()
    stream = tweepy.Stream(auth=api.auth, listener=streamListener,tweet_mode='extended')
    with open("united_arab_emirates.csv", "w", encoding='utf-8') as f:
        f.write("date,user,coordinates,text,place,bounding_box\n")
while True:
	try:
        #enter your bounding box for the target city you want to scrape
		stream.filter(locations=[51.6252,22.6316,56.4263,26.1517])
	except (ProtocolError, AttributeError):
		continue


import streamlit as st

from ruins.core import build_config, debug_view, Config
from ruins.plotting import plot_extreme_pdf

_TRANSLATE_DE = dict(
    title="Temperaturen verschieben sich",
    intro="""Dies ist ein langer introtext für dieses Beispiel""",
    temp_intro="""
Wähle zuerst die **mittlere Temperatur** von der du ausgehen möchtest.

Im zweiten Slider musst du die *Variabilität* der mittleren Temperatur angeben.
""",
    temp_outro="""Nun beschreibt die Abbildung den **Ist**-Zustand, also die Häufigkeit
mit der kalte oder warme Tage vorkommen und wie häufig es zu extremen Ereignissen kommt.
Im nächsten Schritt können wir die Erde erwärmen.
""",
    increase_intro="""In diesem Schritt kannst du nun die Durchschnittstemperatur auf
der Erde erhöhen. Eine Erhöhung um **1°C** Celsius mag nicht nach sehr viel klingen, und
natürlich sind die Schwankungen an einem einzigen Tag sehr viel höher, doch beobachte
genau, was mit den **dunkelroten Punkten** in der Abbildung passiert, wenn die Temperatur
sich erhöht.
""",
    increase_outro="""Wenn du die Abbildung genauer betrachtest siehst du zwei Dinge:

  * Es gibt deutlich mehr heiße Tage
  * Es gibt deutlich weniger kalte Tage

Mehr heiße Tage können wir gut mit Wetterbeobachtungen belegen. Allerdings kommt es
nach wie vor zu **kalten** Extremereignissen. Dies legt nahe, dass sich auch die **Verteilung**
der durchschnittlichen Temperaturen geändert haben muss
""",
)

_TRANSLATE_EN = dict(
    title="Temperatures shift",
    intro="""This is a long intro-text for this example""",
    temp_intro="""
First select the **mean temperature** that you will rely on.

The second slider asks for the temperature's variability.
""",
    temp_outro="""The charts illustrates the current situation.
That is the frequency of warm and cold days as well as extremes.
As a next step, you can increase temperature on earth.
""",
    increase_intro="""Now it's time to increase the temperature on Earth.
A rise of **1°C** might not sound like much, and of course temperature is way more
volatile during the day, but watch out for the **darkred points** on the chart, when
you increase mean temperature.
""",
    increase_outro="""The chart suggest two main observations:

  * There are way more hot days
  * There are way less frost days

We find evidence for more hot days in our observation data. But other than the
graph suggests, we still have cold and frost days.
This implies that also the **distribution** of mean temperatures must have changed.
""",
)

def explainer(config: Config) -> None:
    """Introduce the concept"""
    # show the intro
    t = config.translator(de=_TRANSLATE_DE, en=_TRANSLATE_EN)
    st.title(t('title'))
    st.markdown(t('intro'))

    ok = st.button('GOT IT. Get started...')
    if ok:
        st.session_state.tshift_stage = 'temperature_intro'
        st.experimental_rerun()

def temperature_dist_intro(config: Config) -> None:
    """Introduce temperature distributions"""
    # explain
    t = config.translator(de=_TRANSLATE_DE, en=_TRANSLATE_EN)

    st.title(t('title'))
    plot = st.empty()
    st.markdown(t('temp_intro'))

    # build a slider
    mu = st.slider('Temperature', value=13.3, min_value=4., max_value=24., step=0.1, key='mu1')
    std = st.slider('Variability', value=0.1 * mu, min_value=0.0, max_value=10.0, step=0.01, key='std1')

    # make the plot
    fig = plot_extreme_pdf(mu, std)
    plot.plotly_chart(fig, use_container_width=True)

    # outro
    st.markdown("""
""", unsafe_allow_html=True)
    st.markdown(t('temp_outro'))
    ok = st.button('ERDE ERWÄRMEN' if config.lang=='de' else 'WARM THE EARTH')
    if ok:
        st.session_state.tshift_stage = 't_increase'
        st.experimental_rerun()

def increase_temperatures(config: Config) -> None:
    """Increase the temperature on earth"""
    t = config.translator(de=_TRANSLATE_DE, en=_TRANSLATE_EN)

    # create the sidebar
    mu = st.sidebar.slider('Temperature', value=13.3, min_value=4., max_value=24., step=0.1, key='mu1')
    std = st.sidebar.slider('Variability', value=0.1 * mu, min_value=0.0, max_value=10.0, step=0.01, key='std1')

    # add the title
    st.title(t('title'))
    plot = st.empty()
    st.markdown(t('increase_intro'))

    # make the controller to increase temperature
    mu2 = st.slider('New temperature', min_value=mu, value=mu + 1.5, max_value=25., step=0.1, key='mu2')

    # make the plot
    fig = plot_extreme_pdf([mu, mu2], [std, std])
    plot.plotly_chart(fig, use_container_width=True)

    # outro
    st.markdown("""
""", unsafe_allow_html=True)
    st.markdown(t('increase_outro'))

    # button
    ok = st.button('DIE VERTEILUNG ÄNDERN...' if config.lang=='de' else 'CHANGE THE DISTRIBUTION')
    if ok:
        st.session_state.tshift_stage = 'final'
        st.experimental_rerun()

def full_temperature_shift(config: Config) -> None:
    """Full application with all controls"""
    t = config.translator(de=_TRANSLATE_DE, en=_TRANSLATE_EN)

    # create sidebar
    st.sidebar.markdown('#### Present temperature')
    mu = st.sidebar.slider('Temperature', value=13.3, min_value=4., max_value=24., step=0.1, key='mu1')
    std = st.sidebar.slider('Variability', value=0.1 * mu, min_value=0.0, max_value=10.0, step=0.01, key='std1')

    st.sidebar.markdown('#### Future temperatures')
    mu2 = st.sidebar.slider('New temperature', min_value=mu, value=mu + 1.5, max_value=25., step=0.1, key='mu2')
    std2 = st.sidebar.slider('Variability', value=0.1 * mu2, min_value=0.0, max_value=10.0, step=0.01, key='std2')

    # add the title
    st.title(t('title'))

    # make the plot
    fig = plot_extreme_pdf([mu, mu2], [std, std2])
    fig = st.plotly_chart(fig, use_container_width=True)

def main_app(**kwargs):
    # build the config and dataManager from kwargs
    url_params = st.experimental_get_query_params()
    config, _ = build_config(url_params=url_params, omit_dataManager=True, **kwargs)

    # set page properties and debug view
#    st.set_page_config(page_title='Temperature shift', layout=config.layout)
    debug_view.debug_view(None, config, 'DEBUG - initial state')

    # MAIN APP
    # -----------
    stage = config.get('tshift_stage', 'intro')
    if stage == 'intro':
        explainer(config)
    elif stage == 'temperature_intro':
        temperature_dist_intro(config)
    elif stage == 't_increase':
        increase_temperatures(config)
    elif stage == 'final':
        full_temperature_shift(config)
    else:
        st.error("""The application is in an undefinded state. Please contact the developer with the info below""")
        st.json({'message': 'tshift_stage is unkown', 'value': stage, 'origin': 'temperature_shift.py',})
        st.stop()

    # end state debug
    debug_view.debug_view(None, config, 'DEBUG - final state')

if __name__ == '__main__':
    import fire
    fire.Fire(main_app)


#Xi Pardee
#opencv package is required to make the greyscale picture

import pandas as pd
import numpy as np
import scipy.spatial
from sklearn.metrics import confusion_matrix
import cv2
# number of cluster
k = 30

# calculatethe entropy for each cluster
def centropy(label):
    size = float(label.size)
    total = 0
    u, c = np.unique(label, return_counts=True)
    for i in range(u.shape[0]):
        total += (c[i]/size)*np.log2(c[i]/size)

    i = np.argmax(c)
    return -total, u[i]

# calculate the mean entropy
def meanEntropy(instanceCount, entropy):
    total = np.sum(instanceCount * entropy)
    return total/3823

# run each iteration
def iteration(dataSet, centroid, mss, entropy, instanceCount, finalLabel):
    distance = scipy.spatial.distance.cdist(dataSet, centroid)
    index = np.argmin(distance, axis=1)
    empty = np.full((64), -1)
    oldCentroid = np.copy(centroid)

    # update the cluster center
    for i in range(k):
        indexsNumber = np.where(index == i)
        instances = dataSet.values[indexsNumber]
        label = dataSet.index.values[indexsNumber]
        instanceCount[i] = instances.shape[0]
        if instances.size != 0:
            centroid[i] = np.mean(instances, axis=0)
            mss[i] = np.sum(np.square(instances - centroid[i]))/instances.shape[0]
            entropy[i], finalLabel[i] = centropy(label)
        else:
            centroid[i] = empty

    # for empty cluster, choose a random center from the data set
    for i in range(k):
        if centroid[i][0] == -1:
            index = np.random.randint(dataSet.shape[0])
            centroid[i] = np.copy(dataSet.values[index])

    equ = np.array_equal(oldCentroid, centroid)
    return equ

# calculate the mean square separation
def msp(seeds):
    sum = 0
    for i in range(k):
        for j in range(i+1, k):
           sum += np.sum(np.square(seeds[i] - seeds[j]))
    return sum/(k*(k-1)*2)

# classify the test data
def classify(seeds, finalLabel, testData):
    distance = scipy.spatial.distance.cdist(testData, seeds)
    cl = np.argmin(distance, axis=1)
    pre = np.zeros(cl.shape[0])
    for i in range(cl.shape[0]):
        pre[i] = finalLabel[cl[i]]
    return pre

if __name__ == "__main__":
    data = pd.read_csv('optdigits.train', header=None, index_col=64)
    fseeds = np.random.randint(0, 17, size=(k, 64))
    ffinalLabel = np.zeros(k)
    famse = 10000000

    # create clusters 5 times and choose the one with lowest average mean square error
    for i in range(5):
        seeds = np.random.randint(0, 17, size=(k, 64))
        mss = np.zeros(k)
        instanceCount = np.zeros(k)
        entropy = np.zeros(k)
        finalLabel = np.zeros(k)
        equal = False
        times = 1
        while (equal == False):
            print ('iteration: ', times )
            equal = iteration(data, seeds, mss, entropy, instanceCount, finalLabel)
            times += 1
        amse = np.mean(mss)
        mspp = msp(seeds)
        me = meanEntropy(instanceCount, entropy)
        print('average mean-square-error; ' + str(amse))
        print('mean square separation: ' + str(mspp))
        print('mean entropy: ' + str(me))
        print("")
        if amse < famse:
            famse = amse
            fseeds = seeds
            ffinalLabel = finalLabel

    print(ffinalLabel)
    # read in the test data and use the clusters to classify them
    testData = pd.read_csv('optdigits.test', header=None, index_col=64)
    tru = testData.index.values
    pre = classify(fseeds, ffinalLabel, testData)
    matrix = confusion_matrix(tru, pre)

    print (matrix)
    accuracy = float(np.sum(tru == pre))/tru.shape[0]
    print('accuracy rate of test data: ' + str(accuracy))

    # create the grey scale picture
    for i in range(k):
        img = fseeds[i].reshape(8,8)
        img = img * 255 / 16
        img = img.astype(np.uint8)
        height, width = img.shape[:2]
        img = cv2.resize(img, (5 * height, 5 * width), interpolation=cv2.INTER_NEAREST)
        frame = 'frame' + str(i)
        cv2.imshow(frame, img)
        name = str(i)+'.jpg'
        k = cv2.waitKey(0)
        if k == 27:  # wait for ESC key to exit
            cv2.destroyAllWindows()
        elif k == ord('s'):  # wait for 's' key to save and exit
            cv2.imwrite(name, img)
            cv2.destroyAllWindows()