<commit_before>import uuid
import logging
from os import path
from datetime import datetime

from indra.statements import Agent, Statement
from indra.assemblers.html import HtmlAssembler
from indra.util.statement_presentation import group_and_sort_statements, \
    make_string_from_sort_key

from bioagents.settings import IMAGE_DIR, TIMESTAMP_PICS
from kqml.cl_json import CLJsonConverter

logging.basicConfig(format='%(levelname)s: %(name)s - %(message)s',
                    level=logging.INFO)
logger = logging.getLogger('Bioagents')

from indra.assemblers.english import EnglishAssembler
from kqml import KQMLModule, KQMLPerformative, KQMLList, KQMLString

class BioagentException(Exception):
    pass

class Bioagent(KQMLModule):
    """Abstract class for bioagents."""
    name = "Generic Bioagent (Should probably be overwritten)"
    tasks = []
    converter = CLJsonConverter(token_bools=True)

    def __init__(self, **kwargs):
        super(Bioagent, self).__init__(name=self.name, **kwargs)
        self.my_log_file = self._add_log_file()
        for task in self.tasks:
            self.subscribe_request(task)

        self.ready()
        self.start()
        logger.info("%s has started and is ready." % self.name)
        return

    @classmethod
    def _add_log_file(cls):
        log_file_name = '%s.log' % cls.name
        handler = logging.FileHandler(log_file_name)
        handler.setLevel(logging.DEBUG)
        formatter = logging.Formatter('%(asctime)s - %(levelname)s: '
                                      '%(name)s - %(message)s')
        handler.setFormatter(formatter)
        logger.addHandler(handler)
        return log_file_name

    @classmethod
    def get_agent(cls, cl_agent):
        """Get an agent from the kqml cl-json representation (KQMLList)."""
        agent_json = cls.converter.cl_to_json(cl_agent)
        return Agent._from_json(agent_json)

    @classmethod
    def get_statement(cls, cl_statement):
        """Get an INDRA Statement from cl-json"""
        stmt_json = cls.converter.cl_to_json(cl_statement)
        return Statement._from_json(stmt_json)

    @classmethod
    def get_statements(cls, cl_statements):
        """Get a list of INDRA Statements from cl-json"""
        stmts = [Statement._from_json(cls.converter.cl_to_json(cl_statement))
                 for cl_statement in cl_statements]
        return stmts

    @classmethod
    def make_cljson(cls, entity):
        """Convert an Agent or a Statement into cljson.

        `entity` is expected to have a method `to_json` which returns valid
        json.
        """
        entity_json = entity.to_json()
        return cls.converter.cl_from_json(entity_json)

    @classmethod
    def make_cljson_from_list(cls, entities):
        """Convert a list of Agents or Statements into cljson."""
        entities_json = [entity.to_json() for entity in entities]
        return cls.converter.cl_from_json(entities_json)

    def receive_tell(self, msg, content):
        tell_content = content[0].to_string().upper()
        if tell_content == 'START-CONVERSATION':
            logger.info('%s resetting' % self.name)

    def receive_request(self, msg, content):
        """Handle request messages and respond.

        If a "request" message is received, decode the task and the content
        and call the appropriate function to prepare the response. A reply
        message is then sent back.
        """
        try:
            content = msg.get('content')
            task = content.head().upper()
            logger.info("%s received request with task: %s" % (self.name, task))
        except Exception as e:
            logger.error('Could not get task string from request.')
            logger.error(e)
            reply_content = self.make_failure('INVALID_REQUEST')
            return self.reply_with_content(msg, reply_content)

        if task in self.tasks:
            reply_content = self._respond_to(task, content)
        else:
            logger.error('Could not perform task.')
            logger.error("Task %s not found in %s." %
                         (task, str(self.tasks)))
            reply_content = self.make_failure('UNKNOWN_TASK')

        return self.reply_with_content(msg, reply_content)

    def _respond_to(self, task, content):
        """Get the method to responsd to the task indicated by task."""
        resp_name = "respond_" + task.replace('-', '_').lower()
        try:
            resp = getattr(self, resp_name)
            logger.info("%s will perform task %s with method %s."
                        % (self.name, task, resp_name))
        except AttributeError:
            logger.error("Tried to execute unimplemented task.")
            logger.error("Did not find response method %s." % resp_name)
            return self.make_failure('INVALID_TASK')
        try:
            reply_content = resp(content)
            return reply_content
        except BioagentException:
            raise
        except Exception as e:
            logger.error('Could not perform response to %s' % task)
            logger.exception(e)
            return self.make_failure('INTERNAL_FAILURE')

    def reply_with_content(self, msg, reply_content):
        """A wrapper around the reply method from KQMLModule."""
        reply_msg = KQMLPerformative('reply')
        reply_msg.set('content', reply_content)
        self.reply(msg, reply_msg)
        return

    def tell(self, content):
        """Send a tell message."""
        msg = KQMLPerformative('tell')
        msg.set('content', content)
        return self.send(msg)

    def request(self, content):
        """Send a request message."""
        msg = KQMLPerformative('request')
        msg.set('content', content)
        return self.send(msg)

    def error_reply(self, msg, comment):
        if not self.testing:
            return KQMLModule.error_reply(self, msg, comment)
        else:
            return msg, comment

    def make_failure(self, reason=None, description=None):
        msg = KQMLList('FAILURE')
        if reason:
            msg.set('reason', reason)
        if description:
            msg.sets('description', description)
        return msg

    def send_null_provenance(self, stmt, for_what, reason=''):
        """Send out that no provenance could be found for a given Statement."""
        content_fmt = ('
No supporting evidence found for {statement} from '
                       '{cause}{reason}.
')
        content = KQMLList('add-provenance')
        stmt_txt = EnglishAssembler([stmt]).make_model()
        content.sets('html', content_fmt.format(statement=stmt_txt,
                                                cause=for_what, reason=reason))
        return self.tell(content)

    def send_provenance_for_stmts(self, stmt_list, for_what, limit=50,
                                  ev_counts=None):
        """Send out a provenance tell for a list of INDRA Statements.

        The message is used to provide evidence supporting a conclusion.
        """
        logger.info("Sending provenance for %d statements for \"%s\"."
                    % (len(stmt_list), for_what))
        title = "Supporting evidence from the %s for %s" \
                % (self.name, for_what)
        content_fmt = '
%s (max %s):
\n%s
'
        evidence_html = self._make_report_cols_html(stmt_list, limit=limit,
                                                    ev_counts=ev_counts,
                                                    title=title)

        content = KQMLList('add-provenance')
        content.sets('html', content_fmt % (title, limit, evidence_html))
        return self.tell(content)

    def _make_evidence_html(self, stmts, ev_counts=None,
                            title='Results from the INDRA database'):
        "Make html from a set of statements."
        ha = HtmlAssembler(stmts, db_rest_url='db.indra.bio', title=title,
                           ev_totals=ev_counts)
        return ha.make_model()

    def _stash_evidence_html(self, html):
        """Make html for a set of statements, return a link to the file.

        The if the PROVENANCE_LOCATION environment variable determines where
        the content is stored. The variable should be divided by colons, the
        first division indicating whether the file is stored locally or on s3,
        being either "file" or "s3" respectively.

        If the html will be stored locally, the next and last division should
        be a path (absolute would be best) to the location where html files
        will be stored. For example:

            file:/home/myname/projects/cwc-integ/provenance

        If the html will be stored on s3, the next division should be the
        bucket, and the last division should be a prefix to a "directory" where
        the html files will be stored. For example:

            s3:cwc-stuff:bob/provenance

        The default is:

            file:{this directory}/../../../provenance

        Which should land in the cwc-integ directory. If the directory does not
        yet exist, it will be created.
        """
        # Get the provenance location.
        from os import environ, mkdir

        loc = environ.get('PROVENANCE_LOCATION')
        if loc is None:
            this_dir = path.dirname(path.abspath(__file__))
            rel = path.join(*([this_dir] + 3*[path.pardir] + ['provenance']))
            loc = 'file:' + path.abspath(rel)
        logger.info("Using provenance location: \"%s\"" % loc)

        # Save the file.
        method = loc.split(':')[0]
        fname = '%s.html' % uuid.uuid4()
        if method == 'file':
            prov_path = loc.split(':')[1]
            if not path.exists(prov_path):
                mkdir(prov_path)
            fpath = path.join(prov_path, fname)
            with open(fpath, 'w') as f:
                f.write(html)
            link = fpath
        elif method == 's3':
            bucket = loc.split(':')[1]
            prefix = loc.split(':')[2]

            # Get a connection to s3.
            import boto3
            from botocore import UNSIGNED
            from botocore.client import Config
            s3 = boto3.client('s3', config=Config(signature_version=UNSIGNED))

            key = prefix + fname
            link = 'https://s3.amazonaws.com/%s/%s' % (bucket, key)
            s3.put_object(Bucket=bucket, Key=key, Body=html.encode('utf-8'),
                          ContentType='text/html')
        else:
            logger.error('Invalid PROVENANCE_LOCATION: "%s". HTML not saved.'
                         % loc)
            link = None
        return link

    def say(self, message):
        """Say something to the user."""
        msg = KQMLList('say')
        msg.append(KQMLString(message))
        self.request(msg)

    def _make_report_cols_html(self, stmt_list, limit=5, ev_counts=None,
                               **kwargs):
        """Make columns listing the support given by the statement list."""

        def href(ref, text):
            return '%s' % (ref, text)

        # Build the list of relevant statements and count their prevalence.
        sorted_groups = group_and_sort_statements(stmt_list,
                                                  ev_totals=ev_counts)

        # Build the html.
        lines = []
        for key, verb, stmts in sorted_groups[:limit]:
            count = key[2]
            line = '
%s %s
' % (make_string_from_sort_key(key, verb),
                                       '(%d)' % count)
            lines.append(line)

        # Build the overall html.
        list_html = '
%s
' % ('\n'.join(lines))
        html = self._make_evidence_html(stmt_list, **kwargs)
        link = self._stash_evidence_html(html)
        if link is None:
            link_html = 'I could not generate the full list.'
        elif link.startswith('http'):
            link_html = href(link, 'Here') + ' is the full list.'
        else:
            link_html = 'Here: %s is the full list.' % link

        return list_html + '\n' + link_html

def get_img_path(img_name):
    """Get a full path for the given image name.

    The name will also have a timestamp added if settings.TIMESTAMP_PICS is
    True. The timestamp, if applied, will prepend the name.
    """
    if TIMESTAMP_PICS:
        date_str = datetime.now().strftime('%Y%m%d%H%M%S')
        img_name = '%s_%s' % (date_str, img_name)
    return path.join(IMAGE_DIR, img_name)
<commit_msg>Automatically handle lists of entities.<commit_after>import uuid
import logging
from os import path
from datetime import datetime

from indra.statements import Agent, Statement
from indra.assemblers.html import HtmlAssembler
from indra.util.statement_presentation import group_and_sort_statements, \
    make_string_from_sort_key

from bioagents.settings import IMAGE_DIR, TIMESTAMP_PICS
from kqml.cl_json import CLJsonConverter

logging.basicConfig(format='%(levelname)s: %(name)s - %(message)s',
                    level=logging.INFO)
logger = logging.getLogger('Bioagents')

from indra.assemblers.english import EnglishAssembler
from kqml import KQMLModule, KQMLPerformative, KQMLList, KQMLString

