<commit_before>"""This is a model to produce bootstrap consensus tree of the dtm."""

import numpy as np
import pandas as pd
from Bio import Phylo
from io import StringIO
from skbio import TreeNode
from ete3 import TreeStyle, Tree
from Bio.Phylo.Consensus import *
from scipy.cluster.hierarchy import linkage
from typing import NamedTuple, Optional, List
from lexos.models.base_model import BaseModel
from lexos.models.matrix_model import MatrixModel, IdTempLabelMap
from lexos.receivers.bct_receiver import BCTOption, BCTReceiver

class BCTTestOptions(NamedTuple):
    """A typed tuple to hold test options."""

    doc_term_matrix: pd.DataFrame
    front_end_option: BCTOption
    id_temp_label_map: IdTempLabelMap

class BCTModel(BaseModel):
    """The BCTModel inherits from the BaseModel."""

    def __init__(self, test_options: Optional[BCTTestOptions] = None):
        """Generate bootstrap consensus tree.

        :param test_options: The input used in testing to override the
                             dynamically loaded option.
        """
        super().__init__()
        if test_options is not None:
            self._test_dtm = test_options.doc_term_matrix
            self._test_front_end_option = test_options.front_end_option
            self._test_id_temp_label_map = test_options.id_temp_label_map
        else:
            self._test_dtm = None
            self._test_front_end_option = None
            self._test_id_temp_label_map = None

    @property
    def _doc_term_matrix(self) -> pd.DataFrame:
        """:return: the document term matrix."""
        return self._test_dtm if self._test_dtm is not None \
            else MatrixModel().get_matrix()

    @property
    def _id_temp_label_map(self) -> IdTempLabelMap:
        """:return: a map takes an id to temp labels."""
        return self._test_id_temp_label_map \
            if self._test_id_temp_label_map is not None \
            else MatrixModel().get_id_temp_label_map()

    @property
    def _bct_option(self) -> BCTOption:
        """:return: the front end option of bootstrap consensus tree."""
        return self._test_front_end_option \
            if self._test_front_end_option is not None \
            else BCTReceiver().options_from_front_end()

    def _get_newick_tree(self,
                         labels: List[str],
                         sample_dtm: pd.DataFrame) -> str:
        """Get newick tree based on a subset of the DTM.

        :param labels: All file names from the DTM.
        :param sample_dtm: A 80% subset of the complete DTM.
        :return: A newick formatted tree representing the DTM subset.
        """
        # Create the StringIO newick tree holder.
        newick_tree = StringIO()

        # noinspection PyTypeChecker
        # Get the linkage matrix for the sample doc term matrix.
        linkage_matrix = linkage(
            sample_dtm.values,
            metric=self._bct_option.dist_metric,
            method=self._bct_option.linkage_method
        )

        # Convert linkage matrix to a tree node
        tree = TreeNode.from_linkage_matrix(
            linkage_matrix=linkage_matrix,
            id_list=labels
        )

        # noinspection PyUnresolvedReferences
        # Write the tree to newick format to the stringIO holder.
        tree.write(newick_tree)

        # Get the string in side StringIO object.
        newick_tree_string = newick_tree.getvalue()

        # Return it as a bio python object.
        return Phylo.read(
            StringIO(newick_tree_string),
            format="newick"
        )

    def _get_bootstrap_trees(self) -> List[str]:
        """Do bootstrap on the DTM to get a list of newick trees.

        :return: A list of newick formatted tree where each tree was based on
                 a 80% subset of the complete DTM.
        """
        # Get file names, since tree nodes need labels.
        labels = [self._id_temp_label_map[file_id]
                  for file_id in self._doc_term_matrix.index.values]

        # The bootstrap process to get all the trees.
        return [
            self._get_newick_tree(
                sample_dtm=self._doc_term_matrix.sample(
                    axis=1,
                    frac=0.8,
                    random_state=np.random.RandomState()
                ),
                labels=labels,
            )
            for _ in range(self._bct_option.iterations)
        ]

    def _get_bootstrap_consensus_tree(self) -> str:
        """Get the consensus tree.

        :return: The consensus tree of the list of newick trees.
        """
        # Create the StringIO newick tree holder.
        consensus_tree_holder = StringIO()

        # Check users selection and return the correct consensus tree.
        if self._bct_option.consensus_method == "strict":
            Phylo.write(
                trees=strict_consensus(trees=self._get_bootstrap_trees()),
                file=consensus_tree_holder,
                format="newick"
            )
        elif self._bct_option.consensus_method == "majority":
            Phylo.write(
                trees=majority_consensus(trees=self._get_bootstrap_trees()),
                file=consensus_tree_holder,
                format="newick"
            )
            return majority_consensus(trees=self._get_bootstrap_trees())
        elif self._bct_option.consensus_method == "adam":
            Phylo.write(
                trees=adam_consensus(trees=self._get_bootstrap_trees()),
                file=consensus_tree_holder,
                format="newick"
            )
        else:
            raise ValueError("Invalid bootstrap consensus method.")

        return consensus_tree_holder.getvalue()

    @staticmethod
    def _get_ete_tree_style() -> TreeStyle:
        """

        :return:
        """
        tree_style = TreeStyle()
        tree_style.mode = "c"
        tree_style.scale = None
        tree_style.arc_span = 360
        tree_style.arc_start = 0
        tree_style.show_scale = False
        tree_style.show_leaf_name = True
        tree_style.show_branch_length = True
        tree_style.branch_vertical_margin = 10
        return tree_style

    def get_bootstrap_consensus_result(self):
        """Draw the bootstrap consensus tree result.

        :return:
        """
        # Get the newick formatted consensus tree.
        consensus_tree = self._get_bootstrap_consensus_tree()

        # Read in the newick consensus tree as ete tree object.
        ete_tree = Tree(consensus_tree)

        # Get the ete tree drawing style.
        tree_style = self._get_ete_tree_style()

        ete_tree.render(tree_style=tree_style)

        return "A"
<commit_msg>More testing and play around with ete<commit_after>"""This is a model to produce bootstrap consensus tree of the dtm."""

import os
import numpy as np
import pandas as pd
from Bio import Phylo
from io import StringIO
from skbio import TreeNode
from ete3 import TreeStyle, Tree
from Bio.Phylo.Consensus import *
from scipy.cluster.hierarchy import linkage
from typing import NamedTuple, Optional, List
from lexos.managers import session_manager
from lexos.models.base_model import BaseModel
from lexos.helpers.constants import RESULTS_FOLDER
from lexos.models.matrix_model import MatrixModel, IdTempLabelMap
from lexos.receivers.bct_receiver import BCTOption, BCTReceiver

class BCTTestOptions(NamedTuple):
    """A typed tuple to hold test options."""

    doc_term_matrix: pd.DataFrame
    front_end_option: BCTOption
    id_temp_label_map: IdTempLabelMap

class BCTModel(BaseModel):
    """The BCTModel inherits from the BaseModel."""

    def __init__(self, test_options: Optional[BCTTestOptions] = None):
        """Generate bootstrap consensus tree.

        :param test_options: The input used in testing to override the
                             dynamically loaded option.
        """
        super().__init__()
        if test_options is not None:
            self._test_dtm = test_options.doc_term_matrix
            self._test_front_end_option = test_options.front_end_option
            self._test_id_temp_label_map = test_options.id_temp_label_map
        else:
            self._test_dtm = None
            self._test_front_end_option = None
            self._test_id_temp_label_map = None

    @property
    def _doc_term_matrix(self) -> pd.DataFrame:
        """:return: the document term matrix."""
        return self._test_dtm if self._test_dtm is not None \
            else MatrixModel().get_matrix()

    @property
    def _id_temp_label_map(self) -> IdTempLabelMap:
        """:return: a map takes an id to temp labels."""
        return self._test_id_temp_label_map \
            if self._test_id_temp_label_map is not None \
            else MatrixModel().get_id_temp_label_map()

    @property
    def _bct_option(self) -> BCTOption:
        """:return: the front end option of bootstrap consensus tree."""
        return self._test_front_end_option \
            if self._test_front_end_option is not None \
            else BCTReceiver().options_from_front_end()

    def _get_newick_tree(self,
                         labels: List[str],
                         sample_dtm: pd.DataFrame) -> str:
        """Get newick tree based on a subset of the DTM.

        :param labels: All file names from the DTM.
        :param sample_dtm: A 80% subset of the complete DTM.
        :return: A newick formatted tree representing the DTM subset.
        """
        # Create the StringIO newick tree holder.
        newick_tree = StringIO()

        # noinspection PyTypeChecker
        # Get the linkage matrix for the sample doc term matrix.
        linkage_matrix = linkage(
            sample_dtm.values,
            metric=self._bct_option.dist_metric,
            method=self._bct_option.linkage_method
        )

        # Convert linkage matrix to a tree node
        tree = TreeNode.from_linkage_matrix(
            linkage_matrix=linkage_matrix,
            id_list=labels
        )

        # noinspection PyUnresolvedReferences
        # Write the tree to newick format to the stringIO holder.
        tree.write(newick_tree)

        # Get the string in side StringIO object.
        newick_tree_string = newick_tree.getvalue()

        # Return it as a bio python object.
        return Phylo.read(
            StringIO(newick_tree_string),
            format="newick"
        )

    def _get_bootstrap_trees(self) -> List[str]:
        """Do bootstrap on the DTM to get a list of newick trees.

        :return: A list of newick formatted tree where each tree was based on
                 a 80% subset of the complete DTM.
        """
        # Get file names, since tree nodes need labels.
        labels = [self._id_temp_label_map[file_id]
                  for file_id in self._doc_term_matrix.index.values]

        # The bootstrap process to get all the trees.
        return [
            self._get_newick_tree(
                sample_dtm=self._doc_term_matrix.sample(
                    axis=1,
                    frac=0.8,
                    random_state=np.random.RandomState()
                ),
                labels=labels,
            )
            for _ in range(self._bct_option.iterations)
        ]

    def _get_bootstrap_consensus_tree(self) -> Tree:
        """Get the consensus tree.

        :return: The consensus tree of the list of newick trees.
        """
        # Create the StringIO newick tree holder.
        consensus_tree_holder = StringIO()

        # Check users selection and return the correct consensus tree.
        if self._bct_option.consensus_method == "strict":
            Phylo.write(
                trees=strict_consensus(trees=self._get_bootstrap_trees()),
                file=consensus_tree_holder,
                format="newick"
            )
        elif self._bct_option.consensus_method == "majority":
            Phylo.write(
                trees=majority_consensus(trees=self._get_bootstrap_trees()),
                file=consensus_tree_holder,
                format="newick"
            )
            return majority_consensus(trees=self._get_bootstrap_trees())
        elif self._bct_option.consensus_method == "adam":
            Phylo.write(
                trees=adam_consensus(trees=self._get_bootstrap_trees()),
                file=consensus_tree_holder,
                format="newick"
            )
        else:
            raise ValueError("Invalid bootstrap consensus method.")

        return Tree(consensus_tree_holder.getvalue(), quoted_node_names=True)

    @staticmethod
    def _get_ete_tree_style() -> TreeStyle:
        """

        :return:
        """
        tree_style = TreeStyle()
        tree_style.mode = "c"
        tree_style.scale = None
        tree_style.arc_span = 360
        tree_style.arc_start = 0
        tree_style.show_scale = False
        tree_style.show_leaf_name = True
        tree_style.show_branch_length = True
        tree_style.branch_vertical_margin = 10
        return tree_style

    def get_bootstrap_consensus_result(self) -> str:
        """Draw the bootstrap consensus tree result.

        :return:
        """
        # Get the default saving directory of BCT result.
        result_folder_path = os.path.join(
            session_manager.session_folder(), RESULTS_FOLDER)

        # Attempt to make the directory, if the directory isn't already there.
        if not os.path.isdir(result_folder_path):
            os.makedirs(result_folder_path)

        # Get the complete saving path of BCT result.
        save_path = "static/images/bct_result.png"

        # Get the ete formatted consensus tree.
        consensus_tree = self._get_bootstrap_consensus_tree()

        consensus_tree.render(
            tree_style=self._get_ete_tree_style(),
            file_name="result.png",
            units="px",
            h=700,
            w=700
        )

        return save_path
