<commit_before>import fileinput
import math
import collections
import time
import numpy as np
from pylab import *
from matplotlib import pyplot as plt

file_path = '/media/ABB4-4F3A/DATALOG.TXT'

def split_in_blocks(txt_file, pattern):
	'''
	Find the last appears of the text that indicate a new flight and divide in the number of blocks generated by the rocket
	Return: A list that contains all the different blocks of data and a list containing the header.
	'''
	num_times_find_pattern = []
	for num_line, line in enumerate(fileinput.input(txt_file)):
		if pattern in line:
			num_times_find_pattern.append(num_line)
		if num_line == 0:
			header = list(line.strip().split(","))
			#print header
	blocks_of_data = []
	with open(txt_file) as f:
		lines = f.readlines()
		for num_header_line in num_times_find_pattern:
			if num_header_line == 0:
				num_header_line_prev = num_header_line
			else:
				block_lines = lines[num_header_line_prev + 1 : num_header_line - 1]
				blocks_of_data.append(block_lines)
				num_header_line_prev = num_header_line
		block_lines = lines[num_header_line_prev + 1 : num_line + 1]
		blocks_of_data.append(block_lines)
	return blocks_of_data, header

def manage_data_from_blocks(blocks, header):
	'''
	Divide al the text in blocks tagged with their type of data (accelaration, temperature, ...) continued by a number of block
	Return: A dict that contains all the different types of data diferentiated and numbered.
	'''
	# TODO: Automatize this function to accept more headers!!
	blocks_dict = collections.OrderedDict()
	for block_number, block in enumerate(blocks):
		for item in header:
			blocks_dict['%s%s' % (item,block_number)] = []
		for line in block:
			line_list = line.strip().split(",")
			blocks_dict['f%s' % block_number].append(int(line_list[0]))
			blocks_dict['ax%s' % block_number].append(float(line_list[1]))
			blocks_dict['ay%s' % block_number].append(float(line_list[2]))
			blocks_dict['az%s' % block_number].append(float(line_list[3]))
			blocks_dict['gx%s' % block_number].append(float(line_list[4]))
			blocks_dict['gy%s' % block_number].append(float(line_list[5]))
			blocks_dict['gz%s' % block_number].append(float(line_list[6]))
			blocks_dict['mx%s' % block_number].append(float(line_list[7]))
			blocks_dict['my%s' % block_number].append(float(line_list[8]))
			blocks_dict['mz%s' % block_number].append(float(line_list[9]))
			blocks_dict['t%s' % block_number].append(float(line_list[10]))
			blocks_dict['p%s' % block_number].append(int(line_list[11]))
			blocks_dict['h%s' % block_number].append(float(line_list[12]))
	return blocks_dict

def process_data(blocks_dict, header):
	block_list_header_based = []
	for num, item in enumerate(header):
		block_list_header_based.append([])
		for block in blocks_dict:
			if block.startswith(header[num]):
				block_list_header_based[num].append(block)
				print "%s: %s" % (block, blocks_dict[block])
	print block_list_header_based
	#print blocks_dict.keys()
	fingerprint_basic_info = basic_process_fingerprints(fingerprints)
	temp_basic_info = basic_process_temperatures(temperatures)
	print_temp_evolution(fingerprints, temperatures)
	#print fingerprint_basic_info
	#print temperatures_basic_info

def basic_process_fingerprints(fingerprints):
	fingerprint_basic_info = collections.OrderedDict()
	for fingerprint_block in fingerprints:
		millis_interval_list = []
		for position, millis in enumerate(blocks_dict[fingerprint_block]):
		 	if position != 0:
		 		millis_interval = millis - millis_prev
		 		millis_interval_list.append(millis_interval)
		 	millis_prev = millis
		fingerprint_basic_info[fingerprint_block] = {}
		millis_average = sum(millis_interval_list) / len(blocks_dict[fingerprint_block])
		fingerprint_basic_info[fingerprint_block]["Av"] = millis_average
		millis_max = max(millis_interval_list)
		fingerprint_basic_info[fingerprint_block]["MAX"] = millis_max
		millis_min = min(millis_interval_list)
		fingerprint_basic_info[fingerprint_block]["MIN"] = millis_min
	return fingerprint_basic_info

def basic_process_temperatures(temperatures):
	temp_basic_info = collections.OrderedDict()
	for temp_block in temperatures:
		temp_basic_info[temp_block] = {}
		temp_average = sum(blocks_dict[temp_block]) / len(blocks_dict[temp_block])
		temp_basic_info[temp_block]["Av"] = "%.2f" % temp_average
		temp_max = max(blocks_dict[temp_block])
		temp_basic_info[temp_block]["MAX"] = temp_max
		temp_min = min(blocks_dict[temp_block])
		temp_basic_info[temp_block]["MIN"] = temp_min
	return temp_basic_info

def print_temp_evolution(fingerprints, temperatures):
	plt.figure(1)
	for num in range(len(fingerprints)):
		x = blocks_dict[fingerprints[num]]
		y = blocks_dict[temperatures[num]]
		#subplot(nrows, ncols, plot_number)
		nrows = int(math.ceil(float(len(fingerprints) / 3.0)))
		ncols = 3
		subplot_index = "%s%s%s" % (nrows, ncols, num + 1)
		plt.subplot(subplot_index)

		plt.plot(x, y, linewidth=1.0, color="green")
		xlabel('time (milliseconds)', fontsize = 8)
		ylabel('temperature (C)', fontsize = 8)
		#title('', fontsize=10)
		grid(True)
		plt.xticks(blocks_dict[fingerprints[num]][::len(blocks_dict[fingerprints[num]])/10], rotation=30, fontsize=8)
		plt.annotate('Despegue', xy=(2200, 34.82), xytext=(2300, 34.88),
				bbox=dict(boxstyle="round", fc="0.8"),
	            arrowprops=dict(facecolor='black', shrink=0.05),
	            )
		plt.annotate('Paracaidas', xy=(7200, 34.82), xytext=(6300, 34.88),
	            arrowprops=dict(facecolor='black', shrink=0.05),
	            )
		axvline(x=2200)
		axhspan(34.80, 34.82, facecolor='0.5', alpha=0.5, color="red")
		plt.ylim(min(blocks_dict[temperatures[num]]) - 0.02, max(blocks_dict[temperatures[num]]) + 0.02)
		plt.yticks(fontsize=8)
	plt.suptitle('temperatures in data', fontsize=12)
	plt.show()

#start = time.time()
blocks, header = split_in_blocks(file_path, "m")
blocks_dict = manage_data_from_blocks(blocks, header)
process_data(blocks_dict, header)

#stop = time.time()
#total_time = stop -start
#print total_time<commit_msg>Enhanced the plot for two axys<commit_after>import fileinput
import math
import collections
import time
import numpy as np
from pylab import *
from matplotlib import pyplot as plt

file_path = '/media/ABB4-4F3A/DATALOG.TXT'

def split_in_blocks(txt_file, pattern):
	'''
	Find the last appears of the text that indicate a new flight and divide in the number of blocks generated by the rocket
	Return: A list that contains all the different blocks of data and a list containing the header.
	'''
	num_times_find_pattern = []
	for num_line, line in enumerate(fileinput.input(txt_file)):
		if pattern in line:
			num_times_find_pattern.append(num_line)
		if num_line == 0:
			header = list(line.strip().split(","))
			#print header
	blocks_of_data = []
	with open(txt_file) as f:
		lines = f.readlines()
		for num_header_line in num_times_find_pattern:
			if num_header_line == 0:
				num_header_line_prev = num_header_line
			else:
				block_lines = lines[num_header_line_prev + 1 : num_header_line - 1]
				blocks_of_data.append(block_lines)
				num_header_line_prev = num_header_line
		block_lines = lines[num_header_line_prev + 1 : num_line + 1]
		blocks_of_data.append(block_lines)
	return blocks_of_data, header

def manage_data_from_blocks(blocks, header):
	'''
	Divide al the text in blocks tagged with their type of data (accelaration, temperature, ...) continued by a number of block
	Return: A dict that contains all the different types of data diferentiated and numbered.
	'''
	# TODO: Automatize this function to accept more headers!!
	blocks_dict = collections.OrderedDict()
	for block_number, block in enumerate(blocks):
		for item in header:
			blocks_dict['%s%s' % (item,block_number)] = []
		for line in block:
			line_list = line.strip().split(",")
			blocks_dict['f%s' % block_number].append(int(line_list[0]))
			blocks_dict['ax%s' % block_number].append(float(line_list[1]))
			blocks_dict['ay%s' % block_number].append(float(line_list[2]))
			blocks_dict['az%s' % block_number].append(float(line_list[3]))
			blocks_dict['gx%s' % block_number].append(float(line_list[4]))
			blocks_dict['gy%s' % block_number].append(float(line_list[5]))
			blocks_dict['gz%s' % block_number].append(float(line_list[6]))
			blocks_dict['mx%s' % block_number].append(float(line_list[7]))
			blocks_dict['my%s' % block_number].append(float(line_list[8]))
			blocks_dict['mz%s' % block_number].append(float(line_list[9]))
			blocks_dict['t%s' % block_number].append(float(line_list[10]))
			blocks_dict['p%s' % block_number].append(int(line_list[11]))
			blocks_dict['h%s' % block_number].append(float(line_list[12]))
	return blocks_dict

def process_data(blocks_dict, header):
	block_list_header_based = []
	for num, item in enumerate(header):
		block_list_header_based.append([])
		for block in blocks_dict:
			if block.startswith(header[num]):
				block_list_header_based[num].append(block)
				# DEBUG! print "%s: %s" % (block, blocks_dict[block])
	print block_list_header_based
	fingerprint_basic_info = basic_process_fingerprints(block_list_header_based[0])
	temp_basic_info = basic_process_temperatures(block_list_header_based[1])
	print_basic_evolution_2_axis(block_list_header_based[0], block_list_header_based[9])

def basic_process_fingerprints(fingerprints):
	fingerprint_basic_info = collections.OrderedDict()
	for fingerprint_block in fingerprints:
		millis_interval_list = []
		for position, millis in enumerate(blocks_dict[fingerprint_block]):
		 	if position != 0:
		 		millis_interval = millis - millis_prev
		 		millis_interval_list.append(millis_interval)
		 	millis_prev = millis
		fingerprint_basic_info[fingerprint_block] = {}
		millis_average = sum(millis_interval_list) / len(blocks_dict[fingerprint_block])
		fingerprint_basic_info[fingerprint_block]["Av"] = millis_average
		millis_max = max(millis_interval_list)
		fingerprint_basic_info[fingerprint_block]["MAX"] = millis_max
		millis_min = min(millis_interval_list)
		fingerprint_basic_info[fingerprint_block]["MIN"] = millis_min
	return fingerprint_basic_info

def basic_process_temperatures(temperatures):
	temp_basic_info = collections.OrderedDict()
	for temp_block in temperatures:
		temp_basic_info[temp_block] = {}
		temp_average = sum(blocks_dict[temp_block]) / len(blocks_dict[temp_block])
		temp_basic_info[temp_block]["Av"] = "%.2f" % temp_average
		temp_max = max(blocks_dict[temp_block])
		temp_basic_info[temp_block]["MAX"] = temp_max
		temp_min = min(blocks_dict[temp_block])
		temp_basic_info[temp_block]["MIN"] = temp_min
	return temp_basic_info

