<commit_before>"""Builtin Datasets.
"""

import os
import numpy as np
import pandas as pd
from .. import logging as logg
from .._settings import settings
import scanpy as sc
from ._ebi_expression_atlas import ebi_expression_atlas

def blobs(n_variables=11, n_centers=5, cluster_std=1.0, n_observations=640):
    """Gaussian Blobs.

    Parameters
    ----------
    n_variables : `int`, optional (default: 11)
        Dimension of feature space.
    n_centers : `int`, optional (default: 5)
        Number of cluster centers.
    cluster_std : `float`, optional (default: 1.0)
        Standard deviation of clusters.
    n_observations : `int`, optional (default: 640)
        Number of observations. By default, this is the same observation number as in
        ``sc.datasets.krumsiek11()``.

    Returns
    -------
    adata : :class:`~anndata.AnnData`
        Annotated data matrix containing a observation annotation 'blobs' that
        indicates cluster identity.
    """
    import sklearn.datasets
    X, y = sklearn.datasets.make_blobs(n_samples=n_observations,
                                       n_features=n_variables,
                                       centers=n_centers,
                                       cluster_std=cluster_std,
                                       random_state=0)
    return sc.AnnData(X, obs={'blobs': y.astype(str)})

def burczynski06():
    """Bulk data with conditions ulcerative colitis (UC) and Crohn's disease (CD).

    The study assesses transcriptional profiles in peripheral blood mononuclear
    cells from 42 healthy individuals, 59 CD patients, and 26 UC patients by
    hybridization to microarrays interrogating more than 22,000 sequences.

    Reference
    ---------
    Burczynski et al., "Molecular classification of Crohn's disease and
    ulcerative colitis patients using transcriptional profiles in peripheral
    blood mononuclear cells"
    J Mol Diagn 8, 51 (2006). PMID:16436634.
    """
    filename = settings.datasetdir / 'burczynski06/GDS1615_full.soft.gz'
    url = 'ftp://ftp.ncbi.nlm.nih.gov/geo/datasets/GDS1nnn/GDS1615/soft/GDS1615_full.soft.gz'
    adata = sc.read(filename, backup_url=url)
    return adata

def krumsiek11():
    """Simulated myeloid progenitors [Krumsiek11]_.

    The literature-curated boolean network from [Krumsiek11]_ was used to
    simulate the data. It describes development to four cell fates: 'monocyte',
    'erythrocyte', 'megakaryocyte' and 'neutrophil'.

    See also the discussion of this data in [Wolf17i]_.

    Simulate via :func:`~scanpy.api.sim`.

    Returns
    -------
    adata : :class:`~anndata.AnnData`
        Annotated data matrix.
    """
    filename = os.path.dirname(__file__) + '/krumsiek11.txt'
    verbosity_save = sc.settings.verbosity
    sc.settings.verbosity = 'error'  # suppress output...
    adata = sc.read(filename, first_column_names=True)
    sc.settings.verbosity = verbosity_save
    adata.uns['iroot'] = 0
    fate_labels = {0: 'Stem', 159: 'Mo', 319: 'Ery',
                   459: 'Mk', 619: 'Neu'}
    adata.uns['highlights'] = fate_labels
    cell_type = np.array(['progenitor' for i in range(adata.n_obs)])
    cell_type[80:160] = 'Mo'
    cell_type[240:320] = 'Ery'
    cell_type[400:480] = 'Mk'
    cell_type[560:640] = 'Neu'
    adata.obs['cell_type'] = cell_type
    sc.utils.sanitize_anndata(adata)
    return adata

def moignard15():
    """Hematopoiesis in early mouse embryos [Moignard15]_.

    Returns
    -------
    adata : :class:`~anndata.AnnData`
        Annotated data matrix.
    """
    filename = settings.datasetdir / 'moignard15/nbt.3154-S3.xlsx'
    backup_url = 'http://www.nature.com/nbt/journal/v33/n3/extref/nbt.3154-S3.xlsx'
    adata = sc.read(filename, sheet='dCt_values.txt', cache=True, backup_url=backup_url)
    # filter out 4 genes as in Haghverdi et al. (2016)
    gene_subset = ~np.in1d(adata.var_names, ['Eif2b1', 'Mrpl19', 'Polr2a', 'Ubc'])
    adata = adata[:, gene_subset]  # retain non-removed genes
    # choose root cell for DPT analysis as in Haghverdi et al. (2016)
    adata.uns['iroot'] = 532  # note that in Matlab/R, counting starts at 1
    # annotate with Moignard et al. (2015) experimental cell groups
    groups_order = ['HF', 'NP', 'PS', '4SG', '4SFG']
    # annotate each observation/cell
    adata.obs['exp_groups'] = [
        next(gname for gname in groups_order if sname.startswith(gname))
        for sname in adata.obs_names]
    # fix the order and colors of names in "groups"
    adata.obs['exp_groups'] = pd.Categorical(adata.obs['exp_groups'],
                                             categories=groups_order)
    adata.uns['exp_groups_colors'] = ['#D7A83E', '#7AAE5D', '#497ABC', '#AF353A', '#765099']
    return adata

def paul15():
    """Development of Myeloid Progenitors [Paul15]_.

    Non-logarithmized raw data.

    The data has been sent out by Email from the Amit Lab. An R version for
    loading the data can be found here
    https://github.com/theislab/scAnalysisTutorial

    Returns
    -------
    adata : :class:`~anndata.AnnData`
        Annotated data matrix.
    """
    logg.warn('In Scanpy 0.*, this returned logarithmized data. '
              'Now it returns non-logarithmized data.')
    import h5py
    filename = settings.datasetdir / 'paul15/paul15.h5'
    backup_url = 'http://falexwolf.de/data/paul15.h5'
    sc.utils.check_presence_download(filename, backup_url)
    with h5py.File(filename, 'r') as f:
        X = f['data.debatched'][()]
        gene_names = f['data.debatched_rownames'][()].astype(str)
        cell_names = f['data.debatched_colnames'][()].astype(str)
        clusters = f['cluster.id'][()].flatten()
        infogenes_names = f['info.genes_strings'][()].astype(str)
    # each row has to correspond to a observation, therefore transpose
    adata = sc.AnnData(X.transpose())
    adata.var_names = gene_names
    adata.row_names = cell_names
    # names reflecting the cell type identifications from the paper
    cell_type = {7: 'MEP', 8: 'Mk', 9: 'GMP', 10: 'GMP', 11: 'DC',
                 12: 'Baso', 13: 'Baso', 14: 'Mo', 15: 'Mo',
                 16: 'Neu', 17: 'Neu', 18: 'Eos', 19: 'Lymph'}
    cell_type.update({i: 'Ery' for i in range(1, 7)})
    adata.obs['paul15_clusters'] = [
        str(i) + cell_type[i] for i in clusters.astype(int)]
    # make string annotations categorical (optional)
    sc.utils.sanitize_anndata(adata)
    # just keep the first of the two equivalent names per gene
    adata.var_names = [gn.split(';')[0] for gn in adata.var_names]
    # remove 10 corrupted gene names
    infogenes_names = np.intersect1d(infogenes_names, adata.var_names)
    # restrict data array to the 3461 informative genes
    adata = adata[:, infogenes_names]
    # usually we'd set the root cell to an arbitrary cell in the MEP cluster
    # adata.uns['iroot': np.flatnonzero(adata.obs['paul15_clusters']  == '7MEP')[0]
    # here, set the root cell as in Haghverdi et al. (2016)
    adata.uns['iroot'] = 840  # note that other than in Matlab/R, counting starts at 1
    return adata