<|endoftext|>


<commit_before>"""This is a model to produce bootstrap consensus tree of the dtm."""
import base64
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from Bio import Phylo
from io import StringIO, BytesIO
from skbio import TreeNode
from skbio.tree import majority_rule
from scipy.cluster.hierarchy import linkage
from typing import NamedTuple, Optional, List
from lexos.models.base_model import BaseModel
from lexos.models.matrix_model import MatrixModel, IdTempLabelMap
from lexos.receivers.bct_receiver import BCTOption, BCTReceiver

# Set matplotlib backend to be agg in order to make it work with flask.
plt.switch_backend("agg")

class BCTTestOptions(NamedTuple):
    """A typed tuple to hold test options."""

    doc_term_matrix: pd.DataFrame
    front_end_option: BCTOption
    id_temp_label_map: IdTempLabelMap

class BCTModel(BaseModel):
    """The BCTModel inherits from the BaseModel."""

    def __init__(self, test_options: Optional[BCTTestOptions] = None):
        """Generate bootstrap consensus tree.

        :param test_options: The input used in testing to override the
                             dynamically loaded option.
        """
        super().__init__()
        if test_options is not None:
            self._test_dtm = test_options.doc_term_matrix
            self._test_front_end_option = test_options.front_end_option
            self._test_id_temp_label_map = test_options.id_temp_label_map
        else:
            self._test_dtm = None
            self._test_front_end_option = None
            self._test_id_temp_label_map = None

    @property
    def _doc_term_matrix(self) -> pd.DataFrame:
        """:return: the document term matrix."""
        return self._test_dtm if self._test_dtm is not None \
            else MatrixModel().get_matrix()

    @property
    def _id_temp_label_map(self) -> IdTempLabelMap:
        """:return: a map takes an id to temp labels."""
        return self._test_id_temp_label_map \
            if self._test_id_temp_label_map is not None \
            else MatrixModel().get_id_temp_label_map()

    @property
    def _bct_option(self) -> BCTOption:
        """:return: the front end option of bootstrap consensus tree."""
        return self._test_front_end_option \
            if self._test_front_end_option is not None \
            else BCTReceiver().options_from_front_end()

    def _get_newick_tree(self,
                         labels: List[str],
                         sample_dtm: pd.DataFrame) -> TreeNode:
        """Get newick tree based on a subset of the DTM.

        :param labels: All file names from the DTM.
        :param sample_dtm: A 80% subset of the complete DTM.
        :return: A newick formatted tree representing the DTM subset.
        """
        # noinspection PyTypeChecker
        # Get the linkage matrix for the sample doc term matrix.
        linkage_matrix = linkage(
            sample_dtm.values,
            metric=self._bct_option.dist_metric,
            method=self._bct_option.linkage_method
        )

        # Convert linkage matrix to a tree node and return it.
        return TreeNode.from_linkage_matrix(
            linkage_matrix=linkage_matrix,
            id_list=labels
        )

    def _get_bootstrap_trees(self) -> List[TreeNode]:
        """Do bootstrap on the DTM to get a list of newick trees.

        :return: A list of newick formatted tree where each tree was based on
                 a 80% subset of the complete DTM.
        """
        # Get file names, since tree nodes need labels.
        labels = [self._id_temp_label_map[file_id]
                  for file_id in self._doc_term_matrix.index.values]

        # The bootstrap process to get all the trees.
        return [
            self._get_newick_tree(
                sample_dtm=self._doc_term_matrix.sample(
                    axis=1,
                    frac=0.8,
                    random_state=np.random.RandomState()
                ),
                labels=labels,
            )
            for _ in range(self._bct_option.iterations)
        ]

    def _get_bootstrap_consensus_tree(self) -> Phylo:
        """Get the consensus tree.

        :return: The consensus tree of the list of newick trees.
        """
        # Create the StringIO newick tree holder.
        newick_tree = StringIO()

        # Find the consensus tree among all trees.
        consensus_tree = majority_rule(
            trees=self._get_bootstrap_trees(),
            cutoff=self._bct_option.cutoff
        )

        # Grab the tree from the returned list and convert it to newick format.
        consensus_tree[0].write(newick_tree)
        consensus_tree_str = newick_tree.getvalue()

        # Return consensus tree as a ETE tree object.
        return Phylo.read(
            StringIO(consensus_tree_str),
            format="newick"
        )

    def get_bootstrap_consensus_result(self) -> str:
        """Render the bootstrap consensus tree result and save it to images.

        :return: The rendered BCT result file name.
        """
        # Get the formatted consensus tree.
        consensus_tree = self._get_bootstrap_consensus_tree()

        # Draw the consensus tree as a plt object.
        Phylo.draw(
            consensus_tree,
            do_show=False,
            show_confidence=True
        )

        # Adjust the layout of the figure and add a title for it.
        plt.gca().spines["top"].set_visible(False)
        plt.gca().spines["right"].set_visible(False)
        plt.title("Bootstrap Consensus Tree Result")

        # Create a bytes image holder and save figure to it.
        image_holder = BytesIO()
        plt.savefig(image_holder)
        image_holder.seek(0)

        # Encode the image.
        image = base64.b64encode(b''.join(image_holder))

        # Decode image to utf-8 string.
        return image.decode('utf-8')
<commit_msg>A bit more image style settings<commit_after>"""This is a model to produce bootstrap consensus tree of the dtm."""
import base64
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from Bio import Phylo
from io import StringIO, BytesIO
from skbio import TreeNode
from skbio.tree import majority_rule
from scipy.cluster.hierarchy import linkage
from typing import NamedTuple, Optional, List
from lexos.models.base_model import BaseModel
from lexos.models.matrix_model import MatrixModel, IdTempLabelMap
from lexos.receivers.bct_receiver import BCTOption, BCTReceiver

# Set matplotlib backend to be agg in order to make it work with flask.
plt.switch_backend("agg")

class BCTTestOptions(NamedTuple):
    """A typed tuple to hold test options."""

    doc_term_matrix: pd.DataFrame
    front_end_option: BCTOption
    id_temp_label_map: IdTempLabelMap

