<commit_before># Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import numpy as np
import astropy.units as u
from astropy.time import Time
import warnings

from ..exceptions import PlotBelowHorizonWarning

__all__ = ['plot_sky']

@u.quantity_input(az_label_offset=u.deg)
def plot_sky(target, observer, time, ax=None, style_kwargs=None,
             north_to_east_ccw=True, grid=True, az_label_offset=0.0*u.deg):
    """
    Plots target positions in the sky with respect to the observer's location.

    If a `~matplotlib.axes.Axes` object already exists, plots an additional
    target position on top.
    Otherwise, creates a new `~matplotlib.axes.Axes` object with a sky plot.

    Can pass in a scalar `~astropy.time.Time` object (e.g. ``Time('2000-1-1')``)
    or an array of length one (e.g. ``Time(['2000-1-1'])``) to get plot at one
    instance in time.
    If pass in an `~astropy.time.Time` object with multiple instances of time
    (e.g. ``Time(['2000-1-1 20:00:00', '2000-1-1 20:30:00'])``), target's
    position will be shown at each of these times.

    Parameters
    ----------
    target : `~astroplan.FixedTarget`
        The celestial body of interest.

    observer: `~astroplan.Observer`
        The person, telescope, observatory, etc. doing the observing.

    time : `~astropy.time.Time`
        If pass in an `~astropy.time.Time` object with just one instance in
        time, whether it be a scalar or an array (e.g. ``Time('2000-1-1')``,
        ``Time(['2000-1-1'])``, ``[Time('2000-1-1')]``),
        `~astroplan.plots.plot_sky` will return plot at one instance in
        time.  If pass in an `~astropy.time.Time` object with multiple
        instances in time (e.g. ``Time(['2000-1-1', '2000-1-2'])``) will
        show positions plotted at the exact times specified.

    ax : `~matplotlib.axes.Axes` or None, optional.
        The `~matplotlib.axes.Axes` object to be drawn on.
        If None, uses the current `~matplotlib.axes.Axes`.

    style_kwargs : dict or None, optional.
        A dictionary of keywords passed into `~matplotlib.pyplot.scatter`
        to set plotting styles.

    north_to_east_ccw : bool, optional.
        True by default, meaning that azimuth is shown increasing
        counter-clockwise (CCW), or with North at top, East at left, etc.
        To show azimuth increasing clockwise (CW), set to False.

    grid : bool, optional.
        True by default, meaning that grid is drawn.

    az_label_offset : ``~astropy.units.degree``, optional.
        DANGER: It is not recommended that you change the default behavior,
        as to do so makes it seem as if N/E/S/W are being decoupled from the
        definition of azimuth (North from az = 0 deg., East from az = 90 deg.,
        etc.).
        An offset for azimuth labels from the North label.  A positive
        offset will increase in the same direction as azimuth
        (see ``north_to_east_ccw`` option).

    Returns
    -------
    An `~matplotlib.axes.Axes` object (ax) with a map of the sky.

    Notes
    -----
    Using `~astropy.time.Time` objects:
        See `Astropy`_ documentation for more details.

    Coordinate defaults:

        Altazimuth (local horizon) coordinate system.  North is always at top
        of plot, South is always at the bottom, E/W can be right or left
        depending on the ``north_to_east_cw`` option.

        Altitude: 90 degrees (zenith) is at plot origin (center) and 0 degrees
        (horizon) is at plot edge.  This cannot be changed by user.

        Azimuth: 0 degrees is at North (top of plot), 90 degrees at East, etc.
        DANGER: Azimuth labels can be changed by user via the
        ``az_label_offset`` option, but it is not recommended, as to do so
        makes it seem as if N/E/S/W are being decoupled from the definition
        of azimuth (North from az = 0 deg., East from az = 90 deg., etc.).
    """
    import matplotlib.pyplot as plt

    # Set up axes & plot styles if needed.
    if ax is None:
        ax = plt.gca(projection='polar')
    if style_kwargs is None:
        style_kwargs = {}
    style_kwargs = dict(style_kwargs)
    style_kwargs.setdefault('marker', 'o')

    # Turn scalar Time objects into arrays.
    time = Time(time)
    if time.isscalar:
        time = Time([time])

    # Grab altitude and azimuth from Astroplan objects.
    # Note that values must be made dimensionless before plotting.
    # Modifying altitude is easier than inverting r-axis.
    altitude = (91 * u.deg - observer.altaz(time, target).alt) * (1/u.deg)
    # Azimuth MUST be given to plot() in radians.
    azimuth = observer.altaz(time, target).az * (1/u.deg) * (np.pi/180.0)

    # Some checks & info for labels.
    if not hasattr(target, 'name'):
        target_name = ''
    else:
        target_name = target.name
    style_kwargs.setdefault('label', target_name)

    # We only want to plot positions above the horizon.
    az_plot = None
    for alt in range(0, len(altitude)):
        if altitude[alt] > 91.0:
            msg = 'Target "{0}" is below the horizon at time: {1}'
            msg = msg.format(target_name if target_name else 'Unknown Name',
                             time[alt])
            warnings.warn(msg, PlotBelowHorizonWarning)
        else:
            if az_plot is None:
                az_plot = np.array([azimuth[alt]])
            else:
                az_plot = np.append(az_plot, azimuth[alt])
    alt_plot = altitude[altitude <= 91.0]
    if az_plot is None:
        az_plot = []

    # More axes set-up.
    # Position of azimuth = 0 (data, not label).
    ax.set_theta_zero_location('N')

    # Direction of azimuth increase. Clockwise is -1
    if north_to_east_ccw is False:
        ax.set_theta_direction(-1)

    # Plot target coordinates.
    ax.scatter(az_plot, alt_plot, **style_kwargs)

    # Set radial limits.
    ax.set_rlim(1, 91)

    # Grid, ticks & labels.
    # May need to set ticks and labels AFTER plotting points.
    if grid is True:
        ax.grid(True, which='major')
    if grid is False:
        ax.grid(False)
    degree_sign = u'\N{DEGREE SIGN}'

    # For positively-increasing range (e.g., range(1, 90, 15)),
    # labels go from middle to outside.
    r_labels = [
        '90' + degree_sign,
        '',
        '60' + degree_sign,
        '',
        '30' + degree_sign,
        '',
        '0' + degree_sign + ' Alt.',
        ]

    theta_labels = []
    for chunk in range(0, 7):
        label_angle = (az_label_offset*(1/u.deg)) + (chunk*45.0)
        while label_angle >= 360.0:
            label_angle -= 360.0
        if chunk == 0:
            theta_labels.append('N ' + '\n' + str(label_angle) + degree_sign
                                + ' Az')
        elif chunk == 2:
            theta_labels.append('E' + '\n' + str(label_angle) + degree_sign)
        elif chunk == 4:
            theta_labels.append('S' + '\n' + str(label_angle) + degree_sign)
        elif chunk == 6:
            theta_labels.append('W' + '\n' + str(label_angle) + degree_sign)
        else:
            theta_labels.append(str(label_angle) + degree_sign)

    # Set ticks and labels.
    ax.set_rgrids(range(1, 106, 15), r_labels, angle=-45)
    ax.set_thetagrids(range(0, 360, 45), theta_labels, frac=1.2)

    # Below commands don't seem to work for setting ticks/labels.
    # ax.rgrids(range(1, 91, 15), r_labels, angle=-45)
    # ax.thetagrids(range(0, 360, 45), theta_labels)

    # Redraw the figure for interactive sessions.
    ax.figure.canvas.draw()

    return ax
