"""Api for working with piastrix."""
from hashlib import sha256
from time import time

from requests import post

class PiastrixApi:
    """Creates and sends all requests to work with the piastrix api."""

    def __init__(
        self,
        secret_key,
        shop_id,
        payway,
        url='https://core.piastrix.com/',
    ):
        """
        Keep base settings for all requests.

        Parameters:
            secret_key: str
            shop_id: str
            payway: str
            url: str
        """
        self.secret_key = secret_key
        self.shop_id = shop_id
        self.payway = payway
        self.url = url

    def _post(self, endpoint, data):
        response = post(f'{self.url}{endpoint}', json=data)
        return response.json()

    def _shop_order_id(self):
        return 'ORDER_{0}'.format(int(time()))

    def _sign(self, required_fields, data):
        sorted_data = [data[key] for key in sorted(required_fields)]
        signed_data = ':'.join(sorted_data) + self.secret_key
        data['sign'] = sha256(signed_data.encode('utf-8')).hexdigest()

    def create_bill(self, data):
        """
        Create bill.

        Parameters:
            data: dict, dict with bill information

        Returns:
            return answer from api
        """
        required_fields = [
            'payer_currency', 'shop_amount', 'shop_currency',
            'shop_id', 'shop_order_id',
        ]
        data.update({
            'shop_id': self.shop_id,
            'shop_order_id': self._shop_order_id(),
            'shop_amount': data['amount'],
            'shop_currency': data['currency'],
            'payer_currency': data['currency'],
        })
        self._sign(required_fields, data)
        answer = self._post('bill/create', data)
        answer.update({'type': 'bill'})
        return answer

    def create_invoice(self, data):
        """
        Create invoice.

        Parameters:
            data: dict, dict with invoice information

        Returns:
            return answer from api
        """
        required_fields = [
            'amount', 'currency', 'shop_id',
            'payway', 'shop_order_id',
        ]
        data.update({
            'shop_id': self.shop_id,
            'shop_order_id': self._shop_order_id(),
            'payway': self.payway,
        })
        self._sign(required_fields, data)
        answer = self._post('invoice/create', data)
        answer.update({'type': 'invoice'})
        return answer

    def create_pay_form(self, data):
        """
        Create invoice form.

        Parameters:
            data: dict, dict with invoice information

        Returns:
            return complete data for the invoice form
        """
        required_fields = ['amount', 'currency', 'shop_id', 'shop_order_id']
        data.update({
            'type': 'pay',
            'shop_id': self.shop_id,
            'shop_order_id': self._shop_order_id(),
        })
        self._sign(required_fields, data)
        return data


import gym
import numpy as np
import pickle
import pandas as pd

env = gym.make('FishingDerby-ram-v4')
env.seed(42)

test = True

def bucket(v, mx, buckets=10):
	v = min(v, mx)
	return round(float(v / mx) * float(buckets))

def phi(x):

	line_x = int(x[32])
	line_y = int(x[67])
	fish6_top_x = int(x[70])

	x_dist = fish6_top_x - line_x

	xleft = abs(x_dist) if x_dist < 0 else 0
	xright = x_dist if x_dist > 0 else 0

	y_dist = 245 - line_y
	ytop = abs(y_dist) if y_dist < 0 else 0
	ybot = y_dist if y_dist > 0 else 0

	caught_fish_idx = 112
	v0 = 0 if x[caught_fish_idx] != 2 else 1

	res = np.clip([xleft, xright, ytop, ybot], 0, 20)
	return (res[0], res[1], res[2], res[3], v0)

observation = env.reset()
# state_size = phi(observation).shape[0]
state_size = len(phi(observation))

actions = [2,3,4,5]#,3,4,5]
n_actions = 4 #env.action_space.n

print(env.unwrapped.get_action_meanings())
print('State size:', state_size)

e = 1.0
e_decay_frames = 100000
e_min = 0.05

alpha = 0.1
gamma = 0.99

counter = 0

pending_reward_idx = 114
last_reward_frames = 0
caught_fish_idx = 112
def get_reward(obs, obs_):

	# if obs_[caught_fish_idx] == 0 and obs[caught_fish_idx] == 6 and obs_[pending_reward_idx] == 0:
	# 	return -0.5

	global last_reward_frames
	if last_reward_frames > 0:
		last_reward_frames -= 1
		return 0

	if obs_[caught_fish_idx] == 2 and obs_[67] <= 210:
		pending_reward = abs(obs_[caught_fish_idx] - 7)
		last_reward_frames = pending_reward + 1
		return pending_reward + 1

	return 0

Q = {}
if test:
	u = pickle._Unpickler(open("Q.dump", "rb"))
	u.encoding = 'latin1'
	Q = u.load()

def getQ(s, a):
	if (s,a) not in Q:
		return 0.0
	else:
		return Q[(s,a)]

def learnQ(state, action, reward, value):
	v = getQ(state, action)
	if v is None:
		Q[(state, action)] = reward
	else:
		Q[(state, action)] = v + alpha * (value - v)

def learn(state1, action1, reward, state2):
	maxqnew = max([getQ(state2, a) for a in actions])
	learnQ(state1, action1, reward, reward + gamma * maxqnew)

episode = 0
df = pd.DataFrame(columns=['episode', 'value'])
while episode < 100:
	observation = env.reset()

	total_catch_value = 0
	total_value = 0
	done = False
	while not done:
		# env.render()

		state = phi(observation)

		# Take a random action fraction e (epsilon) of the time
		action = np.random.choice(range(n_actions), p=[0.26,0.23,0.23,0.28])
		# action = np.random.choice(range(n_actions))

		# Take the chosen action
		observation_, reward, done, info = env.step(actions[action])
		# if reward > 0:
		total_catch_value += reward

		reward = get_reward(observation, observation_)

		# if reward == 0:
		# 	reward = -0.01

		total_value += reward

		# Store the tuple
		state_ = phi(observation_)
		# if not test:
		# 	learn(state, action, reward, state_)

		observation = observation_

		counter += 1

		# Anneal epsilon
		if e > e_min and not test:
			e -= (1.0 - e_min) / e_decay_frames
			e = max(e_min, e)

	df.loc[episode,:] = (episode, total_catch_value)
	df.to_csv('nonuniform_random_100.csv')
	print('Finished episode', episode, total_catch_value, total_value, counter, e)

	episode += 1

df.to_csv('nonuniform_random_100.csv')


import contextlib
from datetime import datetime
from functools import partial
import os
import parser

__author__ = 'jay'

def get_page_by_href(href, cursor, path=None, tracker=None, project_name=None):
    if '#' in href:
        href = href[:href.index('#')]
    if '?' in href:
        href = href[:href.index('?')]
    href = os.path.join(os.path.dirname(path), href)
    href = os.path.normpath(href)
    cursor.execute("select * from resources "
                   "join pages on resources.id=pages.resource_id "
                   "where resources.res_type='page' && path=%s && project=%s",
                   (href, project_name))
    target = cursor.fetchone()
    page = {}
    if target:
        page.update(target)
        if target['postfix']:
            page['title'] = add_postfix_to_title(
                page['title'], page['postfix'])
    if tracker is not None:
        tracker.add(path)
    return page

def add_postfix_to_title(page_title, postfix):
    if isinstance(page_title, unicode):
        page_title = page_title.encode('utf-8')
    if isinstance(postfix, unicode):
        postfix = postfix.encode('utf-8')
    return str(page_title) + ' - ' + str(postfix)

def remove_postfix(page_title, postfix):
    if isinstance(postfix, unicode):
        postfix = postfix.encode()
    return page_title[:page_title.rindex(' - ' + str(postfix))]

def update_page(page_handle, attach_handle, page, source_path, wc, logger):
    with open(source_path, 'r') as reader:
        soup = parser.ConfluencePageInflater(reader, page_handle,
                                             attach_handle)
    page_title = soup.title
    postfix_added = False
    if page['oid'] and page['postfix'] and page['title'] == page_title:
        page_title = add_postfix_to_title(page_title, page['postfix'])
        postfix_added = True
    is_home_page = soup.is_home_page
    keep_trying = True
    while keep_trying:
        if not page['oid']:
            message = "trying create page with title %s" % page_title
        else:
            message = "trying to modify page [%s:%s] with title %s" % (
                page['oid'], page['version'], page_title)
        logger.info(message)
        try:
            page_info = wc.register_page(
                page_title, soup.cleaned_src, pid=page['oid'],
                parent_id=page['parent_id'], mark_home=is_home_page,
                version=page['version'])
            page['oid'] = page_info[0]
            page['version'] = page_info[1]
            break
        except wc.OperationError:
            # maybe version not right, let's try again
            try:
                page_info = wc.get_page(pid=page['oid'], title=page_title)
            except wc.OperationError:
                page_info = None
            if page_info:
                if page['oid']:
                    if page_info[1] != page['version']:
                        logger.error(
                            "failed.\n"
                            "updated page version [%s] and try again"
                            % page_info[1])
                        page['version'] = page_info[1]
                    else:
                        logger.error("fatal problem, will panic.")
                        raise
                elif not postfix_added and page['postfix']:
                    logger.warning(
                        'page with title %s exists, will try create new page'
                        ' with postfix %s' % (page_title,
                                              str(page['postfix'])))
                    page_title = add_postfix_to_title(page_title,
                                                      page['postfix'])
                    postfix_added = True
                else:
                    logger.error('page with title %s exists!!!' % page_title)
                    raise
            else:
                if page['oid']:
                    logger.warning("It seems that remote page has been"
                                   " deleted, lets create a new one...")
                    page['oid'], page['version'] = None, None
                else:
                    logger.error("fatal problem, let's panic.")
                    raise
    if postfix_added:
        page['title'] = remove_postfix(page_title, page['postfix'])
    else:
        page['title'] = page_title
        page['postfix'] = None
    return page