class BCTModel(BaseModel):
    """The BCTModel inherits from the BaseModel."""

    def __init__(self, test_options: Optional[BCTTestOptions] = None):
        """Generate bootstrap consensus tree.

        :param test_options: The input used in testing to override the
                             dynamically loaded option.
        """
        super().__init__()
        if test_options is not None:
            self._test_dtm = test_options.doc_term_matrix
            self._test_front_end_option = test_options.front_end_option
            self._test_id_temp_label_map = test_options.id_temp_label_map
        else:
            self._test_dtm = None
            self._test_front_end_option = None
            self._test_id_temp_label_map = None

    @property
    def _doc_term_matrix(self) -> pd.DataFrame:
        """:return: the document term matrix."""
        return self._test_dtm if self._test_dtm is not None \
            else MatrixModel().get_matrix()

    @property
    def _id_temp_label_map(self) -> IdTempLabelMap:
        """:return: a map takes an id to temp labels."""
        return self._test_id_temp_label_map \
            if self._test_id_temp_label_map is not None \
            else MatrixModel().get_id_temp_label_map()

    @property
    def _bct_option(self) -> BCTOption:
        """:return: the front end option of bootstrap consensus tree."""
        return self._test_front_end_option \
            if self._test_front_end_option is not None \
            else BCTReceiver().options_from_front_end()

    def _get_newick_tree(self,
                         labels: List[str],
                         sample_dtm: pd.DataFrame) -> TreeNode:
        """Get newick tree based on a subset of the DTM.

        :param labels: All file names from the DTM.
        :param sample_dtm: A 80% subset of the complete DTM.
        :return: A newick formatted tree representing the DTM subset.
        """
        # noinspection PyTypeChecker
        # Get the linkage matrix for the sample doc term matrix.
        linkage_matrix = linkage(
            sample_dtm.values,
            metric=self._bct_option.dist_metric,
            method=self._bct_option.linkage_method
        )

        # Convert linkage matrix to a tree node and return it.
        return TreeNode.from_linkage_matrix(
            linkage_matrix=linkage_matrix,
            id_list=labels
        )

    def _get_bootstrap_trees(self) -> List[TreeNode]:
        """Do bootstrap on the DTM to get a list of newick trees.

        :return: A list of newick formatted tree where each tree was based on
                 a 80% subset of the complete DTM.
        """
        # Get file names, since tree nodes need labels.
        labels = [self._id_temp_label_map[file_id]
                  for file_id in self._doc_term_matrix.index.values]

        # The bootstrap process to get all the trees.
        return [
            self._get_newick_tree(
                sample_dtm=self._doc_term_matrix.sample(
                    axis=1,
                    frac=0.8,
                    random_state=np.random.RandomState()
                ),
                labels=labels,
            )
            for _ in range(self._bct_option.iterations)
        ]

    def _get_bootstrap_consensus_tree(self) -> Phylo:
        """Get the consensus tree.

        :return: The consensus tree of the list of newick trees.
        """
        # Create the StringIO newick tree holder.
        newick_tree = StringIO()

        # Find the consensus tree among all trees.
        consensus_tree = majority_rule(
            trees=self._get_bootstrap_trees(),
            cutoff=self._bct_option.cutoff
        )

        # Grab the tree from the returned list and convert it to newick format.
        consensus_tree[0].write(newick_tree)
        consensus_tree_str = newick_tree.getvalue()

        # Return consensus tree as a ETE tree object.
        return Phylo.read(
            StringIO(consensus_tree_str),
            format="newick"
        )

    def get_bootstrap_consensus_result(self) -> str:
        """Render the bootstrap consensus tree result and save it to images.

        :return: The rendered BCT result file name.
        """
        # Get the formatted consensus tree.
        consensus_tree = self._get_bootstrap_consensus_tree()

        # Draw the consensus tree as a plt object.
        Phylo.draw(
            consensus_tree,
            do_show=False,
            show_confidence=True
        )

        # Adjust the layout of the figure and add a title for it.
        plt.gca().spines["top"].set_visible(False)
        plt.gca().spines["right"].set_visible(False)
        plt.gcf().set_size_inches(w=10, h=6)
        plt.gcf().subplots_adjust(right=0.8)
        plt.title("Bootstrap Consensus Tree Result")

        # Create a bytes image holder and save figure to it.
        image_holder = BytesIO()
        plt.savefig(image_holder)
        image_holder.seek(0)

        # Encode the image.
        image = base64.b64encode(b''.join(image_holder))

        # Decode image to utf-8 string.
        return image.decode('utf-8')
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# The MIT License (MIT)
# Copyright (c) 2015 Findspire

"""
Django settings for workflow project.

Generated by 'django-admin startproject' using Django 1.8.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
from django.utils.translation import ugettext_lazy as _

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

COMP_STRENGTH_DEFAULT = 5

###############################
# Dev vs Prod options
###############################

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '6og(!94ejbbv6x($=gn&x3ea9ffb!64ev4%3jv(m*uy8-0@f=5'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']

USE_X_FORWARDED_HOST = True

APPEND_SLASH = True

ADMINS = ()
MANAGERS = ADMINS

################################
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
################################

USE_I18N = True

USE_L10N = True

USE_TZ = True

LANGUAGE_CODE = 'en'

LANGUAGES = (
    ('en', _('English')),
    ('fr', _('French')),
)
AVAILABLE_LANGUAGES = dict(LANGUAGES).keys()

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'workflow', 'locale'),
)

TIME_ZONE = 'UTC'

SITE_ID = 1

##################################
# Files location
##################################

MEDIA_ROOT = ''
MEDIA_URL = ''

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_ROOT = ''
STATIC_URL = '/static/'

STATICFILES_DIRS = [
    BASE_DIR + '/workflow/static',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BASE_DIR + '/workflow/templates/',
        ],
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.core.context_processors.request',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
            ],
            'loaders': [
                'hamlpy.template.loaders.HamlPyFilesystemLoader',
                'hamlpy.template.loaders.HamlPyAppDirectoriesLoader',
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ]
        },
    },
]

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

##################################
# Misc
##################################

DEBUG_TOOLBAR_PANELS = [
    #'debug_toolbar.panels.versions.VersionsPanel',
    'debug_toolbar.panels.timer.TimerPanel',
    #'debug_toolbar.panels.settings.SettingsPanel',
    'debug_toolbar.panels.headers.HeadersPanel',
    'debug_toolbar.panels.sql.SQLPanel',
    'debug_toolbar.panels.request.RequestPanel',
    'debug_toolbar.panels.staticfiles.StaticFilesPanel',
    'debug_toolbar.panels.templates.TemplatesPanel',
    'debug_toolbar.panels.cache.CachePanel',
    #'debug_toolbar.panels.signals.SignalsPanel',
    #'debug_toolbar.panels.logging.LoggingPanel',
    #'debug_toolbar.panels.redirects.RedirectsPanel',

    # third-party
    'template_profiler_panel.panels.template.TemplateProfilerPanel',
]

INSTALLED_APPS = (
    # django
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',

    # third party + debug
    'django_user_agents',

    'debug_toolbar',
    'template_profiler_panel',
    'django_extensions',

    # custom
    'workflow.apps.workflow',
    'workflow.apps.team',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',  # first
    'django.middleware.csrf.CsrfViewMiddleware',  # random
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',  # after session
    'django.contrib.auth.middleware.AuthenticationMiddleware',  # after session
    'django.middleware.locale.LocaleMiddleware',  # after session
    'django.middleware.clickjacking.XFrameOptionsMiddleware',

    'django_user_agents.middleware.UserAgentMiddleware',
)

ROOT_URLCONF = 'workflow.urls'

WSGI_APPLICATION = 'workflow.wsgi.application'

HAMLPY_ATTR_WRAPPER = '"'

LOGIN_URL = '/admin/login'

BUG_TRACKER_URL = 'https://redmine.findspire.com/issues/'

############################################
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
############################################

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.dirname(__file__) + '/../workflow.db',
    }
}
<commit_msg>[STATIC] Fix static root dir<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# The MIT License (MIT)
# Copyright (c) 2015 Findspire

"""
Django settings for workflow project.

Generated by 'django-admin startproject' using Django 1.8.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
from django.utils.translation import ugettext_lazy as _

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

COMP_STRENGTH_DEFAULT = 5

###############################
# Dev vs Prod options
###############################

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '6og(!94ejbbv6x($=gn&x3ea9ffb!64ev4%3jv(m*uy8-0@f=5'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']

USE_X_FORWARDED_HOST = True

APPEND_SLASH = True

ADMINS = ()
MANAGERS = ADMINS

################################
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
################################

USE_I18N = True

USE_L10N = True

USE_TZ = True

LANGUAGE_CODE = 'en'

LANGUAGES = (
    ('en', _('English')),
    ('fr', _('French')),
)
AVAILABLE_LANGUAGES = dict(LANGUAGES).keys()

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'workflow', 'locale'),
)

TIME_ZONE = 'UTC'

SITE_ID = 1

##################################
# Files location
##################################

MEDIA_ROOT = ''
MEDIA_URL = ''

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

STATICFILES_DIRS = [
    BASE_DIR + '/workflow/static',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BASE_DIR + '/workflow/templates/',
        ],
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.core.context_processors.request',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
            ],
            'loaders': [
                'hamlpy.template.loaders.HamlPyFilesystemLoader',
                'hamlpy.template.loaders.HamlPyAppDirectoriesLoader',
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ]
        },
    },
]

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

##################################
# Misc
##################################

DEBUG_TOOLBAR_PANELS = [
    #'debug_toolbar.panels.versions.VersionsPanel',
    'debug_toolbar.panels.timer.TimerPanel',
    #'debug_toolbar.panels.settings.SettingsPanel',
    'debug_toolbar.panels.headers.HeadersPanel',
    'debug_toolbar.panels.sql.SQLPanel',
    'debug_toolbar.panels.request.RequestPanel',
    'debug_toolbar.panels.staticfiles.StaticFilesPanel',
    'debug_toolbar.panels.templates.TemplatesPanel',
    'debug_toolbar.panels.cache.CachePanel',
    #'debug_toolbar.panels.signals.SignalsPanel',
    #'debug_toolbar.panels.logging.LoggingPanel',
    #'debug_toolbar.panels.redirects.RedirectsPanel',

    # third-party
    'template_profiler_panel.panels.template.TemplateProfilerPanel',
]

INSTALLED_APPS = (
    # django
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',

    # third party + debug
    'django_user_agents',

    'debug_toolbar',
    'template_profiler_panel',
    'django_extensions',

    # custom
    'workflow.apps.workflow',
    'workflow.apps.team',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',  # first
    'django.middleware.csrf.CsrfViewMiddleware',  # random
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',  # after session
    'django.contrib.auth.middleware.AuthenticationMiddleware',  # after session
    'django.middleware.locale.LocaleMiddleware',  # after session
    'django.middleware.clickjacking.XFrameOptionsMiddleware',

    'django_user_agents.middleware.UserAgentMiddleware',
)

ROOT_URLCONF = 'workflow.urls'

WSGI_APPLICATION = 'workflow.wsgi.application'

HAMLPY_ATTR_WRAPPER = '"'

LOGIN_URL = '/admin/login'

BUG_TRACKER_URL = 'https://redmine.findspire.com/issues/'

############################################
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
############################################

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.dirname(__file__) + '/../workflow.db',
    }
}
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from helpers.aspherical_lens import check_point as check_point_asph
from helpers.spherical_lens import check_point as check_point_sph
from helpers.dimensions import mm2px
from helpers.output import output_array_to_file, output_png

size_x = 8000
size_y = 6000
lam = 0.6  # mm
lam_px = 100

n_sph = 3.14
n_asph = 1.5

d_asph = mm2px(15, lam, lam_px)
x_pos_asph = mm2px(2 * lam, lam, lam_px)
y_pos_asph = round(size_y / 2)
diameter_asph = mm2px(25.2, lam, lam_px)

delta = mm2px(5, lam, lam_px)

radius_sph = mm2px(10, lam, lam_px)
d_sph = mm2px(10 - lam / 2, lam, lam_px)
x_pos_sph = x_pos_asph + d_asph + delta
y_pos_sph = round(size_y / 2)

eps = []

print('Start proccessing epsilon')