<commit_msg>Adding requested plot_sky_24hr [#51]<commit_after># Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import numpy as np
import astropy.units as u
from astropy.time import Time
import warnings

from ..exceptions import PlotBelowHorizonWarning

__all__ = ['plot_sky', 'plot_sky_24hr']

@u.quantity_input(az_label_offset=u.deg)
def plot_sky(target, observer, time, ax=None, style_kwargs=None,
             north_to_east_ccw=True, grid=True, az_label_offset=0.0*u.deg,
             warn_below_horizon=True):
    """
    Plots target positions in the sky with respect to the observer's location.

    If a `~matplotlib.axes.Axes` object already exists, plots an additional
    target position on top.
    Otherwise, creates a new `~matplotlib.axes.Axes` object with a sky plot.

    Can pass in a scalar `~astropy.time.Time` object (e.g. ``Time('2000-1-1')``)
    or an array of length one (e.g. ``Time(['2000-1-1'])``) to get plot at one
    instance in time.
    If pass in an `~astropy.time.Time` object with multiple instances of time
    (e.g. ``Time(['2000-1-1 20:00:00', '2000-1-1 20:30:00'])``), target's
    position will be shown at each of these times.

    Parameters
    ----------
    target : `~astroplan.FixedTarget`
        The celestial body of interest.

    observer: `~astroplan.Observer`
        The person, telescope, observatory, etc. doing the observing.

    time : `~astropy.time.Time`
        If pass in an `~astropy.time.Time` object with just one instance in
        time, whether it be a scalar or an array (e.g. ``Time('2000-1-1')``,
        ``Time(['2000-1-1'])``, ``[Time('2000-1-1')]``),
        `~astroplan.plots.plot_sky` will return plot at one instance in
        time.  If pass in an `~astropy.time.Time` object with multiple
        instances in time (e.g. ``Time(['2000-1-1', '2000-1-2'])``) will
        show positions plotted at the exact times specified.

    ax : `~matplotlib.axes.Axes` or None, optional.
        The `~matplotlib.axes.Axes` object to be drawn on.
        If None, uses the current `~matplotlib.axes.Axes`.

    style_kwargs : dict or None, optional.
        A dictionary of keywords passed into `~matplotlib.pyplot.scatter`
        to set plotting styles.

    north_to_east_ccw : bool, optional.
        True by default, meaning that azimuth is shown increasing
        counter-clockwise (CCW), or with North at top, East at left, etc.
        To show azimuth increasing clockwise (CW), set to False.

    grid : bool, optional.
        True by default, meaning that grid is drawn.

    az_label_offset : ``~astropy.units.degree``, optional.
        DANGER: It is not recommended that you change the default behavior,
        as to do so makes it seem as if N/E/S/W are being decoupled from the
        definition of azimuth (North from az = 0 deg., East from az = 90 deg.,
        etc.).
        An offset for azimuth labels from the North label.  A positive
        offset will increase in the same direction as azimuth
        (see ``north_to_east_ccw`` option).

    warn_below_horizon : bool, optional
        If `False`, don't show warnings when attempting to plot targets below
        the horzion.

    Returns
    -------
    An `~matplotlib.axes.Axes` object (ax) with a map of the sky.

    Notes
    -----
    Using `~astropy.time.Time` objects:
        See `Astropy`_ documentation for more details.

    Coordinate defaults:

        Altazimuth (local horizon) coordinate system.  North is always at top
        of plot, South is always at the bottom, E/W can be right or left
        depending on the ``north_to_east_cw`` option.

        Altitude: 90 degrees (zenith) is at plot origin (center) and 0 degrees
        (horizon) is at plot edge.  This cannot be changed by user.

        Azimuth: 0 degrees is at North (top of plot), 90 degrees at East, etc.
        DANGER: Azimuth labels can be changed by user via the
        ``az_label_offset`` option, but it is not recommended, as to do so
        makes it seem as if N/E/S/W are being decoupled from the definition
        of azimuth (North from az = 0 deg., East from az = 90 deg., etc.).
    """
    import matplotlib.pyplot as plt

    # Set up axes & plot styles if needed.
    if ax is None:
        ax = plt.gca(projection='polar')
    if style_kwargs is None:
        style_kwargs = {}
    style_kwargs = dict(style_kwargs)
    style_kwargs.setdefault('marker', 'o')

    # Turn scalar Time objects into arrays.
    time = Time(time)
    if time.isscalar:
        time = Time([time])

    # Grab altitude and azimuth from Astroplan objects.
    # Note that values must be made dimensionless before plotting.
    # Modifying altitude is easier than inverting r-axis.
    altitude = (91 * u.deg - observer.altaz(time, target).alt) * (1/u.deg)
    # Azimuth MUST be given to plot() in radians.
    azimuth = observer.altaz(time, target).az * (1/u.deg) * (np.pi/180.0)

    # Some checks & info for labels.
    if not hasattr(target, 'name'):
        target_name = ''
    else:
        target_name = target.name
    style_kwargs.setdefault('label', target_name)

    # We only want to plot positions above the horizon.
    az_plot = None
    for alt in range(0, len(altitude)):
        if altitude[alt] > 91.0:
            if warn_below_horizon:
                msg = 'Target "{0}" is below the horizon at time: {1}'
                msg = msg.format(target_name if target_name else 'Unknown Name',
                                 time[alt])
                warnings.warn(msg, PlotBelowHorizonWarning)
        else:
            if az_plot is None:
                az_plot = np.array([azimuth[alt]])
            else:
                az_plot = np.append(az_plot, azimuth[alt])
    alt_plot = altitude[altitude <= 91.0]
    if az_plot is None:
        az_plot = []

    # More axes set-up.
    # Position of azimuth = 0 (data, not label).
    ax.set_theta_zero_location('N')

    # Direction of azimuth increase. Clockwise is -1
    if north_to_east_ccw is False:
        ax.set_theta_direction(-1)

    # Plot target coordinates.
    ax.scatter(az_plot, alt_plot, **style_kwargs)

    # Set radial limits.
    ax.set_rlim(1, 91)

    # Grid, ticks & labels.
    # May need to set ticks and labels AFTER plotting points.
    if grid is True:
        ax.grid(True, which='major')
    if grid is False:
        ax.grid(False)
    degree_sign = u'\N{DEGREE SIGN}'

    # For positively-increasing range (e.g., range(1, 90, 15)),
    # labels go from middle to outside.
    r_labels = [
        '90' + degree_sign,
        '',
        '60' + degree_sign,
        '',
        '30' + degree_sign,
        '',
        '0' + degree_sign + ' Alt.',
        ]

    theta_labels = []
    for chunk in range(0, 7):
        label_angle = (az_label_offset*(1/u.deg)) + (chunk*45.0)
        while label_angle >= 360.0:
            label_angle -= 360.0
        if chunk == 0:
            theta_labels.append('N ' + '\n' + str(label_angle) + degree_sign
                                + ' Az')
        elif chunk == 2:
            theta_labels.append('E' + '\n' + str(label_angle) + degree_sign)
        elif chunk == 4:
            theta_labels.append('S' + '\n' + str(label_angle) + degree_sign)
        elif chunk == 6:
            theta_labels.append('W' + '\n' + str(label_angle) + degree_sign)
        else:
            theta_labels.append(str(label_angle) + degree_sign)

    # Set ticks and labels.
    ax.set_rgrids(range(1, 106, 15), r_labels, angle=-45)
    ax.set_thetagrids(range(0, 360, 45), theta_labels, frac=1.2)

    # Below commands don't seem to work for setting ticks/labels.
    # ax.rgrids(range(1, 91, 15), r_labels, angle=-45)
    # ax.thetagrids(range(0, 360, 45), theta_labels)

    # Redraw the figure for interactive sessions.
    ax.figure.canvas.draw()

    return ax

@u.quantity_input(delta=u.hour)
def plot_sky_24hr(target, observer, time, delta=1*u.hour, ax=None,
                  style_kwargs=None, north_to_east_ccw=True, grid=True,
                  az_label_offset=0.0*u.deg, center_time_style_kwargs=None):
    """
    Plots target positions in the sky with respect to the observer's location
    over a twenty-four hour period centered on ``time``.

    Most arguments are copies of those in `~astroplan.plots.plot_sky`, see
    docs for them. The unique keywords for this function include:

    Parameters
    ----------
    delta : `~astropy.units.Quantity` (optional)
        Interval between times plotted.

    center_time_style_kwargs : dict or `None` (optional)
        Dictionary of style keyword arguments to pass to
        `~matplotlib.pyplot.scatter` to set plotting style of the point at
        time ``time``.

    Returns
    -------
    An `~matplotlib.axes.Axes` object (ax) with a map of the sky.
    """

    time_range = np.arange(-12, 12, delta.to(u.hour).value)*u.hour + time

    ax = plot_sky(target, observer, time_range, ax=ax,
                  style_kwargs=style_kwargs,
                  north_to_east_ccw=north_to_east_ccw, grid=grid,
                  az_label_offset=az_label_offset,
                  warn_below_horizon=False)

    if center_time_style_kwargs is not None:
        ax = plot_sky(target, observer, time, ax=ax,
                      style_kwargs=center_time_style_kwargs,
                      north_to_east_ccw=north_to_east_ccw, grid=grid,
                      az_label_offset=az_label_offset,
                      warn_below_horizon=False)
    return ax
<|endoftext|>


<commit_before>__author__ = 'mslabicki'

from PyGMO import *
from modules.colorOptimizationProblemsDef import maximalThroughputProblemRR
from modules.colorOptimizationProblemsDef import maximalThroughputProblemFS

import copy

numberOfThreads = 4

class pygmoColorConfigurator:
    def __init__(self,parent):
        self.parent = parent

    def findColorsMaxThrRR(self,gen):
        prob = maximalThroughputProblemRR(dim=len(self.parent.bs))
        prob.siec = copy.deepcopy(self.parent)
        algo = algorithm.sga(gen=gen) # 100
        archi = archipelago(algo, prob, numberOfThreads, 10)
        archi.evolve(10)
        archi.join()
        theBestCostF = 0
        islandNumber = -1
        islandCounter = 0
        for island in archi:
            if theBestCostF > island.population.champion.f[0]:
                theBestCostF = island.population.champion.f[0]
                islandNumber = islandCounter
            islandCounter = islandCounter + 1
        for i in range(len(self.parent.bs)):
            self.parent.bs[i].color = archi[islandNumber].population.champion.x[i]

    def findColorsMaxThrRR_SEA(self,gen):
        prob = maximalThroughputProblemRR(dim=len(self.parent.bs))
        prob.siec = copy.deepcopy(self.parent)
        algo = algorithm.sea(gen=gen)
        archi = archipelago(algo, prob, numberOfThreads, 10)
        archi.evolve(10)
        archi.join()
        theBestCostF = 0
        islandNumber = -1
        islandCounter = 0
        for island in archi:
            if theBestCostF > island.population.champion.f[0]:
                theBestCostF = island.population.champion.f[0]
                islandNumber = islandCounter
            islandCounter = islandCounter + 1
        for i in range(len(self.parent.bs)):
            self.parent.bs[i].color = archi[islandNumber].population.champion.x[i]

    def findColorsMaxThrFS(self,gen):
        prob = maximalThroughputProblemFS(dim=len(self.parent.bs))
        prob.siec = copy.deepcopy(self.parent)
        algo = algorithm.sga(gen=gen) # 100
        archi = archipelago(algo, prob, numberOfThreads, 10)
        archi.evolve(10)
        archi.join()
        theBestCostF = 0
        islandNumber = -1
        islandCounter = 0
        for island in archi:
            if theBestCostF > island.population.champion.f[0]:
                theBestCostF = island.population.champion.f[0]
                islandNumber = islandCounter
            islandCounter = islandCounter + 1
        for i in range(len(self.parent.bs)):
            self.parent.bs[i].color = archi[islandNumber].population.champion.x[i]

    def findColorsMaxThrFS_SEA(self,gen):
        prob = maximalThroughputProblemFS(dim=len(self.parent.bs))
        prob.siec = copy.deepcopy(self.parent)
        algo = algorithm.sea(gen=gen)
        archi = archipelago(algo, prob, numberOfThreads, 10)
        archi.evolve(10)
        archi.join()
        theBestCostF = 0
        islandNumber = -1
        islandCounter = 0
        for island in archi:
            if theBestCostF > island.population.champion.f[0]:
                theBestCostF = island.population.champion.f[0]
                islandNumber = islandCounter
            islandCounter = islandCounter + 1
        for i in range(len(self.parent.bs)):
            self.parent.bs[i].color = archi[islandNumber].population.champion.x[i]<commit_msg>Change number of threads form 4 to 6<commit_after>__author__ = 'mslabicki'

from PyGMO import *
from modules.colorOptimizationProblemsDef import maximalThroughputProblemRR
from modules.colorOptimizationProblemsDef import maximalThroughputProblemFS

import copy

numberOfThreads = 6

class pygmoColorConfigurator:
    def __init__(self,parent):
        self.parent = parent

    def findColorsMaxThrRR(self,gen):
        prob = maximalThroughputProblemRR(dim=len(self.parent.bs))
        prob.siec = copy.deepcopy(self.parent)
        algo = algorithm.sga(gen=gen) # 100
        archi = archipelago(algo, prob, numberOfThreads, 10)
        archi.evolve(10)
        archi.join()
        theBestCostF = 0
        islandNumber = -1
        islandCounter = 0
        for island in archi:
            if theBestCostF > island.population.champion.f[0]:
                theBestCostF = island.population.champion.f[0]
                islandNumber = islandCounter
            islandCounter = islandCounter + 1
        for i in range(len(self.parent.bs)):
            self.parent.bs[i].color = archi[islandNumber].population.champion.x[i]

    def findColorsMaxThrRR_SEA(self,gen):
        prob = maximalThroughputProblemRR(dim=len(self.parent.bs))
        prob.siec = copy.deepcopy(self.parent)
        algo = algorithm.sea(gen=gen)
        archi = archipelago(algo, prob, numberOfThreads, 10)
        archi.evolve(10)
        archi.join()
        theBestCostF = 0
        islandNumber = -1
        islandCounter = 0
        for island in archi:
            if theBestCostF > island.population.champion.f[0]:
                theBestCostF = island.population.champion.f[0]
                islandNumber = islandCounter
            islandCounter = islandCounter + 1
        for i in range(len(self.parent.bs)):
            self.parent.bs[i].color = archi[islandNumber].population.champion.x[i]

    def findColorsMaxThrFS(self,gen):
        prob = maximalThroughputProblemFS(dim=len(self.parent.bs))
        prob.siec = copy.deepcopy(self.parent)
        algo = algorithm.sga(gen=gen) # 100
        archi = archipelago(algo, prob, numberOfThreads, 10)
        archi.evolve(10)
        archi.join()
        theBestCostF = 0
        islandNumber = -1
        islandCounter = 0
        for island in archi:
            if theBestCostF > island.population.champion.f[0]:
                theBestCostF = island.population.champion.f[0]
                islandNumber = islandCounter
            islandCounter = islandCounter + 1
        for i in range(len(self.parent.bs)):
            self.parent.bs[i].color = archi[islandNumber].population.champion.x[i]

    def findColorsMaxThrFS_SEA(self,gen):
        prob = maximalThroughputProblemFS(dim=len(self.parent.bs))
        prob.siec = copy.deepcopy(self.parent)
        algo = algorithm.sea(gen=gen)
        archi = archipelago(algo, prob, numberOfThreads, 10)
        archi.evolve(10)
        archi.join()
        theBestCostF = 0
        islandNumber = -1
        islandCounter = 0
        for island in archi:
            if theBestCostF > island.population.champion.f[0]:
                theBestCostF = island.population.champion.f[0]
                islandNumber = islandCounter
            islandCounter = islandCounter + 1
        for i in range(len(self.parent.bs)):
            self.parent.bs[i].color = archi[islandNumber].population.champion.x[i]<|endoftext|>


<commit_before>#!/usr/bin/env python

from __future__ import print_function
import json
import os
import re
import subprocess
import sys

line_rx = re.compile('(?:(?P<dir>.+)\/)?(?P<file>.*){1}(?:\:(?P<comment>.*?))?')

basepath = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
datapath = os.path.join(basepath, 'data')
orchids_lst = os.path.join(datapath, "orchids.lst")

dest_path = os.path.join(basepath, "app", "images", "orchids")
thumb_path = os.path.join(dest_path, "256x192")

if not os.path.exists(thumb_path):
    os.mkdir(thumb_path)

src_path = os.path.join("/archive/Pictures/orchids")
if not os.path.exists(src_path):
    print("image source path does not exist: " + src_path)
    sys.exit(1)

def filename_to_latin(filename):
    # TODO: this could be replaced with a regex but we just want to get it working for now
    index = filename.find("-")
    if index > -1:
        filename = filename[0:index]
    else:
        filename = filename[0:filename.find(".")]
    filename = filename.replace("_subsp_", " subsp. ")
    filename = filename.replace("_var_ ", " var. ")
    return filename.replace('_', ' ').capitalize()

def create_small_image(filename):
    pass

def create_large_image(filename):
    pass

orchids_lst=os.path.abspath(os.path.join(datapath, 'orchids.lst'))
for line in open(orchids_lst):
    line = line.strip()
    match = line_rx.match(line)
    if not match:
        raise ValueError("line does not match regex: " + line)

    groups = match.groupdict()
    filename = groups["file"]
    path = os.path.join(src_path, groups["dir"], filename)
    if not os.path.exists(path):
        print("file does not exist: ", path)
        sys.exit(1)

    # create the thumbnail
    thumb_filename = os.path.join(thumb_path, filename)
    # TODO: create a command line arg to overwrite files if they exists
    # if os.path.exists(path):
    #     print("file already exists", thumb_filename)
    #     sys.exit(1)

    print("converting: ", path)
    subprocess.call(["convert", path, "-thumbnail", "256x192", thumb_filename])

    # TODO: pass image through jpegtran to make it smaller

    # TODO: create a larger version of the same image to display when we click on itpp
<commit_msg>use new pictures.lst for generating thumbnails<commit_after>#!/usr/bin/env python

from __future__ import print_function
import json
import os
import re
import subprocess
import sys

line_rx = re.compile('(?:(?P<dir>.+)\/)?(?P<file>.*){1}(?:\:(?P<comment>.*?))?')

basepath = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
datapath = os.path.join(basepath, 'data')
pictures_lst = os.path.join(datapath, "pictures.lst")

dest_path = os.path.join(basepath, "app", "images", "orchids")
thumb_path = os.path.join(dest_path, "256x192")

if not os.path.exists(thumb_path):
    os.mkdir(thumb_path)

src_path = os.path.join("/archive/Pictures/orchids")
if not os.path.exists(src_path):
    print("image source path does not exist: " + src_path)
    sys.exit(1)

def filename_to_latin(filename):
    # TODO: this could be replaced with a regex but we just want to get it working for now
    index = filename.find("-")
    if index > -1:
        filename = filename[0:index]
    else:
        filename = filename[0:filename.find(".")]
    filename = filename.replace("_subsp_", " subsp. ")
    filename = filename.replace("_var_ ", " var. ")
    return filename.replace('_', ' ').capitalize()

def create_small_image(filename):
    pass

def create_large_image(filename):
    pass

for line in open(pictures_lst):
    line = line.strip()
    match = line_rx.match(line)
    if not match:
        raise ValueError("line does not match regex: " + line)

    groups = match.groupdict()
    filename = groups["file"]
    path = os.path.join(src_path, groups["dir"], filename)
    if not os.path.exists(path):
        print("file does not exist: ", path)
        sys.exit(1)

    # create the thumbnail
    thumb_filename = os.path.join(thumb_path, filename)
    if os.path.exists(thumb_filename):
        # TODO: create a command line arg to overwrite files if they exists
        #print("file already exists", thumb_filename)
        #sys.exit(1)
        pass
    else:
        print("converting: ", path)
        subprocess.call(["convert", path, "-thumbnail", "256x192", thumb_filename])

    # TODO: pass image through jpegtran to make it smaller

    # TODO: create a larger version of the same image to display when we click
    # on itpp
<|endoftext|>


<commit_before>from .permissions import is_location_safe, location_restricted_response

class LocationAccessMiddleware(object):
    """
    Many large projects want to restrict data access by location.
    Views which handle that properly are called "location safe". This
    middleware blocks access to any non location safe features by users who
    have such a restriction. If these users do not have an assigned location,
    they cannot access anything.
    """

    def process_view(self, request, view_fn, view_args, view_kwargs):
        user = getattr(request, 'couch_user', None)
        domain = getattr(request, 'domain', None)
        if not domain or not user or not user.is_member_of(domain):
            # This is probably some non-domain page or a test, let normal auth handle it
            request.can_access_all_locations = True
        elif user.has_permission(domain, 'access_all_locations'):
            request.can_access_all_locations = True
        else:
            request.can_access_all_locations = False
            if (
                not is_location_safe(view_fn, view_args, view_kwargs)
                or not user.get_sql_location(domain)
            ):
                return location_restricted_response(request)
<commit_msg>Clarify usage in docstring [ci skip]<commit_after>from .permissions import is_location_safe, location_restricted_response

class LocationAccessMiddleware(object):
    """
    Many large projects want to restrict data access by location.
    Views which handle that properly are called "location safe". This
    middleware blocks access to any non location safe features by users who
    have such a restriction. If these users do not have an assigned location,
    they cannot access anything.

    This middleware also sets the property can_access_all_locations. This
    property does not imply domain level authentication or access to any
    particular feature. All it says is that whether or not the user has a role
    which restricts their data access by location.
    """

    def process_view(self, request, view_fn, view_args, view_kwargs):
        user = getattr(request, 'couch_user', None)
        domain = getattr(request, 'domain', None)
        if not domain or not user or not user.is_member_of(domain):
            # This is probably some non-domain page or a test, let normal auth handle it
            request.can_access_all_locations = True
        elif user.has_permission(domain, 'access_all_locations'):
            request.can_access_all_locations = True
        else:
            request.can_access_all_locations = False
            if (
                not is_location_safe(view_fn, view_args, view_kwargs)
                or not user.get_sql_location(domain)
            ):
                return location_restricted_response(request)
<|endoftext|>


<commit_before>import datetime

from django.db import models

class Key(models.Model):
    uid = models.CharField(max_length=255, unique=True)

    name = models.TextField()

    created = models.DateTimeField(default=datetime.datetime.utcnow)

    class Meta:
        ordering = ('-created',)
        get_latest_by = 'created'

    def __unicode__(self):
        return u"pk=%d uid=%r name=%r" % (
            self.pk,
            self.uid,
            self.name,
        )
<commit_msg>Populate key upon creation.<commit_after>import datetime

from django.db import models, transaction

class Key(models.Model):
    uid = models.CharField(max_length=255, unique=True)

    name = models.TextField()

    created = models.DateTimeField(default=datetime.datetime.utcnow)

    class Meta:
        ordering = ('-created',)
        get_latest_by = 'created'

    def __unicode__(self):
        return u"pk=%d uid=%r name=%r" % (
            self.pk,
            self.uid,
            self.name,
        )

    def save(self, *args, **kwargs):
        created = not self.pk

        super(Key, self).save(*args, **kwargs)

        if created:
            from .tasks import update_or_create_key

            transaction.on_commit(lambda: update_or_create_key.delay(self.pk))
<|endoftext|>


<commit_before># -*- coding: utf_8 -*-
import socket
import ssl
import time
import re
import random
from threading import *
import sqlite3
import string
import joinlist
counter = 0
re.purge() # some housekeeping
server = "irc.chat.twitch.tv"
port = 443
channohash = "thor10768765"  # target channel without the hashkey
channel = "#" + channohash
#currentchan = channohash
joincount = 0
conn = sqlite3.connect('chatlog_db.db')
c = conn.cursor()

import os.path

counting = 1
def joinnxt(irc):
    global channohash
    #global joinlist
    global counting
    for i in joinlist.joinlist:
        if counting > len(joinlist.joinlist):
            counting = 2
            break

        if i != channohash and counting == 1:
            print i
            continue

        if counting > 1:
            if not os.path.exists(i):
                tempy = i + "/bot6"
                os.makedirs(tempy)
                channohash = i
                irc.send("JOIN " + "#" + i + "\r\n")
                counting = 2
                break
            else:
                continue

        if i == channohash:
            # if it is kill the folder
            if os.path.exists(i):
                if not os.path.exists(i + "/bot6"):
                    print "ohsheet"
                    counting += 1
                    continue
                tempy = i + "/bot6"
                os.removedirs(tempy)

                print "removed"
                if counting > len(joinlist.joinlist):
                    counting = 2
                    break
                counting += 1
                continue # move to next item

# init socket
self = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# connect to said socket
self.connect((server, port))
# wrap in ssl
irc = ssl.wrap_socket(self)

##################################
# login  #
# irc.send("PASS " + botcfg.oa + '\r\n')
irc.send("NICK " + "justinfan420" + '\r\n')
# capabilities request
irc.send("CAP REQ :twitch.tv/membership" + "\r\n")
# and join channel

if not os.path.exists(channohash):
    tmp = channohash + "/bot6"
    os.makedirs(tmp)
    irc.send("JOIN " + channel + '\r\n')

else:
    joinnxt(irc)

##############################################################
irc.send("CAP REQ :twitch.tv/tags" + "\r\n")
########"@"@"@"@"@"@"@"@"@"@""@"@

CHAT_MSG=re.compile(r"@.+?PRIVMSG.+?(:){1}") # New (for irc flags mode)

datatemp = ""

dtemp = ""
def checker(irC):
    global dtemp

    if dtemp == data:
        print "sames"

        joinnxt(irC)
    dtemp = data
    tr = Timer(10.0, checker, [irC])
    tr.start()

ctimr = Timer (5.0, checker, [irc])
ctimr.start()

while True:

        #gets output from IRC server

    data = irc.recv(1204)
    try:
        data = irc.recv(1204)
    except Exception as e:
        print e

    datatemp = data

   # ping/pong
    if data == "PING :tmi.twitch.tv\r\n":
        irc.send("PONG :tmi.twitch.tv\r\n")

    user = data.split('!', 1)[-1]

    user = user.split('@')[0]

    message = CHAT_MSG.sub("", data)
    flags = data.split(':', 1)[0]

    print (user + ": " + message) # new (for flags mode)

    try:
        unicode(message[0:5], "utf-8")
        if "tmi.twitch.tv" not in (user) and "tmi.twitch.tv" not in (message):
            if "jtv MODE" not in (user) and "justinfan" not in (user) and user != "twitchnotify":
                date = time.strftime("%Y-%m-%dT%H:%M:%S")
                  #  c.execute('select * from clog6')
                c.execute('select count (*) from clog6')
                temp = c.fetchone()
                #print temp[0]
                temp = temp[0] + 1
                c.execute('insert into clog6 values (?,?,?,?,?)',
                 (user, message, temp, flags, date))
                conn.commit()

    except Exception as (e):
        date = time.strftime("%Y-%m-%dT%H:%M:%S")
        c.execute('select count (*) from clog6')
        temp = c.fetchone()
        #print temp[0]
        temp = temp[0] + 1
        conn.text_factory = 'utf-8'
        c.execute('insert into clog6 values (?,?,?,?,?)',
                 (user, message, temp, flags, date))
        conn.commit()

        conn.text_factory = 'string'

    time.sleep(0.1)

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

<commit_msg>Delete chatlog6.py<commit_after><|endoftext|>


<commit_before>import re
from django.db import models
from django.core.urlresolvers import reverse_lazy
from django.contrib.auth import get_user_model
from django.contrib.gis.geos import Point
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from stores.utils import caching_geo_lookup

USER_MODEL = get_user_model()

class Chain(models.Model):
    name = models.CharField('Name', max_length=200)
    slug = models.SlugField('URL Slug', max_length=200, blank=True)
    head_office = models.ForeignKey('stores.Address', blank=True, null=True)
    website = models.URLField('Website', blank=True, null=True)
    editor = models.ForeignKey(USER_MODEL, related_name='editable_chains', blank=True, null=True)
    active = models.BooleanField('Active?', default=True)

    long_description = models.TextField('Description', null=True, blank=True)
    links = generic.GenericRelation('stores.Link', content_type_field='object_type')

    def save(self, **kwargs):
        if self.slug == '':
            self.slug = re.sub(r'\W+', '-', str(self.name).lower())
        return super(Chain, self).save(**kwargs)

    def get_absolute_url(self):
        return reverse_lazy('chain-detail', args=[self.slug])

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ['name']
        app_label = 'stores'

class Store(models.Model):

    STORE_TYPE_RETAIL = 1
    STORE_TYPE_ONLINE = 2
    STORE_TYPE_BOTH = 3

    STORE_TYPE_CHOICES = (
        (STORE_TYPE_RETAIL, 'Retail Only'),
        (STORE_TYPE_ONLINE, 'Online Only'),
        (STORE_TYPE_BOTH, 'Retail & Online'),
    )

    name = models.CharField('Name', max_length=200, help_text="Store's full name")
    slug = models.SlugField('URL Slug', max_length=200, blank=True)
    address = models.ForeignKey('stores.Address', related_name='stores')
    store_type = models.IntegerField('Store Type', choices=STORE_TYPE_CHOICES, default=STORE_TYPE_RETAIL)
    chain = models.ForeignKey(Chain, related_name='stores', null=True, blank=True)
    editor = models.ForeignKey(USER_MODEL, related_name='editable_stores', null=True, blank=True)
    active = models.BooleanField('Active?', default=True)

    website = website = models.URLField('Website', null=True, blank=True)
    email = models.EmailField('Email', null=True, blank=True, help_text="Contact email address for the store.")
    phone = models.CharField('Phone', max_length=25, null=True, blank=True, help_text="Contact phone number for the store.")
    links = generic.GenericRelation('stores.Link', content_type_field='object_type')

    long_description = models.TextField('Description', null=True, blank=True, help_text="Full description of the store, including any marketing material. Markdown supported.")
    brands = models.ManyToManyField('stores.Brand', null=True, blank=True, help_text="Brands that are sold by this store.")

    def get_full_address(self):
        if self.address:
            return self.address.full_address

    def get_location(self):
        if self.address:
            return self.address.geo_location

    def save(self, **kwargs):
        if not self.slug or self.slug == '':
            self.slug = re.sub(r'\W+', '-', str(self.name).lower())
        return super(Store, self).save(**kwargs)

    def get_absolute_url(self):
        return reverse_lazy('store-detail', args=[self.slug])

    def __unicode__(self):
        if not self.name and self.chain:
            return self.chain.name
        return self.name

    class Meta:
        ordering = ['name']
        app_label = 'stores'

class Brand(models.Model):
    name = models.CharField('Brand Name', max_length=200)
    website = models.URLField('Brand Website', null=True, blank=True)

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ['name']
        app_label = 'stores'

class County(models.Model):
    """
    UK Counties
    """
    name = models.CharField('Name', max_length=100)
    country = models.ForeignKey('stores.Country', related_name='counties', null=True, blank=True)

    @property
    def stores(self):
        return Store.objects.filter(address__county__pk=self.pk)

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ['name']
        app_label = 'stores'

class Country(models.Model):
    """
    ISO Countries
    """
    iso_code = models.CharField('ISO Code', max_length=3)
    name = models.CharField('Name', max_length=100)

    @property
    def stores(self):
        return Store.objects.filter(address__country__pk=self.pk)

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ['name']
        app_label = 'stores'

class Address(models.Model):
    """
    Represents a location or postal address
    """
    name = models.CharField('Address Name', max_length=200)
    address1 = models.CharField('Address Line 1', max_length=200, help_text="First line of the address. e.g. 1 Station Road")
    address2 = models.CharField('Address Line 2', max_length=200, null=True, blank=True, help_text="(Optional) Second line of the address")
    address3 = models.CharField('Address Line 3', max_length=200, null=True, blank=True, help_text="(Optional) Third line of the address")
    city = models.CharField('Town/City', max_length='50', help_text="City or town")
    county = models.ForeignKey('stores.County', related_name='addresses', null=True, blank=True, help_text="County or suburban area. (Ignore if the store is outside the UK)")
    country = models.ForeignKey('stores.Country', related_name='addresses')
    postcode = models.CharField('Postcode / Zipcode', max_length=20, help_text="Post Code or Zip Code, e.g. M1 1AA or 60647")

    geo_latitude = models.FloatField('Latitude', null=True, blank=True)
    geo_longitude = models.FloatField('Longitude', null=True, blank=True)

    @property
    def full_address(self):
        return u', '.join([
            self.address1,
            self.address2,
            self.address3,
            self.city,
            unicode(self.county),
            self.postcode,
            unicode(self.country),
            ])

    @property
    def address_string(self):
        return u', '.join([
            self.address1,
            self.postcode,
            unicode(self.country),
        ])

    @property
    def geo_location(self):
        return Point(self.geo_longitude, self.geo_latitude)

    def __unicode__(self):
        if self.name:
            return self.name
        return self.address_string

    def save(self, **kwargs):
        no_lookup = kwargs.pop('no_lookup', None)
        if not no_lookup and not self.geo_latitude and not self.geo_longitude:
            res = caching_geo_lookup(self.address_string)
            if res:
                self.geo_latitude, self.geo_longitude = res[1]
        return super(Address, self).save(**kwargs)

    class Meta:
        app_label = 'stores'

class ClaimRequest(models.Model):

    CLAIM_STATUS_PENDING = 0
    CLAIM_STATUS_APPROVED = 1
    CLAIM_STATUS_REJECTED = 2

    CLAIM_STATUS_CHOICES = (
        (CLAIM_STATUS_PENDING, 'Pending'),
        (CLAIM_STATUS_APPROVED, 'Approved'),
        (CLAIM_STATUS_REJECTED, 'Rejected')
    )

    object_id = models.PositiveIntegerField()
    object_type = models.ForeignKey(ContentType)
    generic_obj = generic.GenericForeignKey('object_type', 'object_id')

    user = models.ForeignKey(USER_MODEL, related_name='claims')
    note = models.TextField('Claim Note')
    status = models.IntegerField('Status', choices=CLAIM_STATUS_CHOICES, default=CLAIM_STATUS_PENDING)

    class Meta:
        app_label = 'stores'<commit_msg>Fix full address to only provide the fields that are defined.<commit_after>import re
from django.db import models
from django.core.urlresolvers import reverse_lazy
from django.contrib.auth import get_user_model
from django.contrib.gis.geos import Point
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from stores.utils import caching_geo_lookup

USER_MODEL = get_user_model()

class Chain(models.Model):
    name = models.CharField('Name', max_length=200)
    slug = models.SlugField('URL Slug', max_length=200, blank=True)
    head_office = models.ForeignKey('stores.Address', blank=True, null=True)
    website = models.URLField('Website', blank=True, null=True)
    editor = models.ForeignKey(USER_MODEL, related_name='editable_chains', blank=True, null=True)
    active = models.BooleanField('Active?', default=True)

    long_description = models.TextField('Description', null=True, blank=True)
    links = generic.GenericRelation('stores.Link', content_type_field='object_type')

    def save(self, **kwargs):
        if self.slug == '':
            self.slug = re.sub(r'\W+', '-', str(self.name).lower())
        return super(Chain, self).save(**kwargs)

    def get_absolute_url(self):
        return reverse_lazy('chain-detail', args=[self.slug])

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ['name']
        app_label = 'stores'

class Store(models.Model):

    STORE_TYPE_RETAIL = 1
    STORE_TYPE_ONLINE = 2
    STORE_TYPE_BOTH = 3

    STORE_TYPE_CHOICES = (
        (STORE_TYPE_RETAIL, 'Retail Only'),
        (STORE_TYPE_ONLINE, 'Online Only'),
        (STORE_TYPE_BOTH, 'Retail & Online'),
    )

    name = models.CharField('Name', max_length=200, help_text="Store's full name")
    slug = models.SlugField('URL Slug', max_length=200, blank=True)
    address = models.ForeignKey('stores.Address', related_name='stores')
    store_type = models.IntegerField('Store Type', choices=STORE_TYPE_CHOICES, default=STORE_TYPE_RETAIL)
    chain = models.ForeignKey(Chain, related_name='stores', null=True, blank=True)
    editor = models.ForeignKey(USER_MODEL, related_name='editable_stores', null=True, blank=True)
    active = models.BooleanField('Active?', default=True)

    website = website = models.URLField('Website', null=True, blank=True)
    email = models.EmailField('Email', null=True, blank=True, help_text="Contact email address for the store.")
    phone = models.CharField('Phone', max_length=25, null=True, blank=True, help_text="Contact phone number for the store.")
    links = generic.GenericRelation('stores.Link', content_type_field='object_type')

    long_description = models.TextField('Description', null=True, blank=True, help_text="Full description of the store, including any marketing material. Markdown supported.")
    brands = models.ManyToManyField('stores.Brand', null=True, blank=True, help_text="Brands that are sold by this store.")

    def get_full_address(self):
        if self.address:
            return self.address.full_address

    def get_location(self):
        if self.address:
            return self.address.geo_location

    def save(self, **kwargs):
        if not self.slug or self.slug == '':
            self.slug = re.sub(r'\W+', '-', str(self.name).lower())
        return super(Store, self).save(**kwargs)

    def get_absolute_url(self):
        return reverse_lazy('store-detail', args=[self.slug])

    def __unicode__(self):
        if not self.name and self.chain:
            return self.chain.name
        return self.name

    class Meta:
        ordering = ['name']
        app_label = 'stores'

class Brand(models.Model):
    name = models.CharField('Brand Name', max_length=200)
    website = models.URLField('Brand Website', null=True, blank=True)

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ['name']
        app_label = 'stores'

class County(models.Model):
    """
    UK Counties
    """
    name = models.CharField('Name', max_length=100)
    country = models.ForeignKey('stores.Country', related_name='counties', null=True, blank=True)

    @property
    def stores(self):
        return Store.objects.filter(address__county__pk=self.pk)

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ['name']
        app_label = 'stores'

class Country(models.Model):
    """
    ISO Countries
    """
    iso_code = models.CharField('ISO Code', max_length=3)
    name = models.CharField('Name', max_length=100)

    @property
    def stores(self):
        return Store.objects.filter(address__country__pk=self.pk)

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ['name']
        app_label = 'stores'

class Address(models.Model):
    """
    Represents a location or postal address
    """
    name = models.CharField('Address Name', max_length=200)
    address1 = models.CharField('Address Line 1', max_length=200, help_text="First line of the address. e.g. 1 Station Road")
    address2 = models.CharField('Address Line 2', max_length=200, null=True, blank=True, help_text="(Optional) Second line of the address")
    address3 = models.CharField('Address Line 3', max_length=200, null=True, blank=True, help_text="(Optional) Third line of the address")
    city = models.CharField('Town/City', max_length='50', help_text="City or town")
    county = models.ForeignKey('stores.County', related_name='addresses', null=True, blank=True, help_text="County or suburban area. (Ignore if the store is outside the UK)")
    country = models.ForeignKey('stores.Country', related_name='addresses')
    postcode = models.CharField('Postcode / Zipcode', max_length=20, help_text="Post Code or Zip Code, e.g. M1 1AA or 60647")

    geo_latitude = models.FloatField('Latitude', null=True, blank=True)
    geo_longitude = models.FloatField('Longitude', null=True, blank=True)

    @property
    def full_address(self):
        fields = [
            self.address1,
            self.address2,
            self.address3,
            self.city,
            self.county.name,
            self.postcode,
            self.country.name,
            ]
        return ', '.join([f for f in fields if f])

    @property
    def address_string(self):
        return u', '.join([
            self.address1,
            self.postcode,
            unicode(self.country),
        ])

    @property
    def geo_location(self):
        return Point(self.geo_longitude, self.geo_latitude)

    def __unicode__(self):
        if self.name:
            return self.name
        return self.address_string

    def save(self, **kwargs):
        no_lookup = kwargs.pop('no_lookup', None)
        if not no_lookup and not self.geo_latitude and not self.geo_longitude:
            res = caching_geo_lookup(self.address_string)
            if res:
                self.geo_latitude, self.geo_longitude = res[1]
        return super(Address, self).save(**kwargs)

    class Meta:
        app_label = 'stores'

class ClaimRequest(models.Model):

    CLAIM_STATUS_PENDING = 0
    CLAIM_STATUS_APPROVED = 1
    CLAIM_STATUS_REJECTED = 2

    CLAIM_STATUS_CHOICES = (
        (CLAIM_STATUS_PENDING, 'Pending'),
        (CLAIM_STATUS_APPROVED, 'Approved'),
        (CLAIM_STATUS_REJECTED, 'Rejected')
    )

    object_id = models.PositiveIntegerField()
    object_type = models.ForeignKey(ContentType)
    generic_obj = generic.GenericForeignKey('object_type', 'object_id')

    user = models.ForeignKey(USER_MODEL, related_name='claims')
    note = models.TextField('Claim Note')
    status = models.IntegerField('Status', choices=CLAIM_STATUS_CHOICES, default=CLAIM_STATUS_PENDING)

    class Meta:
        app_label = 'stores'<|endoftext|>


<commit_before>from django.db import models
from django_extensions.db.fields import UUIDField
from django.core.urlresolvers import reverse

class SourceName(models.Model):
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name

class Source(models.Model):
    # The source type that we got this data from (wikipedia, musicbrainz, etc)
    source_name = models.ForeignKey(SourceName)
    # The title of the page on the source website
    title = models.CharField(max_length=255)
    # The URL of the source
    uri = models.CharField(max_length=255)
    last_updated = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return "From %s: %s (%s)" % (self.source_name, self.uri, self.last_updated)

class Description(models.Model):
    """ A short description of a thing in the database.
    It could be a biography, or a description """
    source = models.ForeignKey(Source, blank=True, null=True)
    description = models.TextField()

    def __unicode__(self):
        return "%s - %s" % (self.source, self.description[:100])

class Image(models.Model):
    """ An image of a thing in the database """
    source = models.ForeignKey(Source, blank=True, null=True)
    image = models.ImageField(upload_to="images")

class BaseModel(models.Model):
    class Meta:
        abstract = True

    source = models.ForeignKey(Source, blank=True, null=True, related_name="%(class)s_source_set")
    references = models.ManyToManyField(Source, blank=True, null=True, related_name="%(class)s_reference_set")
    description = models.ForeignKey(Description, blank=True, null=True)
    images = models.ManyToManyField(Image, related_name="%(class)s_image_set")

    def ref(self):
        u = {"url": self.source.uri, "title": self.source.source_name.name}
        return u
        return [u]

    def get_style(self):
        raise Exception("need style")

    def get_object_map(self, key):
        raise Exception("need map")

class Artist(BaseModel):
    class Meta:
        abstract = True

    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female')
    )
    TYPE_CHOICES = (
        ('P', 'Person'),
        ('G', 'Group')
    )
    name = models.CharField(max_length=200)
    mbid = UUIDField(blank=True, null=True)
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True, null=True)
    begin = models.CharField(max_length=10, blank=True, null=True)
    end = models.CharField(max_length=10, blank=True, null=True)
    artist_type = models.CharField(max_length=1, choices=TYPE_CHOICES, default='P')
    main_instrument = models.ForeignKey('Instrument', blank=True, null=True)

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        viewname = "%s-artist" % (self.get_style(), )
        return reverse(viewname, args=[str(self.id)])

    def recordings(self):
        perfs = self.performances()
        IPClass = self.get_object_map("performance")
        performances = IPClass.objects.filter(performer=self)
        recs = [p.recording for p in performances]
        return recs

    def performances(self):
        ConcertClass = self.get_object_map("concert")
        IPClass = self.get_object_map("performance")
        concerts = ConcertClass.objects.filter(tracks__instrumentperformance__performer=self).distinct()
        ret = []
        for c in concerts:
            performances = IPClass.objects.filter(performer=self, recording__concert=c).distinct()
            # Unique the instrument list
            instruments = []
            theperf = []
            for p in performances:
                if p.instrument not in instruments:
                    theperf.append(p)
                    instruments.append(p.instrument)
            ret.append((c, theperf))
        return ret

