text:
import ConfigParser
import os
import json
import shutil
import logging
import datetime
import argparse
import hashlib
import time
import string
import re

join = os.path.join
normpath = os.path.normpath
abspath = os.path.abspath

from flask import Flask, make_response, request, abort, jsonify
from flask.ext.httpauth import HTTPBasicAuth
from flask.ext.restful import Resource, Api
from flask.ext.mail import Mail, Message
from werkzeug import secure_filename
from passlib.hash import sha256_crypt
import passwordmeter

__title__ = 'PyBOX'

# HTTP STATUS CODES
HTTP_OK = 200
HTTP_CREATED = 201
HTTP_ACCEPTED = 202
HTTP_BAD_REQUEST = 400
HTTP_UNAUTHORIZED = 401
HTTP_FORBIDDEN = 403
HTTP_NOT_FOUND = 404
HTTP_CONFLICT = 409
#HTTP 204 No Content: The server successfully processed the request, but is not
#returning any content. Usually used as a response to a successful delete request.
HTTP_DELETED = 204

FILE_ROOT = 'filestorage'

URL_PREFIX = '/API/V1'
SERVER_DIRECTORY = os.path.dirname(__file__)
# Users login data are stored in a json file in the server
USERDATA_FILENAME = 'userdata.json'
PASSWORD_RECOVERY_EMAIL_TEMPLATE_FILE_PATH = os.path.join(SERVER_DIRECTORY,
                                                          'password_recovery_email_template.txt')
SIGNUP_EMAIL_TEMPLATE_FILE_PATH = os.path.join(SERVER_DIRECTORY,
                                               'signup_email_template.txt')

USER_ACTIVATION_TIMEOUT = 60 * 60 * 24 * 3 * 10000 # expires after 3 days
USER_RECOVERPASS_TIMEOUT = 60 * 60 * 24 * 2 * 10000  # expires after 2 days (arbitrarily)

# json/dict key to access to the user directory snapshot:
SNAPSHOT = 'files'
SHARED_FILES ='shared_files'
LAST_SERVER_TIMESTAMP = 'server_timestamp'
PWD = 'password'
USER_CREATION_TIME = 'creation_timestamp'
DEFAULT_USER_DIRS = ('Misc', 'Music', 'Photos', 'Projects', 'Work')
USER_IS_ACTIVE = 'active'
USER_CREATION_DATA = 'activation_data'

UNWANTED_PASS = 'words'

class ServerError(Exception):
    pass

class ServerConfigurationError(ServerError):
    pass

class ServerInternalError(ServerError):
    """
    Custom exception to raise programming errors
    (i.e. when unexpected conditions are found).
    """
    pass

# Logging configuration
# =====================
LOG_FILENAME = 'log/server.log'

# create logger
logger = logging.getLogger('Server log')
logger.setLevel(logging.DEBUG)

# create console handler
console_handler = logging.StreamHandler()
logger.addHandler(console_handler)

# First log message
launched_or_imported = {True: 'launched', False: 'imported'}[__name__ == '__main__']
logger.info('-' * 60)
logger.info('Server {} at {}'.format(launched_or_imported, datetime.datetime.now().isoformat(' ')))
logger.info('-' * 60)

# create formatter and add it to the handler
console_formatter = logging.Formatter('%(asctime)s  %(levelname)-8s  %(message)s', '%Y-%m-%d %H:%M:%S')
console_handler.setFormatter(console_formatter)

# Server initialization
# =====================
userdata = {}

app = Flask(__name__)
app.testing = __name__ != '__main__'  # Reasonable assumption?
# if True, you can see the exception traceback, suppress the sending of emails, etc.
EMAIL_SETTINGS_FILEPATH = join(os.path.dirname(__file__),
                               ('email_settings.ini', 'email_settings.ini.example')[app.testing])

# A regular expression to check if an email address is valid or not.
# WARNING: it seems a not 100%-exhaustive email address validation.
# source: http://www.regular-expressions.info/email.html (modified)

EMAIL_REG_OBJ = re.compile(r'^[A-Z0-9]'  # the first char must be alphanumeric (no dots etc...)
                           r'[A-Z0-9._%+-]+'  # allowed characters in the "local part"
                           # NB: many email providers allow letters, numbers, and '.', '-' and '_' only.
                           # GMail even allows letters, numbers and dots only (no '-' nor underscores).
                           r'[A-Z0-9_-]'  # no dots before the '@'
                           r'@'
                           r'[A-Z0-9.-]+'  # domain part before the last dot ('.' and '-' allowed too)
                           r'\.[A-Z]{2,4}$',  # domain extension: 2, 3 or 4 letters
                           re.IGNORECASE | re.VERBOSE)

api = Api(app)
auth = HTTPBasicAuth()

def validate_email(address):
    """
    Validate an email address according to http://www.regular-expressions.info/email.html.
    In addition, at most one '.' before the '@' and no '..' in the domain part are allowed.
    :param address: str
    :return: bool
    """
    if not re.search(EMAIL_REG_OBJ, address) or '..' in address:
        return False
    else:
        return True

def update_passwordmeter_terms(terms_file):
    """
    Added costume terms list into passwordmeter from words file
    :return:
    """
    costume_password = set()
    try:
        with open(terms_file, 'rb') as terms_file:
            for term in terms_file:
                costume_password.add(term)
    except IOError:
        logger.info('Impossible to load file ! loaded default setting.')
    else:
        passwordmeter.common10k = passwordmeter.common10k.union(costume_password)
    finally:
        del costume_password

def _read_file(filename):
    """
    This function reads and returns the content of the file.
    """
    with open(filename, 'rb') as f:
        content = f.read()
    return content

def check_path(path, username):
    """
    Check that a path don't fall in other user directories or upper.
    Examples:

    >>> check_path('Photos/myphoto.jpg', 'pippo')
    True
    >>> check_path('Photos/../../ciao.txt', 'paperino')
    False
    """
    path = os.path.abspath(join(FILE_ROOT, username, path))
    root = os.path.abspath(join(FILE_ROOT, username))
    if root in path:
        return True
    return False

def userpath2serverpath(username, path=''):
    """
    Given an username and its relative path, return the
    corresponding path in the server. If the path is empty,
    return the user path directory in the server.
    :param username: str
    :param path: str
    :return: str
    """
    return os.path.realpath(os.path.join(FILE_ROOT, username, path))

def now_timestamp():
    """
    Return the current server timestamp as an int.
    :return: long
    """
    return long(time.time()*10000)

def file_timestamp(filepath):
    """
    Return the long of last modification timestamp of <filepath> (i.e. long(os.path.getmtime(filepath))).

    :param filepath: str
    :return: long
    """

    return long(os.path.getmtime(filepath)*10000)

def _encrypt_password(password):
    """
    Return the password encrypted as a string.
    :rtype : str
    """
    return sha256_crypt.encrypt(password)

def init_root_structure():
    """
    Create the file root directory if needed.
    :return: int
    """
    if not os.path.isdir(FILE_ROOT):
        os.mkdir(FILE_ROOT)
        return 1
    else:
        return 0

def init_user_directory(username, default_dirs=DEFAULT_USER_DIRS):
    """
    Create the default user directory.
    :param username: str
    :param default_dirs: dict
    """
    dirpath = join(FILE_ROOT, username)
    if os.path.isdir(dirpath):
        shutil.rmtree(dirpath)
        logger.info('"{}" directory removed'.format(dirpath))
    os.makedirs(dirpath)

    welcome_file = join(dirpath, 'WELCOME')
    with open(welcome_file, 'wb') as fp:
        fp.write('Welcome to %s, %s!\n' % (__title__, username))

    for dirname in default_dirs:
        subdirpath = join(dirpath, dirname)
        filepath = join(subdirpath, '{}.txt'.format(dirname))
        os.mkdir(subdirpath)
        # Create a default file for each default directory
        # beacuse wee need files to see the directories.
        with open(filepath, 'wb') as fp:
            fp.write('{} {}\n'.format(username, dirname))
    logger.info('{} created'.format(dirpath))
    return compute_dir_state(dirpath)

def load_userdata():
    data = {}
    try:
        with open(USERDATA_FILENAME, 'rb') as fp:
            data = json.load(fp, 'utf-8')
    except IOError:
        # If the user data file does not exists, don't raise an exception.
        # (the file will be created with the first user creation)
        pass
    logger.debug('Registered user(s): {}'.format(', '.join(data.keys())))
    logger.info('{:,} registered user(s) found'.format(len(data)))
    return data

def save_userdata():
    """
    Save module level <userdata> dict to disk as json.
    :return: None
    """
    with open(USERDATA_FILENAME, 'wb') as fp:
        json.dump(userdata, fp, 'utf-8', indent=4)
    logger.info('Saved {:,} users'.format(len(userdata)))

def reset_userdata():
    """
    Clear userdata dictionary.
    """
    userdata.clear()

def _is_shared_with_others(path, username):
    """
    Check if the path belong to a shared folder
    """
    shared_path = path.split('/')[0]

    if userdata[username]['shared_with_others'].has_key(shared_path):
        return True
    return False

@auth.verify_password
def verify_password(username, password):
    """
    We redefine this function to check password with the encrypted one.
    """
    if not username:
        # Warning/info?
        return False
    single_user_data = userdata.get(username)
    if single_user_data:
        stored_pw = single_user_data.get(PWD)
        assert stored_pw is not None, 'Server error: user data must contain a password!'
        res = sha256_crypt.verify(password, stored_pw)
    else:
        logger.info('User "{}" does not exist'.format(username))
        res = False
    return res

def activate_user(username, encrypted_password):
    """
    Handle the activation of an existing user(with flag active: True).
    """
    logger.debug('Activating user...')

    temp = init_user_directory(username)
    last_server_timestamp, dir_snapshot = temp[LAST_SERVER_TIMESTAMP], temp[SNAPSHOT]

    single_user_data = {USER_CREATION_TIME: now_timestamp(),
                        PWD: encrypted_password,
                        LAST_SERVER_TIMESTAMP: last_server_timestamp,
                        SNAPSHOT: dir_snapshot,
                        USER_IS_ACTIVE: True,
                        'shared_with_me': {},
                        'shared_with_others': {},
                        'shared_files': {}
                        }
    userdata[username] = single_user_data
    save_userdata()
    response = 'User "{}" activated.\n'.format(username), HTTP_OK

    logger.debug(response)
    return response

