<commit_before>#!/usr/bin/env python
import sys, os, getopt, md5, re, time, tempfile, datetime
from os.path import expanduser

LOCALE=dict()
LOCALE['ioerror']="Unable to open file '%s' for %s"
LOCALE['date'] = '%D'
LOCALE['time'] = '%H:%M'
LOCALE['days'] = '%s days'
LOCALE['hours'] = '%s hours'
LOCALE['minutes'] = '%s minutes'
LOCALE['seconds'] = '%s seconds'
LOCALE['clock'] = 'Clocking %s at %s %s'
LOCALE['currently_working'] = 'Currently clocked in'

CONFIG=dict()
CONFIG['file'] = '.clk'

CONFIG['hi_in'] = 11
CONFIG['hi_out'] = 8
CONFIG['hi_date'] = 12
CONFIG['hi_time'] = 13
CONFIG['hi_now'] = 9
CONFIG['hi_days'] = 8
CONFIG['hi_hours'] = 9
CONFIG['hi_minutes'] = 10
CONFIG['hi_seconds'] = 11

RE = dict()
RE['line'] = re.compile('(\d+) (\w+)')

workingPath = os.getcwd()
filePath = '%s/%s' % (workingPath,CONFIG['file'])

def hi(string, color):
	color = int(color)
	# xterm highlighting
	if color<8:
		return "\033[%dm%s\033[0m" % (color+30,string)
	else:
		return "\033[%dm%s\033[0m" % (color+82,string)

def timeToString(val):
	val = int(val)
	days = val / 86400
	hours = (val % 86400) / 3600
	minutes = (val % 3600) / 60
	seconds = val % 60

	builder = []

	if days>0:
		builder.append(LOCALE['days'] % hi(days, CONFIG['hi_days']))

	if hours>0:
		builder.append(LOCALE['hours'] % hi(hours, CONFIG['hi_hours']))

	if minutes>0:
		builder.append(LOCALE['minutes'] % hi(minutes, CONFIG['hi_minutes']))

	if seconds>0:
		builder.append(LOCALE['seconds'] % hi(seconds, CONFIG['hi_seconds']))

	return ', '.join(builder)

def addLine(line):
	try:
		temp = open(filePath,'a+')
	except IOError:
		print LOCALE['ioerror'] % (filePath,'appending')
		sys.exit(1)
	else:
		unixTime = time.time()
		dateObj = datetime.datetime.fromtimestamp(unixTime)
		dateString = hi(dateObj.strftime(LOCALE['date']), CONFIG['hi_date'])
		timeString = hi(dateObj.strftime(LOCALE['time']), CONFIG['hi_time'])
		status = line
		if status == 'in':
			status = hi(status, CONFIG['hi_in'])
		elif status == 'out':
			status = hi(status, CONFIG['hi_out'])

		print LOCALE['clock'] % (status, dateString, timeString)

		temp.write('%d %s\n' % (time.time(),line))
		temp.close()

def printLines():
	try:
		temp = open(filePath,'r+')
	except IOError:
		print LOCALE['ioerror'] % (filePath,'reading')
		sys.exit(1)
	else:
		lines = [line.strip() for line in temp]
		for line in lines:
			match = RE['line'].search(line)
			if match!=None:
				unixTime = int(match.group(1))
				dateObj = datetime.datetime.fromtimestamp(unixTime)
				dateString = hi(dateObj.strftime(LOCALE['date']), CONFIG['hi_date'])
				timeString = hi(dateObj.strftime(LOCALE['time']), CONFIG['hi_time'])

				status = match.group(2)
				if status == 'in':
					status = hi(status, CONFIG['hi_in'])
				elif status == 'out':
					status = hi(status, CONFIG['hi_out'])

				print '%s %s %s' % (dateString, timeString, status)

def summarizeLines():
	state = 'out'
	startUnix = 0
	startTime = 'none'
	startDate = 'none'
	startStatus = 'none'

	totalTime = 0

	try:
		temp = open(filePath,'r+')
	except IOError:
		print LOCALE['ioerror'] % (filePath,'reading')
		sys.exit(1)
	else:
		lines = [line.strip() for line in temp]
		for line in lines:
			match = RE['line'].search(line)
			if match!=None:
				unixTime = int(match.group(1))
				dateObj = datetime.datetime.fromtimestamp(unixTime)
				dateString = hi(dateObj.strftime(LOCALE['date']), CONFIG['hi_date'])
				timeString = hi(dateObj.strftime(LOCALE['time']), CONFIG['hi_time'])

				status = match.group(2)
				if status == 'in':
					status = hi(status, CONFIG['hi_in'])
				elif status == 'out':
					status = hi(status, CONFIG['hi_out'])

				thisState = match.group(2)
				if state != thisState:
					if thisState == 'out':
						totalTime += unixTime - startUnix
						print '%s %s until %s %s: %s' % (startDate, startTime, dateString, timeString, timeToString(unixTime - startUnix))
					elif thisState == 'in':
						startUnix = unixTime
						startDate = dateString
						startTime = timeString
						startStatus = status
					state = thisState

	if state == 'in':
		unixTime = time.time()
		dateObj = datetime.datetime.fromtimestamp(unixTime)
		dateString = hi(dateObj.strftime(LOCALE['date']), CONFIG['hi_date'])
		timeString = hi(dateObj.strftime(LOCALE['time']), CONFIG['hi_time'])
		print '%s %s %s %s %s: %s' % (startDate, startTime, hi('until',CONFIG['hi_now']), dateString, timeString, timeToString(unixTime - startUnix))
		print hi(LOCALE['currently_working'], CONFIG['hi_now'])

def summarizeDays():
	state = 'out'
	startUnix = 0
	startKey = None
	startTime = 'none'
	startDate = 'none'
	startStatus = 'none'

	totalTime = 0

	days = dict()

	try:
		temp = open(filePath,'r+')
	except IOError:
		print LOCALE['ioerror'] % (filePath,'reading')
		sys.exit(1)
	else:
		lines = [line.strip() for line in temp]
		for line in lines:
			match = RE['line'].search(line)
			if match!=None:
				unixTime = int(match.group(1))
				dateObj = datetime.datetime.fromtimestamp(unixTime)
				dayKey = dateObj.strftime(LOCALE['date'])
				dateString = hi(dateObj.strftime(LOCALE['date']), CONFIG['hi_date'])
				timeString = hi(dateObj.strftime(LOCALE['time']), CONFIG['hi_time'])

				status = match.group(2)
				if status == 'in':
					status = hi(status, CONFIG['hi_in'])
				elif status == 'out':
					status = hi(status, CONFIG['hi_out'])

				thisState = match.group(2)
				if state != thisState:
					if thisState == 'out':
						if startKey not in days:
							days[startKey] = 0
						days[startKey] += unixTime - startUnix
						totalTime += unixTime - startUnix
					elif thisState == 'in':
						startUnix = unixTime
						startKey = dayKey
						startDate = dateString
						startTime = timeString
						startStatus = status
					state = thisState

	if state == 'in':
		unixTime = time.time()
		if startKey not in days:
			days[startKey] = 0
		days[startKey] += unixTime - startUnix
		totalTime += unixTime - startUnix

	for key, val in sorted(days.iteritems()):
		print '%s %s' % (hi(key, CONFIG['hi_date']), timeToString(val))

	print '%s %s' % (hi('total', CONFIG['hi_now']), timeToString(totalTime))

	if state == 'in':
		print hi(LOCALE['currently_working'], CONFIG['hi_now'])

def main(argv):
	if len(argv)==0:
		printLines()
	elif argv[0] == 'in' or argv[0] == 'out':
		addLine(argv[0])
	elif argv[0] == 'print':
		printLines()
	elif argv[0] in ('sum','summary'):
		summarizeLines()
	elif argv[0] in ('day', 'days', 'daily'):
		summarizeDays()

if __name__=='__main__':
	main(sys.argv[1:])
<commit_msg>Added a simple status command<commit_after>#!/usr/bin/env python
import sys, os, getopt, md5, re, time, tempfile, datetime
from os.path import expanduser

LOCALE=dict()
LOCALE['ioerror']="Unable to open file '%s' for %s"
LOCALE['date'] = '%D'
LOCALE['time'] = '%H:%M'
LOCALE['days'] = '%s days'
LOCALE['hours'] = '%s hours'
LOCALE['minutes'] = '%s minutes'
LOCALE['seconds'] = '%s seconds'
LOCALE['clock'] = 'Clocking %s at %s %s'
LOCALE['currently_working'] = 'Currently clocked in'

CONFIG=dict()
CONFIG['file'] = '.clk'

CONFIG['hi_in'] = 11
CONFIG['hi_out'] = 8
CONFIG['hi_date'] = 12
CONFIG['hi_time'] = 13
CONFIG['hi_now'] = 9
CONFIG['hi_days'] = 8
CONFIG['hi_hours'] = 9
CONFIG['hi_minutes'] = 10
CONFIG['hi_seconds'] = 11

RE = dict()
RE['line'] = re.compile('(\d+) (\w+)')

workingPath = os.getcwd()
filePath = '%s/%s' % (workingPath,CONFIG['file'])

def hi(string, color):
	color = int(color)
	# xterm highlighting
	if color<8:
		return "\033[%dm%s\033[0m" % (color+30,string)
	else:
		return "\033[%dm%s\033[0m" % (color+82,string)

def timeToString(val):
	val = int(val)
	days = val / 86400
	hours = (val % 86400) / 3600
	minutes = (val % 3600) / 60
	seconds = val % 60

	builder = []

	if days>0:
		builder.append(LOCALE['days'] % hi(days, CONFIG['hi_days']))

	if hours>0:
		builder.append(LOCALE['hours'] % hi(hours, CONFIG['hi_hours']))

	if minutes>0:
		builder.append(LOCALE['minutes'] % hi(minutes, CONFIG['hi_minutes']))

	if seconds>0:
		builder.append(LOCALE['seconds'] % hi(seconds, CONFIG['hi_seconds']))

	return ', '.join(builder)

def addLine(line):
	try:
		temp = open(filePath,'a+')
	except IOError:
		print LOCALE['ioerror'] % (filePath,'appending')
		sys.exit(1)
	else:
		unixTime = time.time()
		dateObj = datetime.datetime.fromtimestamp(unixTime)
		dateString = hi(dateObj.strftime(LOCALE['date']), CONFIG['hi_date'])
		timeString = hi(dateObj.strftime(LOCALE['time']), CONFIG['hi_time'])
		status = line
		if status == 'in':
			status = hi(status, CONFIG['hi_in'])
		elif status == 'out':
			status = hi(status, CONFIG['hi_out'])

		print LOCALE['clock'] % (status, dateString, timeString)

		temp.write('%d %s\n' % (time.time(),line))
		temp.close()

def printLines():
	try:
		temp = open(filePath,'r+')
	except IOError:
		print LOCALE['ioerror'] % (filePath,'reading')
		sys.exit(1)
	else:
		lines = [line.strip() for line in temp]
		for line in lines:
			match = RE['line'].search(line)
			if match!=None:
				unixTime = int(match.group(1))
				dateObj = datetime.datetime.fromtimestamp(unixTime)
				dateString = hi(dateObj.strftime(LOCALE['date']), CONFIG['hi_date'])
				timeString = hi(dateObj.strftime(LOCALE['time']), CONFIG['hi_time'])

				status = match.group(2)
				if status == 'in':
					status = hi(status, CONFIG['hi_in'])
				elif status == 'out':
					status = hi(status, CONFIG['hi_out'])

				print '%s %s %s' % (dateString, timeString, status)