class Label(BaseModel):
    name = models.CharField(max_length=100)

class Concert(BaseModel):
    class Meta:
        abstract = True
    mbid = UUIDField(blank=True, null=True)
    location = models.ForeignKey('Location', blank=True, null=True)
    title = models.CharField(max_length=100)
    artists = models.ManyToManyField('Artist')
    tracks = models.ManyToManyField('Recording')
    year = models.IntegerField(blank=True, null=True)
    label = models.ForeignKey('Label', blank=True, null=True)

    def length(self):
        tot_len = 0
        print "tracks"
        print self.tracks.all()
        for t in self.tracks.all():
            tot_len += t.length
            print t.length
        import time
        return time.strftime('%H:%M:%S', time.gmtime(tot_len))

    def __unicode__(self):
        ret = ", ".join([unicode(a) for a in self.artists.all()])
        if self.location:
            ret += " at " + unicode(self.location)
        return "%s (%s)" % (self.title, ret)

    def get_absolute_url(self):
        viewname = "%s-concert" % (self.get_style(), )
        return reverse(viewname, args=[str(self.id)])

    def artistnames(self):
        return self.artists.all()
        if len(artists) > 1:
            return artists
        elif len(artists) == 0:
            return None
        else:
            return artists[0]

    def performers(self):
        IPClass = self.get_object_map("performance")
        perf = IPClass.objects.filter(recording__in=self.tracks.all()).distinct().all()
        person = []
        ret = []
        # XXX: Do this as a group by query
        for p in perf:
            if p.performer.id not in person:
                person.append(p.performer.id)
                ret.append(p)
        return ret