def create_user(username, password, activation_code):
    """
    Handle the creation of a new user(with flag active: False).
    User's password is encrypted here.
    """
    logger.debug('Creating user...')
    if username and password:
        enc_pass = _encrypt_password(password)
        single_user_data = {USER_IS_ACTIVE: False,
                            PWD: enc_pass,
                            USER_CREATION_DATA: {'creation_timestamp': now_timestamp(),
                                                 'activation_code': activation_code}
                            }
        userdata[username] = single_user_data
        save_userdata()
        response = 'User activation email sent to {}'.format(username), HTTP_CREATED
    else:
        raise ServerInternalError('Unexpected error: username and password must not be empty here!!!\n'
                                  'username: "%"" - password: "%"' % (username, password))

    logger.debug(response)
    return response

def configure_email():
    """
    Configure Flask Mail from the email_settings.ini in place. Return a flask.ext.mail.Mail instance.
    """
    if not os.path.exists(EMAIL_SETTINGS_FILEPATH):
        # ConfigParser.ConfigParser.read doesn't tell anything if the email configuration file is not found.
        raise ServerConfigurationError('Email configuration file "{}" not found!'.format(EMAIL_SETTINGS_FILEPATH))

    # Relations between Flask configuration keys and settings file fields.
    keys_tuples = [
        ('MAIL_SERVER', 'smtp_address'),  # the address of the smtp server
        ('MAIL_PORT', 'smtp_port'),  # the port of the smtp server
        ('MAIL_USERNAME', 'smtp_username'),  # the username of the smtp server (if required)
        ('MAIL_PASSWORD', 'smtp_password'),  # the password of the smtp server (if required)
    ]

    cfg = ConfigParser.ConfigParser()
    cfg.read(EMAIL_SETTINGS_FILEPATH)
    for flask_key, file_key in keys_tuples:
        value = cfg.get('email', file_key)
        if flask_key == 'MAIL_PORT':
            value = int(value)
        app.config[flask_key] = value

    return Mail(app)

def send_email(subject, sender, recipients, text_body):
    """
    Sent an email and return the Message instance of sent email.

    :param subject: str
    :param sender: str
    :param recipients: list
    :param text_body: str
    :return: Message
    """
    msg = Message(subject, sender=sender, recipients=recipients)
    msg.body = text_body
    mail.send(msg)
    return msg

class Users(Resource):
    @staticmethod
    def _clean_inactive_users():
        """
        Remove expired inactive users (users whose activation time is expired)
        and return a list of them.
        :return: list
        """
        to_remove = [username for (username, data) in userdata.iteritems()
                     if userdata[username][USER_IS_ACTIVE] is False and
                     now_timestamp() - data[USER_CREATION_DATA][USER_CREATION_TIME] >
                     USER_ACTIVATION_TIMEOUT]
        for username in to_remove:
            userdata.pop(username)
        return to_remove

    @auth.login_required
    def get(self, username):
        """
        Show logged user details.
        """
        logged = auth.username()
        if username == logged:
            user_data = userdata[username]
            creation_timestamp = user_data.get(USER_CREATION_TIME)
            if creation_timestamp:
                time_str = time.strftime('%Y-%m-%d at %H:%M:%S', time.localtime(creation_timestamp/10000.0))
            else:
                time_str = '<unknown time>'
            # TODO: return json?
            response = 'You joined on {}.'.format(time_str), HTTP_OK
            return response
        else:
            if app.debug:
                if username == '__all__':
                    # Easter egg to see a list of active and pending (inactive) users.
                    logger.warn('WARNING: showing the list of all users (debug mode)!!!')
                    if userdata:
                        active_users = [username for username in userdata if userdata[username][USER_IS_ACTIVE]]
                        active_users_str = ', '.join(active_users)
                        inactive_users = [username for username in userdata if not userdata[username][USER_IS_ACTIVE]]
                        inactive_users_str = ', '.join(inactive_users)
                    else:
                        reg_users_str = 'neither registered nor pending users'

                    response = 'Activated users: {}. Inactive users: {}'.format(active_users_str, inactive_users_str), HTTP_OK
                else:
                    logger.warn('WARNING: showing {}\'s info (debug mode)!!!'.format(username))
                    if username in userdata:
                        user_data = userdata[username]
                        creation_timestamp = user_data.get(USER_CREATION_TIME)
                        if creation_timestamp:
                            time_str = time.strftime('%Y-%m-%d at %H:%M:%S', time.localtime(creation_timestamp))
                        else:
                            time_str = '<unknown time>'
                        response = 'User {} joined on {}.'.format(username, time_str), HTTP_OK
                    else:
                        response = 'The user {} does not exist.'.format(username), HTTP_NOT_FOUND
                return response

            abort(HTTP_FORBIDDEN)

    def post(self, username):
        """
        A not-logged user is asking to register himself.
        NB: username must be a valid email address.
        """
        if not validate_email(username):
            return 'Error: username must be a valid email address!', HTTP_BAD_REQUEST

        password = request.form['password']
        if not password:
            return 'Error: the password mustn\'t be empty', HTTP_BAD_REQUEST

        strength, improvements = passwordmeter.test(password)
        if strength <= 0.5:
            return improvements, HTTP_FORBIDDEN
        activation_code = os.urandom(16).encode('hex')

        if username in userdata:
            # If an user is pending for activation, it can't be another one with the same name
            #  asking for registration
            return 'Error: username "{}" already exists!\n'.format(username), HTTP_CONFLICT

        # Composing email
        subject = '[{}] Confirm your email address'.format(__title__)
        sender = 'donotreply@{}.com'.format(__title__)
        recipients = [username]
        text_body_template = string.Template(_read_file(SIGNUP_EMAIL_TEMPLATE_FILE_PATH))
        values = dict(code=activation_code,
                      email=username,
                      appname=__title__,
                      )
        text_body = text_body_template.substitute(values)

        send_email(subject, sender, recipients, text_body)

        return create_user(username, password, activation_code)

    def put(self, username):
        """
        Activate user using activation code sent by email, or reset its password.
        """
        # create a list of all usernames with flag active: False

        # Pending users cleanup
        expired_pending_users = self._clean_inactive_users()
        logger.info('Expired pending users: {}'.format(expired_pending_users))

        if username in userdata:
            if userdata[username][USER_IS_ACTIVE] is True:
                # User active -> Password recovery/reset
                try:
                    new_password = request.form[PWD]
                except KeyError:
                    abort(HTTP_BAD_REQUEST)

                strength, improvements = passwordmeter.test(new_password)
                if strength <= 0.5:
                    return improvements, HTTP_FORBIDDEN

                request_recoverpass_code = request.form['recoverpass_code']
                recoverpass_stuff = userdata[username].get('recoverpass_data')

                if recoverpass_stuff:
                    recoverpass_code = recoverpass_stuff['recoverpass_code']
                    recoverpass_timestamp = recoverpass_stuff['timestamp']
                    if request_recoverpass_code == recoverpass_code and \
                            (now_timestamp() - recoverpass_timestamp < USER_RECOVERPASS_TIMEOUT):
                        userdata[username][PWD] = new_password
                        enc_pass = _encrypt_password(new_password)
                        userdata[username][PWD] = enc_pass
                        userdata[username].pop('recoverpass_data')
                        return 'Password changed succesfully', HTTP_OK
                # NB: old generated tokens are refused, but, currently, they are not removed from userdata.
                return 'Invalid code', HTTP_NOT_FOUND
            else:
                # User inactive -> activation
                activation_code = request.form['activation_code']
                logger.debug('Got activation code: {}'.format(activation_code))

                user_data = userdata[username]
                logger.debug('Creating user {}'.format(username))
                if activation_code == user_data[USER_CREATION_DATA]['activation_code']:
                    # Actually activate user
                    encrypted_password = user_data[PWD]
                    return activate_user(username, encrypted_password)
                else:
                    abort(HTTP_NOT_FOUND)
        else:
            # Not-existing user --> 404 (OR create it in debug mode with a backdoor)
            #### DEBUG-MODE BACKDOOR ####
            # Shortcut to create an user skipping the email confirmation (valid in debug mode only!).
            if app.debug and activation_code == 'BACKDOOR':
                password = 'debug-password'
                logger.warn('WARNING: Creating user "{}" (password="{}") '
                             'without email confirmation via backdoor!!!'.format(username, password))
                return create_user(username, password, activation_code)
            #### DEBUG-MODE BACKDOOR ####

            return 'Error: username not found!\n', HTTP_NOT_FOUND

    @auth.login_required
    def delete(self, username):
        """
        Delete all logged user's files and data. Remove also inactive users.
        The same user won't more log in, but it can be recreated with the signup procedure.
        """
        logged = auth.username()

        if username != logged:
            # I mustn't delete other users!
            abort(HTTP_FORBIDDEN)

        if userdata[username][USER_IS_ACTIVE]:
            # Remove also the user's folder
            shutil.rmtree(userpath2serverpath(username))

        userdata.pop(username)
        save_userdata()
        return 'User "{}" removed.\n'.format(username), HTTP_OK

class UsersRecoverPassword(Resource):
    """
    This class handles the recovering of a lost user's password by changing it.

    Use case: the user has forgotten its password and wants to recover it.
    NB: recover the old password is not even possible since it's stored encrypted.
    """
    def post(self, username):
        """
        Handle the request for change the user's password
        by sending a 'recoverpass' token to its email address.
        """
        recoverpass_code = os.urandom(16).encode('hex')

        # The password reset must be called from an active or inactive user
        if username not in userdata:
            abort(HTTP_NOT_FOUND)

        # Composing email
        subject = '[{}] Password recovery'.format(__title__)
        sender = 'donotreply@{}.com'.format(__title__)
        recipients = [username]
        text_body_template = string.Template(_read_file(PASSWORD_RECOVERY_EMAIL_TEMPLATE_FILE_PATH))
        values = dict(code=recoverpass_code,
                      email=username,
                      appname=__title__,
                      )
        text_body = text_body_template.substitute(values)

        send_email(subject, sender, recipients, text_body)

        if userdata[username][USER_IS_ACTIVE] is True:
            # create or update 'recoverpass_data' key.
            userdata[username]['recoverpass_data'] = {
                'recoverpass_code': recoverpass_code,
                'timestamp': now_timestamp()
                }
            save_userdata()

        elif userdata[username][USER_IS_ACTIVE] is False:
            userdata[username][USER_CREATION_DATA] = {'creation_timestamp': now_timestamp(),
                                                      'activation_code': recoverpass_code}
            save_userdata()
        # the else case is already covered in the first if

        return 'Reset email sent to {}'.format(username), HTTP_ACCEPTED

