<commit_before># emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
Test functions for models.regression
"""

import numpy as np
from numpy.random import standard_normal

import scipy.linalg as spl

from ..regression import OLSModel, ARModel, yule_walker

from nose.tools import assert_equal
from numpy.testing import assert_array_almost_equal

W = standard_normal

def test_OLS():
    X = W((40,10))
    Y = W((40,))
    model = OLSModel(design=X)
    results = model.fit(Y)
    assert_equal(results.df_resid, 30)

def test_AR():
    X = W((40,10))
    Y = W((40,))
    model = ARModel(design=X, rho=0.4)
    results = model.fit(Y)
    assert_equal(results.df_resid, 30)

def test_OLS_degenerate():
    X = W((40,10))
    X[:,0] = X[:,1] + X[:,2]
    Y = W((40,))
    model = OLSModel(design=X)
    results = model.fit(Y)
    assert_equal(results.df_resid, 31)

def test_AR_degenerate():
    X = W((40,10))
    X[:,0] = X[:,1] + X[:,2]
    Y = W((40,))
    model = ARModel(design=X, rho=0.9)
    results = model.fit(Y)
    assert_equal(results.df_resid, 31)

def test_yule_walker():
    # Test YW implementation against R results
    Y = np.array([1,3,4,5,8,9,10])
    N = len(Y)
    X = np.ones((N, 2))
    X[:,0] = np.arange(1,8)
    pX = spl.pinv(X)
    betas = pX.dot(Y)
    Yhat = Y - X.dot(betas)
    # R results from saving Yhat, then:
    # ar.yw(yhat$V1, aic=FALSE, order.max=2)
    rhos, sd = yule_walker(Yhat, 1, 'mle')
    assert_array_almost_equal(rhos, [-0.3004], 4)
    rhos, sd = yule_walker(Yhat, 2, 'mle')
    assert_array_almost_equal(rhos, [-0.5113, -0.7021], 4)
    rhos, sd = yule_walker(Yhat, 3, 'mle')
    assert_array_almost_equal(rhos, [-0.6737, -0.8204, -0.2313], 4)

<commit_msg>TST - expand Yule Walker tests to sigma2<commit_after># emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
Test functions for models.regression
"""

import numpy as np
from numpy.random import standard_normal

import scipy.linalg as spl

from ..regression import OLSModel, ARModel, yule_walker

from nose.tools import assert_equal
from numpy.testing import assert_array_almost_equal

W = standard_normal

def test_OLS():
    X = W((40,10))
    Y = W((40,))
    model = OLSModel(design=X)
    results = model.fit(Y)
    assert_equal(results.df_resid, 30)

def test_AR():
    X = W((40,10))
    Y = W((40,))
    model = ARModel(design=X, rho=0.4)
    results = model.fit(Y)
    assert_equal(results.df_resid, 30)

def test_OLS_degenerate():
    X = W((40,10))
    X[:,0] = X[:,1] + X[:,2]
    Y = W((40,))
    model = OLSModel(design=X)
    results = model.fit(Y)
    assert_equal(results.df_resid, 31)

def test_AR_degenerate():
    X = W((40,10))
    X[:,0] = X[:,1] + X[:,2]
    Y = W((40,))
    model = ARModel(design=X, rho=0.9)
    results = model.fit(Y)
    assert_equal(results.df_resid, 31)

def test_yule_walker_R():
    # Test YW implementation against R results
    Y = np.array([1,3,4,5,8,9,10])
    N = len(Y)
    X = np.ones((N, 2))
    X[:,0] = np.arange(1,8)
    pX = spl.pinv(X)
    betas = np.dot(pX, Y)
    Yhat = Y - np.dot(X, betas)
    # R results obtained from:
    # >>> np.savetxt('yhat.csv', Yhat)
    # > yhat = read.table('yhat.csv')
    # > ar.yw(yhat$V1, aic=FALSE, order.max=2)
    def r_fudge(sigma, order):
        # Reverse fudge in ar.R calculation labeled as splus compatibility fix
        return sigma **2 * N / (N-order-1)
    rhos, sd = yule_walker(Yhat, 1, 'mle')
    assert_array_almost_equal(rhos, [-0.3004], 4)
    assert_array_almost_equal(r_fudge(sd, 1), 0.2534, 4)
    rhos, sd = yule_walker(Yhat, 2, 'mle')
    assert_array_almost_equal(rhos, [-0.5113, -0.7021], 4)
    assert_array_almost_equal(r_fudge(sd, 2), 0.1606, 4)
    rhos, sd = yule_walker(Yhat, 3, 'mle')
    assert_array_almost_equal(rhos, [-0.6737, -0.8204, -0.2313], 4)
    assert_array_almost_equal(r_fudge(sd, 3), 0.2027, 4)
<|endoftext|>


<commit_before>#!/usr/bin/env python3
# -*- encoding: utf-8 -*-

from random import randint
HAUT = 'HAUT'
BAS = 'BAS'
GAUCHE = 'GAUCHE'
DROITE = 'DROITE'

class Game():
    def __init__(self):
        self.mat = [[4,4,2,0],[0,0,0,0],[0,0,0,0], [0,0,0,0]]
        self.free = [(i, j) for i in range(4) for j in range(4) if not self.mat[i][j]]
        x,y = self.free[randint(0, len(self.free)-1)]
        self.mat[x][y] = 2
        self.free = [(i, j) for i in range(4) for j in range(4) if not self.mat[i][j]]
        self.score = 0

    def update(self, direction):
        if direction == DROITE:
            for i in range(4):
                for k in range(3, 0, -1):
                    for j in range(k-1, -1, -1):
                        if self.mat[i][k] == self.mat[i][j]:
                            self.mat[i][k] *= 2
                            self.mat[i][j] = 0
                            if self.mat[i][k] != 0:
                                self.score += 1
                        elif self.mat[i][k] == 0:
                            self.mat[i][k] = self.mat[i][j]
                            self.mat[i][j] = 0
        elif direction == GAUCHE:
            for i in range(4):
                for k in range(3):
                    for j in range(k+1, 4):
                        if self.mat[i][k] == self.mat[i][j]:
                            self.mat[i][k] *= 2
                            self.mat[i][j] = 0
                            if self.mat[i][k] != 0:
                                self.score += 1
                        elif self.mat[i][k] == 0:
                            self.mat[i][k] = self.mat[i][j]
                            self.mat[i][j] = 0
        elif direction == HAUT:
            for i in range(4):
                for k in range(3):
                    for j in range(k+1, 4):
                        if self.mat[k][i] == self.mat[j][i]:
                            self.mat[k][i] *= 2
                            self.mat[j][i] = 0
                            if self.mat[k][i] != 0:
                                self.score += 1
                        elif self.mat[k][i] == 0:
                            self.mat[k][i] = self.mat[j][i]
                            self.mat[j][i] = 0
        elif direction == BAS:
            for i in range(4):
                for k in range(3, 0, -1):
                    for j in range(k-1, -1, -1):
                        if self.mat[k][i] == self.mat[j][i]:
                            self.mat[k][i] *= 2
                            self.mat[j][i] = 0
                            if self.mat[i][k] != 0:
                                self.score += 1
                        elif self.mat[k][i] == 0:
                            self.mat[k][i] = self.mat[i][j]
                            self.mat[j][i] = 0

    def play(self, direction):
        for i in range(4):
            print(self.mat[i])
        print()
        self.update(direction)
        self.free = [(i, j) for i in range(4) for j in range(4) if not self.mat[i][j]]
        x,y = self.free[randint(0, len(self.free)-1)]
        self.mat[x][y] = 2
        self.free = [(i, j) for i in range(4) for j in range(4) if not self.mat[i][j]]
        for i in range(4):
            print(self.mat[i])
        print(self.free)
        print(self.score)
        print('\n')

g = Game()
g.play(GAUCHE)
g.play(HAUT)
g.play(BAS)
g.play(GAUCHE)
g.play(DROITE)
g.play(BAS)
<commit_msg>reset mat<commit_after>#!/usr/bin/env python3
# -*- encoding: utf-8 -*-

from random import randint
HAUT = 'HAUT'
BAS = 'BAS'
GAUCHE = 'GAUCHE'
DROITE = 'DROITE'

class Game():
    def __init__(self):
        self.mat = [[0,0,0,0],[0,0,0,0],[0,0,0,0], [0,0,0,0]]
        self.free = [(i, j) for i in range(4) for j in range(4) if not self.mat[i][j]]
        x,y = self.free[randint(0, len(self.free)-1)]
        self.mat[x][y] = 2
        self.free = [(i, j) for i in range(4) for j in range(4) if not self.mat[i][j]]
        self.score = 0

    def update(self, direction):
        if direction == DROITE:
            for i in range(4):
                for k in range(3, 0, -1):
                    for j in range(k-1, -1, -1):
                        if self.mat[i][k] == self.mat[i][j]:
                            self.mat[i][k] *= 2
                            self.mat[i][j] = 0
                            if self.mat[i][k] != 0:
                                self.score += 1
                        elif self.mat[i][k] == 0:
                            self.mat[i][k] = self.mat[i][j]
                            self.mat[i][j] = 0
        elif direction == GAUCHE:
            for i in range(4):
                for k in range(3):
                    for j in range(k+1, 4):
                        if self.mat[i][k] == self.mat[i][j]:
                            self.mat[i][k] *= 2
                            self.mat[i][j] = 0
                            if self.mat[i][k] != 0:
                                self.score += 1
                        elif self.mat[i][k] == 0:
                            self.mat[i][k] = self.mat[i][j]
                            self.mat[i][j] = 0
        elif direction == HAUT:
            for i in range(4):
                for k in range(3):
                    for j in range(k+1, 4):
                        if self.mat[k][i] == self.mat[j][i]:
                            self.mat[k][i] *= 2
                            self.mat[j][i] = 0
                            if self.mat[k][i] != 0:
                                self.score += 1
                        elif self.mat[k][i] == 0:
                            self.mat[k][i] = self.mat[j][i]
                            self.mat[j][i] = 0
        elif direction == BAS:
            for i in range(4):
                for k in range(3, 0, -1):
                    for j in range(k-1, -1, -1):
                        if self.mat[k][i] == self.mat[j][i]:
                            self.mat[k][i] *= 2
                            self.mat[j][i] = 0
                            if self.mat[i][k] != 0:
                                self.score += 1
                        elif self.mat[k][i] == 0:
                            self.mat[k][i] = self.mat[i][j]
                            self.mat[j][i] = 0

    def play(self, direction):
        for i in range(4):
            print(self.mat[i])
        print()
        self.update(direction)
        self.free = [(i, j) for i in range(4) for j in range(4) if not self.mat[i][j]]
        x,y = self.free[randint(0, len(self.free)-1)]
        self.mat[x][y] = 2
        self.free = [(i, j) for i in range(4) for j in range(4) if not self.mat[i][j]]
        for i in range(4):
            print(self.mat[i])
        print(self.free)
        print(self.score)
        print('\n')