class Work(BaseModel):
    class Meta:
        abstract = True
    title = models.CharField(max_length=100)
    mbid = UUIDField(blank=True, null=True)
    composer = models.ForeignKey('Composer', blank=True, null=True)

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        viewname = "%s-work" % (self.get_style(), )
        return reverse(viewname, args=[str(self.id)])

    def concerts(self):
        ConcertClass = self.get_object_map("concert")
        return ConcertClass.objects.filter(tracks__work=self).all()

class WorkAttribute(models.Model):
    class Meta:
        abstract = True
    work = models.ForeignKey('Work')
    attribute_type = models.ForeignKey('WorkAttributeType')
    attribute_value_free = models.CharField(max_length=100, blank=True, null=True)
    attribute_value = models.ForeignKey('WorkAttributeTypeValue', blank=True, null=True)

class WorkAttributeType(models.Model):
    class Meta:
        abstract = True
    type_name = models.CharField(max_length=100)

class WorkAttributeTypeValue(models.Model):
    class Meta:
        abstract = True
    attribute_type = models.ForeignKey('WorkAttributeType')
    value = models.CharField(max_length=100)

class Recording(BaseModel):
    class Meta:
        abstract = True
    title = models.CharField(max_length=100)
    work = models.ForeignKey('Work', blank=True, null=True)
    mbid = UUIDField(blank=True, null=True)
    length = models.IntegerField(blank=True, null=True)
    performance = models.ManyToManyField('Artist', through="InstrumentPerformance")

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        viewname = "%s-recording" % (self.get_style(), )
        return reverse(viewname, args=[str(self.id)])

    def all_artists(self):
        ArtistClass = self.get_object_map("artist")
        return ArtistClass.objects.filter(concert__tracks=self)