class Actions(Resource):
    @auth.login_required
    def post(self, cmd):
        username = auth.username()
        methods = {'delete': self._delete,
                   'copy': self._copy,
                   'move': self._move,
                   }
        try:
            resp = methods[cmd](username)
        except KeyError:
            abort(HTTP_NOT_FOUND)
        else:
            save_userdata()
            return resp

    def _delete(self, username):
        """
        Delete a file for a given <filepath>, and return the current server timestamp in a json.
        json format: {LAST_SERVER_TIMESTAMP: int}
        """
        filepath = request.form['filepath']

        if not check_path(filepath, username):
            abort(HTTP_FORBIDDEN)

        abspath = os.path.abspath(join(FILE_ROOT, username, filepath))

        try:
            os.remove(abspath)
        except OSError:
            # This error raises when the file is missing
            abort(HTTP_NOT_FOUND)
        self._clear_dirs(os.path.dirname(abspath), username)

        # file deleted, last_server_timestamp is set to current timestamp
        last_server_timestamp = now_timestamp()
        userdata[username][LAST_SERVER_TIMESTAMP] = last_server_timestamp
        userdata[username]['files'].pop(normpath(filepath))

        if _is_shared_with_others(filepath, username):
            auto_remove_share = False
            shared_path = filepath.split('/')[0]
            # check if must be removed the share
            shared_abspath = os.path.abspath(join(FILE_ROOT, username, shared_path))

            if not os.path.exists(shared_abspath):
                # maybe difficult to understand this check:
                # if share_abspath is a file then the previous code has deleted it so it must be removed automatically
                # by the share
                #
                # if share_abspath is a folder then we have 2 case:
                # if the folder exist then it means that it isn't empty, otherwise the _clear_dirs function would have
                # deleted it
                # if the folder doesn't exists then it must be removed from share
                auto_remove_share = True

            for user in userdata[username]['shared_with_others'][shared_path]:
                res = 'shared/{0}/{1}'.format(username, filepath)
                userdata[user]['shared_files'].pop(res)
                if auto_remove_share:
                    userdata[user]['shared_with_me'][username].remove(shared_path)

            if auto_remove_share:
                userdata[username]['shared_with_others'].pop(shared_path)

        save_userdata()
        return jsonify({LAST_SERVER_TIMESTAMP: last_server_timestamp})

    def _copy(self, username):
        """
        Copy a file from a given source path to a destination path and return the current server timestamp
        in a json file.jso
        userdata[username]n format: {LAST_SERVER_TIMESTAMP: int}
        """

        src = request.form['src']
        dst = request.form['dst']
        server_src = userpath2serverpath(username, src)
        server_dst = userpath2serverpath(username, dst)

        if not (check_path(src, username) or check_path(dst, username)):
            abort(HTTP_FORBIDDEN)

        if os.path.isfile(server_src):
            if not os.path.exists(os.path.dirname(server_dst)):
                os.makedirs(os.path.dirname(server_dst))
            shutil.copy(server_src, server_dst)
        else:
            abort(HTTP_NOT_FOUND)

        last_server_timestamp = file_timestamp(server_dst)

        _, md5 = userdata[username]['files'][normpath(src)]
        userdata[username][LAST_SERVER_TIMESTAMP] = last_server_timestamp
        userdata[username]['files'][normpath(dst)] = [last_server_timestamp, md5]

        # if path is a shared path then track it in all users that have that share
        if _is_shared_with_others(normpath(dst), username):
            shared_path = normpath(dst).split('/')[0]
            for user in userdata[username]['shared_with_others'][shared_path]:
                res = 'shared/{0}/{1}'.format(username, normpath(dst))
                userdata[user]['shared_files'][res] = [last_server_timestamp, md5]

        save_userdata()
        return jsonify({LAST_SERVER_TIMESTAMP: last_server_timestamp})

    def _move(self, username):
        """
        Move a file from a given source path to a destination path, and return the current server timestamp in a json.
        json format: {LAST_SERVER_TIMESTAMP: int}
        """
        src = request.form['src']
        dst = request.form['dst']
        server_src = userpath2serverpath(username, src)
        server_dst = userpath2serverpath(username, dst)

        if not (check_path(src, username) or check_path(dst, username)):
            abort(HTTP_FORBIDDEN)

        if os.path.isfile(server_src):
            if not os.path.exists(os.path.dirname(server_dst)):
                os.makedirs(os.path.dirname(server_dst))
            shutil.move(server_src, server_dst)
        else:
            abort(HTTP_NOT_FOUND)
        self._clear_dirs(os.path.dirname(server_src), username)

        last_server_timestamp = now_timestamp()

        _, md5 = userdata[username]['files'][normpath(src)]
        userdata[username][LAST_SERVER_TIMESTAMP] = last_server_timestamp
        userdata[username]['files'].pop(normpath(src))
        userdata[username]['files'][normpath(dst)] = [last_server_timestamp, md5]

        # if path is a shared path then track it in all users that have that share
        if _is_shared_with_others(normpath(dst), username):
            shared_path = normpath(dst).split('/')[0]
            for user in userdata[username]['shared_with_others'][shared_path]:
                res = 'shared/{0}/{1}'.format(username, normpath(dst))
                userdata[user]['shared_files'][res] = [last_server_timestamp, md5]

        if _is_shared_with_others(normpath(src), username):
            shared_path = normpath(src).split('/')[0]
            for user in userdata[username]['shared_with_others'][shared_path]:
                res = 'shared/{0}/{1}'.format(username, normpath(src))
                userdata[user]['shared_files'].pop(res)

        save_userdata()
        return jsonify({LAST_SERVER_TIMESTAMP: last_server_timestamp})

    def _clear_dirs(self, path, root):
        """
        Recursively removes all the empty directories that exists after the remotion of a file
        """

        path_to_clear, clean = os.path.split(path)
        path_to_storage, storage = os.path.split(path_to_clear)
        if clean == root and storage == FILE_ROOT:
            return
        try:
            os.rmdir(path)
        except OSError:
            return
        self._clear_dirs(path_to_clear, root)

def calculate_file_md5(fp, chunk_len=2 ** 16):
    """
    Return the md5 digest of the file content of file_path as a string
    with only hexadecimal digits.
    :fp: file (an open file object)
    :chunk_len: int (number of file bytes read per cycle - default = 2^16)
    """
    h = hashlib.md5()
    while True:
        chunk = fp.read(chunk_len)
        if chunk:
            h.update(chunk)
        else:
            break
    res = h.hexdigest()
    fp.seek(0)
    return res

def compute_dir_state(root_path):  # TODO: make function accepting just an username instead of an user root_path.
    """
    Walk on root_path returning the directory snapshot in a dict (dict keys are identified by this 2 constants:
    LAST_SERVER_TIMESTAMP and SNAPSHOT)

    :param root_path: str
    :return: dict.
    """
    snapshot = {}
    last_timestamp = now_timestamp()
    for dirpath, dirs, files in os.walk(root_path):
        for filename in files:
            filepath = join(dirpath, filename)

            # Open file and calculate md5.
            try:
                with open(filepath, 'rb') as fp:
                    md5 = calculate_file_md5(fp)
            except OSError as err:
                logger.warn('calculate_file_md5("{}") --> {}'.format(filepath, err))
            else:
                snapshot[filepath[len(root_path) + 1:]] = [last_timestamp, md5]
    state = {LAST_SERVER_TIMESTAMP: last_timestamp,
             SNAPSHOT: snapshot}
    return state

class Shares(Resource):
    """
    Folder sharing handling class.
    """
    @auth.login_required
    def post(self, root_path, username):
        """API function: creates the share"""

        owner = auth.username()
        # Check if the path is in the owner root
        if not check_path(root_path, owner):
            abort(HTTP_FORBIDDEN)

        # Check if the path exists
        path = os.path.abspath(join(FILE_ROOT, owner, root_path))
        if not os.path.exists(path):
            abort(HTTP_NOT_FOUND)

        # Check if the path is sharable
        if not self._is_sharable(root_path, owner):
            abort(HTTP_FORBIDDEN)

        # create the share
        self._share(root_path, username, owner)
        save_userdata()

        return HTTP_OK

    @auth.login_required
    def delete(self, root_path, username=''):
        """API Function: delete the share"""

        owner = auth.username()
        if not self._is_shared(root_path, owner):
            abort(HTTP_NOT_FOUND)

        if username == '':
            users = userdata[owner]['shared_with_others'][root_path]
            for user in users:
                self._remove_share_from_user(root_path, user, owner)
                save_userdata()
            return HTTP_DELETED

        if username in userdata[owner]['shared_with_others'][root_path]:
            self._remove_share_from_user(root_path, username, owner)
            save_userdata()
            return HTTP_DELETED

        abort(HTTP_NOT_FOUND)

    def _remove_share_from_user(self, root_path, username, owner):
        """Removes the share manipulating the userdata"""

        path = os.path.abspath(join(FILE_ROOT, owner, root_path))
        file_root_abs_path = os.path.abspath(FILE_ROOT)
        if os.path.isdir(path):
            for root, dirs, files in os.walk(path):
                for f in files:
                    temp_path = string.replace(root, join(file_root_abs_path, owner), '')
                    res = 'shared/{0}/{1}'.format(owner, join(temp_path[1:], f))
                    userdata[username]['shared_files'].pop(res)

            userdata[username]['shared_with_me'][owner].remove(root_path)
            userdata[owner]['shared_with_others'][root_path].remove(username)
        else:  # it's a single file
            res = 'shared/{0}/{1}'.format(owner, root_path)
            userdata[username]['shared_files'].pop(res)
            userdata[username]['shared_with_me'][owner].remove(root_path)
            userdata[owner]['shared_with_others'][root_path].remove(username)

    def _is_shared(self, path, owner):
        """Check if the path is a valid shared path"""

        if path in userdata[owner]['shared_with_others']:
            return True
        return False

    def _share(self, path, username, owner):
        """Creates the share manipulating the userdata"""

        if not (owner in userdata[username]['shared_with_me']):
            userdata[username]['shared_with_me'][owner] = []

        if not (path in userdata[owner]['shared_with_others']):
            userdata[owner]['shared_with_others'][path] = []

        # check if the share already exists
        if (path in userdata[username]['shared_with_me'][owner]) or (username in userdata[owner]['shared_with_others'][path]):
            abort(HTTP_CONFLICT)
        userdata[username]['shared_with_me'][owner].append(path)
        userdata[owner]['shared_with_others'][path].append(username)

        # track the shared files into userdata
        abs_path = os.path.abspath(join(FILE_ROOT, owner, path))
        file_root_abs_path = os.path.abspath(FILE_ROOT)
        if os.path.isdir(abs_path):
            for root, dirs, files in os.walk(abs_path):
                for f in files:
                    temp_path = string.replace(root, join(file_root_abs_path, owner), '')
                    userdata[username]['shared_files']['shared/{0}/{1}'.format(owner, join(temp_path[1:], f))] = userdata[owner]['files'][join(temp_path[1:], f)]
        else:
            userdata[username]['shared_files']['shared/{0}/{1}'.format(owner, path)] = userdata[owner]['files'][path]

    def _is_sharable(self, path, owner):
        """
        Checks if the file or folder is located in the owner main root path.
        """
        root_path = os.path.abspath(join(FILE_ROOT, owner))
        sharing_path = os.path.abspath(join(FILE_ROOT, owner, path))
        if os.path.split(sharing_path)[0] == root_path:
            return True
        return False