class BioagentException(Exception):
    pass

class Bioagent(KQMLModule):
    """Abstract class for bioagents."""
    name = "Generic Bioagent (Should probably be overwritten)"
    tasks = []
    converter = CLJsonConverter(token_bools=True)

    def __init__(self, **kwargs):
        super(Bioagent, self).__init__(name=self.name, **kwargs)
        self.my_log_file = self._add_log_file()
        for task in self.tasks:
            self.subscribe_request(task)

        self.ready()
        self.start()
        logger.info("%s has started and is ready." % self.name)
        return

    @classmethod
    def _add_log_file(cls):
        log_file_name = '%s.log' % cls.name
        handler = logging.FileHandler(log_file_name)
        handler.setLevel(logging.DEBUG)
        formatter = logging.Formatter('%(asctime)s - %(levelname)s: '
                                      '%(name)s - %(message)s')
        handler.setFormatter(formatter)
        logger.addHandler(handler)
        return log_file_name

    @classmethod
    def get_agent(cls, cl_agent):
        """Get an agent from the kqml cl-json representation (KQMLList)."""
        agent_json = cls.converter.cl_to_json(cl_agent)
        return Agent._from_json(agent_json)

    @classmethod
    def get_statement(cls, cl_statement):
        """Get an INDRA Statement from cl-json"""
        stmt_json = cls.converter.cl_to_json(cl_statement)
        return Statement._from_json(stmt_json)

    @classmethod
    def get_statements(cls, cl_statements):
        """Get a list of INDRA Statements from cl-json"""
        stmts = [Statement._from_json(cls.converter.cl_to_json(cl_statement))
                 for cl_statement in cl_statements]
        return stmts

    @classmethod
    def make_cljson(cls, entity):
        """Convert an Agent or a Statement into cljson.

        `entity` is expected to have a method `to_json` which returns valid
        json.
        """
        if isinstance(entity, list):
            entity_json = [e.to_json() for e in entity]
        else:
            entity_json = entity.to_json()
        return cls.converter.cl_from_json(entity_json)

    @classmethod
    def make_cljson_from_list(cls, entities):
        """Convert a list of Agents or Statements into cljson."""
        entities_json = [entity.to_json() for entity in entities]
        return cls.converter.cl_from_json(entities_json)

    def receive_tell(self, msg, content):
        tell_content = content[0].to_string().upper()
        if tell_content == 'START-CONVERSATION':
            logger.info('%s resetting' % self.name)

    def receive_request(self, msg, content):
        """Handle request messages and respond.

        If a "request" message is received, decode the task and the content
        and call the appropriate function to prepare the response. A reply
        message is then sent back.
        """
        try:
            content = msg.get('content')
            task = content.head().upper()
            logger.info("%s received request with task: %s" % (self.name, task))
        except Exception as e:
            logger.error('Could not get task string from request.')
            logger.error(e)
            reply_content = self.make_failure('INVALID_REQUEST')
            return self.reply_with_content(msg, reply_content)

        if task in self.tasks:
            reply_content = self._respond_to(task, content)
        else:
            logger.error('Could not perform task.')
            logger.error("Task %s not found in %s." %
                         (task, str(self.tasks)))
            reply_content = self.make_failure('UNKNOWN_TASK')

        return self.reply_with_content(msg, reply_content)

    def _respond_to(self, task, content):
        """Get the method to responsd to the task indicated by task."""
        resp_name = "respond_" + task.replace('-', '_').lower()
        try:
            resp = getattr(self, resp_name)
            logger.info("%s will perform task %s with method %s."
                        % (self.name, task, resp_name))
        except AttributeError:
            logger.error("Tried to execute unimplemented task.")
            logger.error("Did not find response method %s." % resp_name)
            return self.make_failure('INVALID_TASK')
        try:
            reply_content = resp(content)
            return reply_content
        except BioagentException:
            raise
        except Exception as e:
            logger.error('Could not perform response to %s' % task)
            logger.exception(e)
            return self.make_failure('INTERNAL_FAILURE')

    def reply_with_content(self, msg, reply_content):
        """A wrapper around the reply method from KQMLModule."""
        reply_msg = KQMLPerformative('reply')
        reply_msg.set('content', reply_content)
        self.reply(msg, reply_msg)
        return

    def tell(self, content):
        """Send a tell message."""
        msg = KQMLPerformative('tell')
        msg.set('content', content)
        return self.send(msg)

    def request(self, content):
        """Send a request message."""
        msg = KQMLPerformative('request')
        msg.set('content', content)
        return self.send(msg)

    def error_reply(self, msg, comment):
        if not self.testing:
            return KQMLModule.error_reply(self, msg, comment)
        else:
            return msg, comment

    def make_failure(self, reason=None, description=None):
        msg = KQMLList('FAILURE')
        if reason:
            msg.set('reason', reason)
        if description:
            msg.sets('description', description)
        return msg

    def send_null_provenance(self, stmt, for_what, reason=''):
        """Send out that no provenance could be found for a given Statement."""
        content_fmt = ('
No supporting evidence found for {statement} from '
                       '{cause}{reason}.
')
        content = KQMLList('add-provenance')
        stmt_txt = EnglishAssembler([stmt]).make_model()
        content.sets('html', content_fmt.format(statement=stmt_txt,
                                                cause=for_what, reason=reason))
        return self.tell(content)

    def send_provenance_for_stmts(self, stmt_list, for_what, limit=50,
                                  ev_counts=None):
        """Send out a provenance tell for a list of INDRA Statements.

        The message is used to provide evidence supporting a conclusion.
        """
        logger.info("Sending provenance for %d statements for \"%s\"."
                    % (len(stmt_list), for_what))
        title = "Supporting evidence from the %s for %s" \
                % (self.name, for_what)
        content_fmt = '
%s (max %s):
\n%s
'
        evidence_html = self._make_report_cols_html(stmt_list, limit=limit,
                                                    ev_counts=ev_counts,
                                                    title=title)

        content = KQMLList('add-provenance')
        content.sets('html', content_fmt % (title, limit, evidence_html))
        return self.tell(content)

    def _make_evidence_html(self, stmts, ev_counts=None,
                            title='Results from the INDRA database'):
        "Make html from a set of statements."
        ha = HtmlAssembler(stmts, db_rest_url='db.indra.bio', title=title,
                           ev_totals=ev_counts)
        return ha.make_model()

    def _stash_evidence_html(self, html):
        """Make html for a set of statements, return a link to the file.

        The if the PROVENANCE_LOCATION environment variable determines where
        the content is stored. The variable should be divided by colons, the
        first division indicating whether the file is stored locally or on s3,
        being either "file" or "s3" respectively.

        If the html will be stored locally, the next and last division should
        be a path (absolute would be best) to the location where html files
        will be stored. For example:

            file:/home/myname/projects/cwc-integ/provenance

        If the html will be stored on s3, the next division should be the
        bucket, and the last division should be a prefix to a "directory" where
        the html files will be stored. For example:

            s3:cwc-stuff:bob/provenance

        The default is:

            file:{this directory}/../../../provenance

        Which should land in the cwc-integ directory. If the directory does not
        yet exist, it will be created.
        """
        # Get the provenance location.
        from os import environ, mkdir

        loc = environ.get('PROVENANCE_LOCATION')
        if loc is None:
            this_dir = path.dirname(path.abspath(__file__))
            rel = path.join(*([this_dir] + 3*[path.pardir] + ['provenance']))
            loc = 'file:' + path.abspath(rel)
        logger.info("Using provenance location: \"%s\"" % loc)

        # Save the file.
        method = loc.split(':')[0]
        fname = '%s.html' % uuid.uuid4()
        if method == 'file':
            prov_path = loc.split(':')[1]
            if not path.exists(prov_path):
                mkdir(prov_path)
            fpath = path.join(prov_path, fname)
            with open(fpath, 'w') as f:
                f.write(html)
            link = fpath
        elif method == 's3':
            bucket = loc.split(':')[1]
            prefix = loc.split(':')[2]

            # Get a connection to s3.
            import boto3
            from botocore import UNSIGNED
            from botocore.client import Config
            s3 = boto3.client('s3', config=Config(signature_version=UNSIGNED))

            key = prefix + fname
            link = 'https://s3.amazonaws.com/%s/%s' % (bucket, key)
            s3.put_object(Bucket=bucket, Key=key, Body=html.encode('utf-8'),
                          ContentType='text/html')
        else:
            logger.error('Invalid PROVENANCE_LOCATION: "%s". HTML not saved.'
                         % loc)
            link = None
        return link

    def say(self, message):
        """Say something to the user."""
        msg = KQMLList('say')
        msg.append(KQMLString(message))
        self.request(msg)

    def _make_report_cols_html(self, stmt_list, limit=5, ev_counts=None,
                               **kwargs):
        """Make columns listing the support given by the statement list."""

        def href(ref, text):
            return '%s' % (ref, text)

        # Build the list of relevant statements and count their prevalence.
        sorted_groups = group_and_sort_statements(stmt_list,
                                                  ev_totals=ev_counts)

        # Build the html.
        lines = []
        for key, verb, stmts in sorted_groups[:limit]:
            count = key[2]
            line = '
%s %s
' % (make_string_from_sort_key(key, verb),
                                       '(%d)' % count)
            lines.append(line)

        # Build the overall html.
        list_html = '
%s
' % ('\n'.join(lines))
        html = self._make_evidence_html(stmt_list, **kwargs)
        link = self._stash_evidence_html(html)
        if link is None:
            link_html = 'I could not generate the full list.'
        elif link.startswith('http'):
            link_html = href(link, 'Here') + ' is the full list.'
        else:
            link_html = 'Here: %s is the full list.' % link

        return list_html + '\n' + link_html

def get_img_path(img_name):
    """Get a full path for the given image name.

    The name will also have a timestamp added if settings.TIMESTAMP_PICS is
    True. The timestamp, if applied, will prepend the name.
    """
    if TIMESTAMP_PICS:
        date_str = datetime.now().strftime('%Y%m%d%H%M%S')
        img_name = '%s_%s' % (date_str, img_name)
    return path.join(IMAGE_DIR, img_name)
<|endoftext|>


<commit_before># -*- coding:utf-8 -*-
from django import forms
from django.contrib.auth.models import User

class CompleteReg(forms.Form):

    username = forms.RegexField(label=u'Имя пользователя', max_length=30, min_length=4,
                                required=True, regex=r'^[\w.@+-]+$')
    email = forms.EmailField(label=u'Email', required=True)

    def __init__(self, user_id, *args, **kwargs):
        super(CompleteReg, self).__init__(*args, **kwargs)
        self.user_id = user_id

    def clean_username(self):
        if self.cleaned_data['username']:
            try: u = User.objects.exclude(id=self.user_id).get(username=self.cleaned_data['username'])
            # if username is unique - it's ok
            except User.DoesNotExist: u = None

            if u is not None:
                raise forms.ValidationError(u'Пользователь с таким именем уже зарегистрирован')
        return self.cleaned_data['username']

    def clean_email(self):
        if self.cleaned_data['email']:
            try: u = User.objects.exclude(id=self.user_id).get(email=self.cleaned_data['email'])
            # if email is unique - it's ok
            except User.DoesNotExist: u = None

            if u is not None:
                raise forms.ValidationError(u'Пользователь с этим адресом уже зарегистрирован')
        return self.cleaned_data['email']<commit_msg>small refactor<commit_after># -*- coding:utf-8 -*-