def toggleswitch():
    """Simulated toggleswitch.

    Data obtained simulating a simple toggleswitch `Gardner *et al.*, Nature
    (2000) <https://doi.org/10.1038/35002131>`__.

    Simulate via :func:`~scanpy.api.sim`.

    Returns
    -------
    adata : :class:`~anndata.AnnData`
        Annotated data matrix.
    """
    filename = os.path.dirname(__file__) + '/toggleswitch.txt'
    adata = sc.read(filename, first_column_names=True)
    adata.uns['iroot'] = 0
    return adata

def pbmc68k_reduced():
    """Subsampled and processed 68k PBMCs.

    10x PBMC 68k dataset from
    https://support.10xgenomics.com/single-cell-gene-expression/datasets

    The original PBMC 68k dataset was preprocessed using scanpy and was saved
    keeping only 724 cells and 221 highly variable genes.

    The saved file contains the annotation of cell types (key: 'bulk_labels'), UMAP coordinates,
    louvain clustering and gene rankings based on the bulk_labels.

    Returns
    -------
    adata : :class:`~anndata.AnnData`
        Annotated data matrix.
    """

    filename = os.path.dirname(__file__) + '/10x_pbmc68k_reduced.h5ad'
    return sc.read(filename)

def pbmc3k():
    """3k PBMCs from 10x Genomics.

    The data consists in 3k PBMCs from a Healthy Donor and is freely available
    from 10x Genomics (`here
    <http://cf.10xgenomics.com/samples/cell-exp/1.1.0/pbmc3k/pbmc3k_filtered_gene_bc_matrices.tar.gz>`__
    from this `webpage
    <https://support.10xgenomics.com/single-cell-gene-expression/datasets/1.1.0/pbmc3k>`__).

    The exact same data is also used in Seurat's
    `basic clustering tutorial <https://satijalab.org/seurat/pbmc3k_tutorial.html>`__.

    .. note::

        This downloads 5.9 MB of data upon the first call of the function and stores it in `./data/pbmc3k_raw.h5ad`.

    The following code was run to produce the file.

    .. code:: python

        adata = sc.read_10x_mtx(
        './data/filtered_gene_bc_matrices/hg19/',  # the directory with the `.mtx` file
        var_names='gene_symbols',                  # use gene symbols for the variable names (variables-axis index)
        cache=True)                                # write a cache file for faster subsequent reading

        adata.var_names_make_unique()  # this is unnecessary if using 'gene_ids'
        adata.write('write/pbmc3k_raw.h5ad', compression='gzip')

    Returns
    -------
    adata : :class:`~anndata.AnnData`
        Annotated data matrix.
    """
    adata = sc.read(settings.datasetdir / 'data/pbmc3k_raw.h5ad', backup_url='http://falexwolf.de/data/pbmc3k_raw.h5ad')
    return adata
<commit_msg>Fix pbmc3k dataset path<commit_after>"""Builtin Datasets.
"""

import os
import numpy as np
import pandas as pd
from .. import logging as logg
from .._settings import settings
import scanpy as sc
from ._ebi_expression_atlas import ebi_expression_atlas

def blobs(n_variables=11, n_centers=5, cluster_std=1.0, n_observations=640):
    """Gaussian Blobs.

    Parameters
    ----------
    n_variables : `int`, optional (default: 11)
        Dimension of feature space.
    n_centers : `int`, optional (default: 5)
        Number of cluster centers.
    cluster_std : `float`, optional (default: 1.0)
        Standard deviation of clusters.
    n_observations : `int`, optional (default: 640)
        Number of observations. By default, this is the same observation number as in
        ``sc.datasets.krumsiek11()``.

    Returns
    -------
    adata : :class:`~anndata.AnnData`
        Annotated data matrix containing a observation annotation 'blobs' that
        indicates cluster identity.
    """
    import sklearn.datasets
    X, y = sklearn.datasets.make_blobs(n_samples=n_observations,
                                       n_features=n_variables,
                                       centers=n_centers,
                                       cluster_std=cluster_std,
                                       random_state=0)
    return sc.AnnData(X, obs={'blobs': y.astype(str)})

def burczynski06():
    """Bulk data with conditions ulcerative colitis (UC) and Crohn's disease (CD).

    The study assesses transcriptional profiles in peripheral blood mononuclear
    cells from 42 healthy individuals, 59 CD patients, and 26 UC patients by
    hybridization to microarrays interrogating more than 22,000 sequences.

    Reference
    ---------
    Burczynski et al., "Molecular classification of Crohn's disease and
    ulcerative colitis patients using transcriptional profiles in peripheral
    blood mononuclear cells"
    J Mol Diagn 8, 51 (2006). PMID:16436634.
    """
    filename = settings.datasetdir / 'burczynski06/GDS1615_full.soft.gz'
    url = 'ftp://ftp.ncbi.nlm.nih.gov/geo/datasets/GDS1nnn/GDS1615/soft/GDS1615_full.soft.gz'
    adata = sc.read(filename, backup_url=url)
    return adata

def krumsiek11():
    """Simulated myeloid progenitors [Krumsiek11]_.

    The literature-curated boolean network from [Krumsiek11]_ was used to
    simulate the data. It describes development to four cell fates: 'monocyte',
    'erythrocyte', 'megakaryocyte' and 'neutrophil'.

    See also the discussion of this data in [Wolf17i]_.

    Simulate via :func:`~scanpy.api.sim`.

    Returns
    -------
    adata : :class:`~anndata.AnnData`
        Annotated data matrix.
    """
    filename = os.path.dirname(__file__) + '/krumsiek11.txt'
    verbosity_save = sc.settings.verbosity
    sc.settings.verbosity = 'error'  # suppress output...
    adata = sc.read(filename, first_column_names=True)
    sc.settings.verbosity = verbosity_save
    adata.uns['iroot'] = 0
    fate_labels = {0: 'Stem', 159: 'Mo', 319: 'Ery',
                   459: 'Mk', 619: 'Neu'}
    adata.uns['highlights'] = fate_labels
    cell_type = np.array(['progenitor' for i in range(adata.n_obs)])
    cell_type[80:160] = 'Mo'
    cell_type[240:320] = 'Ery'
    cell_type[400:480] = 'Mk'
    cell_type[560:640] = 'Neu'
    adata.obs['cell_type'] = cell_type
    sc.utils.sanitize_anndata(adata)
    return adata

def moignard15():
    """Hematopoiesis in early mouse embryos [Moignard15]_.

    Returns
    -------
    adata : :class:`~anndata.AnnData`
        Annotated data matrix.
    """
    filename = settings.datasetdir / 'moignard15/nbt.3154-S3.xlsx'
    backup_url = 'http://www.nature.com/nbt/journal/v33/n3/extref/nbt.3154-S3.xlsx'
    adata = sc.read(filename, sheet='dCt_values.txt', cache=True, backup_url=backup_url)
    # filter out 4 genes as in Haghverdi et al. (2016)
    gene_subset = ~np.in1d(adata.var_names, ['Eif2b1', 'Mrpl19', 'Polr2a', 'Ubc'])
    adata = adata[:, gene_subset]  # retain non-removed genes
    # choose root cell for DPT analysis as in Haghverdi et al. (2016)
    adata.uns['iroot'] = 532  # note that in Matlab/R, counting starts at 1
    # annotate with Moignard et al. (2015) experimental cell groups
    groups_order = ['HF', 'NP', 'PS', '4SG', '4SFG']
    # annotate each observation/cell
    adata.obs['exp_groups'] = [
        next(gname for gname in groups_order if sname.startswith(gname))
        for sname in adata.obs_names]
    # fix the order and colors of names in "groups"
    adata.obs['exp_groups'] = pd.Categorical(adata.obs['exp_groups'],
                                             categories=groups_order)
    adata.uns['exp_groups_colors'] = ['#D7A83E', '#7AAE5D', '#497ABC', '#AF353A', '#765099']
    return adata