def summarizeLines():
	state = 'out'
	startUnix = 0
	startTime = 'none'
	startDate = 'none'
	startStatus = 'none'

	totalTime = 0

	try:
		temp = open(filePath,'r+')
	except IOError:
		print LOCALE['ioerror'] % (filePath,'reading')
		sys.exit(1)
	else:
		lines = [line.strip() for line in temp]
		for line in lines:
			match = RE['line'].search(line)
			if match!=None:
				unixTime = int(match.group(1))
				dateObj = datetime.datetime.fromtimestamp(unixTime)
				dateString = hi(dateObj.strftime(LOCALE['date']), CONFIG['hi_date'])
				timeString = hi(dateObj.strftime(LOCALE['time']), CONFIG['hi_time'])

				status = match.group(2)
				if status == 'in':
					status = hi(status, CONFIG['hi_in'])
				elif status == 'out':
					status = hi(status, CONFIG['hi_out'])

				thisState = match.group(2)
				if state != thisState:
					if thisState == 'out':
						totalTime += unixTime - startUnix
						print '%s %s until %s %s: %s' % (startDate, startTime, dateString, timeString, timeToString(unixTime - startUnix))
					elif thisState == 'in':
						startUnix = unixTime
						startDate = dateString
						startTime = timeString
						startStatus = status
					state = thisState

	if state == 'in':
		unixTime = time.time()
		dateObj = datetime.datetime.fromtimestamp(unixTime)
		dateString = hi(dateObj.strftime(LOCALE['date']), CONFIG['hi_date'])
		timeString = hi(dateObj.strftime(LOCALE['time']), CONFIG['hi_time'])
		print '%s %s %s %s %s: %s' % (startDate, startTime, hi('until',CONFIG['hi_now']), dateString, timeString, timeToString(unixTime - startUnix))
		print hi(LOCALE['currently_working'], CONFIG['hi_now'])

def summarizeDays():
	state = 'out'
	startUnix = 0
	startKey = None
	startTime = 'none'
	startDate = 'none'
	startStatus = 'none'

	totalTime = 0

	days = dict()

	try:
		temp = open(filePath,'r+')
	except IOError:
		print LOCALE['ioerror'] % (filePath,'reading')
		sys.exit(1)
	else:
		lines = [line.strip() for line in temp]
		for line in lines:
			match = RE['line'].search(line)
			if match!=None:
				unixTime = int(match.group(1))
				dateObj = datetime.datetime.fromtimestamp(unixTime)
				dayKey = dateObj.strftime(LOCALE['date'])
				dateString = hi(dateObj.strftime(LOCALE['date']), CONFIG['hi_date'])
				timeString = hi(dateObj.strftime(LOCALE['time']), CONFIG['hi_time'])

				status = match.group(2)
				if status == 'in':
					status = hi(status, CONFIG['hi_in'])
				elif status == 'out':
					status = hi(status, CONFIG['hi_out'])

				thisState = match.group(2)
				if state != thisState:
					if thisState == 'out':
						if startKey not in days:
							days[startKey] = 0
						days[startKey] += unixTime - startUnix
						totalTime += unixTime - startUnix
					elif thisState == 'in':
						startUnix = unixTime
						startKey = dayKey
						startDate = dateString
						startTime = timeString
						startStatus = status
					state = thisState

	if state == 'in':
		unixTime = time.time()
		if startKey not in days:
			days[startKey] = 0
		days[startKey] += unixTime - startUnix
		totalTime += unixTime - startUnix

	for key, val in sorted(days.iteritems()):
		print '%s %s' % (hi(key, CONFIG['hi_date']), timeToString(val))

	print '%s %s' % (hi('total', CONFIG['hi_now']), timeToString(totalTime))

	if state == 'in':
		print hi(LOCALE['currently_working'], CONFIG['hi_now'])

def printStatus():
	state = 'out'

	try:
		temp = open(filePath,'r+')
	except IOError:
		print LOCALE['ioerror'] % (filePath,'reading')
		sys.exit(1)
	else:
		lines = [line.strip() for line in temp]
		for line in lines:
			match = RE['line'].search(line)
			if match!=None:
				state = match.group(2)

	print state

def main(argv):
	if len(argv)==0:
		printLines()
	elif argv[0] == 'in' or argv[0] == 'out':
		addLine(argv[0])
	elif argv[0] == 'print':
		printLines()
	elif argv[0] in ('sum', 'summary'):
		summarizeLines()
	elif argv[0] in ('day', 'days', 'daily'):
		summarizeDays()
	elif argv[0] in ('st', 'status', 'state'):
		printStatus()

if __name__=='__main__':
	main(sys.argv[1:])
<|endoftext|>


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

import tensorflow as tf

from keras_cv.metrics.coco import utils
from keras_cv.utils import bounding_box
from keras_cv.utils import iou as iou_lib

class COCOMeanAveragePrecision(tf.keras.metrics.Metric):
    """COCOMeanAveragePrecision computes an approximation of MaP.

    Args:
        class_ids: The class IDs to evaluate the metric for.  To evaluate for
            all classes in over a set of sequentially labelled classes, pass
            `range(num_classes)`.
        iou_thresholds: IoU thresholds over which to evaluate the recall.  Must
            be a tuple of floats, defaults to [0.5:0.05:0.95].
        area_range: area range to constrict the considered bounding boxes in
            metric computation. Defaults to `None`, which makes the metric
            count all bounding boxes.  Must be a tuple of floats.  The first
            number in the tuple represents a lower bound for areas, while the
            second value represents an upper bound.  For example, when
            `(0, 32**2)` is passed to the metric, recall is only evaluated for
            objects with areas less than `32*32`.  If `(32**2, 1000000**2)` is
            passed the metric will only be evaluated for boxes with areas larger
            than `32**2`, and smaller than `1000000**2`.
        max_detections: number of maximum detections a model is allowed to make.
            Must be an integer, defaults to `100`.
        recall_thresholds: The list of thresholds to average over in the MaP
            computation.  List of floats.  Defaults to [0:.01:1].
        num_buckets: num_buckets is used to select the number of confidence
            buckets predictions are placed into.  Instead of computation MaP
            over each incrementally selected set of bounding boxes, we instead
            place them into buckets.  This makes distributed computation easier.
            Increasing buckets improves accuracy of the metric, while decreasing
            buckets improves performance.  This is a tradeoff you must weight
            for your use case.  Defaults to 10,000 which is sufficiently large
            for most use cases.

    Usage:

    COCOMeanAveragePrecision accepts two Tensors as input to it's
    `update_state()` method.  These Tensors represent bounding boxes in
    `corners` format.  Utilities to convert Tensors from `xywh` to `corners`
    format can be found in `keras_cv.utils.bounding_box`.

    Each image in a dataset may have a different number of bounding boxes,
    both in the ground truth dataset and the prediction set.  In order to
    account for this, you may either pass a `tf.RaggedTensor`, or pad Tensors
    with `-1`s to indicate unused boxes.  A utility function to perform this
    padding is available at
    `keras_cv_.utils.bounding_box.pad_bounding_box_batch_to_shape()`.

    ```python
    coco_map = keras_cv.metrics.COCOMeanAveragePrecision(
        max_detections=100,
        class_ids=[1]
    )

    y_true = np.array([[[0, 0, 10, 10, 1], [20, 20, 10, 10, 1]]]).astype(np.float32)
    y_pred = np.array([[[0, 0, 10, 10, 1, 1.0], [5, 5, 10, 10, 1, 0.9]]]).astype(
        np.float32
    )
    coco_map.update_state(y_true, y_pred)
    coco_map.result()
    # 0.24752477
    ```
    """

    def __init__(
        self,
        class_ids,
        recall_thresholds=None,
        iou_thresholds=None,
        area_range=None,
        max_detections=100,
        num_buckets=10000,
        **kwargs
    ):
        super().__init__(**kwargs)
        # Initialize parameter values
        self.iou_thresholds = iou_thresholds or [x / 100.0 for x in range(50, 100, 5)]
        self.area_range = area_range
        self.max_detections = max_detections
        self.class_ids = class_ids
        self.recall_thresholds = recall_thresholds or [x / 100 for x in range(0, 101)]
        self.num_buckets = num_buckets

        self.num_iou_thresholds = len(self.iou_thresholds)
        self.num_class_ids = len(self.class_ids)

        self.ground_truths = self.add_weight(
            "ground_truths",
            shape=(self.num_class_ids,),
            dtype=tf.int32,
            initializer="zeros",
        )
        self.true_positive_buckets = self.add_weight(
            "true_positive_buckets",
            shape=(
                self.num_class_ids,
                self.num_iou_thresholds,
                self.num_buckets,
            ),
            dtype=tf.int32,
            initializer="zeros",
        )
        self.false_positive_buckets = self.add_weight(
            "false_positive_buckets",
            shape=(
                self.num_class_ids,
                self.num_iou_thresholds,
                self.num_buckets,
            ),
            dtype=tf.int32,
            initializer="zeros",
        )

    def reset_state(self):
        self.true_positive_buckets.assign(tf.zeros_like(self.true_positive_buckets))
        self.false_positive_buckets.assign(tf.zeros_like(self.false_positive_buckets))
        self.ground_truths.assign(tf.zeros_like(self.ground_truths))

    @tf.function()
    def update_state(self, y_true, y_pred, sample_weight=None):
        if sample_weight is not None:
            warnings.warn(
                "sample_weight is not yet supported in keras_cv COCO metrics."
            )

        num_images = tf.shape(y_true)[0]

        y_pred = utils.sort_bounding_boxes(y_pred, axis=bounding_box.CONFIDENCE)

        ground_truth_boxes_update = tf.zeros_like(self.ground_truths)
        true_positive_buckets_update = tf.zeros_like(self.true_positive_buckets)
        false_positive_buckets_update = tf.zeros_like(self.false_positive_buckets)

        for img in tf.range(num_images):
            ground_truths = utils.filter_out_sentinels(y_true[img])
            detections = utils.filter_out_sentinels(y_pred[img])

            if self.area_range is not None:
                ground_truths = utils.filter_boxes_by_area_range(
                    ground_truths, self.area_range[0], self.area_range[1]
                )
                detections = utils.filter_boxes_by_area_range(
                    detections, self.area_range[0], self.area_range[1]
                )

            detections = detections
            if self.max_detections < tf.shape(detections)[0]:
                detections = detections[: self.max_detections]

            true_positives_update = tf.TensorArray(
                tf.int32, size=self.num_class_ids * self.num_iou_thresholds
            )
            false_positives_update = tf.TensorArray(
                tf.int32, size=self.num_class_ids * self.num_iou_thresholds
            )
            ground_truths_update = tf.TensorArray(tf.int32, size=self.num_class_ids)

            for c_i in range(self.num_class_ids):
                category_id = self.class_ids[c_i]
                ground_truths = utils.filter_boxes(
                    ground_truths, value=category_id, axis=bounding_box.CLASS
                )

                detections = utils.filter_boxes(
                    detections, value=category_id, axis=bounding_box.CLASS
                )
                if self.max_detections < tf.shape(detections)[0]:
                    detections = detections[: self.max_detections]

                ground_truths_update = ground_truths_update.write(
                    c_i, tf.shape(ground_truths)[0]
                )

                ious = iou_lib.compute_ious_for_image(ground_truths, detections)

                for iou_i in range(self.num_iou_thresholds):
                    iou_threshold = self.iou_thresholds[iou_i]
                    pred_matches = utils.match_boxes(ious, iou_threshold)

                    dt_scores = detections[:, bounding_box.CONFIDENCE]

                    true_positives = pred_matches != -1
                    false_positives = pred_matches == -1

                    # We must divide by 1.01 to prevent off by one errors.
                    confidence_buckets = tf.cast(
                        tf.math.floor(self.num_buckets * (dt_scores / 1.01)), tf.int32
                    )
                    true_positives_by_bucket = tf.gather_nd(
                        confidence_buckets, indices=tf.where(true_positives)
                    )
                    false_positives_by_bucket = tf.gather_nd(
                        confidence_buckets, indices=tf.where(false_positives)
                    )

                    true_positive_counts_per_bucket = tf.math.bincount(
                        true_positives_by_bucket,
                        minlength=self.num_buckets,
                        maxlength=self.num_buckets,
                    )
                    false_positives_counts_per_bucket = tf.math.bincount(
                        false_positives_by_bucket,
                        minlength=self.num_buckets,
                        maxlength=self.num_buckets,
                    )

                    true_positives_update = true_positives_update.write(
                        (self.num_iou_thresholds * c_i) + iou_i,
                        true_positive_counts_per_bucket,
                    )
                    false_positives_update = false_positives_update.write(
                        (self.num_iou_thresholds * c_i) + iou_i,
                        false_positives_counts_per_bucket,
                    )

            true_positives_update = tf.reshape(
                true_positives_update.stack(),
                (self.num_class_ids, self.num_iou_thresholds, self.num_buckets),
            )
            false_positives_update = tf.reshape(
                false_positives_update.stack(),
                (self.num_class_ids, self.num_iou_thresholds, self.num_buckets),
            )

            true_positive_buckets_update = (
                true_positive_buckets_update + true_positives_update
            )
            false_positive_buckets_update = (
                false_positive_buckets_update + false_positives_update
            )
            ground_truth_boxes_update = (
                ground_truth_boxes_update + ground_truths_update.stack()
            )

        self.ground_truths.assign_add(ground_truth_boxes_update)
        self.true_positive_buckets.assign_add(true_positive_buckets_update)
        self.false_positive_buckets.assign_add(false_positive_buckets_update)

    @tf.function()
    def result(self):
        true_positives = tf.cast(self.true_positive_buckets, self.dtype)
        false_positives = tf.cast(self.false_positive_buckets, self.dtype)
        ground_truths = tf.cast(self.ground_truths, self.dtype)

        true_positives_sum = tf.cumsum(true_positives, axis=-1)
        false_positives_sum = tf.cumsum(false_positives, axis=-1)

        present_categories = tf.math.reduce_sum(tf.cast(ground_truths != 0, tf.int32))

        if present_categories == 0:
            return 0.0

        recalls = tf.math.divide_no_nan(
            true_positives_sum, ground_truths[:, None, None]
        )
        precisions = true_positives_sum / (false_positives_sum + true_positives_sum)

        result = tf.TensorArray(
            tf.float32, size=self.num_class_ids * self.num_iou_thresholds
        )
        zero_pad = tf.zeros(shape=(1,), dtype=tf.float32)
        # so in this case this should be: [1, 1]
        for i in range(self.num_class_ids):
            for j in range(self.num_iou_thresholds):
                recalls_i = recalls[i, j]
                precisions_i = precisions[i, j]

                # recall threshold=0 finds the first bucket always
                # this is different from the original implementation because the
                # original implementation always has at least one bounding box
                # in the first bucket.
                #
                # as such, we need to mask out the buckets where there is at
                # least one bounding box  Therefore, we must filter out the
                # buckets where (precisions_i) is NaN, as that implies a divide
                # by zero.

                inds = tf.where(not tf.math.is_nan(precisions_i))
                recalls_i = tf.gather_nd(recalls_i, inds)
                precisions_i = tf.gather_nd(precisions_i, inds)

                inds = tf.searchsorted(
                    recalls_i, tf.constant(self.recall_thresholds), side="left"
                )

                # if searchsorted returns len(precisions)+1, we should return 0
                precisions_i = tf.concat([precisions_i, zero_pad], axis=-1)
                precision_per_recall_threshold = tf.gather(precisions_i, inds)

                result_ij = tf.math.reduce_mean(precision_per_recall_threshold, axis=-1)
                result = result.write(j + i * self.num_iou_thresholds, result_ij)

        result = tf.reshape(
            result.stack(), (self.num_class_ids, self.num_iou_thresholds)
        )
        result = tf.math.reduce_mean(result, axis=-1)
        result = tf.math.reduce_sum(result, axis=0) / tf.cast(
            present_categories, tf.float32
        )
        return result