class Filter(object):
    """filter tags and revert

    A filter should work like follow:

    clean up pages:
    process content -> clean up unneeded tags and attributes -> format -> pass to result

    add or modified pages:
                                           not found
    process content -> search attachment  ---------->  create full page      ->      update database
                           |found                                                            |
                           -> create blank page -> upload attachment -> format link -> modified full page

    add attachment: see above

    modified attachment:
    search references -> update all reference -> update database

    delete content:
           page
    delete ----> delete page -----> update database
             | attachment                      |
             -> search references -> delete all reference

    move content: dismiss
    """

    def __init__(self, project_id, db, logger,
                 write_client=None, form=None, **kwargs):
        """

        :param project_id:
        :param db: a database connection. You should be aware that db should
         produce a cursor type that return result as dict.
        :param logger:
        :param write_client:
        :param form:
        :return:
        """
        if not write_client:
            raise ValueError('write_client should not be None!')
        self.project_name = project_id
        self.wc = write_client
        self.db = db
        self.need_update = set()
        self.form = form
        self.logger = logger

    def _put_page(self, cursor, working_dir, page, tracker=None):
        """put a page with client

        :param cursor:
        :param working_dir: path of source, where can actually read source.
            if None, just create a blank page.
        :param page: page info
        :param tracker: a tracker record the page that unable to be update
         complete this time.
        :return:
        """
        source_path = os.path.join(working_dir, page['path'])

        need_update = True
        if not page['oid']:
            need_update = set()

            def do_nothing(*args):
                need_update.add(args[0])

            page = update_page(
                partial(get_page_by_href, path=page['path'], tracker=tracker,
                        cursor=cursor, project_name=self.project_name),
                do_nothing, page, source_path, self.wc, self.logger)
            cursor.execute('insert into resources values(%s, %s, %s, %s)',
                           (None, 'page', page['path'], self.project_name))
            assert cursor.lastrowid > 0
            cursor.execute("insert into pages values(%s, %s, %s, %s, %s,"
                           " %s, %s)",
                           (page['oid'], page['parent_id'], cursor.lastrowid,
                            page['title'], page['postfix'], page['version'],
                            datetime.utcnow()))
        if need_update:
            oid = page['oid']
            page = update_page(
                partial(get_page_by_href, path=page['path'], tracker=tracker,
                        cursor=cursor, project_name=self.project_name),
                partial(self.get_attach_by_href, page_id=page['oid'],
                        base_path=page['path'], cursor=cursor,
                        working_dir=working_dir),
                page, source_path, self.wc, self.logger)
            cursor.execute("update pages set oid=%s, version=%s, title=%s "
                           "where oid=%s", (page['oid'], page['version'],
                                            page['title'], oid))
        return page['oid'], page['version']

    def apply(self, base_dir, outline, changes):
        """push logs to remote

        :param base_dir:
        :param outline: outline of menu, should follow the format {
            phisical_path: list_of_menu_inheritance,
            ...
        }
        :param changes: should follow the format [{
            'operation': operation,  # 'add', 'modified', 'delete' or 'move'
            'content_type': content_type,  # 'page' or 'attachment'
            'path': path,  # path to documentation
            'second_path': second_path,  #  if operation is 'move', this field
            means target_path, else this field is optional.
        }, ...]
        """
        for change_type in ('delete', 'add', 'modified'):
            target_changes = tuple(
                (c['content_type'], c['path'], c['second_path'])
                for c in changes if c['operation'] == change_type)
            if not target_changes:
                continue
            try:
                handle = getattr(self, 'apply_' + change_type)
            except AttributeError:
                continue  # well, fall silently
            try:
                self.db.begin()
                handle(base_dir, outline, target_changes)
                self.db.commit()
            except:
                self.db.rollback()
                raise

    # noinspection PyUnusedLocal
    def apply_delete(self, base_dir, outline, changes):
        """apply delete action on changes

        :param base_dir:
        :param outline:
        :param changes: should follow the format
         [(content_type, path, second_path), ...].
        :return:
        """
        self.logger.info('applying delete changes...')
        attach_changes = tuple(c for c in changes if c[0] == 'attachment')
        page_changes = tuple(c for c in changes if c[0] == 'page')
        with contextlib.closing(self.db.cursor()) as c:
            for change in attach_changes:
                c.execute("select * from resources "
                          "right join attachments"
                          " on resources.id=attachments.resource_id "
                          "where res_type=%s && path=%s && project=%s",
                          ('attachment', change[1], self.project_name))
                attachments = c.fetchall()
                if not attachments:
                    continue  # maybe something we don't care
                for attach in attachments:
                    self.logger.info('deleting attachment: [%s] %s' % (
                        attach['parent_id'], attach['resource_name']))
                    self.wc.delete_attachment(attach['parent_id'],
                                              attach['resource_name'])
                c.execute("delete from attachments where resources_id=%s",
                          (attach['resource_id'],))
                c.execute("delete from resources where id=%s",
                          (attach['resource_id'],))
            for change in page_changes:
                c.execute("select * from resources "
                          "join pages on resources.id=pages.resource_id "
                          "where res_type=%s && path=%s && project",
                          ('page', change[1], self.project_name))
                res = c.fetchone()
                if not res:
                    continue
                self.logger.info(
                    'deleting page: [%s] %s' % (res['oid'], res['title']))
                self.wc.delete_page(res['oid'])
                c.execute("delete from pages where oid=%s", (res['oid'],))
                c.execute("delete from resources where id=%s",
                          (res['resource_id'],))
        self.logger.info('success applying delete.')

    def get_attach_by_href(self, href, title, cursor, base_path=None,
                           page_id=None, working_dir=None):
        """return attachment referenced by href.

         If not exits, will try to create one.
        :param href:
        :param title:
        :param base_path:
        :param page_id:
        :param cursor:
        :param working_dir:
        :return:
        """
        if not page_id:
            return None
        if '#' in href:
            href = href[:href.index('#')]
        if '?' in href:
            href = href[:href.index('?')]
        href = os.path.join(os.path.dirname(base_path), href)
        href = os.path.normpath(href)

        def get_attach():
            cursor.execute("select * from resources "
                           "join attachments "
                           "on resources.id=attachments.resource_id "
                           "where resources.res_type='attachment' "
                           "&& path = %s && parent_id=%s && project=%s",
                           (href, page_id, self.project_name))
            return cursor.fetchone()

        attach = get_attach()
        if attach:
            return attach
        source_path = os.path.join(working_dir, href)
        file_name = href.replace('/', '_')
        try:
            with open(source_path, 'r') as img_reader:
                aid = \
                    self.wc.register_attachment(img_reader,
                                                page_id, file_name)[0]
                self.logger.info(
                    'added a new attachment to page %s: [%s] %s' % (
                        page_id, aid, file_name))
        except IOError:
            return None
        # noinspection PyBroadException
        try:
            cursor.execute("insert into resources values(%s, %s, %s, %s)",
                           (None, 'attachment', href, self.project_name))
            assert cursor.lastrowid > 0
            res_id = cursor.lastrowid
        except:
            # I guess it's because of Duplicate entry. I don't catch
            #  IntegrityError here because no database type is assumed
            cursor.execute("select id from resources "
                           "where path=%s && project=%s",
                           (href, self.project_name))
            res_id = cursor.fetchone()
            if not res_id:
                raise Exception('insert new resources %s fail' % href)
            res_id = res_id['id']

        cursor.execute("insert into attachments values(%s, %s, %s, %s, %s)",
                       (aid, page_id, res_id, file_name, datetime.now()))
        attach = get_attach()
        return attach

    def apply_add(self, base_dir, outline, changes):
        """apply put action on changes

        put action will create a new page if referenced one does not exist.
        only support single parent mode currently.
        :param base_dir:
        :param outline:
        :param changes:
        :return:
        """
        self.logger.info('applying add changes...')
        page_changes = list(c[1] for c in changes if c[0] == 'page')
        tracker = set()  # used to track page not updated completely

        def loop_back(path, c, level, force_update=False):
            c.execute("select * from resources "
                      "join pages on pages.resource_id=resources.id "
                      "where res_type=%s && path=%s && project=%s",
                      ('page', path, self.project_name))
            cur_page = cursor.fetchone()
            if not cur_page:
                dependency = outline.get(path, None)
                if dependency:
                    parent_page = loop_back(dependency[-1], c, level + 1)
                    cur_page = dict(
                        oid=None,
                        version=None,
                        parent_id=parent_page['oid'],
                        path=path,
                        postfix=path,
                    )
                else:
                    cur_page = dict(
                        oid=None,
                        version=None,
                        parent_id=None,
                        path=path,
                        postfix=path,
                    )
                self.logger.info("Can't find registered page with path %s,"
                                 " will create one." % path)
                cur_page['oid'], cur_page['version'] = self._put_page(
                    c, base_dir, cur_page, tracker)
                page_changes.remove(path)
            elif force_update:
                cur_page['oid'], cur_page['version'] = self._put_page(
                    c, base_dir, cur_page, tracker)
                page_changes.remove(path)
            return cur_page

        with contextlib.closing(self.db.cursor()) as cursor:
            clean_outline = set()
            for outline_path in outline:
                if '#' in outline_path:
                    outline_path = outline_path[:outline_path.index('#')]
                if '?' in outline_path:
                    outline_path = outline_path[:outline_path.index('?')]
                clean_outline.add(outline_path)
            # after this loop, all new pages should be created.
            while page_changes:
                page_path = page_changes[-1]
                # we only care about page listed in outline
                if page_path in clean_outline:
                    loop_back(page_path, cursor, 1)
                if page_changes and page_path == page_changes[-1]:
                    page_changes.pop()
            page_changes, tracker = list(tracker), set()
            # after this loop, all possible missing link should be filled.
            while page_changes:
                page_path = page_changes[-1]
                loop_back(page_path, cursor, 1, force_update=True)
        self.logger.info('success applying add.')

    def apply_modified(self, base_dir, outline, changes):
        self.logger.info('applying modified changes...')
        page_changes = tuple(c[1] for c in changes if c[0] == 'page')
        with contextlib.closing(self.db.cursor()) as cursor:
            sql = "select path from resources" \
                  " where project=%s && res_type=%s && path in (%s"
            if len(page_changes) == 1:
                sql += ')'
            else:
                # question: does a sql statement has a length limit?
                sql = ','.join((sql,) +
                               ('%s',) * (len(page_changes) - 2) + ('%s)',))
            args = (self.project_name, 'page') + page_changes
            cursor.execute(sql, args)
            in_path = set(c['path'] for c in cursor.fetchall())
            add_path = set(page_changes) - in_path
            if add_path:
                # in case we start merging from middle
                self.apply_add(base_dir, outline, add_path)
            for page_path in page_changes:
                cursor.execute("select * from resources "
                               "join pages on pages.resource_id=resources.id "
                               "where res_type=%s && path=%s && project=%s",
                               ('page', page_path, self.project_name))
                cur_page = cursor.fetchone()
                assert cur_page is not None
                self._put_page(cursor, base_dir, cur_page, None)
        self.logger.info('success applying modified.')


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