def paul15():
    """Development of Myeloid Progenitors [Paul15]_.

    Non-logarithmized raw data.

    The data has been sent out by Email from the Amit Lab. An R version for
    loading the data can be found here
    https://github.com/theislab/scAnalysisTutorial

    Returns
    -------
    adata : :class:`~anndata.AnnData`
        Annotated data matrix.
    """
    logg.warn('In Scanpy 0.*, this returned logarithmized data. '
              'Now it returns non-logarithmized data.')
    import h5py
    filename = settings.datasetdir / 'paul15/paul15.h5'
    backup_url = 'http://falexwolf.de/data/paul15.h5'
    sc.utils.check_presence_download(filename, backup_url)
    with h5py.File(filename, 'r') as f:
        X = f['data.debatched'][()]
        gene_names = f['data.debatched_rownames'][()].astype(str)
        cell_names = f['data.debatched_colnames'][()].astype(str)
        clusters = f['cluster.id'][()].flatten()
        infogenes_names = f['info.genes_strings'][()].astype(str)
    # each row has to correspond to a observation, therefore transpose
    adata = sc.AnnData(X.transpose())
    adata.var_names = gene_names
    adata.row_names = cell_names
    # names reflecting the cell type identifications from the paper
    cell_type = {7: 'MEP', 8: 'Mk', 9: 'GMP', 10: 'GMP', 11: 'DC',
                 12: 'Baso', 13: 'Baso', 14: 'Mo', 15: 'Mo',
                 16: 'Neu', 17: 'Neu', 18: 'Eos', 19: 'Lymph'}
    cell_type.update({i: 'Ery' for i in range(1, 7)})
    adata.obs['paul15_clusters'] = [
        str(i) + cell_type[i] for i in clusters.astype(int)]
    # make string annotations categorical (optional)
    sc.utils.sanitize_anndata(adata)
    # just keep the first of the two equivalent names per gene
    adata.var_names = [gn.split(';')[0] for gn in adata.var_names]
    # remove 10 corrupted gene names
    infogenes_names = np.intersect1d(infogenes_names, adata.var_names)
    # restrict data array to the 3461 informative genes
    adata = adata[:, infogenes_names]
    # usually we'd set the root cell to an arbitrary cell in the MEP cluster
    # adata.uns['iroot': np.flatnonzero(adata.obs['paul15_clusters']  == '7MEP')[0]
    # here, set the root cell as in Haghverdi et al. (2016)
    adata.uns['iroot'] = 840  # note that other than in Matlab/R, counting starts at 1
    return adata

def toggleswitch():
    """Simulated toggleswitch.

    Data obtained simulating a simple toggleswitch `Gardner *et al.*, Nature
    (2000) <https://doi.org/10.1038/35002131>`__.

    Simulate via :func:`~scanpy.api.sim`.

    Returns
    -------
    adata : :class:`~anndata.AnnData`
        Annotated data matrix.
    """
    filename = os.path.dirname(__file__) + '/toggleswitch.txt'
    adata = sc.read(filename, first_column_names=True)
    adata.uns['iroot'] = 0
    return adata

def pbmc68k_reduced():
    """Subsampled and processed 68k PBMCs.

    10x PBMC 68k dataset from
    https://support.10xgenomics.com/single-cell-gene-expression/datasets

    The original PBMC 68k dataset was preprocessed using scanpy and was saved
    keeping only 724 cells and 221 highly variable genes.

    The saved file contains the annotation of cell types (key: 'bulk_labels'), UMAP coordinates,
    louvain clustering and gene rankings based on the bulk_labels.

    Returns
    -------
    adata : :class:`~anndata.AnnData`
        Annotated data matrix.
    """

    filename = os.path.dirname(__file__) + '/10x_pbmc68k_reduced.h5ad'
    return sc.read(filename)

def pbmc3k():
    """3k PBMCs from 10x Genomics.

    The data consists in 3k PBMCs from a Healthy Donor and is freely available
    from 10x Genomics (`here
    <http://cf.10xgenomics.com/samples/cell-exp/1.1.0/pbmc3k/pbmc3k_filtered_gene_bc_matrices.tar.gz>`__
    from this `webpage
    <https://support.10xgenomics.com/single-cell-gene-expression/datasets/1.1.0/pbmc3k>`__).

    The exact same data is also used in Seurat's
    `basic clustering tutorial <https://satijalab.org/seurat/pbmc3k_tutorial.html>`__.

    .. note::

        This downloads 5.9 MB of data upon the first call of the function and stores it in `./data/pbmc3k_raw.h5ad`.

    The following code was run to produce the file.

    .. code:: python

        adata = sc.read_10x_mtx(
        './data/filtered_gene_bc_matrices/hg19/',  # the directory with the `.mtx` file
        var_names='gene_symbols',                  # use gene symbols for the variable names (variables-axis index)
        cache=True)                                # write a cache file for faster subsequent reading

        adata.var_names_make_unique()  # this is unnecessary if using 'gene_ids'
        adata.write('write/pbmc3k_raw.h5ad', compression='gzip')

    Returns
    -------
    adata : :class:`~anndata.AnnData`
        Annotated data matrix.
    """
    adata = sc.read(settings.datasetdir / 'pbmc3k_raw.h5ad', backup_url='http://falexwolf.de/data/pbmc3k_raw.h5ad')
    return adata
<|endoftext|>


<commit_before>##############################################################################
#
# Copyright (c) 2016 Patrice Clement
#
# This file is part of libzilla
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
##############################################################################

import sys
from docopt import docopt

"""The BaseCommand class is an entry point into the `lzilla' command which
dispatches keywords to subcommands."""

VERSION = '1.2'

class BaseCommand:
    """lzilla is a tool for managing Bugzilla bug reports.

All communications are carried out using
the Bugzilla REST API over the HTTPS scheme.

Usage:
  lzilla [-h|--help] <command> [<args>...]

Commands:
  lzilla stable
  lzilla shell
  lzilla bug
  lzilla git

Options:
  -h,  --help     Display help.
  -v,  --version  Display version."""

    def __init__(self):
        args = docopt(
            BaseCommand.__doc__,
            version=VERSION,
            help=True,
            options_first=True
        )

        runner = {
            'stable': self.run_stable_command,
            'shell':  self.run_shell_command,
            'bug':    self.run_bug_command,
            'git':    self.run_git_command
        }

        try:
            runner[args['<command>']]()
        except KeyError:
            sys.exit("""Error! \'%s\' is an invalid command!
See 'lzilla --help' for a complete list of commands.""" % (args['<command>']))

    def run_bug_command(self):
        from libzilla.cli.bug import BugCommand
        BugCommand(docopt(BugCommand.__doc__))

    def run_git_command(self):
        from libzilla.cli.git import GitCommand
        GitCommand(docopt(GitCommand.__doc__))

    def run_stable_command(self):
        from libzilla.cli.stable import StableCommand
        StableCommand(docopt(StableCommand.__doc__))

    def run_shell_command(self):
        from libzilla.cli.shell import ShellCommand
        ShellCommand(docopt(ShellCommand.__doc__)).cmdloop()