def print_basic_evolution_2_axis(x_axis_data_list, y_axis_data_list):
	plt.figure(1)
	for num in range(len(x_axis_data_list)):
		x = blocks_dict[x_axis_data_list[num]]
		print blocks_dict[y_axis_data_list[num]]
		y = blocks_dict[y_axis_data_list[num]]
		#subplot(nrows, ncols, plot_number)
		nrows = int(math.ceil(float(len(x_axis_data_list) / 3.0)))
		ncols = 3
		subplot_index = "%s%s%s" % (nrows, ncols, num + 1)
		plt.subplot(subplot_index)

		plt.plot(x, y, linewidth=1.0, color="green")
		xlabel('time (milliseconds)', fontsize = 8)
		ylabel('temperature (C)', fontsize = 8)
		#title('', fontsize=10)
		grid(True)
		plt.xticks(blocks_dict[x_axis_data_list[num]][::len(blocks_dict[x_axis_data_list[num]])/10], rotation=30, fontsize=8)
		#plt.annotate('Despegue', xy=(2200, 34.82), xytext=(2300, 34.88),
		#		bbox=dict(boxstyle="round", fc="0.8"),
	    #        arrowprops=dict(facecolor='black', shrink=0.05),
	    #        )
		#plt.annotate('Paracaidas', xy=(7200, 34.82), xytext=(6300, 34.88),
	    #        arrowprops=dict(facecolor='black', shrink=0.05),
	    #        )
		#axvline(x=2200)
		#axhspan(34.80, 34.82, facecolor='0.5', alpha=0.5, color="red")
		plt.ylim(min(blocks_dict[y_axis_data_list[num]]) - 0.02, max(blocks_dict[y_axis_data_list[num]]) + 0.02)
		plt.yticks(fontsize=8)
	plt.suptitle('temperatures in data', fontsize=12)
	plt.show()

#start = time.time()
blocks, header = split_in_blocks(file_path, "m")
blocks_dict = manage_data_from_blocks(blocks, header)
process_data(blocks_dict, header)

#stop = time.time()
#total_time = stop -start
#print total_time<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#pylint: disable=invalid-name, line-too-long
"""
    Main application window
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import sys
import os
import logging
import glob
from PyQt5 import QtGui, QtCore, QtWidgets
import reflectivity_ui.interfaces.generated.ui_main_window

from .data_handling.loader import NexusData
from .configuration import Configuration

class MainWindow(QtWidgets.QMainWindow,
                 reflectivity_ui.interfaces.generated.ui_main_window.Ui_MainWindow):
    """
        Main applicqtion window
    """
    # UI events
    file_loaded_signal = QtCore.pyqtSignal()

    def __init__(self):
        """
            Initialization
        """
        # Base class
        QtWidgets.QMainWindow.__init__(self)

        # Initialize the UI widgets
        self.ui = reflectivity_ui.interfaces.generated.ui_main_window.Ui_MainWindow()
        self.ui.setupUi(self)

        # Application settings
        self.configuration = Configuration()
        self.settings = QtCore.QSettings('.refredm')
        self._current_directory = self.settings.value('current_directory', os.path.expanduser('~'))
        self._current_file = None
        self._active_channel = None

        # Update file list when changes are made
        self._path_watcher = QtCore.QFileSystemWatcher([self._current_directory], self)
        self._path_watcher.directoryChanged.connect(self.update_file_list)

        # Initialization for specific instrument
        # Retrieve configuration from config and enable/disable features
        self.initialize_instrument()
        self.hide_unsupported()

        # UI events
        self.file_loaded_signal.connect(self.update_info)
        self.file_loaded_signal.connect(self.update_daslog)

    def initialize_instrument(self):
        """
            Initialize instrument according to the instrument
        """
        for i in range(1, 12):
            getattr(self.ui, 'selectedChannel%i'%i).hide()
        self.ui.selectedChannel0.show()
        self.ui.selectedChannel0.setText(u"none")

    def hide_unsupported(self):
        """
            Hide what we don't support
        """
        # Hide event filtering (which is not really event filtering)
        for i in range(self.ui.event_filtering_layout.rowCount()*self.ui.event_filtering_layout.columnCount()):
            if self.ui.event_filtering_layout.itemAt(i):
                self.ui.event_filtering_layout.itemAt(i).widget().hide()

        # Hide format selectiuon since we're only using event data
        self.ui.oldFormatActive.hide()
        self.ui.histogramActive.hide()
        self.ui.eventActive.hide()

        # Hide quick reduce button
        self.ui.reduceLastButton.hide()
        self.ui.load_live_data_button.hide()
        self.ui.direct_beam_runs_label.hide()

    # Actions defined in Qt Designer
    def file_open_dialog(self):
        """
            Show a dialog to open a new file.
            TODO: consider multiple selection. In this case QuickNXS tries to automatically sort and reduce.
        """

        if self.ui.histogramActive.isChecked():
            filter_ = u'Histo Nexus (*histo.nxs);;All (*.*)'
        else:
            filter_ = u'Event Nexus (*nxs.h5);;Event Nexus (*event.nxs);;All (*.*)'
        file_path, _ = QtWidgets.QFileDialog.getOpenFileName(self, u'Open NXS file...',
                                                    directory=self._current_directory,
                                                    filter=filter_)

        if file_path:
            file_dir, file_name = os.path.split(unicode(file_path))
            self.settings.setValue('current_directory', file_dir)

            self._path_watcher.removePath(self._current_directory)
            self._current_directory = file_dir
            self._path_watcher.addPath(self._current_directory)
            self._current_file = file_path
            self._current_file_name = file_name
            self.update_file_list()

            # Load the file
            try:
                nexus_data = NexusData(file_path, self.configuration)
                self._data_sets = nexus_data.load()
                self.file_loaded()
            except:
                logging.error("Error loading file: %s", sys.exc_value)
            #self.fileOpen(filenames[0])

    def file_loaded(self):
        current_channel=0
        for i in range(12):
            if getattr(self.ui, 'selectedChannel%i'%i).isChecked():
                current_channel = i

        channels = self._data_sets.keys()
        if current_channel < len(channels):
            self._active_channel = self._data_sets[channels[current_channel]]
        else:
            self._active_channel = self._data_sets[channels[0]]
            self.ui.selectedChannel0.setChecked(True)

        for i, channel in enumerate(channels):
            getattr(self.ui, 'selectedChannel%i'%i).show()
            getattr(self.ui, 'selectedChannel%i'%i).setText(channel)
        for i in range(len(channels), 12):
            getattr(self.ui, 'selectedChannel%i'%i).hide()

        #if self.active_channel in self.ref_list_channels:
        #    for i, refli in enumerate(self.reduction_list):
        #        refli=self.recalculateReflectivity(refli)
        #        self.reduction_list[i]=refli
        #self.plotActiveTab()
        #self.initiateProjectionPlot.emit(False)
        #self.initiateReflectivityPlot.emit(False)

        # Update UI
        self.file_loaded_signal.emit()

    def update_info(self):
        """
            Write file metadata to the labels in the overview tab.
        """
        d=self._active_channel

        try:
            dangle0=u"%.3f° (%.3f°)"%(float(self.ui.dangle0Overwrite.text()), d.dangle0)
        except ValueError:
            dangle0=u"%.3f°"%(d.dangle0)
        if self.ui.directPixelOverwrite.value()>=0:
            dpix=u"%.1f (%.1f)"%(self.ui.directPixelOverwrite.value(), d.dpix)
        else:
            dpix=u"%.1f"%d.dpix
        self.ui.datasetLambda.setText(u"%.2f (%.2f-%.2f) Å"%(d.lambda_center,
                                                             d.lambda_center-1.5,
                                                             d.lambda_center+1.5))
        self.ui.datasetPCharge.setText(u"%.3e"%d.proton_charge)
        self.ui.datasetTime.setText(u"%i s"%d.total_time)
        self.ui.datasetTotCounts.setText(u"%.4e"%d.total_counts)
        try:
            self.ui.datasetRate.setText(u"%.1f cps"%(d.total_counts/d.total_time))
        except ZeroDivisionError:
            self.ui.datasetRate.setText(u"NaN")
        self.ui.datasetDangle.setText(u"%.3f°"%d.dangle)
        self.ui.datasetDangle0.setText(dangle0)
        self.ui.datasetSangle.setText(u"%.3f°"%d.sangle)
        self.ui.datasetDirectPixel.setText(dpix)
        self.ui.currentChannel.setText('%s (%s)   Type: %s   Current State: %s'%(
                                       d.number, d.experiment,
                                       d.measurement_type, d.name))

    def update_daslog(self):
        """
            Write parameters from all file daslogs to the table in the
            daslog tab.
        """
        table=self.ui.daslogTableBox
        table.setRowCount(0)
        table.sortItems(-1)
        table.setColumnCount(len(self._data_sets)+2)
        table.setHorizontalHeaderLabels(['Name']+self._data_sets.keys()+['Unit'])
        for j, key in enumerate(sorted(self._active_channel.logs.keys(), key=lambda s: s.lower())):
            table.insertRow(j)
            table.setItem(j, 0, QtWidgets.QTableWidgetItem(key))
            table.setItem(j, len(self._data_sets)+1,
                          QtWidgets.QTableWidgetItem(self._active_channel.log_units[key]))
            i = 0
            for xs in self._data_sets:
                item=QtWidgets.QTableWidgetItem(u'%g' % self._data_sets[xs].logs[key])
                item.setToolTip(u'MIN: %g   MAX: %g' % (self._data_sets[xs].log_minmax[key]))
                table.setItem(j, i+1, item)
                i += 1
        table.resizeColumnsToContents()

    def _plotActiveTab(self):
        '''
            Select the appropriate function to plot all visible images.
        '''
        if self.auto_change_active or not self.active_data:
            return
        color=str(self.ui.color_selector.currentText())
        if color!=self.color and self.color is not None:
            self.color=color
            plots=[self.ui.xy_pp, self.ui.xy_mp, self.ui.xy_pm, self.ui.xy_mm,
                   self.ui.xtof_pp, self.ui.xtof_mp, self.ui.xtof_pm, self.ui.xtof_mm,
                   self.ui.xy_overview, self.ui.xtof_overview]
            for plot in plots:
                plot.clear_fig()
        elif self.color is None:
            self.color=color
        if self.ui.plotTab.currentIndex()!=4 and self._gisansThread:
            self._gisansThread.finished.disconnect()
            self._gisansThread.terminate()
            self._gisansThread.wait(100)
            self._gisansThread=None
        if self.ui.plotTab.currentIndex()==0:
            self.plot_overview()
        if self.ui.plotTab.currentIndex()==1:
            self.plot_xy()
        if self.ui.plotTab.currentIndex()==2:
            self.plot_xtof()
        if self.ui.plotTab.currentIndex()==3:
            self.plot_offspec()
        if self.ui.plotTab.currentIndex()==4:
            self.plot_gisans()
        if self.ui.plotTab.currentIndex()==5:
            self.update_daslog()

    def update_file_list(self):
        """
            Update the list of data files
        """
        # Update the list of files
        event_file_list = glob.glob(os.path.join(self._current_directory, '*event.nxs'))
        h5_file_list = glob.glob(os.path.join(self._current_directory, '*.nxs.h5'))
        event_file_list.extend(h5_file_list)
        event_file_list.sort()
        event_file_list = [os.path.basename(name) for name in event_file_list]

        current_list=[self.ui.file_list.item(i).text() for i in range(self.ui.file_list.count())]
        if event_file_list != current_list:
            self.ui.file_list.clear()
            for item in event_file_list:
                listitem = QtWidgets.QListWidgetItem(item, self.ui.file_list)
                if item == self._current_file_name:
                    self.ui.file_list.setCurrentItem(listitem)
        else:
            try:
                self.ui.file_list.setCurrentRow(event_file_list.index(self._current_file_name))
            except ValueError:
                logging.error("Could not set file selection: %s", self._current_file_name)
                logging.error(sys.exc_value)

    def reload_file(self):
        """
            Reload the file that is currently selected form the list.
        """
        self.fileOpen(self._current_file)

    def changeActiveChannel(self):
        '''
        The overview and reflectivity channel was changed. This
        recalculates already extracted reflectivities.
        '''
        return self.file_loaded()

    def gather_options(self):
        """
            Gather the reduction options.
        """
        pass

    def plotActiveTab(self): return NotImplemented
    def setNorm(self): return NotImplemented
    def addRefList(self): return NotImplemented
    def clearRefList(self): return NotImplemented
    def overwriteDirectBeam(self): return NotImplemented
    def normalizeTotalReflection(self): return NotImplemented
    def removeRefList(self): return NotImplemented
    def reduceDatasets(self): return NotImplemented
    def clearOverwrite(self): return NotImplemented
    def loadExtraction(self): return NotImplemented
    def reductionTableChanged(self): return NotImplemented
    def clearNormList(self): return NotImplemented
    def change_offspec_colorscale(self): return NotImplemented
    def change_gisans_colorscale(self): return NotImplemented
    def open_compare_window(self): return NotImplemented
    def open_advanced_background(self): return NotImplemented
    def clip_offspec_colorscale(self): return NotImplemented
    def fileOpenSumDialog(self): return NotImplemented
    def changeRegionValues(self): return NotImplemented
    def toggleColorbars(self): return NotImplemented
    def overwriteChanged(self): return NotImplemented
    def toggleHide(self): return NotImplemented
    def fileOpenList(self): return NotImplemented
    def visualizePeakfinding(self): return NotImplemented
    def folderModified(self): return NotImplemented
    def replotProjections(self): return NotImplemented
    def openByNumber(self): return NotImplemented
    def cutPoints(self): return NotImplemented
    def autoRef(self): return NotImplemented
    def stripOverlap(self): return NotImplemented
    def open_polarization_window(self): return NotImplemented
    def open_rawdata_dialog(self): return NotImplemented
    def run_ipython(self): return NotImplemented
    def quickReduce(self): return NotImplemented
    def open_nxs_dialog(self): return NotImplemented
    def open_filter_dialog(self): return NotImplemented
    def open_reduction_preview(self): return NotImplemented
    def live_open(self): return NotImplemented
    def open_database_search(self): return NotImplemented
    def exportRawData(self): return NotImplemented

<commit_msg>Add das logs<commit_after># -*- coding: utf-8 -*-
#pylint: disable=invalid-name, line-too-long
"""
    Main application window
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import sys
import os
import logging
import glob
from PyQt5 import QtGui, QtCore, QtWidgets
import reflectivity_ui.interfaces.generated.ui_main_window