class Files(Resource):
    """
    Class that handle files as web resources.
    """

    @auth.login_required
    def get(self, path=''):
        """
        Download an authenticated user file from server, if <path> is not empty,
        otherwise get a server snapshot of user directory.
        <path> is the path relative to the user local directory.
        :param path: str
        """
        logger.debug('Files.get({})'.format(repr(path)))
        username = auth.username()

        if path:
            if self._is_shared_with_me(path, username):
                _, owner, file_path = path.split('/', 2)
                user_rootpath = join(FILE_ROOT, owner)
                dirname = join(user_rootpath, os.path.dirname(file_path))
                fp = file_path

            else:
                if not check_path(path, username):
                    abort(HTTP_FORBIDDEN)
                user_rootpath = join(FILE_ROOT, username)
                dirname = join(user_rootpath, os.path.dirname(path))
                fp = path

            # Download the file specified by <path>.
            if not os.path.exists(dirname):
                abort(HTTP_NOT_FOUND)
            s_filename = secure_filename(os.path.split(path)[-1])

            try:
                response = make_response(_read_file(join(user_rootpath, fp)))
            except IOError:
                response = 'Error: file {} not found.\n'.format(path), HTTP_NOT_FOUND
            else:
                response.headers['Content-Disposition'] = 'attachment; filename=%s' % s_filename
        else:
            # If path is not given, return the snapshot of user directory.
            user_rootpath = join(FILE_ROOT, username)
            logger.debug('launch snapshot of {}...'.format(repr(user_rootpath)))
            snapshot = userdata[username][SNAPSHOT]
            logger.info('snapshot returned {:,} files'.format(len(snapshot)))
            last_server_timestamp = userdata[username][LAST_SERVER_TIMESTAMP]
            shared_files = userdata[username][SHARED_FILES]
            response = jsonify({LAST_SERVER_TIMESTAMP: last_server_timestamp,
                                SNAPSHOT: snapshot,
                                SHARED_FILES: shared_files})
        logger.debug(response)
        return response

    def _is_shared_with_me(self, path, username):
        """Check if the path belong to a shared path"""

        if path.split('/')[0] == 'shared':
            _, owner, resource = path.split('/', 2)

            resource = resource.split('/')[0]

            if os.path.dirname(resource) in userdata[username]['shared_with_me'].get(owner) or resource in userdata[username]['shared_with_me'].get(owner):
                return True
        return False

    def _update_shared_files(self, path, username, timestamp, md5):
        """Track the path in all users that have that share"""

        shared_path = path.split('/')[0]
        for user in userdata[username]['shared_with_others'][shared_path]:
            res = 'shared/{0}/{1}'.format(username, path)
            userdata[user]['shared_files'][res] = [timestamp, md5]

    def _get_dirname_filename(self, path):
        """
        Return dirname(directory name) and filename(file name) for a given path to complete
        post and put methods
        """
        username = auth.username()
        dirname = os.path.dirname(path)
        dirname = (join(FILE_ROOT, username, dirname))
        filename = os.path.split(path)[-1]

        if not check_path(dirname, username):
            abort(HTTP_FORBIDDEN)

        return dirname, filename

    def _update_user_path(self, username, path):
        """
        Make all needed updates to <userdata> (dict and disk) after a post or a put.
        Return the last modification int timestamp of written file.
        :param username: str
        :param path: str
        :return: int
        """
        filepath = userpath2serverpath(username, path)
        last_server_timestamp = file_timestamp(filepath)
        new_md5 = calculate_file_md5(open(filepath, 'rb'))
        userdata[username][LAST_SERVER_TIMESTAMP] = last_server_timestamp
        userdata[username]['files'][normpath(path)] = [last_server_timestamp, new_md5]

        # if path is a shared path then update userdata to permit all user to synchronize with the share
        if _is_shared_with_others(path, username):
            self._update_shared_files(path, username, last_server_timestamp, new_md5)

        save_userdata()
        return last_server_timestamp

    @auth.login_required
    def post(self, path):
        """
        Upload an authenticated user file to the server, given the path relative to the user directory.
        Return the file timestamp of the file created in the server.
        The file must not exist in the server, otherwise only return an http forbidden code.
        :param path: str
        """
        username = auth.username()

        upload_file = request.files['file']
        md5 = request.form['md5']
        dirname, filename = self._get_dirname_filename(path)

        if calculate_file_md5(upload_file) != md5:
            abort(HTTP_CONFLICT)

        if not os.path.exists(dirname):
            os.makedirs(dirname)
        else:
            if os.path.isfile(join(dirname, filename)):
                abort(HTTP_FORBIDDEN)

        filepath = join(dirname, filename)
        upload_file.save(filepath)

        # Update and save <userdata>, and return the last server timestamp.
        last_server_timestamp = self._update_user_path(username, path)

        resp = jsonify({LAST_SERVER_TIMESTAMP: last_server_timestamp})
        resp.status_code = HTTP_CREATED
        return resp

    @auth.login_required
    def put(self, path):
        """
        Modify an authenticated user file in the server (uploading and overwriting it)
        given the path relative to the user directory. The file must exist in the server.
        Return the file timestamp of the file updated in the server.
        :param path: str
        """
        username = auth.username()
        upload_file = request.files['file']
        md5 = request.form['md5']
        dirname, filename = self._get_dirname_filename(path)

        if calculate_file_md5(upload_file) != md5:
            abort(HTTP_CONFLICT)

        filepath = join(dirname, filename)
        if os.path.isfile(filepath):
            upload_file.save(filepath)
        else:
            abort(HTTP_NOT_FOUND)

        # Update and save <userdata>, and return the last server timestamp.
        last_server_timestamp = self._update_user_path(username, path)

        resp = jsonify({LAST_SERVER_TIMESTAMP: last_server_timestamp})
        resp.status_code = HTTP_CREATED
        return resp

api.add_resource(Files, '{}/files/<path:path>'.format(URL_PREFIX), '{}/files/'.format(URL_PREFIX))
api.add_resource(Actions, '{}/actions/<string:cmd>'.format(URL_PREFIX))
api.add_resource(Shares, '{}/shares/<path:root_path>/<string:username>'.format(URL_PREFIX), '{}/shares/<path:root_path>'.format(URL_PREFIX))
api.add_resource(Users, '{}/users/<string:username>'.format(URL_PREFIX))
api.add_resource(UsersRecoverPassword, '{}/users/<string:username>/reset'.format(URL_PREFIX))

# Set the flask.ext.mail.Mail instance
mail = configure_email()

def create_log_file_handler():
    # create file handler which logs even info messages
    if not os.path.isdir('log'):
        os.mkdir('log')
    file_handler = logging.FileHandler(LOG_FILENAME)
    file_handler.setLevel(logging.INFO)
    file_formatter = logging.Formatter('%(asctime)s %(name)-15s  %(levelname)-8s  %(message)s', '%Y-%m-%d %H:%M:%S')
    file_handler.setFormatter(file_formatter)
    logger.addHandler(file_handler)
    return file_handler

def main():
    file_handler = create_log_file_handler()
    parser = argparse.ArgumentParser()
    parser.add_argument('--debug', default=False, action='store_true',
                        help='set console verbosity level to DEBUG (4) [default: %(default)s]')
    parser.add_argument('--verbose', default=False, action='store_true',
                        help='set console verbosity level to INFO (3) [default: %(default)s]. \
                        Ignored if --debug option is set.')
    parser.add_argument('-v', '--verbosity', type=int, choices=range(5), nargs='?',
                        help='set console verbosity: 0=CRITICAL, 1=ERROR, 2=WARN, 3=INFO, 4=DEBUG. \
                        [default: %(default)s]. Ignored if --verbose or --debug option is set.')
    parser.add_argument('-H', '--host', default='0.0.0.0',
                        help='set host address to run the server. [default: %(default)s].')
    args = parser.parse_args()

    if args.debug:
        # If set to True, win against verbosity and verbose parameter
        console_handler.setLevel(logging.DEBUG)
    elif args.verbose:
        # If set to True, win against verbosity parameter
        console_handler.setLevel(logging.INFO)
    elif args.verbosity:
        levels = [logging.CRITICAL, logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG]
        console_handler.setLevel(levels[args.verbosity])
    else:
        # Set default console lvl
        console_handler.setLevel(logging.WARNING)

    logger.info('Console logging level: {}'.format(console_handler.level))
    logger.info('File logging level: {}'.format(file_handler.level))

    update_passwordmeter_terms(UNWANTED_PASS)

    userdata.update(load_userdata())
    init_root_structure()
    app.run(host=args.host, debug=args.debug)

if __name__ == '__main__':
    main()
else:
    # Silence logger in case of import
    console_handler.setLevel(logging.CRITICAL)


text:
<?php