class InstrumentAlias(models.Model):
    class Meta:
        abstract = True
    name = models.CharField(max_length=50)
    instrument = models.ForeignKey("Instrument", related_name="aliases")

    def __unicode__(self):
        return self.name

class Instrument(BaseModel):
    class Meta:
        abstract = True
    name = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        viewname = "%s-instrument" % (self.get_style(), )
        return reverse(viewname, args=[str(self.id)])

    def artists(self):
        ArtistClass = self.get_object_map("artist")
        return ArtistClass.objects.filter(instrumentperformance__instrument=self).distinct().all()

class InstrumentPerformance(models.Model):
    class Meta:
        abstract = True
    recording = models.ForeignKey('Recording')
    performer = models.ForeignKey('Artist')
    instrument = models.ForeignKey('Instrument')
    lead = models.BooleanField(default=False)

class Composer(BaseModel):
    class Meta:
        abstract = True
    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female')
    )
    name = models.CharField(max_length=200)
    mbid = UUIDField(blank=True, null=True)
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True, null=True)
    begin = models.CharField(max_length=10, blank=True, null=True)
    end = models.CharField(max_length=10, blank=True, null=True)

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        viewname = "%s-composer" % (self.get_style(), )
        return reverse(viewname, args=[str(self.id)])

class Location(BaseModel):
    class Meta:
        abstract = True
    name = models.CharField(max_length=200)
    city = models.CharField(max_length=100, blank=True, null=True)
    region = models.CharField(max_length=100, blank=True, null=True)
    country = models.CharField(max_length=100, blank=True, null=True)
    lat = models.CharField(max_length=10, blank=True, null=True)
    lng = models.CharField(max_length=10, blank=True, null=True)

    def __unicode__(self):
        return self.name

