<commit_before>from django.conf import settings
from django.db.models.sql import aggregates as sqlaggregates
from django.db.models.sql.compiler import SQLCompiler
from django.db.models.sql.constants import LOOKUP_SEP, MULTI, SINGLE
from django.db.models.sql.where import AND, OR
from django.db.utils import DatabaseError, IntegrityError
from django.utils.tree import Node
import random

EMULATED_OPS = {
    'exact': lambda x, y: y in x if isinstance(x, (list,tuple)) else x == y,
    'iexact': lambda x, y: x.lower() == y.lower(),
    'startswith': lambda x, y: x.startswith(y),
    'istartswith': lambda x, y: x.lower().startswith(y.lower()),
    'isnull': lambda x, y: x is None if y else x is not None,
    'in': lambda x, y: x in y,
    'lt': lambda x, y: x < y,
    'lte': lambda x, y: x <= y,
    'gt': lambda x, y: x > y,
    'gte': lambda x, y: x >= y,
}

class NonrelQuery(object):
    # ----------------------------------------------
    # Public API
    # ----------------------------------------------
    def __init__(self, compiler, fields):
        self.fields = fields
        self.compiler = compiler
        self.connection = compiler.connection
        self.query = self.compiler.query
        self._negated = False

    def fetch(self, low_mark=0, high_mark=None):
        raise NotImplementedError('Not implemented')

    def count(self, limit=None):
        raise NotImplementedError('Not implemented')

    def delete(self):
        raise NotImplementedError('Not implemented')

    def order_by(self, ordering):
        raise NotImplementedError('Not implemented')

    # Used by add_filters()
    def add_filter(self, column, lookup_type, negated, db_type, value):
        raise NotImplementedError('Not implemented')

    # This is just a default implementation. You might want to override this
    # in case your backend supports OR queries
    def add_filters(self, filters):
        """Traverses the given Where tree and adds the filters to this query"""
        if filters.negated:
            self._negated = not self._negated

        if not self._negated and filters.connector != AND:
            raise DatabaseError('Only AND filters are supported')

        # Remove unneeded children from tree
        children = self._get_children(filters.children)

        if self._negated and filters.connector != OR and len(children) > 1:
            raise DatabaseError("When negating a whole filter subgroup "
                                "(e.g., a Q object) the subgroup filters must "
                                "be connected via OR, so the non-relational "
                                "backend can convert them like this: "
                                '"not (a OR b) => (not a) AND (not b)".')

        for child in children:
            if isinstance(child, Node):
                self.add_filters(child)
                continue

            column, lookup_type, db_type, value = self._decode_child(child)
            self.add_filter(column, lookup_type, self._negated, db_type, value)

        if filters.negated:
            self._negated = not self._negated

    # ----------------------------------------------
    # Internal API for reuse by subclasses
    # ----------------------------------------------
    def _decode_child(self, child):
        constraint, lookup_type, annotation, value = child
        packed, value = constraint.process(lookup_type, value, self.connection)
        alias, column, db_type = packed
        value = self._normalize_lookup_value(value, annotation, lookup_type)
        return column, lookup_type, db_type, value

    def _normalize_lookup_value(self, value, annotation, lookup_type):
        # Django fields always return a list (see Field.get_db_prep_lookup)
        # except if get_db_prep_lookup got overridden by a subclass
        if lookup_type not in ('in', 'range', 'year') and isinstance(value, (tuple, list)):
            if len(value) > 1:
                raise DatabaseError('Filter lookup type was: %s. Expected the '
                                'filters value not to be a list. Only "in"-filters '
                                'can be used with lists.'
                                % lookup_type)
            elif lookup_type == 'isnull':
                value = annotation
            else:
                value = value[0]

        if isinstance(value, unicode):
            value = unicode(value)
        elif isinstance(value, str):
            value = str(value)

        if lookup_type in ('startswith', 'istartswith'):
            value = value[:-1]
        elif lookup_type in ('endswith', 'iendswith'):
            value = value[1:]
        elif lookup_type in ('contains', 'icontains'):
            value = value[1:-1]

        return value

    def _get_children(self, children):
        # Filter out nodes that were automatically added by sql.Query, but are
        # not necessary with emulated negation handling code
        result = []
        for child in children:
            if isinstance(child, Node) and child.negated and \
                    len(child.children) == 1 and \
                    isinstance(child.children[0], tuple):
                node, lookup_type, annotation, value = child.children[0]
                if lookup_type == 'isnull' and value == True and node.field is None:
                    continue
            result.append(child)
        return result

    def _matches_filters(self, entity, filters):
        # Filters without rules match everything
        if not filters.children:
            return True

        result = filters.connector == AND

        for child in filters.children:
            if isinstance(child, Node):
                submatch = self._matches_filters(entity, child)
            else:
                constraint, lookup_type, annotation, value = child
                packed, value = constraint.process(lookup_type, value, self.connection)
                alias, column, db_type = packed

                # Django fields always return a list (see Field.get_db_prep_lookup)
                # except if get_db_prep_lookup got overridden by a subclass
                if lookup_type != 'in' and isinstance(value, (tuple, list)):
                    if len(value) > 1:
                        raise DatabaseError('Filter lookup type was: %s. '
                            'Expected the filters value not to be a list. '
                            'Only "in"-filters can be used with lists.'
                            % lookup_type)
                    elif lookup_type == 'isnull':
                        value = annotation
                    else:
                        value = value[0]

                submatch = EMULATED_OPS[lookup_type](entity[column], value)

            if filters.connector == OR and submatch:
                result = True
                break
            elif filters.connector == AND and not submatch:
                result = False
                break

        if filters.negated:
            return not result
        return result

    def _order_in_memory(self, lhs, rhs):
        for order in self.compiler._get_ordering():
            if LOOKUP_SEP in order:
                raise DatabaseError("JOINs in ordering not supported (%s)" % order)
            if order == '?':
                result = random.choice([1, 0, -1])
            else:
                column = order.lstrip('-')
                result = cmp(lhs.get(column), rhs.get(column))
                if order.startswith('-'):
                    result *= -1
            if result != 0:
                return result
        return 0

    def convert_value_from_db(self, db_type, value):
        return self.compiler.convert_value_from_db(db_type, value)

    def convert_value_for_db(self, db_type, value):
        return self.compiler.convert_value_for_db(db_type, value)

class NonrelCompiler(SQLCompiler):
    """
    Base class for non-relational compilers. Provides in-memory filter matching
    and ordering. Entities are assumed to be dictionaries where the keys are
    column names.
    """

    # ----------------------------------------------
    # Public API
    # ----------------------------------------------
    def results_iter(self):
        """
        Returns an iterator over the results from executing this query.
        """
        fields = self.get_fields()
        low_mark = self.query.low_mark
        high_mark = self.query.high_mark
        for entity in self.build_query(fields).fetch(low_mark, high_mark):
            yield self._make_result(entity, fields)

    def _make_result(self, entity, fields):
        result = []
        for field in fields:
            if not field.null and entity.get(field.column,
                    field.get_default()) is None:
                raise DatabaseError("Non-nullable field %s can't be None!" % field.name)
            result.append(self.convert_value_from_db(field.db_type(
                connection=self.connection), entity.get(field.column, field.get_default())))
        return result

    def has_results(self):
        return self.get_count(check_exists=True)

    def execute_sql(self, result_type=MULTI):
        """
        Handles aggregate/count queries
        """
        aggregates = self.query.aggregate_select.values()
        # Simulate a count()
        if aggregates:
            assert len(aggregates) == 1
            aggregate = aggregates[0]
            assert isinstance(aggregate, sqlaggregates.Count)
            meta = self.query.get_meta()
            assert aggregate.col == '*' or aggregate.col == (meta.db_table, meta.pk.column)
            count = self.get_count()
            if result_type is SINGLE:
                return [count]
            elif result_type is MULTI:
                return [[count]]
        raise NotImplementedError('The database backend only supports count() queries')

    # ----------------------------------------------
    # Additional NonrelCompiler API
    # ----------------------------------------------
    def get_count(self, check_exists=False):
        """
        Counts matches using the current filter constraints.
        """
        if check_exists:
            high_mark = 1
        else:
            high_mark = self.query.high_mark
        return self.build_query().count(high_mark)

    def build_query(self, fields=None):
        if fields is None:
            fields = self.get_fields()
        query = self.query_class(self, fields)
        query.add_filters(self.query.where)
        query.order_by(self._get_ordering())

        # This at least satisfies the most basic unit tests
        if settings.DEBUG:
            self.connection.queries.append({'sql': repr(query)})
        return query

    def get_fields(self):
        """
        Returns the fields which should get loaded from the backend by self.query
        """
        # We only set this up here because
        # related_select_fields isn't populated until
        # execute_sql() has been called.
        if self.query.select_fields:
            fields = self.query.select_fields + self.query.related_select_fields
        else:
            fields = self.query.model._meta.fields
        # If the field was deferred, exclude it from being passed
        # into `resolve_columns` because it wasn't selected.
        only_load = self.deferred_to_columns()
        if only_load:
            db_table = self.query.model._meta.db_table
            fields = [f for f in fields if db_table in only_load and
                      f.column in only_load[db_table]]
        return fields

    def _get_ordering(self):
        if not self.query.default_ordering:
            ordering = self.query.order_by
        else:
            ordering = self.query.order_by or self.query.get_meta().ordering
        result = []
        for order in ordering:
            if LOOKUP_SEP in order:
                raise DatabaseError("Ordering can't span tables on non-relational backends (%s)" % order)
            if order == '?':
                raise DatabaseError("Randomized ordering isn't supported by the backend")

            order = order.lstrip('+')

            descending = order.startswith('-')
            name = order.lstrip('-')
            if name == 'pk':
                name = self.query.get_meta().pk.name
                order = '-' + name if descending else name

            if self.query.standard_ordering:
                result.append(order)
            else:
                if descending:
                    result.append(name)
                else:
                    result.append('-' + name)
        return result

class NonrelInsertCompiler(object):
    def execute_sql(self, return_id=False):
        data = {}
        for (field, value), column in zip(self.query.values, self.query.columns):
            if field is not None:
                if not field.null and value is None:
                    raise DatabaseError("You can't set %s (a non-nullable "
                                        "field) to None!" % field.name)
                value = self.convert_value_for_db(field.db_type(connection=self.connection),
                    value)
            data[column] = value
        return self.insert(data, return_id=return_id)

class NonrelUpdateCompiler(object):
    def execute_sql(self, result_type=MULTI):
        # TODO: We don't yet support QuerySet.update() in Django-nonrel
        raise NotImplementedError('No updates')

class NonrelDeleteCompiler(object):
    def execute_sql(self, result_type=MULTI):
        self.build_query([self.query.get_meta().pk]).delete()