namespace Sebastienheyd\Boilerplate\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class BoilerplateGuestL6
{
    /**
     * Handle an incoming request.
     *
     * @param  Request  $request
     * @param  Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $guards = empty($guards) ? [null] : $guards;

        foreach ($guards as $guard) {
            if (Auth::guard($guard)->check()) {
                return redirect(route('boilerplate.dashboard'));
            }
        }

        return $next($request);
    }
}

redpajama_set_name: RedPajamaGithub


text:
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable } from 'rxjs';
import { AccountService } from '../account/account.service';
import { environment } from 'src/environments/environment';

@Injectable()
export class JwtInterceptor implements HttpInterceptor {
    constructor(private accountService: AccountService) { }

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        // add auth header with jwt if user is logged in and request is to the api url
        const token = this.accountService.accessToken;
        if (token) {
            request = request.clone({
                setHeaders: {
                    Authorization: `Bearer ${token}`
                }
            });
        }

        return next.handle(request);
    }
}


text:
@interface HGViewController : GLKViewController {
}

@end


text:
$(function() {
    // Initialize the parallax feature
    $( '.parallax' ).parallax();

    // Hide the extra content programmatically so it's still visible when the user has JS disabled
    $( '#fullstory' ).hide();

    // Clicking on the "read the full story" button shows more text
    $( '#read-full-story' ).show().on( 'click', function() {
        $( this ).slideUp();    // hide the button
        $( '#fullstory' ).slideDown();
    });

    // Re-show the page when we're ready
    $( '.no-fouc' ).removeClass( 'no-fouc' );
});


text:
::RefineryApp::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # In the development environment your application's code is reloaded on
  # every request.  This slows down response time but is perfect for development
  # since you don't have to restart the webserver when you make code changes.
  config.cache_classes = false

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log

  # Only use best-standards-support built into browsers
  config.action_dispatch.best_standards_support = :builtin
end

Refinery.rescue_not_found = false

# When true will use Amazon's Simple Storage Service on your production machine
# instead of the default file system for resources and images
Refinery.s3_backend = !(ENV['S3_KEY'].nil? || ENV['S3_SECRET'].nil?)


text:
__author__ = 'yuens'

################################### PART1 IMPORT ######################################
import logging
import time
import numpy as np
import struct

import decorator_of_function

from class_painter_by_ndarray import *

################################### PART2 CLASS && FUNCTION ###########################
class CreateNetwork(object):

    Decorator = decorator_of_function.CreateDecorator()

    @Decorator.log_of_function
    def __init__(self):
        self.start = time.clock()

        logging.basicConfig(level = logging.INFO,
                            format = '%(asctime)s  %(levelname)5s %(filename)19s[line:%(lineno)3d] %(funcName)s %(message)s',
                            datefmt = '%y-%m-%d %H:%M:%S',
                            filename = './my-first-cnn.log',
                            filemode = 'a')
        console = logging.StreamHandler()
        console.setLevel(logging.INFO)

        formatter = logging.Formatter('%(asctime)s  %(levelname)5s %(filename)19s[line:%(lineno)3d] %(funcName)s %(message)s')
        console.setFormatter(formatter)

        logging.getLogger('').addHandler(console)
        logging.info("START CLASS {class_name}.".format(class_name = CreateNetwork.__name__))

    @Decorator.log_of_function
    def __del__(self):
        logging.info("END CLASS {class_name}.".format(class_name = CreateNetwork.__name__))
        self.end = time.clock()
        logging.info("The class {class_name} run time is : {delta_time} seconds".format(class_name = CreateNetwork.__name__, delta_time = self.end))

    @Decorator.log_of_function
    def load_image_data_set(self, img_data_dir):
        logging.info("Load image data set from {0}.".format(img_data_dir))

        with open(img_data_dir, "rb") as binary_file_handle:
            image_data_buffer = binary_file_handle.read()

        # '>IIII'是说使用大端法读取4个unsigned int32
        # unpack_from(...)
        # Unpack the buffer, containing packed C structure data, according to
        # fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).
        head = struct.unpack_from('>IIII' , image_data_buffer ,0)
        logging.info("head:{0}".format(head))

        magic_num = struct.calcsize('>IIII')
        img_num = head[1]
        img_width = head[2]
        img_height = head[3]
        logging.info("magic_num:{0}".format(magic_num))
        logging.info("img_num:{0}".format(img_num))
        logging.info("img_width:{0}".format(img_width))
        logging.info("img_height:{0}".format(img_height))

        #[60000]*28*28
        all_img_bit = img_num * img_width * img_height
        all_img_bit_string = '>' + str(all_img_bit) + 'B' #like '>47040000B'
        logging.info("all_img_bit_string:{0}".format(all_img_bit_string))

        all_image_2d_ndarray = struct.unpack_from(all_img_bit_string, image_data_buffer, magic_num)
        all_image_2d_ndarray = np.reshape(all_image_2d_ndarray, [img_num, img_width, img_height])
        return all_image_2d_ndarray

    @Decorator.log_of_function
    def sigmoid_activation_function(self, input_matrix):
        return 1.0 / (1.0 + np.exp(-input_matrix))

    @Decorator.log_of_function
    def tanh_activation_function(self, input_matrix):
        sub_operator1 = np.exp(input_matrix)
        sub_operator2 = np.exp(-input_matrix)
        return (sub_operator1 - sub_operator2)/(sub_operator1 + sub_operator2)

    @Decorator.log_of_function
    def relu_activation_function(self, input_matrix):
        return self.leaky_relu_activation_function(input_matrix = input_matrix,\
                                                   leaky_coefficient = 1)

    @Decorator.log_of_function
    def leaky_relu_activation_function(self, input_matrix, leaky_coefficient):
        ################# start #################
        def relu(x, leak_coefficient):
            if leaky_coefficient == 1:
                return 0 if x <= 0 else x
            else:
                return 0 if x <= 0 else float(x)/leaky_coefficient
        #################  end  #################
        vectorized_relu = np.vectorize(relu)
        return vectorized_relu(input_matrix, leak_coefficient = leaky_coefficient)

    @Decorator.log_of_function
    def max_out_activation_function(self, input_matrix):
        pass

    @Decorator.log_of_function
    def softplus_activation_function(self, input_matrix):
        return np.log(1 + input_matrix.dot(np.e))

    @Decorator.log_of_function
    def convolution(self, input_matrix, conv_operator_array = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1]])):
        conv_operator_shape_tuple = conv_operator_array.shape
        conv_operator_height = conv_operator_shape_tuple[0]
        conv_operator_width = conv_operator_shape_tuple[1]

        input_matrix_shape_tuple = input_matrix.shape
        input_matrix_height = input_matrix_shape_tuple[0]
        input_matrix_width = input_matrix_shape_tuple[1]

        if input_matrix_height <= conv_operator_height or input_matrix_width <= conv_operator_width:
            logging.error("The input matrix({0}) can't execute convolution({1}) progress.".format(input_matrix_shape_tuple, conv_operator_shape_tuple))
            unconv_input_matrix = input_matrix
            return unconv_input_matrix

        # initialization
        new_conv_matrix_height = input_matrix_height - conv_operator_height + 1
        new_conv_matrix_width = input_matrix_width - conv_operator_width + 1

        # sub-function
        def calculate_start_conv_coordinate_in_origin_matrix(new_conv_matrix_height, new_conv_matrix_width):
            start_conv_coordinate_tuple_list = []
            start_height_xrange = xrange(new_conv_matrix_height)
            start_width_xrange = xrange(new_conv_matrix_width)
            for start_height in start_height_xrange:
                cur_conv_coordinate_tuple_list = map(lambda start_width: (start_height, start_width), start_width_xrange)
                start_conv_coordinate_tuple_list.extend(cur_conv_coordinate_tuple_list)
            return start_conv_coordinate_tuple_list

        start_conv_coordinate_tuple_list_in_origin_matrix = calculate_start_conv_coordinate_in_origin_matrix(new_conv_matrix_height = new_conv_matrix_height,\
                                                                                new_conv_matrix_width = new_conv_matrix_width)
        logging.info("start_conv_coordinate_tuple_list_in_origin_matrix:{0}".format(start_conv_coordinate_tuple_list_in_origin_matrix))
        logging.info("len(start_conv_coordinate_tuple_list_in_origin_matrix):{0}".format(len(start_conv_coordinate_tuple_list_in_origin_matrix)))

        def calculate_conv_matrix_according_to_start_conv_coordinate(start_conv_coordinate_tuple_list_in_origin_matrix, input_matrix, conv_operator_array):
            input_matrix = np.mat(input_matrix)
            conv_operator_matrix = np.mat(conv_operator_array)
            """
            logging.info("conv_operator_matrix:{0}".format(conv_operator_matrix))
            logging.info("input_matrix:{0}".format(input_matrix))
            """

            conv_operator_height, conv_operator_width = conv_operator_matrix.shape
            """
            logging.info("conv_operator_matrix.height:{0}".format(conv_operator_height))
            logging.info("conv_operator_width:{0}".format(conv_operator_width))
            """

            # Initialize a convolution matrix
            #conv_input_matrix = np.ones((new_conv_matrix_height, new_conv_matrix_width))
            conv_input_list = list()

            for start_coord_tuple_idx in xrange(len(start_conv_coordinate_tuple_list_in_origin_matrix)):
                start_coord_tuple = start_conv_coordinate_tuple_list_in_origin_matrix[start_coord_tuple_idx]
                #logging.info("start_coord_tuple:{0}".format(start_coord_tuple))

                start_coord_height = start_coord_tuple[0]
                end_coord_height = start_coord_height+conv_operator_height #+1(dont need plus 1, because operator starts from 1)
                start_coord_width = start_coord_tuple[1]
                end_coord_width = start_coord_width+conv_operator_width #+1(dont need plus 1, because operator starts from 1)
                """
                conv_value = input_matrix[start_coord_height:end_coord_height, start_coord_width:end_coord_width]
                logging.info("conv_value:{0}".format(conv_value))
                break
                """
                conv_value = input_matrix[start_coord_height:end_coord_height, start_coord_width:end_coord_width]\
                    .dot(conv_operator_matrix)\
                    .sum()

                conv_input_list.append(conv_value)

            conv_input_matrix = np\
                .mat(conv_input_list)\
                .reshape(new_conv_matrix_height,\
                         new_conv_matrix_width)

            return conv_input_matrix

        conv_input_matrix = calculate_conv_matrix_according_to_start_conv_coordinate(\
            start_conv_coordinate_tuple_list_in_origin_matrix = start_conv_coordinate_tuple_list_in_origin_matrix,\
            input_matrix = input_matrix,\
            conv_operator_array = conv_operator_array)

        return conv_input_matrix

    @Decorator.log_of_function
    def general_pooling(self, input_matrix, pooling_mode = "max", pooling_operator_shape_tuple = (2, 2)):
        # First, Compare the scale of variable "input_matrix" with pooling operator("pooling_operator_shape_tuple")
        input_matrix = np.mat(input_matrix)
        if input_matrix.shape[0] <= pooling_operator_shape_tuple[0] or input_matrix.shape[1] <= pooling_operator_shape_tuple[1]:
            logging.error("The scale of pooling operator is bigger than input matrix.")
            return input_matrix

        #logging.info("input_matrix.shape:{0}".format(input_matrix.shape))

        # Second, Find all start pooling coordinate in origin matrix("input_matrix")
        new_pooled_matrix_height = int(input_matrix.shape[0]/pooling_operator_shape_tuple[0])
        new_pooled_matrix_width = int(input_matrix.shape[1]/pooling_operator_shape_tuple[1])

        #logging.info("new_pooled_matrix_height:{0}".format(new_pooled_matrix_height))
        #logging.info("new_pooled_matrix_width:{0}".format(new_pooled_matrix_width))

        ################# start #################
        def calculate_start_pooling_coordinate_in_origin_matrix(input_matrix_shape_tuple, pooling_operator_shape_tuple):
            start_pooling_coordinate_tuple_list = []
            start_height_xrange = xrange(0, input_matrix_shape_tuple[0], pooling_operator_shape_tuple[0])
            start_width_xrange = xrange(0, input_matrix_shape_tuple[1], pooling_operator_shape_tuple[1])
            for start_height in start_height_xrange:
                cur_pooling_coordinate_tuple_list = map(lambda start_width: (start_height, start_width), start_width_xrange)
                start_pooling_coordinate_tuple_list.extend(cur_pooling_coordinate_tuple_list)
            return start_pooling_coordinate_tuple_list
        #################  end  #################

        start_pooling_coordinate_tuple_list = calculate_start_pooling_coordinate_in_origin_matrix(input_matrix_shape_tuple = input_matrix.shape,\
                                                                                pooling_operator_shape_tuple = pooling_operator_shape_tuple)
        #logging.info("len(start_pooling_coordinate_tuple_list):{0}".format(len(start_pooling_coordinate_tuple_list)))
        #logging.info("start_pooling_coordinate_tuple_list:{0}".format(start_pooling_coordinate_tuple_list))

        ################# start #################
        def calculate_pooled_matrix_according_to_start_pooling_coordinate(start_pooling_coordinate_tuple_list_in_origin_matrix, input_matrix, pooling_mode, pooling_operator_shape_tuple, new_pooled_matrix_shape_tuple):
            input_matrix = np.mat(input_matrix)
            #logging.info("input_matrix:{0}".format(input_matrix))

            pooling_operator_height, pooling_operator_width = pooling_operator_shape_tuple[0], pooling_operator_shape_tuple[1]
            #logging.info("pooling_operator_matrix.height:{0}".format(pooling_operator_height))
            #logging.info("pooling_operator_width:{0}".format(pooling_operator_width))

            # Initialize a convolution matrix
            pooled_input_list = list()

            for start_coord_tuple_idx in xrange(len(start_pooling_coordinate_tuple_list_in_origin_matrix)):
                start_coord_tuple = start_pooling_coordinate_tuple_list_in_origin_matrix[start_coord_tuple_idx]
                #logging.info("start_coord_tuple:{0}".format(start_coord_tuple))

                start_coord_height = start_coord_tuple[0]
                end_coord_height = start_coord_height+pooling_operator_height #+1(dont need plus 1, because operator starts from 1)
                start_coord_width = start_coord_tuple[1]
                end_coord_width = start_coord_width+pooling_operator_width #+1(dont need plus 1, because operator starts from 1)

                if pooling_mode == "max":
                    pooled_value = input_matrix[start_coord_height:end_coord_height, start_coord_width:end_coord_width]\
                        .max()
                elif pooling_mode == "mean":
                    pooled_value = input_matrix[start_coord_height:end_coord_height, start_coord_width:end_coord_width]\
                        .mean()

                pooled_input_list.append(pooled_value)

            new_pooled_matrix_height, new_pooled_matrix_width = new_pooled_matrix_shape_tuple[0], new_pooled_matrix_shape_tuple[1]
            pooled_input_matrix = np\
                .mat(pooled_input_list)\
                .reshape(new_pooled_matrix_height,\
                         new_pooled_matrix_width)

            return pooled_input_matrix
        #################  end  #################

        pooled_input_matrix = calculate_pooled_matrix_according_to_start_pooling_coordinate(start_pooling_coordinate_tuple_list_in_origin_matrix = start_pooling_coordinate_tuple_list,\
                                                                                input_matrix = input_matrix,\
                                                                                pooling_operator_shape_tuple = pooling_operator_shape_tuple,\
                                                                                pooling_mode = pooling_mode,\
                                                                                new_pooled_matrix_shape_tuple = (new_pooled_matrix_height, new_pooled_matrix_width))
        return pooled_input_matrix

    @Decorator.log_of_function
    def overlapping_pooling(self, input_matrix):
        pass

    @Decorator.log_of_function
    def spatial_pyramid_pooling(self, input_matrix):
        pass

################################### PART3 CLASS TEST ##################################
#'''
# Initialization
train_sample_data_dir = "..//data//input//train-images-idx3-ubyte"
train_label_data_dir = "..//data//input//train-labels-idx1-ubyte"
img_save_dir = "../data/output"
img_filename = "raw-relu.jpg"

# Get data and one image matrix
Net = CreateNetwork()
all_image_2d_ndarray = Net.load_image_data_set(img_data_dir = train_sample_data_dir)
input_matrix = all_image_2d_ndarray[0]
#logging.info(input_matrix)

# convolution
#input_matrix = Net.tanh_function(input_matrix = input_matrix)
#input_matrix = Net.softplus_activation_function(input_matrix = input_matrix)
#input_matrix = Net.convolution(input_matrix = input_matrix)
input_matrix = Net.relu_activation_function(input_matrix = input_matrix)

'''
# pooling
input_matrix = Net.general_pooling(input_matrix = input_matrix,\
                                   pooling_mode = "max",\
                                   pooling_operator_shape_tuple = (2, 2))
'''

#"""
Painter = PaintNDarray()
Painter.paint_one_img(img_ndarray = input_matrix,\
                      dpi = 1)

Painter.save_one_img(img_ndarray = input_matrix,\
                     img_save_dir = img_save_dir,\
                     img_filename = img_filename,\
                     dpi = 100,\
                     img_shape_tuple = input_matrix.shape)
#"""

#'''


text:
using System;
using System.Runtime.InteropServices;
using System.Threading;
using Xunit;

public partial class ThreadPoolBoundHandleTests
{
    [Fact]
    public unsafe void AllocateNativeOverlapped_NullAsCallback_ThrowsArgumentNullException()
    {
        using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            AssertExtensions.Throws<ArgumentNullException>("callback", () =>
            {
                handle.AllocateNativeOverlapped(null, new object(), new byte[256]);
            });
        }
    }

    [Fact]
    public unsafe void AllocateNativeOverlapped_PreAllocated_ThrowsArgumentNullException()
    {
        using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            AssertExtensions.Throws<ArgumentNullException>("preAllocated", () =>
            {
                handle.AllocateNativeOverlapped((PreAllocatedOverlapped)null);
            });
        }
    }

    [Fact]
    public unsafe void AllocateNativeOverlapped_NullAsContext_DoesNotThrow()
    {
        using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            NativeOverlapped* result = handle.AllocateNativeOverlapped((_, __, ___) => { }, (object)null, new byte[256]);

            Assert.True(result != null);

            handle.FreeNativeOverlapped(result);
        }
    }

    [Fact]
    public unsafe void AllocateNativeOverlapped_NullAsPinData_DoesNotThrow()
    {
        using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            NativeOverlapped* result = handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), (byte[])null);

            Assert.True(result != null);

            handle.FreeNativeOverlapped(result);
        }
    }

    [Fact]
    public unsafe void AllocateNativeOverlapped_EmptyArrayAsPinData_DoesNotThrow()
    {
        using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            NativeOverlapped* result = handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), new byte[0]);

            Assert.True(result != null);

            handle.FreeNativeOverlapped(result);
        }
    }

    [Fact]
    public unsafe void AllocateNativeOverlapped_NonBlittableTypeAsPinData_Throws()
    {
        using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            Assert.Throws<ArgumentException>(() => handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), new NonBlittableType() { s = "foo" }));
        }
    }

    [Fact]
    public unsafe void AllocateNativeOverlapped_BlittableTypeAsPinData_DoesNotThrow()
    {
        using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            NativeOverlapped* result = handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), new BlittableType() { i = 42 });

            Assert.True(result != null);

            handle.FreeNativeOverlapped(result);
        }
    }

    [Fact]
    public unsafe void AllocateNativeOverlapped_ObjectArrayAsPinData_DoesNotThrow()
    {
        object[] array = new object[]
        {
            new BlittableType() { i = 1 },
            new byte[5],
        };
        using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            NativeOverlapped* result = handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), array);

            Assert.True(result != null);

            handle.FreeNativeOverlapped(result);
        }
    }

    [Fact]
    public unsafe void AllocateNativeOverlapped_ObjectArrayWithNonBlittableTypeAsPinData_Throws()
    {
        object[] array = new object[]
        {
            new NonBlittableType() { s = "foo" },
            new byte[5],
        };
        using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            Assert.Throws<ArgumentException>(() => handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), array));
        }
    }

    [Fact]
    public unsafe void AllocateNativeOverlapped_ReturnedNativeOverlapped_AllFieldsZero()
    {
        using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            NativeOverlapped* overlapped = handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), new byte[256]);

            Assert.Equal(IntPtr.Zero, overlapped->InternalLow);
            Assert.Equal(IntPtr.Zero, overlapped->InternalHigh);
            Assert.Equal(0, overlapped->OffsetLow);
            Assert.Equal(0, overlapped->OffsetHigh);
            Assert.Equal(IntPtr.Zero, overlapped->EventHandle);

            handle.FreeNativeOverlapped(overlapped);
        }
    }

    [Fact]
    public unsafe void AllocateNativeOverlapped_PreAllocated_ReturnedNativeOverlapped_AllFieldsZero()
    {
        using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            using(PreAllocatedOverlapped preAlloc = new PreAllocatedOverlapped((_, __, ___) => { }, new object(), new byte[256]))
            {
                NativeOverlapped* overlapped = handle.AllocateNativeOverlapped(preAlloc);

                Assert.Equal(IntPtr.Zero, overlapped->InternalLow);
                Assert.Equal(IntPtr.Zero, overlapped->InternalHigh);
                Assert.Equal(0, overlapped->OffsetLow);
                Assert.Equal(0, overlapped->OffsetHigh);
                Assert.Equal(IntPtr.Zero, overlapped->EventHandle);

                handle.FreeNativeOverlapped(overlapped);
            }
        }
    }

    [Fact]
    public unsafe void AllocateNativeOverlapped_PossibleReusedReturnedNativeOverlapped_OffsetLowAndOffsetHighSetToZero()
    {   // The CLR reuses NativeOverlapped underneath, check to make sure that they reset fields back to zero

        using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            NativeOverlapped* overlapped = handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), new byte[256]);
            overlapped->OffsetHigh = 1;
            overlapped->OffsetLow = 1;
            handle.FreeNativeOverlapped(overlapped);

            overlapped = handle.AllocateNativeOverlapped((errorCode, numBytes, overlap) => { }, new object(), new byte[256]);

            Assert.Equal(IntPtr.Zero, overlapped->InternalLow);
            Assert.Equal(IntPtr.Zero, overlapped->InternalHigh);
            Assert.Equal(0, overlapped->OffsetLow);
            Assert.Equal(0, overlapped->OffsetHigh);
            Assert.Equal(IntPtr.Zero, overlapped->EventHandle);

            handle.FreeNativeOverlapped(overlapped);
        }
    }

    [Fact]
    public unsafe void AllocateNativeOverlapped_PreAllocated_ReusedReturnedNativeOverlapped_OffsetLowAndOffsetHighSetToZero()
    {   // The CLR reuses NativeOverlapped underneath, check to make sure that they reset fields back to zero

        using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            PreAllocatedOverlapped preAlloc = new PreAllocatedOverlapped((_, __, ___) => { }, new object(), new byte[256]);
            NativeOverlapped* overlapped = handle.AllocateNativeOverlapped(preAlloc);
            overlapped->OffsetHigh = 1;
            overlapped->OffsetLow = 1;
            handle.FreeNativeOverlapped(overlapped);

            overlapped = handle.AllocateNativeOverlapped(preAlloc);

            Assert.Equal(IntPtr.Zero, overlapped->InternalLow);
            Assert.Equal(IntPtr.Zero, overlapped->InternalHigh);
            Assert.Equal(0, overlapped->OffsetLow);
            Assert.Equal(0, overlapped->OffsetHigh);
            Assert.Equal(IntPtr.Zero, overlapped->EventHandle);

            handle.FreeNativeOverlapped(overlapped);
        }
    }

    [Fact]
    public unsafe void AllocateNativeOverlapped_WhenDisposed_ThrowsObjectDisposedException()
    {
        ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle();
        handle.Dispose();

        Assert.Throws<ObjectDisposedException>(() =>
        {
            handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), new byte[256]);
        });
    }

    [Fact]
    public unsafe void AllocateNativeOverlapped_PreAllocated_WhenDisposed_ThrowsObjectDisposedException()
    {
        using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            PreAllocatedOverlapped preAlloc = new PreAllocatedOverlapped(delegate { }, null, null);
            preAlloc.Dispose();

            Assert.Throws<ObjectDisposedException>(() =>
            {
                handle.AllocateNativeOverlapped(preAlloc);
            });
        }
    }

    [Fact]
    public unsafe void AllocateNativeOverlapped_PreAllocated_WhenHandleDisposed_ThrowsObjectDisposedException()
    {
        ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle();
        handle.Dispose();

        PreAllocatedOverlapped preAlloc = new PreAllocatedOverlapped(delegate { }, null, null);

        Assert.Throws<ObjectDisposedException>(() =>
        {
            handle.AllocateNativeOverlapped(preAlloc);
        });
    }

    [Fact]
    public unsafe void AllocateNativeOverlapped_PreAllocated_WhenAlreadyAllocated_ThrowsArgumentException()
    {
        using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
        {
            using(PreAllocatedOverlapped preAlloc = new PreAllocatedOverlapped(delegate { }, null, null))
            {
                NativeOverlapped* overlapped = handle.AllocateNativeOverlapped(preAlloc);

                Assert.Throws<ArgumentException>(() =>
                {
                    handle.AllocateNativeOverlapped(preAlloc);
                });

                handle.FreeNativeOverlapped(overlapped);
            }
        }
    }
}


text:
package org.apache.camel.component.mail;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
import org.jvnet.mock_javamail.Mailbox;

/**
 * Unit test for Mail using @ in username option
 */
public class MailUsernameWithAtSignTest extends CamelTestSupport {