for y in range(size_y):
    print('Proccessing [{}/{}]'.format(y, size_y - 1), end="\r")
    for x in range(size_x):
        if check_point_asph(x - x_pos_asph, y - y_pos_asph, d_asph, diameter_asph, lam, lam_px):
            eps.append(n_asph)
        elif check_point_sph(x - x_pos_sph, y - y_pos_sph, radius_sph, d_sph, 10000000):
            eps.append(n_sph)
        else:
            eps.append(1)

print('Finish proccessing epsilon')

print('Start writing epsilon')
output_array_to_file(eps, 'epsilon.txt', size_x)
print('Finish writing epsilon')
<commit_msg>add world<commit_after># -*- coding: utf-8 -*-
from .helpers.aspherical_lens import check_point as check_point_asph
from .helpers.spherical_lens import check_point as check_point_sph
from .helpers.dimensions import mm2px
from .helpers.output import output_array_to_file, output_png

size_x = 8000
size_y = 6000
lam = 0.6  # mm
lam_px = 100

n_sph = 3.14
n_asph = 1.5

d_asph = mm2px(15, lam, lam_px)
x_pos_asph = mm2px(2 * lam, lam, lam_px)
y_pos_asph = round(size_y / 2)
diameter_asph = mm2px(25.2, lam, lam_px)

delta = mm2px(5, lam, lam_px)

radius_sph = mm2px(10, lam, lam_px)
d_sph = mm2px(10 - lam / 2, lam, lam_px)
x_pos_sph = x_pos_asph + d_asph + delta
y_pos_sph = round(size_y / 2)

eps = []

print('Start proccessing epsilon')

for y in range(size_y):
    print('Proccessing [{}/{}]'.format(y, size_y - 1), end="\r")
    for x in range(size_x):
        if check_point_asph(x - x_pos_asph, y - y_pos_asph, d_asph, diameter_asph, lam, lam_px):
            eps.append(n_asph)
        elif check_point_sph(x - x_pos_sph, y - y_pos_sph, radius_sph, d_sph, 10000000):
            eps.append(n_sph)
        else:
            eps.append(1)

print('Finish proccessing epsilon')

print('Start writing epsilon')
output_array_to_file(eps, 'epsilon.txt', size_x)
print('Finish writing epsilon')
<|endoftext|>


<commit_before>
"""
    Dataset slicing test module.

    Tests all supported slicing operations, including read/write and
    broadcasting operations.  Does not test type conversion except for
    corner cases overlapping with slicing; for example, when selecting
    specific fields of a compound type.
"""

import numpy as np

from .common import ut, TestCase

import h5py
from h5py.highlevel import File

class BaseSlicing(TestCase):

    def setUp(self):
        self.f = File(self.mktemp(), 'w')

    def tearDown(self):
        if self.f:
            self.f.close()

class TestSingleElement(BaseSlicing):

    """
        Feature: Retrieving a single element works with NumPy semantics
    """

    def test_single_index(self):
        """ Single-element selection with [index] yields array scalar """
        dset = self.f.create_dataset('x', (1,), dtype='i1')
        out = dset[0]
        self.assertIsInstance(out, np.int8)

    def test_single_null(self):
        """ Single-element selection with [()] yields ndarray """
        dset = self.f.create_dataset('x', (1,), dtype='i1')
        out = dset[()]
        self.assertIsInstance(out, np.ndarray)
        self.assertEqual(out.shape, (1,))

    def test_scalar_index(self):
        """ Slicing with [...] yields scalar ndarray """
        dset = self.f.create_dataset('x', shape=(), dtype='f')
        out = dset[...]
        self.assertIsInstance(out, np.ndarray)
        self.assertEqual(out.shape, ())

    def test_scalar_null(self):
        """ Slicing with [()] yields array scalar """
        dset = self.f.create_dataset('x', shape=(), dtype='i1')
        out = dset[()]
        self.assertIsInstance(out, np.int8)

    def test_compound(self):
        """ Compound scalar is numpy.void, not tuple (issue 135) """
        dt = np.dtype([('a','i4'),('b','f8')])
        v = np.ones((4,), dtype=dt)
        dset = self.f.create_dataset('foo', (4,), data=v)
        self.assertEqual(dset[0], v[0])
        self.assertIsInstance(dset[0], np.void)

class TestObjectIndex(BaseSlicing):

    """
        Feauture: numpy.object_ subtypes map to real Python objects
    """

    def test_reference(self):
        """ Indexing a reference dataset returns a h5py.Reference instance """
        dset = self.f.create_dataset('x', (1,), dtype=h5py.special_dtype(ref=h5py.Reference))
        dset[0] = self.f.ref
        self.assertEqual(type(dset[0]), h5py.Reference)

    def test_regref(self):
        """ Indexing a region reference dataset returns a h5py.RegionReference
        """
        dset1 = self.f.create_dataset('x', (10,10))
        regref = dset1.regionref[...]
        dset2 = self.f.create_dataset('y', (1,), dtype=h5py.special_dtype(ref=h5py.RegionReference))
        dset2[0] = regref
        self.assertEqual(type(dset2[0]), h5py.RegionReference)

    def test_reference_field(self):
        """ Compound types of which a reference is an element work right """
        reftype = h5py.special_dtype(ref=h5py.Reference)
        dt = np.dtype([('a', 'i'),('b',reftype)])

        dset = self.f.create_dataset('x', (1,), dtype=dt)
        dset[0] = (42, self.f['/'].ref)

        out = dset[0]
        self.assertEqual(type(out[1]), h5py.Reference)  # isinstance does NOT work

    def test_scalar(self):
        """ Indexing returns a real Python object on scalar datasets """
        dset = self.f.create_dataset('x', (), dtype=h5py.special_dtype(ref=h5py.Reference))
        dset[()] = self.f.ref
        self.assertEqual(type(dset[()]), h5py.Reference)

    def test_bytestr(self):
        """ Indexing a byte string dataset returns a real python byte string
        """
        dset = self.f.create_dataset('x', (1,), dtype=h5py.special_dtype(vlen=bytes))
        dset[0] = b"Hello there!"
        self.assertEqual(type(dset[0]), bytes)

class TestSimpleSlicing(TestCase):

    """
        Feature: Simple NumPy-style slices (start:stop:step) are supported.
    """

    def setUp(self):
        self.f = File(self.mktemp(), 'w')
        self.arr = np.arange(10)
        self.dset = self.f.create_dataset('x', data=self.arr)

    def tearDown(self):
        if self.f:
            self.f.close()

    def test_negative_stop(self):
        """ Negative stop indexes work as they do in NumPy """
        self.assertArrayEqual(self.dset[2:-2], self.arr[2:-2])

<commit_msg>Add tests for issue 211<commit_after>
"""
    Dataset slicing test module.

    Tests all supported slicing operations, including read/write and
    broadcasting operations.  Does not test type conversion except for
    corner cases overlapping with slicing; for example, when selecting
    specific fields of a compound type.
"""

import numpy as np

from .common import ut, TestCase

import h5py
from h5py.highlevel import File

class BaseSlicing(TestCase):

    def setUp(self):
        self.f = File(self.mktemp(), 'w')

    def tearDown(self):
        if self.f:
            self.f.close()

class TestSingleElement(BaseSlicing):

    """
        Feature: Retrieving a single element works with NumPy semantics
    """

    def test_single_index(self):
        """ Single-element selection with [index] yields array scalar """
        dset = self.f.create_dataset('x', (1,), dtype='i1')
        out = dset[0]
        self.assertIsInstance(out, np.int8)

    def test_single_null(self):
        """ Single-element selection with [()] yields ndarray """
        dset = self.f.create_dataset('x', (1,), dtype='i1')
        out = dset[()]
        self.assertIsInstance(out, np.ndarray)
        self.assertEqual(out.shape, (1,))

    def test_scalar_index(self):
        """ Slicing with [...] yields scalar ndarray """
        dset = self.f.create_dataset('x', shape=(), dtype='f')
        out = dset[...]
        self.assertIsInstance(out, np.ndarray)
        self.assertEqual(out.shape, ())

    def test_scalar_null(self):
        """ Slicing with [()] yields array scalar """
        dset = self.f.create_dataset('x', shape=(), dtype='i1')
        out = dset[()]
        self.assertIsInstance(out, np.int8)

    def test_compound(self):
        """ Compound scalar is numpy.void, not tuple (issue 135) """
        dt = np.dtype([('a','i4'),('b','f8')])
        v = np.ones((4,), dtype=dt)
        dset = self.f.create_dataset('foo', (4,), data=v)
        self.assertEqual(dset[0], v[0])
        self.assertIsInstance(dset[0], np.void)

class TestObjectIndex(BaseSlicing):

    """
        Feauture: numpy.object_ subtypes map to real Python objects
    """

    def test_reference(self):
        """ Indexing a reference dataset returns a h5py.Reference instance """
        dset = self.f.create_dataset('x', (1,), dtype=h5py.special_dtype(ref=h5py.Reference))
        dset[0] = self.f.ref
        self.assertEqual(type(dset[0]), h5py.Reference)

    def test_regref(self):
        """ Indexing a region reference dataset returns a h5py.RegionReference
        """
        dset1 = self.f.create_dataset('x', (10,10))
        regref = dset1.regionref[...]
        dset2 = self.f.create_dataset('y', (1,), dtype=h5py.special_dtype(ref=h5py.RegionReference))
        dset2[0] = regref
        self.assertEqual(type(dset2[0]), h5py.RegionReference)

    def test_reference_field(self):
        """ Compound types of which a reference is an element work right """
        reftype = h5py.special_dtype(ref=h5py.Reference)
        dt = np.dtype([('a', 'i'),('b',reftype)])

        dset = self.f.create_dataset('x', (1,), dtype=dt)
        dset[0] = (42, self.f['/'].ref)

        out = dset[0]
        self.assertEqual(type(out[1]), h5py.Reference)  # isinstance does NOT work

    def test_scalar(self):
        """ Indexing returns a real Python object on scalar datasets """
        dset = self.f.create_dataset('x', (), dtype=h5py.special_dtype(ref=h5py.Reference))
        dset[()] = self.f.ref
        self.assertEqual(type(dset[()]), h5py.Reference)

    def test_bytestr(self):
        """ Indexing a byte string dataset returns a real python byte string
        """
        dset = self.f.create_dataset('x', (1,), dtype=h5py.special_dtype(vlen=bytes))
        dset[0] = b"Hello there!"
        self.assertEqual(type(dset[0]), bytes)