from .data_handling.loader import NexusData
from .configuration import Configuration

class MainWindow(QtWidgets.QMainWindow,
                 reflectivity_ui.interfaces.generated.ui_main_window.Ui_MainWindow):
    """
        Main applicqtion window
    """
    # UI events
    file_loaded_signal = QtCore.pyqtSignal()

    def __init__(self):
        """
            Initialization
        """
        # Base class
        QtWidgets.QMainWindow.__init__(self)

        # Initialize the UI widgets
        self.ui = reflectivity_ui.interfaces.generated.ui_main_window.Ui_MainWindow()
        self.ui.setupUi(self)

        # Application settings
        self.configuration = Configuration()
        self.settings = QtCore.QSettings('.refredm')
        self._current_directory = self.settings.value('current_directory', os.path.expanduser('~'))
        self._current_file = None
        self._active_channel = None

        # Update file list when changes are made
        self._path_watcher = QtCore.QFileSystemWatcher([self._current_directory], self)
        self._path_watcher.directoryChanged.connect(self.update_file_list)

        # Initialization for specific instrument
        # Retrieve configuration from config and enable/disable features
        self.initialize_instrument()
        self.hide_unsupported()

        # UI events
        self.file_loaded_signal.connect(self.update_info)
        self.file_loaded_signal.connect(self.update_daslog)
        self.file_loaded_signal.connect(self.plotActiveTab)

    def initialize_instrument(self):
        """
            Initialize instrument according to the instrument
        """
        for i in range(1, 12):
            getattr(self.ui, 'selectedChannel%i'%i).hide()
        self.ui.selectedChannel0.show()
        self.ui.selectedChannel0.setText(u"none")

    def hide_unsupported(self):
        """
            Hide what we don't support
        """
        # Hide event filtering (which is not really event filtering)
        for i in range(self.ui.event_filtering_layout.rowCount()*self.ui.event_filtering_layout.columnCount()):
            if self.ui.event_filtering_layout.itemAt(i):
                self.ui.event_filtering_layout.itemAt(i).widget().hide()

        # Hide format selectiuon since we're only using event data
        self.ui.oldFormatActive.hide()
        self.ui.histogramActive.hide()
        self.ui.eventActive.hide()

        # Hide quick reduce button
        self.ui.reduceLastButton.hide()
        self.ui.load_live_data_button.hide()
        self.ui.direct_beam_runs_label.hide()

    # Actions defined in Qt Designer
    def file_open_dialog(self):
        """
            Show a dialog to open a new file.
            TODO: consider multiple selection. In this case QuickNXS tries to automatically sort and reduce.
        """

        if self.ui.histogramActive.isChecked():
            filter_ = u'Histo Nexus (*histo.nxs);;All (*.*)'
        else:
            filter_ = u'Event Nexus (*nxs.h5);;Event Nexus (*event.nxs);;All (*.*)'
        file_path, _ = QtWidgets.QFileDialog.getOpenFileName(self, u'Open NXS file...',
                                                    directory=self._current_directory,
                                                    filter=filter_)

        if file_path:
            file_dir, file_name = os.path.split(unicode(file_path))
            self.settings.setValue('current_directory', file_dir)

            self._path_watcher.removePath(self._current_directory)
            self._current_directory = file_dir
            self._path_watcher.addPath(self._current_directory)
            self._current_file = file_path
            self._current_file_name = file_name
            self.update_file_list()

            # Load the file
            try:
                nexus_data = NexusData(file_path, self.configuration)
                self._data_sets = nexus_data.load()
                self.file_loaded()
            except:
                logging.error("Error loading file: %s", sys.exc_value)
            #self.fileOpen(filenames[0])

    def file_loaded(self):
        current_channel=0
        for i in range(12):
            if getattr(self.ui, 'selectedChannel%i'%i).isChecked():
                current_channel = i

        channels = self._data_sets.keys()
        if current_channel < len(channels):
            self._active_channel = self._data_sets[channels[current_channel]]
        else:
            self._active_channel = self._data_sets[channels[0]]
            self.ui.selectedChannel0.setChecked(True)

        for i, channel in enumerate(channels):
            getattr(self.ui, 'selectedChannel%i'%i).show()
            getattr(self.ui, 'selectedChannel%i'%i).setText(channel)
        for i in range(len(channels), 12):
            getattr(self.ui, 'selectedChannel%i'%i).hide()

        #if self.active_channel in self.ref_list_channels:
        #    for i, refli in enumerate(self.reduction_list):
        #        refli=self.recalculateReflectivity(refli)
        #        self.reduction_list[i]=refli
        #self.plotActiveTab()
        #self.initiateProjectionPlot.emit(False)
        #self.initiateReflectivityPlot.emit(False)

        # Update UI
        self.file_loaded_signal.emit()

    def update_info(self):
        """
            Write file metadata to the labels in the overview tab.
        """
        d=self._active_channel

        try:
            dangle0=u"%.3f° (%.3f°)"%(float(self.ui.dangle0Overwrite.text()), d.dangle0)
        except ValueError:
            dangle0=u"%.3f°"%(d.dangle0)
        if self.ui.directPixelOverwrite.value()>=0:
            dpix=u"%.1f (%.1f)"%(self.ui.directPixelOverwrite.value(), d.dpix)
        else:
            dpix=u"%.1f"%d.dpix
        self.ui.datasetLambda.setText(u"%.2f (%.2f-%.2f) Å"%(d.lambda_center,
                                                             d.lambda_center-1.5,
                                                             d.lambda_center+1.5))
        self.ui.datasetPCharge.setText(u"%.3e"%d.proton_charge)
        self.ui.datasetTime.setText(u"%i s"%d.total_time)
        self.ui.datasetTotCounts.setText(u"%.4e"%d.total_counts)
        try:
            self.ui.datasetRate.setText(u"%.1f cps"%(d.total_counts/d.total_time))
        except ZeroDivisionError:
            self.ui.datasetRate.setText(u"NaN")
        self.ui.datasetDangle.setText(u"%.3f°"%d.dangle)
        self.ui.datasetDangle0.setText(dangle0)
        self.ui.datasetSangle.setText(u"%.3f°"%d.sangle)
        self.ui.datasetDirectPixel.setText(dpix)
        self.ui.currentChannel.setText('%s (%s)   Type: %s   Current State: %s'%(
                                       d.number, d.experiment,
                                       d.measurement_type, d.name))

    def update_daslog(self):
        """
            Write parameters from all file daslogs to the table in the
            daslog tab.
        """
        table=self.ui.daslogTableBox
        table.setRowCount(0)
        table.sortItems(-1)
        table.setColumnCount(len(self._data_sets)+2)
        table.setHorizontalHeaderLabels(['Name']+self._data_sets.keys()+['Unit'])
        for j, key in enumerate(sorted(self._active_channel.logs.keys(), key=lambda s: s.lower())):
            table.insertRow(j)
            table.setItem(j, 0, QtWidgets.QTableWidgetItem(key))
            table.setItem(j, len(self._data_sets)+1,
                          QtWidgets.QTableWidgetItem(self._active_channel.log_units[key]))
            i = 0
            for xs in self._data_sets:
                item=QtWidgets.QTableWidgetItem(u'%g' % self._data_sets[xs].logs[key])
                item.setToolTip(u'MIN: %g   MAX: %g' % (self._data_sets[xs].log_minmax[key]))
                table.setItem(j, i+1, item)
                i += 1
        table.resizeColumnsToContents()

    def _plotActiveTab(self):
        '''
            Select the appropriate function to plot all visible images.
        '''
        if self._active_channel is None:
            return
        color=str(self.ui.color_selector.currentText())
        if color!=self.color and self.color is not None:
            self.color=color
            plots=[self.ui.xy_pp, self.ui.xy_mp, self.ui.xy_pm, self.ui.xy_mm,
                   self.ui.xtof_pp, self.ui.xtof_mp, self.ui.xtof_pm, self.ui.xtof_mm,
                   self.ui.xy_overview, self.ui.xtof_overview]
            for plot in plots:
                plot.clear_fig()
        elif self.color is None:
            self.color=color
        if self.ui.plotTab.currentIndex()!=4 and self._gisansThread:
            self._gisansThread.finished.disconnect()
            self._gisansThread.terminate()
            self._gisansThread.wait(100)
            self._gisansThread=None
        if self.ui.plotTab.currentIndex()==0:
            self.plot_overview()
        if self.ui.plotTab.currentIndex()==1:
            self.plot_xy()
        if self.ui.plotTab.currentIndex()==2:
            self.plot_xtof()
        if self.ui.plotTab.currentIndex()==3:
            self.plot_offspec()
        if self.ui.plotTab.currentIndex()==4:
            self.plot_gisans()

    def update_file_list(self):
        """
            Update the list of data files
        """
        # Update the list of files
        event_file_list = glob.glob(os.path.join(self._current_directory, '*event.nxs'))
        h5_file_list = glob.glob(os.path.join(self._current_directory, '*.nxs.h5'))
        event_file_list.extend(h5_file_list)
        event_file_list.sort()
        event_file_list = [os.path.basename(name) for name in event_file_list]

        current_list=[self.ui.file_list.item(i).text() for i in range(self.ui.file_list.count())]
        if event_file_list != current_list:
            self.ui.file_list.clear()
            for item in event_file_list:
                listitem = QtWidgets.QListWidgetItem(item, self.ui.file_list)
                if item == self._current_file_name:
                    self.ui.file_list.setCurrentItem(listitem)
        else:
            try:
                self.ui.file_list.setCurrentRow(event_file_list.index(self._current_file_name))
            except ValueError:
                logging.error("Could not set file selection: %s", self._current_file_name)
                logging.error(sys.exc_value)

    def reload_file(self):
        """
            Reload the file that is currently selected form the list.
        """
        self.fileOpen(self._current_file)

    def changeActiveChannel(self):
        '''
        The overview and reflectivity channel was changed. This
        recalculates already extracted reflectivities.
        '''
        return self.file_loaded()

    def plot_xy(self):
        '''
        X vs. Y plots for all channels.
        '''
        plots=[self.ui.xy_pp, self.ui.xy_mm, self.ui.xy_pm, self.ui.xy_mp]
        for i in range(len(self.active_data), 4):
            if plots[i].cplot is not None:
                plots[i].clear()
                plots[i].draw()
        imin=1e20
        imax=1e-20
        xynormed=[]
        for dataset in self._data_sets[:4]:
            d=dataset.xydata/dataset.proton_charge
            xynormed.append(d)
            if dataset.total_counts==0:
                continue
            imin=min(imin, d[d>0].min())
            imax=max(imax, d.max())

        if len(xynormed)>1:
            self.ui.frame_xy_mm.show()
            if len(xynormed)==4:
                self.ui.frame_xy_sf.show()
            else:
                self.ui.frame_xy_sf.hide()
        else:
            self.ui.frame_xy_mm.hide()
            self.ui.frame_xy_sf.hide()

        for i, datai in enumerate(xynormed):
            if self.active_data[i].total_counts==0:
                continue
            if self.ui.tthPhi.isChecked():
                plots[i].clear()
                rad_per_pixel=dataset.det_size_x/dataset.dist_sam_det/dataset.xydata.shape[1]
                phi_range=datai.shape[0]*rad_per_pixel*180./pi
                tth_range=datai.shape[1]*rad_per_pixel*180./pi
                phi0=self.ui.refYPos.value()*rad_per_pixel*180./pi
                tth0=(dataset.dangle-dataset.dangle0)-(datai.shape[1]-dataset.dpix)*rad_per_pixel*180./pi
                plots[i].imshow(datai, log=self.ui.logarithmic_colorscale.isChecked(), imin=imin, imax=imax,
                                aspect='auto', cmap=self.color, origin='lower',
                                extent=[tth_range+tth0, tth0, phi0, phi0-phi_range])
                plots[i].set_xlabel(u'2$\\Theta{}$ [°]')
                plots[i].set_ylabel(u'$\\phi{}$ [°]')
            else:
                plots[i].imshow(datai, log=self.ui.logarithmic_colorscale.isChecked(), imin=imin, imax=imax,
                                aspect='auto', cmap=self.color, origin='lower')
                plots[i].set_xlabel(u'x [pix]')
                plots[i].set_ylabel(u'y [pix]')
            plots[i].set_title(self.channels[i])
            if plots[i].cplot is not None:
                plots[i].cplot.set_clim([imin, imax])
            if plots[i].cplot is not None and self.ui.show_colorbars.isChecked() and plots[i].cbar is None:
                plots[i].cbar=plots[i].canvas.fig.colorbar(plots[i].cplot)
            plots[i].draw()

    def gather_options(self):
        """
            Gather the reduction options.
        """
        pass

    def setNorm(self): return NotImplemented
    def addRefList(self): return NotImplemented
    def clearRefList(self): return NotImplemented
    def overwriteDirectBeam(self): return NotImplemented
    def normalizeTotalReflection(self): return NotImplemented
    def removeRefList(self): return NotImplemented
    def reduceDatasets(self): return NotImplemented
    def clearOverwrite(self): return NotImplemented
    def loadExtraction(self): return NotImplemented
    def reductionTableChanged(self): return NotImplemented
    def clearNormList(self): return NotImplemented
    def change_offspec_colorscale(self): return NotImplemented
    def change_gisans_colorscale(self): return NotImplemented
    def open_compare_window(self): return NotImplemented
    def open_advanced_background(self): return NotImplemented
    def clip_offspec_colorscale(self): return NotImplemented
    def fileOpenSumDialog(self): return NotImplemented
    def changeRegionValues(self): return NotImplemented
    def toggleColorbars(self): return NotImplemented
    def overwriteChanged(self): return NotImplemented
    def toggleHide(self): return NotImplemented
    def fileOpenList(self): return NotImplemented
    def visualizePeakfinding(self): return NotImplemented
    def folderModified(self): return NotImplemented
    def replotProjections(self): return NotImplemented
    def openByNumber(self): return NotImplemented
    def cutPoints(self): return NotImplemented
    def autoRef(self): return NotImplemented
    def stripOverlap(self): return NotImplemented
    def open_polarization_window(self): return NotImplemented
    def open_rawdata_dialog(self): return NotImplemented
    def run_ipython(self): return NotImplemented
    def quickReduce(self): return NotImplemented
    def open_nxs_dialog(self): return NotImplemented
    def open_filter_dialog(self): return NotImplemented
    def open_reduction_preview(self): return NotImplemented
    def live_open(self): return NotImplemented
    def open_database_search(self): return NotImplemented
    def exportRawData(self): return NotImplemented