    @Test
    public void testMailUsingAtSignInUsername() throws Exception {
        Mailbox.clearAll();

        String body = "Hello Claus.\nYes it does.\n\nRegards James.";
        template.sendBody("direct:a", body);

        MockEndpoint mock = getMockEndpoint("mock:result");
        mock.expectedMessageCount(1);
        mock.expectedBodiesReceived(body);
        mock.assertIsSatisfied();
    }

    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            public void configure() throws Exception {
                from("direct:a").to("smtp://localhost?username=james@localhost");

                from("pop3://localhost?username=james&password=secret&consumer.initialDelay=100&consumer.delay=100").to("mock:result");
            }
        };
    }
}


text:
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml.Schema;

using Sce.Atf;

using Sce.Sled.Shared.Utilities;

namespace Sce.Sled.Shared
{
    /// <summary>
    /// Static class containing some shared items
    /// </summary>
    public static class SledShared
    {
        /// <summary>
        /// SLED XML namespace
        /// </summary>
        public const string SledXmlNamespace = "sled";

        /// <summary>
        /// Path to the schema directory (schema embedded in .exe)
        /// </summary>
        public const string SchemaPath = "Sce.Sled.Shared.Resources.Schemas.SledProjectFiles.xsd";

        /// <summary>
        /// Path to icon directory (icons embedded in .exe)
        /// </summary>
        public const string IconPathBase = "Sce.Sled.Shared.Resources.Icons";