class TestSimpleSlicing(TestCase):

    """
        Feature: Simple NumPy-style slices (start:stop:step) are supported.
    """

    def setUp(self):
        self.f = File(self.mktemp(), 'w')
        self.arr = np.arange(10)
        self.dset = self.f.create_dataset('x', data=self.arr)

    def tearDown(self):
        if self.f:
            self.f.close()

    def test_negative_stop(self):
        """ Negative stop indexes work as they do in NumPy """
        self.assertArrayEqual(self.dset[2:-2], self.arr[2:-2])

class TestArraySlicing(BaseSlicing):

    """
        Feature: Array types are handled appropriately
    """

    def test_read(self):
        """ Read arrays tack array dimensions onto end of shape tuple """
        dt = np.dtype('(3,)f8')
        dset = self.f.create_dataset('x',(10,),dtype=dt)
        self.assertEqual(dset.shape, (10,))
        self.assertEqual(dset.dtype, dt)

        # Full read
        out = dset[...]
        self.assertEqual(out.dtype, np.dtype('f8'))
        self.assertEqual(out.shape, (10,3))

        # Single element
        out = dset[0]
        self.assertEqual(out.dtype, np.dtype('f8'))
        self.assertEqual(out.shape, (3,))

        # Range
        out = dset[2:8:2]
        self.assertEqual(out.dtype, np.dtype('f8'))
        self.assertEqual(out.shape, (3,3))

    @ut.expectedFailure
    def test_write_broadcast(self):
        """ Fill an array type with a constant

        Issue 211.
        """
        dt = np.dtype('(3,)f8')
        dset = self.f.create_dataset('x', (10,), dtype=dt)
        dset[...] = 42.0
        out = dset[...]
        self.assertTrue(np.all(out == 42))

    @ut.expectedFailure
    def test_write_element(self):
        """ Write a single element to the array

        Issue 211.
        """
        dt = np.dtype('(3,)f8')
        dset = self.f.create_dataset('x', (10,), dtype=dt)

        data = np.array([1,2,3.0])
        dset[4] = data

        out = dset[4]
        self.assertTrue(np.all(out == data))

    @ut.expectedFailure
    def test_roundtrip(self):
        """ Read the contents of an array and write them back

        Issue 211.
        """
        dt = np.dtype('(3,)f8')
        dset = self.f.create_dataset('x', (10,), dtype=dt)

        out = dset[...]
        dset[...] = out

        self.assertTrue(np.all(dset[...] == out))

<|endoftext|>


<commit_before>from typing import Any, Callable, Union

import flask
from flask import blueprints

AppOrBlueprint = Union[flask.Flask, blueprints.Blueprint]
Initializer = Callable[[], Any]

class _AppcontextVariable:
    """Wrapper for flask.g cached variables pattern."""

    def __init__(self, app: AppOrBlueprint, name: str, init: Initializer):
        self._app = app
        self._name = name
        self._init = init

    @property
    def _value(self):
        return getattr(flask.g, self._name, None)

    @_value.setter
    def _value(self, value):
        setattr(flask.g, self._name, value)

    def __call__(self):
        """Initializes value if necessary and returns it."""
        value = self._value
        if value is None:
            value = self._init()
            self._value = value
        return value

    def teardown(self, closer):
        """Registers closer.

        Calls closer(value) on context destruction if the value was created.
        """
        def actual_closer(exception):
            value = self._value
            if value is not None:
                closer(value)

        def register_closer(setup: blueprints.BlueprintSetupState):
            setup.app.teardown_appcontext(actual_closer)
        if hasattr(self._app, 'record'):
            self._app.record(register_closer)
        elif hasattr(self._app, 'teardown_appcontext'):
            self._app.teardown_appcontext(actual_closer)
        else:
            raise AttributeError(
                'teardown is not supported by', type(self._app))

def appcontext_var(
    app: flask.Flask
) -> Callable[[Initializer], _AppcontextVariable]:
    """Wraps appcontext variable initializer.

    Example:
    @flaskutil.appcontext_var(app)
    def mydb():
        return MyDB()

    @mydb.teardown
    def closedb(db):
        db.close()
    """
    def decorator(initializer: Initializer) -> _AppcontextVariable:
        return _AppcontextVariable(app, initializer.__name__, initializer)
    return decorator
<commit_msg>Use correct type for appcontext_var() parameter<commit_after>from typing import Any, Callable, Union

import flask
from flask import blueprints

AppOrBlueprint = Union[flask.Flask, blueprints.Blueprint]
Initializer = Callable[[], Any]

class _AppcontextVariable:
    """Wrapper for flask.g cached variables pattern."""

    def __init__(self, app: AppOrBlueprint, name: str, init: Initializer):
        self._app = app
        self._name = name
        self._init = init

    @property
    def _value(self):
        return getattr(flask.g, self._name, None)

    @_value.setter
    def _value(self, value):
        setattr(flask.g, self._name, value)

    def __call__(self):
        """Initializes value if necessary and returns it."""
        value = self._value
        if value is None:
            value = self._init()
            self._value = value
        return value

    def teardown(self, closer):
        """Registers closer.

        Calls closer(value) on context destruction if the value was created.
        """
        def actual_closer(exception):
            value = self._value
            if value is not None:
                closer(value)

        def register_closer(setup: blueprints.BlueprintSetupState):
            setup.app.teardown_appcontext(actual_closer)
        if hasattr(self._app, 'record'):
            self._app.record(register_closer)
        elif hasattr(self._app, 'teardown_appcontext'):
            self._app.teardown_appcontext(actual_closer)
        else:
            raise AttributeError(
                'teardown is not supported by', type(self._app))

def appcontext_var(
    app: AppOrBlueprint
) -> Callable[[Initializer], _AppcontextVariable]:
    """Wraps appcontext variable initializer.

    Example:
    @flaskutil.appcontext_var(app)
    def mydb():
        return MyDB()

    @mydb.teardown
    def closedb(db):
        db.close()
    """
    def decorator(initializer: Initializer) -> _AppcontextVariable:
        return _AppcontextVariable(app, initializer.__name__, initializer)
    return decorator
<|endoftext|>


<commit_before>#  Licensed under the Apache License, Version 2.0 (the "License"); you may
#  not use this file except in compliance with the License. You may obtain
#  a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#  License for the specific language governing permissions and limitations
#  under the License.

import re
import tokenize

from hacking import core

FORMAT_RE = re.compile("%(?:"
                       "%|"           # Ignore plain percents
                       "(\(\w+\))?"   # mapping key
                       "([#0 +-]?"    # flag
                       "(?:\d+|\*)?"  # width
                       "(?:\.\d+)?"   # precision
                       "[hlL]?"       # length mod
                       "\w))")        # type

class LocalizationError(Exception):
    pass

def check_i18n():
    """Generator that checks token stream for localization errors.

    Expects tokens to be ``send``ed one by one.
    Raises LocalizationError if some error is found.
    """
    while True:
        try:
            token_type, text, _, _, line = yield
        except GeneratorExit:
            return

        if text == "def" and token_type == tokenize.NAME:
            # explicitly ignore function definitions, as oslo defines these
            return
        if (token_type == tokenize.NAME and
                text in ["_", "_LI", "_LW", "_LE", "_LC"]):

            while True:
                token_type, text, start, _, _ = yield
                if token_type != tokenize.NL:
                    break
            if token_type != tokenize.OP or text != "(":
                continue  # not a localization call

            format_string = ''
            while True:
                token_type, text, start, _, _ = yield
                if token_type == tokenize.STRING:
                    format_string += eval(text)
                elif token_type == tokenize.NL:
                    pass
                else:
                    break

            if not format_string:
                raise LocalizationError(
                    start, "H701: Empty localization string")
            if token_type != tokenize.OP:
                raise LocalizationError(
                    start, "H701: Invalid localization call")
            if text != ")":
                if text == "%":
                    raise LocalizationError(
                        start,
                        "H702: Formatting operation should be outside"
                        " of localization method call")
                elif text == "+":
                    raise LocalizationError(
                        start,
                        "H702: Use bare string concatenation instead of +")
                else:
                    raise LocalizationError(
                        start, "H702: Argument to _, _LI, _LW, _LC, or _LE "
                        "must be just a string")

            format_specs = FORMAT_RE.findall(format_string)
            positional_specs = [(key, spec) for key, spec in format_specs
                                if not key and spec]
            # not spec means %%, key means %(smth)s
            if len(positional_specs) > 1:
                raise LocalizationError(
                    start, "H703: Multiple positional placeholders")

@core.flake8ext
def hacking_localization_strings(logical_line, tokens, noqa):
    r"""Check localization in line.

    Okay: _("This is fine")
    Okay: _LI("This is fine")
    Okay: _LW("This is fine")
    Okay: _LE("This is fine")
    Okay: _LC("This is fine")
    Okay: _("This is also fine %s")
    Okay: _("So is this %s, %(foo)s") % {foo: 'foo'}
    H701: _('')
    Okay: def _(msg):\n    pass
    Okay: def _LE(msg):\n    pass
    H701: _LI('')
    H701: _LW('')
    H701: _LE('')
    H701: _LC('')
    Okay: _('')  # noqa
    H702: _("Bob" + " foo")
    H702: _LI("Bob" + " foo")
    H702: _LW("Bob" + " foo")
    H702: _LE("Bob" + " foo")
    H702: _LC("Bob" + " foo")
    Okay: _("Bob" + " foo")  # noqa
    H702: _("Bob %s" % foo)
    H702: _LI("Bob %s" % foo)
    H702: _LW("Bob %s" % foo)
    H702: _LE("Bob %s" % foo)
    H702: _LC("Bob %s" % foo)
    H702: _("%s %s" % (foo, bar))
    H703: _("%s %s") % (foo, bar)
    """
    if noqa:
        return
    gen = check_i18n()
    next(gen)
    try:
        list(map(gen.send, tokens))
        gen.close()
    except LocalizationError as e:
        yield e.args