<commit_msg>only return one artist name for albums<commit_after>from django.db import models
from django_extensions.db.fields import UUIDField
from django.core.urlresolvers import reverse

class SourceName(models.Model):
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name

class Source(models.Model):
    # The source type that we got this data from (wikipedia, musicbrainz, etc)
    source_name = models.ForeignKey(SourceName)
    # The title of the page on the source website
    title = models.CharField(max_length=255)
    # The URL of the source
    uri = models.CharField(max_length=255)
    last_updated = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return "From %s: %s (%s)" % (self.source_name, self.uri, self.last_updated)

class Description(models.Model):
    """ A short description of a thing in the database.
    It could be a biography, or a description """
    source = models.ForeignKey(Source, blank=True, null=True)
    description = models.TextField()

    def __unicode__(self):
        return "%s - %s" % (self.source, self.description[:100])

class Image(models.Model):
    """ An image of a thing in the database """
    source = models.ForeignKey(Source, blank=True, null=True)
    image = models.ImageField(upload_to="images")

class BaseModel(models.Model):
    class Meta:
        abstract = True

    source = models.ForeignKey(Source, blank=True, null=True, related_name="%(class)s_source_set")
    references = models.ManyToManyField(Source, blank=True, null=True, related_name="%(class)s_reference_set")
    description = models.ForeignKey(Description, blank=True, null=True)
    images = models.ManyToManyField(Image, related_name="%(class)s_image_set")

    def ref(self):
        u = {"url": self.source.uri, "title": self.source.source_name.name}
        return u
        return [u]

    def get_style(self):
        raise Exception("need style")

    def get_object_map(self, key):
        raise Exception("need map")

class Artist(BaseModel):
    class Meta:
        abstract = True

    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female')
    )
    TYPE_CHOICES = (
        ('P', 'Person'),
        ('G', 'Group')
    )
    name = models.CharField(max_length=200)
    mbid = UUIDField(blank=True, null=True)
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True, null=True)
    begin = models.CharField(max_length=10, blank=True, null=True)
    end = models.CharField(max_length=10, blank=True, null=True)
    artist_type = models.CharField(max_length=1, choices=TYPE_CHOICES, default='P')
    main_instrument = models.ForeignKey('Instrument', blank=True, null=True)

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        viewname = "%s-artist" % (self.get_style(), )
        return reverse(viewname, args=[str(self.id)])

    def recordings(self):
        perfs = self.performances()
        IPClass = self.get_object_map("performance")
        performances = IPClass.objects.filter(performer=self)
        recs = [p.recording for p in performances]
        return recs

    def performances(self):
        ConcertClass = self.get_object_map("concert")
        IPClass = self.get_object_map("performance")
        concerts = ConcertClass.objects.filter(tracks__instrumentperformance__performer=self).distinct()
        ret = []
        for c in concerts:
            performances = IPClass.objects.filter(performer=self, recording__concert=c).distinct()
            # Unique the instrument list
            instruments = []
            theperf = []
            for p in performances:
                if p.instrument not in instruments:
                    theperf.append(p)
                    instruments.append(p.instrument)
            ret.append((c, theperf))
        return ret

class Label(BaseModel):
    name = models.CharField(max_length=100)

class Concert(BaseModel):
    class Meta:
        abstract = True
    mbid = UUIDField(blank=True, null=True)
    location = models.ForeignKey('Location', blank=True, null=True)
    title = models.CharField(max_length=100)
    artists = models.ManyToManyField('Artist')
    tracks = models.ManyToManyField('Recording')
    year = models.IntegerField(blank=True, null=True)
    label = models.ForeignKey('Label', blank=True, null=True)

    def length(self):
        tot_len = 0
        print "tracks"
        print self.tracks.all()
        for t in self.tracks.all():
            tot_len += t.length
            print t.length
        import time
        return time.strftime('%H:%M:%S', time.gmtime(tot_len))

    def __unicode__(self):
        ret = ", ".join([unicode(a) for a in self.artists.all()])
        if self.location:
            ret += " at " + unicode(self.location)
        return "%s (%s)" % (self.title, ret)

    def get_absolute_url(self):
        viewname = "%s-concert" % (self.get_style(), )
        return reverse(viewname, args=[str(self.id)])

    def artistnames(self):
        artists = self.artists.all()
        if len(artists) > 1:
            return artists
        elif len(artists) == 0:
            return None
        else:
            return artists[0]

    def performers(self):
        IPClass = self.get_object_map("performance")
        perf = IPClass.objects.filter(recording__in=self.tracks.all()).distinct().all()
        person = []
        ret = []
        # XXX: Do this as a group by query
        for p in perf:
            if p.performer.id not in person:
                person.append(p.performer.id)
                ret.append(p)
        return ret

class Work(BaseModel):
    class Meta:
        abstract = True
    title = models.CharField(max_length=100)
    mbid = UUIDField(blank=True, null=True)
    composer = models.ForeignKey('Composer', blank=True, null=True)

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        viewname = "%s-work" % (self.get_style(), )
        return reverse(viewname, args=[str(self.id)])

    def concerts(self):
        ConcertClass = self.get_object_map("concert")
        return ConcertClass.objects.filter(tracks__work=self).all()

class WorkAttribute(models.Model):
    class Meta:
        abstract = True
    work = models.ForeignKey('Work')
    attribute_type = models.ForeignKey('WorkAttributeType')
    attribute_value_free = models.CharField(max_length=100, blank=True, null=True)
    attribute_value = models.ForeignKey('WorkAttributeTypeValue', blank=True, null=True)

class WorkAttributeType(models.Model):
    class Meta:
        abstract = True
    type_name = models.CharField(max_length=100)