from django import forms
from django.contrib.auth.models import User

class CompleteReg(forms.Form):

    username = forms.RegexField(label=u'Имя пользователя', max_length=30, min_length=4,
                                required=True, regex=r'^[\w.@+-]+$')
    email = forms.EmailField(label=u'Email', required=True)

    def __init__(self, user_id, *args, **kwargs):
        super(CompleteReg, self).__init__(*args, **kwargs)
        self.user_id = user_id

    def clean_username(self):
        if self.cleaned_data['username']:
            try:
                u = User.objects.exclude(id=self.user_id).get(username=self.cleaned_data['username'])
            # if username is unique - it's ok
            except User.DoesNotExist:
                u = None

            if u is not None:
                raise forms.ValidationError(u'Пользователь с таким именем уже зарегистрирован')
        return self.cleaned_data['username']

    def clean_email(self):
        if self.cleaned_data['email']:
            try:
                u = User.objects.exclude(id=self.user_id).get(email=self.cleaned_data['email'])
            # if email is unique - it's ok
            except User.DoesNotExist:
                u = None

            if u is not None:
                raise forms.ValidationError(u'Пользователь с этим адресом уже зарегистрирован')
        return self.cleaned_data['email']<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Created on Sat Jan 23 13:03:00 2016

@author: p
"""

import wellapplication as wa
import matplotlib
import pandas as pd

def test_getelev():
    x = [-111.21,41.4]
    g = wa.getelev(x)
    assert g > 100.0

def test_gethuc():
    x = [-111.21,41.4]
    g = wa.get_huc(x)
    assert len(g[0])>0

def test_USGSID():
    x = [-111.21,41.4]
    g = wa.USGSID([-111.21,41.4])
    assert g == '412400111123601'

def test_get_nwis():
    val_list = '01585200'
    g = wa.get_nwis(val_list, 'dv_site', '2012-06-01', '2012-07-01')
    assert len(g) == 2

def test_get_nwis_huc():
    g = wa.get_nwis('16010204', 'gw_huc')
    assert len(g) == 2

def test_get_station_info():
    assert len(wa.get_station_info(['01585200','10136500'])) == 2

def test_cleanGWL():
    stat, levs = wa.get_nwis('16010204', 'gw_huc')
    levs2 = wa.cleanGWL(levs)
    assert type(levs2.qualifiers[0]) == str

def test_avg_wl():
    g = wa.avg_wl('16030006',numObs= 50, grptype = 'monthly', avgtype = 'avgDiffWL')
    assert len(g)==12

def test_mktest():
    x = range(0,100)
    trend = wa.MannKendall.mk_test(x,0.05)
    assert trend.trend == 'increasing'

def test_mkts():
    import pandas as pd
    # import data via rest query
    infile= r'http://nwis.waterdata.usgs.gov/usa/nwis/qwdata/?site_no=11530500&agency_cd=USGS&inventory_output=0&rdb_inventory_output=file&TZoutput=0&pm_cd_compare=Greater%20than&radio_parm_cds=parm_cd_list&radio_multiple_parm_cds=00665&qw_attributes=0&format=rdb&qw_sample_wide=wide&rdb_qw_attributes=0&date_format=YYYY-MM-DD&rdb_compression=value&submitted_form=brief_list'
    #response = urllib2.urlopen(infile)

    # designate header
    cols = ['datetime','agency','site','end_dt','end_tm','dtm','dtm_cd','coll_ent_cd','medium_cd','tu_id','body_part_id','PO4']

    # read data into a Pandas dataframe
    usgsP = pd.read_table(infile, skiprows=65, skipfooter=5, na_values=('-','','No Data','No data'),
                          parse_dates={'datetime':[2,3]}, engine='python')
    usgsP.columns = cols

    # set row names as datetime
    usgsP.set_index('datetime',inplace=True)

    # drop unused columns
    usgsP.drop(['agency','site','end_dt','end_tm','dtm','dtm_cd','coll_ent_cd','medium_cd','tu_id','body_part_id'],axis=1,inplace=True)

    # add year and month columns
    usgsP['month'] = usgsP.index.to_datetime().month.astype(int)
    usgsP['year'] = usgsP.index.to_datetime().year.astype(int)

    # filter data to relevant dates
    usgsP = usgsP[(usgsP['year']>=1972) & (usgsP.index.to_datetime() <= pd.datetime(1979,10,31))]

    # sort data by month then year for analysis
    usgsP.sort_values(by=['month','year'],axis=0, inplace=True)

    # remove strings from data column and convert to numbers
    usgsP['PO4'] = usgsP['PO4'].map(lambda x: x.strip('><E '))
    #usgsP['PO4'] = usgsP['PO4'].astype(float)

    usgsP['PO4'] = pd.to_numeric(usgsP['PO4'])
    usgsP.dropna(inplace=True)
    g = wa.MannKendall.mk_ts(usgsP, 'PO4', 'month', 'year',0.05)
    assert g.S == -87

def test_piper():
    pipr = wa.piper()
    Chem =  {'Type':[1,2,2,3], 'Cl':[1.72,0.90,4.09,1.52], 'HCO3':[4.02,1.28,4.29,3.04],
             'SO4':[0.58,0.54,0.38,0.46], 'NaK':[1.40,0.90,3.38,2.86], 'Ca':[4.53,None,4.74,1.90],
             'Mg':[0.79,0.74,0.72,0.66], 'EC':[672.0,308.0,884.0,542.0], 'NO3':[0.4,0.36,0.08,0.40],
             'Sicc':[0.21,0.56,None,-0.41]}
    chem = pd.DataFrame(Chem)
    chem1 = pipr.fillMissing(chem)
    chem2 = pipr.convertIons(chem1)
    chem3 = pipr.ionPercentage(chem2)
    assert "CO3EC" in list(chem3.columns)

def test_pipe()
    pipr = piper()
    Chem =  {'Type':[1,2,2,3], 'Cl':[1.72,0.90,4.09,1.52], 'HCO3':[4.02,1.28,4.29,3.04],
             'SO4':[0.58,0.54,0.38,0.46], 'NaK':[1.40,0.90,3.38,2.86], 'Ca':[4.53,None,4.74,1.90],
             'Mg':[0.79,0.74,0.72,0.66], 'EC':[672.0,308.0,884.0,542.0], 'NO3':[0.4,0.36,0.08,0.40],
             'Sicc':[0.21,0.56,None,-0.41]}
    chem = pd.DataFrame(Chem)
    pipr.piperplot(chem)
    assert type(pipr.plot) == matplotlib.figure.Figure

def test_fdc():
    sites,levs = wa.get_nwis('01585200')
    assert len(wa.fdc(levs,'value')[0]) > 100
<commit_msg>Update test.py<commit_after># -*- coding: utf-8 -*-
"""
Created on Sat Jan 23 13:03:00 2016