# TODO(jogo) Dict and list objects
<commit_msg>Fix python 3.6 escape char warnings in strings<commit_after>#  Licensed under the Apache License, Version 2.0 (the "License"); you may
#  not use this file except in compliance with the License. You may obtain
#  a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#  License for the specific language governing permissions and limitations
#  under the License.

import re
import tokenize

from hacking import core

FORMAT_RE = re.compile(r"%(?:"
                       r"%|"           # Ignore plain percents
                       r"(\(\w+\))?"   # mapping key
                       r"([#0 +-]?"    # flag
                       r"(?:\d+|\*)?"  # width
                       r"(?:\.\d+)?"   # precision
                       r"[hlL]?"       # length mod
                       r"\w))")        # type

class LocalizationError(Exception):
    pass

def check_i18n():
    """Generator that checks token stream for localization errors.

    Expects tokens to be ``send``ed one by one.
    Raises LocalizationError if some error is found.
    """
    while True:
        try:
            token_type, text, _, _, line = yield
        except GeneratorExit:
            return

        if text == "def" and token_type == tokenize.NAME:
            # explicitly ignore function definitions, as oslo defines these
            return
        if (token_type == tokenize.NAME and
                text in ["_", "_LI", "_LW", "_LE", "_LC"]):

            while True:
                token_type, text, start, _, _ = yield
                if token_type != tokenize.NL:
                    break
            if token_type != tokenize.OP or text != "(":
                continue  # not a localization call

            format_string = ''
            while True:
                token_type, text, start, _, _ = yield
                if token_type == tokenize.STRING:
                    format_string += eval(text)
                elif token_type == tokenize.NL:
                    pass
                else:
                    break

            if not format_string:
                raise LocalizationError(
                    start, "H701: Empty localization string")
            if token_type != tokenize.OP:
                raise LocalizationError(
                    start, "H701: Invalid localization call")
            if text != ")":
                if text == "%":
                    raise LocalizationError(
                        start,
                        "H702: Formatting operation should be outside"
                        " of localization method call")
                elif text == "+":
                    raise LocalizationError(
                        start,
                        "H702: Use bare string concatenation instead of +")
                else:
                    raise LocalizationError(
                        start, "H702: Argument to _, _LI, _LW, _LC, or _LE "
                        "must be just a string")

            format_specs = FORMAT_RE.findall(format_string)
            positional_specs = [(key, spec) for key, spec in format_specs
                                if not key and spec]
            # not spec means %%, key means %(smth)s
            if len(positional_specs) > 1:
                raise LocalizationError(
                    start, "H703: Multiple positional placeholders")

@core.flake8ext
def hacking_localization_strings(logical_line, tokens, noqa):
    r"""Check localization in line.

    Okay: _("This is fine")
    Okay: _LI("This is fine")
    Okay: _LW("This is fine")
    Okay: _LE("This is fine")
    Okay: _LC("This is fine")
    Okay: _("This is also fine %s")
    Okay: _("So is this %s, %(foo)s") % {foo: 'foo'}
    H701: _('')
    Okay: def _(msg):\n    pass
    Okay: def _LE(msg):\n    pass
    H701: _LI('')
    H701: _LW('')
    H701: _LE('')
    H701: _LC('')
    Okay: _('')  # noqa
    H702: _("Bob" + " foo")
    H702: _LI("Bob" + " foo")
    H702: _LW("Bob" + " foo")
    H702: _LE("Bob" + " foo")
    H702: _LC("Bob" + " foo")
    Okay: _("Bob" + " foo")  # noqa
    H702: _("Bob %s" % foo)
    H702: _LI("Bob %s" % foo)
    H702: _LW("Bob %s" % foo)
    H702: _LE("Bob %s" % foo)
    H702: _LC("Bob %s" % foo)
    H702: _("%s %s" % (foo, bar))
    H703: _("%s %s") % (foo, bar)
    """
    if noqa:
        return
    gen = check_i18n()
    next(gen)
    try:
        list(map(gen.send, tokens))
        gen.close()
    except LocalizationError as e:
        yield e.args

# TODO(jogo) Dict and list objects
<|endoftext|>


<commit_before>from .settings import *

DEBUG = False

import dj_database_url
DATABASES['default'] =  dj_database_url.config()

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers
ALLOWED_HOSTS = ['*']

# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
<commit_msg>fix: heroku static background not displayed #2<commit_after>from .settings import *

DEBUG = False

import dj_database_url
DATABASES['default'] =  dj_database_url.config()

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers
ALLOWED_HOSTS = ['*']

# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
<|endoftext|>


<commit_before>from django.db import models
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse

from wye.base.constants import (
    FeedbackType,
    WorkshopAction,
    WorkshopLevel,
    WorkshopStatus,
)
from wye.regions.models import Location
from wye.base.models import TimeAuditModel
from wye.organisations.models import Organisation

from .decorators import validate_action_param

# class WorkshopLevel(TimeAuditModel):
#     '''
#     Beginners, Intermediate, Advance
#     '''
#     name = models.CharField(max_length=300, unique=True)
#
#     class Meta:
#         db_table = 'workshop_level'
#
#     def __str__(self):
#         return '{}'.format(self.name)

class WorkshopSections(TimeAuditModel):
    '''
    python2, Python3, Django, Flask, Gaming
    '''
    name = models.CharField(max_length=300, unique=True)

    class Meta:
        db_table = 'workshop_section'

    def __str__(self):
        return '{}'.format(self.name)

class Workshop(TimeAuditModel):
    no_of_participants = models.PositiveIntegerField()
    expected_date = models.DateField()
    description = models.TextField()
    requester = models.ForeignKey(
        Organisation, related_name='workshop_requester')
    presenter = models.ManyToManyField(User, related_name='workshop_presenter')
    location = models.ForeignKey(Location, related_name='workshop_location')
    workshop_level = models.PositiveSmallIntegerField(
        choices=WorkshopLevel.CHOICES, verbose_name="Workshop Level")
    workshop_section = models.ForeignKey(WorkshopSections)
    is_active = models.BooleanField(default=True)
    status = models.PositiveSmallIntegerField(
        choices=WorkshopStatus.CHOICES, verbose_name="Current Status",
        default=WorkshopStatus.DRAFT)

    class Meta:
        db_table = 'workshops'

    def __str__(self):
        return '{}-{}'.format(self.requester, self.workshop_section)

    def is_presenter(self, user):
        return self.presenter.filter(pk=user.pk).exists()

    def is_organiser(self, user):
        return self.requester.user.filter(pk=user.pk).exists()

    @validate_action_param(WorkshopAction.ACTIVE)
    def toggle_active(self, user, **kwargs):
        """
        Helper method to toggle is_active for the model.
        """

        action_map = {'active': True, 'deactive': False}
        action = kwargs.get('action')
        self.is_active = action_map.get(action)
        self.save()
        return {
            'status': True,
            'msg': 'Workshop successfully updated.'}

    @validate_action_param(WorkshopAction.ASSIGNME)
    def assign_me(self, user, **kwargs):
        """
        Method to assign workshop by presenter self.
        """

        # if workshop completed don't accept
        # presenter.
        if self.status == WorkshopStatus.COMPLETED:
            return {
                'status': False,
                'msg': 'Sorry, but it would seem that this \
                        workshop is already completed and hence \
                        won\'t be able to accept a presenter.'}

        action_map = {
            'opt-in': self.presenter.add,
            'opt-out': self.presenter.remove
        }
        message_map = {
            'opt-in': 'Assigned successfully.',
            'opt-out': 'Unassigned successfully.'
        }
        assigned = {
            'opt-in': True,
            'opt-out': False
        }
        action = kwargs.get('action')
        if assigned[action] and self.presenter.filter(pk=user.pk).exists():
            return {
                'status': False,
                'msg': 'Workshop has already been assigned.'
            }

        func = action_map.get(action)
        func(user)
        return {
            'status': True,
            'assigned': assigned[action],
            'msg': message_map[action]
        }

    def get_presenter_list(self):
        return [user.get_full_name() for user in self.presenter.all()]

    def get_tweet(self, context):
        workshop = self
        date = workshop.expected_date
        topic = workshop.workshop_section
        organization = workshop.requester
        workshop_url = context.get('workshop_url', None)
        message = "{} workshop at {} on {} confirmed! Details at {}".format(
            topic, organization, date, workshop_url)
        if len(message) >= 140:
            message = "{} workshop on {} confirmed! Details at {}".format(
                topic, date, workshop_url)

        return message

class WorkshopRatingValues(TimeAuditModel):
    '''
    Requesting Rating values -2, -1, 0 , 1, 2
    '''

    name = models.CharField(max_length=300)

    class Meta:
        db_table = 'workshop_vote_value'

    def __str__(self):
        return '{}'.format(self.name)

    @classmethod
    def get_questions(cls):
        return cls.objects.values('name', 'pk')

class WorkshopFeedBack(TimeAuditModel):
    '''
    Requesting for Feedback from requester and Presenter
    '''

    workshop = models.ForeignKey(Workshop)
    comment = models.TextField()
    feedback_type = models.PositiveSmallIntegerField(
        choices=FeedbackType.CHOICES, verbose_name="User_type")

    class Meta:
        db_table = 'workshop_feedback'

    def __str__(self):
        return '{}'.format(self.workshop)

    @classmethod
    def save_feedback(cls, user, workshop_id, **kwargs):
        workshop = Workshop.objects.get(pk=workshop_id)
        presenter = workshop.is_presenter(user)
        organiser = workshop.is_organiser(user)
        comment = kwargs.get('comment', '')
        del kwargs['comment']

        if presenter:
            feedback_type = FeedbackType.PRESENTER
        elif organiser:
            feedback_type = FeedbackType.ORGANISATION

        workshop_feedback = cls.objects.create(
            workshop=workshop,
            comment=comment,
            feedback_type=feedback_type
        )
        WorkshopVoting.save_rating(workshop_feedback, **kwargs)