g = Game()
g.play(GAUCHE)
g.play(HAUT)
g.play(BAS)
g.play(GAUCHE)
g.play(DROITE)
g.play(BAS)
<|endoftext|>


<commit_before>#
# Author    : Manuel Bernal Llinares
# Project   : trackhub-creator
# Timestamp : 07-09-2017 11:24
# ---
# © 2017 Manuel Bernal Llinares <mbdebian@gmail.com>
# All rights reserved.
#

"""
This pipeline creates a trackhub for a PRIDE project, based on the information provided via a JSON formatted file, as it
can be seen on this sample:
{
  "trackHubName" : "PXD000625",
  "trackHubShortLabel" : "PXD000625 - Hepatoc...",
  "trackHubLongLabel" : "Experimental design For the label-free ...",
  "trackHubType" : "PROTEOMICS",
  "trackHubEmail" : "pride-support@ebi.ac.uk",
  "trackHubInternalAbsolutePath" : "...",
  "TrackhubCreationReportFilePath": "...",
  "trackMaps" : [ {
    "trackName" : "PXD000625_10090_Original",
    "trackShortLabel" : "PXD000625 - Mus musc...",
    "trackLongLabel" : "Experimental design For the label-free proteome analysis 17 mice were used composed of 5 ...",
    "trackSpecie" : "10090",
    "pogoFile" : "..."
  } ]
}
"""

import os
import json
import time
# App imports
import config_manager
import ensembl.service
import toolbox.general as general_toolbox
from pipelines.template_pipeline import PogoBasedPipelineDirector, DirectorConfigurationManager

# Globals
__configuration_file = None
__pipeline_arguments = None
__pipeline_director = None

# Pipeline properties access
def set_configuration_file(config_file):
    global __configuration_file
    if __configuration_file is None:
        __configuration_file = config_file
    return __configuration_file

def set_pipeline_arguments(pipeline_arguments):
    global __pipeline_arguments
    if __pipeline_arguments is None:
        __pipeline_arguments = pipeline_arguments
    return __pipeline_arguments

def get_pipeline_director():
    global __pipeline_director
    if __pipeline_director is None:
        __pipeline_director = TrackhubCreatorForProject(config_manager.read_config_from_file(__configuration_file),
                                                        __configuration_file,
                                                        __pipeline_arguments)
    return __pipeline_director

class ConfigManager(DirectorConfigurationManager):
    # Command Line Arguments for this pipeline look like
    #   # This is a JSON formatted file that contains all the relevant information needed for processing the project
    #   # data and create its trackhub
    #   project_data_file=project_data.json

    # Command Line Argument keys
    _CONFIG_COMMAND_LINE_ARGUMENT_KEY_PROJECT_DATA_FILE = 'project_data_file'

    def __init__(self, configuration_object, configuration_file, pipeline_arguments):
        super(ConfigManager, self).__init__(configuration_object, configuration_file, pipeline_arguments)
        # Lazy Process command line arguments
        self.__pipeline_arguments_object = None
        self.__running_mode = None

    def get_project_data_file_path(self):
        return self._get_value_for_pipeline_argument_key(self._CONFIG_COMMAND_LINE_ARGUMENT_KEY_PROJECT_DATA_FILE)

    def get_file_path_trackhub_creation_report(self):
        return os.path.join(config_manager.get_app_config_manager().get_session_working_dir(),
                            "trackhub_creation.report")

# Models for dealing with the data file that describes the project
class ProjectTrackDescriptor:
    # Project Data File keys relative to every TrackMap object
    _PROJECT_DATA_FILE_KEY_TRACK_NAME = 'trackName'
    _PROJECT_DATA_FILE_KEY_TRACK_SHORT_LABEL = 'trackShortLabel'
    _PROJECT_DATA_FILE_KEY_TRACK_LONG_LABEL = 'trackLongLabel'
    _PROJECT_DATA_FILE_KEY_TRACK_SPECIES = 'trackSpecie'
    _PROJECT_DATA_FILE_KEY_TRACK_POGO_FILE_PATH = 'pogoFile'

    def __init__(self, project_track_descriptor_object):
        self.__project_track_descriptor_object = project_track_descriptor_object

    def _get_value_for_key(self, key, default=""):
        if self.__project_track_descriptor_object and (key in self.__project_track_descriptor_object):
            return self.__project_track_descriptor_object[key]
        return default

    def get_track_name(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACK_NAME)

    def get_track_short_label(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACK_SHORT_LABEL)

    def get_track_long_label(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACK_LONG_LABEL)

    def get_track_species(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACK_SPECIES)

    def get_track_file_path_pogo(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACK_POGO_FILE_PATH)

class ProjectTrackhubDescriptor:
    # Project Data File keys
    _PROJECT_DATA_FILE_KEY_TRACKHUB_NAME = 'trackHubName'
    _PROJECT_DATA_FILE_KEY_TRACKHUB_SHORT_LABEL = 'trackHubShortLabel'
    _PROJECT_DATA_FILE_KEY_TRACKHUB_LONG_LABEL = 'trackHubLongLabel'
    _PROJECT_DATA_FILE_KEY_TRACKHUB_HUB_TYPE = 'trackHubType'
    _PROJECT_DATA_FILE_KEY_TRACKHUB_EMAIL = 'trackHubEmail'
    _PROJECT_DATA_FILE_KEY_TRACKHUB_INTERNAL_ABSOLUTE_PATH = 'trackHubInternalAbsolutePath'
    _PROJECT_DATA_FILE_KEY_TRACKHUB_REPORT_FILE = 'TrackhubCreationReportFilePath'
    _PROJECT_DATA_FILE_KEY_TRACKHUB_SECTION_TRACKMAPS = 'trackMaps'

    def __init__(self, project_data_file_path):
        self.__project_data_file_path = project_data_file_path
        self.__project_data_object = None
        self.__project_tracks_descriptors = None

    def _get_project_data_object(self):
        if not self.__project_data_object:
            self.__project_data_object = general_toolbox.read_json(self.__project_data_file_path)
        return self.__project_data_object

    def _get_value_for_key(self, key, default=""):
        # TODO - I should start thinking about refactoring this out
        if key in self._get_project_data_object():
            return self._get_project_data_object()[key]
        return default

    def get_trackhub_name(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACKHUB_NAME,
                                       os.path.basename(self.__project_data_file_path))

    def get_trackhub_short_label(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACKHUB_SHORT_LABEL,
                                       "--- NO SHORT LABEL HAS BEEN DEFINED FOR THIS TRACKHUB ---")

    def get_trackhub_long_label(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACKHUB_LONG_LABEL,
                                       "--- NO LONG LABEL HAS BEEN DEFINED FOR THIS TRACKHUB ---")

    def get_trackhub_hub_type(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACKHUB_HUB_TYPE,
                                       "PROTEOMICS")

    def get_trackhub_email(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACKHUB_EMAIL,
                                       "pride-support@ebi.ac.uk")

    def get_trackhub_destination_path(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACKHUB_INTERNAL_ABSOLUTE_PATH)

    def get_trackhub_project_defined_tracks(self):
        if not self.__project_tracks_descriptors:
            # Default value is an empty list of tracks
            self.__project_tracks_descriptors = []
            data_file_project_track_description_objects = \
                self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACKHUB_SECTION_TRACKMAPS)
            if data_file_project_track_description_objects:
                self.__project_tracks_descriptors = \
                    [ProjectTrackDescriptor(data_file_project_track_description_object)
                     for data_file_project_track_description_object
                     in data_file_project_track_description_objects]
        return self.__project_tracks_descriptors

    def get_trackhub_report_file_path(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACKHUB_REPORT_FILE)

class PipelineResult:
    # TODO
    _VALUE_STATUS_SUCCESS = 'SUCCESS'
    _VALUE_STATUS_ERROR = 'ERROR'

    def __init__(self):
        self.status = self._VALUE_STATUS_SUCCESS
        self.error_messages = []
        self.success_messages = []
        self.hub_descriptor_file_path = ""
        # Absolute file path to the folder that represents the running session of the pipeline
        self.file_path_pipeline_session = ""
        # Absolute file path to the log files that belong to the running session of the pipeline
        self.file_path_log_files = []

    def set_status_error(self):
        self.status = self._VALUE_STATUS_ERROR

    def add_error_message(self, error_message):
        # This is the report on the final result from running the pipeline
        self.set_status_error()
        self.error_messages.append(error_message)

    def add_success_message(self, success_message):
        self.success_messages.append(success_message)

    def add_log_files(self, log_files):
        self.file_path_log_files.extend(log_files)

    def __str__(self):
        return json.dumps({'status': self.status,
                           'success_messages': self.success_messages,
                           'error_messages': self.error_messages,
                           'hub_descriptor_file_path': self.hub_descriptor_file_path,
                           "pipeline_session_working_dir": self.file_path_pipeline_session,
                           "log_files": self.file_path_log_files})