<commit_msg>Disable loop unrolling for COCOMaP (#216)<commit_after># Copyright 2022 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import warnings

import tensorflow as tf

from keras_cv.metrics.coco import utils
from keras_cv.utils import bounding_box
from keras_cv.utils import iou as iou_lib

class COCOMeanAveragePrecision(tf.keras.metrics.Metric):
    """COCOMeanAveragePrecision computes an approximation of MaP.

    Args:
        class_ids: The class IDs to evaluate the metric for.  To evaluate for
            all classes in over a set of sequentially labelled classes, pass
            `range(num_classes)`.
        iou_thresholds: IoU thresholds over which to evaluate the recall.  Must
            be a tuple of floats, defaults to [0.5:0.05:0.95].
        area_range: area range to constrict the considered bounding boxes in
            metric computation. Defaults to `None`, which makes the metric
            count all bounding boxes.  Must be a tuple of floats.  The first
            number in the tuple represents a lower bound for areas, while the
            second value represents an upper bound.  For example, when
            `(0, 32**2)` is passed to the metric, recall is only evaluated for
            objects with areas less than `32*32`.  If `(32**2, 1000000**2)` is
            passed the metric will only be evaluated for boxes with areas larger
            than `32**2`, and smaller than `1000000**2`.
        max_detections: number of maximum detections a model is allowed to make.
            Must be an integer, defaults to `100`.
        recall_thresholds: The list of thresholds to average over in the MaP
            computation.  List of floats.  Defaults to [0:.01:1].
        num_buckets: num_buckets is used to select the number of confidence
            buckets predictions are placed into.  Instead of computation MaP
            over each incrementally selected set of bounding boxes, we instead
            place them into buckets.  This makes distributed computation easier.
            Increasing buckets improves accuracy of the metric, while decreasing
            buckets improves performance.  This is a tradeoff you must weight
            for your use case.  Defaults to 10,000 which is sufficiently large
            for most use cases.

    Usage:

    COCOMeanAveragePrecision accepts two Tensors as input to it's
    `update_state()` method.  These Tensors represent bounding boxes in
    `corners` format.  Utilities to convert Tensors from `xywh` to `corners`
    format can be found in `keras_cv.utils.bounding_box`.

    Each image in a dataset may have a different number of bounding boxes,
    both in the ground truth dataset and the prediction set.  In order to
    account for this, you may either pass a `tf.RaggedTensor`, or pad Tensors
    with `-1`s to indicate unused boxes.  A utility function to perform this
    padding is available at
    `keras_cv_.utils.bounding_box.pad_bounding_box_batch_to_shape()`.

    ```python
    coco_map = keras_cv.metrics.COCOMeanAveragePrecision(
        max_detections=100,
        class_ids=[1]
    )

    y_true = np.array([[[0, 0, 10, 10, 1], [20, 20, 10, 10, 1]]]).astype(np.float32)
    y_pred = np.array([[[0, 0, 10, 10, 1, 1.0], [5, 5, 10, 10, 1, 0.9]]]).astype(
        np.float32
    )
    coco_map.update_state(y_true, y_pred)
    coco_map.result()
    # 0.24752477
    ```
    """

    def __init__(
        self,
        class_ids,
        recall_thresholds=None,
        iou_thresholds=None,
        area_range=None,
        max_detections=100,
        num_buckets=10000,
        **kwargs
    ):
        super().__init__(**kwargs)
        # Initialize parameter values
        self.iou_thresholds = iou_thresholds or [x / 100.0 for x in range(50, 100, 5)]
        self.area_range = area_range
        self.max_detections = max_detections
        self.class_ids = class_ids
        self.recall_thresholds = recall_thresholds or [x / 100 for x in range(0, 101)]
        self.num_buckets = num_buckets

        self.num_iou_thresholds = len(self.iou_thresholds)
        self.num_class_ids = len(self.class_ids)

        self.ground_truths = self.add_weight(
            "ground_truths",
            shape=(self.num_class_ids,),
            dtype=tf.int32,
            initializer="zeros",
        )
        self.true_positive_buckets = self.add_weight(
            "true_positive_buckets",
            shape=(
                self.num_class_ids,
                self.num_iou_thresholds,
                self.num_buckets,
            ),
            dtype=tf.int32,
            initializer="zeros",
        )
        self.false_positive_buckets = self.add_weight(
            "false_positive_buckets",
            shape=(
                self.num_class_ids,
                self.num_iou_thresholds,
                self.num_buckets,
            ),
            dtype=tf.int32,
            initializer="zeros",
        )

    def reset_state(self):
        self.true_positive_buckets.assign(tf.zeros_like(self.true_positive_buckets))
        self.false_positive_buckets.assign(tf.zeros_like(self.false_positive_buckets))
        self.ground_truths.assign(tf.zeros_like(self.ground_truths))

    @tf.function()
    def update_state(self, y_true, y_pred, sample_weight=None):
        if sample_weight is not None:
            warnings.warn(
                "sample_weight is not yet supported in keras_cv COCO metrics."
            )

        class_ids = tf.constant(self.class_ids, dtype=self.compute_dtype)
        iou_thresholds = tf.constant(self.iou_thresholds, dtype=self.compute_dtype)

        num_images = tf.shape(y_true)[0]

        y_pred = utils.sort_bounding_boxes(y_pred, axis=bounding_box.CONFIDENCE)

        ground_truth_boxes_update = tf.zeros_like(self.ground_truths)
        true_positive_buckets_update = tf.zeros_like(self.true_positive_buckets)
        false_positive_buckets_update = tf.zeros_like(self.false_positive_buckets)

        for img in tf.range(num_images):
            ground_truths = utils.filter_out_sentinels(y_true[img])
            detections = utils.filter_out_sentinels(y_pred[img])

            if self.area_range is not None:
                ground_truths = utils.filter_boxes_by_area_range(
                    ground_truths, self.area_range[0], self.area_range[1]
                )
                detections = utils.filter_boxes_by_area_range(
                    detections, self.area_range[0], self.area_range[1]
                )

            if self.max_detections < tf.shape(detections)[0]:
                detections = detections[: self.max_detections]

            true_positives_update = tf.TensorArray(
                tf.int32, size=self.num_class_ids * self.num_iou_thresholds
            )
            false_positives_update = tf.TensorArray(
                tf.int32, size=self.num_class_ids * self.num_iou_thresholds
            )
            ground_truths_update = tf.TensorArray(tf.int32, size=self.num_class_ids)

            for c_i in tf.range(self.num_class_ids):
                category_id = class_ids[c_i]
                ground_truths = utils.filter_boxes(
                    ground_truths, value=category_id, axis=bounding_box.CLASS
                )

                detections = utils.filter_boxes(
                    detections, value=category_id, axis=bounding_box.CLASS
                )
                if self.max_detections < tf.shape(detections)[0]:
                    detections = detections[: self.max_detections]

                ground_truths_update = ground_truths_update.write(
                    c_i, tf.shape(ground_truths)[0]
                )

                ious = iou_lib.compute_ious_for_image(ground_truths, detections)

                for iou_i in tf.range(self.num_iou_thresholds):
                    iou_threshold = iou_thresholds[iou_i]
                    pred_matches = utils.match_boxes(ious, iou_threshold)

                    dt_scores = detections[:, bounding_box.CONFIDENCE]

                    true_positives = pred_matches != -1
                    false_positives = pred_matches == -1

                    # We must divide by 1.01 to prevent off by one errors.
                    confidence_buckets = tf.cast(
                        tf.math.floor(self.num_buckets * (dt_scores / 1.01)), tf.int32
                    )
                    true_positives_by_bucket = tf.gather_nd(
                        confidence_buckets, indices=tf.where(true_positives)
                    )
                    false_positives_by_bucket = tf.gather_nd(
                        confidence_buckets, indices=tf.where(false_positives)
                    )

                    true_positive_counts_per_bucket = tf.math.bincount(
                        true_positives_by_bucket,
                        minlength=self.num_buckets,
                        maxlength=self.num_buckets,
                    )
                    false_positives_counts_per_bucket = tf.math.bincount(
                        false_positives_by_bucket,
                        minlength=self.num_buckets,
                        maxlength=self.num_buckets,
                    )

                    true_positives_update = true_positives_update.write(
                        (self.num_iou_thresholds * c_i) + iou_i,
                        true_positive_counts_per_bucket,
                    )
                    false_positives_update = false_positives_update.write(
                        (self.num_iou_thresholds * c_i) + iou_i,
                        false_positives_counts_per_bucket,
                    )

            true_positives_update = tf.reshape(
                true_positives_update.stack(),
                (self.num_class_ids, self.num_iou_thresholds, self.num_buckets),
            )
            false_positives_update = tf.reshape(
                false_positives_update.stack(),
                (self.num_class_ids, self.num_iou_thresholds, self.num_buckets),
            )

            true_positive_buckets_update = (
                true_positive_buckets_update + true_positives_update
            )
            false_positive_buckets_update = (
                false_positive_buckets_update + false_positives_update
            )
            ground_truth_boxes_update = (
                ground_truth_boxes_update + ground_truths_update.stack()
            )

        self.ground_truths.assign_add(ground_truth_boxes_update)
        self.true_positive_buckets.assign_add(true_positive_buckets_update)
        self.false_positive_buckets.assign_add(false_positive_buckets_update)

    @tf.function()
    def result(self):
        true_positives = tf.cast(self.true_positive_buckets, self.dtype)
        false_positives = tf.cast(self.false_positive_buckets, self.dtype)
        ground_truths = tf.cast(self.ground_truths, self.dtype)

        true_positives_sum = tf.cumsum(true_positives, axis=-1)
        false_positives_sum = tf.cumsum(false_positives, axis=-1)

        present_categories = tf.math.reduce_sum(tf.cast(ground_truths != 0, tf.int32))

        if present_categories == 0:
            return 0.0

        recalls = tf.math.divide_no_nan(
            true_positives_sum, ground_truths[:, None, None]
        )
        precisions = true_positives_sum / (false_positives_sum + true_positives_sum)

        result = tf.TensorArray(
            tf.float32, size=self.num_class_ids * self.num_iou_thresholds
        )
        zero_pad = tf.zeros(shape=(1,), dtype=tf.float32)
        # so in this case this should be: [1, 1]
        for i in tf.range(self.num_class_ids):
            for j in tf.range(self.num_iou_thresholds):
                recalls_i = recalls[i, j]
                precisions_i = precisions[i, j]

                # recall threshold=0 finds the first bucket always
                # this is different from the original implementation because the
                # original implementation always has at least one bounding box
                # in the first bucket.
                #
                # as such, we need to mask out the buckets where there is at
                # least one bounding box  Therefore, we must filter out the
                # buckets where (precisions_i) is NaN, as that implies a divide
                # by zero.

                inds = tf.where(not tf.math.is_nan(precisions_i))
                recalls_i = tf.gather_nd(recalls_i, inds)
                precisions_i = tf.gather_nd(precisions_i, inds)

                inds = tf.searchsorted(
                    recalls_i, tf.constant(self.recall_thresholds), side="left"
                )

                # if searchsorted returns len(precisions)+1, we should return 0
                precisions_i = tf.concat([precisions_i, zero_pad], axis=-1)
                precision_per_recall_threshold = tf.gather(precisions_i, inds)

                result_ij = tf.math.reduce_mean(precision_per_recall_threshold, axis=-1)
                result = result.write(j + i * self.num_iou_thresholds, result_ij)

        result = tf.reshape(
            result.stack(), (self.num_class_ids, self.num_iou_thresholds)
        )
        result = tf.math.reduce_mean(result, axis=-1)
        result = tf.math.reduce_sum(result, axis=0) / tf.cast(
            present_categories, tf.float32
        )
        return result
<|endoftext|>


<commit_before>import os
import os.path

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask_admin import Admin, form
from flask_admin.form import rules
from flask_admin.contrib.sqla import ModelView

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)