<|endoftext|>


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

import unittest
from mock import patch, MagicMock, call
from pip.utils import get_installed_version

from kubos import update
from kubos.utils.constants import *

class Args():
    def __init__(self):
        self.component = None

class UpdateTest(unittest.TestCase):
    @patch('kubos.update.update_source_modules')
    def test_exec_command_update_source(self, update_source_modules):
        args = Args()
        args.component = 's'

        update.execCommand(args, None)

        update_source_modules.assert_called_with(args)

    @patch('kubos.update.update_cli')
    def test_exec_command_update_cli(self, update_cli):
        args = Args()
        args.component = 'c'

        update.execCommand(args, None)

        update_cli.assert_called()

    @patch('subprocess.check_call')
    @patch('logging.info')
    def test_update_cli_success(self, info, check_call):
        return_code = 0
        check_call.return_value = return_code

        update.update_cli()

        check_call.assert_called_with(update.INSTALL_COMMAND)
        calls = [
            call('Updating the Kubos CLI...'),
            call('Succesfully updated the Kubos CLI module')
        ]
        info.assert_has_calls(calls)

    @patch('logging.error')
    @patch('subprocess.check_call')
    @patch('logging.info')
    def test_update_cli_error(self, info, check_call, error):
        return_code = -1
        check_call.return_value = return_code

        update.update_cli()

        check_call.assert_called_with(update.INSTALL_COMMAND)
        info.assert_called_with('Updating the Kubos CLI...')
        error.assert_called_with('There was an issue updating the Kubos CLI module. See the above log for the error details.')

    @patch('kubos.utils.git_utils.get_tag_list')
    @patch('kubos.utils.git_utils.get_latest_tag')
    @patch('kubos.utils.git_utils.check_provided_version')
    @patch('kubos.utils.status_spinner.stop_spinner')
    @patch('kubos.utils.git_utils.clone_example_repo')
    @patch('kubos.utils.git_utils.clone_repo')
    @patch('kubos.utils.status_spinner.start_spinner')
    @patch('logging.info')
    @patch('os.chdir')
    @patch('os.path.isdir')
    def test_update_source_modules(self, isdir, chdir, info, start_spinner, clone_repo, clone_example_repo, stop_spinner, check_provided_version, get_latest_tag, get_tag_list):
        args = Args()
        set_version = 'test-set_version'
        args.set_version = set_version
        args.latest = True
        isdir.return_value = True
        spinner = 'test-spinner'
        start_spinner.return_value = spinner
        src_repo = 'test-src-repo'
        clone_repo.return_value = src_repo
        latest_tag = 'latest-tag'
        get_latest_tag.return_value = latest_tag
        tag_list = []
        get_tag_list.return_value = tag_list

        update.update_source_modules(args)

        isdir.assert_called_with(update.KUBOS_DIR)
        chdir.assert_called_with(update.KUBOS_DIR)
        info_calls = [
            call('Checking for the most recent KubOS Source...'),
            call('Setting provided release: %s' % set_version),
            call('Setting latest release: %s' % latest_tag)
        ]
        info.assert_has_calls(info_calls)
        start_spinner.assert_called()
        clone_repo.assert_called_with(update.KUBOS_SRC_DIR, update.KUBOS_SRC_URL)
        clone_example_repo_calls = [
            call(update.KUBOS_RT_EXAMPLE_DIR, update.KUBOS_RT_EXAMPLE_URL),
            call(update.KUBOS_LINUX_EXAMPLE_DIR, update.KUBOS_LINUX_EXAMPLE_URL)
        ]
        clone_example_repo.assert_has_calls(clone_example_repo_calls)
        stop_spinner.assert_called_with(spinner)
        check_provided_version_calls = [
            call(set_version, src_repo),
            call(latest_tag, src_repo)
        ]
        check_provided_version.assert_has_calls(check_provided_version_calls)
        get_latest_tag.assert_called_with(tag_list)
        get_tag_list.assert_called_with(src_repo)

if __name__ == '__main__':
    unittest.main()
<commit_msg>Fixing misspelling in update unittest.<commit_after># Kubos CLI
# Copyright (C) 2017 Kubos Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
from mock import patch, MagicMock, call
from pip.utils import get_installed_version

from kubos import update
from kubos.utils.constants import *

class Args():
    def __init__(self):
        self.component = None

class UpdateTest(unittest.TestCase):
    @patch('kubos.update.update_source_modules')
    def test_exec_command_update_source(self, update_source_modules):
        args = Args()
        args.component = 's'

        update.execCommand(args, None)

        update_source_modules.assert_called_with(args)

    @patch('kubos.update.update_cli')
    def test_exec_command_update_cli(self, update_cli):
        args = Args()
        args.component = 'c'

        update.execCommand(args, None)

        update_cli.assert_called()

    @patch('subprocess.check_call')
    @patch('logging.info')
    def test_update_cli_success(self, info, check_call):
        return_code = 0
        check_call.return_value = return_code

        update.update_cli()

        check_call.assert_called_with(update.INSTALL_COMMAND)
        calls = [
            call('Updating the Kubos CLI...'),
            call('Successfully updated the Kubos CLI module')
        ]
        info.assert_has_calls(calls)

    @patch('logging.error')
    @patch('subprocess.check_call')
    @patch('logging.info')
    def test_update_cli_error(self, info, check_call, error):
        return_code = -1
        check_call.return_value = return_code

        update.update_cli()

        check_call.assert_called_with(update.INSTALL_COMMAND)
        info.assert_called_with('Updating the Kubos CLI...')
        error.assert_called_with('There was an issue updating the Kubos CLI module. See the above log for the error details.')

    @patch('kubos.utils.git_utils.get_tag_list')
    @patch('kubos.utils.git_utils.get_latest_tag')
    @patch('kubos.utils.git_utils.check_provided_version')
    @patch('kubos.utils.status_spinner.stop_spinner')
    @patch('kubos.utils.git_utils.clone_example_repo')
    @patch('kubos.utils.git_utils.clone_repo')
    @patch('kubos.utils.status_spinner.start_spinner')
    @patch('logging.info')
    @patch('os.chdir')
    @patch('os.path.isdir')
    def test_update_source_modules(self, isdir, chdir, info, start_spinner, clone_repo, clone_example_repo, stop_spinner, check_provided_version, get_latest_tag, get_tag_list):
        args = Args()
        set_version = 'test-set_version'
        args.set_version = set_version
        args.latest = True
        isdir.return_value = True
        spinner = 'test-spinner'
        start_spinner.return_value = spinner
        src_repo = 'test-src-repo'
        clone_repo.return_value = src_repo
        latest_tag = 'latest-tag'
        get_latest_tag.return_value = latest_tag
        tag_list = []
        get_tag_list.return_value = tag_list

        update.update_source_modules(args)

        isdir.assert_called_with(update.KUBOS_DIR)
        chdir.assert_called_with(update.KUBOS_DIR)
        info_calls = [
            call('Checking for the most recent KubOS Source...'),
            call('Setting provided release: %s' % set_version),
            call('Setting latest release: %s' % latest_tag)
        ]
        info.assert_has_calls(info_calls)
        start_spinner.assert_called()
        clone_repo.assert_called_with(update.KUBOS_SRC_DIR, update.KUBOS_SRC_URL)
        clone_example_repo_calls = [
            call(update.KUBOS_RT_EXAMPLE_DIR, update.KUBOS_RT_EXAMPLE_URL),
            call(update.KUBOS_LINUX_EXAMPLE_DIR, update.KUBOS_LINUX_EXAMPLE_URL)
        ]
        clone_example_repo.assert_has_calls(clone_example_repo_calls)
        stop_spinner.assert_called_with(spinner)
        check_provided_version_calls = [
            call(set_version, src_repo),
            call(latest_tag, src_repo)
        ]
        check_provided_version.assert_has_calls(check_provided_version_calls)
        get_latest_tag.assert_called_with(tag_list)
        get_tag_list.assert_called_with(src_repo)

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