import re
import os
from itertools import chain

data_path = os.path.join(os.getcwd(), "data\en_words\words.txt")

word_regexes = {
    'en': r'[A-Za-z]+'
}

alphabets = {
    'en': 'abcdefghijklmnopqrstuvwxyz',
}

def load_data(file):
    f = open(file, 'r')
    words = {}
    for (i, word) in enumerate(f):
        word = word.strip('\n')
        pairs = word.split(" ")
        words[pairs[0]] = pairs[1]
    f.close()
    return words

class Correcter:
    def __init__(self, threshold=0, lang='en'):
        self.threshold = threshold
        self.nlp_data = load_data(data_path)
        self.lang = lang

        if threshold > 0:
            print(f'Original number of words: {len(self.nlp_data)}')
            self.nlp_data = {k: v for k, v in self.nlp_data.items()
                            if v > threshold}
            print(f'After applying threshold: {len(self.nlp_data)}')

    def existing(self, words):
        """{'the', 'teh'} => {'the'}"""
        return set(word for word in words
                   if word in self.nlp_data)

    def autocorrect_word(self, word):
        """most likely correction for everything up to a double typo"""
        w = Word(word, self.lang)
        candidates = (self.existing([word]) or
                      self.existing(list(w.typos())) or
                      self.existing(list(w.double_typos())) or
                      [word])

        return min(candidates, key=lambda k: self.nlp_data[k])

    def autocorrect_sentence(self, sentence):
        return re.sub(word_regexes[self.lang],
                      lambda match: self.autocorrect_word(match.group(0)),
                      sentence)

    def __call__(self, sentence):
        return(self.autocorrect_sentence(sentence))

class Word(object):
    """container for word-based methods"""

    def __init__(self, word, lang='en'):
        """
        Generate slices to assist with typo
        definitions.
        'the' => (('', 'the'), ('t', 'he'),
                  ('th', 'e'), ('the', ''))
        """
        slice_range = range(len(word) + 1)
        self.slices = tuple((word[:i], word[i:])
                            for i in slice_range)
        self.word = word
        self.alphabet = alphabets[lang]

    def _deletes(self):
        """th"""
        return (self.concat(a, b[1:])
                for a, b in self.slices[:-1])

    def _transposes(self):
        """teh"""
        return (self.concat(a, reversed(b[:2]), b[2:])
                for a, b in self.slices[:-2])

    def _replaces(self):
        """tge"""
        return (self.concat(a, c, b[1:])
                for a, b in self.slices[:-1]
                for c in self.alphabet)

    def _inserts(self):
        """thwe"""
        return (self.concat(a, c, b)
                for a, b in self.slices
                for c in self.alphabet)

    def concat(self, *args):
        """reversed('th'), 'e' => 'hte'"""
        try:
            return ''.join(args)
        except TypeError:
            return ''.join(chain.from_iterable(args))

    def typos(self):
        """letter combinations one typo away from word"""
        yield from self._deletes()
        yield from self._transposes()
        yield from self._replaces()
        yield from self._inserts()

    def double_typos(self):
        """letter combinations two typos away from word"""
        return (e2 for e1 in self.typos()
                for e2 in Word(e1).typos())

correct = Correcter(lang='en')
correct("th")

# In[ ]:


# Script that creates CSV file from images in folder
from os import listdir
# Open CSV file where to store output data
csv_file= open("data.csv", "w")
# Specify directory where images used for training are stored
img_directory= "/home/paok/Documents/FaceRecognition/trainImages"
#create a list with file names in image directory
#Iterate through folder in img_directory
subjects= listdir(img_directory)
#Iterate through file name list and add to csv file
for i in range(len(subjects)):
    pictures = listdir("/home/paok/Documents/FaceRecognition/trainImages/"+subjects[i])
    for pic in pictures:
        # Format text for CSV file: directory/target/file_name/#number
        # sample: /home/paok/Documents/FaceRecognition/trainImages/target/IMG2.jpg;0
        out_put = img_directory+"/"+subjects[i]+"/"+pic+";"+subjects[i]
        # Write output to CSV file
        csv_file.write(out_put)
        csv_file.write("\n")


from django.shortcuts import render,redirect
from django.http.response import HttpResponse
from django.contrib.auth.decorators import login_required
# Create your views here.

@login_required(login_url='/logreg/')
def content_view(request):
    return render(request,"lsssons-gal.html",{"token":1,"username":request.user.username})

@login_required(login_url='/logreg/')
def content_etails_view(request):
    return render(request,"lsssons-gal.html",{"token":1,"username":request.user.username})
def content_details_view(request,lesson_id):
    lessonurl='lessons/'
    if lesson_id==str(1):
        lessonurl+="1t.html"
    elif lesson_id==str(2):
        lessonurl+="2t.html"
    elif lesson_id==str(3):
        lessonurl+="3t.html"
    elif lesson_id==str(4):
        lessonurl+="4t.html"
    elif lesson_id==str(5):
        lessonurl+="5t.html"
    elif lesson_id==str(6):
        lessonurl+="6t.html"
    elif lesson_id==str(7):
        lessonurl+="7t.html"
    elif lesson_id==str(8):
        lessonurl+="8t.html"
    elif lesson_id==str(9):
        lessonurl+="9t.html"
    elif lesson_id==str(10):
        lessonurl+="10t.html"
    else:
        return HttpResponse("<h1> lesson is not here : "+str(lesson_id)+"</h1>")
        lessonurl+="10.html"
    return render(request,lessonurl,{"token":1,"username":request.user.username})

def contentnew_view(request):
    return render(request, "newgal.html")


# -*- coding: utf-8 -*-

#原本为获得as, cp, sin三个加密参数使用，后使用搜索功能不再需要

from selenium import webdriver
import time
import json
import requests
from urllib.parse import urlencode

def getUrls(page, keyWord):
    url = 'https://www.toutiao.com/search_content/'
    returnUrls = []
    for num in range(page):
        data = {'offset': str(num * 20), 'format': 'json', 'keyword': keyWord, 'autoload': 'true', 'count': '20',
                'cur_tab': '1', 'from': 'search_tab'}
        returnUrls.append(url + '?' + urlencode(data))
    return returnUrls

#下面为之前的函数，不用的
def getUrl():
    dic = getKey()
    hot_time = int(time.time())
    url = "https://www.toutiao.com/api/pc/feed/?category=news_tech&" \
          "utm_source=toutiao&widen=1&max_behot_time="+str(hot_time)+"&max_behot_time_tmp="+str(hot_time)+"&" \
          "tadrequire=true&as="+str(dic['as'])+"&cp="+str(dic['cp'])+"1&_signature="+str(dic['sin'])
    with open('filee.txt', 'wb') as f:
        f.write(bytes(url, encoding='utf-8'))
    return url

def getKey():
    web = webdriver.Chrome()
    web.get('https://www.toutiao.com/ch/news_tech/')
    ascp = web.execute_script('return ascp.getHoney()')
    strSin = web.execute_script('return TAC.sign(0)')
    web.quit()
    strAs = ascp['as']
    strCp = ascp['cp']
    dic = {'as': strAs, 'cp': strCp, 'sin': strSin}
    return dic


from transformation.tasks.diff_tasks.base_diff_task import BaseDiffTask
from transformation.assignment.assignment import Assignment
from transformation.assignment.assignment_set import AssignmentSet
from metamodel.document import Document
from metamodel.field import Field
from utilities.utilities import class_object_to_underscore_format
from jinja2 import Template
import os
from diff.operation_type import OperationType
from tracing.manual_tracing import IManualTracing
from transformation.conflict_resolution.question import Question
from transformation.conflict_resolution.answer import Answer
from messages.question_text import *
from messages.question_titles import *
from messages.answer_text import *
from utilities.exceptions import GenerationValidationException