class TrackhubCreatorForProject(PogoBasedPipelineDirector):
    # TODO
    def __init__(self, configuration_object, configuration_file, pipeline_arguments):
        runner_id = "{}-{}".format(__name__, time.time())
        super(TrackhubCreatorForProject, self).__init__(runner_id)
        self.__config_manager = ConfigManager(configuration_object, configuration_file, pipeline_arguments)
        self.__project_trackhub_descriptor = None
        # Only the valid project tracks will be processed for being included in the trackhub
        self.__valid_project_tracks = None
        # Pipeline result object
        self.__pipeline_result_object = PipelineResult()

    def __get_valid_project_tracks(self):
        """
        This helper creates a list of valid trackhub tracks from the given project, i.e. tracks that meet this cirteria:
            - Its taxonomy ID is available on Ensembl
        :return: a list of valid trackhub tracks for the given project
        """
        if not self.__valid_project_tracks:
            self.__valid_project_tracks = []
            ensembl_service = ensembl.service.get_service()
            for project_track_descriptor in self.__project_trackhub_descriptor.get_trackhub_project_defined_tracks():
                if ensembl_service.get_species_data_service().get_species_entry_for_taxonomy_id(
                        project_track_descriptor.get_track_species()):
                    self.__valid_project_tracks.append(project_track_descriptor)
        return self.__valid_project_tracks

    def _before(self):
        # Set Pipeline Session working directory
        self.__pipeline_result_object.file_path_pipeline_session = \
            config_manager.get_app_config_manager().get_session_working_dir()
        # Add this pipeline session log files to the final report
        self.__pipeline_result_object.add_log_files(config_manager.get_app_config_manager().get_session_log_files())
        if self.__config_manager.get_project_data_file_path():
            self._get_logger().info("Reading Project Trackhub Descriptor from file at '{}'"
                                    .format(self.__config_manager.get_project_data_file_path()))
            self.__project_trackhub_descriptor = \
                ProjectTrackhubDescriptor(self.__config_manager.get_project_data_file_path())
            # Check that the destination folder exists
            if not os.path.isdir(self.__project_trackhub_descriptor.get_trackhub_destination_path()):
                error_message = "Trackhub destination path NOT VALID, '{}'"\
                    .format(self.__project_trackhub_descriptor.get_trackhub_destination_path())
                self._get_logger().error(error_message)
                self.__pipeline_result_object.add_error_message(error_message)
                return False
            # Check valid project tracks
            if not self.__get_valid_project_tracks():
                # It makes no sense to go ahead if this project has no valid tracks
                error_message = "Project Trackhub contains NO VALID TRACKS"
                self._get_logger().error(error_message)
                self.__pipeline_result_object.add_error_message(error_message)
                return False
            return True
        error_message = "INVALID / MISSING Project Trackhub Descriptor file, '{}'"\
            .format(self.__config_manager.get_project_data_file_path())
        self._get_logger().error(error_message)
        self.__pipeline_result_object.add_error_message(error_message)
        return False

    def _run_pipeline(self):
        # TODO
        pass

    def _after(self):
        if not self.is_pipeline_status_ok():
            self._get_logger().warning("This Pipeline is finishing with NON-OK status.")
        report_files = [self.__config_manager.get_file_path_trackhub_creation_report()]
        if self.__project_trackhub_descriptor.get_trackhub_report_file_path():
            report_files.append(self.__project_trackhub_descriptor.get_trackhub_report_file_path())
        for report_file in report_files:
            self._get_logger().info("Dumping Pipeline Report to '{}'".format(report_file))
            with open(report_file, 'w') as f:
                f.write(str(self.__pipeline_result_object))

if __name__ == '__main__':
    print("ERROR: This script is part of a pipeline collection and it is not meant to be run in stand alone mode")
<commit_msg>set pipeline status as 'fail' when the preparation stage fails<commit_after>#
# Author    : Manuel Bernal Llinares
# Project   : trackhub-creator
# Timestamp : 07-09-2017 11:24
# ---
# © 2017 Manuel Bernal Llinares <mbdebian@gmail.com>
# All rights reserved.
#

"""
This pipeline creates a trackhub for a PRIDE project, based on the information provided via a JSON formatted file, as it
can be seen on this sample:
{
  "trackHubName" : "PXD000625",
  "trackHubShortLabel" : "PXD000625 - Hepatoc...",
  "trackHubLongLabel" : "Experimental design For the label-free ...",
  "trackHubType" : "PROTEOMICS",
  "trackHubEmail" : "pride-support@ebi.ac.uk",
  "trackHubInternalAbsolutePath" : "...",
  "TrackhubCreationReportFilePath": "...",
  "trackMaps" : [ {
    "trackName" : "PXD000625_10090_Original",
    "trackShortLabel" : "PXD000625 - Mus musc...",
    "trackLongLabel" : "Experimental design For the label-free proteome analysis 17 mice were used composed of 5 ...",
    "trackSpecie" : "10090",
    "pogoFile" : "..."
  } ]
}
"""

import os
import json
import time
# App imports
import config_manager
import ensembl.service
import toolbox.general as general_toolbox
from pipelines.template_pipeline import PogoBasedPipelineDirector, DirectorConfigurationManager

# Globals
__configuration_file = None
__pipeline_arguments = None
__pipeline_director = None

# Pipeline properties access
def set_configuration_file(config_file):
    global __configuration_file
    if __configuration_file is None:
        __configuration_file = config_file
    return __configuration_file

def set_pipeline_arguments(pipeline_arguments):
    global __pipeline_arguments
    if __pipeline_arguments is None:
        __pipeline_arguments = pipeline_arguments
    return __pipeline_arguments

def get_pipeline_director():
    global __pipeline_director
    if __pipeline_director is None:
        __pipeline_director = TrackhubCreatorForProject(config_manager.read_config_from_file(__configuration_file),
                                                        __configuration_file,
                                                        __pipeline_arguments)
    return __pipeline_director

class ConfigManager(DirectorConfigurationManager):
    # Command Line Arguments for this pipeline look like
    #   # This is a JSON formatted file that contains all the relevant information needed for processing the project
    #   # data and create its trackhub
    #   project_data_file=project_data.json

    # Command Line Argument keys
    _CONFIG_COMMAND_LINE_ARGUMENT_KEY_PROJECT_DATA_FILE = 'project_data_file'

    def __init__(self, configuration_object, configuration_file, pipeline_arguments):
        super(ConfigManager, self).__init__(configuration_object, configuration_file, pipeline_arguments)
        # Lazy Process command line arguments
        self.__pipeline_arguments_object = None
        self.__running_mode = None

    def get_project_data_file_path(self):
        return self._get_value_for_pipeline_argument_key(self._CONFIG_COMMAND_LINE_ARGUMENT_KEY_PROJECT_DATA_FILE)

    def get_file_path_trackhub_creation_report(self):
        return os.path.join(config_manager.get_app_config_manager().get_session_working_dir(),
                            "trackhub_creation.report")

# Models for dealing with the data file that describes the project
class ProjectTrackDescriptor:
    # Project Data File keys relative to every TrackMap object
    _PROJECT_DATA_FILE_KEY_TRACK_NAME = 'trackName'
    _PROJECT_DATA_FILE_KEY_TRACK_SHORT_LABEL = 'trackShortLabel'
    _PROJECT_DATA_FILE_KEY_TRACK_LONG_LABEL = 'trackLongLabel'
    _PROJECT_DATA_FILE_KEY_TRACK_SPECIES = 'trackSpecie'
    _PROJECT_DATA_FILE_KEY_TRACK_POGO_FILE_PATH = 'pogoFile'

    def __init__(self, project_track_descriptor_object):
        self.__project_track_descriptor_object = project_track_descriptor_object

    def _get_value_for_key(self, key, default=""):
        if self.__project_track_descriptor_object and (key in self.__project_track_descriptor_object):
            return self.__project_track_descriptor_object[key]
        return default

    def get_track_name(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACK_NAME)

    def get_track_short_label(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACK_SHORT_LABEL)

    def get_track_long_label(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACK_LONG_LABEL)

    def get_track_species(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACK_SPECIES)

    def get_track_file_path_pogo(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACK_POGO_FILE_PATH)

class ProjectTrackhubDescriptor:
    # Project Data File keys
    _PROJECT_DATA_FILE_KEY_TRACKHUB_NAME = 'trackHubName'
    _PROJECT_DATA_FILE_KEY_TRACKHUB_SHORT_LABEL = 'trackHubShortLabel'
    _PROJECT_DATA_FILE_KEY_TRACKHUB_LONG_LABEL = 'trackHubLongLabel'
    _PROJECT_DATA_FILE_KEY_TRACKHUB_HUB_TYPE = 'trackHubType'
    _PROJECT_DATA_FILE_KEY_TRACKHUB_EMAIL = 'trackHubEmail'
    _PROJECT_DATA_FILE_KEY_TRACKHUB_INTERNAL_ABSOLUTE_PATH = 'trackHubInternalAbsolutePath'
    _PROJECT_DATA_FILE_KEY_TRACKHUB_REPORT_FILE = 'TrackhubCreationReportFilePath'
    _PROJECT_DATA_FILE_KEY_TRACKHUB_SECTION_TRACKMAPS = 'trackMaps'

    def __init__(self, project_data_file_path):
        self.__project_data_file_path = project_data_file_path
        self.__project_data_object = None
        self.__project_tracks_descriptors = None

    def _get_project_data_object(self):
        if not self.__project_data_object:
            self.__project_data_object = general_toolbox.read_json(self.__project_data_file_path)
        return self.__project_data_object

    def _get_value_for_key(self, key, default=""):
        # TODO - I should start thinking about refactoring this out
        if key in self._get_project_data_object():
            return self._get_project_data_object()[key]
        return default

    def get_trackhub_name(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACKHUB_NAME,
                                       os.path.basename(self.__project_data_file_path))

    def get_trackhub_short_label(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACKHUB_SHORT_LABEL,
                                       "--- NO SHORT LABEL HAS BEEN DEFINED FOR THIS TRACKHUB ---")

    def get_trackhub_long_label(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACKHUB_LONG_LABEL,
                                       "--- NO LONG LABEL HAS BEEN DEFINED FOR THIS TRACKHUB ---")

    def get_trackhub_hub_type(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACKHUB_HUB_TYPE,
                                       "PROTEOMICS")

    def get_trackhub_email(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACKHUB_EMAIL,
                                       "pride-support@ebi.ac.uk")

    def get_trackhub_destination_path(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACKHUB_INTERNAL_ABSOLUTE_PATH)

    def get_trackhub_project_defined_tracks(self):
        if not self.__project_tracks_descriptors:
            # Default value is an empty list of tracks
            self.__project_tracks_descriptors = []
            data_file_project_track_description_objects = \
                self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACKHUB_SECTION_TRACKMAPS)
            if data_file_project_track_description_objects:
                self.__project_tracks_descriptors = \
                    [ProjectTrackDescriptor(data_file_project_track_description_object)
                     for data_file_project_track_description_object
                     in data_file_project_track_description_objects]
        return self.__project_tracks_descriptors

    def get_trackhub_report_file_path(self):
        return self._get_value_for_key(self._PROJECT_DATA_FILE_KEY_TRACKHUB_REPORT_FILE)

class PipelineResult:
    # TODO
    _VALUE_STATUS_SUCCESS = 'SUCCESS'
    _VALUE_STATUS_ERROR = 'ERROR'

    def __init__(self):
        self.status = self._VALUE_STATUS_SUCCESS
        self.error_messages = []
        self.success_messages = []
        self.hub_descriptor_file_path = ""
        # Absolute file path to the folder that represents the running session of the pipeline
        self.file_path_pipeline_session = ""
        # Absolute file path to the log files that belong to the running session of the pipeline
        self.file_path_log_files = []

    def set_status_error(self):
        self.status = self._VALUE_STATUS_ERROR

    def add_error_message(self, error_message):
        # This is the report on the final result from running the pipeline
        self.set_status_error()
        self.error_messages.append(error_message)

    def add_success_message(self, success_message):
        self.success_messages.append(success_message)

    def add_log_files(self, log_files):
        self.file_path_log_files.extend(log_files)

    def __str__(self):
        return json.dumps({'status': self.status,
                           'success_messages': self.success_messages,
                           'error_messages': self.error_messages,
                           'hub_descriptor_file_path': self.hub_descriptor_file_path,
                           "pipeline_session_working_dir": self.file_path_pipeline_session,
                           "log_files": self.file_path_log_files})