<commit_before>import requests

from will.plugin import WillPlugin
from will.decorators import respond_to, periodic, hear, randomly, route, rendered_template, require_settings

class PugPlugin(WillPlugin):

    @hear(r'\bpugs?\b')
    def talk_on_pug(self, message):
        req = requests.get('http://pugme.herokuapp.com/random')
        if req.ok:
            pug = req.json()['pug']
            if 'media.tumblr.com' in pug:
                pug = 'https://media.tumblr.com' + pug.split('media.tumblr.com')[1]
                self.say(pug, message=message)
            else:
                self.say(pug, message=message)
<commit_msg>less code, same result<commit_after>import requests

from will.plugin import WillPlugin
from will.decorators import respond_to, periodic, hear, randomly, route, rendered_template, require_settings

class PugPlugin(WillPlugin):

    @hear(r'\bpugs?\b')
    def talk_on_pug(self, message):
        req = requests.get('http://pugme.herokuapp.com/random')
        if req.ok:
            pug = req.json()['pug']
            if 'media.tumblr.com' in pug:
                pug = 'https://media.tumblr.com' + pug.split('media.tumblr.com')[1]

            self.say(pug, message=message)
<|endoftext|>


<commit_before>"""
Utility functions for filename and path routines.
"""

import re
import os
import yaml
from natsort import natsorted
from os.path import join, dirname, realpath
from scipy.misc import imread

SUPPORTED_IMAGE_FILES = ['png', 'jpg', 'tiff', 'jpeg']

# A regexp to detect supported files. Used to filter images in a directory.
_image_regex = re.compile('.*(' + '|'.join(
    ['\\.' + ext for ext in SUPPORTED_IMAGE_FILES]) + ')$', re.IGNORECASE)

class SiteImage:
    """Utility class for site images files"""

    # (height, width) just like numpy shape
    size = None

    def __init__(self, filename, row_nr, col_nr):
        if not os.path.isabs(filename):
            self.filename = os.path.abspath(filename)
        else:
            self.filename = filename
        self.row_nr = row_nr
        self.col_nr = col_nr

    @staticmethod
    def from_filename(filename, cfg):
        regexp = cfg['COORDINATE_FROM_FILENAME']
        m = re.search(regexp, filename)
        if not m:
            raise Exception('Can\'t create SiteImage object '
                            'from filename ' + filename)
        else:
            row_nr, col_nr = map(int, m.groups())
            if cfg['COORDINATES_IN_FILENAME_ONE_BASED']:
                row_nr -= 1
                col_nr -= 1
            return SiteImage(filename, row_nr, col_nr)

    def as_numpy_array(self):
        return imread(self.filename)

    def get_size(self):
        if not SiteImage.size:
            SiteImage.size = self.as_numpy_array().shape
        return SiteImage.size

class CycleDirectory:
    """Utility class for cycle directories"""

    def __init__(self, filename, experiment_name, cycle_number):
        self.filename = filename
        self.experiment_name = experiment_name
        self.cycle_number = cycle_number

    @staticmethod
    def is_correct_filename(filename, cfg):
        regexp = regex_from_format_string(cfg['CYCLE_SUBDIRECTORY_NAME_FORMAT'])
        return re.match(regexp, filename) is not None

    @staticmethod
    def from_filename(filename, cfg):
        regexp = regex_from_format_string(cfg['CYCLE_SUBDIRECTORY_NAME_FORMAT'])
        m = re.match(regexp, filename)
        if not m:
            raise Exception('Can\'t create CycleDirectory object '
                            'from filename ' + filename)
        else:
            return CycleDirectory(
                filename, m.group('experiment_name'),
                int(m.group('cycle_number')))

    def __str__(self):
        return '%s - %s' % (self.experiment_name, self.cycle_number)

    def __unicode__(self):
        return self.__str__()

def is_image(filename):
    "Check if filename ends with a supported file extension"
    return _image_regex.match(filename) is not None

class Util:

    def __init__(self, config_settings):
        """
        Configuration settings provided by YAML file.
        """
        self.cfg = config_settings

    def get_cycle_nr_from_filename(self, filename):
        try:
            return int(re.search(self.cfg['CYCLE_NR_FROM_FILENAME'], filename).groups()[0])
        except Exception as error:
            raise Exception('Can\'t get cycle number from filename %s\n%s'
                            % (filename, error))

    def get_channel_nr_from_filename(self, filename):
        try:
            return int(re.search(self.cfg['CHANNEL_NR_FROM_FILENAME'], filename).groups()[0])
        except Exception as error:
            raise Exception('Can\'t get channel number from filename %s'
                            % (filename, error))

    def get_expname_from_filename(self, filename):
        try:
            return re.search(self.cfg['EXPERIMENT_NAME_FROM_FILENAME'], filename).groups()[0]
        except Exception as error:
            raise Exception('Can\'t get experiment name from filename %s'
                            % (filename, error))

    def get_cycle_directories(self, root_dir):
        dir_content = os.listdir(root_dir)
        dir_content = natsorted(dir_content)
        cycle_dirs = []
        for f in dir_content:
            if os.path.isdir(os.path.join(root_dir, f)) \
                    and CycleDirectory.is_correct_filename(f, self.cfg):
                cdir = CycleDirectory.from_filename(f, self.cfg)
                cycle_dirs.append(cdir)
        return cycle_dirs

    def get_image_files(self, root_dir, cycle_dir_object):
        image_folder = join(root_dir, self.cfg['IMAGE_FOLDER_LOCATION'].format(
            cycle_subdirectory=cycle_dir_object.filename))
        files = [join(image_folder, f) for f in os.listdir(image_folder)
                 if is_image(f)]
        files = natsorted(files)
        return files

    def get_image_dir(self, root_dir, cycle_dir_object):
        folder = join(root_dir, self.cfg['IMAGE_FOLDER_LOCATION'].format(
            cycle_subdirectory=cycle_dir_object.filename))
        return folder

    def get_segmentation_dir(self, root_dir, cycle_dir_object):
        folder = join(root_dir, self.cfg['SEGMENTATION_FOLDER_LOCATION'].format(
            cycle_subdirectory=cycle_dir_object.filename))
        return folder

    def get_shift_dir(self, root_dir, cycle_dir_object):
        folder = join(root_dir, self.cfg['SHIFT_FOLDER_LOCATION'].format(
            cycle_subdirectory=cycle_dir_object.filename))
        return folder

    def get_rootdir_from_image_file(self, imagefile):
        levels = len(self.cfg['IMAGE_FOLDER_LOCATION'].split('/'))
        return realpath(join(dirname(imagefile), *['..'] * levels))

def get_coord_by_regex(filename, pattern, one_based):
    """
    Get the coordinate for a filename assuming that the
    coordinate is encoded in the file's name.
    The returned coordinates must be 0-based!

    :filename: the image name : string
    :pattern:  the regex to extract the coordinates.
                  Needs 2 groups for row and col (in that order).
    :one_based: if the coordinates encoded in the file name are 1-based.
                If False they are assumed to be 0-based.
    :returns: 0-based row and column of the image : (int, int).

    """
    m = re.search(pattern, filename)
    if m is None:
        raise Exception(
            "Malformed filename! Couldn't extract row and column number with "
            "given regexp and filename: " + filename)
    else:
        row_nr, col_nr = map(int, m.groups())
        if one_based:
            return (row_nr - 1, col_nr - 1)
        else:
            return (row_nr, col_nr)

def regex_from_format_string(format_string):
    """
    Convert a format string of the sort
    '{experiment_name}_bla/something_{number}'
    to a named regular expression.
    """
    # Extract the names of all placeholders from the format string
    placeholders_inner_parts = re.findall(r'{(.+?)}', format_string)
    # Remove format strings
    placeholder_names = [pl.split(':')[0] for pl in placeholders_inner_parts]
    placeholder_regexes = [re.escape('{%s}') % pl
                           for pl in placeholders_inner_parts]

    regex = format_string
    for pl_name, pl_regex in zip(placeholder_names, placeholder_regexes):
        regex = re.sub(pl_regex, '(?P<%s>.*)' % pl_name, regex)

    return regex

def load_config(config_filename):
    '''Load project configuration from yaml file.'''
    if not os.path.exists(config_filename):
        raise Exception('Error: configuration file %s does not exist!'
                        % config_filename)
    return yaml.load(open(config_filename).read())

def check_config(config):
    yaml_keys = [
        'COORDINATE_FROM_FILENAME',
        'COORDINATES_IN_FILENAME_ONE_BASED',
        'SHIFT_DESCRIPTOR_FILE_LOCATION',
        'CYCLE_SUBDIRECTORY_NAME_FORMAT',
        'CYCLE_NR_FROM_FILENAME',
        'EXPERIMENT_NAME_FROM_FILENAME',
        'IMAGE_FOLDER_LOCATION',
        'STATS_FOLDER_LOCATION',
        'STATS_FILE_FORMAT',
        'CHANNEL_NR_FROM_FILENAME',
        'MEASUREMENT_FOLDER_LOCATION',
        'CELL_ID_FORMAT'
    ]
    for key in yaml_keys:
        if key not in config:
            print('Error: configuration file must contain the key "%s"' % key)
<commit_msg>Add new keys in config file to list of checked keys<commit_after>"""
Utility functions for filename and path routines.
"""

import re
import os
import yaml
from natsort import natsorted
from os.path import join, dirname, realpath
from scipy.misc import imread

SUPPORTED_IMAGE_FILES = ['png', 'jpg', 'tiff', 'jpeg']

# A regexp to detect supported files. Used to filter images in a directory.
_image_regex = re.compile('.*(' + '|'.join(
    ['\\.' + ext for ext in SUPPORTED_IMAGE_FILES]) + ')$', re.IGNORECASE)

class SiteImage:
    """Utility class for site images files"""

    # (height, width) just like numpy shape
    size = None

    def __init__(self, filename, row_nr, col_nr):
        if not os.path.isabs(filename):
            self.filename = os.path.abspath(filename)
        else:
            self.filename = filename
        self.row_nr = row_nr
        self.col_nr = col_nr

    @staticmethod
    def from_filename(filename, cfg):
        regexp = cfg['COORDINATE_FROM_FILENAME']
        m = re.search(regexp, filename)
        if not m:
            raise Exception('Can\'t create SiteImage object '
                            'from filename ' + filename)
        else:
            row_nr, col_nr = map(int, m.groups())
            if cfg['COORDINATES_IN_FILENAME_ONE_BASED']:
                row_nr -= 1
                col_nr -= 1
            return SiteImage(filename, row_nr, col_nr)

    def as_numpy_array(self):
        return imread(self.filename)

    def get_size(self):
        if not SiteImage.size:
            SiteImage.size = self.as_numpy_array().shape
        return SiteImage.size