class WorkshopVoting(TimeAuditModel):
    workshop_feedback = models.ForeignKey(
        WorkshopFeedBack, related_name='workshop_feedback')
    workshop_rating = models.ForeignKey(
        WorkshopRatingValues, related_name='workshop_rating')
    rating = models.IntegerField()

    class Meta:
        db_table = 'workshop_votes'

    def __str__(self):
        return '{}-{}-{}'.format(self.workshop_feedback,
                                 self.workshop_rating,
                                 self.rating)

    @classmethod
    def save_rating(cls, workshop_feedback, **kwargs):
        object_list = [
            cls(workshop_feedback=workshop_feedback,
                workshop_rating_id=int(k), rating=v)
            for k, v in kwargs.iteritems()
        ]

        cls.objects.bulk_create(object_list)
<commit_msg>Removes unwanted imports and fixes formatting<commit_after>from django.db import models
from django.contrib.auth.models import User

from wye.base.constants import (
    FeedbackType,
    WorkshopAction,
    WorkshopLevel,
    WorkshopStatus,
)
from wye.regions.models import Location
from wye.base.models import TimeAuditModel
from wye.organisations.models import Organisation

from .decorators import validate_action_param

# class WorkshopLevel(TimeAuditModel):
#     '''
#     Beginners, Intermediate, Advance
#     '''
#     name = models.CharField(max_length=300, unique=True)
#
#     class Meta:
#         db_table = 'workshop_level'
#
#     def __str__(self):
#         return '{}'.format(self.name)

class WorkshopSections(TimeAuditModel):
    '''
    python2, Python3, Django, Flask, Gaming
    '''
    name = models.CharField(max_length=300, unique=True)

    class Meta:
        db_table = 'workshop_section'

    def __str__(self):
        return '{}'.format(self.name)

class Workshop(TimeAuditModel):
    no_of_participants = models.PositiveIntegerField()
    expected_date = models.DateField()
    description = models.TextField()
    requester = models.ForeignKey(
        Organisation, related_name='workshop_requester')
    presenter = models.ManyToManyField(User, related_name='workshop_presenter')
    location = models.ForeignKey(Location, related_name='workshop_location')
    workshop_level = models.PositiveSmallIntegerField(
        choices=WorkshopLevel.CHOICES, verbose_name="Workshop Level")
    workshop_section = models.ForeignKey(WorkshopSections)
    is_active = models.BooleanField(default=True)
    status = models.PositiveSmallIntegerField(
        choices=WorkshopStatus.CHOICES, verbose_name="Current Status",
        default=WorkshopStatus.DRAFT)

    class Meta:
        db_table = 'workshops'

    def __str__(self):
        return '{}-{}'.format(self.requester, self.workshop_section)

    def is_presenter(self, user):
        return self.presenter.filter(pk=user.pk).exists()

    def is_organiser(self, user):
        return self.requester.user.filter(pk=user.pk).exists()

    @validate_action_param(WorkshopAction.ACTIVE)
    def toggle_active(self, user, **kwargs):
        """
        Helper method to toggle is_active for the model.
        """

        action_map = {'active': True, 'deactive': False}
        action = kwargs.get('action')
        self.is_active = action_map.get(action)
        self.save()
        return {
            'status': True,
            'msg': 'Workshop successfully updated.'}

    @validate_action_param(WorkshopAction.ASSIGNME)
    def assign_me(self, user, **kwargs):
        """
        Method to assign workshop by presenter self.
        """

        # if workshop completed don't accept
        # presenter.
        if self.status == WorkshopStatus.COMPLETED:
            return {
                'status': False,
                'msg': 'Sorry, but it would seem that this \
                        workshop is already completed and hence \
                        won\'t be able to accept a presenter.'}

        action_map = {
            'opt-in': self.presenter.add,
            'opt-out': self.presenter.remove
        }
        message_map = {
            'opt-in': 'Assigned successfully.',
            'opt-out': 'Unassigned successfully.'
        }
        assigned = {
            'opt-in': True,
            'opt-out': False
        }
        action = kwargs.get('action')
        if assigned[action] and self.presenter.filter(pk=user.pk).exists():
            return {
                'status': False,
                'msg': 'Workshop has already been assigned.'
            }

        func = action_map.get(action)
        func(user)
        return {
            'status': True,
            'assigned': assigned[action],
            'msg': message_map[action]
        }

    def get_presenter_list(self):
        return [user.get_full_name() for user in self.presenter.all()]

    def get_tweet(self, context):
        workshop = self
        date = workshop.expected_date
        topic = workshop.workshop_section
        organization = workshop.requester
        workshop_url = context.get('workshop_url', None)
        message = "{} workshop at {} on {} confirmed! Details at {}".format(
            topic, organization, date, workshop_url)
        if len(message) >= 140:
            message = "{} workshop on {} confirmed! Details at {}".format(
                topic, date, workshop_url)

        return message

class WorkshopRatingValues(TimeAuditModel):
    '''
    Requesting Rating values -2, -1, 0 , 1, 2
    '''

    name = models.CharField(max_length=300)

    class Meta:
        db_table = 'workshop_vote_value'

    def __str__(self):
        return '{}'.format(self.name)

    @classmethod
    def get_questions(cls):
        return cls.objects.values('name', 'pk')

class WorkshopFeedBack(TimeAuditModel):
    '''
    Requesting for Feedback from requester and Presenter
    '''

    workshop = models.ForeignKey(Workshop)
    comment = models.TextField()
    feedback_type = models.PositiveSmallIntegerField(
        choices=FeedbackType.CHOICES, verbose_name="User_type")

    class Meta:
        db_table = 'workshop_feedback'

    def __str__(self):
        return '{}'.format(self.workshop)

    @classmethod
    def save_feedback(cls, user, workshop_id, **kwargs):
        workshop = Workshop.objects.get(pk=workshop_id)
        presenter = workshop.is_presenter(user)
        organiser = workshop.is_organiser(user)
        comment = kwargs.get('comment', '')
        del kwargs['comment']

        if presenter:
            feedback_type = FeedbackType.PRESENTER
        elif organiser:
            feedback_type = FeedbackType.ORGANISATION

        workshop_feedback = cls.objects.create(
            workshop=workshop,
            comment=comment,
            feedback_type=feedback_type
        )
        WorkshopVoting.save_rating(workshop_feedback, **kwargs)

class WorkshopVoting(TimeAuditModel):
    workshop_feedback = models.ForeignKey(
        WorkshopFeedBack, related_name='workshop_feedback')
    workshop_rating = models.ForeignKey(
        WorkshopRatingValues, related_name='workshop_rating')
    rating = models.IntegerField()

    class Meta:
        db_table = 'workshop_votes'

    def __str__(self):
        return '{}-{}-{}'.format(self.workshop_feedback,
                                 self.workshop_rating,
                                 self.rating)

    @classmethod
    def save_rating(cls, workshop_feedback, **kwargs):
        object_list = [
            cls(workshop_feedback=workshop_feedback,
                workshop_rating_id=int(k), rating=v)
            for k, v in kwargs.iteritems()
        ]

        cls.objects.bulk_create(object_list)
<|endoftext|>


<commit_before>#!/usr/bin/python
#
# HTML source analyzer
# bl4de | bloorq@gmail.com | Twitter: @_bl4de
#
import sys

class _PefOutput:
    Black = '\33[30m'
    Red = '\33[31m'
    Green = '\33[32m'
    Yellow = '\33[33m'
    Blue ='\33[34m'
    Magenta = '\33[35m'
    Cyan = '\33[36m'
    White = '\33[37m'
    _endline = '\33[0m'

    efMsgFound = "exploitable function call"
    efMsgGlobalFound = "global variable explicit call"

_ident = "unknown"

def main():
    _file = open(sys.argv[1], "r")
    i = 0
    print _PefOutput.Green, "=" * 26, "HTML Analyze", "=" * 26
    print "-" * 6, " GitHub: bl4de | Twitter: @_bl4de | bloorq@gmail.com ", "-" * 6, _PefOutput._endline

    print
    for _line in _file:
        i = i + 1
        analyzeLine(_line, i)
        identify(_line)
    showStats(_file, i)

# using osftare recognition
def identify(_line):
    if "Jommla" in _line:
        _ident = "Joomla CMS"
    if "wp-content" in _line:
        _ident = "WordPress CMS"

def showStats(_file, i):
    print _PefOutput.Green, "\n------ SUMMARY -------\n"
    print "total lines of code: %d" % (i)
    print "identified software: %s" %(_ident), _PefOutput._endline
    # end of summary
    print "\n"

# find interesting string(s)
def analyzeLine(_line, i):
    if "<!--" in _line:
        print _PefOutput.White, "line %d:" % (i), _PefOutput.Yellow, "comment found at line %d: %s" % (i, _line.rstrip()), _PefOutput._endline
    if "admin" in _line:
        print _PefOutput.White, "line %d:" % (i), _PefOutput.Red, "'admin' string found at line: %d" % (i), _PefOutput._endline
    if "debug" in _line:
        print _PefOutput.White, "line %d:" % (i), _PefOutput.Red, "debug information found at line %d" % (i), _PefOutput._endline
    if "<script>" in _line:
        print _PefOutput.White, "line %d:" % (i), _PefOutput.Cyan, "inline JavaScript found at line %d" % (i), _PefOutput._endline
    if "\"/" in _line:
        print _PefOutput.White, "line %d:" % (i), _PefOutput.Magenta, "possible directory path found at line %d" % (i), _PefOutput._endline

# main program
if __name__ == "__main__":
    if len(sys.argv) == 2:
        main()
    else:
        print "Enter HTML file name"<commit_msg>[htmlanalyzer.py] reformat code<commit_after>#!/usr/bin/python
#
# HTML source analyzer
# bl4de | bloorq@gmail.com | Twitter: @_bl4de
#
import sys

class _PefOutput:
    Black = '\33[30m'
    Red = '\33[31m'
    Green = '\33[32m'
    Yellow = '\33[33m'
    Blue = '\33[34m'
    Magenta = '\33[35m'
    Cyan = '\33[36m'
    White = '\33[37m'
    _endline = '\33[0m'

    efMsgFound = "exploitable function call"
    efMsgGlobalFound = "global variable explicit call"

_ident = "unknown"

def main():
    _file = open(sys.argv[1], "r")
    i = 0
    print _PefOutput.Green, "=" * 26, "HTML Analyze", "=" * 26
    print "-" * 6, " GitHub: bl4de | Twitter: @_bl4de | bloorq@gmail.com ", "-" * 6, _PefOutput._endline

    print
    for _line in _file:
        i = i + 1
        analyzeLine(_line, i)
        identify(_line)
    showStats(_file, i)