# Create directory for file fields to use
file_path = os.path.join(os.path.dirname(__file__), 'static', 'files')
try:
    os.mkdir(file_path)
except OSError:
    pass

from app import views, models

class ShowView(ModelView):
    # Override form field to use Flask-Admin FileUploadField
    form_overrides = {
        'imagePath': form.FileUploadField
    }

    # Pass additional parameters to 'path' to FileUploadField constructor
    form_args = {
        'imagePath': {
            'label': 'Image',
            'base_path': file_path,
            'allow_overwrite': False
        }
    }

class EpisodeView(ModelView):
    # Override form field to use Flask-Admin FileUploadField
    form_overrides = {
        'image_path': form.FileUploadField
    }

    # Pass additional parameters to 'path' to FileUploadField constructor
    form_args = {
        'image_path': {
            'label': 'Image',
            'base_path': file_path,
            'allow_overwrite': False
        }
    }

# ADMIN STUFF
admin = Admin(app, name='microblog', template_mode='bootstrap3')
admin.add_view(ShowView(models.Show, db.session))
admin.add_view(ModelView(models.Episode, db.session))
<commit_msg>Update admin view<commit_after>import os
import os.path

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask_admin import Admin, form
from flask_admin.form import rules
from flask_admin.contrib.sqla import ModelView

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)

# Create directory for file fields to use
file_path = os.path.join(os.path.dirname(__file__), 'static', 'files')
try:
    os.mkdir(file_path)
except OSError:
    pass

from app import views, models

class ShowView(ModelView):
    # Override form field to use Flask-Admin FileUploadField
    form_overrides = {
        'imagePath': form.FileUploadField
    }

    # Pass additional parameters to 'path' to FileUploadField constructor
    form_args = {
        'imagePath': {
            'label': 'Image',
            'base_path': file_path,
            'allow_overwrite': False
        }
    }

class EpisodeView(ModelView):
    # Override form field to use Flask-Admin FileUploadField
    form_overrides = {
        'image_path': form.FileUploadField
    }

    # Pass additional parameters to 'path' to FileUploadField constructor
    form_args = {
        'image_path': {
            'label': 'Image',
            'base_path': file_path,
            'allow_overwrite': False
        }
    }

# ADMIN STUFF
admin = Admin(app, name='microblog', template_mode='bootstrap3')
admin.add_view(ShowView(models.Show, db.session))
admin.add_view(EpisodeView(models.Episode, db.session))
<|endoftext|>


<commit_before>import errno
import socket
import signal
import sshuttle.ssyslog as ssyslog
import sys
import os
import platform
import traceback
from sshuttle.helpers import debug1, debug2, Fatal
from sshuttle.methods import get_auto_method, get_method

hostmap = {}
HOSTSFILE = '/etc/hosts'

def rewrite_etc_hosts(port):
    BAKFILE = '%s.sbak' % HOSTSFILE
    APPEND = '# sshuttle-firewall-%d AUTOCREATED' % port
    old_content = ''
    st = None
    try:
        old_content = open(HOSTSFILE).read()
        st = os.stat(HOSTSFILE)
    except IOError as e:
        if e.errno == errno.ENOENT:
            pass
        else:
            raise
    if old_content.strip() and not os.path.exists(BAKFILE):
        os.link(HOSTSFILE, BAKFILE)
    tmpname = "%s.%d.tmp" % (HOSTSFILE, port)
    f = open(tmpname, 'w')
    for line in old_content.rstrip().split('\n'):
        if line.find(APPEND) >= 0:
            continue
        f.write('%s\n' % line)
    for (name, ip) in sorted(hostmap.items()):
        f.write('%-30s %s\n' % ('%s %s' % (ip, name), APPEND))
    f.close()

    if st is not None:
        os.chown(tmpname, st.st_uid, st.st_gid)
        os.chmod(tmpname, st.st_mode)
    else:
        os.chown(tmpname, 0, 0)
        os.chmod(tmpname, 0o600)
    os.rename(tmpname, HOSTSFILE)

def restore_etc_hosts(port):
    global hostmap
    hostmap = {}
    rewrite_etc_hosts(port)

# Isolate function that needs to be replaced for tests
def setup_daemon():
    if os.getuid() != 0:
        raise Fatal('you must be root (or enable su/sudo) to set the firewall')

    # don't disappear if our controlling terminal or stdout/stderr
    # disappears; we still have to clean up.
    signal.signal(signal.SIGHUP, signal.SIG_IGN)
    signal.signal(signal.SIGPIPE, signal.SIG_IGN)
    signal.signal(signal.SIGTERM, signal.SIG_IGN)
    signal.signal(signal.SIGINT, signal.SIG_IGN)

    # ctrl-c shouldn't be passed along to me.  When the main sshuttle dies,
    # I'll die automatically.
    os.setsid()

    # because of limitations of the 'su' command, the *real* stdin/stdout
    # are both attached to stdout initially.  Clone stdout into stdin so we
    # can read from it.
    os.dup2(1, 0)

    return sys.stdin, sys.stdout