class CycleDirectory:
    """Utility class for cycle directories"""

    def __init__(self, filename, experiment_name, cycle_number):
        self.filename = filename
        self.experiment_name = experiment_name
        self.cycle_number = cycle_number

    @staticmethod
    def is_correct_filename(filename, cfg):
        regexp = regex_from_format_string(cfg['CYCLE_SUBDIRECTORY_NAME_FORMAT'])
        return re.match(regexp, filename) is not None

    @staticmethod
    def from_filename(filename, cfg):
        regexp = regex_from_format_string(cfg['CYCLE_SUBDIRECTORY_NAME_FORMAT'])
        m = re.match(regexp, filename)
        if not m:
            raise Exception('Can\'t create CycleDirectory object '
                            'from filename ' + filename)
        else:
            return CycleDirectory(
                filename, m.group('experiment_name'),
                int(m.group('cycle_number')))

    def __str__(self):
        return '%s - %s' % (self.experiment_name, self.cycle_number)

    def __unicode__(self):
        return self.__str__()

def is_image(filename):
    "Check if filename ends with a supported file extension"
    return _image_regex.match(filename) is not None

class Util:

    def __init__(self, config_settings):
        """
        Configuration settings provided by YAML file.
        """
        self.cfg = config_settings

    def get_cycle_nr_from_filename(self, filename):
        try:
            return int(re.search(self.cfg['CYCLE_NR_FROM_FILENAME'], filename).groups()[0])
        except Exception as error:
            raise Exception('Can\'t get cycle number from filename %s\n%s'
                            % (filename, error))

    def get_channel_nr_from_filename(self, filename):
        try:
            return int(re.search(self.cfg['CHANNEL_NR_FROM_FILENAME'], filename).groups()[0])
        except Exception as error:
            raise Exception('Can\'t get channel number from filename %s'
                            % (filename, error))

    def get_expname_from_filename(self, filename):
        try:
            return re.search(self.cfg['EXPERIMENT_NAME_FROM_FILENAME'], filename).groups()[0]
        except Exception as error:
            raise Exception('Can\'t get experiment name from filename %s'
                            % (filename, error))

    def get_cycle_directories(self, root_dir):
        dir_content = os.listdir(root_dir)
        dir_content = natsorted(dir_content)
        cycle_dirs = []
        for f in dir_content:
            if os.path.isdir(os.path.join(root_dir, f)) \
                    and CycleDirectory.is_correct_filename(f, self.cfg):
                cdir = CycleDirectory.from_filename(f, self.cfg)
                cycle_dirs.append(cdir)
        return cycle_dirs

    def get_image_files(self, root_dir, cycle_dir_object):
        image_folder = join(root_dir, self.cfg['IMAGE_FOLDER_LOCATION'].format(
            cycle_subdirectory=cycle_dir_object.filename))
        files = [join(image_folder, f) for f in os.listdir(image_folder)
                 if is_image(f)]
        files = natsorted(files)
        return files

    def get_image_dir(self, root_dir, cycle_dir_object):
        folder = join(root_dir, self.cfg['IMAGE_FOLDER_LOCATION'].format(
            cycle_subdirectory=cycle_dir_object.filename))
        return folder

    def get_segmentation_dir(self, root_dir, cycle_dir_object):
        folder = join(root_dir, self.cfg['SEGMENTATION_FOLDER_LOCATION'].format(
            cycle_subdirectory=cycle_dir_object.filename))
        return folder

    def get_shift_dir(self, root_dir, cycle_dir_object):
        folder = join(root_dir, self.cfg['SHIFT_FOLDER_LOCATION'].format(
            cycle_subdirectory=cycle_dir_object.filename))
        return folder

    def get_rootdir_from_image_file(self, imagefile):
        levels = len(self.cfg['IMAGE_FOLDER_LOCATION'].split('/'))
        return realpath(join(dirname(imagefile), *['..'] * levels))

def get_coord_by_regex(filename, pattern, one_based):
    """
    Get the coordinate for a filename assuming that the
    coordinate is encoded in the file's name.
    The returned coordinates must be 0-based!

    :filename: the image name : string
    :pattern:  the regex to extract the coordinates.
                  Needs 2 groups for row and col (in that order).
    :one_based: if the coordinates encoded in the file name are 1-based.
                If False they are assumed to be 0-based.
    :returns: 0-based row and column of the image : (int, int).

    """
    m = re.search(pattern, filename)
    if m is None:
        raise Exception(
            "Malformed filename! Couldn't extract row and column number with "
            "given regexp and filename: " + filename)
    else:
        row_nr, col_nr = map(int, m.groups())
        if one_based:
            return (row_nr - 1, col_nr - 1)
        else:
            return (row_nr, col_nr)

def regex_from_format_string(format_string):
    """
    Convert a format string of the sort
    '{experiment_name}_bla/something_{number}'
    to a named regular expression.
    """
    # Extract the names of all placeholders from the format string
    placeholders_inner_parts = re.findall(r'{(.+?)}', format_string)
    # Remove format strings
    placeholder_names = [pl.split(':')[0] for pl in placeholders_inner_parts]
    placeholder_regexes = [re.escape('{%s}') % pl
                           for pl in placeholders_inner_parts]

    regex = format_string
    for pl_name, pl_regex in zip(placeholder_names, placeholder_regexes):
        regex = re.sub(pl_regex, '(?P<%s>.*)' % pl_name, regex)

    return regex

def load_config(config_filename):
    '''Load project configuration from yaml file.'''
    if not os.path.exists(config_filename):
        raise Exception('Error: configuration file %s does not exist!'
                        % config_filename)
    return yaml.load(open(config_filename).read())

def check_config(config):
    yaml_keys = [
        'COORDINATE_FROM_FILENAME',
        'COORDINATES_IN_FILENAME_ONE_BASED',
        'SHIFT_DESCRIPTOR_FILE_LOCATION',
        'CYCLE_SUBDIRECTORY_NAME_FORMAT',
        'CYCLE_NR_FROM_FILENAME',
        'EXPERIMENT_NAME_FROM_FILENAME',
        'IMAGE_FOLDER_LOCATION',
        'SEGMENTATION_FOLDER_LOCATION',
        'SHIFT_FOLDER_LOCATION',
        'STATS_FOLDER_LOCATION',
        'STATS_FILE_FORMAT',
        'CHANNEL_NR_FROM_FILENAME',
        'MEASUREMENT_FOLDER_LOCATION',
        'CELL_ID_FORMAT'
    ]
    for key in yaml_keys:
        if key not in config:
            print('Error: configuration file must contain the key "%s"' % key)
<|endoftext|>


<commit_before>#
# Creates 2010 archive for NiftyPlugins.
#
import zipfile
import os
import sys

QUIET = 0
VS2010 = 0
VS2005 = 0
DEVENV = ''

if 'VS100COMNTOOLS' in os.environ.keys():
    VS2010 = 1
    DEVENV = os.path.abspath(os.path.join(os.environ['VS100COMNTOOLS'], '..', 'IDE', 'devenv.com'))
elif 'VS80COMNTOOLS' in os.environ.keys():
    VS2005 = 1
    DEVENV = os.path.abspath(os.path.join(os.environ['VS80COMNTOOLS'], '..', 'IDE', 'devenv.com'))
else:
    print "No visual studio found!"

# (source name, dest dir)
FILES=[
    (r'NiftySolution\bin\NiftySolution.dll', ''),
    (r'NiftyPerforce\bin\NiftyPerforce.dll', ''),
    (r'NiftyPerforce\bin\AuroraCore.dll', ''),
    #(r'NiftySolution\NiftySolution2010.AddIn', ''),
    #(r'NiftyPerforce\NiftyPerforce2010.AddIn', ''),
    (r'NiftySolution\NiftySolution.AddIn', ''),
    (r'NiftyPerforce\NiftyPerforce.AddIn', ''),
    (r'NiftySolution\bin\en-US\NiftySolution.resources.dll', 'en-US'),
    (r'NiftyPerforce\bin\en-US\NiftyPerforce.resources.dll', 'en-US'),
]

BASE=os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
DISTDIR=os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'Build'))
SOLUTION2010='NiftyPlugins2010.sln'
SOLUTION2005='NiftyPlugins.sln'

def execute(commandline, silent = 1):
    """
        Executes a command line and if the command fails, exits.
    """
    if QUIET:
        silent = 1

    logfile = os.path.abspath(os.path.join(os.environ['TEMP'], 'capture_tmp.txt'))
    batchfile = os.path.abspath(os.path.join(os.environ['TEMP'], 'capture_tmp.cmd'))

    try:
        os.unlink(logfile)
    except WindowsError:
        pass

    if silent:
        open(batchfile, 'wt').write(
            '@echo off\n\n%s > "%s" 2>&1' % (commandline, logfile)
        )
    else:
        open(batchfile, 'wt').write(
            '%s' % (commandline)
        )

    result = os.system(batchfile)
    # We failed. Damn.
    if 0 != result:
        lines = open(logfile).readlines()
        filtered = []
        pattern = re.compile('error ([^:]+):', re.I)
        for line in lines:
            if pattern.search(line):
                filtered.append(line)

        if silent:
            print '>>> The output from the program was: '
            print ''.join(filtered)
            print '>>> execution stopped due to error (truncated output above)'
        else:
            print '>>> execution stopped due to error'
        sys.exit(result)

def cleanSolution(solutionfile, config):
    if VS2010:
        name, ext = os.path.splitext(solutionfile)
        solutionfile = name + '_VS2010' + ext
        config = config.split('|')[0]
        execute('m "%(solutionfile)s" /t:Clean "/p:Configuration=%(config)s"' % locals())
        return
    if VS2005:
        devenv = DEVENV
        execute( '"%(devenv)s" "%(solutionfile)s" /clean "%(config)s"' % locals() )
        return

def buildSolution(solutionfile, config):
    if VS2010:
        name, ext = os.path.splitext(solutionfile)
        solutionfile = name + '_VS2010' + ext
        config = config.split('|')[0]
        execute('m "%(solutionfile)s" /t:Build "/p:Configuration=%(config)s"' % locals())
        return
    if VS2005:
        devenv = DEVENV
        execute( '"%(devenv)s" "%(solutionfile)s" /build "%(config)s"' % locals(), 0 )
        return

def msbuild(solution, config, command):
    MSBUILD=r'%s\Microsoft.NET\Framework64\v4.0.30319\MsBuild.exe' % os.environ['windir']
    cmdline = '%(MSBUILD)s /nologo /verbosity:minimal "%(solution)s" /t:%(command)s /p:Configuration=%(config)s' % locals()
    res = os.system(cmdline)
    if 0 != res:
        print 'Failed to execute %s' % cmdline
        print 'Result: %s' % res
        sys.exit(res)

def makearchive(filename):
    z = zipfile.ZipFile(filename, 'w')
    for name, destdir in FILES:
        fullname = os.path.join(BASE, name)
        basename = os.path.basename(fullname)
        destname = os.path.join(destdir, basename)

        z.write(fullname, destname)
    z.close()

def main(args):
    """
Usage: build.py <version>
    """

    if len(args) != 1:
        print main.__doc__
        return 1

    version = args[0]

    if VS2010:
        msbuild(os.path.join(BASE, SOLUTION2010), 'Release', 'Rebuild')
    elif VS2005:
        cleanSolution(os.path.join(BASE, SOLUTION2005), 'Release')
        buildSolution(os.path.join(BASE, SOLUTION2005), 'Release')

    makearchive(os.path.join(DISTDIR, 'NiftyPlugins-%s.zip' % version))

if __name__ == '__main__':
    sys.exit( main(sys.argv[1:]) )
<commit_msg>- Added packing of meta files.<commit_after>#
# Creates 2010 archive for NiftyPlugins.
#
import zipfile
import os
import sys

QUIET = 0
VS2010 = 0
VS2005 = 0
DEVENV = ''

if 'VS100COMNTOOLS' in os.environ.keys():
    VS2010 = 1
    DEVENV = os.path.abspath(os.path.join(os.environ['VS100COMNTOOLS'], '..', 'IDE', 'devenv.com'))