        /// <summary>
        /// Path to base directory of application
        /// <remarks>ie. it is Application.StartupPath</remarks>
        /// </summary>
        public static string DirectoryPath
        {
            get { return s_directoryPath; }
            set { if (s_directoryPath == null) s_directoryPath = value; }
        }
        private static string s_directoryPath;

        /// <summary>
        /// Gets or sets path to plugin directory
        /// <remarks>DirectoryPath + "Plugins\"</remarks>
        /// </summary>
        public static string PluginPath
        {
            get { return s_pluginPath; }
            set { if (s_pluginPath == null) s_pluginPath = value; }
        }
        private static string s_pluginPath;

        /// <summary>
        /// Gets or sets XmlSchema
        /// </summary>
        public static XmlSchema XmlSchema
        {
            get { return s_xmlSchema; }
            set { if (s_xmlSchema == null) s_xmlSchema = value; }
        }
        private static XmlSchema s_xmlSchema;

        /// <summary>
        /// Gets whether the running code is running inside SLED</summary>
        public static bool IsSled
        {
            get
            {
                // Check once
                if (!s_bIsSledCheck)
                {
                    var assembly = Assembly.GetEntryAssembly();
                    s_bIsSled = assembly.IsDefined(typeof(SledAssemblyAttribute), false);
                    s_bIsSledCheck = true;
                }

                return s_bIsSled;
            }
        }
        private static bool s_bIsSledCheck;
        private static bool s_bIsSled;

        /// <summary>
        /// Gets whether SLED is running as a 64 bit process or not
        /// </summary>
        public static bool Is64Bit
        {
            get { return IntPtr.Size == 8; }
        }

        /// <summary>
        /// Register images with ATF
        /// </summary>
        public static void RegisterImages()
        {
            if (s_registeredImages)
                return;

            var assembly = Assembly.GetAssembly(typeof(SledShared));

            const string pngExt = ".png";
            const string icoExt = ".ico";
            const string period = ".";

            var dictImages =
                new Dictionary<string, string>
                    {
                        {SledIcon.DebugConnect, "debug_connect" + pngExt},
                        {SledIcon.DebugDisconnect, "debug_disconnect" + pngExt},
                        {SledIcon.DebugManageTargets, "debug_manage_targets" + pngExt},
                        {SledIcon.DebugStart, "debug_start_debug" + pngExt},
                        {SledIcon.DebugStepOut, "debug_stepOut_debug" + pngExt},
                        {SledIcon.DebugStepInto, "debug_step_debug" + pngExt},
                        {SledIcon.DebugStepOver, "debug_step_over_debug" + pngExt},
                        {SledIcon.DebugStop, "debug_stop_debug" + pngExt},
                        {SledIcon.FileClose, "file_close" + pngExt},
                        {SledIcon.FileNewLua, "file_new_lua" + pngExt},
                        {SledIcon.FileNewTxt, "file_new_txt" + pngExt},
                        {SledIcon.FileOpen, "file_open" + pngExt},
                        {SledIcon.ProjectFileAdd, "file_project_add" + pngExt},
                        {SledIcon.ProjectClose, "file_project_close" + pngExt},
                        {SledIcon.ProjectNew, "file_project_new" + pngExt},
                        {SledIcon.ProjectOpen, "file_project_open" + pngExt},
                        {SledIcon.ProjectFileRemove, "file_project_remove" + pngExt},
                        {SledIcon.ProjectSave, "file_project_save" + pngExt},
                        {SledIcon.ProjectSaveAs, "file_project_save_as" + pngExt},
                        {SledIcon.ProjectParseVariables, "project_parse_globals" + pngExt},
                        {SledIcon.ProjectSyntaxCheck, "project_syntax_check" + pngExt},
                        {SledIcon.ProjectToggleBreakpoint, "project_toggle_breakpoints" + pngExt},
                        {SledIcon.ProjectToggleMemoryTracer, "project_toggle_memory_trc" + pngExt},
                        {SledIcon.ProjectToggleProfiler, "project_toggle_profiler" + pngExt},
                        {SledIcon.Breakpoint, "bp" + pngExt},
                        {SledIcon.BreakpointHit, "bp_hit" + pngExt},
                        {SledIcon.BreakpointCsi, "bp_csi" + pngExt},
                        {SledIcon.Go, "go" + pngExt},
                        {SledIcon.Stop, "stop" + pngExt}
                    };

            foreach (var kv in dictImages)
            {
                var iPos = kv.Value.IndexOf(pngExt, StringComparison.Ordinal);

                var sz16 = kv.Value.Insert(iPos, "16");
                var sz24 = kv.Value.Insert(iPos, "24");
                var sz32 = kv.Value.Insert(iPos, "32");

                ResourceUtil.RegisterImage(
                    kv.Key,
                    GdiUtil.GetImage(assembly, IconPathBase + period + sz16),
                    GdiUtil.GetImage(assembly, IconPathBase + period + sz24),
                    GdiUtil.GetImage(assembly, IconPathBase + period + sz32));
            }

            using (var icon = GdiUtil.GetIcon(assembly, IconPathBase + period + "Sled" + icoExt))
            {
                ResourceUtil.RegisterImage(SledIcon.Sled, icon.ToBitmap());
            }

            s_registeredImages = true;
        }