class TrackhubCreatorForProject(PogoBasedPipelineDirector):
    # TODO
    def __init__(self, configuration_object, configuration_file, pipeline_arguments):
        runner_id = "{}-{}".format(__name__, time.time())
        super(TrackhubCreatorForProject, self).__init__(runner_id)
        self.__config_manager = ConfigManager(configuration_object, configuration_file, pipeline_arguments)
        self.__project_trackhub_descriptor = None
        # Only the valid project tracks will be processed for being included in the trackhub
        self.__valid_project_tracks = None
        # Pipeline result object
        self.__pipeline_result_object = PipelineResult()

    def __get_valid_project_tracks(self):
        """
        This helper creates a list of valid trackhub tracks from the given project, i.e. tracks that meet this cirteria:
            - Its taxonomy ID is available on Ensembl
        :return: a list of valid trackhub tracks for the given project
        """
        if not self.__valid_project_tracks:
            self.__valid_project_tracks = []
            ensembl_service = ensembl.service.get_service()
            for project_track_descriptor in self.__project_trackhub_descriptor.get_trackhub_project_defined_tracks():
                if ensembl_service.get_species_data_service().get_species_entry_for_taxonomy_id(
                        project_track_descriptor.get_track_species()):
                    self.__valid_project_tracks.append(project_track_descriptor)
        return self.__valid_project_tracks

    def _before(self):
        # Set Pipeline Session working directory
        self.__pipeline_result_object.file_path_pipeline_session = \
            config_manager.get_app_config_manager().get_session_working_dir()
        # Add this pipeline session log files to the final report
        self.__pipeline_result_object.add_log_files(config_manager.get_app_config_manager().get_session_log_files())
        if self.__config_manager.get_project_data_file_path():
            self._get_logger().info("Reading Project Trackhub Descriptor from file at '{}'"
                                    .format(self.__config_manager.get_project_data_file_path()))
            self.__project_trackhub_descriptor = \
                ProjectTrackhubDescriptor(self.__config_manager.get_project_data_file_path())
            # Check that the destination folder exists
            if not os.path.isdir(self.__project_trackhub_descriptor.get_trackhub_destination_path()):
                error_message = "Trackhub destination path NOT VALID, '{}'"\
                    .format(self.__project_trackhub_descriptor.get_trackhub_destination_path())
                self._get_logger().error(error_message)
                self.__pipeline_result_object.add_error_message(error_message)
                self.set_pipeline_status_fail()
                return False
            # Check valid project tracks
            if not self.__get_valid_project_tracks():
                # It makes no sense to go ahead if this project has no valid tracks
                error_message = "Project Trackhub contains NO VALID TRACKS"
                self._get_logger().error(error_message)
                self.__pipeline_result_object.add_error_message(error_message)
                self.set_pipeline_status_fail()
                return False
            return True
        error_message = "INVALID / MISSING Project Trackhub Descriptor file, '{}'"\
            .format(self.__config_manager.get_project_data_file_path())
        self._get_logger().error(error_message)
        self.__pipeline_result_object.add_error_message(error_message)
        self.set_pipeline_status_fail()
        return False

    def _run_pipeline(self):
        # TODO
        pass

    def _after(self):
        if not self.is_pipeline_status_ok():
            self._get_logger().warning("This Pipeline is finishing with NON-OK status.")
        report_files = [self.__config_manager.get_file_path_trackhub_creation_report()]
        if self.__project_trackhub_descriptor.get_trackhub_report_file_path():
            report_files.append(self.__project_trackhub_descriptor.get_trackhub_report_file_path())
        for report_file in report_files:
            self._get_logger().info("Dumping Pipeline Report to '{}'".format(report_file))
            with open(report_file, 'w') as f:
                f.write(str(self.__pipeline_result_object))

if __name__ == '__main__':
    print("ERROR: This script is part of a pipeline collection and it is not meant to be run in stand alone mode")
<|endoftext|>


<commit_before>"""Cinder URLconf."""
# Copyright 2014 - 2015 Solinea, Inc.
#
# Licensed under the Solinea Software License Agreement (goldstone),
# Version 1.0 (the "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at:
#
#     http://www.solinea.com/goldstone/LICENSE.pdf
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expressed or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from django.conf.urls import patterns, url
from rest_framework.routers import DefaultRouter
from .views import ReportView, VolumesDataViewSet, \
    BackupsDataViewSet, SnapshotsDataViewSet, ServicesDataViewSet, \
    VolumeTypesDataViewSet, TransfersDataViewSet

# Views handled by DjangoRestFramework ViewSets.
router = DefaultRouter(trailing_slash=False)
router.register(r'^backups[/]?$',
                BackupsDataViewSet,
                base_name='cinder-backups')
router.register(r'^services[/]?$',
                ServicesDataViewSet,
                base_name='cinder-services')
router.register(r'^snapshots[/]?$',
                SnapshotsDataViewSet,
                base_name='cinder-snapshots')
router.register(r'^transfers[/]?$',
                TransfersDataViewSet,
                base_name='cinder-transfers')
router.register(r'^volumes[/]?$',
                VolumesDataViewSet,
                base_name='cinder-volumes')
router.register(r'^volume_types[/]?$',
                VolumeTypesDataViewSet,
                base_name='cinder-volume-types')

urlpatterns = router.urls

# Other views.
urlpatterns += patterns('',
                        url(r'^report[/]?$',
                            ReportView.as_view(),
                            name='cinder-report-view'),
                        )
<commit_msg>GOLD-717: Remove ReportView from cinder.urls.<commit_after>"""Cinder URLconf."""
# Copyright 2014 - 2015 Solinea, Inc.
#
# Licensed under the Solinea Software License Agreement (goldstone),
# Version 1.0 (the "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at:
#
#     http://www.solinea.com/goldstone/LICENSE.pdf
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expressed or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from rest_framework.routers import DefaultRouter
from .views import VolumesDataViewSet, \
    BackupsDataViewSet, SnapshotsDataViewSet, ServicesDataViewSet, \
    VolumeTypesDataViewSet, TransfersDataViewSet

# Views handled by DjangoRestFramework ViewSets.
router = DefaultRouter(trailing_slash=False)
router.register(r'^backups[/]?$',
                BackupsDataViewSet,
                base_name='cinder-backups')
router.register(r'^services[/]?$',
                ServicesDataViewSet,
                base_name='cinder-services')
router.register(r'^snapshots[/]?$',
                SnapshotsDataViewSet,
                base_name='cinder-snapshots')
router.register(r'^transfers[/]?$',
                TransfersDataViewSet,
                base_name='cinder-transfers')
router.register(r'^volumes[/]?$',
                VolumesDataViewSet,
                base_name='cinder-volumes')
router.register(r'^volume_types[/]?$',
                VolumeTypesDataViewSet,
                base_name='cinder-volume-types')

urlpatterns = router.urls
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
###############################################################################
#
# ODOO (ex OpenERP)
# Open Source Management Solution
# Copyright (C) 2001-2015 Micronaet S.r.l. (<http://www.micronaet.it>)
# Developer: Nicola Riolini @thebrush (<https://it.linkedin.com/in/thebrush>)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################

import os
import sys
import logging
import openerp
import dbf
import openerp.netsvc as netsvc
import openerp.addons.decimal_precision as dp
from openerp.osv import fields, osv, expression, orm
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from openerp import SUPERUSER_ID, api
from openerp import tools
from openerp.tools.translate import _
from openerp.tools.float_utils import float_round as round
from openerp.tools import (DEFAULT_SERVER_DATE_FORMAT,
    DEFAULT_SERVER_DATETIME_FORMAT,
    DATETIME_FORMATS_MAP,
    float_compare)

_logger = logging.getLogger(__name__)