<commit_msg>added query checks and detection for multi-table inheritance<commit_after>from django.conf import settings
from django.db.models.sql import aggregates as sqlaggregates
from django.db.models.sql.compiler import SQLCompiler
from django.db.models.sql.constants import LOOKUP_SEP, MULTI, SINGLE
from django.db.models.sql.where import AND, OR
from django.db.utils import DatabaseError, IntegrityError
from django.utils.tree import Node
import random

EMULATED_OPS = {
    'exact': lambda x, y: y in x if isinstance(x, (list,tuple)) else x == y,
    'iexact': lambda x, y: x.lower() == y.lower(),
    'startswith': lambda x, y: x.startswith(y),
    'istartswith': lambda x, y: x.lower().startswith(y.lower()),
    'isnull': lambda x, y: x is None if y else x is not None,
    'in': lambda x, y: x in y,
    'lt': lambda x, y: x < y,
    'lte': lambda x, y: x <= y,
    'gt': lambda x, y: x > y,
    'gte': lambda x, y: x >= y,
}

class NonrelQuery(object):
    # ----------------------------------------------
    # Public API
    # ----------------------------------------------
    def __init__(self, compiler, fields):
        self.fields = fields
        self.compiler = compiler
        self.connection = compiler.connection
        self.query = self.compiler.query
        self._negated = False

    def fetch(self, low_mark=0, high_mark=None):
        raise NotImplementedError('Not implemented')

    def count(self, limit=None):
        raise NotImplementedError('Not implemented')

    def delete(self):
        raise NotImplementedError('Not implemented')

    def order_by(self, ordering):
        raise NotImplementedError('Not implemented')

    # Used by add_filters()
    def add_filter(self, column, lookup_type, negated, db_type, value):
        raise NotImplementedError('Not implemented')

    # This is just a default implementation. You might want to override this
    # in case your backend supports OR queries
    def add_filters(self, filters):
        """Traverses the given Where tree and adds the filters to this query"""
        if filters.negated:
            self._negated = not self._negated

        if not self._negated and filters.connector != AND:
            raise DatabaseError('Only AND filters are supported')

        # Remove unneeded children from tree
        children = self._get_children(filters.children)

        if self._negated and filters.connector != OR and len(children) > 1:
            raise DatabaseError("When negating a whole filter subgroup "
                                "(e.g., a Q object) the subgroup filters must "
                                "be connected via OR, so the non-relational "
                                "backend can convert them like this: "
                                '"not (a OR b) => (not a) AND (not b)".')

        for child in children:
            if isinstance(child, Node):
                self.add_filters(child)
                continue

            column, lookup_type, db_type, value = self._decode_child(child)
            self.add_filter(column, lookup_type, self._negated, db_type, value)

        if filters.negated:
            self._negated = not self._negated

    # ----------------------------------------------
    # Internal API for reuse by subclasses
    # ----------------------------------------------
    def _decode_child(self, child):
        constraint, lookup_type, annotation, value = child
        packed, value = constraint.process(lookup_type, value, self.connection)
        alias, column, db_type = packed
        if alias and alias != self.query.model._meta.db_table:
            raise DatabaseError("This database doesn't support JOINs "
                                "and multi-table inheritance.")
        value = self._normalize_lookup_value(value, annotation, lookup_type)
        return column, lookup_type, db_type, value

    def _normalize_lookup_value(self, value, annotation, lookup_type):
        # Django fields always return a list (see Field.get_db_prep_lookup)
        # except if get_db_prep_lookup got overridden by a subclass
        if lookup_type not in ('in', 'range', 'year') and isinstance(value, (tuple, list)):
            if len(value) > 1:
                raise DatabaseError('Filter lookup type was: %s. Expected the '
                                'filters value not to be a list. Only "in"-filters '
                                'can be used with lists.'
                                % lookup_type)
            elif lookup_type == 'isnull':
                value = annotation
            else:
                value = value[0]

        if isinstance(value, unicode):
            value = unicode(value)
        elif isinstance(value, str):
            value = str(value)

        if lookup_type in ('startswith', 'istartswith'):
            value = value[:-1]
        elif lookup_type in ('endswith', 'iendswith'):
            value = value[1:]
        elif lookup_type in ('contains', 'icontains'):
            value = value[1:-1]

        return value

    def _get_children(self, children):
        # Filter out nodes that were automatically added by sql.Query, but are
        # not necessary with emulated negation handling code
        result = []
        for child in children:
            if isinstance(child, Node) and child.negated and \
                    len(child.children) == 1 and \
                    isinstance(child.children[0], tuple):
                node, lookup_type, annotation, value = child.children[0]
                if lookup_type == 'isnull' and value == True and node.field is None:
                    continue
            result.append(child)
        return result

    def _matches_filters(self, entity, filters):
        # Filters without rules match everything
        if not filters.children:
            return True

        result = filters.connector == AND

        for child in filters.children:
            if isinstance(child, Node):
                submatch = self._matches_filters(entity, child)
            else:
                constraint, lookup_type, annotation, value = child
                packed, value = constraint.process(lookup_type, value, self.connection)
                alias, column, db_type = packed
                if alias != self.query.model._meta.db_table:
                    raise DatabaseError("This database doesn't support JOINs "
                                        "and multi-table inheritance.")

                # Django fields always return a list (see Field.get_db_prep_lookup)
                # except if get_db_prep_lookup got overridden by a subclass
                if lookup_type != 'in' and isinstance(value, (tuple, list)):
                    if len(value) > 1:
                        raise DatabaseError('Filter lookup type was: %s. '
                            'Expected the filters value not to be a list. '
                            'Only "in"-filters can be used with lists.'
                            % lookup_type)
                    elif lookup_type == 'isnull':
                        value = annotation
                    else:
                        value = value[0]

                submatch = EMULATED_OPS[lookup_type](entity[column], value)

            if filters.connector == OR and submatch:
                result = True
                break
            elif filters.connector == AND and not submatch:
                result = False
                break

        if filters.negated:
            return not result
        return result

    def _order_in_memory(self, lhs, rhs):
        for order in self.compiler._get_ordering():
            if LOOKUP_SEP in order:
                raise DatabaseError("JOINs in ordering not supported (%s)" % order)
            if order == '?':
                result = random.choice([1, 0, -1])
            else:
                column = order.lstrip('-')
                result = cmp(lhs.get(column), rhs.get(column))
                if order.startswith('-'):
                    result *= -1
            if result != 0:
                return result
        return 0

    def convert_value_from_db(self, db_type, value):
        return self.compiler.convert_value_from_db(db_type, value)

    def convert_value_for_db(self, db_type, value):
        return self.compiler.convert_value_for_db(db_type, value)

class NonrelCompiler(SQLCompiler):
    """
    Base class for non-relational compilers. Provides in-memory filter matching
    and ordering. Entities are assumed to be dictionaries where the keys are
    column names.
    """

    # ----------------------------------------------
    # Public API
    # ----------------------------------------------
    def results_iter(self):
        """
        Returns an iterator over the results from executing this query.
        """
        self.check_query()
        fields = self.get_fields()
        low_mark = self.query.low_mark
        high_mark = self.query.high_mark
        for entity in self.build_query(fields).fetch(low_mark, high_mark):
            yield self._make_result(entity, fields)

    def _make_result(self, entity, fields):
        result = []
        for field in fields:
            if not field.null and entity.get(field.column,
                    field.get_default()) is None:
                raise DatabaseError("Non-nullable field %s can't be None!" % field.name)
            result.append(self.convert_value_from_db(field.db_type(
                connection=self.connection), entity.get(field.column, field.get_default())))
        return result

    def has_results(self):
        return self.get_count(check_exists=True)

    def execute_sql(self, result_type=MULTI):
        """
        Handles aggregate/count queries
        """
        aggregates = self.query.aggregate_select.values()
        # Simulate a count()
        if aggregates:
            assert len(aggregates) == 1
            aggregate = aggregates[0]
            assert isinstance(aggregate, sqlaggregates.Count)
            meta = self.query.get_meta()
            assert aggregate.col == '*' or aggregate.col == (meta.db_table, meta.pk.column)
            count = self.get_count()
            if result_type is SINGLE:
                return [count]
            elif result_type is MULTI:
                return [[count]]
        raise NotImplementedError('The database backend only supports count() queries')

    # ----------------------------------------------
    # Additional NonrelCompiler API
    # ----------------------------------------------
    def check_query(self):
        if (len([a for a in self.query.alias_map if self.query.alias_refcount[a]]) > 1
                or self.query.distinct or self.query.extra or self.query.having):
            raise DatabaseError('This query is not supported by the database.')

    def get_count(self, check_exists=False):
        """
        Counts matches using the current filter constraints.
        """
        if check_exists:
            high_mark = 1
        else:
            high_mark = self.query.high_mark
        return self.build_query().count(high_mark)

    def build_query(self, fields=None):
        if fields is None:
            fields = self.get_fields()
        query = self.query_class(self, fields)
        query.add_filters(self.query.where)
        query.order_by(self._get_ordering())

        # This at least satisfies the most basic unit tests
        if settings.DEBUG:
            self.connection.queries.append({'sql': repr(query)})
        return query

    def get_fields(self):
        """
        Returns the fields which should get loaded from the backend by self.query
        """
        # We only set this up here because
        # related_select_fields isn't populated until
        # execute_sql() has been called.
        if self.query.select_fields:
            fields = self.query.select_fields + self.query.related_select_fields
        else:
            fields = self.query.model._meta.fields
        # If the field was deferred, exclude it from being passed
        # into `resolve_columns` because it wasn't selected.
        only_load = self.deferred_to_columns()
        if only_load:
            db_table = self.query.model._meta.db_table
            fields = [f for f in fields if db_table in only_load and
                      f.column in only_load[db_table]]
        for field in fields:
            if field.model._meta != self.query.model._meta:
                raise DatabaseError('Multi-table inheritance is not supported '
                                    'by non-relational DBs.')
        return fields

    def _get_ordering(self):
        if not self.query.default_ordering:
            ordering = self.query.order_by
        else:
            ordering = self.query.order_by or self.query.get_meta().ordering
        result = []
        for order in ordering:
            if LOOKUP_SEP in order:
                raise DatabaseError("Ordering can't span tables on non-relational backends (%s)" % order)
            if order == '?':
                raise DatabaseError("Randomized ordering isn't supported by the backend")

            order = order.lstrip('+')

            descending = order.startswith('-')
            name = order.lstrip('-')
            if name == 'pk':
                name = self.query.get_meta().pk.name
                order = '-' + name if descending else name

            if self.query.standard_ordering:
                result.append(order)
            else:
                if descending:
                    result.append(name)
                else:
                    result.append('-' + name)
        return result