<commit_msg>document functions<commit_after>##############################################################################
#
# Copyright (c) 2016 Patrice Clement
#
# This file is part of libzilla
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
##############################################################################

import sys
from docopt import docopt

"""The BaseCommand class is an entry point into the `lzilla' command which
dispatches keywords to subcommands."""

VERSION = '1.2'

class BaseCommand:
    """lzilla is a tool for managing Bugzilla bug reports.

All communications are carried out using
the Bugzilla REST API over the HTTPS scheme.

Usage:
  lzilla [-h|--help] <command> [<args>...]

Commands:
  lzilla stable
  lzilla shell
  lzilla bug
  lzilla git

Options:
  -h,  --help     Display help.
  -v,  --version  Display version."""

    def __init__(self):
        args = docopt(
            BaseCommand.__doc__,
            version=VERSION,
            help=True,
            options_first=True
        )

        runner = {
            'stable': self.run_stable_command,
            'shell':  self.run_shell_command,
            'bug':    self.run_bug_command,
            'git':    self.run_git_command
        }

        try:
            runner[args['<command>']]()
        except KeyError:
            sys.exit("""Error! \'%s\' is an invalid command!
See 'lzilla --help' for a complete list of commands.""" % (args['<command>']))

    def run_bug_command(self):
        """Run the `lzilla bug' command."""
        from libzilla.cli.bug import BugCommand
        BugCommand(docopt(BugCommand.__doc__))

    def run_git_command(self):
        """Run the `lzilla git' command."""
        from libzilla.cli.git import GitCommand
        GitCommand(docopt(GitCommand.__doc__))

    def run_stable_command(self):
        """Run the `lzilla stable' command."""
        from libzilla.cli.stable import StableCommand
        StableCommand(docopt(StableCommand.__doc__))

    def run_shell_command(self):
        """Run the `lzilla shell' command."""
        from libzilla.cli.shell import ShellCommand
        ShellCommand(docopt(ShellCommand.__doc__)).cmdloop()
<|endoftext|>


<commit_before>from collections import namedtuple

import numpy as np
import pandas as pd
import warnings

Occurrence = namedtuple('Occurrence', 'start stop slice duration')