# This is some voodoo for setting up the kernel's transparent
# proxying stuff.  If subnets is empty, we just delete our sshuttle rules;
# otherwise we delete it, then make them from scratch.
#
# This code is supposed to clean up after itself by deleting its rules on
# exit.  In case that fails, it's not the end of the world; future runs will
# supercede it in the transproxy list, at least, so the leftover rules
# are hopefully harmless.
def main(method_name, syslog):
    stdin, stdout = setup_daemon()

    debug1('firewall manager: Starting firewall with Python version %s\n'
           % platform.python_version())

    if method_name == "auto":
        method = get_auto_method()
    else:
        method = get_method(method_name)

    if syslog:
        ssyslog.start_syslog()
        ssyslog.stderr_to_syslog()

    debug1('firewall manager: ready method name %s.\n' % method.name)
    stdout.write('READY %s\n' % method.name)
    stdout.flush()

    # we wait until we get some input before creating the rules.  That way,
    # sshuttle can launch us as early as possible (and get sudo password
    # authentication as early in the startup process as possible).
    line = stdin.readline(128)
    if not line:
        return  # parent died; nothing to do

    subnets = []
    if line != 'ROUTES\n':
        raise Fatal('firewall: expected ROUTES but got %r' % line)
    while 1:
        line = stdin.readline(128)
        if not line:
            raise Fatal('firewall: expected route but got %r' % line)
        elif line.startswith("NSLIST\n"):
            break
        try:
            (family, width, exclude, ip) = line.strip().split(',', 3)
        except:
            raise Fatal('firewall: expected route or NSLIST but got %r' % line)
        subnets.append((int(family), int(width), bool(int(exclude)), ip))
    debug2('firewall manager: Got subnets: %r\n' % subnets)

    nslist = []
    if line != 'NSLIST\n':
        raise Fatal('firewall: expected NSLIST but got %r' % line)
    while 1:
        line = stdin.readline(128)
        if not line:
            raise Fatal('firewall: expected nslist but got %r' % line)
        elif line.startswith("PORTS "):
            break
        try:
            (family, ip) = line.strip().split(',', 1)
        except:
            raise Fatal('firewall: expected nslist or PORTS but got %r' % line)
        nslist.append((int(family), ip))
        debug2('firewall manager: Got partial nslist: %r\n' % nslist)
    debug2('firewall manager: Got nslist: %r\n' % nslist)

    if not line.startswith('PORTS '):
        raise Fatal('firewall: expected PORTS but got %r' % line)
    _, _, ports = line.partition(" ")
    ports = ports.split(",")
    if len(ports) != 4:
        raise Fatal('firewall: expected 4 ports but got %n' % len(ports))
    port_v6 = int(ports[0])
    port_v4 = int(ports[1])
    dnsport_v6 = int(ports[2])
    dnsport_v4 = int(ports[3])

    assert(port_v6 >= 0)
    assert(port_v6 <= 65535)
    assert(port_v4 >= 0)
    assert(port_v4 <= 65535)
    assert(dnsport_v6 >= 0)
    assert(dnsport_v6 <= 65535)
    assert(dnsport_v4 >= 0)
    assert(dnsport_v4 <= 65535)

    debug2('firewall manager: Got ports: %d,%d,%d,%d\n'
           % (port_v6, port_v4, dnsport_v6, dnsport_v4))

    line = stdin.readline(128)
    if not line:
        raise Fatal('firewall: expected GO but got %r' % line)
    elif not line.startswith("GO "):
        raise Fatal('firewall: expected GO but got %r' % line)

    _, _, udp = line.partition(" ")
    udp = bool(int(udp))
    debug2('firewall manager: Got udp: %r\n' % udp)

    try:
        debug1('firewall manager: setting up.\n')

        do_wait = None
        nslist_v6 = [i for i in nslist if i[0] == socket.AF_INET6]
        subnets_v6 = [i for i in subnets if i[0] == socket.AF_INET6]
        if port_v6 > 0:
            debug2('firewall manager: setting up IPv6.\n')
            do_wait = method.setup_firewall(
                port_v6, dnsport_v6, nslist_v6,
                socket.AF_INET6, subnets_v6, udp)
        elif len(subnets_v6) > 0:
            debug1("IPv6 subnets defined but IPv6 disabled\n")

        nslist_v4 = [i for i in nslist if i[0] == socket.AF_INET]
        subnets_v4 = [i for i in subnets if i[0] == socket.AF_INET]
        if port_v4 > 0:
            debug2('firewall manager: setting up IPv4.\n')
            do_wait = method.setup_firewall(
                port_v4, dnsport_v4, nslist_v4,
                socket.AF_INET, subnets_v4, udp)
        elif len(subnets_v4) > 0:
            debug1('firewall manager: '
                   'IPv4 subnets defined but IPv4 disabled\n')

        stdout.write('STARTED\n')

        try:
            stdout.flush()
        except IOError:
            # the parent process died for some reason; he's surely been loud
            # enough, so no reason to report another error
            return

        # Now we wait until EOF or any other kind of exception.  We need
        # to stay running so that we don't need a *second* password
        # authentication at shutdown time - that cleanup is important!
        while 1:
            if do_wait is not None:
                do_wait()
            line = stdin.readline(128)
            if line.startswith('HOST '):
                (name, ip) = line[5:].strip().split(',', 1)
                hostmap[name] = ip
                debug2('firewall manager: setting up /etc/hosts.\n')
                rewrite_etc_hosts(port_v6 or port_v4)
            elif line:
                if not method.firewall_command(line):
                    raise Fatal('firewall: expected command, got %r' % line)
            else:
                break
    finally:
        try:
            debug1('firewall manager: undoing changes.\n')
        except:
            pass

        try:
            if port_v6:
                debug2('firewall manager: undoing IPv6 changes.\n')
                method.setup_firewall(port_v6, 0, [], socket.AF_INET6, [], udp)
        except:
            try:
                debug1("firewall manager: "
                       "Error trying to undo IPv6 firewall.\n")
                for line in traceback.format_exc().splitlines():
                    debug1("---> %s\n" % line)
            except:
                pass

        try:
            if port_v4:
                debug2('firewall manager: undoing IPv4 changes.\n')
            method.setup_firewall(port_v4, 0, [], socket.AF_INET, [], udp)
        except:
            try:
                debug1("firewall manager: "
                       "Error trying to undo IPv4 firewall.\n")
                for line in traceback.format_exc().splitlines():
                    debug1("firewall manager: ---> %s\n" % line)
            except:
                pass

        try:
            debug2('firewall manager: undoing /etc/hosts changes.\n')
            restore_etc_hosts(port_v6 or port_v4)
        except:
            try:
                debug1("firewall manager: "
                       "Error trying to undo /etc/hosts changes.\n")
                for line in traceback.format_exc().splitlines():
                    debug1("firewall manager: ---> %s\n" % line)
            except:
                pass
<commit_msg>hostmap shouldn't be global.<commit_after>import errno
import socket
import signal
import sshuttle.ssyslog as ssyslog
import sys
import os
import platform
import traceback
from sshuttle.helpers import debug1, debug2, Fatal
from sshuttle.methods import get_auto_method, get_method

HOSTSFILE = '/etc/hosts'

def rewrite_etc_hosts(hostmap, port):
    BAKFILE = '%s.sbak' % HOSTSFILE
    APPEND = '# sshuttle-firewall-%d AUTOCREATED' % port
    old_content = ''
    st = None
    try:
        old_content = open(HOSTSFILE).read()
        st = os.stat(HOSTSFILE)
    except IOError as e:
        if e.errno == errno.ENOENT:
            pass
        else:
            raise
    if old_content.strip() and not os.path.exists(BAKFILE):
        os.link(HOSTSFILE, BAKFILE)
    tmpname = "%s.%d.tmp" % (HOSTSFILE, port)
    f = open(tmpname, 'w')
    for line in old_content.rstrip().split('\n'):
        if line.find(APPEND) >= 0:
            continue
        f.write('%s\n' % line)
    for (name, ip) in sorted(hostmap.items()):
        f.write('%-30s %s\n' % ('%s %s' % (ip, name), APPEND))
    f.close()

    if st is not None:
        os.chown(tmpname, st.st_uid, st.st_gid)
        os.chmod(tmpname, st.st_mode)
    else:
        os.chown(tmpname, 0, 0)
        os.chmod(tmpname, 0o600)
    os.rename(tmpname, HOSTSFILE)

def restore_etc_hosts(port):
    rewrite_etc_hosts({}, port)

# Isolate function that needs to be replaced for tests
def setup_daemon():
    if os.getuid() != 0:
        raise Fatal('you must be root (or enable su/sudo) to set the firewall')

    # don't disappear if our controlling terminal or stdout/stderr
    # disappears; we still have to clean up.
    signal.signal(signal.SIGHUP, signal.SIG_IGN)
    signal.signal(signal.SIGPIPE, signal.SIG_IGN)
    signal.signal(signal.SIGTERM, signal.SIG_IGN)
    signal.signal(signal.SIGINT, signal.SIG_IGN)

    # ctrl-c shouldn't be passed along to me.  When the main sshuttle dies,
    # I'll die automatically.
    os.setsid()

    # because of limitations of the 'su' command, the *real* stdin/stdout
    # are both attached to stdout initially.  Clone stdout into stdin so we
    # can read from it.
    os.dup2(1, 0)

    return sys.stdin, sys.stdout

# This is some voodoo for setting up the kernel's transparent
# proxying stuff.  If subnets is empty, we just delete our sshuttle rules;
# otherwise we delete it, then make them from scratch.
#
# This code is supposed to clean up after itself by deleting its rules on
# exit.  In case that fails, it's not the end of the world; future runs will
# supercede it in the transproxy list, at least, so the leftover rules
# are hopefully harmless.
def main(method_name, syslog):
    stdin, stdout = setup_daemon()
    hostmap = {}

    debug1('firewall manager: Starting firewall with Python version %s\n'
           % platform.python_version())

    if method_name == "auto":
        method = get_auto_method()
    else:
        method = get_method(method_name)

    if syslog:
        ssyslog.start_syslog()
        ssyslog.stderr_to_syslog()

    debug1('firewall manager: ready method name %s.\n' % method.name)
    stdout.write('READY %s\n' % method.name)
    stdout.flush()

    # we wait until we get some input before creating the rules.  That way,
    # sshuttle can launch us as early as possible (and get sudo password
    # authentication as early in the startup process as possible).
    line = stdin.readline(128)
    if not line:
        return  # parent died; nothing to do

    subnets = []
    if line != 'ROUTES\n':
        raise Fatal('firewall: expected ROUTES but got %r' % line)
    while 1:
        line = stdin.readline(128)
        if not line:
            raise Fatal('firewall: expected route but got %r' % line)
        elif line.startswith("NSLIST\n"):
            break
        try:
            (family, width, exclude, ip) = line.strip().split(',', 3)
        except:
            raise Fatal('firewall: expected route or NSLIST but got %r' % line)
        subnets.append((int(family), int(width), bool(int(exclude)), ip))
    debug2('firewall manager: Got subnets: %r\n' % subnets)

    nslist = []
    if line != 'NSLIST\n':
        raise Fatal('firewall: expected NSLIST but got %r' % line)
    while 1:
        line = stdin.readline(128)
        if not line:
            raise Fatal('firewall: expected nslist but got %r' % line)
        elif line.startswith("PORTS "):
            break
        try:
            (family, ip) = line.strip().split(',', 1)
        except:
            raise Fatal('firewall: expected nslist or PORTS but got %r' % line)
        nslist.append((int(family), ip))
        debug2('firewall manager: Got partial nslist: %r\n' % nslist)
    debug2('firewall manager: Got nslist: %r\n' % nslist)

    if not line.startswith('PORTS '):
        raise Fatal('firewall: expected PORTS but got %r' % line)
    _, _, ports = line.partition(" ")
    ports = ports.split(",")
    if len(ports) != 4:
        raise Fatal('firewall: expected 4 ports but got %n' % len(ports))
    port_v6 = int(ports[0])
    port_v4 = int(ports[1])
    dnsport_v6 = int(ports[2])
    dnsport_v4 = int(ports[3])

    assert(port_v6 >= 0)
    assert(port_v6 <= 65535)
    assert(port_v4 >= 0)
    assert(port_v4 <= 65535)
    assert(dnsport_v6 >= 0)
    assert(dnsport_v6 <= 65535)
    assert(dnsport_v4 >= 0)
    assert(dnsport_v4 <= 65535)

    debug2('firewall manager: Got ports: %d,%d,%d,%d\n'
           % (port_v6, port_v4, dnsport_v6, dnsport_v4))

    line = stdin.readline(128)
    if not line:
        raise Fatal('firewall: expected GO but got %r' % line)
    elif not line.startswith("GO "):
        raise Fatal('firewall: expected GO but got %r' % line)

    _, _, udp = line.partition(" ")
    udp = bool(int(udp))
    debug2('firewall manager: Got udp: %r\n' % udp)

    try:
        debug1('firewall manager: setting up.\n')

        do_wait = None
        nslist_v6 = [i for i in nslist if i[0] == socket.AF_INET6]
        subnets_v6 = [i for i in subnets if i[0] == socket.AF_INET6]
        if port_v6 > 0:
            debug2('firewall manager: setting up IPv6.\n')
            do_wait = method.setup_firewall(
                port_v6, dnsport_v6, nslist_v6,
                socket.AF_INET6, subnets_v6, udp)
        elif len(subnets_v6) > 0:
            debug1("IPv6 subnets defined but IPv6 disabled\n")

        nslist_v4 = [i for i in nslist if i[0] == socket.AF_INET]
        subnets_v4 = [i for i in subnets if i[0] == socket.AF_INET]
        if port_v4 > 0:
            debug2('firewall manager: setting up IPv4.\n')
            do_wait = method.setup_firewall(
                port_v4, dnsport_v4, nslist_v4,
                socket.AF_INET, subnets_v4, udp)
        elif len(subnets_v4) > 0:
            debug1('firewall manager: '
                   'IPv4 subnets defined but IPv4 disabled\n')

        stdout.write('STARTED\n')

        try:
            stdout.flush()
        except IOError:
            # the parent process died for some reason; he's surely been loud
            # enough, so no reason to report another error
            return

        # Now we wait until EOF or any other kind of exception.  We need
        # to stay running so that we don't need a *second* password
        # authentication at shutdown time - that cleanup is important!
        while 1:
            if do_wait is not None:
                do_wait()
            line = stdin.readline(128)
            if line.startswith('HOST '):
                (name, ip) = line[5:].strip().split(',', 1)
                hostmap[name] = ip
                debug2('firewall manager: setting up /etc/hosts.\n')
                rewrite_etc_hosts(hostmap, port_v6 or port_v4)
            elif line:
                if not method.firewall_command(line):
                    raise Fatal('firewall: expected command, got %r' % line)
            else:
                break
    finally:
        try:
            debug1('firewall manager: undoing changes.\n')
        except:
            pass

        try:
            if port_v6:
                debug2('firewall manager: undoing IPv6 changes.\n')
                method.setup_firewall(port_v6, 0, [], socket.AF_INET6, [], udp)
        except:
            try:
                debug1("firewall manager: "
                       "Error trying to undo IPv6 firewall.\n")
                for line in traceback.format_exc().splitlines():
                    debug1("---> %s\n" % line)
            except:
                pass

        try:
            if port_v4:
                debug2('firewall manager: undoing IPv4 changes.\n')
            method.setup_firewall(port_v4, 0, [], socket.AF_INET, [], udp)
        except:
            try:
                debug1("firewall manager: "
                       "Error trying to undo IPv4 firewall.\n")
                for line in traceback.format_exc().splitlines():
                    debug1("firewall manager: ---> %s\n" % line)
            except:
                pass

        try:
            debug2('firewall manager: undoing /etc/hosts changes.\n')
            restore_etc_hosts(port_v6 or port_v4)
        except:
            try:
                debug1("firewall manager: "
                       "Error trying to undo /etc/hosts changes.\n")
                for line in traceback.format_exc().splitlines():
                    debug1("firewall manager: ---> %s\n" % line)
            except:
                pass