class EasyLabelPurchaseWizard(orm.TransientModel):
    '''Wizard for export the list of label to be lauched on Easy Label PC
    '''
    _name = 'easylabel.purchase.wizard'

    # -------------
    # Event button:
    # -------------

    def export_label(self, cr, uid, ids, context=None):
        ''' This event generate the batch command to be launched from Easy
            label PC, after generate the command to print all the label in
            correct order.
            After this the label employee have to launch the link on his
            desktop PC
        '''
        # ---------------------------------------------------------------------
        #                               Pool used
        # ---------------------------------------------------------------------
        easylabel_pool = self.pool.get('easylabel.easylabel')
        po_pool = self.pool.get('purchase.order')

        wiz_proxy = self.browse(cr, uid, ids, context=context)[0]

        # ---------------------------------------------------------------------
        #                           Start setup file
        # ---------------------------------------------------------------------
        root_path = wiz_proxy.label_id.root_id.local_path

        # file name:
        cmd_file = os.path.expanduser(
            os.path.join(root_path, "purchase.cmd")) # Label
        bat_file =  os.path.expanduser(
            os.path.join(root_path, "purchase.bat")) # Command
        dbf_file =  os.path.expanduser(
            os.path.join(root_path, "purchase.dbf")) # Database

        # file handle:
        label_file = open(cmd_file, "w")
        batch_file = open(bat_file, 'w')
        # TODO choose correct field:
        table = dbf.Table(dbf_file, # TODO correct?
           'code C(18); desc C(80); colour C(80); ean C(12); imagine C(100); '
           'pack C(10); piece C(10); order C(30)')
        table.open()

        # ---------------------
        # Populate with labels:
        # ---------------------
        po_id = context.get('active_id', False) # TODO check!
        po_proxy = po_pool.browse(cr, uid, po_id, context=context)
        parameters = {} # used for merge in the label (postprocessor)
        i = 1
        label_to_print = '' # TODO for comunicate in batch
        for item in po_proxy.order_line: # loop on all lines:

            if not item.product_id:
                continue # jump line witout product

            # Add record in database:
            default_code = item.product_id.default_code
            # TODO chose correct field:
            ean = item.product_id.ean13[:12] if item.product_id.ean13 else ''
            name = item.product_id.name.split("] ")[-1]
            if default_code == 'SBEASECTNBI--S':
                import pdb; pdb.set_trace()
            try:
                colls = int(element.product_id.colls) or 1
            except:
                colls = 1
            try:
                q_x_pack = int(element.product_id.q_x_pack or '1')
            except:
                q_x_pack = 1

            # Write record in database:
            table.append((
                default_code,
                name,
                item.product_id.colour,
                ean,
                'C:\Immagini\%s.jpg' % default_code,
                str(colls or 0), # Pack TODO not used, remove!!! (also in label)
                str(q_x_pack), # Pieces
                item.order_id.name,
                ))

            # Windows path:
            path_label = "%s\\%s\\%s" % (
                wiz_proxy.label_id.path_id,
                wiz_proxy.label_id.folder,
                wiz_proxy.label_id.label_name[:-4],
                )

            # Loop on label for colls:
            for part in range(1, colls + 1):
                # Parameter to pass during print (cmd file):
                partno = "%s/%s" % (part, colls)
                parameters['code'] = default_code
                parameters['partno'] = partno
                # TODO need other params?

                label_file.write(
                    "formatname=\"%s\"\r\n" % (
                        path_label.replace("\\\\", "\\")))

                # TODO check the total of label to print
                tot_label = 1 if wiz_proxy.test else int(item.product_qty)
                label_file.write("formatcount=%s\r\n" % tot_label)

                if i == 1 and part == 1: # first loop first part number
                   label_file.write("testprint=off\r\n") # only one time

                # -----------
                # Parameters:
                # -----------
                for param in wiz_proxy.label_id.parameter_ids:
                    if param.mode == 'static': # currently not used but leaved!
                       label_file.write("%s=\"%s\"\r\n" % (
                           param.name, param.value))
                    else: # dynamic
                       if param.mode_type in parameters and parameters[
                               param.mode_type]: # test if there is parameter
                           label_file.write("%s=\"%s\"\r\n" % (
                               param.name, parameters[param.mode_type]
                               ))
                       else: # error param. not present!! erase not waste label
                          error.append( # TODO raise one time or collect
                              "Parameter not found: %s" % param.mode_type)

                # Be carefull: works with printer_id, write printer.number
                label_file.write(
                    "useprinter=%d\r\n" % wiz_proxy.printer_id.number)

                label_file.write(
                    _("jobdescription=\"Order: %s (%s Part: %s)\"\r\n") % (
                        po_proxy.name, default_code, partno))

                if i == 1 and part == 1:
                    label_file.write("singlejob=on\r\n") # only one time

                label_file.write(";\r\n") # end of record label
                label_to_print += "@echo Code %s  -  Tot. %s (%s) %s\r\n" % (
                    default_code,
                    tot_label,
                    partno,
                    '*test' if wiz_proxy.test else '',
                    )
                i += 1
        label_file.close()
        table.close()

        # ----------------------------
        # Create batch file to launch:
        # ----------------------------
        param_ids = easylabel_pool.search(
            cr, uid, [], context=context)
        if not param_ids:
           error.append('Error, Some parameter not found!')
           raise osv.except_osv("Error", error)
           return False

        param_proxy = easylabel_pool.browse(
            cr, uid, param_ids, context=context)
        print_comment = ("%s [%s]" % (
            po_proxy.name, po_proxy.partner_id.name)
            ).replace('\'', '').replace('"', '')

        # TODO parametrize mapped drive and print message
        batch_file.write(
            "@echo off\r\n" \
            "@net use o: %s /persistent:yes\r\n" % param_proxy[
                0].oerp_command + \
            "@xcopy o:\purchase.cmd \"%s\" /y\r\n" % (param_proxy[
                0].path, ) + \
            "@del \"%s\\purchase.ixl\"\r\n" % (param_proxy[
                0].path, ) + \
            "@xcopy o:\purchase.dbf \"%s\" /y\r\n" % (param_proxy[
                0].path, ) + \
            "@cd \"%s\"\r\n" %(param_proxy[
                0].path, ) + \
            "@cls\r\n" + \
            _("@echo Start printing... %s\r\n") % print_comment + \
            _("@echo Print note: %s\r\n") % (
                wiz_proxy.note or _('No note!'), ) + \
            _("@echo Job detail:\r\n%s\r\n") % label_to_print + \
            "@pause\r\n" + \
            "@\"%s\\%s\" purchase\r\n" % (
                param_proxy[0].path,
                param_proxy[0].command,
                ))
        batch_file.close()
        return True

    _columns = {
        'label_id': fields.many2one('easylabel.label', 'Label', required=True,
            domain=[('area', '=', 'purchase')]),
        'printer_id': fields.many2one('easylabel.printer', 'Printer',
            required=True),
        'test': fields.boolean('Print test',
            help='The print test launch a batch job with 1 label for all'),
        'note': fields.text('Note', help='Note for label employee'),
        'run_note': fields.text('Procedure', readonly=True,
            help='Help user with the procedure'),
        }
    _defaults = {
        'test': lambda *x: False,
        'run_note': lambda *x: '''

Export label procedure:

               Choose the purchase label for print this order and the default
               printer to use, after export all the command file with the
               button.

               To print the label run the batch on print workstation (destkop)
               link, ensure that the printer correct is open and with the right
               labels.

            ''',
        }
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<commit_msg>bugfix<commit_after># -*- coding: utf-8 -*-
###############################################################################
#
# ODOO (ex OpenERP)
# Open Source Management Solution
# Copyright (C) 2001-2015 Micronaet S.r.l. (<http://www.micronaet.it>)
# Developer: Nicola Riolini @thebrush (<https://it.linkedin.com/in/thebrush>)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################

import os
import sys
import logging
import openerp
import dbf
import openerp.netsvc as netsvc
import openerp.addons.decimal_precision as dp
from openerp.osv import fields, osv, expression, orm
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from openerp import SUPERUSER_ID, api
from openerp import tools
from openerp.tools.translate import _
from openerp.tools.float_utils import float_round as round
from openerp.tools import (DEFAULT_SERVER_DATE_FORMAT,
    DEFAULT_SERVER_DATETIME_FORMAT,
    DATETIME_FORMATS_MAP,
    float_compare)

_logger = logging.getLogger(__name__)

class EasyLabelPurchaseWizard(orm.TransientModel):
    '''Wizard for export the list of label to be lauched on Easy Label PC
    '''
    _name = 'easylabel.purchase.wizard'

    # -------------
    # Event button:
    # -------------

    def export_label(self, cr, uid, ids, context=None):
        ''' This event generate the batch command to be launched from Easy
            label PC, after generate the command to print all the label in
            correct order.
            After this the label employee have to launch the link on his
            desktop PC
        '''
        # ---------------------------------------------------------------------
        #                               Pool used
        # ---------------------------------------------------------------------
        easylabel_pool = self.pool.get('easylabel.easylabel')
        po_pool = self.pool.get('purchase.order')

        wiz_proxy = self.browse(cr, uid, ids, context=context)[0]

        # ---------------------------------------------------------------------
        #                           Start setup file
        # ---------------------------------------------------------------------
        root_path = wiz_proxy.label_id.root_id.local_path

        # file name:
        cmd_file = os.path.expanduser(
            os.path.join(root_path, "purchase.cmd")) # Label
        bat_file =  os.path.expanduser(
            os.path.join(root_path, "purchase.bat")) # Command
        dbf_file =  os.path.expanduser(
            os.path.join(root_path, "purchase.dbf")) # Database

        # file handle:
        label_file = open(cmd_file, "w")
        batch_file = open(bat_file, 'w')
        # TODO choose correct field:
        table = dbf.Table(dbf_file, # TODO correct?
           'code C(18); desc C(80); colour C(80); ean C(12); imagine C(100); '
           'pack C(10); piece C(10); order C(30)')
        table.open()

        # ---------------------
        # Populate with labels:
        # ---------------------
        po_id = context.get('active_id', False) # TODO check!
        po_proxy = po_pool.browse(cr, uid, po_id, context=context)
        parameters = {} # used for merge in the label (postprocessor)
        i = 1
        label_to_print = '' # TODO for comunicate in batch
        for item in po_proxy.order_line: # loop on all lines:

            if not item.product_id:
                continue # jump line witout product

            # Add record in database:
            default_code = item.product_id.default_code
            # TODO chose correct field:
            ean = item.product_id.ean13[:12] if item.product_id.ean13 else ''
            name = item.product_id.name.split("] ")[-1]
            try:
                colls = int(item.product_id.colls) or 1
            except:
                colls = 1
            try:
                q_x_pack = int(item.product_id.q_x_pack) or 1
            except:
                q_x_pack = 1

            # Write record in database:
            table.append((
                default_code,
                name,
                item.product_id.colour,
                ean,
                'C:\Immagini\%s.jpg' % default_code,
                str(colls or 0), # Pack TODO not used, remove!!! (also in label)
                str(q_x_pack), # Pieces
                item.order_id.name,
                ))

            # Windows path:
            path_label = "%s\\%s\\%s" % (
                wiz_proxy.label_id.path_id,
                wiz_proxy.label_id.folder,
                wiz_proxy.label_id.label_name[:-4],
                )

            # Loop on label for colls:
            for part in range(1, colls + 1):
                # Parameter to pass during print (cmd file):
                partno = "%s/%s" % (part, colls)
                parameters['code'] = default_code
                parameters['partno'] = partno
                # TODO need other params?

                label_file.write(
                    "formatname=\"%s\"\r\n" % (
                        path_label.replace("\\\\", "\\")))

                # TODO check the total of label to print
                tot_label = 1 if wiz_proxy.test else int(item.product_qty)
                label_file.write("formatcount=%s\r\n" % tot_label)

                if i == 1 and part == 1: # first loop first part number
                   label_file.write("testprint=off\r\n") # only one time

                # -----------
                # Parameters:
                # -----------
                for param in wiz_proxy.label_id.parameter_ids:
                    if param.mode == 'static': # currently not used but leaved!
                       label_file.write("%s=\"%s\"\r\n" % (
                           param.name, param.value))
                    else: # dynamic
                       if param.mode_type in parameters and parameters[
                               param.mode_type]: # test if there is parameter
                           label_file.write("%s=\"%s\"\r\n" % (
                               param.name, parameters[param.mode_type]
                               ))
                       else: # error param. not present!! erase not waste label
                          error.append( # TODO raise one time or collect
                              "Parameter not found: %s" % param.mode_type)

                # Be carefull: works with printer_id, write printer.number
                label_file.write(
                    "useprinter=%d\r\n" % wiz_proxy.printer_id.number)

                label_file.write(
                    _("jobdescription=\"Order: %s (%s Part: %s)\"\r\n") % (
                        po_proxy.name, default_code, partno))

                if i == 1 and part == 1:
                    label_file.write("singlejob=on\r\n") # only one time

                label_file.write(";\r\n") # end of record label
                label_to_print += "@echo Code %s  -  Tot. %s (%s) %s\r\n" % (
                    default_code,
                    tot_label,
                    partno,
                    '*test' if wiz_proxy.test else '',
                    )
                i += 1
        label_file.close()
        table.close()

        # ----------------------------
        # Create batch file to launch:
        # ----------------------------
        param_ids = easylabel_pool.search(
            cr, uid, [], context=context)
        if not param_ids:
           error.append('Error, Some parameter not found!')
           raise osv.except_osv("Error", error)
           return False

        param_proxy = easylabel_pool.browse(
            cr, uid, param_ids, context=context)
        print_comment = ("%s [%s]" % (
            po_proxy.name, po_proxy.partner_id.name)
            ).replace('\'', '').replace('"', '')

        # TODO parametrize mapped drive and print message
        batch_file.write(
            "@echo off\r\n" \
            "@net use o: %s /persistent:yes\r\n" % param_proxy[
                0].oerp_command + \
            "@xcopy o:\purchase.cmd \"%s\" /y\r\n" % (param_proxy[
                0].path, ) + \
            "@del \"%s\\purchase.ixl\"\r\n" % (param_proxy[
                0].path, ) + \
            "@xcopy o:\purchase.dbf \"%s\" /y\r\n" % (param_proxy[
                0].path, ) + \
            "@cd \"%s\"\r\n" %(param_proxy[
                0].path, ) + \
            "@cls\r\n" + \
            _("@echo Start printing... %s\r\n") % print_comment + \
            _("@echo Print note: %s\r\n") % (
                wiz_proxy.note or _('No note!'), ) + \
            _("@echo Job detail:\r\n%s\r\n") % label_to_print + \
            "@pause\r\n" + \
            "@\"%s\\%s\" purchase\r\n" % (
                param_proxy[0].path,
                param_proxy[0].command,
                ))
        batch_file.close()
        return True

    _columns = {
        'label_id': fields.many2one('easylabel.label', 'Label', required=True,
            domain=[('area', '=', 'purchase')]),
        'printer_id': fields.many2one('easylabel.printer', 'Printer',
            required=True),
        'test': fields.boolean('Print test',
            help='The print test launch a batch job with 1 label for all'),
        'note': fields.text('Note', help='Note for label employee'),
        'run_note': fields.text('Procedure', readonly=True,
            help='Help user with the procedure'),
        }
    _defaults = {
        'test': lambda *x: False,
        'run_note': lambda *x: '''

Export label procedure:

               Choose the purchase label for print this order and the default
               printer to use, after export all the command file with the
               button.

               To print the label run the batch on print workstation (destkop)
               link, ensure that the printer correct is open and with the right
               labels.

            ''',
        }
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import(print_function)