class Events(object):
    """Object to represent events found in time series data

    A representation of events based off a ``bool`` conditional array.

    Attributes:
        name (``str``): User provided name for events.
        _starts (``np.array`` of ``int``): The index for event starts
        _stops (``np.array`` of ``int``): The index for event stops
        _period (``float``): Time between each value of the original condition array
        _condition_size (``int``): The size of the original condition array

    """
    def __init__(self, starts, stops, period, name, condition_size):
        self.name = name
        self._starts = starts
        self._stops = stops
        self._period = period
        self._condition_size = condition_size

    @property
    def durations(self):
        """Return a ``numpy.ndarray`` of event durations in seconds.

        Examples:
            >>> from trouve import find_events
            >>> x = np.array([2, 2, 4, 5, 3, 2])
            >>> condition = x == 2
            >>> events = find_events(condition, period=1)
            >>> print(events.to_array())
            [ 1.  1.  0.  0.  0.  1.]
            >>> print(events.durations)
            [2 1]

        """
        durations = (self._stops - self._starts) * self._period
        return durations

    def to_array(self, inactive_values=0, active_values=1, dtype=np.float):
        """Returns a ``numpy.ndarray`` identifying found events

        Useful for plotting or building another mask based on identified
        events.

        Parameters:
            inactive_values(``float``, optional): Default is 0.
                Value of array where events are not active.
            active_values (``float``, optional): Default is 1.
                Value of array where events are active.
            dtype (``numpy.dtype``, optional): Default is ``numpy.float``.
                Datatype of returned array.

        Returns:
            ``numpy.ndarray``: An array where values are coded to
                identify when events are active or inactive.

        Examples:
            >>> from trouve import find_events
            >>> x = np.array([2, 2, 4, 5, 3, 2])
            >>> condition = x > 2
            >>> print(condition)
            [False False  True  True  True False]
            >>> events = find_events(condition, period=1)
            >>> print(events.to_array())
            [ 0.  0.  1.  1.  1.  0.]

        """
        output = np.ones(self._condition_size, dtype=dtype) * inactive_values
        for start, stop in zip(self._starts, self._stops):
            output[start:stop] = 1 * active_values
        return output.astype(dtype)

    # TODO force defaults to np.int8 datatype
    # specify false and true values as None, check if values and dtype is noe
    # and specify appropriately
    def as_array(self, false_values=0, true_values=1, dtype=np.float):
        """Returns a ``numpy.ndarray`` identifying found events

        Useful for plotting.

        Parameters:
            false_values(``float``, optional): Default is 0.
                Value of array where events are not active.
            true_values (``float``, optional): Default is 1.
                Value of array where events are active.
            dtype (``numpy.dtype``, optional): Default is ``numpy.float``.
                Datatype of returned array.

        Returns:
            ``numpy.ndarray``:
                An array where values are coded to identify when events are active
                or inactive.

        Examples:
            >>> from trouve import find_events
            >>> x = np.array([2, 2, 4, 5, 3, 2])
            >>> condition = x > 2
            >>> print(condition)
            [False False  True  True  True False]
            >>> events = find_events(condition, period=1)
            >>> print(events.to_array())
            [ 0.  0.  1.  1.  1.  0.]

        """
        warnings.warn('Use to_array instead', DeprecationWarning)
        # TODO Deprecating in v0.5.x, removing in v0.6.x
        output = np.ones(self._condition_size, dtype=dtype) * false_values
        for start, stop in zip(self._starts, self._stops):
            output[start:stop] = 1 * true_values
        return output.astype(dtype)

    def to_series(self, inactive_value=0, active_value=1,
                  index=None, dtype=None, name=None):
        """Returns a ``pandas.Series`` identifying found events

        Useful for plotting and for filtering a ``pandas.DataFrame``

        Parameters:
            inactive_value(``float``, optional): Default is 0.
                Value of array where events are not active.
            active_value (``float``, optional): Default is 1.
                Value of array where events are active.
            index (``array-like`` or ``Index`` (1d)):Values must be
                hashable and have the same length as data. Non-unique
                index values are allowed. Will default to
                RangeIndex(len(data)) if not provided. If both a dict
                and index sequence are used, the index will override
                the keys found in the dict.
            dtype (``numpy.dtype`` or ``None``): If ``None``, ``dtype``
                will be inferred.
            name (``str``, optional): Default is :attr:`Events.name`.
                Name of series.

        Returns:
            ``pandas.Series``:
                A series where values are coded to identify when events are active
                or inactive.

        Examples:
            >>> from trouve import find_events
            >>> x = np.array([2, 2, 4, 5, 3, 2])
            >>> condition = x > 2
            >>> print(condition)
            [False False  True  True  True False]
            >>> events = find_events(condition, period=1)
            >>> print(events.as_series())
            0    0.0
            1    0.0
            2    1.0
            3    1.0
            4    1.0
            5    0.0
            Name: events, dtype: float64

        """
        if name is None:
            name = self.name
        data = self.to_array(inactive_values=inactive_value, active_values=active_value, dtype=dtype)
        return pd.Series(data=data, index=index, name=name)

    def as_mask(self):
        """Returns a ``numpy.ndarray`` ``bool`` mask for use with ``numpy.ma``

        This method returns a ``np.ndarray`` of ``bool`` where values are
        False where the condition is met, and True where the conditions
        are not met. Trouve treats conditionals opposite of how numpy
        treats them. That is to say that numpy will mask out values in
        an array that meet the condition, however Trouve is by design
        more interested in finding and keeping events that meet the
        given condition. This method makes it more convenient to
        interact with the numpy masked array module as it will return
        a mask that can be directly used by the ``numpy.ma`` module.
        This is similar to what the tilde does for bool arrays.

        Returns:
            ``np.ndarray``:
                An array of bools where the values are ``True`` when the condition
                isn't met and ``False`` when the conditions are met.

        Examples:
            >>> from trouve import find_events
            >>> x = np.array([2, 2, 4, 5, 3, 2])
            >>> condition = x > 2
            >>> print(condition)
            [False False  True  True  True False]
            >>> events = find_events(condition, period=1)
            >>> print(events.to_array())
            [ 0.  0.  1.  1.  1.  0.]
            >>> print(events.as_mask())
            [ True  True False False False  True]
            >>> print(np.ma.masked_where(events.as_mask(), x))
            [-- -- 4 5 3 --]

        """
        warnings.warn('Use to_array or to_series instead', DeprecationWarning)
        # TODO Deprecating in v0.5.x, removing in v0.6.x
        return self.to_array(1, 0, np.int8).view(bool)

    def __iter__(self):
        self._i = 0
        return self

    def __next__(self):
        """Iterate through ``Events._starts`` and ``Events._stops`` and return an :class:`.Occurrence`

        Examples:
            >>> import numpy as np
            >>> from trouve import find_events
            >>> x = np.array([0, 1, 1, 0, 1, 0])
            >>> example = find_events(x, period=1, name='example')
            >>> for event in example:
            ...     print(event)
            ...
            Occurrence(start=1, stop=2, slice=slice(1, 3, None), duration=2)
            Occurrence(start=4, stop=4, slice=slice(4, 5, None), duration=1)

        """
        try:
            occurrence = Occurrence(
                start=self._starts[self._i],
                stop=self._stops[self._i] - 1,
                slice=slice(self._starts[self._i], self._stops[self._i]),
                duration=(self._stops[self._i] - self._starts[self._i]) * self._period
            )
            self._i += 1
            return occurrence
        except IndexError:
            raise StopIteration

    def __getitem__(self, item):
        """Get a specific :class:`.Occurrence`

        Examples:
            >>> import numpy as np
            >>> from trouve import find_events
            >>> x = np.array([0, 1, 1, 0, 1, 0])
            >>> example = find_events(x, period=1, name='example')
            >>> first_event = example[0]
            >>> print(first_event)
            Occurrence(start=1, stop=2, slice=slice(1, 3, None), duration=2)

        """
        occurrence = Occurrence(
            start=self._starts[item],
            stop=self._stops[item]-1,
            slice=slice(self._starts[item], self._stops[item]),
            duration=(self._stops[item] - self._starts[item]) * self._period
        )
        return occurrence

    def __len__(self):
        """Returns the number of events found

        Redirects to :any:`Events._starts` and returns ``Events._starts.size``

        Examples:
            >>> import numpy as np
            >>> from trouve import find_events
            >>> x = np.array([0, 1, 1, 0, 1, 0])
            >>> example = find_events(x, period=1, name='example')
            >>> len(example)
            2

        """
        return self._starts.size

    def __repr__(self):
        return (
            '{__class__.__name__}(_starts={_starts!r}, '
            '_stops={_stops!r}, '
            '_period={_period!r}, '
            'name={name!r}, '
            '_condition_size={_condition_size!r})'
        ).format(__class__=self.__class__, **self.__dict__)

    def __str__(self):
        """Prints a summary of the events

        Examples:
            >>> import numpy as np
            >>> from trouve import find_events
            >>> x = np.array([0, 1, 1, 0, 1, 0])
            >>> example = find_events(x, period=1, name='example')
            >>> print(example)
            example
            Number of events: 2
            Min, Max, Mean Duration: 1.000s, 2.000s, 1.500s

        """
        args = [len(self),
                np.min(self.durations),
                np.max(self.durations),
                np.mean(self.durations)]

        kwargs = {'name': '{}'.format(self.name),
                  'period': '{}s'.format(self._period)}
        return (
            '{name}'
            '\nNumber of events: {0}'
            '\nMin, Max, Mean Duration: {1:.3f}s, {2:.3f}s, {3:.3f}s'
        ).format(*args, **kwargs)

    def __eq__(self, other):
        """Determine if two Events objects are identical

        Compares :attr:`Events._starts`, :attr:`Events._stops`, :attr:`Events._period`
        and :attr:`Events.condition.size` to determine if equality of two events.
        Events objects can have different names and still be equal.

        Examples:
            >>> import numpy as np
            >>> from trouve import find_events
            >>> x = np.array([0, 1, 1, 0, 1, 0])
            >>> example = find_events(x, period=1, name='example')
            >>> other = find_events(x, period=1, name='other')
            >>> id(example) # doctest: +SKIP
            2587452050568
            >>> id(other) # doctest: +SKIP
            2587452084352
            >>> example == other
            True
            >>> example != other
            False

        """
        if (np.all(self._starts == other._starts)
                and np.all(self._stops == other._stops)
                and self._period == other._period
                and self._condition_size == other._condition_size):
            return True
        else:
            return False

    def __hash__(self):
        """Numpy arrays aren't hashable.

        Researching solution that doesn't require something beyond
        standard lib.
        """
        return id(self)
<commit_msg>Add numpy and pandas arguments to to_* methods<commit_after>from collections import namedtuple

import numpy as np
import pandas as pd
import warnings

Occurrence = namedtuple('Occurrence', 'start stop slice duration')