<|endoftext|>


<commit_before># forms.py

import re

from django import forms

from main.models import Episode, Location, Mark

class Who(forms.Form):
    locked_by = forms.CharField(max_length=32, required=True,
            label="Please enter your name")

class Location_Form(forms.ModelForm):
    class Meta:
        model = Location
        exclude = ['active',]

class Location_Active_Form(forms.ModelForm):
    class Meta:
        model = Location
        fields = ['active',]

class Episode_Form_Preshow(forms.ModelForm):
    authors = forms.CharField(max_length=255, required=False)
    emails = forms.CharField(max_length=255, required=False)

    def __init__(self, *args, **kwargs):
        locations = kwargs.get('locations', Location.objects.all())
        if 'locations' in kwargs:
           del kwargs['locations']
        super(Episode_Form_Preshow, self).__init__(*args, **kwargs)
        self.fields['location']._set_choices([(l.id, l.name) for l in locations])

    class Meta:
        model = Episode
        fields = ('sequence',
                  'name', 'slug',
                  'show','location',
                  'start', 'duration',
                  'authors',
                  'emails',
                  'released',
                  'description',
                  'summary',
                  'tags',
                  'twitter_id',
                  'language',
                  )

class Episode_Form_small(forms.ModelForm):
    class Meta:
        model = Episode
        fields = ('state', 'locked', 'locked_by', 'start', 'duration',
                  'name',
                  'emails',
                  'released',
                  'normalise', 'channelcopy',
                  'thumbnail', 'description', 'comment')

class Episode_Form_Mini(forms.ModelForm):
    emails = forms.CharField(max_length=255, required=False)
    class Meta:
        model = Episode
        fields = ('name',
                  'emails',
                  'description',
                  'comment',
                  'start','end',
                  )
        widgets = {
        	'meails': forms.Textarea(attrs={'cols': 80, 'rows': 2}),
        }

class Episode_Reschedule_Form(forms.ModelForm):

    def clean_start_time(self):
        # t is time in "hh mm ss"
        label = 'start'
        t = self.cleaned_data['start_time']
        basedate = self.instance.start
        # print("1 {}".format(t))
        if t:
            try:
                h,m,s = [int(s) for s in re.split('[ _:]', t)]
                dt = basedate.replace(hour=h, minute=m, second=s)
            except Exception as e:
                raise forms.ValidationError(
                        # django doesn't seem to like this, yet.
			'{label} data bad!: {t} - e:{e}',
			code='invalid',
			params={
                             'label':label,
                             't': t,
                             'e':e.message,
                             },
			)

            marks = Mark.objects.filter(click=dt)
            if not marks:
                raise forms.ValidationError(
                        's datetime not found in marks: {dt}',
                        code='invalid',
                        params={'dt': dt},
                        )
        else:
            dt = None
        return dt

    def clean_end_time(self):
        t = self.cleaned_data['end_time']
        if t:
            try:
                h,m,s = [int(s) for s in t.split()]
                dt = self.instance.end.replace(hour=h, minute=m, second=s)
            except Exception as e:
                raise forms.ValidationError(
			 '{label} data bad!: {t} - e:{e}',
			 code='invalid',
			 params={
                             'label':'start',
                             't': t,
                             'e':e,
                             },
			 )

            marks = Mark.objects.filter(click=dt)
            if not marks:
                raise forms.ValidationError(
                        'e datetime not found in marks: {dt}',
                           code='invalid',
                            params={'dt': dt},
                                )
        else:
            dt = None
        return dt

    start_time = forms.CharField(max_length=8,
            required=False,
            widget=forms.TextInput(attrs={'size':'8'}),
            )
            # to_python=ck_start)

    end_time = forms.CharField(max_length=8,
            required=False,
            widget=forms.TextInput(attrs={'size':'8'}),
            )

class clrfForm(forms.Form):
    clid = forms.IntegerField(widget=forms.HiddenInput())
    trash = forms.BooleanField(label="Trash",required=False)
    apply = forms.BooleanField(label="Apply",required=False)
    split = forms.BooleanField(label="Split",required=False)
    sequence = forms.IntegerField(label="Sequence",required=False,
      widget=forms.TextInput(attrs={'size':'3','class':'suSpinButton'}))
    start = forms.CharField(max_length=12,label="Start",required=False,
      help_text = "offset from start in h:m:s or frames, blank for start",
      widget=forms.TextInput(attrs={'size':'9'}))
    end = forms.CharField(max_length=12,label="End",required=False,
      help_text = "offset from start in h:m:s or frames, blank for end",
      widget=forms.TextInput(attrs={'size':'9'}))
    rf_comment = forms.CharField(label="Raw_File comment",required=False,
      widget=forms.Textarea(attrs={'rows':'2','cols':'20'}))
    cl_comment = forms.CharField(label="Cut_List comment",required=False,
      widget=forms.Textarea(attrs={'rows':'2','cols':'20'}))

class Add_CutList_to_Ep(forms.Form):
    rf_filename = forms.CharField(max_length=132,required=False,
      help_text = "root is .../show/dv/location/, example: 2013-03-13/13:13:30.dv" )
    sequence = forms.IntegerField(label="Sequence",required=False,
      widget=forms.TextInput(attrs={'size':'3','class':'suSpinButton'}))
    getit = forms.BooleanField(label="get this", required=False,
            help_text="check and save to add this")

class AddImageToEp(forms.Form):
    image_id = forms.IntegerField(widget=forms.HiddenInput())
    episode_ids  = forms.CharField(max_length=35, required=False,)

class AddEpisodeToRaw(forms.ModelForm):
    class Meta:
        model = Episode
        fields = ('name',
                'duration',
               # 'comment',
                )
    raw_id = forms.IntegerField(widget=forms.HiddenInput())

class MarkPicker(forms.Form):
    apply = forms.BooleanField(label="Apply",required=False)
    click = forms.CharField(max_length=19,label="Start",required=False,
      widget=forms.TextInput(attrs={'size':'15'}))

<commit_msg>Episode_Reschedule_Form tweeks.<commit_after># forms.py

import re

from django import forms

from main.models import Episode, Location, Mark
from main.models import STATES

class Who(forms.Form):
    locked_by = forms.CharField(max_length=32, required=True,
            label="Please enter your name")

class Location_Form(forms.ModelForm):
    class Meta:
        model = Location
        exclude = ['active',]

class Location_Active_Form(forms.ModelForm):
    class Meta:
        model = Location
        fields = ['active',]

class Episode_Form_Preshow(forms.ModelForm):
    authors = forms.CharField(max_length=255, required=False)
    emails = forms.CharField(max_length=255, required=False)

    def __init__(self, *args, **kwargs):
        locations = kwargs.get('locations', Location.objects.all())
        if 'locations' in kwargs:
           del kwargs['locations']
        super(Episode_Form_Preshow, self).__init__(*args, **kwargs)
        self.fields['location']._set_choices([(l.id, l.name) for l in locations])

    class Meta:
        model = Episode
        fields = ('sequence',
                  'name', 'slug',
                  'show','location',
                  'start', 'duration',
                  'authors',
                  'emails',
                  'released',
                  'description',
                  'summary',
                  'tags',
                  'twitter_id',
                  'language',
                  )

class Episode_Form_small(forms.ModelForm):
    class Meta:
        model = Episode
        fields = ('state', 'locked', 'locked_by', 'start', 'duration',
                  'name',
                  'emails',
                  'released',
                  'normalise', 'channelcopy',
                  'thumbnail', 'description', 'comment')

class Episode_Form_Mini(forms.ModelForm):
    emails = forms.CharField(max_length=255, required=False)
    class Meta:
        model = Episode
        fields = ('name',
                  'emails',
                  'description',
                  'comment',
                  'start','end',
                  )
        widgets = {
        	'meails': forms.Textarea(attrs={'cols': 80, 'rows': 2}),
        }

class Episode_Reschedule_Form(forms.ModelForm):

    def mk_dt(self, t, basedate, label):
        # t is time from form in "hh mm ss"

        if t:
            try:
                h,m,s = [int(s) for s in re.split('[ _:]', t)]
                dt = basedate.replace(hour=h, minute=m, second=s)
            except Exception as e:
                print(e)
                raise forms.ValidationError(
                        # django doesn't seem to like this, yet.
			'{label} data bad!: {t} - e:{e}'.format(
                            label=label,
                            t=t,
                            e=e),
			code='invalid',
			params={
                             'label':label,
                             't': t,
                             'e':e.message,
                             },
			)

            marks = Mark.objects.filter(click=dt)
            if not marks:
                raise forms.ValidationError(
                        's datetime not found in marks: {dt}',
                        code='invalid',
                        params={'dt': dt},
                        )
        else:
            dt = None
        return dt

    def clean_start_time(self):
        # t is time in "hh mm ss"
        label = 'start'
        t = self.cleaned_data['start_time']
        basedate = self.instance.start
        dt = self.mk_dt(t, basedate, label)
        return dt

    def clean_end_time(self):
        # t is time in "hh mm ss"
        label = 'end'
        t = self.cleaned_data['end_time']
        basedate = self.instance.end
        dt = self.mk_dt(t, basedate, label)
        return dt

    start_time = forms.CharField(max_length=8,
            required=False,
            widget=forms.TextInput(attrs={'size':'8'}),
            # help = "HH MM SS",
            )

    end_time = forms.CharField(max_length=8,
            required=False,
            widget=forms.TextInput(attrs={'size':'8'}),
            # help = "HH MM SS",
            )