class DiffDemoManualTracingTask(BaseDiffTask, IManualTracing):

    def __init__(self, generator, priority=2, template_name_=None):
        if template_name_ is not None and template_name_ != "":
            self._template_name = template_name_
        else:
            self._template_name = "first_template.tpl"

        self._additional_templates_path = "diff_demo_task/"
        self._additional_templates = {}
        self._template_folder_path = "../../generator_templates/"
        self.insert_additional_templates()

        super().__init__(priority=priority, template_name=template_name_, generator=generator)

    @property
    def template_name(self):
        return self._template_name

    def filtered_elements(self, model):
        """Return iterator over elements in model that are passed to the above template."""
        documents = (document for document in model.elements.values() if type(document) == Document)
        for document in documents:
            yield document

    def insert_additional_templates(self):
        self._additional_templates["field"] = "field_template.tpl"
        self._additional_templates["fields"] = "fields_template.tpl"
        self._additional_templates["document"] = "document_template.tpl"

    def should_generate(self, model, element):
        for filtered_element in self.filtered_elements(model):
            if element.id == filtered_element.id:
                return True

        return False

    def relative_path_for_element(self, diff):
        """Return relative file path receiving the generator output for given element."""
        try:
            document_name = diff.old_object_ref.name
        except:
            document_name = diff.new_object_ref.name
        return document_name + ".task1.html"

    def add(self, diff, filepath):

        if os.path.isfile(filepath):
            relative_path = os.path.join(self._additional_templates_path, self._additional_templates["document"])
        else:
            relative_path = self._template_name

        template_file = open(self._template_folder_path + relative_path)
        template = template_file.read()
        template_file.close()
        content = Template(template).render(element=diff.new_object_ref)
        parser = self._generator.get_parser(filepath)

        if os.path.isfile(filepath):
            tracer = self._generator.tracer
            for trace in tracer.get_traces(diff.new_object_ref.id, self._generator.id):
                parser.insert_element_by_path(trace.old_path, content)
        else:
            parser.parser.parseStr(content)

    def add_with_file(self, diff, filepath):
        template_file = open(self._template_folder_path + self._additional_templates["document"])
        template = template_file.read()
        template_file.close()
        content = Template(template).render(element=diff.new_object_ref)
        parser = self._generator.get_parser(filepath)
        parser.parser.parseStr(content)

    def remove(self, diff, filepath):
        id_ = diff.old_object_ref.id
        self._generator.get_parser(filepath).remove_element_by_id(id_)

    def change(self, diff, filepath):
        parser = self._generator.get_parser(filepath)
        tracer = self._generator.tracer

        if diff.operation_type in [OperationType.CHANGE]:
            old_element = diff.old_object_ref
            new_element = diff.new_object_ref
        else:
            old_element = diff.old_object_ref.elements[diff.key]
            new_element = diff.new_object_ref.elements[diff.key]

        for trace in tracer.get_traces(diff.key, self._generator.id):
            old_property_xpaths = self.get_property_traces(old_element)[diff.property_name]
            new_property_xpaths = self.get_property_traces(new_element)[diff.property_name]
            template_file = open(os.path.join("../templates/",
                                              self._template_name))
            template = template_file.read()
            template_file.close()

            content = Template(template).render(element=diff.new_object_ref)

            for old_property_xpath, new_property_xpath in zip(old_property_xpaths, new_property_xpaths):
                parser.update_element_by_path(old_property_xpath, new_property_xpath, content)

    def subelement_add(self, diff, filepath):
        parser = self._generator.get_parser(filepath)
        tracer = self._generator.tracer
        for trace in tracer.get_traces(diff.new_value.id, self._generator.id):
            template_file = open(os.path.join(self._template_folder_path,
                                              self._additional_templates_path,
                                              self._additional_templates["field"]))
            template = template_file.read()
            template_file.close()
            content = Template(template).render(element=diff.new_value)

            parser.insert_element_by_path(trace.old_path, content)

    def subelement_remove(self, diff, filepath):
        parser = self._generator.get_parser(filepath)
        parser.remove_element_by_id(diff.old_value.id)

    def subelement_change(self, diff, filepath):
        parser = self._generator.get_parser(filepath)
        parser.update_element_by_id(diff.key, diff.property_name, diff.new_value)

    def check_if_element_exists(self, id_, filepath):
        parser = self._generator.get_parser(filepath)
        return parser.check_if_element_exists(id_)

    def ignore(self, diff, filepath):
        pass

    def raise_issue(self, diff, filepath):
        raise GenerationValidationException("Resolve issue later")

    def recreate(self, diff, filepath):
        if diff.operation_type is OperationType.CHANGE:
            self.add(diff, filepath)
        elif diff.operation_type is OperationType.SUBELEMENT_CHANGE:
            self.subelement_add(diff, filepath)

    def recreate_parent(self, diff, filepath):
        if diff.operation_type is OperationType.SUBELEMENT_CHANGE:
            if not os.path.isfile(filepath):
                self.add(diff, filepath)
        elif diff.operation_type is OperationType.SUBELEMENT_ADD:
            self.subelement_add(diff, filepath)
        elif diff.operation_type is OperationType.SUBELEMENT_REMOVE:
            pass

    def compare_versions(self, diff, filepath):
        pass

    def generate_file(self, diff, filepath):
        """Actual file generation from model element."""

        self.insert_traces(diff)

        method = None
        questions = []

        if diff.operation_type == OperationType.ADD:
            method = self.add

        elif diff.operation_type == OperationType.REMOVE:
            exists, element = self.check_if_element_exists(diff.key, filepath)
            if exists:
                method = self.remove
            else:
                question = Question(DOES_NOT_EXIST_TITLE, DOES_NOT_EXIST_TEXT.format(str(diff.old_object_ref)))
                question.element_xpath = self.generator.get_parser(filepath).get_element_xpath(element)
                assignment_set_a1 = AssignmentSet(Assignment(self.ignore))
                assignment_set_a1.setup_context(diff=diff, filepath=filepath)
                a1 = Answer(IGNORE, assignment_set_a1)

                assignment_set_a2 = AssignmentSet(Assignment(self.raise_issue))
                assignment_set_a2.setup_context(diff=diff, filepath=filepath)
                a2 = Answer(RAISE_ISSUE, assignment_set_a2)
                question.answers = [a1, a2]
                questions.append(question)

        elif diff.operation_type in [OperationType.CHANGE, OperationType.SUBELEMENT_CHANGE]:
            exists, element = self.check_if_element_exists(diff.key, filepath)
            if exists:
                self.compare_versions(diff, filepath)
                method = self.change
            else:
                question = Question(DOES_NOT_EXIST_TITLE, DOES_NOT_EXIST_TEXT.format(str(diff.old_object_refl)))
                question.element_xpath = self.generator.get_parser(filepath).get_element_xpath(element)
                assignment_set_a1 = AssignmentSet(Assignment(self.ignore))
                assignment_set_a1.setup_context(diff=diff, filepath=filepath)
                a1 = Answer(IGNORE, assignment_set_a1)

                assignment_set_a2 = AssignmentSet(Assignment(self.recreate))
                assignment_set_a2.setup_context(diff=diff, filepath=filepath)
                a2 = Answer(RECREATE, assignment_set_a2)
                question.answers = [a1, a2]
                questions.append(question)

        elif diff.operation_type == OperationType.SUBELEMENT_ADD:
            parent_element = diff.old_object_ref
            exists, element = self.check_if_element_exists(parent_element.id, filepath)
            if not exists:
                question = Question(DOES_NOT_EXIST_TITLE, DOES_NOT_EXIST_TEXT.format(str(parent_element)))
                assignment_set_a1 = AssignmentSet(Assignment(self.ignore))
                assignment_set_a1.setup_context(diff=diff, filepath=filepath)
                a1 = Answer(IGNORE, assignment_set_a1)

                assignment_set_a2 = AssignmentSet(Assignment(self.recreate_parent), Assignment(self.subelement_add))
                assignment_set_a2.setup_context(diff=diff, filepath=filepath)
                a2 = Answer(RECREATE, assignment_set_a2)
                question.answers = [a1, a2]
                questions.append(question)

            new_element = diff.new_value
            exists, element = self.check_if_element_exists(new_element.id, filepath)
            if exists:
                question = Question(ALREADY_EXISTS_TITLE, ALREADY_EXISTS_TITLE.format(str(new_element)))
                question.element_xpath = self.generator.get_parser(filepath).get_element_xpath(element)
                assignment_set_a1 = AssignmentSet(Assignment(self.ignore))
                assignment_set_a1.setup_context(diff=diff, filepath=filepath)
                a1 = Answer(IGNORE, assignment_set_a1)

                assignment_set_a2 = AssignmentSet(Assignment(self.subelement_add))
                assignment_set_a2.setup_context(diff=diff, filepath=filepath)
                a2 = Answer(ADD, assignment_set_a2)
                question.answers = [a1, a2]
                questions.append(question)
            else:
                method = self.subelement_add

        elif diff.operation_type == OperationType.SUBELEMENT_REMOVE:
            parent_element = diff.old_object_ref
            exists, element = self.check_if_element_exists(parent_element.id, filepath)
            if not exists:
                question = Question(DOES_NOT_EXIST_TITLE, DOES_NOT_EXIST_TEXT.format(str(parent_element)))
                assignment_set_a1 = AssignmentSet(Assignment(self.ignore))
                assignment_set_a1.setup_context(diff=diff, filepath=filepath)
                a1 = Answer(IGNORE, [self.ignore])

                assignment_set_a2 = AssignmentSet(Assignment(self.recreate), Assignment(self.subelement_remove))
                assignment_set_a2.setup_context(diff=diff, filepath=filepath)
                a2 = Answer(RECREATE, assignment_set_a2)
                question.answers = [a1, a2]
                questions.append(question)
            else:
                method = self.subelement_remove

        elif diff.operation_type == OperationType.SUBELEMENT_CHANGE:
            parent_element = diff.old_object_ref
            exists, element = self.check_if_element_exists(parent_element.id, filepath)
            if not exists:
                question = Question(DOES_NOT_EXIST_TITLE, DOES_NOT_EXIST_TEXT.format(str(parent_element)))
                question.element_xpath = self.generator.get_parser(filepath).get_element_xpath(element)
                assignment_set_a1 = AssignmentSet(Assignment(self.ignore))
                assignment_set_a1.setup_context(diff=diff, filepath=filepath)
                a1 = Answer(IGNORE, assignment_set_a1)

                assignment_set_a2 = AssignmentSet(Assignment(self.recreate))
                assignment_set_a2.setup_context(diff=diff, filepath=filepath)
                a2 = Answer(RECREATE, assignment_set_a2)
                question.answers = [a1, a2]
                questions.append(question)
            else:
                method = self.subelement_change

        assignment_set = AssignmentSet(Assignment(method))
        assignment_set.setup_context(diff=diff, filepath=filepath, task=self)
        return assignment_set, questions

    def evaluate_single_element_template(self, diff):
        folder_name = class_object_to_underscore_format(type(self))
        path = os.path.join(self._generator.templates_path, folder_name)

    def get_traces(self, diff):
        return self._generator.tracer.get_traces(diff.key, self._generator.id)

    def get_selection_trace(self, element):
        if type(element) is Document:
            return "//body/div[@_id=\"" + str(element.id) + "\"]"
        if issubclass(type(element), Field):
            return "//body/div[@_id=\"" + str(element.container.id) + "\"]/ul/li[@_id=\"" + str(element.id) + "\"]"

    def get_insertion_trace(self, element):
        if type(element) is Document:
            return "//body"
        if issubclass(type(element), Field):
            return "//body/div[@_id=\"" + str(element.container.id) + "\"]/ul"

    def get_property_traces(self, element):
        properties = {}
        if type(element) is Document:
            properties["id"] = ["//body/div[@_id=\"" + str(element.id) + "\"]"]
            properties["name"] = ["//body/div[@_id=\"" + str(element.id) + "\"]/p[text()=\"My document name: " +
                                  str(element.name) + "\"]"]
            properties["project.id"] = ["//body/div[@_id=\"" + str(element.id) +
                                        "\"]/p[text()=\"Parent project id:  " + str(element.container.id) + "\"]"]
            properties["project.name"] = ["//body/div[@_id=\"" + str(element.id) +
                                          "\"]/p[text()=\"Parent project name:  " + str(element.container.name) + "\"]"]

        if issubclass(type(element), Field):
            properties["id"] = ["//body/div[@_id=\"" + str(element.container.id) + "\"]/ul/li[@_id]"]
            properties["name"] = ["//body/div[@_id=\"" + str(element.container.id) + "\"]/ul/li[@_id=\"" +
                                  str(element.id) + "\"][text()=\"Field (" + str(element.name) + ")\"]",

                                  "//body/div[@_id=\"" + str(element.container.id) + "\"]/ul/li[@_id=\"" +
                                  str(element.id) + "\"]"]

        return properties


# This code flattens Amazon product reviews into csv file
import json
import os
import sys
import pandas
import flatten_json
sys.path.append("etl_lib")
import etl_lib