elif 'VS80COMNTOOLS' in os.environ.keys():
    VS2005 = 1
    DEVENV = os.path.abspath(os.path.join(os.environ['VS80COMNTOOLS'], '..', 'IDE', 'devenv.com'))
else:
    print "No visual studio found!"

# (source name, dest dir)
FILES=[
    (r'README', ''),
    (r'COPYING', ''),
    (r'AUTHORS', ''),
    (r'NiftySolution\bin\NiftySolution.dll', ''),
    (r'NiftyPerforce\bin\NiftyPerforce.dll', ''),
    (r'NiftyPerforce\bin\AuroraCore.dll', ''),
    #(r'NiftySolution\NiftySolution2010.AddIn', ''),
    #(r'NiftyPerforce\NiftyPerforce2010.AddIn', ''),
    (r'NiftySolution\NiftySolution.AddIn', ''),
    (r'NiftyPerforce\NiftyPerforce.AddIn', ''),
    (r'NiftySolution\bin\en-US\NiftySolution.resources.dll', 'en-US'),
    (r'NiftyPerforce\bin\en-US\NiftyPerforce.resources.dll', 'en-US'),
]

BASE=os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
DISTDIR=os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'Build'))
SOLUTION2010='NiftyPlugins2010.sln'
SOLUTION2005='NiftyPlugins.sln'

def execute(commandline, silent = 1):
    """
        Executes a command line and if the command fails, exits.
    """
    if QUIET:
        silent = 1

    logfile = os.path.abspath(os.path.join(os.environ['TEMP'], 'capture_tmp.txt'))
    batchfile = os.path.abspath(os.path.join(os.environ['TEMP'], 'capture_tmp.cmd'))

    try:
        os.unlink(logfile)
    except WindowsError:
        pass

    if silent:
        open(batchfile, 'wt').write(
            '@echo off\n\n%s > "%s" 2>&1' % (commandline, logfile)
        )
    else:
        open(batchfile, 'wt').write(
            '%s' % (commandline)
        )

    result = os.system(batchfile)
    # We failed. Damn.
    if 0 != result:
        lines = open(logfile).readlines()
        filtered = []
        pattern = re.compile('error ([^:]+):', re.I)
        for line in lines:
            if pattern.search(line):
                filtered.append(line)

        if silent:
            print '>>> The output from the program was: '
            print ''.join(filtered)
            print '>>> execution stopped due to error (truncated output above)'
        else:
            print '>>> execution stopped due to error'
        sys.exit(result)

def cleanSolution(solutionfile, config):
    if VS2010:
        name, ext = os.path.splitext(solutionfile)
        solutionfile = name + '_VS2010' + ext
        config = config.split('|')[0]
        execute('m "%(solutionfile)s" /t:Clean "/p:Configuration=%(config)s"' % locals())
        return
    if VS2005:
        devenv = DEVENV
        execute( '"%(devenv)s" "%(solutionfile)s" /clean "%(config)s"' % locals() )
        return

def buildSolution(solutionfile, config):
    if VS2010:
        name, ext = os.path.splitext(solutionfile)
        solutionfile = name + '_VS2010' + ext
        config = config.split('|')[0]
        execute('m "%(solutionfile)s" /t:Build "/p:Configuration=%(config)s"' % locals())
        return
    if VS2005:
        devenv = DEVENV
        execute( '"%(devenv)s" "%(solutionfile)s" /build "%(config)s"' % locals(), 0 )
        return

def msbuild(solution, config, command):
    MSBUILD=r'%s\Microsoft.NET\Framework64\v4.0.30319\MsBuild.exe' % os.environ['windir']
    cmdline = '%(MSBUILD)s /nologo /verbosity:minimal "%(solution)s" /t:%(command)s /p:Configuration=%(config)s' % locals()
    res = os.system(cmdline)
    if 0 != res:
        print 'Failed to execute %s' % cmdline
        print 'Result: %s' % res
        sys.exit(res)

def makearchive(filename):
    z = zipfile.ZipFile(filename, 'w')
    for name, destdir in FILES:
        fullname = os.path.join(BASE, name)
        basename = os.path.basename(fullname)
        destname = os.path.join(destdir, basename)

        z.write(fullname, destname)
    z.close()

def main(args):
    """
Usage: build.py <version>
    """

    if len(args) != 1:
        print main.__doc__
        return 1

    version = args[0]

    if VS2010:
        msbuild(os.path.join(BASE, SOLUTION2010), 'Release', 'Rebuild')
    elif VS2005:
        cleanSolution(os.path.join(BASE, SOLUTION2005), 'Release')
        buildSolution(os.path.join(BASE, SOLUTION2005), 'Release')

    makearchive(os.path.join(DISTDIR, 'NiftyPlugins-%s.zip' % version))

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


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

#Importa as bibliotecas necessárias
import feedparser, urllib2, os, re

#-----------------------------------#
#------CONFIGURACOES DO USUARIO-----#
#-----------------------------------#

#Define o diretório que quer salvar os arquivos
os.chdir( '/home/nome_do_usuario' )
#Ex: os.chdir( '/home/fulano/downloads' )
#Tenha certeza que tem direito para escrever no diretório

#Define os releases que deverão ser baixados
#Não se esqueça de colocar o nome de cada release separado por vírgula e dentro de aspas duplas
RELEASES = "Release1|Release2|Release3"
#Ex: RELEASES = "Game.of.Thrones|Spiderman|Da.Vinci"

#Aqui é preciso definir os cookies necessários para acessar o site
PASS = "Sua_Passkey_do_BJ2" #Coloque aqui sua pass do cookie. Não se esqueça de colocar entre aspas.
#Ex: PASS = "abcdefg123456789"
UID = "Sua_UID_do_BJ2" #Coloque aqui seu UID do BJ2. Não se esqueça de colocar entre aspas.
#Ex: UID = "123456789"

#------------------------------------#
#----NAO EDITE NADA A PARTIR DAQUI---#
#------------------------------------#

#Verifica se algum release definido satisfaz algum download
if (PASS == "Sua_Passkey_do_BJ2" or UID == "Sua_UID_do_BJ2" or RELEASES == "Release1|Release2|Release3"):
        print( 'Modifique as informações antes de utilizar o script.' )
        print( 'Toda informação está aqui: https://github.com/pulgalipe/bj2feeder\nBye!' )
else:
        print('Procurando por torrents para baixar...\n')
        #Define a URL do Tracker que sera acessado pelo feedparser
        #Utilize o metodo "feedparser.parse" sempre que precisar acessar um feed
        bjurl = 'http://bj2.me/rss.php'
        bjurl = feedparser.parse( bjurl )

        for rel in range( len(RELEASES) ):
                #Percorre a lista de feeds e os mostra um a um
                for max_feeds in range( 20 ):
                        #percorre os feeds e mostra apenas os que tem alguma palavra definida no
                        title = bjurl.entries[ max_feeds ].title
                        link = bjurl.entries[ max_feeds ].link
                        link = link.replace( 'detalhes', 'download' )
                        if re.search( RELEASES[ rel ], title, re.I ):
                                print( 'Baixando Torrent: ' + title )
                                title = title.encode('ascii', 'ignore')
                                print( 'Link: ' + link )
                                os.system( "wget -q --header \"Cookie: pass=" + PASS + ";uid=" + UID + "\" -O \"" + title + ".torrent\" " + link )
                                print( 'Download concluido.\n' )
print( 'Script finalizado com sucesso!' )

<commit_msg>Update bj2feeder.py<commit_after># -*- coding: utf-8 -*-
#!/usr/bin/python

#Importa as bibliotecas necessárias
import feedparser, urllib2, os, re

#-----------------------------------#
#------CONFIGURACOES DO USUARIO-----#
#-----------------------------------#

#Define o diretório que quer salvar os arquivos
os.chdir( '/home/nome_do_usuario' )
#Ex: os.chdir( '/home/fulano/downloads' )
#Tenha certeza que tem direito para escrever no diretório

#Define os releases que deverão ser baixados
#Não se esqueça de colocar o nome de cada release separado por vírgula e dentro de aspas duplas
RELEASES = "Release1|Release2|Release3"
#Ex: RELEASES = "Game.of.Thrones|Spiderman|Da.Vinci"

#Aqui é preciso definir os cookies necessários para acessar o site
PASS = "Sua_Passkey_do_BJ2" #Coloque aqui sua pass do cookie. Não se esqueça de colocar entre aspas.
#Ex: PASS = "abcdefg123456789"
UID = "Sua_UID_do_BJ2" #Coloque aqui seu UID do BJ2. Não se esqueça de colocar entre aspas.
#Ex: UID = "123456789"

#------------------------------------#
#----NAO EDITE NADA A PARTIR DAQUI---#
#------------------------------------#

#Verifica se algum release definido satisfaz algum download
if (PASS == "Sua_Passkey_do_BJ2" or UID == "Sua_UID_do_BJ2" or RELEASES == "Release1|Release2|Release3"):
        print( 'Modifique as informações antes de utilizar o script.' )
        print( 'Toda informação está aqui: https://github.com/pulgalipe/bj2feeder\nBye!' )
else:
        print('Procurando por torrents para baixar...\n')
        #Define a URL do Tracker que sera acessado pelo feedparser
        #Utilize o metodo "feedparser.parse" sempre que precisar acessar um feed
        bjurl = 'http://bj2.me/rss.php'
        bjurl = feedparser.parse( bjurl )

        for rel in range( len(RELEASES) ):
                #Percorre a lista de feeds e os mostra um a um
                for max_feeds in range( 20 ):
                        #percorre os feeds e mostra apenas os que tem alguma palavra definida no
                        title = bjurl.entries[ max_feeds ].title
                        link = bjurl.entries[ max_feeds ].link
                        link = link.replace( 'detalhes', 'download' )
                        if re.search( RELEASES[ rel ], title, re.I ):
                                print( 'Baixando Torrent: ' + title )
                                title = title.encode('ascii', 'ignore')
                                print( 'Link: ' + link )
                                os.system( "wget -q --header \"Cookie: pass=" + PASS + ";uid=" + UID + "\" -O \"" + title + ".torrent\" " + link )
                                print( 'Download concluido.\n' )
        print( 'Script finalizado com sucesso!' )
<|endoftext|>


<commit_before>#!/usr/bin/env python2
# -*- coding: utf-8 -*-
'''
Just render something.

Usage:
    ./sclrq.py [options] (--show|-s) <i>
    ./sclrq.py [options] <i> <outname>

Options:
    --help -h
    --show -s           Show
    --nozip -U          flds are NOT gzipped.
    --log10 -l          Log it.
    --lims=LIM          Set lims [default: (1e18,1e23)]
    --highlight=H       Set highlight.
    --quantity=Q -Q Q   Render this quantity [default: RhoN10]
    --dir=D -D D        Read from this dir [default: .]
    --restrict=R        Restrict it.
    --x-restrict=R      Restrict by positions as a 4 tuple.
    --title=T           Set the title [default: Electron density]
    --units=U           Set the colorbar units [default: number/cc]
    --laser             Plot contours of the laser poyting vector.
    --intensity=I -I I  Make a contour of this intensity [default: 3e18]
'''
from docopt import docopt;
import numpy as np;
import numpy.linalg as lin;
from pys import parse_ftuple, parse_ituple;
from lspreader.flds import read_indexed, restrict
from lspplot.sclr import S;
from lspplot.pc import pc,highlight;
from lspplot.consts import c,mu0,e0;

opts = docopt(__doc__,help=True);
gzip = not opts['--nozip'];
quantity = opts['--quantity'];

fvar=['E','B'] if opts['--laser'] else None;
titlestr=opts['--title']
units=opts['--units'];
svar=[quantity];
#####################################
#reading data
d = read_indexed(int(opts['<i>']),
    flds=fvar,sclr=svar,
    gzip=gzip,dir=opts['--dir'],
              gettime=True,vector_norms=False);