class NonrelInsertCompiler(object):
    def execute_sql(self, return_id=False):
        data = {}
        for (field, value), column in zip(self.query.values, self.query.columns):
            if field is not None:
                if not field.null and value is None:
                    raise DatabaseError("You can't set %s (a non-nullable "
                                        "field) to None!" % field.name)
                value = self.convert_value_for_db(field.db_type(connection=self.connection),
                    value)
            data[column] = value
        return self.insert(data, return_id=return_id)

class NonrelUpdateCompiler(object):
    def execute_sql(self, result_type=MULTI):
        # TODO: We don't yet support QuerySet.update() in Django-nonrel
        raise NotImplementedError('No updates')

class NonrelDeleteCompiler(object):
    def execute_sql(self, result_type=MULTI):
        self.build_query([self.query.get_meta().pk]).delete()
<|endoftext|>


<commit_before>import os
import urllib2
import sys

from flask import Flask, render_template

from highchart import glucose_target_range_chart
from highchart import input_history_area_chart
from highchart import line_chart

from openaps_reports import OpenAPS
from openaps_reports import Schedule
from settings import Settings
from units import fix_units

app = Flask(__name__)

@app.route('/')
def monitor():
    aps = app.config['OPENAPS']

    recent_glucose = fix_units(aps.recent_glucose())
    predicted_glucose = fix_units(aps.predicted_glucose())
    predicted_glucose_without_dose = aps.predicted_glucose_without_dose()
    targets = Schedule(aps.read_bg_targets()['targets'])
    normalized_history = aps.normalized_history()
    iob = aps.iob()

    target_glucose = glucose_target_range_chart(targets, recent_glucose, predicted_glucose, predicted_glucose_without_dose)

    basal, bolus, square, carbs = input_history_area_chart(reversed(normalized_history))
    actual_glucose = line_chart(reversed(recent_glucose), name='Glucose')
    predicted_glucose = line_chart(predicted_glucose, name='Predicted')
    predicted_glucose_without_dose = line_chart(predicted_glucose_without_dose, name='Predicted Without Dose')
    iob = line_chart(iob, 'IOB')
    cob = line_chart(aps.cob(), 'COB')

    return render_template(
        'monitor.html',
        openaps=aps,
        actual_glucose=actual_glucose,
        predicted_glucose=predicted_glucose,
        predicted_glucose_without_dose=predicted_glucose_without_dose,
        target_glucose=target_glucose,
        iob=iob,
        cob=cob,
        basal=basal,
        bolus=bolus,
        square=square,
        carbs=carbs,
        CSS_ASSETS=CSS_ASSETS,
        JS_ASSETS=JS_ASSETS,
        display_unit=Settings.DISPLAY_UNIT
    )