class clrfForm(forms.Form):
    clid = forms.IntegerField(widget=forms.HiddenInput())
    trash = forms.BooleanField(label="Trash",required=False)
    apply = forms.BooleanField(label="Apply",required=False)
    split = forms.BooleanField(label="Split",required=False)
    sequence = forms.IntegerField(label="Sequence",required=False,
      widget=forms.TextInput(attrs={'size':'3','class':'suSpinButton'}))
    start = forms.CharField(max_length=12,label="Start",required=False,
      help_text = "offset from start in h:m:s or frames, blank for start",
      widget=forms.TextInput(attrs={'size':'9'}))
    end = forms.CharField(max_length=12,label="End",required=False,
      help_text = "offset from start in h:m:s or frames, blank for end",
      widget=forms.TextInput(attrs={'size':'9'}))
    rf_comment = forms.CharField(label="Raw_File comment",required=False,
      widget=forms.Textarea(attrs={'rows':'2','cols':'20'}))
    cl_comment = forms.CharField(label="Cut_List comment",required=False,
      widget=forms.Textarea(attrs={'rows':'2','cols':'20'}))

class Add_CutList_to_Ep(forms.Form):
    rf_filename = forms.CharField(max_length=132,required=False,
      help_text = "root is .../show/dv/location/, example: 2013-03-13/13:13:30.dv" )
    sequence = forms.IntegerField(label="Sequence",required=False,
      widget=forms.TextInput(attrs={'size':'3','class':'suSpinButton'}))
    getit = forms.BooleanField(label="get this", required=False,
            help_text="check and save to add this")

class AddImageToEp(forms.Form):
    image_id = forms.IntegerField(widget=forms.HiddenInput())
    episode_ids  = forms.CharField(max_length=35, required=False,)

class AddEpisodeToRaw(forms.ModelForm):
    class Meta:
        model = Episode
        fields = ('name',
                'duration',
               # 'comment',
                )
    raw_id = forms.IntegerField(widget=forms.HiddenInput())

class MarkPicker(forms.Form):
    apply = forms.BooleanField(label="Apply",required=False)
    click = forms.CharField(max_length=19,label="Start",required=False,
      widget=forms.TextInput(attrs={'size':'15'}))

<|endoftext|>


<commit_before>import util

class test_bincount:
    def init(self):
        for dtype in util.TYPES.SIGNED_INT:
            for shape in util.gen_shapes(1, 100, min_ndim=1, iters=5):
                cmd = "R = bh.random.RandomState(42); "
                cmd += "a=R.random_integers(0, high=100, size=%d, dtype=%s, bohrium=BH);" % (shape[0], dtype)
                yield (cmd)

    def test_bincount(self, cmd):
        cmd += "res = M.bincount(a)"
        return cmd

<commit_msg>test: added test of bincount() with weights<commit_after>import util

class test_bincount:
    def init(self):
        for dtype in util.TYPES.SIGNED_INT:
            for shape in util.gen_shapes(1, 100, min_ndim=1, iters=5):
                cmd = "R = bh.random.RandomState(42); "
                cmd += "a=R.random_integers(0, high=100, size=%d, dtype=%s, bohrium=BH);" % (shape[0], dtype)
                yield (cmd)

    def test_bincount(self, cmd):
        cmd += "res = M.bincount(a)"
        return cmd

    def test_int_weights(self, cmd):
        cmd += "w = M.arange(a.shape[0]);"
        cmd += "res = M.bincount(a, w)"
        return cmd

    def test_float_weights(self, cmd):
        cmd += "w = M.arange(a.shape[0]) * 0.42;"
        cmd += "res = M.bincount(a, w)"
        return cmd

<|endoftext|>


<commit_before># Import flask and template operators
from flask import Flask, render_template

# Import SQLAlchemy
from flask.ext.sqlalchemy import SQLAlchemy

# Define the WSGI application object
app = Flask(__name__)

# Configurations
app.config.from_object('config')

# Define the database object which is imported
# by modules and controllers
db = SQLAlchemy(app)

# Sample HTTP error handling
@app.errorhandler(404)
def not_found(error):
    return render_template('404.html'), 404

# Import a module / component using its blueprint handler variable (mod_auth)
# from app.mod_auth.controllers import mod_auth as auth_module

# Register blueprint(s)
# app.register_blueprint(auth_module)
# app.register_blueprint(xyz_module)
# ..

# Build the database:
# This will create the database file using SQLAlchemy
db.create_all()
<commit_msg>Added telegram methods to app init<commit_after># Import flask and template operators
from flask import Flask, render_template

# Import SQLAlchemy
from flask.ext.sqlalchemy import SQLAlchemy

# Import Telepot and pretty printing
import telepot
from pprint import pprint

# Define the WSGI application object
app = Flask(__name__)

# Configurations
app.config.from_object('config')

# Define the database object which is imported
# by modules and controllers
db = SQLAlchemy(app)

# Start Telegram bot loop
TOKEN = '228426808:AAFjJ1Aj9PaRhlVSIIQ3sNRhxjFT_nEEd1A'
bot = telepot.Bot(TOKEN)

def handle(message):
    pprint(message)

bot.message_loop(handle)

# Sample HTTP error handling
@app.errorhandler(404)
def not_found(error):
    return render_template('404.html'), 404

# Import a module / component using its blueprint handler variable (mod_auth)
# from app.mod_auth.controllers import mod_auth as auth_module

# Register blueprint(s)
# app.register_blueprint(auth_module)
# app.register_blueprint(xyz_module)
# ..

# Build the database:
# This will create the database file using SQLAlchemy
db.create_all()
<|endoftext|>


<commit_before>"""
Django middleware to make the admin https only.
"""
__author__ = 'Matt Deacalion Stevens'
__version__ = '1.1.3'
<commit_msg>Bump version to `1.1.4`<commit_after>"""
Django middleware to make the admin https only.
"""
__author__ = 'Matt Deacalion Stevens'
__version__ = '1.1.4'
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Licensed under a 3-clause BSD style license - see LICENSE.rst

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import re
import glob
import os
import shutil
import sys
import datetime
import json

import six
import pytest

from os.path import abspath, dirname, join, isfile, relpath

from asv import config, environment, util
from asv.results import iter_results_for_machine
from asv.util import check_output, which

from . import tools
from .tools import dummy_packages, HAS_CONDA

dummy_values = [
    (None, None),
    (1, 1),
    (3, 1),
    (None, 1),
    (6, None),
    (5, 1),
    (6, 1),
    (6, 1),
    (6, 6),
    (6, 6),
]

def generate_basic_conf(tmpdir, repo_subdir=''):
    tmpdir = six.text_type(tmpdir)
    local = abspath(dirname(__file__))
    os.chdir(tmpdir)

    # Use relative paths on purpose since this is what will be in
    # actual config files

    shutil.copytree(os.path.join(local, 'benchmark'), 'benchmark')

    machine_file = join(tmpdir, 'asv-machine.json')

    shutil.copyfile(join(local, 'asv-machine.json'),
                    machine_file)

    repo_path = tools.generate_test_repo(tmpdir, dummy_values,
                                         subdir=repo_subdir).path

    conf_dict = {
        'env_dir': 'env',
        'benchmark_dir': 'benchmark',
        'results_dir': 'results_workflow',
        'html_dir': 'html',
        'repo': relpath(repo_path),
        'dvcs': 'git',
        'project': 'asv',
        'matrix': {
            "asv_dummy_test_package_1": [""],
            "asv_dummy_test_package_2": tools.DUMMY2_VERSIONS,
        },
    }
    if repo_subdir:
        conf_dict['repo_subdir'] = repo_subdir

    conf = config.Config.from_json(conf_dict)

    if hasattr(sys, 'pypy_version_info'):
        conf.pythons = ["pypy{0[0]}.{0[1]}".format(sys.version_info)]

    return tmpdir, local, conf, machine_file

@pytest.fixture
def basic_conf(tmpdir, dummy_packages):
    return generate_basic_conf(tmpdir)

def test_run_publish(capfd, basic_conf):
    tmpdir, local, conf, machine_file = basic_conf

    conf.matrix = {
        "req": dict(conf.matrix),
        "env":{"SOME_TEST_VAR": ["1"]},
    }

    # Tests a typical complete run/publish workflow
    tools.run_asv_with_conf(conf, 'run', "master~5..master", '--steps=2',
                            '--quick', '--show-stderr', '--profile',
                            '-a', 'warmup_time=0',
                            '--durations=5',
                            _machine_file=machine_file)
    text, err = capfd.readouterr()

    assert len(os.listdir(join(tmpdir, 'results_workflow', 'orangutan'))) == 5
    assert len(os.listdir(join(tmpdir, 'results_workflow'))) == 2
    assert 'asv: benchmark timed out (timeout 0.1s)' in text
    assert 'total duration' in text

    tools.run_asv_with_conf(conf, 'publish')

    assert isfile(join(tmpdir, 'html', 'index.html'))
    assert isfile(join(tmpdir, 'html', 'index.json'))
    assert isfile(join(tmpdir, 'html', 'asv.js'))
    assert isfile(join(tmpdir, 'html', 'asv.css'))

    # Check parameterized test json data format
    filename = glob.glob(join(tmpdir, 'html', 'graphs', 'arch-x86_64',
                              'asv_dummy_test_package_1',
                              'asv_dummy_test_package_2-' + tools.DUMMY2_VERSIONS[1],
                              'branch-master',
                              'cpu-Blazingly fast',
                              'env-SOME_TEST_VAR-1',
                              'machine-orangutan',
                              'os-GNU_Linux', 'python-*', 'ram-128GB',
                              'params_examples.time_skip.json'))[0]
    with open(filename, 'r') as fp:
        data = json.load(fp)
        assert len(data) == 2
        assert isinstance(data[0][0], six.integer_types)  # revision
        assert len(data[0][1]) == 3
        assert len(data[1][1]) == 3
        assert isinstance(data[0][1][0], float)
        assert isinstance(data[0][1][1], float)
        assert data[0][1][2] is None

    # Check that the skip options work
    capfd.readouterr()
    tools.run_asv_with_conf(conf, 'run', "master~5..master", '--steps=2',
                            '--quick', '--skip-existing-successful',
                            '--bench=time_secondary.track_value',
                            '--skip-existing-failed',
                            _machine_file=join(tmpdir, 'asv-machine.json'))
    tools.run_asv_with_conf(conf, 'run', "master~5..master", '--steps=2',
                            '--bench=time_secondary.track_value',
                            '--quick', '--skip-existing-commits',
                            _machine_file=join(tmpdir, 'asv-machine.json'))
    text, err = capfd.readouterr()
    assert 'Running benchmarks.' not in text

    # Check EXISTING and --environment work
    if HAS_CONDA:
        env_spec = ("-E", "conda:{0[0]}.{0[1]}".format(sys.version_info))
    else:
        env_spec = ("-E", "virtualenv:{0[0]}.{0[1]}".format(sys.version_info))
    tools.run_asv_with_conf(conf, 'run', "EXISTING", '--quick',
                            '--bench=time_secondary.track_value',
                            *env_spec,
                            _machine_file=machine_file)

    # Remove the benchmarks.json file and check publish fails

    os.remove(join(tmpdir, "results_workflow", "benchmarks.json"))

    with pytest.raises(util.UserError):
        tools.run_asv_with_conf(conf, 'publish')