class Events(object):
    """Object to represent events found in time series data

    A representation of events based off a ``bool`` conditional array.

    Attributes:
        name (``str``): User provided name for events.
        _starts (``np.array`` of ``int``): The index for event starts
        _stops (``np.array`` of ``int``): The index for event stops
        _period (``float``): Time between each value of the original condition array
        _condition_size (``int``): The size of the original condition array

    """
    def __init__(self, starts, stops, period, name, condition_size):
        self.name = name
        self._starts = starts
        self._stops = stops
        self._period = period
        self._condition_size = condition_size

    @property
    def durations(self):
        """Return a ``numpy.ndarray`` of event durations in seconds.

        Examples:
            >>> from trouve import find_events
            >>> x = np.array([2, 2, 4, 5, 3, 2])
            >>> condition = x == 2
            >>> events = find_events(condition, period=1)
            >>> print(events.to_array())
            [ 1.  1.  0.  0.  0.  1.]
            >>> print(events.durations)
            [2 1]

        """
        durations = (self._stops - self._starts) * self._period
        return durations

    def to_array(self, inactive_values=0, active_values=1, dtype=None, order='C'):
        """Returns a ``numpy.ndarray`` identifying found events

        Useful for plotting or building another mask based on identified
        events.

        Parameters:
            inactive_values(``float``, optional): Default is 0.
                Value of array where events are not active.
            active_values (``float``, optional): Default is 1.
                Value of array where events are active.
            dtype (``numpy.dtype``, optional): Default is ``numpy.float64``.
                Datatype of returned array.
            order (``str``, optional): Default is 'C'. {'C', 'F'} whether to
                store multidimensional data in C- or Fortran-contiguous (row-
                or column-wise) order in memory.

        Returns:
            ``numpy.ndarray``: An array where values are coded to
                identify when events are active or inactive.

        Examples:
            >>> from trouve import find_events
            >>> x = np.array([2, 2, 4, 5, 3, 2])
            >>> condition = x > 2
            >>> print(condition)
            [False False  True  True  True False]
            >>> events = find_events(condition, period=1)
            >>> print(events.to_array())
            [ 0.  0.  1.  1.  1.  0.]

        """
        output = np.ones(self._condition_size, dtype=dtype, order=order) * inactive_values
        for start, stop in zip(self._starts, self._stops):
            output[start:stop] = active_values
        return output.astype(dtype)

    # TODO force defaults to np.int8 datatype
    # specify false and true values as None, check if values and dtype is noe
    # and specify appropriately
    def as_array(self, false_values=0, true_values=1, dtype=np.float):
        """Returns a ``numpy.ndarray`` identifying found events

        Useful for plotting.

        Parameters:
            false_values(``float``, optional): Default is 0.
                Value of array where events are not active.
            true_values (``float``, optional): Default is 1.
                Value of array where events are active.
            dtype (``numpy.dtype``, optional): Default is ``numpy.float``.
                Datatype of returned array.

        Returns:
            ``numpy.ndarray``:
                An array where values are coded to identify when events are active
                or inactive.

        Examples:
            >>> from trouve import find_events
            >>> x = np.array([2, 2, 4, 5, 3, 2])
            >>> condition = x > 2
            >>> print(condition)
            [False False  True  True  True False]
            >>> events = find_events(condition, period=1)
            >>> print(events.to_array())
            [ 0.  0.  1.  1.  1.  0.]

        """
        warnings.warn('Use to_array instead', DeprecationWarning)
        # TODO Deprecating in v0.5.x, removing in v0.6.x
        output = np.ones(self._condition_size, dtype=dtype) * false_values
        for start, stop in zip(self._starts, self._stops):
            output[start:stop] = 1 * true_values
        return output.astype(dtype)

    def to_series(self, inactive_value=0, active_value=1,
                  index=None, dtype=None, name=None):
        """Returns a ``pandas.Series`` identifying found events

        Useful for plotting and for filtering a ``pandas.DataFrame``

        Parameters:
            inactive_value(``float``, optional): Default is 0.
                Value of array where events are not active.
            active_value (``float``, optional): Default is 1.
                Value of array where events are active.
            index (``array-like`` or ``Index`` (1d)):Values must be
                hashable and have the same length as data. Non-unique
                index values are allowed. Will default to
                RangeIndex(len(data)) if not provided. If both a dict
                and index sequence are used, the index will override
                the keys found in the dict.
            dtype (``numpy.dtype`` or ``None``): If ``None``, ``dtype``
                will be inferred.
            name (``str``, optional): Default is :attr:`Events.name`.
                Name of series.

        Returns:
            ``pandas.Series``:
                A series where values are coded to identify when events are active
                or inactive.

        Examples:
            >>> from trouve import find_events
            >>> x = np.array([2, 2, 4, 5, 3, 2])
            >>> condition = x > 2
            >>> print(condition)
            [False False  True  True  True False]
            >>> events = find_events(condition, period=1)
            >>> print(events.to_series())
            0    0.0
            1    0.0
            2    1.0
            3    1.0
            4    1.0
            5    0.0
            Name: events, dtype: float64

        """
        if name is None:
            name = self.name
        data = self.to_array(inactive_values=inactive_value, active_values=active_value, dtype=dtype)
        return pd.Series(data=data, index=index, name=name)

    def as_mask(self):
        """Returns a ``numpy.ndarray`` ``bool`` mask for use with ``numpy.ma``

        This method returns a ``np.ndarray`` of ``bool`` where values are
        False where the condition is met, and True where the conditions
        are not met. Trouve treats conditionals opposite of how numpy
        treats them. That is to say that numpy will mask out values in
        an array that meet the condition, however Trouve is by design
        more interested in finding and keeping events that meet the
        given condition. This method makes it more convenient to
        interact with the numpy masked array module as it will return
        a mask that can be directly used by the ``numpy.ma`` module.
        This is similar to what the tilde does for bool arrays.

        Returns:
            ``np.ndarray``:
                An array of bools where the values are ``True`` when the condition
                isn't met and ``False`` when the conditions are met.

        Examples:
            >>> from trouve import find_events
            >>> x = np.array([2, 2, 4, 5, 3, 2])
            >>> condition = x > 2
            >>> print(condition)
            [False False  True  True  True False]
            >>> events = find_events(condition, period=1)
            >>> print(events.to_array())
            [ 0.  0.  1.  1.  1.  0.]
            >>> print(events.as_mask())
            [ True  True False False False  True]
            >>> print(np.ma.masked_where(events.as_mask(), x))
            [-- -- 4 5 3 --]

        """
        warnings.warn('Use to_array or to_series instead', DeprecationWarning)
        # TODO Deprecating in v0.5.x, removing in v0.6.x
        return self.to_array(1, 0, np.int8).view(bool)

    def __iter__(self):
        self._i = 0
        return self

    def __next__(self):
        """Iterate through ``Events._starts`` and ``Events._stops`` and return an :class:`.Occurrence`

        Examples:
            >>> import numpy as np
            >>> from trouve import find_events
            >>> x = np.array([0, 1, 1, 0, 1, 0])
            >>> example = find_events(x, period=1, name='example')
            >>> for event in example:
            ...     print(event)
            ...
            Occurrence(start=1, stop=2, slice=slice(1, 3, None), duration=2)
            Occurrence(start=4, stop=4, slice=slice(4, 5, None), duration=1)

        """
        try:
            occurrence = Occurrence(
                start=self._starts[self._i],
                stop=self._stops[self._i] - 1,
                slice=slice(self._starts[self._i], self._stops[self._i]),
                duration=(self._stops[self._i] - self._starts[self._i]) * self._period
            )
            self._i += 1
            return occurrence
        except IndexError:
            raise StopIteration

    def __getitem__(self, item):
        """Get a specific :class:`.Occurrence`

        Examples:
            >>> import numpy as np
            >>> from trouve import find_events
            >>> x = np.array([0, 1, 1, 0, 1, 0])
            >>> example = find_events(x, period=1, name='example')
            >>> first_event = example[0]
            >>> print(first_event)
            Occurrence(start=1, stop=2, slice=slice(1, 3, None), duration=2)

        """
        occurrence = Occurrence(
            start=self._starts[item],
            stop=self._stops[item]-1,
            slice=slice(self._starts[item], self._stops[item]),
            duration=(self._stops[item] - self._starts[item]) * self._period
        )
        return occurrence

    def __len__(self):
        """Returns the number of events found

        Redirects to :any:`Events._starts` and returns ``Events._starts.size``

        Examples:
            >>> import numpy as np
            >>> from trouve import find_events
            >>> x = np.array([0, 1, 1, 0, 1, 0])
            >>> example = find_events(x, period=1, name='example')
            >>> len(example)
            2

        """
        return self._starts.size

    def __repr__(self):
        return (
            '{__class__.__name__}(_starts={_starts!r}, '
            '_stops={_stops!r}, '
            '_period={_period!r}, '
            'name={name!r}, '
            '_condition_size={_condition_size!r})'
        ).format(__class__=self.__class__, **self.__dict__)

    def __str__(self):
        """Prints a summary of the events

        Examples:
            >>> import numpy as np
            >>> from trouve import find_events
            >>> x = np.array([0, 1, 1, 0, 1, 0])
            >>> example = find_events(x, period=1, name='example')
            >>> print(example)
            example
            Number of events: 2
            Min, Max, Mean Duration: 1.000s, 2.000s, 1.500s

        """
        args = [len(self),
                np.min(self.durations),
                np.max(self.durations),
                np.mean(self.durations)]

        kwargs = {'name': '{}'.format(self.name),
                  'period': '{}s'.format(self._period)}
        return (
            '{name}'
            '\nNumber of events: {0}'
            '\nMin, Max, Mean Duration: {1:.3f}s, {2:.3f}s, {3:.3f}s'
        ).format(*args, **kwargs)

    def __eq__(self, other):
        """Determine if two Events objects are identical

        Compares :attr:`Events._starts`, :attr:`Events._stops`, :attr:`Events._period`
        and :attr:`Events.condition.size` to determine if equality of two events.
        Events objects can have different names and still be equal.

        Examples:
            >>> import numpy as np
            >>> from trouve import find_events
            >>> x = np.array([0, 1, 1, 0, 1, 0])
            >>> example = find_events(x, period=1, name='example')
            >>> other = find_events(x, period=1, name='other')
            >>> id(example) # doctest: +SKIP
            2587452050568
            >>> id(other) # doctest: +SKIP
            2587452084352
            >>> example == other
            True
            >>> example != other
            False

        """
        if (np.all(self._starts == other._starts)
                and np.all(self._stops == other._stops)
                and self._period == other._period
                and self._condition_size == other._condition_size):
            return True
        else:
            return False

    def __hash__(self):
        """Numpy arrays aren't hashable.

        Researching solution that doesn't require something beyond
        standard lib.
        """
        return id(self)