class WorkAttributeTypeValue(models.Model):
    class Meta:
        abstract = True
    attribute_type = models.ForeignKey('WorkAttributeType')
    value = models.CharField(max_length=100)

class Recording(BaseModel):
    class Meta:
        abstract = True
    title = models.CharField(max_length=100)
    work = models.ForeignKey('Work', blank=True, null=True)
    mbid = UUIDField(blank=True, null=True)
    length = models.IntegerField(blank=True, null=True)
    performance = models.ManyToManyField('Artist', through="InstrumentPerformance")

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        viewname = "%s-recording" % (self.get_style(), )
        return reverse(viewname, args=[str(self.id)])

    def all_artists(self):
        ArtistClass = self.get_object_map("artist")
        return ArtistClass.objects.filter(concert__tracks=self)

class InstrumentAlias(models.Model):
    class Meta:
        abstract = True
    name = models.CharField(max_length=50)
    instrument = models.ForeignKey("Instrument", related_name="aliases")

    def __unicode__(self):
        return self.name

class Instrument(BaseModel):
    class Meta:
        abstract = True
    name = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        viewname = "%s-instrument" % (self.get_style(), )
        return reverse(viewname, args=[str(self.id)])

    def artists(self):
        ArtistClass = self.get_object_map("artist")
        return ArtistClass.objects.filter(instrumentperformance__instrument=self).distinct().all()

class InstrumentPerformance(models.Model):
    class Meta:
        abstract = True
    recording = models.ForeignKey('Recording')
    performer = models.ForeignKey('Artist')
    instrument = models.ForeignKey('Instrument')
    lead = models.BooleanField(default=False)

class Composer(BaseModel):
    class Meta:
        abstract = True
    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female')
    )
    name = models.CharField(max_length=200)
    mbid = UUIDField(blank=True, null=True)
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True, null=True)
    begin = models.CharField(max_length=10, blank=True, null=True)
    end = models.CharField(max_length=10, blank=True, null=True)

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        viewname = "%s-composer" % (self.get_style(), )
        return reverse(viewname, args=[str(self.id)])

class Location(BaseModel):
    class Meta:
        abstract = True
    name = models.CharField(max_length=200)
    city = models.CharField(max_length=100, blank=True, null=True)
    region = models.CharField(max_length=100, blank=True, null=True)
    country = models.CharField(max_length=100, blank=True, null=True)
    lat = models.CharField(max_length=10, blank=True, null=True)
    lng = models.CharField(max_length=10, blank=True, null=True)

    def __unicode__(self):
        return self.name

<|endoftext|>


<commit_before>import ncs
import socket
import time

def search_and_destroy(box):
    """
    A function that deletes the object groups from the device's object group
    list that are not found in any of the inputted device's access lists
    """
    #Initializing python lists
    og_list = []
    og_typ = []
    acl_list = []
    ret = {}
    #empty = True
    used_group_ogs = set()
    orphaned_ogs = set()
    orphaned_dict = {}
    delete_first = set()

    #Creating transaction and setting root to access NSO
    with ncs.maapi.single_write_trans('ncsadmin', 'python', groups=['ncsadmin']) as t:
        root = ncs.maagic.get_root(t)
        #Adding all of the object groups and their types to python lists
        for ogtyp in root.devices.device[box].config.asa__object_group:
            for og in root.devices.device[box].config.asa__object_group[ogtyp]:
                og_list.append(og.id)
                og_typ.append(str(og))      #str(og) is the object group type

        #Adding each access list's rules to a python list (temp_rul_list) and
        #then adding those lists as elements of another python list (acl_list)
        for acl in root.devices.device[box].config.asa__access_list.access_list_id:
            temp_rul_list = []
            for rul in root.devices.device[box].config.asa__access_list.access_list_id[acl.id].rule:
                if "object-group" in rul.id:
                    temp_rul_list.append(rul.id)
            acl_list.append(temp_rul_list)

        #Iterating through both object group and object group type lists simultaneously
        for og, typ in zip(og_list, og_typ):
            flag = 0
            for acl in acl_list:
                #flag indicates whether og was found in an access list
                for rule in acl:
                    if og in rule:
                        used_group_ogs = rec_group_og(used_group_ogs, root, box, og, typ)
                        flag = 1
                        break
                #If found, continue to the next object group
                if flag:
                    break
            #If not found in any of the access lists, delete from object group list
            #and add to dictionary
            if not flag:
                orphaned_ogs.add(og)
                inner_dict = {"og_type" : typ, "group_ogs" : [], "deleted" : 0}
                if root.devices.device[box].config.asa__object_group[typ][og].group_object:
                    delete_first.add(og)
                    for group_og in root.devices.device[box].config.asa__object_group[typ][og].group_object:
                        inner_dict["group_ogs"].append(group_og.id)
                orphaned_dict[og] = inner_dict

        #print "Used_Group_OGs: ", used_group_ogs
        #print '\n'
        #print "Orphaned_OGs: ", orphaned_ogs

        orphaned_ogs = orphaned_ogs.difference(used_group_ogs)
        delete_first = delete_first.difference(used_group_ogs)
        delete_second = orphaned_ogs.difference(delete_first)

        ret = del_1(root, box, delete_first, orphaned_dict, ret)
        ret = del_2(root, box, delete_second, orphaned_dict, ret)

        try:
            t.apply()
            stat = "Success"
        #Provides error message if there is a problem removing an OG
        except Exception, err:
            stat = Exception, err

        return ret, stat

def rec_group_og(used_group_ogs, root, box, og, typ):
    if root.devices.device[box].config.asa__object_group[typ][og].group_object:
        for group_og in root.devices.device[box].config.asa__object_group[typ][og].group_object:
            used_group_ogs.add(group_og.id)
            used_group_ogs = rec_group_og(used_group_ogs, root, box, group_og.id, typ)
    return used_group_ogs

def del_1(root, box, delete_first, orphaned_dict, ret):
    del_f_count = len(delete_first)
    while del_f_count:
        for og in delete_first:
            if orphaned_dict[og]["deleted"] == 1:
                continue
            flag = 0
            for og2 in delete_first:
                if orphaned_dict[og]["deleted"] == 1:
                    continue
                if og in orphaned_dict[og2]["group_ogs"]:
                    flag = 1
                    break
            if not flag:
                if orphaned_dict[og]["og_type"] in ret.keys():
                    ret[orphaned_dict[og]["og_type"]].append(og)
                else:
                    ret[orphaned_dict[og]["og_type"]] = [og]

                del_f_count -= 1
                orphaned_dict[og]["deleted"] = 1
                #print og + ": "
                #print orphaned_dict[og]["group_ogs"]
                del root.devices.device[box].config.asa__object_group[orphaned_dict[og]["og_type"]][og]

    return ret

def del_2(root, box, delete_second, orphaned_dict, ret):
    for og in delete_second:
        if orphaned_dict[og]["og_type"] in ret.keys():
            ret[orphaned_dict[og]["og_type"]].append(og)
        else:
            ret[orphaned_dict[og]["og_type"]] = [og]
        #print og + ':'
        del root.devices.device[box].config.asa__object_group[orphaned_dict[og]["og_type"]][og]

    return ret

def flag_ogs_in_box_test(box):
    """
    A function that returns a dictionary of the object groups that are not found
    in any of the inputted device's access lists, organized by object group type.
    """

    #Initializing python lists and flags
    og_list = []
    og_typ = []
    acl_list = []
    ret = {}
    empty = True

    #Creating transaction and setting root to access NSO
    with ncs.maapi.single_read_trans('ncsadmin', 'python', groups=['ncsadmin']) as t:
        root = ncs.maagic.get_root(t)
        #Adding all of the object groups and their types to python lists
        for ogtyp in root.devices.device[box].config.asa__object_group:
            for og in root.devices.device[box].config.asa__object_group[ogtyp]:
                og_list.append(og.id)
                og_typ.append(str(og))      #str(og) is the object group type

        #Adding each access list's rules to a python list (temp_rul_list) and
        #then adding those lists as elements of another python list (acl_list)
        for acl in root.devices.device[box].config.asa__access_list.access_list_id:
            temp_rul_list = []
            for rul in root.devices.device[box].config.asa__access_list.access_list_id[acl.id].rule:
                if "object-group" in rul.id:
                    temp_rul_list.append(rul.id)
            acl_list.append(temp_rul_list)

    #Iterating through both object group and object group type lists simultaneously
    for og, typ in zip(og_list, og_typ):
        #flag indicates whether og was found in an access list
        flag = 0
        for acl in acl_list:
            #Iterates through the rules of an acl list
            for rule in acl:
                if og in rule:
                    flag = 1
                    break

            #If found, continue to the next object group
            if flag:
                break
        #If not found in any of the access lists, add to the dictionary
        if not flag:
            if empty:
                empty = False
            #If key has been created already, add og to key
            if typ in ret.keys():
                ret[typ].append(og)
            #Else, create key and append og
            else:
                ret[typ] = [og]

    if empty:
        stat = "No Orphaned Object Groups"
    else:
        stat = "Success"

    return ret

def remove_ogs(box, og_id, og_type):
    """
    A function that removes the object group from the object group list using
    the arguments passed: device name, object group name, and object group type.
    """
    with ncs.maapi.single_write_trans('ncsadmin', 'python', groups=['ncsadmin']) as t:
        del root.devices.device[box].config.asa__object_group[og_type][og_id]
        try:
            t.apply()
            stat = "Success"
        except:
            stat = "Error Removing"<commit_msg>hello<commit_after>import ncs
import socket
import time