amazon_reviews ="/Volumes/My Passport/BU - Data warehousing/datasets/AmazonReviews"
top_level_keys =[u'Reviews', u'ProductInfo']
review_keys =[u'Author', u'ReviewID', u'Overall', u'Content', u'Title', u'Date']
product_keys =[u'Price', u'ProductID', u'Features', u'ImgURL', u'Name']

# For each product there are many reviews ie. 1 to many relationship

#Print columns first
category_list=[dir.strip() for dir in os.listdir(amazon_reviews) if dir != '.DS_Store']
log_file = open("print_amzn.log","w")
exception_log = open("print_amzn_except.log","w")

for category in category_list:
 full_dir_path = amazon_reviews + "/" + category
 file_list = [fl for fl in os.listdir(full_dir_path) if fl != '.DS_Store']
 for fl in file_list:
    try:
     with open(full_dir_path + "/" + fl) as f:
      data = f.read()
      jsondata = json.loads(data)
      for row in jsondata:
          # Pull the product info first
          #Notice the use of string to deal with None return values in case lookup of column fails
          product_info="@".join([jsondata['ProductInfo'][key] or '' for key in product_keys])
          number_reviews = len(jsondata['Reviews'])
          for i in range(number_reviews):
              review = jsondata['Reviews'][i]
              review_info="@".join([review[key] or '' for key in review_keys])
              #use * as record terminator
              sys.stdout.write(category + "@" + product_info + "@" + review_info + "*")
    except:
        exception_log.write(str(sys.exc_info()[0]) + "\n")
        log_file.write(full_dir_path + "/" + fl + "\n")

log_file.close()
exception_log.close()


import discord
import asyncio
import pickle

with open("contestantsID.txt", "rb") as fp:
    local_contestantsID = pickle.load(fp)

with open("contestants.txt", "rb") as fp:
    local_contestants = pickle.load(fp)

commands = ["register","unregister","help","players"]
commands_text = ["Used followed by name like '>register PlayerName'","Used followed by name like '>unregister PlayerName'","Gives a list of commands","Give a list of players in tournaments"]

client = discord.Client()

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('-----------')

@client.event
async def on_message(message):
    b = message.author.id
    if message.content.startswith('>register'):
        if len(local_contestants) == 16:
            await message.channel.send("The tournament is full right now!")

        else:
            m = message.content.split()
            try:
                a = m[1].upper()
                await message.channel.send(a+", will be your name in the tournament!")
                local_contestants.append(a)
                local_contestantsID.append(b)
                with open("contestants.txt", "wb") as fp:
                    pickle.dump(local_contestants, fp)

                with open("contestantsID.txt", "wb") as fp:
                    pickle.dump(local_contestantsID, fp)

            except:
                await message.channel.send("You did not give me a name!")

    elif message.content.startswith('>unregister'):
        m = message.content.split()

        try:
            a = m[1].upper()

            for x in range(0,len(local_contestants)):
                if local_contestants[x] == a:
                    if message.author.id == local_contestantsID[x]:
                        local_contestants.pop(x)
                        local_contestantsID.pop(x)

                        with open("contestants.txt", "wb") as fp:
                            pickle.dump(local_contestants, fp)

                        with open("contestantsID.txt", "wb") as fp:
                            pickle.dump(local_contestantsID, fp)

                        await message.channel.send("Done!")

                    else:
                        await message.channel.send("That is not your registration!")

        except:
            await message.channel.send("You did not give me a name!")

    elif message.content.startswith('>help'):
        for i in range(0,len(commands)):
            await message.channel.send(commands[i])
            await message.channel.send(commands_text[i])
            await message.channel.send("-------------")

    elif message.content.startswith('>players'):

        await message.channel.send("Currently, there are "+ str(len(local_contestants)) +" players in the tournament:")

        for i in range(0,len(local_contestants)):
            await message.channel.send(local_contestants[i])

    elif message.content.startswith('>purge'):
        if message.author.id == 340610417967497216 or message.author.id == 364830006334980096:
            local_contestants.clear()
            local_contestantsID.clear()

            with open("contestants.txt", "wb") as fp:
                pickle.dump(local_contestants, fp)

            with open("contestantsID.txt", "wb") as fp:
                pickle.dump(local_contestantsID, fp)

            await message.channel.send("Done!")

    elif message.content.startswith('>delete'):
        if message.author.id == 340610417967497216 or message.author.id == 364830006334980096:
            m = message.content.split()
            a = m[1].upper()

            for l in range(0,len(local_contestants)):
                if local_contestants[l] == a:
                    local_contestants.pop(l)
                    local_contestantsID.pop(l)

                    with open("contestants.txt", "wb") as fp:
                        pickle.dump(local_contestants, fp)

                    with open("contestantsID.txt", "wb") as fp:
                        pickle.dump(local_contestantsID, fp)

                    await message.channel.send("Done!")

    elif message.content.startswith('>data'):
        if message.author.id == 340610417967497216 or message.author.id == 364830006334980096:
            await message.channel.send(local_contestants)
            await message.channel.send(local_contestantsID)

    elif message.content.startswith('>timer'):
        await message.channel.send("Tournaments are held every Friday at 5:00 PM BST.")

f = open("env.txt", "r")
contents = f.read()
client.run(contents)


#sam deploy --no-confirm-changeset
import json
import os
import logging
import boto3
import io
#import request
from botocore.exceptions import ClientError  # type: ignore[import]
import base64
import copy

from common import cors_headers

logger = logging.getLogger('scambiupload')
logger.setLevel(logging.INFO)

SCAMBIFOLDER = os.environ.get('SCAMBIFOLDER')
SCAMBIIMAGES = os.environ.get('SCAMBIIMAGES')
SCAMBICONFIG = os.environ.get('SCAMBICONFIG')
CONFIG_FILE = os.environ.get('CONFIG_FILE')
SAMPLE_CONFIG_FILE = os.environ.get('SAMPLE_CONFIG_FILE')
SIM_LAMBDA = os.environ.get('SIM_LAMBDA')
RAW_IMAGE = os.environ.get('RAW_IMAGE')
OVERLAY_IMAGE = os.environ.get('OVERLAY_IMAGE')
SCAMBIWEB = os.environ.get('SCAMBIWEB')
_EVENT_QUEUE_URL = os.environ.get('EVENT_QUEUE_URL')
EVENTS_TABLE = os.environ.get('EVENTS_TABLE')

s3_client = boto3.resource('s3')
sqs_client = boto3.client('sqs')
# sqs_url = sqs_client.get_queue_url(
#         QueueName="positions",
#     )
dynamodb = boto3.resource('dynamodb')
db_table_client = dynamodb.Table(EVENTS_TABLE)
lambda_client = boto3.client('lambda')

def purge_queue(_sqs_client, queue_url):
    """
    Deletes the messages in a specified queue
    """
    try:
        response = _sqs_client.purge_queue(QueueUrl=queue_url)
    except ClientError:
        logger.exception(f'Could not purge the queue - {queue_url}.')
        raise
    else:
        return response

def send_message(queue, message_body, message_attributes=None):
    """
    Send a message to an Amazon SQS queue.

    :param queue: The queue that receives the message.
    :param message_body: The body text of the message.
    :param message_attributes: Custom attributes of the message. These are key-value
                               pairs that can be whatever you want.
    :return: The response from SQS that contains the assigned message ID.
    """
    if not message_attributes:
        message_attributes = {}

    try:
        response = queue.send_message(
            MessageBody=message_body,
            MessageAttributes=message_attributes
        )
    except ClientError as error:
        logger.exception("Send message failed: %s", message_body)
        raise error
    else:
        return response

class Boto3S3Client():

    def __init__(self, aws_region):
        self._aws_region = aws_region
        self._s3_client = boto3.client(  # type: ignore[attr-defined]
            's3',
            region_name=aws_region
            )

    def write_img(
        self,
        img,
        bucket_name: str,
        folder_name: str,
        object_name: str
    ):
        if folder_name is None:
            self._s3_client.put_object(
                Bucket=bucket_name,
                Key=f"{object_name}",
                Body=bytes(img))
        else:
            self._s3_client.put_object(
                Bucket=bucket_name,
                Key=f"{folder_name}/{object_name}",
                Body=img)

    def delete(
            self,
            bucket_name: str,
            folder_name: str,
            object_name: str
    ):

        if folder_name is None:
            self._s3_client.delete_object(
                Bucket=bucket_name,
                Key=object_name)

        else:
            self._s3_client.delete_object(
                Bucket=bucket_name,
                Key=f"{folder_name}/{object_name}"
                )

    def write(
            self,
            input_bytes: bytes,
            *,
            bucket_name: str,
            folder_name: str,
            object_name: str
    ):

        bytes_io = io.BytesIO(input_bytes)
        if folder_name is None:
            self._s3_client.upload_fileobj(
                bytes_io,
                bucket_name,
                object_name
                )
        else:
            self._s3_client.upload_fileobj(
                bytes_io,
                bucket_name,
                f"{folder_name}/{object_name}"
                )

    def read(
            self,
            *,
            bucket_name: str,
            folder_name: str,
            object_name: str,
    ):

        obj = io.BytesIO()
        try:
            self._s3_client.download_fileobj(
                bucket_name,
                f"{folder_name}/{object_name}",
                obj)
        except ClientError as error:
            # logging here
            raise error

        return obj.getvalue()

    def list_files(
            self,
            *,
            bucket_name: str,
            folder_name: str,
    ):

        #  paginator required to avoid item limitation returned
        #  by list_objects_v2
        paginator = self._s3_client.get_paginator('list_objects_v2')
        pages = paginator.paginate(
            Bucket=bucket_name,
            Prefix=folder_name)

        files = []
        for page in pages:
            files += [i["Key"] for i in page["Contents"]]

        out_filenames = []
        prefix = folder_name + "/"
        for file in files:
            #  ensure file has folder prefix
            if file[0:len(prefix)] == prefix:
                out_filenames.append(file.replace(prefix, ""))

        return out_filenames
s3_custom = Boto3S3Client("us-east-1")

def decode_image_from_str(encoded_image: str):
    """decodes image from string. Expects base64 encoding

    Args:
        encoded_image: str representing image

    Returns:
        np.array image"""
    jpg_original = base64.b64decode(encoded_image)
    return jpg_original

def str_to_bytes(string_: str):
    return str.encode(string_)

def bytes_to_str(bytes_: bytes):
    return bytes_.decode()