CSS_ASSETS = (
    ('static/third_party/bootstrap.css', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'),
    ('static/styles.css', None)
)

JS_ASSETS = (
    ('static/third_party/jquery.js', 'https://code.jquery.com/jquery-2.1.4.min.js'),
    ('static/third_party/bootstrap.js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js'),
    ('static/third_party/highcharts.js', 'http://code.highcharts.com/highcharts.js'),
    ('static/third_party/highcharts-more.js', 'http://code.highcharts.com/highcharts-more.js'),
    ('static/monitor.js', None),
)

def preload_assets():
    for filename, url in (JS_ASSETS + CSS_ASSETS):
        if not os.path.exists(filename):
            print '{} not found, downloading from {}'.format(filename, url)
            try:
                contents = urllib2.urlopen(url).read()
            except ValueError, urllib2.HTTPError:
                pass
            else:
                try:
                    os.makedirs(os.path.dirname(filename))
                except os.error:
                    pass

                with open(filename, mode='w') as fp:
                    fp.write(contents)

if __name__ == '__main__':
    path = sys.argv[1]
    preload_assets()

    if os.path.exists(path):
        path = os.path.abspath(path)
        app.config['OPENAPS'] = OpenAPS(path)
        app.debug = True
        app.run(host='0.0.0.0')
    else:
        exit(1)
<commit_msg>Converted units in predicted_glucose_without_dose<commit_after>import os
import urllib2
import sys

from flask import Flask, render_template

from highchart import glucose_target_range_chart
from highchart import input_history_area_chart
from highchart import line_chart

from openaps_reports import OpenAPS
from openaps_reports import Schedule
from settings import Settings
from units import fix_units

app = Flask(__name__)

@app.route('/')
def monitor():
    aps = app.config['OPENAPS']

    recent_glucose = fix_units(aps.recent_glucose())
    predicted_glucose = fix_units(aps.predicted_glucose())
    predicted_glucose_without_dose = fix_units(aps.predicted_glucose_without_dose())
    targets = Schedule(aps.read_bg_targets()['targets'])
    normalized_history = aps.normalized_history()
    iob = aps.iob()

    target_glucose = glucose_target_range_chart(targets, recent_glucose, predicted_glucose, predicted_glucose_without_dose)

    basal, bolus, square, carbs = input_history_area_chart(reversed(normalized_history))
    actual_glucose = line_chart(reversed(recent_glucose), name='Glucose')
    predicted_glucose = line_chart(predicted_glucose, name='Predicted')
    predicted_glucose_without_dose = line_chart(predicted_glucose_without_dose, name='Predicted Without Dose')
    iob = line_chart(iob, 'IOB')
    cob = line_chart(aps.cob(), 'COB')

    return render_template(
        'monitor.html',
        openaps=aps,
        actual_glucose=actual_glucose,
        predicted_glucose=predicted_glucose,
        predicted_glucose_without_dose=predicted_glucose_without_dose,
        target_glucose=target_glucose,
        iob=iob,
        cob=cob,
        basal=basal,
        bolus=bolus,
        square=square,
        carbs=carbs,
        CSS_ASSETS=CSS_ASSETS,
        JS_ASSETS=JS_ASSETS,
        display_unit=Settings.DISPLAY_UNIT
    )

CSS_ASSETS = (
    ('static/third_party/bootstrap.css', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'),
    ('static/styles.css', None)
)

JS_ASSETS = (
    ('static/third_party/jquery.js', 'https://code.jquery.com/jquery-2.1.4.min.js'),
    ('static/third_party/bootstrap.js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js'),
    ('static/third_party/highcharts.js', 'http://code.highcharts.com/highcharts.js'),
    ('static/third_party/highcharts-more.js', 'http://code.highcharts.com/highcharts-more.js'),
    ('static/monitor.js', None),
)

def preload_assets():
    for filename, url in (JS_ASSETS + CSS_ASSETS):
        if not os.path.exists(filename):
            print '{} not found, downloading from {}'.format(filename, url)
            try:
                contents = urllib2.urlopen(url).read()
            except ValueError, urllib2.HTTPError:
                pass
            else:
                try:
                    os.makedirs(os.path.dirname(filename))
                except os.error:
                    pass

                with open(filename, mode='w') as fp:
                    fp.write(contents)

if __name__ == '__main__':
    path = sys.argv[1]
    preload_assets()

    if os.path.exists(path):
        path = os.path.abspath(path)
        app.config['OPENAPS'] = OpenAPS(path)
        app.debug = True
        app.run(host='0.0.0.0')
    else:
        exit(1)
<|endoftext|>


<commit_before>"""
Dummy database backend for Django.

Django uses this if the database ENGINE setting is empty (None or empty string).

Each of these API functions, except connection.close(), raises
ImproperlyConfigured.
"""

import logging
from itertools import islice

from django.core.exceptions import ImproperlyConfigured
from django.db.backends import (
    BaseDatabaseOperations,
    BaseDatabaseClient,
    BaseDatabaseIntrospection,
    BaseDatabaseWrapper,
    BaseDatabaseFeatures,
    BaseDatabaseValidation
)

from django.db.backends.schema import BaseDatabaseSchemaEditor
from django.db.backends.creation import BaseDatabaseCreation
from django.db.models.sql.subqueries import InsertQuery

from google.appengine.ext.db import metadata
from google.appengine.api import datastore

class DatabaseError(Exception):
    pass

class IntegrityError(DatabaseError):
    pass

class NotSupportedError(DatabaseError):
    pass

class Connection(object):
    """ Dummy connection class """
    def __init__(self, wrapper, params):
        self.ops = wrapper.ops
        self.params = params

    def rollback(self):
        pass

    def commit(self):
        pass

    def close(self):
        pass

class Cursor(object):
    """ Dummy cursor class """
    def __init__(self, connection):
        self.connection = connection
        self.results = None
        self.query = None
        self.start_cursor = None
        self.returned_ids = []
        self.queried_fields = []

    def django_instance_to_entity(self, model, fields, raw, instance):
        field_values = {}

        for field in fields:

            value = field.get_db_prep_save(
                getattr(instance, field.attname) if raw else field.pre_save(instance, instance._state.adding),
                connection = self.connection
            )

            if (not field.null and not field.primary_key) and value is None:
                raise IntegrityError("You can't set %s (a non-nullable "
                                         "field) to None!" % field.name)

            #value = self.connection.ops.value_for_db(value, field)
            field_values[field.column] = value

        entity = datastore.Entity(model._meta.db_table)
        entity.update(field_values)
        return entity

    def execute(self, sql, *params):
        raise RuntimeError("Can't execute traditional SQL: '%s'", sql)

    def execute_appengine_query(self, model, query):
        if isinstance(query, InsertQuery):
            self.returned_ids = datastore.Put([ self.django_instance_to_entity(model, query.fields, query.raw, x) for x in query.objs ])

        else:
            #Store the fields we are querying on so we can process the results
            self.queried_fields = [ x.col[1] for x in query.select ]
            if self.queried_fields:
                projection = self.queried_fields
            else:
                projection = None
                self.queried_fields = [ x.column for x in model._meta.fields ]

            pk_field = model._meta.pk.name
            try:
                #Set the column name to __key__ and then we know the order it came
                #if the id was asked for
                self.queried_fields[self.queried_fields.index(pk_field)] = "__key__"
            except ValueError:
                pass

            self.query = datastore.Query(
                model._meta.db_table,
                projection=projection
            )

            #Apply filters

    def fetchmany(self, size):
        logging.error("NOT IMPLEMENTED: Called fetchmany")
        if self.results is None:
            if self.query is None:
                raise Database.Error()

            self.results = self.query.Run(limit=size, start=self.start_cursor)

        self.start_cursor = self.query.GetCursor()

        results = []
        for entity in self.results:
            result = []
            for col in self.queried_fields:
                if col == "__key__":
                    result.append(entity.key().id_or_name())
                else:
                    result.append(entity.get(col))

            results.append(tuple(result))
        return results

    @property
    def lastrowid(self):
        logging.error("NOT IMPLEMENTED: Last row id")
        return self.returned_ids[-1].id_or_name()

class Database(object):
    """ Fake DB API 2.0 for App engine """

    Error = DatabaseError
    DataError = DatabaseError
    DatabaseError = DatabaseError
    OperationalError = DatabaseError
    IntegrityError = IntegrityError
    InternalError = DatabaseError
    ProgrammingError = DatabaseError
    NotSupportedError = NotSupportedError
    InterfaceError = DatabaseError

class DatabaseOperations(BaseDatabaseOperations):
    compiler_module = "djangae.db.backends.appengine.compiler"

    def quote_name(self, name):
        return name

class DatabaseClient(BaseDatabaseClient):
    pass

class DatabaseCreation(BaseDatabaseCreation):
    def sql_create_model(self, model, *args, **kwargs):
        return [], {}

    def sql_for_pending_references(self, model, *args, **kwargs):
        return []

    def sql_indexes_for_model(self, model, *args, **kwargs):
        return []

class DatabaseIntrospection(BaseDatabaseIntrospection):
    def get_table_list(self, cursor):
        return metadata.get_kinds()

class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
    def column_sql(self, model, field):
        return "", {}

    def create_model(self, model):
        """ Don't do anything when creating tables """
        pass

class DatabaseFeatures(BaseDatabaseFeatures):
    empty_fetchmany_value = []

class DatabaseWrapper(BaseDatabaseWrapper):
    operators = {
        'exact': '= %s',
        'gt': '> %s',
        'gte': '>= %s',
        'lt': '< %s',
        'lte': '<= %s'
    }

    Database = Database

    def __init__(self, *args, **kwargs):
        super(DatabaseWrapper, self).__init__(*args, **kwargs)

        self.features = DatabaseFeatures(self)
        self.ops = DatabaseOperations(self)
        self.client = DatabaseClient(self)
        self.creation = DatabaseCreation(self)
        self.introspection = DatabaseIntrospection(self)
        self.validation = BaseDatabaseValidation(self)

    def is_usable(self):
        return True

    def get_connection_params(self):
        return {}

    def get_new_connection(self, params):
        return Connection(self, params)

    def init_connection_state(self):
        pass

    def _set_autocommit(self, enabled):
        pass

    def create_cursor(self):
        return Cursor(self.connection)

    def schema_editor(self, *args, **kwargs):
        return DatabaseSchemaEditor(self, *args, **kwargs)

<commit_msg>Implement more of the appengine database connector, projection queries are now correctly handled<commit_after>"""
Dummy database backend for Django.

Django uses this if the database ENGINE setting is empty (None or empty string).

Each of these API functions, except connection.close(), raises
ImproperlyConfigured.
"""

import logging
from itertools import islice

from django.core.exceptions import ImproperlyConfigured
from django.db.backends import (
    BaseDatabaseOperations,
    BaseDatabaseClient,
    BaseDatabaseIntrospection,
    BaseDatabaseWrapper,
    BaseDatabaseFeatures,
    BaseDatabaseValidation
)

from django.db.backends.schema import BaseDatabaseSchemaEditor
from django.db.backends.creation import BaseDatabaseCreation
from django.db.models.sql.subqueries import InsertQuery

from google.appengine.ext.db import metadata
from google.appengine.api import datastore

from django.db.models.sql.where import AND, OR
from django.utils.tree import Node

OPERATORS_MAP = {
    'exact': '=',
    'gt': '>',
    'gte': '>=',
    'lt': '<',
    'lte': '<=',

    # The following operators are supported with special code below.
    'isnull': None,
    'in': None,
    'startswith': None,
    'range': None,
    'year': None,
}

class DatabaseError(Exception):
    pass

class IntegrityError(DatabaseError):
    pass

class NotSupportedError(DatabaseError):
    pass

class Connection(object):
    """ Dummy connection class """
    def __init__(self, wrapper, params):
        self.creation = wrapper.creation
        self.ops = wrapper.ops
        self.params = params

    def rollback(self):
        pass

    def commit(self):
        pass

    def close(self):
        pass

class Cursor(object):
    """ Dummy cursor class """
    def __init__(self, connection):
        self.connection = connection
        self.results = None
        self.queries = []
        self.start_cursor = None
        self.returned_ids = []
        self.queried_fields = []

    @property
    def query(self):
        return self.queries[0]

    @query.setter
    def query(self, value):
        if self.queries:
            self.queries[0] = value
        else:
            self.queries = [ value ]

    def django_instance_to_entity(self, model, fields, raw, instance):
        field_values = {}

        for field in fields:

            value = field.get_db_prep_save(
                getattr(instance, field.attname) if raw else field.pre_save(instance, instance._state.adding),
                connection = self.connection
            )

            if (not field.null and not field.primary_key) and value is None:
                raise IntegrityError("You can't set %s (a non-nullable "
                                         "field) to None!" % field.name)

            #value = self.connection.ops.value_for_db(value, field)
            field_values[field.column] = value

        entity = datastore.Entity(model._meta.db_table)
        entity.update(field_values)
        return entity

    def execute(self, sql, *params):
        raise RuntimeError("Can't execute traditional SQL: '%s'", sql)

    def _update_projection_state(self, field, lookup_type):
        db_type = field.db_type(self.connection)

        disable_projection = False
        projected_fields = self.query.GetQueryOptions().projection

        if not projected_fields or field.column not in projected_fields:
            return

        if db_type in ("text", "bytes"):
            disable_projection = True

        if lookup_type in ('exact', 'in'):
            disable_projection = True

        if disable_projection:
            new_queries = []
            for query in self.queries:
                new_query = datastore.Query(query._Query__kind) #Nasty
                new_query.update(query)
                new_queries.append(new_query)

            self.queries = new_queries

    def _apply_filters(self, model, where, negated=False):
        if where.negated:
            negated = not negated

        if not negated and where.connector != AND:
            raise DatabaseError("Only AND filters are supported")

        for child in where.children:
            if isinstance(child, Node):
                self._apply_filters(model, child, negated)
                continue

            field, lookup_type, value = self._parse_child(model, child)
            self._apply_filter(model, field, lookup_type, negated, value)

        if where.negated:
            negated = not negated

    def _apply_filter(self, model, field, lookup_type, negated, value, query_to_update=None):
        query_to_update = query_to_update or self.query

        if lookup_type not in OPERATORS_MAP:
            raise DatabaseError("Lookup type %r isn't supported." % lookup_type)

        column = field.column
        if field.primary_key:
            column = "__key__"

        #Disable projection queries if neccessary
        self._update_projection_state(field, lookup_type)

        op = OPERATORS_MAP.get(lookup_type)

        if op is None:
            if lookup_type == "in":
                new_queries = []
                for query in self.queries:
                    for v in value:
                        new_query = datastore.Query(model._meta.db_table)
                        new_query.update(query)
                        new_query = self._apply_filter(model, field, 'exact', negated, v, query_to_update=new_query)
                        new_queries.append(new_query)

                self.queries = new_queries
                return

        if op is None:
            import pdb; pdb.set_trace()

        assert(op is not None)

        query_to_update["%s %s" % (column, op)] = value
        return query_to_update

    def _parse_child(self, model, child):
        constraint, lookup_type, annotation, value = child
        packed, value = constraint.process(lookup_type, value, self.connection)
        alias, column, db_type = packed
        field = constraint.field

        #FIXME: Add support for simple inner joins
        opts = model._meta
        if alias and alias != opts.db_table:
            raise DatabaseError("This database doesn't support JOINs "
                                "and multi-table inheritance.")

        # For parent.child_set queries the field held by the constraint
        # is the parent's primary key, while the field the filter
        # should consider is the child's foreign key field.
        if column != field.column:
            assert field.primary_key
            field = opts.get_field(column[:-3]) #Remove _id
            assert field.rel is not None

        #FIXME: Probably need to do some processing of the value here like
        #djangoappengine does
        return field, lookup_type, value

    def execute_appengine_query(self, model, query):
        if isinstance(query, InsertQuery):
            self.returned_ids = datastore.Put([ self.django_instance_to_entity(model, query.fields, query.raw, x) for x in query.objs ])

        else:
            #Store the fields we are querying on so we can process the results
            self.queried_fields = [ x.col[1] for x in query.select ]
            if self.queried_fields:
                projection = self.queried_fields
            else:
                projection = None
                self.queried_fields = [ x.column for x in model._meta.fields ]

            pk_field = model._meta.pk.name
            try:
                #Set the column name to __key__ and then we know the order it came
                #if the id was asked for
                self.queried_fields[self.queried_fields.index(pk_field)] = "__key__"
            except ValueError:
                pass

            self.query = datastore.Query(
                model._meta.db_table,
                projection=projection
            )

            #Apply filters
            self._apply_filters(model, query.where)

            try:
                self.queries[1]
                self.queries = [ datastore.MultiQuery(self.queries, []) ]
            except IndexError:
                pass

    def fetchmany(self, size):
        logging.error("NOT IMPLEMENTED: Called fetchmany")
        if self.results is None:
            if not self.queries:
                raise Database.Error()

            self.results = self.query.Run(limit=size, start=self.start_cursor)

        self.start_cursor = self.query.GetCursor()

        results = []
        for entity in self.results:
            result = []
            for col in self.queried_fields:
                if col == "__key__":
                    result.append(entity.key().id_or_name())
                else:
                    result.append(entity.get(col))

            results.append(tuple(result))

        return results

    @property
    def lastrowid(self):
        logging.error("NOT IMPLEMENTED: Last row id")
        return self.returned_ids[-1].id_or_name()

class Database(object):
    """ Fake DB API 2.0 for App engine """

    Error = DatabaseError
    DataError = DatabaseError
    DatabaseError = DatabaseError
    OperationalError = DatabaseError
    IntegrityError = IntegrityError
    InternalError = DatabaseError
    ProgrammingError = DatabaseError
    NotSupportedError = NotSupportedError
    InterfaceError = DatabaseError

class DatabaseOperations(BaseDatabaseOperations):
    compiler_module = "djangae.db.backends.appengine.compiler"

    def quote_name(self, name):
        return name

class DatabaseClient(BaseDatabaseClient):
    pass

class DatabaseCreation(BaseDatabaseCreation):
    data_types = {
        'AutoField':                  'key',
        'RelatedAutoField':           'key',
        'ForeignKey':                 'key',
        'OneToOneField':              'key',
        'ManyToManyField':            'key',
        'BigIntegerField':            'long',
        'BooleanField':               'bool',
        'CharField':                  'string',
        'CommaSeparatedIntegerField': 'string',
        'DateField':                  'date',
        'DateTimeField':              'datetime',
        'DecimalField':               'decimal',
        'EmailField':                 'string',
        'FileField':                  'string',
        'FilePathField':              'string',
        'FloatField':                 'float',
        'ImageField':                 'string',
        'IntegerField':               'integer',
        'IPAddressField':             'string',
        'NullBooleanField':           'bool',
        'PositiveIntegerField':       'integer',
        'PositiveSmallIntegerField':  'integer',
        'SlugField':                  'string',
        'SmallIntegerField':          'integer',
        'TextField':                  'string',
        'TimeField':                  'time',
        'URLField':                   'string',
        'AbstractIterableField':      'list',
        'ListField':                  'list',
        'RawField':                   'raw',
        'BlobField':                  'bytes',
        'TextField':                  'text',
        'XMLField':                   'text',
        'SetField':                   'list',
        'DictField':                  'bytes',
        'EmbeddedModelField':         'bytes'
    }

    def sql_create_model(self, model, *args, **kwargs):
        return [], {}

    def sql_for_pending_references(self, model, *args, **kwargs):
        return []

    def sql_indexes_for_model(self, model, *args, **kwargs):
        return []

class DatabaseIntrospection(BaseDatabaseIntrospection):
    def get_table_list(self, cursor):
        return metadata.get_kinds()

class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
    def column_sql(self, model, field):
        return "", {}

    def create_model(self, model):
        """ Don't do anything when creating tables """
        pass

class DatabaseFeatures(BaseDatabaseFeatures):
    empty_fetchmany_value = []

class DatabaseWrapper(BaseDatabaseWrapper):
    operators = {
        'exact': '= %s',
        'gt': '> %s',
        'gte': '>= %s',
        'lt': '< %s',
        'lte': '<= %s'
    }

    Database = Database

    def __init__(self, *args, **kwargs):
        super(DatabaseWrapper, self).__init__(*args, **kwargs)

        self.features = DatabaseFeatures(self)
        self.ops = DatabaseOperations(self)
        self.client = DatabaseClient(self)
        self.creation = DatabaseCreation(self)
        self.introspection = DatabaseIntrospection(self)
        self.validation = BaseDatabaseValidation(self)

    def is_usable(self):
        return True

    def get_connection_params(self):
        return {}

    def get_new_connection(self, params):
        return Connection(self, params)

    def init_connection_state(self):
        pass

    def _set_autocommit(self, enabled):
        pass

    def create_cursor(self):
        return Cursor(self.connection)

    def schema_editor(self, *args, **kwargs):
        return DatabaseSchemaEditor(self, *args, **kwargs)

<|endoftext|>


<commit_before><commit_msg>Avoid Django migration changes on switching FORM_STYLE_CHOICES<commit_after><|endoftext|>


<commit_before>from django.db import models

class Hubs(models.Model):
    HUB = 'h'
    C_MANAGER = 'c'
    TYPE_CHOICES = (
            (HUB, 'Hub'),
            (C_MANAGER, 'Country Manager'),
    )
    hub_type = models.CharField(max_length=5, choices=TYPE_CHOICES, default=HUB)
    name = models.CharField(max_length=100)
    manager = models.CharField(max_length=100)
    country = models.CharField(max_length=50)
    active = models.BooleanField(default=True)
    #Hub area ?
<commit_msg>Add def str to hubs/models<commit_after>from django.db import models

class Hubs(models.Model):
    HUB = 'h'
    C_MANAGER = 'c'
    TYPE_CHOICES = (
            (HUB, 'Hub'),
            (C_MANAGER, 'Country Manager'),
    )
    hub_type = models.CharField(max_length=5, choices=TYPE_CHOICES, default=HUB)
    name = models.CharField(max_length=100)
    manager = models.CharField(max_length=100)
    country = models.CharField(max_length=50)
    active = models.BooleanField(default=True)
    #Hub area ?

    def __str__(self):
        return self.name

<|endoftext|>


<commit_before>from django.conf import settings

from ..conf import conf
from .util import boolean_check

@boolean_check("SECURITY_MIDDLEWARE_NOT_INSTALLED")
def check_security_middleware():
    return ("djangosecure.middleware.SecurityMiddleware" in
            settings.MIDDLEWARE_CLASSES)

check_security_middleware.messages = {
    "SECURITY_MIDDLEWARE_NOT_INSTALLED": (
        "You do not have 'djangosecure.middleware.SecurityMiddleware' "
        "in your MIDDLEWARE_CLASSES, so the SECURE_HSTS_SECONDS, "
        "SECURE_FRAME_DENY, SECURE_CONTENT_TYPE_NOSNIFF, "
        "SECURE_BROWSER_XSS_FILTER and SECURE_SSL_REDIRECT settings "
        "will have no effect.")
    }

@boolean_check("STRICT_TRANSPORT_SECURITY_NOT_ENABLED")
def check_sts():
    return bool(conf.SECURE_HSTS_SECONDS)

check_sts.messages = {
    "STRICT_TRANSPORT_SECURITY_NOT_ENABLED": (
        "You have not set a value for the SECURE_HSTS_SECONDS setting. "
        "If your entire site is served only over SSL, you may want to consider "
        "setting a value and enabling HTTP Strict Transport Security "
        "(see http://en.wikipedia.org/wiki/Strict_Transport_Security)."
        )
    }

@boolean_check("STRICT_TRANSPORT_SECURITY_NO_SUBDOMAINS")
def check_sts_include_subdomains():
    return bool(conf.SECURE_HSTS_INCLUDE_SUBDOMAINS)

check_sts_include_subdomains.messages = {
    "STRICT_TRANSPORT_SECURITY_NO_SUBDOMAINS": (
        "You have not set the SECURE_HSTS_INCLUDE_SUBDOMAINS setting to True. "
        "Without this, your site is potentially vulnerable to attack "
        "via an insecure connection to a subdomain."
        )
    }

@boolean_check("FRAME_DENY_NOT_ENABLED")
def check_frame_deny():
    return conf.SECURE_FRAME_DENY

check_frame_deny.messages = {
    "FRAME_DENY_NOT_ENABLED": (
        "Your SECURE_FRAME_DENY setting is not set to True, "
        "so your pages will not be served with an "
        "'x-frame-options: DENY' header. "
        "Unless there is a good reason for your site to be served in a frame, "
        "you should consider enabling this header "
        "to help prevent clickjacking attacks."
        )
    }

@boolean_check("CONTENT_TYPE_NOSNIFF_NOT_ENABLED")
def check_content_type_nosniff():
    return conf.SECURE_CONTENT_TYPE_NOSNIFF

check_content_type_nosniff.messages = {
    "CONTENT_TYPE_NOSNIFF_NOT_ENABLED": (
        "Your SECURE_CONTENT_TYPE_NOSNIFF setting is not set to True, "
        "so your pages will not be served with an "
        "'x-content-type-options: nosniff' header. "
        "You should consider enabling this header to prevent the "
        "browser from identifying content types incorrectly."
        )
    }

@boolean_check("BROWSER_XSS_FILTER_NOT_ENABLED")
def check_xss_filter():
    return conf.SECURE_BROWSER_XSS_FILTER

check_xss_filter.messages = {
    "BROWSER_XSS_FILTER_NOT_ENABLED": (
        "Your SECURE_BROWSER_XSS_FILTER setting is not set to True, "
        "so your pages will not be served with an "
        "'x-xss-protection: 1; mode=block' header. "
        "You should consider enabling this header to activate the "
        "browser's XSS filtering and help prevent XSS attacks."
        )
    }

@boolean_check("SSL_REDIRECT_NOT_ENABLED")
def check_ssl_redirect():
    return conf.SECURE_SSL_REDIRECT

check_ssl_redirect.messages = {
    "SSL_REDIRECT_NOT_ENABLED": (
        "Your SECURE_SSL_REDIRECT setting is not set to True. "
        "Unless your site should be available over both SSL and non-SSL "
        "connections, you may want to either set this setting True "
        "or configure a loadbalancer or reverse-proxy server "
        "to redirect all connections to HTTPS."
        )
    }

@boolean_check("BAD_SECRET_KEY")
def check_secret_key():
    return len(set(conf.SECRET_KEY)) >= 5 if \
                               getattr(settings, 'SECRET_KEY', None) else False

check_ssl_redirect.messages = {
    "BAD_SECRET_KEY": (
        "Your SECRET_KEY is either an empty string, non-existant, or has not "
        "enough characters. Please generate a long and random SECRET_KEY, "
        "otherwise many of Django's security-critical features will be "
        "vulnerable to attack."
        )
    }
<commit_msg>Fixes after code review<commit_after>from django.conf import settings

from ..conf import conf
from .util import boolean_check

@boolean_check("SECURITY_MIDDLEWARE_NOT_INSTALLED")
def check_security_middleware():
    return ("djangosecure.middleware.SecurityMiddleware" in
            settings.MIDDLEWARE_CLASSES)

check_security_middleware.messages = {
    "SECURITY_MIDDLEWARE_NOT_INSTALLED": (
        "You do not have 'djangosecure.middleware.SecurityMiddleware' "
        "in your MIDDLEWARE_CLASSES, so the SECURE_HSTS_SECONDS, "
        "SECURE_FRAME_DENY, SECURE_CONTENT_TYPE_NOSNIFF, "
        "SECURE_BROWSER_XSS_FILTER and SECURE_SSL_REDIRECT settings "
        "will have no effect.")
    }

@boolean_check("STRICT_TRANSPORT_SECURITY_NOT_ENABLED")
def check_sts():
    return bool(conf.SECURE_HSTS_SECONDS)

check_sts.messages = {
    "STRICT_TRANSPORT_SECURITY_NOT_ENABLED": (
        "You have not set a value for the SECURE_HSTS_SECONDS setting. "
        "If your entire site is served only over SSL, you may want to consider "
        "setting a value and enabling HTTP Strict Transport Security "
        "(see http://en.wikipedia.org/wiki/Strict_Transport_Security)."
        )
    }

@boolean_check("STRICT_TRANSPORT_SECURITY_NO_SUBDOMAINS")
def check_sts_include_subdomains():
    return bool(conf.SECURE_HSTS_INCLUDE_SUBDOMAINS)

check_sts_include_subdomains.messages = {
    "STRICT_TRANSPORT_SECURITY_NO_SUBDOMAINS": (
        "You have not set the SECURE_HSTS_INCLUDE_SUBDOMAINS setting to True. "
        "Without this, your site is potentially vulnerable to attack "
        "via an insecure connection to a subdomain."
        )
    }

@boolean_check("FRAME_DENY_NOT_ENABLED")
def check_frame_deny():
    return conf.SECURE_FRAME_DENY

check_frame_deny.messages = {
    "FRAME_DENY_NOT_ENABLED": (
        "Your SECURE_FRAME_DENY setting is not set to True, "
        "so your pages will not be served with an "
        "'x-frame-options: DENY' header. "
        "Unless there is a good reason for your site to be served in a frame, "
        "you should consider enabling this header "
        "to help prevent clickjacking attacks."
        )
    }

@boolean_check("CONTENT_TYPE_NOSNIFF_NOT_ENABLED")
def check_content_type_nosniff():
    return conf.SECURE_CONTENT_TYPE_NOSNIFF

check_content_type_nosniff.messages = {
    "CONTENT_TYPE_NOSNIFF_NOT_ENABLED": (
        "Your SECURE_CONTENT_TYPE_NOSNIFF setting is not set to True, "
        "so your pages will not be served with an "
        "'x-content-type-options: nosniff' header. "
        "You should consider enabling this header to prevent the "
        "browser from identifying content types incorrectly."
        )
    }

@boolean_check("BROWSER_XSS_FILTER_NOT_ENABLED")
def check_xss_filter():
    return conf.SECURE_BROWSER_XSS_FILTER

check_xss_filter.messages = {
    "BROWSER_XSS_FILTER_NOT_ENABLED": (
        "Your SECURE_BROWSER_XSS_FILTER setting is not set to True, "
        "so your pages will not be served with an "
        "'x-xss-protection: 1; mode=block' header. "
        "You should consider enabling this header to activate the "
        "browser's XSS filtering and help prevent XSS attacks."
        )
    }

@boolean_check("SSL_REDIRECT_NOT_ENABLED")
def check_ssl_redirect():
    return conf.SECURE_SSL_REDIRECT

check_ssl_redirect.messages = {
    "SSL_REDIRECT_NOT_ENABLED": (
        "Your SECURE_SSL_REDIRECT setting is not set to True. "
        "Unless your site should be available over both SSL and non-SSL "
        "connections, you may want to either set this setting True "
        "or configure a loadbalancer or reverse-proxy server "
        "to redirect all connections to HTTPS."
        )
    }

@boolean_check("BAD_SECRET_KEY")
def check_secret_key():
    if getattr(settings, 'SECRET_KEY', None):
        return len(set(conf.SECRET_KEY)) >= 5
    else:
        return False

check_ssl_redirect.messages = {
    "BAD_SECRET_KEY": (
        "Your SECRET_KEY is either an empty string, non-existent, or has not "
        "enough characters. Please generate a long and random SECRET_KEY, "
        "otherwise many of Django's security-critical features will be "
        "vulnerable to attack."
        )
    }
<|endoftext|>


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

# whiteboard recorder

# records the stream of data coming from a whiteboard system, timestamping the data as it goes

import Axon

from Axon.Component import component
from Axon.Ipc import WaitComplete, producerFinished, shutdownMicroprocess
from Kamaelia.Util.Console import ConsoleReader, ConsoleEchoer
from Kamaelia.Util.Graphline import Graphline
from Kamaelia.Util.PipelineComponent import pipeline
from Kamaelia.Visualisation.PhysicsGraph.chunks_to_lines import chunks_to_lines
from Kamaelia.Visualisation.PhysicsGraph.lines_to_tokenlists import lines_to_tokenlists as text_to_tokenlists

from Whiteboard.Tokenisation import tokenlists_to_lines, lines_to_tokenlists

import sys

from Whiteboard.Entuple import Entuple

class Timestamp(component):
    def shutdown(self):
        while self.dataReady("control"):
            msg = self.recv("control")
            self.send(msg,"signal")
            if isinstance(msg, (producerFinished, shutdownMicroprocess)):
                return True
        return False

    def main(self):
        import time
        start=time.time()

        while not self.shutdown():
            while self.dataReady("inbox"):
                data = self.recv("inbox")
                msg = str(time.time()-start) + " " + data
                self.send( msg, "outbox" )
            self.pause()
            yield 1

class DeTimestamp(component):
    Outboxes = { "outbox" : "Detimestamped string data",
                 "signal" : "Shutdown signalling",
                 "next"   : "Requests for more timestamped data (number of items needed)",
               }

    def shutdown(self):
        while self.dataReady("control"):
            msg = self.recv("control")
            if isinstance(msg, (producerFinished, shutdownMicroprocess)):
                return msg
        return False

    def main(self):
        import time
        start=None
        waiting = []
        shuttingdown=False
        BUFFERSIZE=10

        self.send(BUFFERSIZE, "next")

        while not shuttingdown or waiting or self.dataReady("inbox"):
            shuttingdown = shuttingdown or self.shutdown()

            if self.dataReady("inbox"):
                msg = self.recv("inbox")
                when, data = msg.split(" ",1)
                if start==None:
                    start=time.time()
                when = start+ float(when)
                waiting.append( (when,data) )

            sentcount=0
            while waiting and waiting[0][0] <= time.time():
                when, data = waiting.pop(0)
                self.send(data,"outbox")
                sentcount+=1
            if sentcount:
                self.send(sentcount, "next")

            if not waiting and not shuttingdown and not self.dataReady("inbox"):
                self.pause()
            yield 1
        self.send(shuttingdown,"signal")

# class IntersperseNewlines(component):
#     def shutdown(self):
#         while self.dataReady("control"):
#             msg = self.recv("control")
#             self.send(msg,"signal")
#             if isinstance(msg, (producerFinished, shutdownMicroprocess)):
#                 return True
#         return False
#
#     def main(self):
#         while not self.shutdown():
#             while self.dataReady("inbox"):
#                 data = self.recv("inbox")
#                 self.send( data, "outbox" )
#                 self.send("\n", "outbox" )
#             self.pause()
#             yield 1
#

if __name__=="__main__":

    from Kamaelia.Internet.TCPClient import TCPClient
    from Kamaelia.File.Reading import PromptedFileReader
    from Kamaelia.File.Writing import SimpleFileWriter
    from Whiteboard.SingleShot import OneShot

    try:
        if "--help" in sys.argv:
            sys.stderr.write("Usage:\n    ./WhiteboardClerk play|record filename host port\n\n")
            sys.exit(0)
        mode = sys.argv[1].lower().strip()

        assert(mode=="play" or mode=="record")
        filename = sys.argv[2]
        rhost = sys.argv[3]
        rport = int(sys.argv[4])
    except:
        sys.stderr.write("Usage:\n    ./WhiteboardClerk play|record filename host port\n\n")
        sys.exit(1)

    if mode == "record":
        print "Recording..."
        pipeline(
            OneShot(msg=[["GETIMG"]]),
            tokenlists_to_lines(),
            TCPClient(host=rhost, port=rport),
            chunks_to_lines(),
            Timestamp(),
            IntersperseNewlines(),
            SimpleFileWriter(filename),
        ).run()

    elif mode == "play":
        print "Playing..."
        pipeline(
            Graphline(
                FILEREADER  = PromptedFileReader(filename, "lines"),
                DETIMESTAMP = DeTimestamp(),
                linkages = {
                    # data from file gets detimestamped and sent on
                    ("FILEREADER",  "outbox") : ("DETIMESTAMP", "inbox"),
                    ("DETIMESTAMP", "outbox") : ("",            "outbox"),

                    # detimestamper asks for more data to be read from file
                    ("DETIMESTAMP", "next")   : ("FILEREADER",  "inbox"),

                    # shutdown wiring
                    ("",            "control") : ("FILEREADER",  "control"),
                    ("FILEREADER",  "signal")  : ("DETIMESTAMP", "control"),
                    ("DETIMESTAMP", "signal")  : ("",            "signal"),
                }
            ),
            TCPClient(host=rhost, port=rport),
        ).run()
        <commit_msg>Bugfix: restored code that was needed for recording.<commit_after>#!/usr/bin/env python

# whiteboard recorder

# records the stream of data coming from a whiteboard system, timestamping the data as it goes

import Axon

from Axon.Component import component
from Axon.Ipc import WaitComplete, producerFinished, shutdownMicroprocess
from Kamaelia.Util.Console import ConsoleReader, ConsoleEchoer
from Kamaelia.Util.Graphline import Graphline
from Kamaelia.Util.PipelineComponent import pipeline
from Kamaelia.Visualisation.PhysicsGraph.chunks_to_lines import chunks_to_lines
from Kamaelia.Visualisation.PhysicsGraph.lines_to_tokenlists import lines_to_tokenlists as text_to_tokenlists

from Whiteboard.Tokenisation import tokenlists_to_lines, lines_to_tokenlists

import sys

from Whiteboard.Entuple import Entuple

class Timestamp(component):
    def shutdown(self):
        while self.dataReady("control"):
            msg = self.recv("control")
            self.send(msg,"signal")
            if isinstance(msg, (producerFinished, shutdownMicroprocess)):
                return True
        return False

    def main(self):
        import time
        start=time.time()

        while not self.shutdown():
            while self.dataReady("inbox"):
                data = self.recv("inbox")
                msg = str(time.time()-start) + " " + data
                self.send( msg, "outbox" )
            self.pause()
            yield 1

class DeTimestamp(component):
    Outboxes = { "outbox" : "Detimestamped string data",
                 "signal" : "Shutdown signalling",
                 "next"   : "Requests for more timestamped data (number of items needed)",
               }

    def shutdown(self):
        while self.dataReady("control"):
            msg = self.recv("control")
            if isinstance(msg, (producerFinished, shutdownMicroprocess)):
                return msg
        return False

    def main(self):
        import time
        start=None
        waiting = []
        shuttingdown=False
        BUFFERSIZE=10

        self.send(BUFFERSIZE, "next")

        while not shuttingdown or waiting or self.dataReady("inbox"):
            shuttingdown = shuttingdown or self.shutdown()

            if self.dataReady("inbox"):
                msg = self.recv("inbox")
                when, data = msg.split(" ",1)
                if start==None:
                    start=time.time()
                when = start+ float(when)
                waiting.append( (when,data) )

            sentcount=0
            while waiting and waiting[0][0] <= time.time():
                when, data = waiting.pop(0)
                self.send(data,"outbox")
                sentcount+=1
            if sentcount:
                self.send(sentcount, "next")

            if not waiting and not shuttingdown and not self.dataReady("inbox"):
                self.pause()
            yield 1
        self.send(shuttingdown,"signal")

class IntersperseNewlines(component):
    def shutdown(self):
        while self.dataReady("control"):
            msg = self.recv("control")
            self.send(msg,"signal")
            if isinstance(msg, (producerFinished, shutdownMicroprocess)):
                return True
        return False

    def main(self):
        while not self.shutdown():
            while self.dataReady("inbox"):
                data = self.recv("inbox")
                self.send( data, "outbox" )
                self.send("\n", "outbox" )
            self.pause()
            yield 1

if __name__=="__main__":

    from Kamaelia.Internet.TCPClient import TCPClient
    from Kamaelia.File.Reading import PromptedFileReader
    from Kamaelia.File.Writing import SimpleFileWriter
    from Whiteboard.SingleShot import OneShot

    try:
        if "--help" in sys.argv:
            sys.stderr.write("Usage:\n    ./WhiteboardClerk play|record filename host port\n\n")
            sys.exit(0)
        mode = sys.argv[1].lower().strip()

        assert(mode=="play" or mode=="record")
        filename = sys.argv[2]
        rhost = sys.argv[3]
        rport = int(sys.argv[4])
    except:
        sys.stderr.write("Usage:\n    ./WhiteboardClerk play|record filename host port\n\n")
        sys.exit(1)

    if mode == "record":
        print "Recording..."
        pipeline(
            OneShot(msg=[["GETIMG"]]),
            tokenlists_to_lines(),
            TCPClient(host=rhost, port=rport),
            chunks_to_lines(),
            Timestamp(),
            IntersperseNewlines(),
            SimpleFileWriter(filename),
        ).run()

    elif mode == "play":
        print "Playing..."
        pipeline(
            Graphline(
                FILEREADER  = PromptedFileReader(filename, "lines"),
                DETIMESTAMP = DeTimestamp(),
                linkages = {
                    # data from file gets detimestamped and sent on
                    ("FILEREADER",  "outbox") : ("DETIMESTAMP", "inbox"),
                    ("DETIMESTAMP", "outbox") : ("",            "outbox"),

                    # detimestamper asks for more data to be read from file
                    ("DETIMESTAMP", "next")   : ("FILEREADER",  "inbox"),

                    # shutdown wiring
                    ("",            "control") : ("FILEREADER",  "control"),
                    ("FILEREADER",  "signal")  : ("DETIMESTAMP", "control"),
                    ("DETIMESTAMP", "signal")  : ("",            "signal"),
                }
            ),
            TCPClient(host=rhost, port=rport),
        ).run()
        <|endoftext|>


<commit_before>"""Monitors tests manifest files"""

import argparse
import json
import logging
import os
import sys

from manifestparser import TestManifest

# Setting up logging
log = logging.getLogger('manifestmonitor')
log.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
log.addHandler(handler)

CONFIG_CHOICES = {'os': ['linux', 'bsd', 'win', 'mac', 'unix'],
                  'bits': ['32', '64'],
                  'processor': ['x86', 'x86_64', 'ppc'],
                  'toolkit': ['gonk', 'android']}

def get_ini_files(src_dir_path):
    """Give the list of all the .ini files in a given directory"""
    ini_files = set()
    for root, dirs, files in os.walk(src_dir_path):
        if root[len(src_dir_path)+1:].startswith('obj'):
            continue
        ini_files |= {os.path.join(root, f) for f in files if f.endswith('.ini')}
    return ini_files

def manifests_state(manifest_paths, options):
    """Parses a list of given files as manifests, skipping those that are unparsable.
    Outputs a summary that gives information about the tests activated/skipped."""
    total_skipped = 0
    total_tests = 0
    manifests = dict()
    for manifest_path in manifest_paths:
        try:
            test_manifest = TestManifest([manifest_path])
            if test_manifest.tests:
                active_tests_paths = [t['path'] for t in test_manifest.active_tests(options=options)]
                skipped_tests = [t for t in test_manifest.tests if t['path'] not in active_tests_paths]

                total_tests += len(test_manifest.tests)
                if skipped_tests:
                    total_skipped += len(skipped_tests)
                    manifests[manifest_path] = dict(total_tests=len(test_manifest.tests),
                                                    skipped_tests=len(skipped_tests),
                                                    skipped_tests_paths=skipped_tests)
        except Exception as e:
            log.debug('Skipping {}. Exception: {}.'.format(manifest_path, e))

    result = dict(options=options, manifests=manifests, total_skipped=total_skipped, total_tests=total_tests)
    return result

def display_summary(summary, verbose=False):
    total_skipped = summary['total_skipped']
    total_tests = summary['total_tests']
    manifests = summary['manifests']

    log.info('')
    log.info('# Number of manifests: {}.'.format(len(manifests)))
    log.info('# Tests skipped : {}.'.format(total_skipped))

    for manifest, manifest_data in manifests.iteritems():
        total_tests, skipped_tests = manifest_data['total_tests'], manifest_data['skipped_tests']
        if verbose:
            log.info('. Manifest "{}": {} total tests. {} active tests.'.format(manifest, total_tests, skipped_tests))
            continue

        if skipped_tests > 0:
            log.info('. Manifest "{}": {}/{} tests skipped'.format(manifest, skipped_tests, total_tests))

def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('src_path', type=str,
                         help="path to the source directory")
    parser.add_argument('dest_path', type=str,
                         help="path where the JSON result will be stored")
    for key in CONFIG_CHOICES:
        parser.add_argument('--%s' % key, dest=key,
                           nargs='?',
                           help="display choices for %s" % key)
    args = parser.parse_args()
    src_path = args.src_path
    dest_path = args.dest_path
    options = {option: value for option, value in args.__dict__.iteritems()
               if value is not None and not option in {'src_path', 'dest_path'}}

    log.info("Options = {}".format(options))

    ini_files = get_ini_files(src_path)
    summary = manifests_state(ini_files, options)
    display_summary(summary)
    # Saving the result to the output file
    with open(dest_path, 'w+') as output_file:
        output_file.write(json.dumps(summary, indent=4))
    log.info("JSON result saved to '{}' !".format(dest_path))

if __name__ == '__main__':
    main()<commit_msg>Minor fixes<commit_after>"""Monitors tests manifest files"""

import argparse
import json
import logging
import os
import sys

from manifestparser import TestManifest

# Setting up logging
log = logging.getLogger('manifestmonitor')
log.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
log.addHandler(handler)

CONFIG_CHOICES = {'os': ['linux', 'bsd', 'win', 'mac', 'unix'],
                  'bits': ['32', '64'],
                  'processor': ['x86', 'x86_64', 'ppc'],
                  'toolkit': ['gonk', 'android', 'gtk2']}

def get_ini_files(src_dir_path):
    """Give the list of all the .ini files in a given directory"""
    ini_files = set()
    for root, dirs, files in os.walk(src_dir_path):
        if root[len(src_dir_path)+1:].startswith('obj'):
            continue
        ini_files |= {os.path.join(root, f) for f in files if f.endswith('.ini')}
    return ini_files

def manifests_state(manifest_paths, options):
    """Parses a list of given files as manifests, skipping those that are unparsable.
    Outputs a summary that gives information about the tests activated/skipped."""
    total_skipped = 0
    total_tests = 0
    manifests = dict()
    for manifest_path in manifest_paths:
        try:
            test_manifest = TestManifest([manifest_path])
            if test_manifest.tests:
                active_tests = test_manifest.active_tests(exists=False, **options)
                active_tests_paths = [t['path'] for t in active_tests]
                skipped_tests = [t for t in test_manifest.tests if t['path'] not in active_tests_paths]

                total_tests += len(test_manifest.tests)
                if skipped_tests:
                    log.info('')
                    log.info(active_tests_paths)
                    log.info('')
                    log.info([t['path'] for t in test_manifest.tests])

                    total_skipped += len(skipped_tests)
                    manifests[manifest_path] = dict(total_tests=len(test_manifest.tests),
                                                    skipped_tests=len(skipped_tests),
                                                    skipped_tests_paths=skipped_tests)
        except Exception as e:
            log.info('Skipping {}. Exception: {}.'.format(manifest_path, e))

    result = dict(options=options, manifests=manifests, total_skipped=total_skipped, total_tests=total_tests)
    return result

def display_summary(summary, verbose=False):
    total_skipped = summary['total_skipped']
    total_tests = summary['total_tests']
    manifests = summary['manifests']

    log.info('')
    log.info('# Number of manifests: {}.'.format(len(manifests)))
    log.info('# Tests skipped : {}.'.format(total_skipped))

    for manifest, manifest_data in manifests.iteritems():
        total_tests, skipped_tests = manifest_data['total_tests'], manifest_data['skipped_tests']
        if verbose:
            log.info('. Manifest "{}": {} total tests. {} active tests.'.format(manifest, total_tests, skipped_tests))
            continue

        if skipped_tests > 0:
            log.info('. Manifest "{}": {}/{} tests skipped'.format(manifest, skipped_tests, total_tests))

MANIFESTS_REL_PATHS = ['reftest/reftest.ini', 'marionette/tests/testing/marionette/client/marionette/tests/unit-tests.ini']
def generate_manifest_paths(src_dir_path):
    return [os.path.join(src_dir_path, rel_path) for rel_path in MANIFESTS_REL_PATHS]

def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('tests_path', type=str,
                         help="path to the tests directory")
    parser.add_argument('dest_path', type=str,
                         help="path where the JSON result will be stored")
    for key in CONFIG_CHOICES:
        parser.add_argument('--%s' % key, dest=key,
                           nargs='?',
                           help="display choices for %s" % key)
    args = parser.parse_args()
    tests_path = args.tests_path
    dest_path = args.dest_path
    options = {option: value for option, value in args.__dict__.iteritems()
               if value is not None and not option in {'tests_path', 'dest_path'}}

    log.info("Options = {}.".format(options))

    manifest_paths = generate_manifest_paths(tests_path)
    summary = manifests_state(manifest_paths, options)
    display_summary(summary)
    # Saving the result to the output file
    with open(dest_path, 'w+') as output_file:
        output_file.write(json.dumps(summary, indent=4))
    log.info("JSON result saved to '{}' !".format(dest_path))

if __name__ == '__main__':
    main()<|endoftext|>


<commit_before># vim: set expandtab sw=4 ts=4:
#
# Unit tests for Trend class
#
# Copyright (C) 2014 Dieter Adriaenssens <ruleant@users.sourceforge.net>
#
# This file is part of buildtime-trend
# <https://github.com/ruleant/buildtime-trend/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from buildtimetrend.build import Build
from lxml import etree
import unittest

TEST_SAMPLE_FILE = 'test/testsample_timestamps.csv'

class TestBuild(unittest.TestCase):
    def setUp(self):
        self.build = Build()

    def test_novalue(self):
        # number of stages should be zero
        self.assertEquals(0, len(self.build.stages.stages))
        self.assertEquals(0, len(self.build.properties))

        # dict should be empty
        self.assertDictEqual({'stages' : {}}, self.build.to_dict())

        # xml shouldn't contain items
        self.assertEquals(
            '<build><stages/></build>', etree.tostring(self.build.to_xml()))
        self.assertEquals(
            '<build>\n'
            '  <stages/>\n'
            '</build>\n', self.build.to_xml_string())

    def test_nofile(self):
        # number of stages should be zero when file doesn't exist
        self.build = Build('nofile.csv')
        self.assertEquals(0, len(self.build.stages.stages))

        self.build = Build('')
        self.assertEquals(0, len(self.build.stages.stages))

    def test_add_property(self):
        self.build.add_property('test1', 2)
        self.assertEquals(1, len(self.build.properties))
        self.assertDictEqual({'test1': 2}, self.build.properties)

        self.build.add_property('test2', 3)
        self.assertEquals(2, len(self.build.properties))
        self.assertDictEqual({'test1': 2, 'test2': 3}, self.build.properties)

        self.build.add_property('test2', 4)
        self.assertEquals(2, len(self.build.properties))
        self.assertDictEqual({'test1': 2, 'test2': 4}, self.build.properties)

    def test_to_dict(self):
        # read and parse sample file
        self.build = Build(TEST_SAMPLE_FILE)

        # test dict
        self.assertDictEqual(
            {'stages': {'stage1': {'duration': 2, 'name': 'stage1'},
            'stage2': {'duration': 5, 'name': 'stage2'},
            'stage3': {'duration': 10, 'name': 'stage3'}}},
            self.build.to_dict())

        # add properties
        self.build.add_property('test1', 2)
        self.build.add_property('test2', 3)
        # test dict
        self.assertDictEqual(
            {'test1': 2, 'test2': 3,
            'stages': {'stage1': {'duration': 2, 'name': 'stage1'},
            'stage2': {'duration': 5, 'name': 'stage2'},
            'stage3': {'duration': 10, 'name': 'stage3'}}},
            self.build.to_dict())

    def test_stages_to_dict(self):
        # read and parse sample file
        self.build = Build(TEST_SAMPLE_FILE)

        # test dict
        self.assertDictEqual(
            {'stage1': {'duration': 2, 'name': 'stage1'},
            'stage2': {'duration': 5, 'name': 'stage2'},
            'stage3': {'duration': 10, 'name': 'stage3'}},
            self.build.stages_to_dict())

        # add properties
        self.build.add_property('test1', 2)
        self.build.add_property('test2', 3)
        # test dict
        self.assertDictEqual(
            {'stage1': {'duration': 2, 'name': 'stage1',
            'build': {'test1': 2, 'test2': 3}},
            'stage2': {'duration': 5, 'name': 'stage2',
            'build': {'test1': 2, 'test2': 3}},
            'stage3': {'duration': 10, 'name': 'stage3',
            'build': {'test1': 2, 'test2': 3}},
            },
            self.build.stages_to_dict())

    def test_to_xml(self):
        # read and parse sample file
        self.build = Build(TEST_SAMPLE_FILE)

        # test xml output
        self.assertEquals(
            '<build><stages><stage duration="2" name="stage1"/>'
            '<stage duration="5" name="stage2"/>'
            '<stage duration="10" name="stage3"/></stages></build>',
            etree.tostring(self.build.to_xml()))

        # add properties
        self.build.add_property('test1', 2)
        self.build.add_property('test2', 3)
        # test xml output
        self.assertEquals(
            '<build test1="2" test2="3">'
            '<stages><stage duration="2" name="stage1"/>'
            '<stage duration="5" name="stage2"/>'
            '<stage duration="10" name="stage3"/></stages></build>',
            etree.tostring(self.build.to_xml()))

    def test_to_xml_string(self):
        # read and parse sample file
        self.build = Build(TEST_SAMPLE_FILE)

        # test xml string output
        self.assertEquals(
            '<build>\n'
            '  <stages>\n'
            '    <stage duration="2" name="stage1"/>\n'
            '    <stage duration="5" name="stage2"/>\n'
            '    <stage duration="10" name="stage3"/>\n'
            '  </stages>\n'
            '</build>\n',
            self.build.to_xml_string())

        # add properties
        self.build.add_property('test1', 2)
        self.build.add_property('test2', 3)
        # test xml string output
        self.assertEquals(
            '<build test1="2" test2="3">\n'
            '  <stages>\n'
            '    <stage duration="2" name="stage1"/>\n'
            '    <stage duration="5" name="stage2"/>\n'
            '    <stage duration="10" name="stage3"/>\n'
            '  </stages>\n'
            '</build>\n',
            self.build.to_xml_string())
<commit_msg>rename property names<commit_after># vim: set expandtab sw=4 ts=4:
#
# Unit tests for Trend class
#
# Copyright (C) 2014 Dieter Adriaenssens <ruleant@users.sourceforge.net>
#
# This file is part of buildtime-trend
# <https://github.com/ruleant/buildtime-trend/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from buildtimetrend.build import Build
from lxml import etree
import unittest

TEST_SAMPLE_FILE = 'test/testsample_timestamps.csv'

class TestBuild(unittest.TestCase):
    def setUp(self):
        self.build = Build()

    def test_novalue(self):
        # number of stages should be zero
        self.assertEquals(0, len(self.build.stages.stages))
        self.assertEquals(0, len(self.build.properties))

        # dict should be empty
        self.assertDictEqual({'stages' : {}}, self.build.to_dict())

        # xml shouldn't contain items
        self.assertEquals(
            '<build><stages/></build>', etree.tostring(self.build.to_xml()))
        self.assertEquals(
            '<build>\n'
            '  <stages/>\n'
            '</build>\n', self.build.to_xml_string())

    def test_nofile(self):
        # number of stages should be zero when file doesn't exist
        self.build = Build('nofile.csv')
        self.assertEquals(0, len(self.build.stages.stages))

        self.build = Build('')
        self.assertEquals(0, len(self.build.stages.stages))

    def test_add_property(self):
        self.build.add_property('property1', 2)
        self.assertEquals(1, len(self.build.properties))
        self.assertDictEqual({'property1': 2}, self.build.properties)

        self.build.add_property('property2', 3)
        self.assertEquals(2, len(self.build.properties))
        self.assertDictEqual({'property1': 2, 'property2': 3}, self.build.properties)

        self.build.add_property('property2', 4)
        self.assertEquals(2, len(self.build.properties))
        self.assertDictEqual({'property1': 2, 'property2': 4}, self.build.properties)

    def test_to_dict(self):
        # read and parse sample file
        self.build = Build(TEST_SAMPLE_FILE)

        # test dict
        self.assertDictEqual(
            {'stages': {'stage1': {'duration': 2, 'name': 'stage1'},
            'stage2': {'duration': 5, 'name': 'stage2'},
            'stage3': {'duration': 10, 'name': 'stage3'}}},
            self.build.to_dict())

        # add properties
        self.build.add_property('property1', 2)
        self.build.add_property('property2', 3)
        # test dict
        self.assertDictEqual(
            {'property1': 2, 'property2': 3,
            'stages': {'stage1': {'duration': 2, 'name': 'stage1'},
            'stage2': {'duration': 5, 'name': 'stage2'},
            'stage3': {'duration': 10, 'name': 'stage3'}}},
            self.build.to_dict())

    def test_stages_to_dict(self):
        # read and parse sample file
        self.build = Build(TEST_SAMPLE_FILE)

        # test dict
        self.assertDictEqual(
            {'stage1': {'duration': 2, 'name': 'stage1'},
            'stage2': {'duration': 5, 'name': 'stage2'},
            'stage3': {'duration': 10, 'name': 'stage3'}},
            self.build.stages_to_dict())

        # add properties
        self.build.add_property('property1', 2)
        self.build.add_property('property2', 3)
        # test dict
        self.assertDictEqual(
            {'stage1': {'duration': 2, 'name': 'stage1',
            'build': {'property1': 2, 'property2': 3}},
            'stage2': {'duration': 5, 'name': 'stage2',
            'build': {'property1': 2, 'property2': 3}},
            'stage3': {'duration': 10, 'name': 'stage3',
            'build': {'property1': 2, 'property2': 3}},
            },
            self.build.stages_to_dict())

    def test_to_xml(self):
        # read and parse sample file
        self.build = Build(TEST_SAMPLE_FILE)

        # test xml output
        self.assertEquals(
            '<build><stages><stage duration="2" name="stage1"/>'
            '<stage duration="5" name="stage2"/>'
            '<stage duration="10" name="stage3"/></stages></build>',
            etree.tostring(self.build.to_xml()))

        # add properties
        self.build.add_property('property1', 2)
        self.build.add_property('property2', 3)
        # test xml output
        self.assertEquals(
            '<build property1="2" property2="3">'
            '<stages><stage duration="2" name="stage1"/>'
            '<stage duration="5" name="stage2"/>'
            '<stage duration="10" name="stage3"/></stages></build>',
            etree.tostring(self.build.to_xml()))

    def test_to_xml_string(self):
        # read and parse sample file
        self.build = Build(TEST_SAMPLE_FILE)

        # test xml string output
        self.assertEquals(
            '<build>\n'
            '  <stages>\n'
            '    <stage duration="2" name="stage1"/>\n'
            '    <stage duration="5" name="stage2"/>\n'
            '    <stage duration="10" name="stage3"/>\n'
            '  </stages>\n'
            '</build>\n',
            self.build.to_xml_string())

        # add properties
        self.build.add_property('property1', 2)
        self.build.add_property('property2', 3)
        # test xml string output
        self.assertEquals(
            '<build property1="2" property2="3">\n'
            '  <stages>\n'
            '    <stage duration="2" name="stage1"/>\n'
            '    <stage duration="5" name="stage2"/>\n'
            '    <stage duration="10" name="stage3"/>\n'
            '  </stages>\n'
            '</build>\n',
            self.build.to_xml_string())
<|endoftext|>


<commit_before>import os

def is_c_compiler_flag(line):
    return (line.startswith('C_FLAGS')
        or line.startswith('C_DEFINES')
        or line.startswith('C_INCLUDES'))

flags = ['-x', 'c']
for (dirname, __, files) in os.walk(os.path.dirname(os.path.abspath(__file__))):
  for i in files:
    if i == 'flags.make':
      lines = open(os.path.join(dirname, i)).readlines()
      lines = filter(lambda x : is_c_compiler_flag(x), lines)
      f = [a.split()[2:] for a in lines]
      flags += [i for j in f for i in j]

print flags

def FlagsForFile( filename, **kwargs ):
  return {
    'flags': flags,
    'do_cache': True
  }
<commit_msg>devenv: Make ycm_extra_conf py3-compatible<commit_after>from __future__ import print_function
import os

def is_c_compiler_flag(line):
    return (line.startswith('C_FLAGS')
        or line.startswith('C_DEFINES')
        or line.startswith('C_INCLUDES'))

flags = ['-x', 'c']
for (dirname, __, files) in os.walk(os.path.dirname(os.path.abspath(__file__))):
  for i in files:
    if i == 'flags.make':
      lines = open(os.path.join(dirname, i)).readlines()
      lines = filter(lambda x : is_c_compiler_flag(x), lines)
      f = [a.split()[2:] for a in lines]
      flags += [i for j in f for i in j]

print(flags)

def FlagsForFile( filename, **kwargs ):
  return {
    'flags': flags,
    'do_cache': True
  }
<|endoftext|>