<commit_msg>TST: fix too long path in test_workflow<commit_after># -*- coding: utf-8 -*-
# Licensed under a 3-clause BSD style license - see LICENSE.rst

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import re
import glob
import os
import shutil
import sys
import datetime
import json

import six
import pytest

from os.path import abspath, dirname, join, isfile, relpath

from asv import config, environment, util
from asv.results import iter_results_for_machine
from asv.util import check_output, which

from . import tools
from .tools import dummy_packages, HAS_CONDA

dummy_values = [
    (None, None),
    (1, 1),
    (3, 1),
    (None, 1),
    (6, None),
    (5, 1),
    (6, 1),
    (6, 1),
    (6, 6),
    (6, 6),
]

def generate_basic_conf(tmpdir, repo_subdir=''):
    tmpdir = six.text_type(tmpdir)
    local = abspath(dirname(__file__))
    os.chdir(tmpdir)

    # Use relative paths on purpose since this is what will be in
    # actual config files

    shutil.copytree(os.path.join(local, 'benchmark'), 'benchmark')

    machine_file = join(tmpdir, 'asv-machine.json')

    shutil.copyfile(join(local, 'asv-machine.json'),
                    machine_file)

    repo_path = tools.generate_test_repo(tmpdir, dummy_values,
                                         subdir=repo_subdir).path

    conf_dict = {
        'env_dir': 'env',
        'benchmark_dir': 'benchmark',
        'results_dir': 'results_workflow',
        'html_dir': 'html',
        'repo': relpath(repo_path),
        'dvcs': 'git',
        'project': 'asv',
        'matrix': {
            "asv_dummy_test_package_1": [""],
            "asv_dummy_test_package_2": tools.DUMMY2_VERSIONS,
        },
    }
    if repo_subdir:
        conf_dict['repo_subdir'] = repo_subdir

    conf = config.Config.from_json(conf_dict)

    if hasattr(sys, 'pypy_version_info'):
        conf.pythons = ["pypy{0[0]}.{0[1]}".format(sys.version_info)]

    return tmpdir, local, conf, machine_file

@pytest.fixture
def basic_conf(tmpdir, dummy_packages):
    return generate_basic_conf(tmpdir)

def test_run_publish(capfd, basic_conf):
    tmpdir, local, conf, machine_file = basic_conf
    tmpdir = util.long_path(tmpdir)

    conf.matrix = {
        "req": dict(conf.matrix),
        "env":{"SOME_TEST_VAR": ["1"]},
    }

    # Tests a typical complete run/publish workflow
    tools.run_asv_with_conf(conf, 'run', "master~5..master", '--steps=2',
                            '--quick', '--show-stderr', '--profile',
                            '-a', 'warmup_time=0',
                            '--durations=5',
                            _machine_file=machine_file)
    text, err = capfd.readouterr()

    assert len(os.listdir(join(tmpdir, 'results_workflow', 'orangutan'))) == 5
    assert len(os.listdir(join(tmpdir, 'results_workflow'))) == 2
    assert 'asv: benchmark timed out (timeout 0.1s)' in text
    assert 'total duration' in text

    tools.run_asv_with_conf(conf, 'publish')

    assert isfile(join(tmpdir, 'html', 'index.html'))
    assert isfile(join(tmpdir, 'html', 'index.json'))
    assert isfile(join(tmpdir, 'html', 'asv.js'))
    assert isfile(join(tmpdir, 'html', 'asv.css'))

    # Check parameterized test json data format
    filename = glob.glob(join(tmpdir, 'html', 'graphs', 'arch-x86_64',
                              'asv_dummy_test_package_1',
                              'asv_dummy_test_package_2-' + tools.DUMMY2_VERSIONS[1],
                              'branch-master',
                              'cpu-Blazingly fast',
                              'env-SOME_TEST_VAR-1',
                              'machine-orangutan',
                              'os-GNU_Linux', 'python-*', 'ram-128GB',
                              'params_examples.time_skip.json'))[0]
    with open(filename, 'r') as fp:
        data = json.load(fp)
        assert len(data) == 2
        assert isinstance(data[0][0], six.integer_types)  # revision
        assert len(data[0][1]) == 3
        assert len(data[1][1]) == 3
        assert isinstance(data[0][1][0], float)
        assert isinstance(data[0][1][1], float)
        assert data[0][1][2] is None

    # Check that the skip options work
    capfd.readouterr()
    tools.run_asv_with_conf(conf, 'run', "master~5..master", '--steps=2',
                            '--quick', '--skip-existing-successful',
                            '--bench=time_secondary.track_value',
                            '--skip-existing-failed',
                            _machine_file=join(tmpdir, 'asv-machine.json'))
    tools.run_asv_with_conf(conf, 'run', "master~5..master", '--steps=2',
                            '--bench=time_secondary.track_value',
                            '--quick', '--skip-existing-commits',
                            _machine_file=join(tmpdir, 'asv-machine.json'))
    text, err = capfd.readouterr()
    assert 'Running benchmarks.' not in text

    # Check EXISTING and --environment work
    if HAS_CONDA:
        env_spec = ("-E", "conda:{0[0]}.{0[1]}".format(sys.version_info))
    else:
        env_spec = ("-E", "virtualenv:{0[0]}.{0[1]}".format(sys.version_info))
    tools.run_asv_with_conf(conf, 'run', "EXISTING", '--quick',
                            '--bench=time_secondary.track_value',
                            *env_spec,
                            _machine_file=machine_file)

    # Remove the benchmarks.json file and check publish fails

    os.remove(join(tmpdir, "results_workflow", "benchmarks.json"))

    with pytest.raises(util.UserError):
        tools.run_asv_with_conf(conf, 'publish')
<|endoftext|>


<commit_before>"""Test fixtures."""
from builtins import super
from mock import Mock

import pytest
from napalm_base.test import conftest as parent_conftest

from napalm_base.test.double import BaseTestDouble

from napalm_f5 import f5

@pytest.fixture(scope='class')
def set_device_parameters(request):
    """Set up the class."""
    def fin():
        pass
        #request.cls.device.close()
    request.addfinalizer(fin)

    request.cls.driver = f5.F5Driver
    request.cls.patched_driver = PatchedF5Driver
    request.cls.vendor = 'f5'
    parent_conftest.set_device_parameters(request)

def pytest_generate_tests(metafunc):
    """Generate test cases dynamically."""
    parent_conftest.pytest_generate_tests(metafunc, __file__)

class PatchedF5Driver(f5.F5Driver):
    """Patched F5 Driver."""

    def __init__(self, hostname, username, password, timeout=60, optional_args=None):
        """Patched F5 Driver constructor."""
        super().__init__(hostname, username, password, timeout, optional_args)

        self.patched_attrs = ['device']
        self.device = FakeF5Device()
        #self.devices = None

    def open(self):
        pass

class FakeF5Device():
    """F5 device test double."""

    def __init__(self):
        pass
        self.Management = Mock()
        self.Networking = Mock()
        self.System = Mock()

        self.System.SystemInfo.get_hostname = iControlMock.get_hostname
        self.System.SystemInfo.get_marketing_name = iControlMock.get_marketing_name
        self.System.SystemInfo.get_uptime = iControlMock.get_uptime
        self.System.SystemInfo.get_version = iControlMock.get_version
        self.System.SystemInfo.get_system_information = iControlMock.get_system_information

    def open(self):
        pass

    #@staticmethod
    #def get_content(**kwargs):
    #    return 10

    #@staticmethod
    #def name(orig_name):
    #    name = orig_name.replace('.', '_').lower()

class iControlMock():
    @staticmethod
    def get_uptime():
        return 1457282

    @staticmethod
    def get_marketing_name():
        return 'BIG-IP Virtual Edition'

    @staticmethod
    def get_version():
        return 'BIG-IP_v13.0.0'

    @staticmethod
    def get_hostname():
        return 'BIG-IP_v13.0.0'

    @staticmethod
    def get_system_information():
        return {   'annunciator_board_part_revision': None,
    'annunciator_board_serial': None,
    'chassis_serial': '4225052f-6da8-a9c6-ae4b3609da29',
    'host_board_part_revision': None,
    'host_board_serial': None,
    'host_name': 'localhost.localdomain',
    'os_machine': 'x86_64',
    'os_release': '3.10.0-327.36.3.el7.ve.x86_64',
    'os_version': '#1 SMP Tue Jan 31 16:09:28 PST 2017',
    'platform': 'Z100',
    'product_category': 'Virtual Edition',
    'switch_board_part_revision': None,
    'switch_board_serial': None,
    'system_name': 'Linux'}

<commit_msg>_get_facts test<commit_after>"""Test fixtures."""
from builtins import super
from mock import Mock

import pytest
from napalm_base.test import conftest as parent_conftest

from napalm_base.test.double import BaseTestDouble

from napalm_f5 import f5

@pytest.fixture(scope='class')
def set_device_parameters(request):
    """Set up the class."""
    def fin():
        pass
        #request.cls.device.close()
    request.addfinalizer(fin)

    request.cls.driver = f5.F5Driver
    request.cls.patched_driver = PatchedF5Driver
    request.cls.vendor = 'f5'
    parent_conftest.set_device_parameters(request)

def pytest_generate_tests(metafunc):
    """Generate test cases dynamically."""
    parent_conftest.pytest_generate_tests(metafunc, __file__)

class PatchedF5Driver(f5.F5Driver):
    """Patched F5 Driver."""

    def __init__(self, hostname, username, password, timeout=60, optional_args=None):
        """Patched F5 Driver constructor."""
        super().__init__(hostname, username, password, timeout, optional_args)

        self.patched_attrs = ['device']
        self.device = FakeF5Device()
        self.devices = None

    def open(self):
        pass

class FakeF5Device():
    """F5 device test double."""

    def __init__(self):
        self.Management = Mock()
        self.Networking = Mock()
        self.System = Mock()

        self.System.SystemInfo.get_hostname = iControlMock.get_hostname
        self.System.SystemInfo.get_marketing_name = iControlMock.get_marketing_name
        self.System.SystemInfo.get_uptime = iControlMock.get_uptime
        self.System.SystemInfo.get_version = iControlMock.get_version
        self.System.SystemInfo.get_system_information = iControlMock.get_system_information

    def open(self):
        pass

    #@staticmethod
    #def get_content(**kwargs):
    #    return 10

    #@staticmethod
    #def name(orig_name):
    #    name = orig_name.replace('.', '_').lower()

class iControlMock():
    @staticmethod
    def get_uptime():
        return 1457282

    @staticmethod
    def get_marketing_name():
        return 'BIG-IP Virtual Edition'

    @staticmethod
    def get_version():
        return 'BIG-IP_v13.0.0'

    @staticmethod
    def get_hostname():
        return 'BIG-IP_v13.0.0'

    @staticmethod
    def get_system_information():
        return {   'annunciator_board_part_revision': None,
    'annunciator_board_serial': None,
    'chassis_serial': '4225052f-6da8-a9c6-ae4b3609da29',
    'host_board_part_revision': None,
    'host_board_serial': None,
    'host_name': 'localhost.localdomain',
    'os_machine': 'x86_64',
    'os_release': '3.10.0-327.36.3.el7.ve.x86_64',
    'os_version': '#1 SMP Tue Jan 31 16:09:28 PST 2017',
    'platform': 'Z100',
    'product_category': 'Virtual Edition',
    'switch_board_part_revision': None,
    'switch_board_serial': None,
    'system_name': 'Linux'}

<|endoftext|>


<commit_before>__version__ = "0.1.93"

default_app_config = "djes.apps.DJESConfig"
<commit_msg>bump version<commit_after>__version__ = "0.1.94"

default_app_config = "djes.apps.DJESConfig"
<|endoftext|>