def lambda_handler(event, context):

    order = json.loads(event['body'])
    # this is the dynamic reference in template.yaml
    authentication_code = order['authentication']

    if authentication_code not in ["farts", "teehee"]:
        print("bad log in")
        return{
            'statusCode': 201,
            'headers': cors_headers,
            'body': json.dumps({'message': 'stranger danger'})
        }

    action = order['action']

    if action == "image_raw":

        image_bytes = str_to_bytes(order['payload'])
        img_jpg = decode_image_from_str(order['payload'])
        s3_custom.write(
            input_bytes=image_bytes,
            bucket_name=SCAMBIFOLDER,
            folder_name=SCAMBIIMAGES,
            object_name=RAW_IMAGE)

        s3_custom.write_img(
            img=img_jpg,
            bucket_name=SCAMBIWEB,
            folder_name=None,
            object_name=RAW_IMAGE)

        return{
            'statusCode': 201,
            'headers': cors_headers,
            'body': json.dumps({
                'message': 'uploaded image to ',
                'bucketfiles': ""})
        }

    if action == "image_overlay":

        image_bytes = str_to_bytes(order['payload'])

        img_jpg = decode_image_from_str(order['payload'])

        s3_custom.write(
            input_bytes=image_bytes,
            bucket_name=SCAMBIFOLDER,
            folder_name=SCAMBIIMAGES,
            object_name=OVERLAY_IMAGE)

        s3_custom.write_img(
            img=img_jpg,
            bucket_name=SCAMBIWEB,
            folder_name=None,
            object_name=OVERLAY_IMAGE)

        return{
            'statusCode': 201,
            'headers': cors_headers,
            'body': json.dumps({
                'message': 'uploaded image to ',
                'bucketfiles': ""})
        }

    if action == "getimage_raw":

        obj = s3_custom.read(
            bucket_name=SCAMBIFOLDER,
            folder_name=SCAMBIIMAGES,
            object_name=RAW_IMAGE)

        return{
            'statusCode': 201,
            'headers': cors_headers,
            'body': json.dumps({
                'message': 'got raw image ok',
                'image': bytes_to_str(obj)})
        }

    if action == "getimage_overlay":

        obj = s3_custom.read(
            bucket_name=SCAMBIFOLDER,
            folder_name=SCAMBIIMAGES,
            object_name=OVERLAY_IMAGE)

        return{
            'statusCode': 201,
            'headers': cors_headers,
            'body': json.dumps({
                'message': 'got overlay image ok',
                'image': bytes_to_str(obj)})
        }

    if action in ["check_event"]:
        # check for event and then remove all
        scan = db_table_client.scan()# do not use on big tables!!

        print("getting event", scan['Items'])
        output = copy.deepcopy(scan['Items'])
        with db_table_client.batch_writer() as batch:
            for each in scan['Items']:
                batch.delete_item(Key=each)
        return{
            'statusCode': 200,
            'headers': cors_headers,
            'body': json.dumps(output)
        }
    if action in [
                    "reset",
                    "update_image",
                    "update_image_all"]:

        if action in ["update_image", "update_image_all"]:
            s3_custom.delete(
                bucket_name=SCAMBIWEB,
                folder_name=None,
                object_name=OVERLAY_IMAGE)
            s3_custom.delete(
                bucket_name=SCAMBIWEB,
                folder_name=None,
                object_name=RAW_IMAGE)
        # we onlyt want one action at a time

        scan = db_table_client.scan()# do not use on big tables!!
        print(scan['Items'])
        with db_table_client.batch_writer() as batch:
            for each in scan['Items']:
                batch.delete_item(Key=each)

        response = db_table_client.put_item(
            Item={
                'event': action
            }
        )

        status_code = response['ResponseMetadata']['HTTPStatusCode']
        print("WRITE TO DB", status_code)

        # need this for CORS
        return{
            'statusCode': 200,
            'headers': cors_headers,
            'body': json.dumps({
                'message': f'{action} ok'})
        }
    #sam deploy --no-confirm-changeset
    if action == "send_sample_config":
        click_data = (order['data'])
        messages_bytes = str_to_bytes(json.dumps(click_data))
        print(messages_bytes)

        # now load existing one
        curr_config_bytes = s3_custom.read(
            bucket_name=SCAMBIFOLDER,
            folder_name=SCAMBICONFIG,
            object_name=SAMPLE_CONFIG_FILE)

        curr_config_str = bytes_to_str(curr_config_bytes)
        curr_config_json = json.loads(curr_config_str)
        print("curr_config_json", curr_config_json)
        print("type curr_config_json", type(curr_config_json))
        print("click_data", click_data)
        print("type click_data", type(click_data))

        # check both have same keys
        if not set(curr_config_json.keys()) == set(click_data.keys()):
            return {
                'statusCode': 201,
                'headers': cors_headers,
                'body': json.dumps({
                    'message': "ERROR - CONFIG KEYS DO NOT MATCH - RELOAD DEFAULTS"})
            }
        s3_custom.write(
            input_bytes=messages_bytes,
            bucket_name=SCAMBIFOLDER,
            folder_name=SCAMBICONFIG,
            object_name=SAMPLE_CONFIG_FILE)

        # need this for CORS
        return {
            'statusCode': 200,
            'headers': cors_headers,
            'body': json.dumps({
                'message': "send_sample_config ok"})
        }

    if action == "get_region_sim":
        print("get region sim")
        lambda_payload = json.dumps({}).encode('utf-8') # doesnt matter for now - but add action in here later
        print("get region sim created json")
        response = lambda_client.invoke(FunctionName=SIM_LAMBDA,
                     InvocationType='RequestResponse',
                     Payload=lambda_payload)
        print("region sim response", response)
        return{
            'statusCode': 201,
            'headers': cors_headers,
            'body': json.dumps({
                'message': 'request_config ok',
                'config': bytes_to_str(config_bytes)})
        }

    if action == "request_config":

        config_bytes = s3_custom.read(
            bucket_name=SCAMBIFOLDER,
            folder_name=SCAMBICONFIG,
            object_name=CONFIG_FILE)
        print("req", config_bytes)
        return{
            'statusCode': 201,
            'headers': cors_headers,
            'body': json.dumps({
                'message': 'request_config ok',
                'config': bytes_to_str(config_bytes)})
        }
    if action == "request_sample_config":

        config_bytes = s3_custom.read(
            bucket_name=SCAMBIFOLDER,
            folder_name=SCAMBICONFIG,
            object_name=SAMPLE_CONFIG_FILE)
        print("req", config_bytes)
        return{
            'statusCode': 201,
            'headers': cors_headers,
            'body': json.dumps({
                'message': 'request_sample_config ok',
                'config': bytes_to_str(config_bytes)})
        }
    if action == "sendposfinish":
        # should be a list of dictionaries
        click_data = (order['data'])

        # for getting objects off a queue
        # clickpositions = []
        # for get_msg in range(0, 200):
        #     # Receive message from SQS queue
        #     response = sqs_client.receive_message(
        #         QueueUrl=_EVENT_QUEUE_URL,
        #         AttributeNames=[
        #             'SentTimestamp'
        #         ],
        #         MaxNumberOfMessages=1,
        #         MessageAttributeNames=[
        #             'All'
        #         ],
        #         VisibilityTimeout=0,
        #         WaitTimeSeconds=0
        #     )

        #     # obviously much better way of doing this
        #     if 'Messages' not in response:
        #         break

        #     message = response['Messages'][0]
        #     clickpositions.append(message['Body'])
        #     receipt_handle = message['ReceiptHandle']

        #     # Delete received message from queue
        #     sqs_client.delete_message(
        #         QueueUrl=_EVENT_QUEUE_URL,
        #         ReceiptHandle=receipt_handle
        #     )

        messages_bytes = str_to_bytes(json.dumps(click_data))
        print(messages_bytes)
        s3_custom.write(
            input_bytes=messages_bytes,
            bucket_name=SCAMBIFOLDER,
            folder_name=SCAMBICONFIG,
            object_name=CONFIG_FILE)

        # need this for CORS
        return {
            'statusCode': 200,
            'headers': cors_headers,
            'body': json.dumps({
                'message': "processed OK"})
        }

    return{
        'statusCode': 201,
        'headers': cors_headers,
        'body': json.dumps({
            'message': 'unmatched action'})
    }


# Standard packages
import tensorflow as tf
import numpy as np
import time

# Custom packages
from core.simulation.simulation import Simulation
from core.simulation.game import Game
from core.simulation.reward import RewardId
from core.simulation.random_generator import RandomGeneratorUniform
from core.layers.modelpicnnthree import ModelPICNNThree
from core.layers.modelpicnntwo import ModelPICNNTwo
from core.simulation.validation import Optimum2PeriodSolution
import matplotlib.pyplot as plt

from core.simulation.greedy_estimator import GreedyEstimator

# Initalize necessary simulation objects:

# Define the random generator for the price process
random_generator_uniform = RandomGeneratorUniform(0.8, 1.8)

# Define the neural network
# 2 Layer PICNN
negQ = ModelPICNNTwo(
    [200],
    [200, 1],
    weight_initializer=tf.random_normal_initializer(mean=0.0, stddev=0.4),
    name="negQ",
)

# Define the game setting
# Two period game
game_two_period = Game(
    x_0=5.0,
    y_0=10.0,
    S_0=1.0,
    T=2,
    alpha=1,
    random_generator=random_generator_uniform,
    reward_func=RewardId,
)

# Three period game
game_three_period = Game(
    x_0=1.0,
    y_0=10.0,
    S_0=1.0,
    T=3,
    alpha=0.01,
    random_generator=random_generator_uniform,
    reward_func=RewardId,
)
# Define exploration process via the epsilon greedy factor
greedy_estimator = GreedyEstimator(
    stop_exploring_at=0.075, final_exploration_rate=0.1, stagnate_epsilon_at=0.25
)

# Setup the simulation object
simulation = Simulation(
    greedy_estimator=greedy_estimator,
    ICNN_model=negQ,
    game=game_two_period,
    num_episodes=50000,
    ITERATIONS=1,
    size_minibatches=1,
    capacity_replay_memory=1,
    optimization_iterations=3,
    optimizer=tf.keras.optimizers.SGD(learning_rate=0.000025),
    discount_factor=0.5,
    show_plot_every=1000000,
    LOG_NUM=3336,
    initial_action_for_optimization=tf.Variable([[0.7], [0.2]]),
)