__version__ = filter(str.isdigit, '$Revision: $')

import logging
import sys

class CRC8(object):
    """
    CRC8 algorithm
    """
    def __init__(self, polynomial=0x07):
        self.logger = logging.getLogger(__name__)
        self.logger.debug('__init__')

        self.polynomial = polynomial
        self.logger.debug('polynomial = 0x{:02x}'.format(self.polynomial))

        return

    def generate_table(self, output_file = sys.stdout, language = 'c'):
        self.logger.debug('generate_table({},{})'.format(repr(output_file),repr(language)))
        if language == 'c':
            print('const uint8_t crc8_polynomial = 0x{:02x}'.format(self.polynomial))
            print('const uint8_t crc8_table[] = {', file=output_file)
        elif language == 'python':
            print('CRC8_POLYNOMIAL = 0x{:02x}'.format(self.polynomial))
            print('CRC8_TABLE = {', file=output_file)

        for divident in range(0,256):
            current_byte = divident
            if divident % 8 == 0:
                if language == 'c':
                    output_file.write('                              ')
                elif language == 'python':
                    output_file.write('              ')
            for bit in range(0,8):
                if (current_byte & 0x80) != 0:
                    current_byte = current_byte << 1
                    current_byte = current_byte ^ self.polynomial
                else:
                    current_byte = current_byte << 1
                current_byte = current_byte & 0xff
            output_file.write('0x{:02x},'.format(current_byte))
            if divident % 8 == 7:
                output_file.write('\n')

        if language == 'c':
            print('                             };', file=output_file)
        elif language == 'python':
            print('             }', file=output_file)

    def calculate(self, data):
        self.logger.debug('calculate({})'.format(repr(data)))

if __name__ == '__main__':
    import argparse
    import random
    import time
    import packet

    parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
                                     description='Generate CRC8 Table')

    parser.add_argument('-f', '--file',
                        help='File to write the output to',
                        default=sys.stdout)
    parser.add_argument('-p', '--polynomial',
                        help='Polynomial to use in generating CRC8 table',
                        type=int,
                        default=0x07)
    parser.add_argument('-v', '--verbose',
                        help='Set verbosity level',
                        action='count')

    args = parser.parse_args()

    # Setup the logger
    logging.basicConfig(format='%(asctime)s: [%(levelname)-7s] %(name)-10s| %(message)s')
    root_logger = logging.getLogger()

    if args.verbose > 1:
        root_logger.setLevel(logging.DEBUG)
    elif args.verbose > 0:
        root_logger.setLevel(logging.INFO)

    crc8 = CRC8(args.polynomial)
    crc8.generate_table(args.file)
<commit_msg>Updated CRC8 to use generated table and calculate CRC<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import(print_function)

__version__ = filter(str.isdigit, '$Revision: $')

import logging
import sys

CRC8_POLYNOMIAL = 0x07
CRC8_TABLE = [
              0x00,0x07,0x0e,0x09,0x1c,0x1b,0x12,0x15,
              0x38,0x3f,0x36,0x31,0x24,0x23,0x2a,0x2d,
              0x70,0x77,0x7e,0x79,0x6c,0x6b,0x62,0x65,
              0x48,0x4f,0x46,0x41,0x54,0x53,0x5a,0x5d,
              0xe0,0xe7,0xee,0xe9,0xfc,0xfb,0xf2,0xf5,
              0xd8,0xdf,0xd6,0xd1,0xc4,0xc3,0xca,0xcd,
              0x90,0x97,0x9e,0x99,0x8c,0x8b,0x82,0x85,
              0xa8,0xaf,0xa6,0xa1,0xb4,0xb3,0xba,0xbd,
              0xc7,0xc0,0xc9,0xce,0xdb,0xdc,0xd5,0xd2,
              0xff,0xf8,0xf1,0xf6,0xe3,0xe4,0xed,0xea,
              0xb7,0xb0,0xb9,0xbe,0xab,0xac,0xa5,0xa2,
              0x8f,0x88,0x81,0x86,0x93,0x94,0x9d,0x9a,
              0x27,0x20,0x29,0x2e,0x3b,0x3c,0x35,0x32,
              0x1f,0x18,0x11,0x16,0x03,0x04,0x0d,0x0a,
              0x57,0x50,0x59,0x5e,0x4b,0x4c,0x45,0x42,
              0x6f,0x68,0x61,0x66,0x73,0x74,0x7d,0x7a,
              0x89,0x8e,0x87,0x80,0x95,0x92,0x9b,0x9c,
              0xb1,0xb6,0xbf,0xb8,0xad,0xaa,0xa3,0xa4,
              0xf9,0xfe,0xf7,0xf0,0xe5,0xe2,0xeb,0xec,
              0xc1,0xc6,0xcf,0xc8,0xdd,0xda,0xd3,0xd4,
              0x69,0x6e,0x67,0x60,0x75,0x72,0x7b,0x7c,
              0x51,0x56,0x5f,0x58,0x4d,0x4a,0x43,0x44,
              0x19,0x1e,0x17,0x10,0x05,0x02,0x0b,0x0c,
              0x21,0x26,0x2f,0x28,0x3d,0x3a,0x33,0x34,
              0x4e,0x49,0x40,0x47,0x52,0x55,0x5c,0x5b,
              0x76,0x71,0x78,0x7f,0x6a,0x6d,0x64,0x63,
              0x3e,0x39,0x30,0x37,0x22,0x25,0x2c,0x2b,
              0x06,0x01,0x08,0x0f,0x1a,0x1d,0x14,0x13,
              0xae,0xa9,0xa0,0xa7,0xb2,0xb5,0xbc,0xbb,
              0x96,0x91,0x98,0x9f,0x8a,0x8d,0x84,0x83,
              0xde,0xd9,0xd0,0xd7,0xc2,0xc5,0xcc,0xcb,
              0xe6,0xe1,0xe8,0xef,0xfa,0xfd,0xf4,0xf3,
             ]

class CRC8(object):
    """
    CRC8 algorithm
    """
    def __init__(self, polynomial=0x07):
        self.logger = logging.getLogger(__name__)
        self.logger.debug('__init__')

        self.polynomial = polynomial
        self.logger.debug('polynomial = 0x{:02x}'.format(self.polynomial))

        return

    def generate_table(self, output_file = sys.stdout, language = 'c'):
        self.logger.debug('generate_table({},{})'.format(repr(output_file),repr(language)))
        if language == 'c':
            print('const uint8_t crc8_polynomial = 0x{:02x}'.format(self.polynomial))
            print('const uint8_t crc8_table[] = {', file=output_file)
        elif language == 'python':
            print('CRC8_POLYNOMIAL = 0x{:02x}'.format(self.polynomial))
            print('CRC8_TABLE = [', file=output_file)

        for divident in range(0,256):
            current_byte = divident
            if divident % 8 == 0:
                if language == 'c':
                    output_file.write('                              ')
                elif language == 'python':
                    output_file.write('              ')
            for bit in range(0,8):
                if (current_byte & 0x80) != 0:
                    current_byte = current_byte << 1
                    current_byte = current_byte ^ self.polynomial
                else:
                    current_byte = current_byte << 1
                current_byte = current_byte & 0xff
            output_file.write('0x{:02x},'.format(current_byte))
            if divident % 8 == 7:
                output_file.write('\n')

        if language == 'c':
            print('                             };', file=output_file)
        elif language == 'python':
            print('             ]', file=output_file)

    def calculate(self, data):
        self.logger.debug('calculate({})'.format(repr(data)))
        if CRC8_POLYNOMIAL != self.polynomial:
            self.logger.error('CRC8 polynomial does not match generated table!')
            raise ValueError('CRC8 polynomial does not match generated table!')
        crc = 0
        for byte in bytearray(data):
            temp = byte ^ crc
            crc = CRC8_TABLE[temp]
        return crc