        private static bool s_registeredImages;
    }
}


text:
__all__ = ['Simulation']

__docformat__ = 'restructuredtext'

from tabulate import tabulate
import numpy as np
import os

class Simulation:
    """Simulation

    Base class for all simulations.

    Handles the caching and some displaying option.

    Args:
        S (Structure): sample to do simulations with.
        force_recalc (boolean): force recalculation of results.

    Keyword Args:
        save_data (boolean): true to save simulation results.
        cache_dir (str): path to cached data.
        disp_messages (boolean): true to display messages from within the
            simulations.
        progress_bar (boolean): enable tqdm progress bar.

    Attributes:
        S (Structure): sample structure to calculate simulations on.
        force_recalc (boolean): force recalculation of results.
        save_data (boolean): true to save simulation results.
        cache_dir (str): path to cached data.
        disp_messages (boolean): true to display messages from within the
            simulations.
        progress_bar (boolean): enable tqdm progress bar.

    """

    def __init__(self, S, force_recalc, **kwargs):
        self.S = S
        self.force_recalc = force_recalc
        self.save_data = kwargs.get('save_data', True)
        self.cache_dir = kwargs.get('cache_dir', './')
        self.disp_messages = kwargs.get('disp_messages', True)
        self.progress_bar = kwargs.get('progress_bar', True)

    def __str__(self, output=[]):
        """String representation of this class"""
        output = [['force recalc', self.force_recalc],
                  ['cache directory', self.cache_dir],
                  ['display messages', self.disp_messages],
                  ['save data', self.save_data],
                  ['progress bar', self.progress_bar]] + output

        class_str = 'This is the current structure for the simulations:\n\n'
        class_str += self.S.__str__()
        class_str += '\n\nDisplay properties:\n\n'
        class_str += tabulate(output, headers=['parameter', 'value'], tablefmt="rst",
                              colalign=('right',), floatfmt=('.2f', '.2f'))
        return class_str

    def disp_message(self, message):
        """disp_message

        Wrapper to display messages for that class.

        Args:
            message (str): message to display.

        """
        if self.disp_messages:
            print(message)

    def save(self, full_filename, data, *args):
        """save

        Save data to file. The variable name can be handed as variable argument.

        Args:
            full_filename (str): full file name to data file.
            data (ndarray): actual data to save.
            *args (str, optional): variable name within the data file.

        """
        if len(args) == 1:
            var_name = args[0]
        else:
            var_name = '_data_'
        if self.save_data:
            np.savez(full_filename, **data)
            filename = os.path.basename(full_filename)
            self.disp_message('{:s} saved to file:\n\t {:s}'.format(var_name, filename))

    @staticmethod
    def conv_with_function(y, x, handle):
        """conv_with_function

        Convolutes the array :math:`y(x)` with a function given by the handle
        on the argument array :math:`x`.

        Args:
            y (ndarray[float]): y data.
            x (ndarray[float]): x data.
            handle (@lamdba): convolution function.

        Returns:
            y_conv (ndarray[float]): convoluted data.

        """
        dx = np.min(np.diff(x))
        x_lin = np.r_[np.min(x):np.max(x):dx]
        y_lin = np.interp(x_lin, x, y)
        x0 = np.mean(x_lin)
        y_handle = handle(x_lin-x0)

        temp = np.convolve(y_lin, y_handle/y_handle.sum(), mode="same")

        y_conv = np.interp(x, x_lin, temp)
        # finally remove NaN entries due to the interpolation
        y_conv[np.isnan(y_conv)] = 0
        return y_conv

    @property
    def cache_dir(self):
        return self._cache_dir

    @cache_dir.setter
    def cache_dir(self, cache_dir):
        import os.path as path
        if path.exists(cache_dir):
            self._cache_dir = cache_dir
        else:
            print('Cache dir does not exist.\nPlease create the path first.')


text:
.oo-ui-icon-alignCentre {
	background-image: /* @embed */ url(themes/apex/images/icons/align-center.svg);
}
.oo-ui-icon-alignLeft {
	background-image: /* @embed */ url(themes/apex/images/icons/align-float-left.svg);
}
.oo-ui-icon-alignRight {
	background-image: /* @embed */ url(themes/apex/images/icons/align-float-right.svg);
}
.oo-ui-icon-insert {
	background-image: /* @embed */ url(themes/apex/images/icons/insert.svg);
}
.oo-ui-icon-layout {
	background-image: /* @embed */ url(themes/apex/images/icons/layout-ltr.svg);
}
.oo-ui-icon-newline {
	background-image: /* @embed */ url(themes/apex/images/icons/newline-ltr.svg);
}
.oo-ui-icon-redirect {
	background-image: /* @embed */ url(themes/apex/images/icons/redirect-ltr.svg);
}
.oo-ui-icon-noWikiText {
	background-image: /* @embed */ url(themes/apex/images/icons/noWikiText-ltr.svg);
}
.oo-ui-icon-outline {
	background-image: /* @embed */ url(themes/apex/images/icons/outline-ltr.svg);
}
.oo-ui-icon-puzzle {
	background-image: /* @embed */ url(themes/apex/images/icons/puzzle-ltr.svg);
}
.oo-ui-icon-quotes {
	background-image: /* @embed */ url(themes/apex/images/icons/quotes-ltr.svg);
}
.oo-ui-icon-quotesAdd {
	background-image: /* @embed */ url(themes/apex/images/icons/quotesAdd-ltr.svg);
}
.oo-ui-icon-searchCaseSensitive {
	background-image: /* @embed */ url(themes/apex/images/icons/case-sensitive.svg);
}
.oo-ui-icon-searchRegularExpression {
	background-image: /* @embed */ url(themes/apex/images/icons/regular-expression.svg);
}
.oo-ui-icon-specialCharacter {
	background-image: /* @embed */ url(themes/apex/images/icons/specialCharacter.svg);
}
.oo-ui-icon-table {
	background-image: /* @embed */ url(themes/apex/images/icons/table.svg);
}
.oo-ui-icon-tableAddColumnAfter {
	background-image: /* @embed */ url(themes/apex/images/icons/table-insert-column-rtl.svg);
}
.oo-ui-icon-tableAddColumnBefore {
	background-image: /* @embed */ url(themes/apex/images/icons/table-insert-column-ltr.svg);
}
.oo-ui-icon-tableAddRowAfter {
	background-image: /* @embed */ url(themes/apex/images/icons/table-insert-row-after.svg);
}
.oo-ui-icon-tableAddRowBefore {
	background-image: /* @embed */ url(themes/apex/images/icons/table-insert-row-before.svg);
}
.oo-ui-icon-tableCaption {
	background-image: /* @embed */ url(themes/apex/images/icons/table-caption.svg);
}
.oo-ui-icon-tableMergeCells {
	background-image: /* @embed */ url(themes/apex/images/icons/table-merge-cells.svg);
}
.oo-ui-icon-templateAdd {
	background-image: /* @embed */ url(themes/apex/images/icons/templateAdd-ltr.svg);
}
.oo-ui-icon-translation {
	background-image: /* @embed */ url(themes/apex/images/icons/translation-ltr.svg);
}
.oo-ui-icon-wikiText {
	background-image: /* @embed */ url(themes/apex/images/icons/wikiText.svg);
}


text:
<?php
namespace QuickBooksOnline\API\Data;

/**
 * @xmlNamespace http://schema.intuit.com/finance/v3
 * @xmlType IntuitEntity
 * @xmlName IPPVendorType
 * @var IPPVendorType
 * @xmlDefinition Vendor types allow categorizing vendors in ways that are meaningful to the business. For example, one could set up vendor types so that they indicate a vendor's industry or geographic location. The categorization then can be used for reporting.
 */
class IPPVendorType
	extends IPPIntuitEntity	{

		/**
		* Initializes this object, optionally with pre-defined property values
		*
		* Initializes this object and it's property members, using the dictionary
		* of key/value pairs passed as an optional argument.
		*
		* @param dictionary $keyValInitializers key/value pairs to be populated into object's properties
		* @param boolean $verbose specifies whether object should echo warnings
		*/
		public function __construct($keyValInitializers=array(), $verbose=FALSE)
		{
			foreach($keyValInitializers as $initPropName => $initPropVal)
			{
				if (property_exists('IPPVendorType',$initPropName) || property_exists('QuickBooksOnline\API\Data\IPPVendorType',$initPropName))
				{
					$this->{$initPropName} = $initPropVal;
				}
				else
				{
					if ($verbose)
						echo "Property does not exist ($initPropName) in class (".get_class($this).")";
				}
			}
		}

	/**
	 * @Definition User recognizable name for the Vendor Type.
								Length Restriction:
								QBD: 1024

	 * @xmlType element
	 * @xmlNamespace http://schema.intuit.com/finance/v3
	 * @xmlMinOccurs 0
	 * @xmlName Name
	 * @var string
	 */
	public $Name;
	/**
	 * @Definition VendorType Parent referene
	 * @xmlType element
	 * @xmlNamespace http://schema.intuit.com/finance/v3
	 * @xmlMinOccurs 0
	 * @xmlName ParentRef
	 * @var com\intuit\schema\finance\v3\IPPReferenceType
	 */
	public $ParentRef;
	/**
	 * @Definition
								Product: ALL
								Description: Fully qualified name of the entity. The fully qualified name prepends the topmost parent, followed by each sub element separated by colons. Takes the form of Parent:Customer:Job:Sub-job. Limited to 5 levels.[br /]Max. length: 41 characters (single name) or 209 characters (fully qualified name).

	 * @xmlType element
	 * @xmlNamespace http://schema.intuit.com/finance/v3
	 * @xmlMinOccurs 0
	 * @xmlName FullyQualifiedName
	 * @var string
	 */
	public $FullyQualifiedName;
	/**
	 * @Definition Whether or not active inactive customer types may be hidden from most display purposes and may not be used on financial transactions
	 * @xmlType element
	 * @xmlNamespace http://schema.intuit.com/finance/v3
	 * @xmlMinOccurs 0
	 * @xmlName Active
	 * @var boolean
	 */
	public $Active;

} // end class IPPVendorType