print(
    "\n==============",
    "The optimal choice of this simulation is {}, with expected value of the random generator {}".format(
        Optimum2PeriodSolution(
            np.array(
                [
                    [game_three_period.x_0],
                    [game_three_period.y_0],
                    [game_three_period.S_0],
                    [0],
                ]
            ),
            game_three_period,
        ),
        random_generator_uniform.mean,
    ),
)

# Run the simulation with the specified parameters
start_simulation = time.time()

simulation.run_simulation()

end_simulation = time.time()

print("Simulation took {}".format(end_simulation - start_simulation))

print("First Weight:", simulation.negQ.trainable_variables[0])


import argparse
import json
import logging

from nlp_architect.data.cdc_resources.relations.verbocean_relation_extraction import (
    VerboceanRelationExtraction,
)
from nlp_architect.models.cross_doc_coref.system.cdc_utils import load_mentions_vocab_from_files
from nlp_architect.utils import io

logger = logging.getLogger(__name__)

parser = argparse.ArgumentParser(description="Create Verb-Ocean dataset only dump")

parser.add_argument("--vo", type=str, help="Verb Ocean file", required=True)

parser.add_argument("--mentions", type=str, help="dataset mentions", required=True)

parser.add_argument("--output", type=str, help="location were to create dump file", required=True)

args = parser.parse_args()

def vo_dump():
    vo_file = args.vo
    out_file = args.output
    mentions_event_gold_file = [args.mentions]
    vocab = load_mentions_vocab_from_files(mentions_event_gold_file, True)
    vo = VerboceanRelationExtraction.load_verbocean_file(vo_file)
    vo_for_vocab = {}
    for word in vocab:
        if word in vo:
            vo_for_vocab[word] = vo[word]

    logger.info("Found %d words from vocabulary", len(vo_for_vocab.keys()))
    logger.info("Preparing to save refDict output file")

    with open(out_file, "w") as f:
        json.dump(vo_for_vocab, f)
    logger.info("Done saved to-%s", out_file)

if __name__ == "__main__":
    io.validate_existing_filepath(args.mentions)
    io.validate_existing_filepath(args.output)
    io.validate_existing_filepath(args.vo)
    vo_dump()


#!/usr/bin/env python3

"""
Crawler for historical exchange rates. Finds Excel files with currency rates
published at the UAE Central Bank, and then writes them to a database.
It has no arguments, but can be customized via the config.yaml file
in the same directory.
"""

import datetime
import hashlib
import logging
import os
import re
import shutil
import ssl

import pandas
import requests
from bs4 import BeautifulSoup

from modules.crawler import UAExchangeRatesCrawler
from modules.db import Event

class HistoricalUAExchangeRatesCrawler(UAExchangeRatesCrawler):
    __historical_files_directory: str = ""

    def __init__(self, file, updating_event):

        super().__init__(file, updating_event)

        self._init_historical_files_directory()

    def _init_historical_files_directory(self) -> None:

        self.__historical_files_directory = os.path.join(
            self._current_directory, "history"
        )

        try:
            os.makedirs(self.__historical_files_directory, exist_ok=True)
        except OSError:
            pass  # TODO needs to be processed

    def _get_links_to_files(self) -> list | None:
        def is_link_to_excel_file(href):
            return href and re.compile("/media/.*[a-z0-9]\\.xlsx").search(href)

        links = []

        logging.debug("Attempting to find links to Excel files...")

        page_url = "https://www.centralbank.ae/en/forex-eibor/exchange-rates/"
        response = self._get_response_for_request(page_url)

        if response is not None:

            page = BeautifulSoup(response.text, features="html.parser")
            tags = page.find_all("a", href=is_link_to_excel_file)

            for tag in tags:
                links.append(f'https://www.centralbank.ae{tag.get("href")}')

            logging.debug("Search results: %d link(s).", len(links))

        return links

    def _load_currency_rates_from_file(self, link, currency_rates):

        ssl._create_default_https_context = ssl._create_unverified_context

        unknown_currencies = []

        excel_data = pandas.read_excel(link, sheet_name=0, header=2)
        excel_dict = excel_data.to_dict()

        currency_column = excel_dict["Currency"]
        rate_column = excel_dict["Rate"]
        date_column = excel_dict["Date"]

        max_index = len(currency_column) - 1

        for index in range(0, max_index):

            currency_name = currency_column[index]
            currency_code = self.get_currency_code(currency_name)

            if currency_code is None:
                unknown_currencies.append(currency_name)
                continue

            if not self._is_currency_code_allowed(currency_code):
                continue

            rate_date = self.get_datetime_from_date(date_column[index])

            currency_rates.append(
                {
                    "currency_code": currency_code,
                    "import_date": self._current_datetime,
                    "rate_date": rate_date + datetime.timedelta(days=1),
                    "rate": float(rate_column[index]),
                }
            )

        self._unknown_currencies_warning(unknown_currencies)

    def _currency_rates_from_file(self, file_link: str) -> list | None:

        currency_rates = []

        logging.debug("LINK TO PROCESS: %s", file_link)

        file_path = self.__file_path_in_historical_files_directory(file_link)

        if file_path is not None:

            file_hash = self.__file_hash(file_path)

            logging.debug("Downloaded file hash: %s", file_hash)

            historical_file = self._db.historical_file(file_link)

            load = False

            if historical_file is None:

                logging.debug(
                    "The file hasn't been processed before "
                    "(unable to find a previous file hash in the database)."
                )

                load = True

            elif historical_file["hash"] != file_hash:

                logging.debug(
                    "The file has been updated "
                    "since the last processing ({}), "
                    "because previous file hash ({}) "
                    "is not equal to the current one.".format(
                        self.date_with_time_as_string(historical_file["import_date"]),
                        historical_file["hash"],
                    )
                )

                load = True

            else:

                logging.debug(
                    "The file hasn't been updated "
                    "since the last processing (%s), "
                    "because a previous file hash "
                    "is equal to the current one.",
                    self.date_with_time_as_string(historical_file["import_date"]),
                )

            if load:

                self._load_currency_rates_from_file(file_link, currency_rates)

                if historical_file is None:
                    self._db.insert_historical_file(
                        file_link, file_hash, import_date=self._current_datetime
                    )
                else:
                    self._db.update_historical_file(
                        file_link, file_hash, import_date=self._current_datetime
                    )

        return currency_rates

    def run(self):

        ssl_ctx = ssl.create_default_context()
        ssl_ctx.check_hostname = False
        ssl_ctx.verify_mode = ssl.CERT_NONE

        log_title = "import of historical exchange rates"

        self._import_started(log_title)

        links_to_files = self._get_links_to_files()

        if links_to_files is not None:

            changed_rates_number = 0

            for link_to_file in links_to_files:

                currency_rates = self._currency_rates_from_file(link_to_file)

                logging.debug("Crawling results: %d rate(s).", len(currency_rates))

                changed_rates_number += self._process_currency_rates_to_import(
                    currency_rates
                )

            self._db.insert_import_date(self._current_datetime)

            self._log_import_completed(
                title=log_title, changed_rates_number=changed_rates_number, event=Event.HISTORICAL_RATES_LOADING
            )

        else:

            self._log_import_failed(title=log_title)

        self._db.disconnect()

    def __file_path_in_historical_files_directory(self, file_link: str) -> str | None:

        file_name = file_link.split("/")[-1]
        file_path = os.path.join(self.__historical_files_directory, file_name)

        file_is_downloaded = False
        attempt_number = 0

        while not file_is_downloaded:

            if attempt_number == 3:
                break

            attempt_number += 1

            logging.debug("Attempt #%d to download the file...", attempt_number)

            try:

                with requests.get(file_link, stream=True) as response:
                    with open(file_path, "wb") as file:
                        shutil.copyfileobj(response.raw, file)
                        file_is_downloaded = True
            except (requests.exceptions.RequestException, shutil.Error) as exception:
                logging.error(exception)

        if not file_is_downloaded:
            logging.debug("Unable to download the file!")
            file_path = None

        return file_path

    @staticmethod
    def __file_hash(file_path: str):

        md5 = hashlib.md5()

        with open(file_path, "rb") as file:
            md5.update(file.read())

        return md5.hexdigest()

if __name__ == "__main__":
    HistoricalUAExchangeRatesCrawler(
        file=__file__, updating_event=Event.HISTORICAL_RATES_UPDATING
    ).run()


import os
import Utils
import samba_utils
import sys

def bzr_version_summary(path):
    try:
        import bzrlib
    except ImportError:
        return ("BZR-UNKNOWN", {})

    import bzrlib.ui
    bzrlib.ui.ui_factory = bzrlib.ui.make_ui_for_terminal(
        sys.stdin, sys.stdout, sys.stderr)
    from bzrlib import branch, osutils, workingtree
    from bzrlib.plugin import load_plugins
    load_plugins()

    b = branch.Branch.open(path)
    (revno, revid) = b.last_revision_info()
    rev = b.repository.get_revision(revid)

    fields = {
        "BZR_REVISION_ID": revid,
        "BZR_REVNO": revno,
        "COMMIT_DATE": osutils.format_date_with_offset_in_original_timezone(rev.timestamp,
            rev.timezone or 0),
        "COMMIT_TIME": int(rev.timestamp),
        "BZR_BRANCH": rev.properties.get("branch-nick", ""),
        }

    # If possible, retrieve the git sha
    try:
        from bzrlib.plugins.git.object_store import get_object_store
    except ImportError:
        # No git plugin
        ret = "BZR-%d" % revno
    else:
        store = get_object_store(b.repository)
        full_rev = store._lookup_revision_sha1(revid)
        fields["GIT_COMMIT_ABBREV"] = full_rev[:7]
        fields["GIT_COMMIT_FULLREV"] = full_rev
        ret = "GIT-" + fields["GIT_COMMIT_ABBREV"]

    if workingtree.WorkingTree.open(path).has_changes():
        fields["COMMIT_IS_CLEAN"] = 0
        ret += "+"
    else:
        fields["COMMIT_IS_CLEAN"] = 1
    return (ret, fields)