<|endoftext|>


<commit_before>import os
from os import environ as env
from django.conf import global_settings
import dj_database_url

DEBUG = env.get('DJANGO_DEBUG', 'true') == 'true'
TEMPLATE_DEBUG = DEBUG
PROJECT_ROOT = os.path.dirname(os.path.realpath(__name__))

ADMINS = (
    ('Adi Eyal', 'adi@openup.org.za'),
)

MANAGERS = ADMINS

DATABASES = {
    # load from DATABASE_URL env var, or default to this
    'default': dj_database_url.config(default='sqlite:///mpr.db')
}

# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = ['code4sa-mpr.herokuapp.com', 'mpr.code4sa.org', 'localhost']

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'Africa/Johannesburg'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: '/var/www/example.com/media/'
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: 'http://example.com/media/', 'http://media.example.com/'
MEDIA_URL = '/media/'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' 'static/' subdirectories and in STATICFILES_DIRS.
# Example: '/var/www/example.com/static/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')

# URL prefix for static files.
# Example: 'http://example.com/static/', 'http://static.example.com/'
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like '/home/html/static' or 'C:/www/django/static'.
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(PROJECT_ROOT, 'mpr', 'static'),
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

if DEBUG:
    # Make this unique, and don't share it with anybody.
    SECRET_KEY = 'ub7dk%m=d*k=aip1rl)z&v8sj&fg2msc&=km0z3u#5ct9+_43w'
else:
    SECRET_KEY = env.get('DJANGO_SECRET_KEY')

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'mpr.middleware.CORSMiddleware',
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'mpr.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'mpr.wsgi.application'

TEMPLATE_DIRS = (
    # Put strings here, like '/home/html/django_templates' or 'C:/www/django/templates'.
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(PROJECT_ROOT, 'templates'),
)

def settings_context(context):
    return { 'debug' : DEBUG }

#TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
#    'mpr.settings.settings_context',
#)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'pipeline',
    'mpr',
#    'behave_django',
)

STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'
PIPELINE_CSS = {
    'mpr': {
        'source_filenames': (
          'css/bootstrap.css',
          #'css/bootstrap-theme.css',
          'css/custom.css',
          #'css/survey.css',
        ),
        'output_filename': 'css/mpr.css',
    },
}
PIPELINE_JS = {
    'mprbase': {
        'source_filenames': (
          'js/jquery-1.10.2.js',
          'js/bootstrap.js',
          'js/jquery.ba-hashchange.js',
          'js/knockout.js',
          'js/survey.ko.js',
          'js/medloader.js',
        ),
        'output_filename': 'js/mprbase.js',
    },
    'oldie': {
        'source_filenames': (
          'js/html5shiv.js',
          'js/respond.src.js',
        ),
        'output_filename': 'js/oldie.js',
    },
}
PIPELINE_CSS_COMPRESSOR = 'pipeline.compressors.yuglify.YuglifyCompressor'
PIPELINE_JS_COMPRESSOR = 'pipeline.compressors.yuglify.YuglifyCompressor'
PIPELINE_DISABLE_WRAPPER = True

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format' : '[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s',
            'datefmt' : '%d/%b/%Y %H:%M:%S'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'ERROR',
            'propagate': True,
        },
        'mpr': {
            'handlers': ['console'],
            'level': 'DEBUG' if DEBUG else 'ERROR',
            'propagate': True,
        },
    }
}

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
        'TIMEOUT' : 60*60*24*7
    }
}

SEGMENT_IO_KEY = env.get('SEGMENT_IO_KEY')
SESSION = 'django.contrib.sessions.backends.signed_cookies'

PRICE_PARAMETERS = {
    "VAT" : 1.15,
    "prices" : [
        (109.56, 0.46, 14.50),
        (292.25, 0.33, 27.75),
        (1022.94, 0.15, 79),
        (float('inf'), 0.05, 182),
    ]
}

AMPLITUDE_KEY = env.get("AMPLITUDE_KEY", "Test key")
import loganalytics
if DEBUG:
    ANALYTICS = loganalytics.test_log_analytics
else:
    ANALYTICS = loganalytics.log_analytics
<commit_msg>Modernised settings.py<commit_after>import os
from os import environ as env
from django.conf import global_settings
import dj_database_url

DEBUG = env.get('DJANGO_DEBUG', 'true') == 'true'
PROJECT_ROOT = os.path.dirname(os.path.realpath(__name__))

ADMINS = (
    ('Adi Eyal', 'adi@openup.org.za'),
)

MANAGERS = ADMINS

DATABASES = {
    # load from DATABASE_URL env var, or default to this
    'default': dj_database_url.config(default='sqlite:///mpr.db')
}

# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = ['code4sa-mpr.herokuapp.com', 'mpr.code4sa.org', 'localhost', '127.0.0.1']

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'Africa/Johannesburg'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: '/var/www/example.com/media/'
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: 'http://example.com/media/', 'http://media.example.com/'
MEDIA_URL = '/media/'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' 'static/' subdirectories and in STATICFILES_DIRS.
# Example: '/var/www/example.com/static/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')

# URL prefix for static files.
# Example: 'http://example.com/static/', 'http://static.example.com/'
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like '/home/html/static' or 'C:/www/django/static'.
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(PROJECT_ROOT, 'mpr', 'static'),
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'pipeline.finders.PipelineFinder',
)

if DEBUG:
    # Make this unique, and don't share it with anybody.
    SECRET_KEY = 'ub7dk%m=d*k=aip1rl)z&v8sj&fg2msc&=km0z3u#5ct9+_43w'
else:
    SECRET_KEY = env.get('DJANGO_SECRET_KEY')

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth'
            ]
        }
    },
]