def search_and_destroy(box):
    """
    A function that deletes the object groups from the device's object group
    list that are not found in any of the inputted device's access lists
    """
    #Initializing python lists
    og_list = []
    og_typ = []
    acl_list = []
    ret = {}
    #empty = True
    used_group_ogs = set()
    orphaned_ogs = set()
    orphaned_dict = {}
    delete_first = set()

    #Creating transaction and setting root to access NSO
    with ncs.maapi.single_write_trans('ncsadmin', 'python', groups=['ncsadmin']) as t:
        root = ncs.maagic.get_root(t)
        #Adding all of the object groups and their types to python lists
        for ogtyp in root.devices.device[box].config.asa__object_group:
            for og in root.devices.device[box].config.asa__object_group[ogtyp]:
                og_list.append(og.id)
                og_typ.append(str(og))      #str(og) is the object group type

        #Adding each access list's rules to a python list (temp_rul_list) and
        #then adding those lists as elements of another python list (acl_list)
        for acl in root.devices.device[box].config.asa__access_list.access_list_id:
            temp_rul_list = []
            for rul in root.devices.device[box].config.asa__access_list.access_list_id[acl.id].rule:
                if "object-group" in rul.id:
                    temp_rul_list.append(rul.id)
            acl_list.append(temp_rul_list)

        #Iterating through both object group and object group type lists simultaneously
        for og, typ in zip(og_list, og_typ):
            flag = 0
            for acl in acl_list:
                #flag indicates whether og was found in an access list
                for rule in acl:
                    if og in rule:
                        used_group_ogs = rec_group_og(used_group_ogs, root, box, og, typ)
                        flag = 1
                        break
                #If found, continue to the next object group
                if flag:
                    break
            #If not found in any of the access lists, delete from object group list
            #and add to dictionary
            if not flag:
                orphaned_ogs.add(og)
                inner_dict = {"og_type" : typ, "group_ogs" : [], "deleted" : 0}
                if root.devices.device[box].config.asa__object_group[typ][og].group_object:
                    delete_first.add(og)
                    for group_og in root.devices.device[box].config.asa__object_group[typ][og].group_object:
                        inner_dict["group_ogs"].append(group_og.id)
                orphaned_dict[og] = inner_dict

        #print "Used_Group_OGs: ", used_group_ogs
        #print '\n'
        #print "Orphaned_OGs: ", orphaned_ogs

        orphaned_ogs = orphaned_ogs.difference(used_group_ogs)
        delete_first = delete_first.difference(used_group_ogs)
        delete_second = orphaned_ogs.difference(delete_first)

        ret = del_1(root, box, delete_first, orphaned_dict, ret)
        ret = del_2(root, box, delete_second, orphaned_dict, ret)

        try:
            t.apply()
            stat = "Success"
        #Provides error message if there is a problem removing an OG
        except Exception, err:
            stat = Exception, err

        return ret, stat

def rec_group_og(used_group_ogs, root, box, og, typ):
    if root.devices.device[box].config.asa__object_group[typ][og].group_object:
        for group_og in root.devices.device[box].config.asa__object_group[typ][og].group_object:
            used_group_ogs.add(group_og.id)
            used_group_ogs = rec_group_og(used_group_ogs, root, box, group_og.id, typ)
    return used_group_ogs

def del_1(root, box, delete_first, orphaned_dict, ret):
    del_f_count = len(delete_first)
    while del_f_count:
        for og in delete_first:
            if orphaned_dict[og]["deleted"] == 1:
                continue
            flag = 0
            for og2 in delete_first:
                if orphaned_dict[og]["deleted"] == 1:
                    continue
                if og in orphaned_dict[og2]["group_ogs"]:
                    flag = 1
                    break
            if not flag:
                if orphaned_dict[og]["og_type"] in ret.keys():
                    ret[orphaned_dict[og]["og_type"]].append(og)
                else:
                    ret[orphaned_dict[og]["og_type"]] = [og]

                del_f_count -= 1
                orphaned_dict[og]["deleted"] = 1
                #print og + ": "
                #print orphaned_dict[og]["group_ogs"]
                del root.devices.device[box].config.asa__object_group[orphaned_dict[og]["og_type"]][og]

    return ret

def del_2(root, box, delete_second, orphaned_dict, ret):
    for og in delete_second:
        if orphaned_dict[og]["og_type"] in ret.keys():
            ret[orphaned_dict[og]["og_type"]].append(og)
        else:
            ret[orphaned_dict[og]["og_type"]] = [og]
        #print og + ':'
        del root.devices.device[box].config.asa__object_group[orphaned_dict[og]["og_type"]][og]

    return ret

def flag_ogs_in_box_test(box):
    """
    A function that returns a dictionary of the object groups that are not found
    in any of the inputted device's access lists, organized by object group type.
    """

    #Initializing python lists
    og_list = []
    og_typ = []
    acl_list = []
    ret = {}
    #empty = True
    used_group_ogs = set()
    orphaned_ogs = set()
    orphaned_dict = {}
    delete_first = set()

    #Creating transaction and setting root to access NSO
    with ncs.maapi.single_write_trans('ncsadmin', 'python', groups=['ncsadmin']) as t:
        root = ncs.maagic.get_root(t)
        #Adding all of the object groups and their types to python lists
        for ogtyp in root.devices.device[box].config.asa__object_group:
            for og in root.devices.device[box].config.asa__object_group[ogtyp]:
                og_list.append(og.id)
                og_typ.append(str(og))      #str(og) is the object group type

        #Adding each access list's rules to a python list (temp_rul_list) and
        #then adding those lists as elements of another python list (acl_list)
        for acl in root.devices.device[box].config.asa__access_list.access_list_id:
            temp_rul_list = []
            for rul in root.devices.device[box].config.asa__access_list.access_list_id[acl.id].rule:
                if "object-group" in rul.id:
                    temp_rul_list.append(rul.id)
            acl_list.append(temp_rul_list)

        #Iterating through both object group and object group type lists simultaneously
        for og, typ in zip(og_list, og_typ):
            flag = 0
            for acl in acl_list:
                #flag indicates whether og was found in an access list
                for rule in acl:
                    if og in rule:
                        used_group_ogs = rec_group_og(used_group_ogs, root, box, og, typ)
                        flag = 1
                        break
                #If found, continue to the next object group
                if flag:
                    break
            #If not found in any of the access lists, delete from object group list
            #and add to dictionary
            if not flag:
                orphaned_ogs.add(og)
                inner_dict = {"og_type" : typ, "group_ogs" : [], "deleted" : 0}
                if root.devices.device[box].config.asa__object_group[typ][og].group_object:
                    delete_first.add(og)
                    for group_og in root.devices.device[box].config.asa__object_group[typ][og].group_object:
                        inner_dict["group_ogs"].append(group_og.id)
                orphaned_dict[og] = inner_dict

        #print "Used_Group_OGs: ", used_group_ogs
        #print '\n'
        #print "Orphaned_OGs: ", orphaned_ogs

        orphaned_ogs = orphaned_ogs.difference(used_group_ogs)
        delete_first = delete_first.difference(used_group_ogs)
        delete_second = orphaned_ogs.difference(delete_first)

        ret = srch_1(root, box, delete_first, orphaned_dict, ret)
        ret = srch_2(root, box, delete_second, orphaned_dict, ret

    if empty:
        stat = "No Orphaned Object Groups"
    else:
        stat = "Success"

    return ret

def srch_1(root, box, delete_first, orphaned_dict, ret):
    del_f_count = len(delete_first)
    while del_f_count:
        for og in delete_first:
            if orphaned_dict[og]["deleted"] == 1:
                continue
            flag = 0
            for og2 in delete_first:
                if orphaned_dict[og]["deleted"] == 1:
                    continue
                if og in orphaned_dict[og2]["group_ogs"]:
                    flag = 1
                    break
            if not flag:
                if orphaned_dict[og]["og_type"] in ret.keys():
                    ret[orphaned_dict[og]["og_type"]].append(og)
                else:
                    ret[orphaned_dict[og]["og_type"]] = [og]

                del_f_count -= 1
                orphaned_dict[og]["deleted"] = 1
                #print og + ": "
                #print orphaned_dict[og]["group_ogs"]
                #del root.devices.device[box].config.asa__object_group[orphaned_dict[og]["og_type"]][og]

    return ret

def srch_2(root, box, delete_second, orphaned_dict, ret):
    for og in delete_second:
        if orphaned_dict[og]["og_type"] in ret.keys():
            ret[orphaned_dict[og]["og_type"]].append(og)
        else:
            ret[orphaned_dict[og]["og_type"]] = [og]
        #print og + ':'
        #del root.devices.device[box].config.asa__object_group[orphaned_dict[og]["og_type"]][og]

    return ret

def remove_ogs(box, og_id, og_type):
    """
    A function that removes the object group from the object group list using
    the arguments passed: device name, object group name, and object group type.
    """
    with ncs.maapi.single_write_trans('ncsadmin', 'python', groups=['ncsadmin']) as t:
        del root.devices.device[box].config.asa__object_group[og_type][og_id]
        try:
            t.apply()
            stat = "Success"
        except:
            stat = "Error Removing"
<|endoftext|>


<commit_before><commit_msg>montage<commit_after><|endoftext|>


<commit_before>import nrrd
import sys
import numpy as np

data, header = nrrd.read(str(sys.argv[2]))

lines = [line.strip() for line in open(str(sys.argv[1]))]

for line in lines:
    if '#' not in line:
        values = line.split(' ')
        xl = np.floor(np.float(values[2])) / np.float(header['space directions'][0][0])
        yl = np.floor(np.float(values[3])) / np.float(header['space directions'][1][1])
        zl = np.floor(np.float(values[4])) / np.float(header['space directions'][2][2])
        xh = np.ceil(np.float(values[2])) / np.float(header['space directions'][0][0])
        yh = np.ceil(np.float(values[3])) / np.float(header['space directions'][1][1])
        zh = np.ceil(np.float(values[4])) / np.float(header['space directions'][2][2])
        value = data[np.floor(xl):np.ceil(xh)+1, np.floor(yl):np.ceil(yh)+1, np.floor(zl):np.ceil(zh)+1]
        if np.sum(value) < 1:
            print(line)
            print(value)
            print(str(xl) + ',' + str(xh))
            print(str(yl) + ',' + str(yh))
            print(str(zl) + ',' + str(zh))
            print(np.shape(data))<commit_msg>tidying up<commit_after>import nrrd
import sys
import numpy as np

data, header = nrrd.read(str(sys.argv[2]))

lines = [line.strip() for line in open(str(sys.argv[1]))]

for line in lines:
    if '#' not in line:
        values = line.split(' ')
        xl = np.floor(np.floor(np.float(values[2])) / np.float(header['space directions'][0][0]))
        yl = np.floor(np.floor(np.float(values[3])) / np.float(header['space directions'][1][1]))
        zl = np.floor(np.floor(np.float(values[4])) / np.float(header['space directions'][2][2]))
        xh = np.ceil(np.ceil(np.float(values[2])) / np.float(header['space directions'][0][0]))+1
        yh = np.ceil(np.ceil(np.float(values[3])) / np.float(header['space directions'][1][1]))+1
        zh = np.ceil(np.ceil(np.float(values[4])) / np.float(header['space directions'][2][2]))+1
        value = data[xl:xh, yl:yh, zl:zh]
        if np.sum(value) < 1:
            print(line)
            print(value)<|endoftext|>