if __name__ == '__main__':
    import argparse
    import random
    import time
    import packet

    parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
                                     description='Generate CRC8 Table')

    parser.add_argument('-f', '--file',
                        help='File to write the output to',
                        default=sys.stdout)
    parser.add_argument('-d', '--data',
                        help='Data to calculate the CRC8 of',
                        default='123456789')
    parser.add_argument('-l', '--language',
                        help='Language to output in (c or python)',
                        default='c')
    parser.add_argument('-p', '--polynomial',
                        help='Polynomial to use in generating CRC8 table',
                        type=int,
                        default=0x07)
    parser.add_argument('-v', '--verbose',
                        help='Set verbosity level',
                        action='count')

    args = parser.parse_args()

    # Setup the logger
    logging.basicConfig(format='%(asctime)s: [%(levelname)-7s] %(name)-10s| %(message)s')
    root_logger = logging.getLogger()

    if args.verbose > 1:
        root_logger.setLevel(logging.DEBUG)
    elif args.verbose > 0:
        root_logger.setLevel(logging.INFO)

    crc8 = CRC8(args.polynomial)
    crc8.generate_table(args.file,args.language)

    print('crc8 of {} = 0x{:02x}'.format(repr(args.data),crc8.calculate(args.data)))
<|endoftext|>


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

from nose.tools import assert_true, assert_false, assert_raises
try:
    from nose.tools import assert_is_not, assert_is, assert_set_equal, assert_dict_equal
except ImportError:
    from landlab.testing.tools import (assert_is_not, assert_is, assert_set_equal,
                                       assert_dict_equal)
import numpy as np
from numpy.testing import assert_array_equal

from landlab.field import ModelDataFields, GroupError, FieldError

def test_init():
    fields = ModelDataFields()
    assert_set_equal(set(), fields.groups)

def test_new_field_location():
    fields = ModelDataFields()
    fields.new_field_location('node', 12)
    assert_set_equal(set(['node']), fields.groups)

def test_add_existing_group():
    fields = ModelDataFields()
    fields.new_field_location('node', size=12)
    assert_raises(ValueError, fields.new_field_location, 'node', size=24)

def test_add_multiple_groups():
    fields = ModelDataFields()
    fields.new_field_location('node', 12)
    fields.new_field_location('cell', 2)
    fields.new_field_location('face', 7)
    fields.new_field_location('link', 7)
    assert_set_equal(set(['node', 'cell', 'face', 'link']), fields.groups)

def test_ones():
    fields = ModelDataFields()
    fields.new_field_location('node', 12)
    fields.new_field_location('cell', 2)

    value_array = fields.ones('node')
    assert_array_equal(np.ones(12), value_array)

    value_array = fields.ones('cell')
    assert_array_equal(np.ones(2), value_array)

def test_add_ones():
    fields = ModelDataFields()
    fields.new_field_location('node', 12)
    fields.new_field_location('cell', 2)

    fields.add_ones('z', at='node')
    assert_array_equal(np.ones(12), fields['node']['z'])
    assert_array_equal(np.ones(12), fields.field_values('node', 'z'))

    fields.add_ones('z', at='cell')
    assert_array_equal(np.ones(2), fields['cell']['z'])
    assert_array_equal(np.ones(2), fields.field_values('cell', 'z'))

def test_add_ones_return_value():
    fields = ModelDataFields()
    fields.new_field_location('node', 12)
    fields.new_field_location('cell', 2)

    rtn_value = fields.add_ones('z', at='node')
    assert_array_equal(rtn_value, np.ones(12))
    assert_is(rtn_value, fields['node']['z'])
    assert_is(rtn_value, fields.field_values('node', 'z'))

    rtn_value = fields.add_ones('z', at='cell')
    assert_array_equal(rtn_value, np.ones(2))
    assert_is(rtn_value, fields['cell']['z'])
    assert_is(rtn_value, fields.field_values('cell', 'z'))

def test_add_existing_field_default():
    """Test default is to not replace existing field."""
    fields = ModelDataFields()
    fields.new_field_location('node', 12)
    fields.add_empty('z', at='node')

    assert_raises(FieldError, fields.add_empty, 'z', at='node')
    assert_raises(FieldError, fields.add_ones, 'z', at='node')
    assert_raises(FieldError, fields.add_zeros, 'z', at='node')

def test_add_existing_field_with_noclobber():
    """Test noclobber raises an error with an existing field."""
    fields = ModelDataFields()
    fields.new_field_location('node', 12)
    fields.add_empty('z', at='node')

    assert_raises(FieldError, fields.add_empty, 'z', at='node', noclobber=True)
    assert_raises(FieldError, fields.add_ones, 'z', at='node', noclobber=True)
    assert_raises(FieldError, fields.add_zeros, 'z', at='node', noclobber=True)

def test_add_field_with_noclobber():
    """Test noclobber does not raise an error with an new field."""
    fields = ModelDataFields()
    fields.new_field_location('node', 12)

    fields.add_empty('a', at='node', noclobber=True)
    assert_true('a' in fields['node'])

    fields.add_ones('b', at='node', noclobber=True)
    assert_true('b' in fields['node'])

    fields.add_zeros('c', at='node', noclobber=True)
    assert_true('c' in fields['node'])

def test_add_field_with_clobber():
    """Test adding a field with clobber on."""
    fields = ModelDataFields()
    fields.new_field_location('node', 12)

    assert_is_not(fields.add_empty('a', at='node'),
                  fields.add_empty('a', at='node', noclobber=False))
    assert_is_not(fields.add_ones('b', at='node'),
                  fields.add_ones('b', at='node', noclobber=False))
    assert_is_not(fields.add_zeros('c', at='node'),
                  fields.add_zeros('c', at='node', noclobber=False))

def test_getitem():
    fields = ModelDataFields()
    fields.new_field_location('node', 12)

    assert_dict_equal(dict(), fields['node'])
    assert_raises(GroupError, lambda k: fields[k], 'cell')
    assert_raises(KeyError, lambda k: fields[k], 'cell')

def test_at_attribute():
    fields = ModelDataFields()
    fields.new_field_location('node', 12)

    assert_dict_equal(dict(), fields.at_node)
    assert_raises(AttributeError, lambda: fields.at_cell)

    fields.add_ones('z', at='node')
    assert_array_equal(np.ones(12), fields.at_node['z'])

def test_has_group():
    fields = ModelDataFields()
    fields.new_field_location('node', 12)

    assert_true(fields.has_group('node'))
    assert_false(fields.has_group('cell'))

def test_delete_field():
    fields = ModelDataFields()
    fields.new_field_location('link', 17)

    assert_dict_equal(dict(), fields.at_link)
    assert_raises(AttributeError, lambda: fields.at_node)

    fields.add_zeros('link', 'vals')
    assert_array_equal(np.zeros(17), fields.at_link['vals'])

    assert_raises(KeyError, lambda: fields.delete_field('node', 'vals'))
    fields.delete_field('link', 'vals')
    assert_raises(KeyError, lambda: fields.field_units('link', 'vals'))
    assert_raises(KeyError, lambda: fields.at_link['vals'])

def test_scalar_field():
    fields = ModelDataFields()
    fields.new_field_location('all_over_the_place', 1)

    assert_dict_equal(dict(), fields.at_all_over_the_place)
    assert_raises(AttributeError, lambda: fields.at_cell)

    fields.at_all_over_the_place['const'] = 1.
    assert_array_equal(np.array(1.), fields.at_all_over_the_place['const'])

    assert_raises(ValueError, fields.add_field, 'new_value', [0,1], at='all_over_the_place')

def test_grid_field():
    fields = ModelDataFields()
    fields.new_field_location('grid', 1)

    assert_raises(ValueError, fields.add_zeros, 'value', at='grid')
    assert_raises(ValueError, fields.add_empty, 'value', at='grid')
    assert_raises(ValueError, fields.add_ones, 'value', at='grid')

    assert_raises(ValueError, fields.zeros, at='grid')
    assert_raises(ValueError, fields.empty, at='grid')
    assert_raises(ValueError, fields.ones, at='grid')
<commit_msg>Minor changes to pass unit tests.<commit_after>#! /usr/bin/env python

from nose.tools import assert_true, assert_false, assert_raises
try:
    from nose.tools import assert_is_not, assert_is, assert_set_equal, assert_dict_equal
except ImportError:
    from landlab.testing.tools import (assert_is_not, assert_is, assert_set_equal,
                                       assert_dict_equal)
import numpy as np
from numpy.testing import assert_array_equal

from landlab.field import ModelDataFields, GroupError, FieldError

def test_init():
    fields = ModelDataFields()
    assert_set_equal(set(), fields.groups)

def test_new_field_location():
    fields = ModelDataFields()
    fields.new_field_location('node', 12)
    assert_set_equal(set(['node']), fields.groups)

def test_add_existing_group():
    fields = ModelDataFields()
    fields.new_field_location('node', size=12)
    assert_raises(ValueError, fields.new_field_location, 'node', size=24)

def test_add_multiple_groups():
    fields = ModelDataFields()
    fields.new_field_location('node', 12)
    fields.new_field_location('cell', 2)
    fields.new_field_location('face', 7)
    fields.new_field_location('link', 7)
    assert_set_equal(set(['node', 'cell', 'face', 'link']), fields.groups)

def test_ones():
    fields = ModelDataFields()
    fields.new_field_location('node', 12)
    fields.new_field_location('cell', 2)

    value_array = fields.ones('node')
    assert_array_equal(np.ones(12), value_array)

    value_array = fields.ones('cell')
    assert_array_equal(np.ones(2), value_array)

def test_add_ones():
    fields = ModelDataFields()
    fields.new_field_location('node', 12)
    fields.new_field_location('cell', 2)

    fields.add_ones('z', at='node')
    assert_array_equal(np.ones(12), fields['node']['z'])
    assert_array_equal(np.ones(12), fields.field_values('node', 'z'))

    fields.add_ones('z', at='cell')
    assert_array_equal(np.ones(2), fields['cell']['z'])
    assert_array_equal(np.ones(2), fields.field_values('cell', 'z'))

def test_add_ones_return_value():
    fields = ModelDataFields()
    fields.new_field_location('node', 12)
    fields.new_field_location('cell', 2)

    rtn_value = fields.add_ones('z', at='node')
    assert_array_equal(rtn_value, np.ones(12))
    assert_is(rtn_value, fields['node']['z'])
    assert_is(rtn_value, fields.field_values('node', 'z'))

    rtn_value = fields.add_ones('z', at='cell')
    assert_array_equal(rtn_value, np.ones(2))
    assert_is(rtn_value, fields['cell']['z'])
    assert_is(rtn_value, fields.field_values('cell', 'z'))