MIDDLEWARE_CLASSES = (
    'mpr.middleware.CORSMiddleware',
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'mpr.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'mpr.wsgi.application'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'pipeline',
    'mpr',
#    'behave_django',
)

STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'

PIPELINE = {
    'ENABLED': True,
    'JAVASCRIPT': {
        'mprbase' : {
            'source_filenames': (
              'js/jquery-1.10.2.js',
              'js/bootstrap.js',
              'js/jquery.ba-hashchange.js',
              'js/knockout.js',
              'js/survey.ko.js',
              'js/medloader.js',
            ),
            'output_filename': 'js/mprbase.js',
        },
    },
    'STYLESHEETS' : {
        'mpr' : {
            'source_filenames': (
              'css/bootstrap.css',
              #'css/bootstrap-theme.css',
              'css/custom.css',
              #'css/survey.css',
            ),
            'output_filename': 'css/mpr.css',
        }
    },
    'CSS_COMPRESSOR' : 'pipeline.compressors.yuglify.YuglifyCompressor',
    'JS_COMPRESSOR' : 'pipeline.compressors.yuglify.YuglifyCompressor',
    'DISABLE_WRAPPER' : True
}

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format' : '[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s',
            'datefmt' : '%d/%b/%Y %H:%M:%S'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'ERROR',
            'propagate': True,
        },
        'mpr': {
            'handlers': ['console'],
            'level': 'DEBUG' if DEBUG else 'ERROR',
            'propagate': True,
        },
    }
}

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
        'TIMEOUT' : 60*60*24*7
    }
}

SEGMENT_IO_KEY = env.get('SEGMENT_IO_KEY')
SESSION = 'django.contrib.sessions.backends.signed_cookies'

PRICE_PARAMETERS = {
    "VAT" : 1.15,
    "prices" : [
        (109.56, 0.46, 14.50),
        (292.25, 0.33, 27.75),
        (1022.94, 0.15, 79),
        (float('inf'), 0.05, 182),
    ]
}

AMPLITUDE_KEY = env.get("AMPLITUDE_KEY", "Test key")
import loganalytics
if DEBUG:
    ANALYTICS = loganalytics.test_log_analytics
else:
    ANALYTICS = loganalytics.log_analytics
<|endoftext|>


<commit_before>##############################################################################
# Copyright (c) 2013-2016, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://github.com/llnl/spack
# Please also see the NOTICE and LICENSE files for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License (as
# published by the Free Software Foundation) version 2.1, February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
from spack import *
from spack import spack_root

class GobjectIntrospection(Package):
    """The GObject Introspection is used to describe the program APIs and
    collect them in a uniform, machine readable format.Cairo is a 2D graphics
    library with support for multiple output"""

    homepage = "https://wiki.gnome.org/Projects/GObjectIntrospection"
    url      = "http://ftp.gnome.org/pub/gnome/sources/gobject-introspection/1.49/gobject-introspection-1.49.2.tar.xz"

    version('1.49.2', 'c47a76b05b2d8438089f519922180747')
    version('1.48.0', '01301fa9019667d48e927353e08bc218')

    depends_on("glib@2.49.2:", when="@1.49.2:")
    # version 1.48.0 build fails with glib 2.49.4
    depends_on("glib@2.48.1", when="@1.48.0")
    depends_on("python")
    depends_on("cairo")
    depends_on("bison", type="build")
    depends_on("flex", type="build")
    depends_on("pkg-config@0.9.0:", type="build")

    # This package creates several scripts from
    # toosl/g-ir-tool-template.in.  In their original form these
    # scripts end up with a sbang line like
    #
    # `#!/usr/bin/env /path/to/spack/python`.
    #
    # These scripts are generated and then used as part of the build
    # (other packages also use the scripts after they've been
    # installed).
    #
    # The path to the spack python can become too long.  Because these
    # tools are used as part of the build, the normal hook that fixes
    # this problem can't help us.
    # This package fixes the problem in two steps:
    # - it rewrites the g-ir-tool-template so that its sbang line
    #   refers directly to spack's python (filter_file step below); and
    # - it patches the Makefile.in so that the generated Makefile has an
    #   extra sed expression in its TOOL_SUBSTITUTION that results in
    #   an `#!/bin/bash /path/to/spack/bin/sbang` unconditionally being
    #   inserted into the scripts as they're generated.
    patch("sbang.patch")

    def install(self, spec, prefix):
        configure("--prefix=%s" % prefix)
        # we need to filter this file to avoid an overly long hashbang line
        filter_file('#!/usr/bin/env @PYTHON@', '#!@PYTHON@',
                    'tools/g-ir-tool-template.in')
        make()
        make("install")

    def setup_environment(self, spack_env, run_env):
        spack_env.set('SPACK_SBANG', "%s/bin/sbang" % spack_root)
<commit_msg>fix GobjectIntrospection on Darwin (#4872)<commit_after>##############################################################################
# Copyright (c) 2013-2016, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://github.com/llnl/spack
# Please also see the NOTICE and LICENSE files for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License (as
# published by the Free Software Foundation) version 2.1, February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
from spack import *
from spack import spack_root

class GobjectIntrospection(Package):
    """The GObject Introspection is used to describe the program APIs and
    collect them in a uniform, machine readable format.Cairo is a 2D graphics
    library with support for multiple output"""

    homepage = "https://wiki.gnome.org/Projects/GObjectIntrospection"
    url      = "http://ftp.gnome.org/pub/gnome/sources/gobject-introspection/1.49/gobject-introspection-1.49.2.tar.xz"

    version('1.49.2', 'c47a76b05b2d8438089f519922180747')
    version('1.48.0', '01301fa9019667d48e927353e08bc218')

    depends_on("glib@2.49.2:", when="@1.49.2:")
    # version 1.48.0 build fails with glib 2.49.4
    depends_on("glib@2.48.1", when="@1.48.0")
    depends_on("python")
    depends_on("cairo")
    depends_on("bison", type="build")
    depends_on("flex", type="build")
    depends_on("pkg-config@0.9.0:", type="build")

    # GobjectIntrospection does not build with sed from darwin:
    depends_on('sed', when='platform=darwin', type='build')

    # This package creates several scripts from
    # toosl/g-ir-tool-template.in.  In their original form these
    # scripts end up with a sbang line like
    #
    # `#!/usr/bin/env /path/to/spack/python`.
    #
    # These scripts are generated and then used as part of the build
    # (other packages also use the scripts after they've been
    # installed).
    #
    # The path to the spack python can become too long.  Because these
    # tools are used as part of the build, the normal hook that fixes
    # this problem can't help us.
    # This package fixes the problem in two steps:
    # - it rewrites the g-ir-tool-template so that its sbang line
    #   refers directly to spack's python (filter_file step below); and
    # - it patches the Makefile.in so that the generated Makefile has an
    #   extra sed expression in its TOOL_SUBSTITUTION that results in
    #   an `#!/bin/bash /path/to/spack/bin/sbang` unconditionally being
    #   inserted into the scripts as they're generated.
    patch("sbang.patch")

    def install(self, spec, prefix):
        configure("--prefix=%s" % prefix)
        # we need to filter this file to avoid an overly long hashbang line
        filter_file('#!/usr/bin/env @PYTHON@', '#!@PYTHON@',
                    'tools/g-ir-tool-template.in')
        make()
        make("install")

    def setup_environment(self, spack_env, run_env):
        spack_env.set('SPACK_SBANG', "%s/bin/sbang" % spack_root)
<|endoftext|>