def git_version_summary(path, env=None):
    # Get version from GIT
    if not 'GIT' in env and os.path.exists("/usr/bin/git"):
        # this is useful when doing make dist without configuring
        env.GIT = "/usr/bin/git"

    if not 'GIT' in env:
        return ("GIT-UNKNOWN", {})

    environ = dict(os.environ)
    environ["GIT_DIR"] = '%s/.git' % path
    environ["GIT_WORK_TREE"] = path
    git = Utils.cmd_output(env.GIT + ' show --pretty=format:"%h%n%ct%n%H%n%cd" --stat HEAD', silent=True, env=environ)

    lines = git.splitlines()
    if not lines or len(lines) < 4:
        return ("GIT-UNKNOWN", {})

    fields = {
            "GIT_COMMIT_ABBREV": lines[0],
            "GIT_COMMIT_FULLREV": lines[2],
            "COMMIT_TIME": int(lines[1]),
            "COMMIT_DATE": lines[3],
            }

    ret = "GIT-" + fields["GIT_COMMIT_ABBREV"]

    if env.GIT_LOCAL_CHANGES:
        clean = Utils.cmd_output('%s diff HEAD | wc -l' % env.GIT, silent=True).strip()
        if clean == "0":
            fields["COMMIT_IS_CLEAN"] = 1
        else:
            fields["COMMIT_IS_CLEAN"] = 0
            ret += "+"

    return (ret, fields)

class SambaVersion(object):

    def __init__(self, version_dict, path, env=None):
        '''Determine the version number of samba

See VERSION for the format.  Entries on that file are
also accepted as dictionary entries here
        '''

        self.MAJOR=None
        self.MINOR=None
        self.RELEASE=None
        self.REVISION=None
        self.TP_RELEASE=None
        self.ALPHA_RELEASE=None
        self.PRE_RELEASE=None
        self.RC_RELEASE=None
        self.IS_SNAPSHOT=True
        self.RELEASE_NICKNAME=None
        self.VENDOR_SUFFIX=None
        self.VENDOR_PATCH=None

        for a, b in version_dict.iteritems():
            if a.startswith("SAMBA_VERSION_"):
                setattr(self, a[14:], b)
            else:
                setattr(self, a, b)

        if self.IS_GIT_SNAPSHOT == "yes":
            self.IS_SNAPSHOT=True
        elif self.IS_GIT_SNAPSHOT == "no":
            self.IS_SNAPSHOT=False
        else:
            raise Exception("Unknown value for IS_GIT_SNAPSHOT: %s" % self.IS_GIT_SNAPSHOT)

 ##
 ## start with "3.0.22"
 ##
        self.MAJOR=int(self.MAJOR)
        self.MINOR=int(self.MINOR)
        self.RELEASE=int(self.RELEASE)

        SAMBA_VERSION_STRING = ("%u.%u.%u" % (self.MAJOR, self.MINOR, self.RELEASE))

##
## maybe add "3.0.22a" or "4.0.0tp11" or "4.0.0alpha1" or "3.0.22pre1" or "3.0.22rc1"
## We do not do pre or rc version on patch/letter releases
##
        if self.REVISION is not None:
            SAMBA_VERSION_STRING += self.REVISION
        if self.TP_RELEASE is not None:
            self.TP_RELEASE = int(self.TP_RELEASE)
            SAMBA_VERSION_STRING += "tp%u" % self.TP_RELEASE
        if self.ALPHA_RELEASE is not None:
            self.ALPHA_RELEASE = int(self.ALPHA_RELEASE)
            SAMBA_VERSION_STRING += ("alpha%u" % self.ALPHA_RELEASE)
        if self.PRE_RELEASE is not None:
            self.PRE_RELEASE = int(self.PRE_RELEASE)
            SAMBA_VERSION_STRING += ("pre%u" % self.PRE_RELEASE)
        if self.RC_RELEASE is not None:
            self.RC_RELEASE = int(self.RC_RELEASE)
            SAMBA_VERSION_STRING += ("rc%u" % self.RC_RELEASE)

        if self.IS_SNAPSHOT:
            if os.path.exists(os.path.join(path, ".git")):
                suffix, self.vcs_fields = git_version_summary(path, env=env)
            elif os.path.exists(os.path.join(path, ".bzr")):
                suffix, self.vcs_fields = bzr_version_summary(path)
            else:
                suffix = "UNKNOWN"
                self.vcs_fields = {}
            SAMBA_VERSION_STRING += "-" + suffix
        else:
            self.vcs_fields = {}

        self.OFFICIAL_STRING = SAMBA_VERSION_STRING

        if self.VENDOR_SUFFIX is not None:
            SAMBA_VERSION_STRING += ("-" + self.VENDOR_SUFFIX)
            self.VENDOR_SUFFIX = self.VENDOR_SUFFIX

            if self.VENDOR_PATCH is not None:
                SAMBA_VERSION_STRING += ("-" + self.VENDOR_PATCH)
                self.VENDOR_PATCH = self.VENDOR_PATCH

        self.STRING = SAMBA_VERSION_STRING

        if self.RELEASE_NICKNAME is not None:
            self.STRING_WITH_NICKNAME = "%s (%s)" % (self.STRING, self.RELEASE_NICKNAME)
        else:
            self.STRING_WITH_NICKNAME = self.STRING

    def __str__(self):
        string="/* Autogenerated by waf */\n"
        string+="#define SAMBA_VERSION_MAJOR %u\n" % self.MAJOR
        string+="#define SAMBA_VERSION_MINOR %u\n" % self.MINOR
        string+="#define SAMBA_VERSION_RELEASE %u\n" % self.RELEASE
        if self.REVISION is not None:
            string+="#define SAMBA_VERSION_REVISION %u\n" % self.REVISION

        if self.TP_RELEASE is not None:
            string+="#define SAMBA_VERSION_TP_RELEASE %u\n" % self.TP_RELEASE

        if self.ALPHA_RELEASE is not None:
            string+="#define SAMBA_VERSION_ALPHA_RELEASE %u\n" % self.ALPHA_RELEASE

        if self.PRE_RELEASE is not None:
            string+="#define SAMBA_VERSION_PRE_RELEASE %u\n" % self.PRE_RELEASE

        if self.RC_RELEASE is not None:
            string+="#define SAMBA_VERSION_RC_RELEASE %u\n" % self.RC_RELEASE

        for name in sorted(self.vcs_fields.keys()):
            string+="#define SAMBA_VERSION_%s " % name
            value = self.vcs_fields[name]
            if isinstance(value, basestring):
                string += "\"%s\"" % value
            elif type(value) is int:
                string += "%d" % value
            else:
                raise Exception("Unknown type for %s: %r" % (name, value))
            string += "\n"

        string+="#define SAMBA_VERSION_OFFICIAL_STRING \"" + self.OFFICIAL_STRING + "\"\n"

        if self.VENDOR_SUFFIX is not None:
            string+="#define SAMBA_VERSION_VENDOR_SUFFIX " + self.VENDOR_SUFFIX + "\n"
            if self.VENDOR_PATCH is not None:
                string+="#define SAMBA_VERSION_VENDOR_PATCH " + self.VENDOR_PATCH + "\n"

        if self.RELEASE_NICKNAME is not None:
            string+="#define SAMBA_VERSION_RELEASE_NICKNAME " + self.RELEASE_NICKNAME + "\n"

        # We need to put this #ifdef in to the headers so that vendors can override the version with a function
        string+='''
#ifdef SAMBA_VERSION_VENDOR_FUNCTION
#  define SAMBA_VERSION_STRING SAMBA_VERSION_VENDOR_FUNCTION
#else /* SAMBA_VERSION_VENDOR_FUNCTION */
#  define SAMBA_VERSION_STRING "''' + self.STRING_WITH_NICKNAME + '''"
#endif
'''
        string+="/* Version for mkrelease.sh: \nSAMBA_VERSION_STRING=" + self.STRING_WITH_NICKNAME + "\n */\n"

        return string

def samba_version_file(version_file, path, env=None):
    '''Parse the version information from a VERSION file'''

    f = open(version_file, 'r')
    version_dict = {}
    for line in f:
        line = line.strip()
        if line == '':
            continue
        if line.startswith("#"):
            continue
        try:
            split_line = line.split("=")
            if split_line[1] != "":
                value = split_line[1].strip('"')
                version_dict[split_line[0]] = value
        except:
            print("Failed to parse line %s from %s" % (line, version_file))
            raise

    return SambaVersion(version_dict, path, env=env)

def load_version(env=None):
    '''load samba versions either from ./VERSION or git
    return a version object for detailed breakdown'''
    if not env:
        env = samba_utils.LOAD_ENVIRONMENT()

    version = samba_version_file("./VERSION", ".", env)
    Utils.g_module.VERSION = version.STRING
    return version


import os, time
import pika

time.sleep(10)

########### CONNEXIÓN A RABBIT MQ #######################

HOST = os.environ['RABBITMQ_HOST']
print("rabbit:"+HOST)

connection = pika.BlockingConnection(
    pika.ConnectionParameters(host=HOST))
channel = connection.channel()

#El consumidor utiliza el exchange 'cartero'
channel.exchange_declare(exchange='cartero', exchange_type='topic', durable=True)

#Se crea un cola temporaria exclusiva para este consumidor (búzon de correos)
result = channel.queue_declare(queue="traductor", exclusive=True, durable=True)
queue_name = result.method.queue

#La cola se asigna a un 'exchange'
channel.queue_bind(exchange='cartero', queue=queue_name, routing_key="traductor")

##########################################################

########## ESPERA Y HACE ALGO CUANDO RECIBE UN MENSAJE ####

print(' [*] Waiting for messages. To exit press CTRL+C')

#----------------------------------
import requests

def Traduccion(source, target, text):
	parametros = {'sl': source, 'tl': target, 'q': text}
	cabeceras = {"Charset":"UTF-8","User-Agent":"AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1"}
	url = "https://translate.google.com/translate_a/single?client=at&dt=t&dt=ld&dt=qca&dt=rm&dt=bd&dj=1&hl=es-ES&ie=UTF-8&oe=UTF-8&inputm=2&otf=2&iid=1dd3b944-fa62-4b55-b330-74909a99969e"
	response = requests.post(url, data=parametros, headers=cabeceras)
	if response.status_code == 200:
		for x in response.json()['sentences']:
			return x['trans']
	else:
		return "Ocurrió un error"

#---------------------------------

def callback(ch, method, properties, body):
	print(body.decode("UTF-8"))
	arguments = body.decode("UTF-8").split(" ")

	idioma = arguments[1]
	frase = " ".join(arguments[2:])
	result = Traduccion("es", idioma, frase)

	########## PUBLICA EL RESULTADO COMO EVENTO EN RABBITMQ ##########
	print("send a new message to rabbitmq: "+result)
	channel.basic_publish(exchange='cartero',routing_key="discord_writer",body=result)

channel.basic_consume(
    queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()

#######################