def test_add_existing_field_default():
    """Test default is to not replace existing field."""
    fields = ModelDataFields()
    fields.new_field_location('node', 12)
    fields.add_empty('z', at='node')

    assert_raises(FieldError, fields.add_empty, 'z', at='node')
    assert_raises(FieldError, fields.add_ones, 'z', at='node')
    assert_raises(FieldError, fields.add_zeros, 'z', at='node')

def test_add_existing_field_with_noclobber():
    """Test noclobber raises an error with an existing field."""
    fields = ModelDataFields()
    fields.new_field_location('node', 12)
    fields.add_empty('z', at='node')

    assert_raises(FieldError, fields.add_empty, 'z', at='node', noclobber=True)
    assert_raises(FieldError, fields.add_ones, 'z', at='node', noclobber=True)
    assert_raises(FieldError, fields.add_zeros, 'z', at='node', noclobber=True)

def test_add_field_with_noclobber():
    """Test noclobber does not raise an error with an new field."""
    fields = ModelDataFields()
    fields.new_field_location('node', 12)

    fields.add_empty('a', at='node', noclobber=True)
    assert_true('a' in fields['node'])

    fields.add_ones('b', at='node', noclobber=True)
    assert_true('b' in fields['node'])

    fields.add_zeros('c', at='node', noclobber=True)
    assert_true('c' in fields['node'])

def test_add_field_with_clobber():
    """Test adding a field with clobber on."""
    fields = ModelDataFields()
    fields.new_field_location('node', 12)

    assert_is_not(fields.add_empty('a', at='node'),
                  fields.add_empty('a', at='node', noclobber=False))
    assert_is_not(fields.add_ones('b', at='node'),
                  fields.add_ones('b', at='node', noclobber=False))
    assert_is_not(fields.add_zeros('c', at='node'),
                  fields.add_zeros('c', at='node', noclobber=False))

def test_getitem():
    fields = ModelDataFields()
    fields.new_field_location('node', 12)

    assert_dict_equal(dict(), fields['node'])
    assert_raises(GroupError, lambda k: fields[k], 'cell')
    assert_raises(KeyError, lambda k: fields[k], 'cell')

def test_at_attribute():
    fields = ModelDataFields()
    fields.new_field_location('node', 12)

    assert_dict_equal(dict(), fields.at_node)
    assert_raises(AttributeError, lambda: fields.at_cell)

    fields.add_ones('z', at='node')
    assert_array_equal(np.ones(12), fields.at_node['z'])

def test_has_group():
    fields = ModelDataFields()
    fields.new_field_location('node', 12)

    assert_true(fields.has_group('node'))
    assert_false(fields.has_group('cell'))

def test_delete_field():
    fields = ModelDataFields()
    fields.new_field_location('link', 17)

    assert_dict_equal(dict(), fields.at_link)
    assert_raises(AttributeError, lambda: fields.at_node)

    fields.add_zeros('link', 'vals')
    assert_array_equal(np.zeros(17), fields.at_link['vals'])

    assert_raises(KeyError, lambda: fields.delete_field('node', 'vals'))
    fields.delete_field('link', 'vals')
    assert_raises(KeyError, lambda: fields.field_units('link', 'vals'))
    assert_raises(KeyError, lambda: fields.at_link['vals'])

def test_scalar_field():
    fields = ModelDataFields()
    fields.new_field_location('all_over_the_place', 1)

    assert_dict_equal(dict(), fields.at_all_over_the_place)
    assert_raises(AttributeError, lambda: fields.at_cell)

    fields.at_all_over_the_place['const'] = 1.
    assert_array_equal(np.array(1.), fields.at_all_over_the_place['const'])

    assert_raises(ValueError, fields.add_field, 'new_value', [0,1], at='all_over_the_place')

def test_grid_field():
    fields = ModelDataFields()
    fields.new_field_location('grid', 1)

    assert_raises(ValueError, fields.add_zeros, 'value', at='grid')
    assert_raises(ValueError, fields.add_empty, 'value', at='grid')
    assert_raises(ValueError, fields.add_ones, 'value', at='grid')

    assert_raises(ValueError, fields.zeros, 'grid')
    assert_raises(ValueError, fields.empty, 'grid')
    assert_raises(ValueError, fields.ones, 'grid')
<|endoftext|>


<commit_before>import curses
import time
import queue
import sys
import requests
try:
    import RPi.GPIO as gpio
except:
    print("Couldn't load GPIO")
    global gpio
    gpio = lambda: None
    setattr(gpio, 'setup', lambda _, __: None)
    setattr(gpio, 'setwarnings', lambda _: None)
    setattr(gpio, 'setmode', lambda _: None)
    setattr(gpio, 'output', lambda _, __: None)
    setattr(gpio, 'BOARD', None)
    setattr(gpio, 'OUT', None)

BUZZER = 15
BEEPER = 13

gpio.setwarnings(False);
gpio.setmode(gpio.BOARD)
gpio.setup(BUZZER, gpio.OUT)
gpio.setup(BEEPER, gpio.OUT)

class Game:
    def __init__(self, graphics, serial, stdscr, play, framerate=30):
        self.play = play
        self.stdscr = stdscr
        self.graphics = graphics
        self.serial = serial
        self.sprites = set()
        self.framerate = framerate
        self.input_queue = queue.Queue()
        self.keys = set()

    def send_input(self, name):
        self.input_queue.put(name)

    def handle_events(self):
        self.keys = set()

        try:
            while True:
                key = self.stdscr.getkey().lower()
                if key == '\x1b':
                    self.play.exec_game('menu')
                    return
                self.keys.add(key)
        except curses.error:
            pass

        try:
            while True:
                item = self.input_queue.get_nowait().lower()
                self.keys.add(item)
        except queue.Empty:
            pass

    def loop(self):
        self.graphics.clear()
        for sprite in self.sprites:
            sprite.draw(self.graphics)

        self.graphics.draw(self.serial)
        time.sleep(1 / self.framerate)

    def stop(self):
        self.running = False

    def run(self):
        self.running = True
        while self.running:
            self.handle_events()
            self.loop()

    def trigger(self, item, act):
        print("Triggering {}.{}".format(item, act))
        if item == "alert" and requests:
            requests.get("localhost:5000/{}/a/7".format(act))
        elif item == "buzzer" and gpio:
            gpio.output(BUZZER, 1 if act == "on" else 0)
        elif item == "beeper" and gpio:
            gpio.output(BEEPER, 1 if act == "on" else 0)
<commit_msg>More debug<commit_after>import curses
import time
import queue
import sys
import requests
try:
    import RPi.GPIO as gpio
except:
    print("Couldn't load GPIO")
    global gpio
    gpio = lambda: None
    setattr(gpio, 'setup', lambda _, __: None)
    setattr(gpio, 'setwarnings', lambda _: None)
    setattr(gpio, 'setmode', lambda _: None)
    setattr(gpio, 'output', lambda _, __: None)
    setattr(gpio, 'BOARD', None)
    setattr(gpio, 'OUT', None)

BUZZER = 15
BEEPER = 13

gpio.setwarnings(False);
gpio.setmode(gpio.BOARD)
gpio.setup(BUZZER, gpio.OUT)
gpio.setup(BEEPER, gpio.OUT)

class Game:
    def __init__(self, graphics, serial, stdscr, play, framerate=30):
        self.play = play
        self.stdscr = stdscr
        self.graphics = graphics
        self.serial = serial
        self.sprites = set()
        self.framerate = framerate
        self.input_queue = queue.Queue()
        self.keys = set()

    def send_input(self, name):
        self.input_queue.put(name)

    def handle_events(self):
        self.keys = set()

        try:
            while True:
                key = self.stdscr.getkey().lower()
                if key == '\x1b':
                    self.play.exec_game('menu')
                    return
                self.keys.add(key)
        except curses.error:
            pass

        try:
            while True:
                item = self.input_queue.get_nowait().lower()
                self.keys.add(item)
        except queue.Empty:
            pass

    def loop(self):
        self.graphics.clear()
        for sprite in self.sprites:
            sprite.draw(self.graphics)

        self.graphics.draw(self.serial)
        time.sleep(1 / self.framerate)

    def stop(self):
        self.running = False

    def run(self):
        self.running = True
        while self.running:
            self.handle_events()
            self.loop()

    def trigger(self, item, act):
        print("Triggering {}.{}".format(item, act))
        if item == "alert" and requests:
            requests.get("localhost:5000/{}/a/7".format(act))
            print("alert " + act)
        elif item == "buzzer" and gpio:
            gpio.output(BUZZER, 1 if act == "on" else 0)
            print("buzzer " + act)
        elif item == "beeper" and gpio:
            gpio.output(BEEPER, 1 if act == "on" else 0)
            print("beeper " + act)
<|endoftext|>


<commit_before>"""
Django settings for directory project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '(echxevhb8hnhy#n884nfbivj^a5o%tf(1wr5jf@b3c2udyewg'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'simmons_directory',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'directory.urls'

WSGI_APPLICATION = 'directory.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/directory/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
<commit_msg>I fucked up again<commit_after>"""
Django settings for directory project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '(echxevhb8hnhy#n884nfbivj^a5o%tf(1wr5jf@b3c2udyewg'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'simmons_directory',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'directory.urls'

WSGI_APPLICATION = 'directory.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/directory/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class RunningWindow(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        layout = QHBoxLayout()
        button = QPushButton('DiRT Start!', self)
        layout.addWidget(button)

        self.widget = QWidget()
        self.widget.setLayout(layout)

        self.setCentralWidget(self.widget)
        self.setWindowTitle("Win1")

        self.connect(button, SIGNAL('clicked()'), self.newWindow)
        self.show()

    def newWindow(self):
        self.other_window = MainWindow()
        self.other_window.show()

def main():
    app = QtGui.QApplication(sys.argv)
    rw = RunningWindow()
    rw.show()

    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

<commit_msg>Layout<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class RunningWindow(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)

        self.layout = BoxLayout(self)
        self.setCentralWidget(self.layout)

        self.setGeometry(300, 300, 350, 350)
        self.setWindowTitle('DiRT Startup')
        self.show()

class BoxLayout(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.setWindowTitle('box layout')

        startButton = QtGui.QPushButton("DiRT Start")

        hbox = QtGui.QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(startButton)

        vbox = QtGui.QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox)
        self.setLayout(vbox)

        self.connect(startButton, SIGNAL('clicked()'), self.newWindow)

    def newWindow(self):
        self.other_window = MainWindow()
        self.other_window.show()

def main():
    app = QtGui.QApplication(sys.argv)
    rw = RunningWindow()
    rw.show()

    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

<|endoftext|>