# using osftare recognition
def identify(_line):
    if "Jommla" in _line:
        _ident = "Joomla CMS"
    if "wp-content" in _line:
        _ident = "WordPress CMS"

def showStats(_file, i):
    print _PefOutput.Green, "\n------ SUMMARY -------\n"
    print "total lines of code: %d" % (i)
    print "identified software: %s" % (_ident), _PefOutput._endline
    # end of summary
    print "\n"

# find interesting string(s)
def analyzeLine(_line, i):
    if "<!--" in _line:
        print _PefOutput.White, "line %d:" % (
        i), _PefOutput.Yellow, "comment found at line %d: %s" % (
        i, _line.rstrip()), _PefOutput._endline
    if "admin" in _line:
        print _PefOutput.White, "line %d:" % (
        i), _PefOutput.Red, "'admin' string found at line: %d" % (
        i), _PefOutput._endline
    if "debug" in _line:
        print _PefOutput.White, "line %d:" % (
        i), _PefOutput.Red, "debug information found at line %d" % (
        i), _PefOutput._endline
    if "<script>" in _line:
        print _PefOutput.White, "line %d:" % (
        i), _PefOutput.Cyan, "inline JavaScript found at line %d" % (
        i), _PefOutput._endline
    if "\"/" in _line:
        print _PefOutput.White, "line %d:" % (
        i), _PefOutput.Magenta, "possible directory path found at line %d" % (
        i), _PefOutput._endline

# main program
if __name__ == "__main__":
    if len(sys.argv) == 2:
        main()
    else:
        print "Enter HTML file name"
<|endoftext|>


<commit_before>import numpy as np
import matplotlib.colors as mcolors
from matplotlib.colors import ListedColormap
import json
import os

COLOR_PATH = os.path.join(os.path.dirname(__file__))
with open(COLOR_PATH + '/colormap.json', 'r') as f:
    colors = json.load(f)

def cmap(name, bins=None):
    data = np.array(colors[name])
    data = data / np.max(data)
    cmap = ListedColormap(data, name=name)

    if isinstance(bins, int):
        cmap = cmap._resample(bins)
    return cmap

def subplots(nrow=2, ncol=2, figsize=None, ind=None):
    import cartopy.crs as ccrs

    if figsize is not None:
        fig = plt.figure(figsize=figsize)
    else:
        fig = plt.figure()

    tol = nrow * ncol
    if ind is not None:
        pass
    else:
        ind = np.arange(tol) + 1

    projection = ccrs.PlateCarree()
    ax = [fig.add_subplot(nrow, ncol, i, projection=projection) for i in ind]

    return ax

<commit_msg>add more plot utilities<commit_after>import numpy as np
import matplotlib.colors as mcolors
from matplotlib.colors import ListedColormap
import json
import os

COLOR_PATH = os.path.join(os.path.dirname(__file__))
with open(COLOR_PATH + '/colormap.json', 'r') as f:
    colors = json.load(f)

def cmap(name, bins=None):
    data = np.array(colors[name])
    data = data / np.max(data)
    cmap = ListedColormap(data, name=name)

    if isinstance(bins, int):
        cmap = cmap._resample(bins)
    return cmap

def subplots(nrow=2, ncol=2, figsize=None, ind=None):
    import cartopy.crs as ccrs
    import matplotlib.pyplot as plt

    if figsize is not None:
        fig = plt.figure(figsize=figsize)
    else:
        fig = plt.figure()

    tol = nrow * ncol
    if ind is not None:
        pass
    else:
        ind = np.arange(tol) + 1

    projection = ccrs.PlateCarree()
    ax = [fig.add_subplot(nrow, ncol, i, projection=projection) for i in ind]

    return ax

<|endoftext|>


<commit_before>"""
Update HTPC-Manager from Github. Either through git command or tarball.
Original code by Mikie (https://github.com/Mikie-Ghost/)
"""
import os
import urllib2
import tarfile
import shutil
import platform
import subprocess
import re
from json import loads
import cherrypy
import htpc
import logging

class Updater:
    """ Main class """
    def __init__(self):
        """ Set GitHub constants on load """
        self.user = 'mbw2001'
        self.repo = 'htpc-manager'
        self.branch = 'Updater'
        self.git = 'git'
        self.logger = logging.getLogger('htpc.updater')

    @cherrypy.expose()
    @cherrypy.tools.json_out()
    def index(self):
        """ Handle server requests. Update on POST. Get status on GET. """
        if cherrypy.request.method.upper() == 'POST':
            cherrypy.engine.exit()
            status = self.git_update()
            cherrypy.server.start()
            return status
        else:
            return self.check_update()

    def current(self):
        """ Get hash of current Git commit """
        output = self.git_exec('rev-parse HEAD')
        if re.match('^[a-z0-9]+$', output):
            return output

    def latest(self):
        """ Get hash of latest git commit """
        try:
            url = 'https://api.github.com/repos/%s/%s/commits/%s' % (
                    self.user, self.repo, self.branch)
            result = loads(urllib2.urlopen(url).read())
            return result['sha'].strip()
        except:
            return None

    def behind_by(self, current, latest):
        """ Check how many commits between current and latest """
        try:
            url = 'https://api.github.com/repos/%s/%s/compare/%s...%s' % (
                    self.user, self.repo, current, latest)
            result = loads(urllib2.urlopen(url).read())
            return int(result['total_commits'])
        except Exception, e:
            self.logger.error(str(e))
            return -1

    def check_update(self):
        """ Check for updates """
        self.logger.info("Checking for updates.")
        current = self.current()
        latest = self.latest()
        if current == latest:
            self.logger.info("HTPC-Manager is Up-To-Date.")
            return 0
        else:
            behind = self.behind_by(current, latest)
            if behind == -1:
                return behind
            self.logger.info("Currently " + str(behind) + " commits behind.")
            return (behind, 'https://github.com/%s/%s/compare/%s...%s' % (
                      self.user, self.repo, current, latest))

    def git_update(self):
        """ Do update through git """
        self.logger.info("Attempting update through Git.")
        output = self.git_exec('pull origin %s' % self.branch)

        if not output:
            self.logger.error("Unable to update through git. Make sure that Git is located in your path and can be accessed by this application.")
            return False
        elif line.endswith('Aborting.'):
            self.logger.error("Update aborted.")
            return False

        return True

    def git_exec(self, args):
        """ Tool for running git program on system """
        try:
            proc = subprocess.Popen(self.git + " " +args, stdout=subprocess.PIPE,
                   stderr=subprocess.STDOUT, shell=True, cwd=htpc.RUNDIR)
            output, err = proc.communicate()
        except OSError, e:
            self.logger.warning(str(e))
            return ''

        if err:
            self.logger.warning(output + ' - ' + err)
            return ''
        elif any(s in output for s in ['not found', 'not recognized', 'fatal:']):
            self.logger.warning(output)
            return ''
        else:
            return output.strip()
<commit_msg>Test commit<commit_after>"""
Update HTPC-Manager from Github. Either through git command or tarball.
Original code by Mikie (https://github.com/Mikie-Ghost/)
"""
import os
import urllib2
import tarfile
import shutil
import platform
import subprocess
import re
from json import loads
import cherrypy
import htpc
import logging

class Updater:
    """ Main class """
    def __init__(self):
        """ Set GitHub constants on load """
        self.user = 'mbw2001'
        self.repo = 'htpc-manager'
        self.branch = 'Updater'
        self.git = 'git'
        self.logger = logging.getLogger('htpc.updater')

    @cherrypy.expose()
    @cherrypy.tools.json_out()
    def index(self):
        """ Handle server requests. Update on POST. Get status on GET. """
        if cherrypy.request.method.upper() == 'POST':
            cherrypy.engine.exit()
            status = self.git_update()
            cherrypy.server.start()
            return status
        else:
            return self.check_update()

    def current(self):
        """ Get hash of current Git commit """
        output = self.git_exec('rev-parse HEAD')
        if re.match('^[a-z0-9]+$', output):
            return output

    def latest(self):
        """ Get hash of latest git commit """
        try:
            url = 'https://api.github.com/repos/%s/%s/commits/%s' % (
                    self.user, self.repo, self.branch)
            result = loads(urllib2.urlopen(url).read())
            return result['sha'].strip()
        except:
            return None

    def behind_by(self, current, latest):
        """ Check how many commits between current and latest """
        try:
            url = 'https://api.github.com/repos/%s/%s/compare/%s...%s' % (
                    self.user, self.repo, current, latest)
            result = loads(urllib2.urlopen(url).read())
            return int(result['total_commits'])
        except Exception, e:
            self.logger.error(str(e))
            return -1

    def check_update(self):
        """ Check for updates """
        self.logger.info("Checking for updates.")
        current = self.current()
        latest = self.latest()
        if current == latest:
            self.logger.info("HTPC-Manager is Up-To-Date.")
            return 0
        else:
            behind = self.behind_by(current, latest)
            if behind == -1:
                return behind
            self.logger.info("Currently " + str(behind) + " commits behind.")
            return (behind, 'https://github.com/%s/%s/compare/%s...%s' % (
                      self.user, self.repo, current, latest))

    def git_update(self):
        """ Do update through git """
        self.logger.info("Attempting update through Git.")
        output = self.git_exec('pull origin %s' % self.branch)

        if not output:
            self.logger.error("Unable to update through git. Make sure that Git is located in your path and can be accessed by this application.")
            return False
        elif line.endswith('Aborting.'):
            self.logger.error("Update aborted.")
            return False

        return True

    def git_exec(self, args):
        """ Tool for running git program on system """
        try:
            proc = subprocess.Popen(self.git + " " +args, stdout=subprocess.PIPE,
                   stderr=subprocess.STDOUT, shell=True, cwd=htpc.RUNDIR)
            output, err = proc.communicate()
        except OSError, e:
            self.logger.warning(str(e))
            return ''

        if err:
            self.logger.warning(output + ' - ' + err)
            return ''
        elif any(s in output for s in ['not found', 'not recognized', 'fatal:']):
            self.logger.warning(output)
            return ''
        else:
            return output.strip()
<|endoftext|>