#choosing positions
ylabel =  'z' if np.isclose(d['y'].max(),d['y'].min()) else 'y';

if opts['--x-restrict']:
    res = parse_ftuple(opts['--x-restrict'], length=4);
    res[:2] = [ np.abs(d['x'][:,0]*1e4 - ires).argmin() for ires in res[:2] ];
    res[2:] = [ np.abs(d[ylabel][0,:]*1e4 - ires).argmin() for ires in res[2:] ];
    restrict(d,res);
elif opts['--restrict']:
    res = parse_ituple(opts['--restrict'],length=None);
    restrict(d,res);

x,y = d['x']*1e4, d[ylabel]*1e4;
#massaging data
t  = d['t'];
q = d[quantity];

#####################################
#plotting

#getting options from user
mn,mx = parse_ftuple(opts['--lims'],length=2);

#plot the density
title="{}\nTime: {:.2f} fs".format(titlestr,t*1e6);
r=pc(
    q,(x,y), lims=(mn,mx),log=opts['--log10'],
    clabel=units, title=title,
    agg=not opts['--show']);

if opts['--highlight']:
    myhi  = float(opts['--highlight']);
    highlight(
        r, myhi,
        color="lightyellow", alpha=0.5);

if opts['--laser']:
    laser = S(d);
    I = float(opts['--intensity']);
    highlight(r, I, q=laser,
              color="red", alpha=0.15);

import matplotlib.pyplot as plt;
if opts['--show']:
    plt.show();
else:
    plt.savefig(opts['<outname>']);

<commit_msg>include the edges<commit_after>#!/usr/bin/env python2
# -*- coding: utf-8 -*-
'''
Just render something.

Usage:
    ./sclrq.py [options] (--show|-s) <i>
    ./sclrq.py [options] <i> <outname>

Options:
    --help -h
    --show -s           Show
    --nozip -U          flds are NOT gzipped.
    --log10 -l          Log it.
    --lims=LIM          Set lims [default: (1e18,1e23)]
    --highlight=H       Set highlight.
    --quantity=Q -Q Q   Render this quantity [default: RhoN10]
    --dir=D -D D        Read from this dir [default: .]
    --restrict=R        Restrict it.
    --x-restrict=R      Restrict by positions as a 4 tuple.
    --title=T           Set the title [default: Electron density]
    --units=U           Set the colorbar units [default: number/cc]
    --laser             Plot contours of the laser poyting vector.
    --intensity=I -I I  Make a contour of this intensity [default: 3e18]
'''
from docopt import docopt;
import numpy as np;
import numpy.linalg as lin;
from pys import parse_ftuple, parse_ituple;
from lspreader.flds import read_indexed, restrict
from lspplot.sclr import S;
from lspplot.pc import pc,highlight;
from lspplot.consts import c,mu0,e0;

opts = docopt(__doc__,help=True);
gzip = not opts['--nozip'];
quantity = opts['--quantity'];

fvar=['E','B'] if opts['--laser'] else None;
titlestr=opts['--title']
units=opts['--units'];
svar=[quantity];
#####################################
#reading data
d = read_indexed(int(opts['<i>']),
    flds=fvar,sclr=svar,
    gzip=gzip,dir=opts['--dir'],
              gettime=True,vector_norms=False);
#choosing positions
ylabel =  'z' if np.isclose(d['y'].max(),d['y'].min()) else 'y';

if opts['--x-restrict']:
    res = parse_ftuple(opts['--x-restrict'], length=4);
    res[:2] = [ np.abs(d['x'][:,0]*1e4 - ires).argmin() for ires in res[:2] ];
    res[2:] = [ np.abs(d[ylabel][0,:]*1e4 - ires).argmin() for ires in res[2:] ];
    #including the edges
    res[1]+=1;
    res[3]+=1;
    restrict(d,res);
elif opts['--restrict']:
    res = parse_ituple(opts['--restrict'],length=None);
    restrict(d,res);

x,y = d['x']*1e4, d[ylabel]*1e4;
#massaging data
t  = d['t'];
q = d[quantity];

#####################################
#plotting

#getting options from user
mn,mx = parse_ftuple(opts['--lims'],length=2);

#plot the density
title="{}\nTime: {:.2f} fs".format(titlestr,t*1e6);
r=pc(
    q,(x,y), lims=(mn,mx),log=opts['--log10'],
    clabel=units, title=title,
    agg=not opts['--show']);

if opts['--highlight']:
    myhi  = float(opts['--highlight']);
    highlight(
        r, myhi,
        color="lightyellow", alpha=0.5);

if opts['--laser']:
    laser = S(d);
    I = float(opts['--intensity']);
    highlight(r, I, q=laser,
              color="red", alpha=0.15);

import matplotlib.pyplot as plt;
if opts['--show']:
    plt.show();
else:
    plt.savefig(opts['<outname>']);

<|endoftext|>


<commit_before>from django import forms
from django.contrib.auth import get_user_model
from django.forms.formsets import formset_factory
from django.utils.safestring import mark_safe

from .models import Date, MultipleChoice, PageBase, QuestionPage, TextPage

User = get_user_model()

class BasePageForm(forms.Form):
    sections = dict(PageBase.SECTION_CHOICES)

class QuestionPageForm(BasePageForm):

    def __init__(self, *args, **kwargs):
        super(QuestionPageForm, self).__init__(*args, **kwargs)
        self.has_tooltip = False
        extra_fields = {}
        date_fields = []
        for item in self.items:
            question_id = 'question_%s' % item.pk
            self.fields[question_id] = item.make_field()
            self.fields[question_id].help_text = mark_safe(item.descriptive_text + self.fields[question_id].help_text)
            if isinstance(item, MultipleChoice):
                for idx, choice_with_extra in enumerate(item.get_choices()):
                    placeholder = choice_with_extra.extra_info_placeholder
                    if placeholder:
                        extra_dict = {}
                        extra_field = forms.CharField(label=mark_safe("%s" % placeholder),
                                                      required=False,
                                                      widget=forms.TextInput(attrs={'class':
                                                                                "form-control extra_text_info",
                                                                                'placeholder': placeholder}),)
                        id_for_extra_field = '%s_extra-%s' % (question_id, choice_with_extra.pk)
                        self.fields[id_for_extra_field] = extra_field
                        radio_button_name = "%s-%s" % (self.prefix, question_id)
                        extra_dict['radio_button_name'] = radio_button_name
                        extra_dict['extra_field_id'] = '%s-%s' % (self.prefix, id_for_extra_field)
                        extra_checked = "%s_%s" % (radio_button_name, idx)
                        extra_dict['extra_checked'] = extra_checked
                        extra_fields["id_%s" % extra_checked] = extra_dict
            if isinstance(item, Date):
                date_fields.append('id_%s-%s' % (self.prefix, question_id))
        if len(extra_fields) > 0:
            self.extra_fields = extra_fields
        if len(date_fields) > 0:
            self.date_fields = date_fields

class TextPageForm(BasePageForm):
    pass

def get_form_pages(page_map):
    pages = sorted(page_map, key=lambda p: p[0].position)
    generated_forms = []
    section_map = {}
    for (section, _) in PageBase.SECTION_CHOICES:
        start = next((idx for idx, page in enumerate(pages) if page[0].section == section), None)
        section_map[section] = start

    for idx, (page, item_set) in enumerate(pages):
        if isinstance(page, QuestionPage):
            FormType = type('Page%iForm' % idx, (QuestionPageForm,),
                            {"items": sorted(item_set, key=lambda i: i.position),
                             "encouragement": page.encouragement,
                             "infobox": page.infobox,
                             "page_section": page.section,
                             "section_map": section_map,
                             "page_index": idx})
            if page.multiple:
                FormSet = formset_factory(FormType, extra=0, min_num=1)
                FormSetType = type('FormSetForm', (FormSet,), {"sections": dict(PageBase.SECTION_CHOICES),
                                                               "name_for_multiple": page.name_for_multiple,
                                                               "page_id": page.pk,
                                                               "page_section": page.section,
                                                               "section_map": section_map,
                                                               "page_index": idx})
                generated_forms.append(FormSetType)
            else:
                generated_forms.append(FormType)
        elif isinstance(page, TextPage):
            FormType = type('Page%iForm' % idx, (TextPageForm,),
                            {"title": page.title,
                             "text": page.text,
                             "page_section": page.section,
                             "section_map": section_map})
            generated_forms.append(FormType)
    return generated_forms
<commit_msg>remove styling from extra fields<commit_after>from django import forms
from django.contrib.auth import get_user_model
from django.forms.formsets import formset_factory
from django.utils.safestring import mark_safe

from .models import Date, MultipleChoice, PageBase, QuestionPage, TextPage

User = get_user_model()

class BasePageForm(forms.Form):
    sections = dict(PageBase.SECTION_CHOICES)

class QuestionPageForm(BasePageForm):

    def __init__(self, *args, **kwargs):
        super(QuestionPageForm, self).__init__(*args, **kwargs)
        self.has_tooltip = False
        extra_fields = {}
        date_fields = []
        for item in self.items:
            question_id = 'question_%s' % item.pk
            self.fields[question_id] = item.make_field()
            self.fields[question_id].help_text = mark_safe(item.descriptive_text + self.fields[question_id].help_text)
            if isinstance(item, MultipleChoice):
                for idx, choice_with_extra in enumerate(item.get_choices()):
                    placeholder = choice_with_extra.extra_info_placeholder
                    if placeholder:
                        extra_dict = {}
                        extra_field = forms.CharField(required=False, max_length=500)
                        id_for_extra_field = '%s_extra-%s' % (question_id, choice_with_extra.pk)
                        self.fields[id_for_extra_field] = extra_field
                        radio_button_name = "%s-%s" % (self.prefix, question_id)
                        extra_dict['radio_button_name'] = radio_button_name
                        extra_dict['extra_field_id'] = '%s-%s' % (self.prefix, id_for_extra_field)
                        extra_checked = "%s_%s" % (radio_button_name, idx)
                        extra_dict['extra_checked'] = extra_checked
                        extra_fields["id_%s" % extra_checked] = extra_dict
            if isinstance(item, Date):
                date_fields.append('id_%s-%s' % (self.prefix, question_id))
        if len(extra_fields) > 0:
            self.extra_fields = extra_fields
        if len(date_fields) > 0:
            self.date_fields = date_fields

class TextPageForm(BasePageForm):
    pass

def get_form_pages(page_map):
    pages = sorted(page_map, key=lambda p: p[0].position)
    generated_forms = []
    section_map = {}
    for (section, _) in PageBase.SECTION_CHOICES:
        start = next((idx for idx, page in enumerate(pages) if page[0].section == section), None)
        section_map[section] = start

    for idx, (page, item_set) in enumerate(pages):
        if isinstance(page, QuestionPage):
            FormType = type('Page%iForm' % idx, (QuestionPageForm,),
                            {"items": sorted(item_set, key=lambda i: i.position),
                             "encouragement": page.encouragement,
                             "infobox": page.infobox,
                             "page_section": page.section,
                             "section_map": section_map,
                             "page_index": idx})
            if page.multiple:
                FormSet = formset_factory(FormType, extra=0, min_num=1)
                FormSetType = type('FormSetForm', (FormSet,), {"sections": dict(PageBase.SECTION_CHOICES),
                                                               "name_for_multiple": page.name_for_multiple,
                                                               "page_id": page.pk,
                                                               "page_section": page.section,
                                                               "section_map": section_map,
                                                               "page_index": idx})
                generated_forms.append(FormSetType)
            else:
                generated_forms.append(FormType)
        elif isinstance(page, TextPage):
            FormType = type('Page%iForm' % idx, (TextPageForm,),
                            {"title": page.title,
                             "text": page.text,
                             "page_section": page.section,
                             "section_map": section_map})
            generated_forms.append(FormType)
    return generated_forms
<|endoftext|>