@author: p
"""

import wellapplication as wa
import matplotlib
import pandas as pd

def test_getelev():
    x = [-111.21,41.4]
    g = wa.getelev(x)
    assert g > 100.0

def test_gethuc():
    x = [-111.21,41.4]
    g = wa.get_huc(x)
    assert len(g[0])>0

def test_USGSID():
    x = [-111.21,41.4]
    g = wa.USGSID([-111.21,41.4])
    assert g == '412400111123601'

def test_get_nwis():
    val_list = '01585200'
    g = wa.get_nwis(val_list, 'dv_site', '2012-06-01', '2012-07-01')
    assert len(g) == 2

def test_get_nwis_huc():
    g = wa.get_nwis('16010204', 'gw_huc')
    assert len(g) == 2

def test_get_station_info():
    assert len(wa.get_station_info(['01585200','10136500'])) == 2

def test_cleanGWL():
    stat, levs = wa.get_nwis('16010204', 'gw_huc')
    levs2 = wa.cleanGWL(levs)
    assert type(levs2.qualifiers[0]) == str

def test_avg_wl():
    g = wa.avg_wl('16030006',numObs= 50, grptype = 'monthly', avgtype = 'avgDiffWL')
    assert len(g)==12

def test_mktest():
    x = range(0,100)
    trend = wa.MannKendall.mk_test(x,0.05)
    assert trend.trend == 'increasing'

def test_mkts():
    import pandas as pd
    # import data via rest query
    infile= r'http://nwis.waterdata.usgs.gov/usa/nwis/qwdata/?site_no=11530500&agency_cd=USGS&inventory_output=0&rdb_inventory_output=file&TZoutput=0&pm_cd_compare=Greater%20than&radio_parm_cds=parm_cd_list&radio_multiple_parm_cds=00665&qw_attributes=0&format=rdb&qw_sample_wide=wide&rdb_qw_attributes=0&date_format=YYYY-MM-DD&rdb_compression=value&submitted_form=brief_list'
    #response = urllib2.urlopen(infile)

    # designate header
    cols = ['datetime','agency','site','end_dt','end_tm','dtm','dtm_cd','coll_ent_cd','medium_cd','tu_id','body_part_id','PO4']

    # read data into a Pandas dataframe
    usgsP = pd.read_table(infile, skiprows=65, skipfooter=5, na_values=('-','','No Data','No data'),
                          parse_dates={'datetime':[2,3]}, engine='python')
    usgsP.columns = cols

    # set row names as datetime
    usgsP.set_index('datetime',inplace=True)

    # drop unused columns
    usgsP.drop(['agency','site','end_dt','end_tm','dtm','dtm_cd','coll_ent_cd','medium_cd','tu_id','body_part_id'],axis=1,inplace=True)

    # add year and month columns
    usgsP['month'] = usgsP.index.to_datetime().month.astype(int)
    usgsP['year'] = usgsP.index.to_datetime().year.astype(int)

    # filter data to relevant dates
    usgsP = usgsP[(usgsP['year']>=1972) & (usgsP.index.to_datetime() <= pd.datetime(1979,10,31))]

    # sort data by month then year for analysis
    usgsP.sort_values(by=['month','year'],axis=0, inplace=True)

    # remove strings from data column and convert to numbers
    usgsP['PO4'] = usgsP['PO4'].map(lambda x: x.strip('><E '))
    #usgsP['PO4'] = usgsP['PO4'].astype(float)

    usgsP['PO4'] = pd.to_numeric(usgsP['PO4'])
    usgsP.dropna(inplace=True)
    g = wa.MannKendall.mk_ts(usgsP, 'PO4', 'month', 'year',0.05)
    assert g.S == -87

def test_piper():
    pipr = wa.piper()
    Chem =  {'Type':[1,2,2,3], 'Cl':[1.72,0.90,4.09,1.52], 'HCO3':[4.02,1.28,4.29,3.04],
             'SO4':[0.58,0.54,0.38,0.46], 'NaK':[1.40,0.90,3.38,2.86], 'Ca':[4.53,None,4.74,1.90],
             'Mg':[0.79,0.74,0.72,0.66], 'EC':[672.0,308.0,884.0,542.0], 'NO3':[0.4,0.36,0.08,0.40],
             'Sicc':[0.21,0.56,None,-0.41]}
    chem = pd.DataFrame(Chem)
    chem1 = pipr.fillMissing(chem)
    chem2 = pipr.convertIons(chem1)
    chem3 = pipr.ionPercentage(chem2)
    assert "CO3EC" in list(chem3.columns)

def test_pipe():
    pipr = piper()
    Chem =  {'Type':[1,2,2,3], 'Cl':[1.72,0.90,4.09,1.52], 'HCO3':[4.02,1.28,4.29,3.04],
             'SO4':[0.58,0.54,0.38,0.46], 'NaK':[1.40,0.90,3.38,2.86], 'Ca':[4.53,None,4.74,1.90],
             'Mg':[0.79,0.74,0.72,0.66], 'EC':[672.0,308.0,884.0,542.0], 'NO3':[0.4,0.36,0.08,0.40],
             'Sicc':[0.21,0.56,None,-0.41]}
    chem = pd.DataFrame(Chem)
    pipr.piperplot(chem)
    assert type(pipr.plot) == matplotlib.figure.Figure

def test_fdc():
    sites,levs = wa.get_nwis('01585200')
    assert len(wa.fdc(levs,'value')[0]) > 100

def test_gannt():
    sites,levs = wa.get_nwis('01585200')
    levs.drop(['qualifiers','site_no'],axis=1,inplace=True)
    levs['value'] = pd.to_numeric(levs['value'])
    gnt = wa.gantt(levs,stations=['value'])
    assert gnt.sitestats['StationId'].values[0] == 'value'
<|endoftext|>


<commit_before>import os
import shutil

from mock import patch, Mock, PropertyMock

import delegator
import toml

from pipenv.cli import (
    ensure_proper_casing,
    pip_install, pip_download
)

class TestPipenvWindows():

    def test_existience(self):
        assert True

    def test_cli_usage(self):
        delegator.run('mkdir test_project')
        os.chdir('test_project')

        os.environ['PIPENV_VENV_IN_PROJECT'] = '1'

        assert delegator.run('copy /y nul Pipfile').return_code == 0

        assert delegator.run('pipenv --python python').return_code == 0
        assert delegator.run('pipenv install Werkzeug').return_code == 0
        assert delegator.run('pipenv install pytest --dev').return_code == 0
        assert delegator.run('pipenv install git+https://github.com/requests/requests.git@v2.18.4#egg=requests').return_code == 0
        assert delegator.run('pipenv lock').return_code == 0

        # Test uninstalling a package after locking.
        assert delegator.run('pipenv uninstall Werkzeug').return_code == 0

        pipfile_output = delegator.run('type Pipfile').out
        lockfile_output = delegator.run('type Pipfile.lock').out

        # Ensure uninstall works.
        assert 'Werkzeug' not in pipfile_output
        assert 'werkzeug' not in lockfile_output

        # Ensure dev-packages work.
        assert 'pytest' in pipfile_output
        assert 'pytest' in lockfile_output

        # Ensure vcs dependencies work.
        assert 'requests' in pipfile_output
        assert '"git": "https://github.com/requests/requests.git"' in lockfile_output

        os.chdir('..')
        shutil.rmtree('test_project')

    def test_requirements_to_pipfile(self):
        delegator.run('mkdir test_requirements_to_pip')
        os.chdir('test_requirements_to_pip')

        os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
        os.environ['PIPENV_MAX_DEPTH'] = '1'

        with open('requirements.txt', 'w') as f:
            f.write('requests[socks]==2.18.1\n'
                    'git+https://github.com/kennethreitz/records.git@v0.5.0#egg=records\n'
                    '-e git+https://github.com/kennethreitz/tablib.git@v0.11.5#egg=tablib\n'
                    'six==1.10.0\n')

        c = delegator.run('pipenv --python python')
        print(c.err)
        assert c.return_code == 0

        print(delegator.run('pipenv lock').err)
        assert delegator.run('pipenv lock').return_code == 0

        pipfile_output = delegator.run('type Pipfile').out
        lockfile_output = delegator.run('type Pipfile.lock').out

        # Ensure extras work.
        assert 'extras = [ "socks",]' in pipfile_output
        assert 'pysocks' in lockfile_output

        # Ensure vcs dependencies work.
        assert 'packages.records' in pipfile_output
        assert '"git": "https://github.com/kennethreitz/records.git"' in lockfile_output

        # Ensure editable packages work.
        assert 'ref = "v0.11.5"' in pipfile_output
        assert '"editable": true' in lockfile_output

        # Ensure BAD_PACKAGES aren't copied into Pipfile from requirements.txt.
        assert 'six = "==1.10.0"' not in pipfile_output

        os.chdir('..')
        shutil.rmtree('test_requirements_to_pip')
        del os.environ['PIPENV_MAX_DEPTH']

    def test_timeout_long(self):
        delegator.run('mkdir test_timeout_long')
        os.chdir('test_timeout_long')

        os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
        os.environ['PIPENV_TIMEOUT'] = '60'

        assert delegator.run('copy /y nul Pipfile').return_code == 0

        assert delegator.run('pipenv --python python').return_code == 0

        os.chdir('..')
        shutil.rmtree('test_timeout_long')
        del os.environ['PIPENV_TIMEOUT']

    def test_timeout_short(self):
        delegator.run('mkdir test_timeout_short')
        os.chdir('test_timeout_short')

        os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
        os.environ['PIPENV_TIMEOUT'] = '0'

        assert delegator.run('copy /y nul Pipfile').return_code == 0

        assert delegator.run('pipenv --python python').return_code == 1

        os.chdir('..')
        shutil.rmtree('test_timeout_short')
        del os.environ['PIPENV_TIMEOUT']

    def test_pipenv_uninstall(self):
        delegator.run('mkdir test_pipenv_uninstall')
        os.chdir('test_pipenv_uninstall')

        # Build the environment.
        os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
        assert delegator.run('copy /y nul Pipfile').return_code == 0
        assert delegator.run('pipenv --python python').return_code == 0

        # Add entries to Pipfile.
        assert delegator.run('pipenv install Werkzeug').return_code == 0
        assert delegator.run('pipenv install pytest --dev').return_code == 0

        pipfile_output = delegator.run('type Pipfile').out
        pipfile_list = pipfile_output.split('\n')

        assert 'werkzeug = "*"' in pipfile_list
        assert 'pytest = "*"' in pipfile_list
        assert '[packages]' in pipfile_list
        assert '[dev-packages]' in pipfile_list

        # Uninstall from dev-packages, removing TOML section.
        assert delegator.run('pipenv uninstall pytest').return_code == 0

        # Test uninstalling non-existant dependency.
        c = delegator.run('pipenv uninstall NotAPackage')
        assert c.return_code == 0
        assert 'No package NotAPackage to remove from Pipfile.' in c.out

        pipfile_output = delegator.run('type Pipfile').out
        pipfile_list = pipfile_output.split('\n')

        assert 'Werkzeug = "*"' in pipfile_list
        assert 'pytest = "*"' not in pipfile_list
        assert '[packages]' in pipfile_list
        assert '[dev-packages]' not in pipfile_list

        os.chdir('..')
        shutil.rmtree('test_pipenv_uninstall')

    def test_pipenv_run(self):
        working_dir = 'test_pipenv_run'
        delegator.run('mkdir {0}'.format(working_dir))
        os.chdir(working_dir)

        # Build the environment.
        os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
        assert delegator.run('copy /y nul Pipfile').return_code == 0
        assert delegator.run('pipenv --python python').return_code == 0

        # Install packages for test.
        assert delegator.run('pipenv install pep8').return_code == 0
        assert delegator.run('pipenv install pytest').return_code == 0

        # Run test commands.
        assert delegator.run('pipenv run python -c \'print("test")\'').return_code == 0
        assert delegator.run('pipenv run pep8 --version').return_code == 0
        assert delegator.run('pipenv run pytest --version').return_code == 0

        os.chdir('..')
        shutil.rmtree(working_dir)

    def test_ensure_proper_casing_names(self):
        """Ensure proper casing for package names."""
        pfile_test = ("[packages]\n"
                      "DjAnGO = \"*\"\n"
                      "flask = \"==0.11\"\n"
                      "\n\n"
                      "[dev-packages]\n"
                      "PyTEST = \"*\"\n")

        # Load test Pipfile.
        p = toml.loads(pfile_test)

        assert 'DjAnGO' in p['packages']
        assert 'PyTEST' in p['dev-packages']

        changed = ensure_proper_casing(p)

        assert 'Django' in p['packages']
        assert 'DjAnGO' not in p['packages']

        assert 'pytest' in p['dev-packages']
        assert 'PyTEST' not in p['dev-packages']

        assert changed is True

    @patch('pipenv.project.Project.sources', new_callable=PropertyMock)
    @patch('delegator.run')
    def test_pip_install_should_try_every_possible_source(self, mocked_delegator, mocked_sources):
        sources = [
            {'url': 'http://dontexistis.in.pypi/simple'},
            {'url': 'http://existis.in.pypi/simple'}
        ]
        mocked_sources.return_value = sources
        first_cmd_return = Mock()
        first_cmd_return.return_code = 1
        second_cmd_return = Mock()
        second_cmd_return.return_code = 0
        mocked_delegator.side_effect = [first_cmd_return, second_cmd_return]
        c = pip_install('package')
        assert c.return_code == 0

    @patch('pipenv.project.Project.sources', new_callable=PropertyMock)
    @patch('delegator.run')
    def test_pip_install_should_return_the_last_error_if_no_cmd_worked(self, mocked_delegator, mocked_sources):
        sources = [
            {'url': 'http://dontexistis.in.pypi/simple'},
            {'url': 'http://dontexistis.in.pypi/simple'}
        ]
        mocked_sources.return_value = sources
        first_cmd_return = Mock()
        first_cmd_return.return_code = 1
        second_cmd_return = Mock()
        second_cmd_return.return_code = 1
        mocked_delegator.side_effect = [first_cmd_return, second_cmd_return]
        c = pip_install('package')
        assert c.return_code == 1
        assert c == second_cmd_return

    @patch('pipenv.project.Project.sources', new_callable=PropertyMock)
    @patch('delegator.run')
    def test_pip_install_should_return_the_first_cmd_that_worked(self, mocked_delegator, mocked_sources):
        sources = [
            {'url': 'http://existis.in.pypi/simple'},
            {'url': 'http://existis.in.pypi/simple'}
        ]
        mocked_sources.return_value = sources
        first_cmd_return = Mock()
        first_cmd_return.return_code = 0
        second_cmd_return = Mock()
        second_cmd_return.return_code = 0
        mocked_delegator.side_effect = [first_cmd_return, second_cmd_return]
        c = pip_install('package')
        assert c.return_code == 0
        assert c == first_cmd_return

    @patch('pipenv.project.Project.sources', new_callable=PropertyMock)
    @patch('delegator.run')
    def test_pip_download_should_try_every_possible_source(self, mocked_delegator, mocked_sources):
        sources = [
            {'url': 'http://dontexistis.in.pypi/simple'},
            {'url': 'http://existis.in.pypi/simple'}
        ]
        mocked_sources.return_value = sources
        first_cmd_return = Mock()
        first_cmd_return.return_code = 1
        second_cmd_return = Mock()
        second_cmd_return.return_code = 0
        mocked_delegator.side_effect = [first_cmd_return, second_cmd_return]
        c = pip_download('package')
        assert c.return_code == 0

    @patch('pipenv.project.Project.sources', new_callable=PropertyMock)
    @patch('delegator.run')
    def test_pip_download_should_return_the_last_error_if_no_cmd_worked(self, mocked_delegator, mocked_sources):
        sources = [
            {'url': 'http://dontexistis.in.pypi/simple'},
            {'url': 'http://dontexistis.in.pypi/simple'}
        ]
        mocked_sources.return_value = sources
        first_cmd_return = Mock()
        first_cmd_return.return_code = 1
        second_cmd_return = Mock()
        second_cmd_return.return_code = 1
        mocked_delegator.side_effect = [first_cmd_return, second_cmd_return]
        c = pip_download('package')
        assert c.return_code == 1
        assert c == second_cmd_return

    @patch('pipenv.project.Project.sources', new_callable=PropertyMock)
    @patch('delegator.run')
    def test_pip_download_should_return_the_first_cmd_that_worked(self, mocked_delegator, mocked_sources):
        sources = [
            {'url': 'http://existis.in.pypi/simple'},
            {'url': 'http://existis.in.pypi/simple'}
        ]
        mocked_sources.return_value = sources
        first_cmd_return = Mock()
        first_cmd_return.return_code = 0
        second_cmd_return = Mock()
        second_cmd_return.return_code = 0
        mocked_delegator.side_effect = [first_cmd_return, second_cmd_return]
        c = pip_download('package')
        assert c.return_code == 0
        assert c == first_cmd_return

    def test_lock_requirements_file(self):
        delegator.run('mkdir test_pipenv_requirements')
        os.chdir('test_pipenv_requirements')

        os.environ['PIPENV_VENV_IN_PROJECT'] = '1'

        assert delegator.run('copy /y nul Pipfile').return_code == 0
        assert delegator.run('pipenv --python python').return_code == 0
        assert delegator.run('pipenv install requests==2.14.0').return_code == 0
        assert delegator.run('pipenv install flask==0.12.2').return_code == 0
        assert delegator.run('pipenv install --dev pytest==3.1.1').return_code == 0

        req_list = ("requests==2.14.0", "flask==0.12.2", "pytest==3.1.1")

        # Validate requirements.txt.
        c = delegator.run('pipenv lock -r')
        assert c.return_code == 0
        for req in req_list:
            assert req in c.out

        # Cleanup.
        os.chdir('..')
        shutil.rmtree('test_pipenv_requirements')
<commit_msg>try this<commit_after>import os
import shutil

from mock import patch, Mock, PropertyMock

import delegator
import toml

from pipenv.cli import (
    ensure_proper_casing,
    pip_install, pip_download
)

class TestPipenvWindows():

    def test_existience(self):
        assert True

    def test_cli_usage(self):
        delegator.run('mkdir test_project')
        os.chdir('test_project')

        os.environ['PIPENV_VENV_IN_PROJECT'] = '1'

        assert delegator.run('copy /y nul Pipfile').return_code == 0

        assert delegator.run('pipenv --python python.exe').return_code == 0
        assert delegator.run('pipenv install Werkzeug').return_code == 0
        assert delegator.run('pipenv install pytest --dev').return_code == 0
        assert delegator.run('pipenv install git+https://github.com/requests/requests.git@v2.18.4#egg=requests').return_code == 0
        assert delegator.run('pipenv lock').return_code == 0

        # Test uninstalling a package after locking.
        assert delegator.run('pipenv uninstall Werkzeug').return_code == 0

        pipfile_output = delegator.run('type Pipfile').out
        lockfile_output = delegator.run('type Pipfile.lock').out

        # Ensure uninstall works.
        assert 'Werkzeug' not in pipfile_output
        assert 'werkzeug' not in lockfile_output

        # Ensure dev-packages work.
        assert 'pytest' in pipfile_output
        assert 'pytest' in lockfile_output

        # Ensure vcs dependencies work.
        assert 'requests' in pipfile_output
        assert '"git": "https://github.com/requests/requests.git"' in lockfile_output

        os.chdir('..')
        shutil.rmtree('test_project')

    def test_requirements_to_pipfile(self):
        delegator.run('mkdir test_requirements_to_pip')
        os.chdir('test_requirements_to_pip')

        os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
        os.environ['PIPENV_MAX_DEPTH'] = '1'

        with open('requirements.txt', 'w') as f:
            f.write('requests[socks]==2.18.1\n'
                    'git+https://github.com/kennethreitz/records.git@v0.5.0#egg=records\n'
                    '-e git+https://github.com/kennethreitz/tablib.git@v0.11.5#egg=tablib\n'
                    'six==1.10.0\n')

        c = delegator.run('pipenv --python python.exe')
        print(c.err)
        assert c.return_code == 0

        print(delegator.run('pipenv lock').err)
        assert delegator.run('pipenv lock').return_code == 0

        pipfile_output = delegator.run('type Pipfile').out
        lockfile_output = delegator.run('type Pipfile.lock').out

        # Ensure extras work.
        assert 'extras = [ "socks",]' in pipfile_output
        assert 'pysocks' in lockfile_output

        # Ensure vcs dependencies work.
        assert 'packages.records' in pipfile_output
        assert '"git": "https://github.com/kennethreitz/records.git"' in lockfile_output

        # Ensure editable packages work.
        assert 'ref = "v0.11.5"' in pipfile_output
        assert '"editable": true' in lockfile_output

        # Ensure BAD_PACKAGES aren't copied into Pipfile from requirements.txt.
        assert 'six = "==1.10.0"' not in pipfile_output

        os.chdir('..')
        shutil.rmtree('test_requirements_to_pip')
        del os.environ['PIPENV_MAX_DEPTH']

    def test_timeout_long(self):
        delegator.run('mkdir test_timeout_long')
        os.chdir('test_timeout_long')

        os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
        os.environ['PIPENV_TIMEOUT'] = '60'

        assert delegator.run('copy /y nul Pipfile').return_code == 0

        assert delegator.run('pipenv --python python.exe').return_code == 0

        os.chdir('..')
        shutil.rmtree('test_timeout_long')
        del os.environ['PIPENV_TIMEOUT']

    def test_timeout_short(self):
        delegator.run('mkdir test_timeout_short')
        os.chdir('test_timeout_short')

        os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
        os.environ['PIPENV_TIMEOUT'] = '0'

        assert delegator.run('copy /y nul Pipfile').return_code == 0

        assert delegator.run('pipenv --python python.exe').return_code == 1

        os.chdir('..')
        shutil.rmtree('test_timeout_short')
        del os.environ['PIPENV_TIMEOUT']

    def test_pipenv_uninstall(self):
        delegator.run('mkdir test_pipenv_uninstall')
        os.chdir('test_pipenv_uninstall')

        # Build the environment.
        os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
        assert delegator.run('copy /y nul Pipfile').return_code == 0
        assert delegator.run('pipenv --python python.exe').return_code == 0

        # Add entries to Pipfile.
        assert delegator.run('pipenv install Werkzeug').return_code == 0
        assert delegator.run('pipenv install pytest --dev').return_code == 0

        pipfile_output = delegator.run('type Pipfile').out
        pipfile_list = pipfile_output.split('\n')

        assert 'werkzeug = "*"' in pipfile_list
        assert 'pytest = "*"' in pipfile_list
        assert '[packages]' in pipfile_list
        assert '[dev-packages]' in pipfile_list

        # Uninstall from dev-packages, removing TOML section.
        assert delegator.run('pipenv uninstall pytest').return_code == 0

        # Test uninstalling non-existant dependency.
        c = delegator.run('pipenv uninstall NotAPackage')
        assert c.return_code == 0
        assert 'No package NotAPackage to remove from Pipfile.' in c.out

        pipfile_output = delegator.run('type Pipfile').out
        pipfile_list = pipfile_output.split('\n')

        assert 'Werkzeug = "*"' in pipfile_list
        assert 'pytest = "*"' not in pipfile_list
        assert '[packages]' in pipfile_list
        assert '[dev-packages]' not in pipfile_list

        os.chdir('..')
        shutil.rmtree('test_pipenv_uninstall')

    def test_pipenv_run(self):
        working_dir = 'test_pipenv_run'
        delegator.run('mkdir {0}'.format(working_dir))
        os.chdir(working_dir)

        # Build the environment.
        os.environ['PIPENV_VENV_IN_PROJECT'] = '1'
        assert delegator.run('copy /y nul Pipfile').return_code == 0
        assert delegator.run('pipenv --python python.exe').return_code == 0

        # Install packages for test.
        assert delegator.run('pipenv install pep8').return_code == 0
        assert delegator.run('pipenv install pytest').return_code == 0

        # Run test commands.
        assert delegator.run('pipenv run python -c \'print("test")\'').return_code == 0
        assert delegator.run('pipenv run pep8 --version').return_code == 0
        assert delegator.run('pipenv run pytest --version').return_code == 0

        os.chdir('..')
        shutil.rmtree(working_dir)

    def test_ensure_proper_casing_names(self):
        """Ensure proper casing for package names."""
        pfile_test = ("[packages]\n"
                      "DjAnGO = \"*\"\n"
                      "flask = \"==0.11\"\n"
                      "\n\n"
                      "[dev-packages]\n"
                      "PyTEST = \"*\"\n")

        # Load test Pipfile.
        p = toml.loads(pfile_test)

        assert 'DjAnGO' in p['packages']
        assert 'PyTEST' in p['dev-packages']

        changed = ensure_proper_casing(p)

        assert 'Django' in p['packages']
        assert 'DjAnGO' not in p['packages']

        assert 'pytest' in p['dev-packages']
        assert 'PyTEST' not in p['dev-packages']

        assert changed is True

    @patch('pipenv.project.Project.sources', new_callable=PropertyMock)
    @patch('delegator.run')
    def test_pip_install_should_try_every_possible_source(self, mocked_delegator, mocked_sources):
        sources = [
            {'url': 'http://dontexistis.in.pypi/simple'},
            {'url': 'http://existis.in.pypi/simple'}
        ]
        mocked_sources.return_value = sources
        first_cmd_return = Mock()
        first_cmd_return.return_code = 1
        second_cmd_return = Mock()
        second_cmd_return.return_code = 0
        mocked_delegator.side_effect = [first_cmd_return, second_cmd_return]
        c = pip_install('package')
        assert c.return_code == 0

    @patch('pipenv.project.Project.sources', new_callable=PropertyMock)
    @patch('delegator.run')
    def test_pip_install_should_return_the_last_error_if_no_cmd_worked(self, mocked_delegator, mocked_sources):
        sources = [
            {'url': 'http://dontexistis.in.pypi/simple'},
            {'url': 'http://dontexistis.in.pypi/simple'}
        ]
        mocked_sources.return_value = sources
        first_cmd_return = Mock()
        first_cmd_return.return_code = 1
        second_cmd_return = Mock()
        second_cmd_return.return_code = 1
        mocked_delegator.side_effect = [first_cmd_return, second_cmd_return]
        c = pip_install('package')
        assert c.return_code == 1
        assert c == second_cmd_return

    @patch('pipenv.project.Project.sources', new_callable=PropertyMock)
    @patch('delegator.run')
    def test_pip_install_should_return_the_first_cmd_that_worked(self, mocked_delegator, mocked_sources):
        sources = [
            {'url': 'http://existis.in.pypi/simple'},
            {'url': 'http://existis.in.pypi/simple'}
        ]
        mocked_sources.return_value = sources
        first_cmd_return = Mock()
        first_cmd_return.return_code = 0
        second_cmd_return = Mock()
        second_cmd_return.return_code = 0
        mocked_delegator.side_effect = [first_cmd_return, second_cmd_return]
        c = pip_install('package')
        assert c.return_code == 0
        assert c == first_cmd_return

    @patch('pipenv.project.Project.sources', new_callable=PropertyMock)
    @patch('delegator.run')
    def test_pip_download_should_try_every_possible_source(self, mocked_delegator, mocked_sources):
        sources = [
            {'url': 'http://dontexistis.in.pypi/simple'},
            {'url': 'http://existis.in.pypi/simple'}
        ]
        mocked_sources.return_value = sources
        first_cmd_return = Mock()
        first_cmd_return.return_code = 1
        second_cmd_return = Mock()
        second_cmd_return.return_code = 0
        mocked_delegator.side_effect = [first_cmd_return, second_cmd_return]
        c = pip_download('package')
        assert c.return_code == 0

    @patch('pipenv.project.Project.sources', new_callable=PropertyMock)
    @patch('delegator.run')
    def test_pip_download_should_return_the_last_error_if_no_cmd_worked(self, mocked_delegator, mocked_sources):
        sources = [
            {'url': 'http://dontexistis.in.pypi/simple'},
            {'url': 'http://dontexistis.in.pypi/simple'}
        ]
        mocked_sources.return_value = sources
        first_cmd_return = Mock()
        first_cmd_return.return_code = 1
        second_cmd_return = Mock()
        second_cmd_return.return_code = 1
        mocked_delegator.side_effect = [first_cmd_return, second_cmd_return]
        c = pip_download('package')
        assert c.return_code == 1
        assert c == second_cmd_return

    @patch('pipenv.project.Project.sources', new_callable=PropertyMock)
    @patch('delegator.run')
    def test_pip_download_should_return_the_first_cmd_that_worked(self, mocked_delegator, mocked_sources):
        sources = [
            {'url': 'http://existis.in.pypi/simple'},
            {'url': 'http://existis.in.pypi/simple'}
        ]
        mocked_sources.return_value = sources
        first_cmd_return = Mock()
        first_cmd_return.return_code = 0
        second_cmd_return = Mock()
        second_cmd_return.return_code = 0
        mocked_delegator.side_effect = [first_cmd_return, second_cmd_return]
        c = pip_download('package')
        assert c.return_code == 0
        assert c == first_cmd_return

    def test_lock_requirements_file(self):
        delegator.run('mkdir test_pipenv_requirements')
        os.chdir('test_pipenv_requirements')

        os.environ['PIPENV_VENV_IN_PROJECT'] = '1'

        assert delegator.run('copy /y nul Pipfile').return_code == 0
        assert delegator.run('pipenv --python python.exe').return_code == 0
        assert delegator.run('pipenv install requests==2.14.0').return_code == 0
        assert delegator.run('pipenv install flask==0.12.2').return_code == 0
        assert delegator.run('pipenv install --dev pytest==3.1.1').return_code == 0

        req_list = ("requests==2.14.0", "flask==0.12.2", "pytest==3.1.1")

        # Validate requirements.txt.
        c = delegator.run('pipenv lock -r')
        assert c.return_code == 0
        for req in req_list:
            assert req in c.out

        # Cleanup.
        os.chdir('..')
        shutil.rmtree('test_pipenv_requirements')
<|endoftext|>


<commit_before>from pprint import pprint

from django.db import connections
from django.db.models import Exists, OuterRef
from django.http import JsonResponse

from utils.views import totalize_data
from base.views import O2BaseGetView, O2BaseGetPostView

import comercial.models
from comercial.views.estoque import grade_meta_estoque
import comercial.forms
import produto.queries
import produto.models

import lotes.models
from lotes.views.quant_estagio import grade_meta_giro

class AProduzir(O2BaseGetView):

    def __init__(self, *args, **kwargs):
        super(AProduzir, self).__init__(*args, **kwargs)
        self.template_name = 'lotes/a_produzir.html'
        self.title_name = 'A produzir por modelo'

    def mount_context(self):
        cursor = connections['so'].cursor()

        data = []

        metas = comercial.models.MetaEstoque.objects
        metas = metas.annotate(antiga=Exists(
            comercial.models.MetaEstoque.objects.filter(
                modelo=OuterRef('modelo'),
                data__gt=OuterRef('data')
            )
        ))
        metas = metas.filter(antiga=False)
        metas = metas.exclude(venda_mensal=0)
        metas = metas.values()

        for row in metas:
            data_row = next(
                (dr for dr in data if dr['modelo'] == row['modelo']),
                False)
            if not data_row:
                data_row = {
                    'modelo': row['modelo'],
                }
                data.append(data_row)
            data_row['meta_giro'] = row['meta_giro']
            data_row['meta_estoque'] = row['meta_estoque']
            data_row['meta'] = row['meta_giro'] + row['meta_estoque']
            data_row['total_op'] = 0
            data_row['total_op|CLASS'] = 'total_op-{}'.format(row['modelo'])
            data_row['total_ped'] = 0
            data_row['total_ped|CLASS'] = 'total_ped-{}'.format(row['modelo'])
            data_row['op_menos_ped'] = 0
            data_row['op_menos_ped|CLASS'] = 'op_menos_ped-{}'.format(
                row['modelo'])
            data_row['a_produzir'] = data_row['meta']
            data_row['a_produzir|CLASS'] = 'a_produzir-{}'.format(
                row['modelo'])
            data_row['excesso'] = 0
            data_row['excesso|CLASS'] = 'excesso-{}'.format(
                row['modelo'])

        data = sorted(data, key=lambda i: -i['meta'])

        totalize_data(data, {
            'sum': ['meta_giro', 'meta_estoque', 'meta', 'total_op',
                    'total_ped', 'op_menos_ped', 'a_produzir', 'excesso'],
            'count': [],
            'descr': {'modelo': 'Totais:'},
            'row_style': 'font-weight: bold;',
            'class_suffix': '__total',
        })

        self.context.update({
            'headers': ['Modelo', 'Meta de estoque', 'Meta de giro (lead)',
                        'Total das metas (A)', 'Total das OPs',
                        'Carteira de pedidos', 'OPs – Pedidos (B)',
                        'A produzir (A-B)[+]', 'Excesso (A-B)[-]'],
            'fields': ['modelo', 'meta_estoque', 'meta_giro',
                       'meta', 'total_op',
                       'total_ped', 'op_menos_ped',
                       'a_produzir', 'excesso'],
            'data': data,
            'style': {
                2: 'text-align: right;',
                3: 'text-align: right;',
                4: 'text-align: right;',
                5: 'text-align: right;',
                6: 'text-align: right;',
                7: 'text-align: right;',
                8: 'text-align: right;',
                9: 'text-align: right;',
            },
        })

def op_producao_modelo(request, modelo):
    cursor = connections['so'].cursor()
    data = {
        'modelo': modelo,
    }

    try:
        data_op = lotes.models.busca_op(
            cursor, modelo=modelo, tipo='v', tipo_alt='p',
            situacao='a', posicao='p')

        if len(data_op) == 0:
            total_op = 0
        else:
            totalize_data(data_op, {
                'sum': ['QTD_AP'],
                'count': [],
                'descr': {'OP': 'T:'},
            })
            total_op = data_op[-1]['QTD_AP']

    except Exception:
        data.update({
            'result': 'ERR',
            'descricao_erro': 'Erro ao buscar OP',
        })
        return JsonResponse(data, safe=False)

    data.update({
        'result': 'OK',
        'total_op': total_op,
    })
    return JsonResponse(data, safe=False)

def pedido_lead_modelo(request, modelo):
    cursor = connections['so'].cursor()
    data = {
        'modelo': modelo,
    }

    try:
        colecao = produto.queries.colecao_de_modelo(cursor, modelo)
        if colecao == -1:
            lead = 0
        else:
            try:
                lc = lotes.models.LeadColecao.objects.get(colecao=colecao)
                lead = lc.lead
            except models.LeadColecao.DoesNotExist:
                lead = 0

        if lead == 0:
            periodo = ''
        else:
            periodo = lead + 7

        data_ped = lotes.models.busca_pedido(
            cursor, modelo=modelo, periodo=':{}'.format(periodo))

        if len(data_ped) == 0:
            total_ped = 0
        else:
            totalize_data(data_ped, {
                'sum': ['QTD'],
                'count': [],
                'descr': {'REF': 'T:'}})
            total_ped = data_ped[-1]['QTD']

    except Exception:
        data.update({
            'result': 'ERR',
            'descricao_erro': 'Erro ao buscar Pedido',
        })
        return JsonResponse(data, safe=False)

    data.update({
        'result': 'OK',
        'total_ped': total_ped,
    })
    return JsonResponse(data, safe=False)

def get_celula(grd, tam, cor):
    result = 0
    sortimento_field = grd['fields'][0]
    for linha in grd['data'][:-1]:
        if linha[sortimento_field] == cor:
            if tam in linha:
                result = linha[tam]
            else:
                result = 0
            break
    return result

def soma_grades(g1, g2):
    return opera_grades(g1, g2, '+')

def subtrai_grades(g1, g2):
    return opera_grades(g1, g2, '-')

def opera_grades(g1, g2, operacao):
    tamanhos1 = set(g1['headers'][1:-1])
    tamanhos2 = set(g2['headers'][1:-1])
    tamanhos = list(tamanhos1.union(tamanhos2))
    s_tamanhos = produto.models.S_Tamanho.objects.all().values()
    tamanhos = sorted(
        tamanhos,
        key=lambda tam: [
            s_tam['ordem_tamanho']
            for s_tam in s_tamanhos
            if s_tam['tamanho_ref'] == tam
        ])

    sortimento_field1 = g1['fields'][0]
    sortimento_field2 = g2['fields'][0]
    cores1 = set([
        linha[sortimento_field1]
        for linha in g1['data'][:-1]
    ])
    cores2 = set([
        linha[sortimento_field2]
        for linha in g2['data'][:-1]
    ])
    cores = list(cores1.union(cores2))
    cores = sorted(cores)

    grade = {}
    grade['fields'] = [sortimento_field1, *tamanhos, 'total']
    grade['headers'] = ['Cor/Tamanho', *tamanhos, 'Total']
    grade['style'] = {
        i+2: 'text-align: right;'
        for i, _ in enumerate(tamanhos)
    }
    grade['style'][len(tamanhos)+2] = 'text-align: right; font-weight: bold;'
    grade['data'] = []
    linha_zerada = {
        tam: 0
        for tam in tamanhos
    }
    linha_zerada.update({
        'total': 0,
    })
    totais = linha_zerada.copy()

    for cor in cores:
        linha = linha_zerada.copy()
        linha[sortimento_field1] = cor
        total_linha = 0
        for tam in tamanhos:
            valor1 = get_celula(g1, tam, cor)
            valor2 = get_celula(g2, tam, cor)
            if operacao == '+':
                valor = valor1 + valor2
            else:
                valor = valor1 - valor2
            linha[tam] = valor
            totais[tam] += valor
            total_linha += valor
        linha['total'] = total_linha
        totais['total'] += total_linha
        grade['data'].append(linha)

    totais[sortimento_field1] = 'Total'
    totais['|STYLE'] = 'font-weight: bold;'
    grade['data'].append(totais)
    return grade

class GradeProduzir(O2BaseGetPostView):

    def __init__(self, *args, **kwargs):
        super(GradeProduzir, self).__init__(*args, **kwargs)
        self.Form_class = comercial.forms.AnaliseModeloForm
        self.template_name = 'lotes/grade_produzir.html'
        self.title_name = 'Grade de modelo a produzir'
        self.get_args = ['modelo']

    def mount_context(self):
        cursor = connections['so'].cursor()

        modelo = self.form.cleaned_data['modelo']
        self.context.update({
            'modelo': modelo,
        })

        metas = comercial.models.MetaEstoque.objects
        metas = metas.annotate(antiga=Exists(
            comercial.models.MetaEstoque.objects.filter(
                modelo=OuterRef('modelo'),
                data__gt=OuterRef('data')
            )
        ))
        metas = metas.filter(antiga=False, modelo=modelo)
        metas = metas.order_by('-meta_estoque')
        if len(metas) == 0:
            self.context.update({
                'msg_meta_estoque': 'Modelo sem meta de estoque definida',
                'msg_meta_giro': 'Modelo sem meta de giro definida',
            })
            return
        else:
            meta = metas[0]

        calcula_grade = False
        if meta.meta_estoque == 0:
            self.context.update({
                'msg_meta_estoque': 'Modelo com meta de estoque zerada',
            })
        else:
            gme = grade_meta_estoque(meta)
            self.context.update({
                'gme': gme,
            })
            calcula_grade = True

        colecao = produto.queries.colecao_de_modelo(
            cursor, modelo)
        if colecao == -1:
            lead = 0
        else:
            try:
                lc = lotes.models.LeadColecao.objects.get(
                    colecao=colecao)
                lead = lc.lead
            except lotes.models.LeadColecao.DoesNotExist:
                lead = 0

        if meta.meta_giro == 0:
            self.context.update({
                'msg_meta_giro': 'Modelo com meta de giro zerada',
            })
        else:
            gmg = grade_meta_giro(meta, lead, show_distrib=False)
            self.context.update({
                'gmg': gmg,
            })
            calcula_grade = True

        if not calcula_grade:
            return

        if meta.meta_estoque == 0:
            gm = gmg
        elif meta.meta_giro == 0:
            gm = gme
        else:
            gm = soma_grades(gme, gmg)

        self.context.update({
            'gm': gm,
        })

        g_header, g_fields, g_data, g_style, total_op = \
            lotes.models.op_sortimentos(
                cursor, tipo='a', descr_sort=False, modelo=modelo,
                situacao='a', tipo_ref='v', tipo_alt='p', total='Total')

        if total_op != 0:
            gop = {
                'headers': g_header,
                'fields': g_fields,
                'data': g_data,
                'style': g_style,
            }
            self.context.update({
                'gop': gop,
            })

        if lead == 0:
            periodo = ''
        else:
            periodo = lead + 7

        gp_header, gp_fields, gp_data, gp_style, total_ped = \
            lotes.models.ped_sortimento(
                cursor, tipo_sort='c', descr_sort=False, modelo=modelo,
                cancelado='n', faturado='n', total='Total',
                periodo=':{}'.format(periodo))

        if total_ped != 0:
            gped = {
                'headers': gp_header,
                'fields': gp_fields,
                'data': gp_data,
                'style': gp_style,
            }
            self.context.update({
                'gped': gped,
            })
<commit_msg>apresentação inicial da grade de OP - pedidos<commit_after>from pprint import pprint

from django.db import connections
from django.db.models import Exists, OuterRef
from django.http import JsonResponse

from utils.views import totalize_data
from base.views import O2BaseGetView, O2BaseGetPostView

import comercial.models
from comercial.views.estoque import grade_meta_estoque
import comercial.forms
import produto.queries
import produto.models

import lotes.models
from lotes.views.quant_estagio import grade_meta_giro

class AProduzir(O2BaseGetView):

    def __init__(self, *args, **kwargs):
        super(AProduzir, self).__init__(*args, **kwargs)
        self.template_name = 'lotes/a_produzir.html'
        self.title_name = 'A produzir por modelo'

    def mount_context(self):
        cursor = connections['so'].cursor()

        data = []

        metas = comercial.models.MetaEstoque.objects
        metas = metas.annotate(antiga=Exists(
            comercial.models.MetaEstoque.objects.filter(
                modelo=OuterRef('modelo'),
                data__gt=OuterRef('data')
            )
        ))
        metas = metas.filter(antiga=False)
        metas = metas.exclude(venda_mensal=0)
        metas = metas.values()

        for row in metas:
            data_row = next(
                (dr for dr in data if dr['modelo'] == row['modelo']),
                False)
            if not data_row:
                data_row = {
                    'modelo': row['modelo'],
                }
                data.append(data_row)
            data_row['meta_giro'] = row['meta_giro']
            data_row['meta_estoque'] = row['meta_estoque']
            data_row['meta'] = row['meta_giro'] + row['meta_estoque']
            data_row['total_op'] = 0
            data_row['total_op|CLASS'] = 'total_op-{}'.format(row['modelo'])
            data_row['total_ped'] = 0
            data_row['total_ped|CLASS'] = 'total_ped-{}'.format(row['modelo'])
            data_row['op_menos_ped'] = 0
            data_row['op_menos_ped|CLASS'] = 'op_menos_ped-{}'.format(
                row['modelo'])
            data_row['a_produzir'] = data_row['meta']
            data_row['a_produzir|CLASS'] = 'a_produzir-{}'.format(
                row['modelo'])
            data_row['excesso'] = 0
            data_row['excesso|CLASS'] = 'excesso-{}'.format(
                row['modelo'])

        data = sorted(data, key=lambda i: -i['meta'])

        totalize_data(data, {
            'sum': ['meta_giro', 'meta_estoque', 'meta', 'total_op',
                    'total_ped', 'op_menos_ped', 'a_produzir', 'excesso'],
            'count': [],
            'descr': {'modelo': 'Totais:'},
            'row_style': 'font-weight: bold;',
            'class_suffix': '__total',
        })

        self.context.update({
            'headers': ['Modelo', 'Meta de estoque', 'Meta de giro (lead)',
                        'Total das metas (A)', 'Total das OPs',
                        'Carteira de pedidos', 'OPs – Pedidos (B)',
                        'A produzir (A-B)[+]', 'Excesso (A-B)[-]'],
            'fields': ['modelo', 'meta_estoque', 'meta_giro',
                       'meta', 'total_op',
                       'total_ped', 'op_menos_ped',
                       'a_produzir', 'excesso'],
            'data': data,
            'style': {
                2: 'text-align: right;',
                3: 'text-align: right;',
                4: 'text-align: right;',
                5: 'text-align: right;',
                6: 'text-align: right;',
                7: 'text-align: right;',
                8: 'text-align: right;',
                9: 'text-align: right;',
            },
        })

def op_producao_modelo(request, modelo):
    cursor = connections['so'].cursor()
    data = {
        'modelo': modelo,
    }

    try:
        data_op = lotes.models.busca_op(
            cursor, modelo=modelo, tipo='v', tipo_alt='p',
            situacao='a', posicao='p')

        if len(data_op) == 0:
            total_op = 0
        else:
            totalize_data(data_op, {
                'sum': ['QTD_AP'],
                'count': [],
                'descr': {'OP': 'T:'},
            })
            total_op = data_op[-1]['QTD_AP']

    except Exception:
        data.update({
            'result': 'ERR',
            'descricao_erro': 'Erro ao buscar OP',
        })
        return JsonResponse(data, safe=False)

    data.update({
        'result': 'OK',
        'total_op': total_op,
    })
    return JsonResponse(data, safe=False)

def pedido_lead_modelo(request, modelo):
    cursor = connections['so'].cursor()
    data = {
        'modelo': modelo,
    }

    try:
        colecao = produto.queries.colecao_de_modelo(cursor, modelo)
        if colecao == -1:
            lead = 0
        else:
            try:
                lc = lotes.models.LeadColecao.objects.get(colecao=colecao)
                lead = lc.lead
            except models.LeadColecao.DoesNotExist:
                lead = 0

        if lead == 0:
            periodo = ''
        else:
            periodo = lead + 7

        data_ped = lotes.models.busca_pedido(
            cursor, modelo=modelo, periodo=':{}'.format(periodo))

        if len(data_ped) == 0:
            total_ped = 0
        else:
            totalize_data(data_ped, {
                'sum': ['QTD'],
                'count': [],
                'descr': {'REF': 'T:'}})
            total_ped = data_ped[-1]['QTD']

    except Exception:
        data.update({
            'result': 'ERR',
            'descricao_erro': 'Erro ao buscar Pedido',
        })
        return JsonResponse(data, safe=False)

    data.update({
        'result': 'OK',
        'total_ped': total_ped,
    })
    return JsonResponse(data, safe=False)

def get_celula(grd, tam, cor):
    result = 0
    sortimento_field = grd['fields'][0]
    for linha in grd['data'][:-1]:
        if linha[sortimento_field] == cor:
            if tam in linha:
                result = linha[tam]
            else:
                result = 0
            break
    return result

def soma_grades(g1, g2):
    return opera_grades(g1, g2, '+')

def subtrai_grades(g1, g2):
    return opera_grades(g1, g2, '-')

def opera_grades(g1, g2, operacao):
    tamanhos1 = set(g1['headers'][1:-1])
    tamanhos2 = set(g2['headers'][1:-1])
    tamanhos = list(tamanhos1.union(tamanhos2))
    s_tamanhos = produto.models.S_Tamanho.objects.all().values()
    tamanhos = sorted(
        tamanhos,
        key=lambda tam: [
            s_tam['ordem_tamanho']
            for s_tam in s_tamanhos
            if s_tam['tamanho_ref'] == tam
        ])

    sortimento_field1 = g1['fields'][0]
    sortimento_field2 = g2['fields'][0]
    cores1 = set([
        linha[sortimento_field1]
        for linha in g1['data'][:-1]
    ])
    cores2 = set([
        linha[sortimento_field2]
        for linha in g2['data'][:-1]
    ])
    cores = list(cores1.union(cores2))
    cores = sorted(cores)

    grade = {}
    grade['fields'] = [sortimento_field1, *tamanhos, 'total']
    grade['headers'] = ['Cor/Tamanho', *tamanhos, 'Total']
    grade['style'] = {
        i+2: 'text-align: right;'
        for i, _ in enumerate(tamanhos)
    }
    grade['style'][len(tamanhos)+2] = 'text-align: right; font-weight: bold;'
    grade['data'] = []
    linha_zerada = {
        tam: 0
        for tam in tamanhos
    }
    linha_zerada.update({
        'total': 0,
    })
    totais = linha_zerada.copy()

    for cor in cores:
        linha = linha_zerada.copy()
        linha[sortimento_field1] = cor
        total_linha = 0
        for tam in tamanhos:
            valor1 = get_celula(g1, tam, cor)
            valor2 = get_celula(g2, tam, cor)
            if operacao == '+':
                valor = valor1 + valor2
            else:
                valor = valor1 - valor2
            linha[tam] = valor
            totais[tam] += valor
            total_linha += valor
        linha['total'] = total_linha
        totais['total'] += total_linha
        grade['data'].append(linha)

    totais[sortimento_field1] = 'Total'
    totais['|STYLE'] = 'font-weight: bold;'
    grade['data'].append(totais)
    return grade

class GradeProduzir(O2BaseGetPostView):

    def __init__(self, *args, **kwargs):
        super(GradeProduzir, self).__init__(*args, **kwargs)
        self.Form_class = comercial.forms.AnaliseModeloForm
        self.template_name = 'lotes/grade_produzir.html'
        self.title_name = 'Grade de modelo a produzir'
        self.get_args = ['modelo']

    def mount_context(self):
        cursor = connections['so'].cursor()

        modelo = self.form.cleaned_data['modelo']
        self.context.update({
            'modelo': modelo,
        })

        metas = comercial.models.MetaEstoque.objects
        metas = metas.annotate(antiga=Exists(
            comercial.models.MetaEstoque.objects.filter(
                modelo=OuterRef('modelo'),
                data__gt=OuterRef('data')
            )
        ))
        metas = metas.filter(antiga=False, modelo=modelo)
        metas = metas.order_by('-meta_estoque')
        if len(metas) == 0:
            self.context.update({
                'msg_meta_estoque': 'Modelo sem meta de estoque definida',
                'msg_meta_giro': 'Modelo sem meta de giro definida',
            })
            return
        else:
            meta = metas[0]

        calcula_grade = False
        if meta.meta_estoque == 0:
            self.context.update({
                'msg_meta_estoque': 'Modelo com meta de estoque zerada',
            })
        else:
            gme = grade_meta_estoque(meta)
            self.context.update({
                'gme': gme,
            })
            calcula_grade = True

        colecao = produto.queries.colecao_de_modelo(
            cursor, modelo)
        if colecao == -1:
            lead = 0
        else:
            try:
                lc = lotes.models.LeadColecao.objects.get(
                    colecao=colecao)
                lead = lc.lead
            except lotes.models.LeadColecao.DoesNotExist:
                lead = 0

        if meta.meta_giro == 0:
            self.context.update({
                'msg_meta_giro': 'Modelo com meta de giro zerada',
            })
        else:
            gmg = grade_meta_giro(meta, lead, show_distrib=False)
            self.context.update({
                'gmg': gmg,
            })
            calcula_grade = True

        if not calcula_grade:
            return

        if meta.meta_estoque == 0:
            gm = gmg
        elif meta.meta_giro == 0:
            gm = gme
        else:
            gm = soma_grades(gme, gmg)

        self.context.update({
            'gm': gm,
        })

        g_header, g_fields, g_data, g_style, total_op = \
            lotes.models.op_sortimentos(
                cursor, tipo='a', descr_sort=False, modelo=modelo,
                situacao='a', tipo_ref='v', tipo_alt='p', total='Total')

        if total_op != 0:
            gop = {
                'headers': g_header,
                'fields': g_fields,
                'data': g_data,
                'style': g_style,
            }
            self.context.update({
                'gop': gop,
            })

        if lead == 0:
            periodo = ''
        else:
            periodo = lead + 7

        gp_header, gp_fields, gp_data, gp_style, total_ped = \
            lotes.models.ped_sortimento(
                cursor, tipo_sort='c', descr_sort=False, modelo=modelo,
                cancelado='n', faturado='n', total='Total',
                periodo=':{}'.format(periodo))

        if total_ped != 0:
            gped = {
                'headers': gp_header,
                'fields': gp_fields,
                'data': gp_data,
                'style': gp_style,
            }
            self.context.update({
                'gped': gped,
            })

        gopp = subtrai_grades(gop, gped)

        self.context.update({
            'gopp': gopp,
        })
<|endoftext|>


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

import json
import re
import urllib

import weechat as w

w.register('weebullet', 'Lefty', '0.0.1', 'BSD', 'weebullet pushes notifications from IRC to Pushbullet.', '', '')

w.hook_print("", "irc_privmsg", "", 1, "priv_msg_cb", "")
w.hook_command(
    "send_push_note", #command
    "send a push note", # description
    "[message]" # arguments description,
    "", #argument
    "",
    "",
    "cmd_send_push_note", "")

configs = {
#    "timeout": 20000,
    "api_key": ""
}

for option, default_value in configs.items():
    if w.config_get_plugin(option) == "":
        w.prnt("", w.prefix("error") + "pushbullet: Please set option: %s" % option)
        if type(default_value) == "str":
            w.prnt("", "pushbullet: /set plugins.var.python.weebullet.%s STRING" % option)
        elif type(default_value) == "int":
            w.prnt("", "pushbullet: /set plugins.var.python.weebullet.%s INT" % option)
        else:
            w.prnt("", "pushbullet: /set plugins.var.python.weebullet.%s VALUE" % option)

def process_pushbullet_cb(data, url, status, response, err):
    body = None
    headers = {}
    lines = response.rstrip().splitlines()
    status_code = int(lines.pop(0).split()[1])
    for line in lines:
        if body == "":
            body += line
            continue
        header_line = line.split(":", 2)
        if len(header_line) != 2:
            body = ""
            continue
        headers[header_line[0].strip()] = header_line[1].strip()

    # response is the string of http body
    if status == w.WEECHAT_HOOK_PROCESS_ERROR:
        w.prnt("", "[weebullet] Error sending to pushbullet: %s - %s" % (status, url))
        return w.WEECHAT_RC_ERROR

    if status_code is not 200:
        w.prnt("", "[weebullet] Error sending to pushbullet: %s - %s - %s" % (url, status_code, body))
        return w.WEECHAT_RC_ERROR

    return w.WEECHAT_RC_OK

def send_push(title, body):
    apikey = w.config_get_plugin("api_key")
    apiurl = "https://%s:@api.pushbullet.com/api/pushes" % (apikey)
    timeout = 20000 # FIXME - actually use config
    payload = urllib.urlencode({'type': 'note', 'title': title, 'body': body})
    w.hook_process_hashtable("url:" + apiurl, { "postfields": payload, "header":"1" }, timeout, "process_pushbullet_cb", "")

def cmd_send_push_note(data, buffer, args):
    send_push(
            title="Manual Notification from weechat",
            body=args
            )
    return w.WEECHAT_RC_OK

def priv_msg_cb(data, bufferp, uber_empty, tagsn, isdisplayed,
        ishilight, prefix, message):
    """Sends highlighted message to be printed on notification"""

    am_away = w.buffer_get_string(bufferp, 'localvar_away')

    if not am_away:
	w.prnt("", "[weebullet] Not away, skipping notification")
	return w.WEECHAT_RC_OK

    notif_body = u"<%s> %s" % (
        prefix.decode('utf-8'),
	message.decode('utf-8')
    )

    # Check that it's in a "/q" buffer and that I'm not the one writing the msg
    is_pm = w.buffer_get_string(bufferp, "localvar_type") == "private"
    is_notify_private = re.search(r'(^|,)notify_private(,|$)', tagsn) is not None
    # PM (query)
    if (is_pm and is_notify_private):
	send_push(
		title="Privmsg from %s" % prefix.decode('utf-8'),
		body=notif_body
	)

    # Highlight (your nick is quoted)
    elif (ishilight == "1"):
        bufname = (w.buffer_get_string(bufferp, "short_name") or
                w.buffer_get_string(bufferp, "name"))
	send_push(
		title="Highlight in %s" % bufname.decode('utf-8'),
		body=notif_body
	)

    return w.WEECHAT_RC_OK
<commit_msg>Handle bad api key specifically<commit_after>#!/usr/bin/env python

import json
import re
import urllib

import weechat as w

w.register('weebullet', 'Lefty', '0.0.1', 'BSD', 'weebullet pushes notifications from IRC to Pushbullet.', '', '')

w.hook_print("", "irc_privmsg", "", 1, "priv_msg_cb", "")
w.hook_command(
    "send_push_note", #command
    "send a push note", # description
    "[message]" # arguments description,
    "", #argument
    "",
    "",
    "cmd_send_push_note", "")

configs = {
#    "timeout": 20000,
    "api_key": ""
}

for option, default_value in configs.items():
    if w.config_get_plugin(option) == "":
        w.prnt("", w.prefix("error") + "pushbullet: Please set option: %s" % option)
        if type(default_value) == "str":
            w.prnt("", "pushbullet: /set plugins.var.python.weebullet.%s STRING" % option)
        elif type(default_value) == "int":
            w.prnt("", "pushbullet: /set plugins.var.python.weebullet.%s INT" % option)
        else:
            w.prnt("", "pushbullet: /set plugins.var.python.weebullet.%s VALUE" % option)

def process_pushbullet_cb(data, url, status, response, err):
    body = None
    headers = {}
    lines = response.rstrip().splitlines()
    status_code = int(lines.pop(0).split()[1])
    for line in lines:
        if body == "":
            body += line
            continue
        header_line = line.split(":", 2)
        if len(header_line) != 2:
            body = ""
            continue
        headers[header_line[0].strip()] = header_line[1].strip()

    # response is the string of http body
    if status == w.WEECHAT_HOOK_PROCESS_ERROR:
        w.prnt("", "[weebullet] Error sending to pushbullet: %s - %s" % (status, url))
        return w.WEECHAT_RC_ERROR

    if status_code is 401 or status_code is 403:
        w.prnt("", "[weebullet] Invalid API Token: %s" % (w.config_get_plugin("api_key")))
        return w.WEECHAT_RC_ERROR
    if status_code is not 200:
        w.prnt("", "[weebullet] Error sending to pushbullet: %s - %s - %s" % (url, status_code, body))
        return w.WEECHAT_RC_ERROR

    return w.WEECHAT_RC_OK

def send_push(title, body):
    apikey = w.config_get_plugin("api_key")
    apiurl = "https://%s:@api.pushbullet.com/api/pushes" % (apikey)
    timeout = 20000 # FIXME - actually use config
    payload = urllib.urlencode({'type': 'note', 'title': title, 'body': body})
    w.hook_process_hashtable("url:" + apiurl, { "postfields": payload, "header":"1" }, timeout, "process_pushbullet_cb", "")

def cmd_send_push_note(data, buffer, args):
    send_push(
            title="Manual Notification from weechat",
            body=args
            )
    return w.WEECHAT_RC_OK

def priv_msg_cb(data, bufferp, uber_empty, tagsn, isdisplayed,
        ishilight, prefix, message):
    """Sends highlighted message to be printed on notification"""

    am_away = w.buffer_get_string(bufferp, 'localvar_away')

    if not am_away:
	w.prnt("", "[weebullet] Not away, skipping notification")
	return w.WEECHAT_RC_OK

    notif_body = u"<%s> %s" % (
        prefix.decode('utf-8'),
	message.decode('utf-8')
    )

    # Check that it's in a "/q" buffer and that I'm not the one writing the msg
    is_pm = w.buffer_get_string(bufferp, "localvar_type") == "private"
    is_notify_private = re.search(r'(^|,)notify_private(,|$)', tagsn) is not None
    # PM (query)
    if (is_pm and is_notify_private):
	send_push(
		title="Privmsg from %s" % prefix.decode('utf-8'),
		body=notif_body
	)

    # Highlight (your nick is quoted)
    elif (ishilight == "1"):
        bufname = (w.buffer_get_string(bufferp, "short_name") or
                w.buffer_get_string(bufferp, "name"))
	send_push(
		title="Highlight in %s" % bufname.decode('utf-8'),
		body=notif_body
	)

    return w.WEECHAT_RC_OK
<|endoftext|>