<commit_before>#!/usr/bin/env python
# coding=utf-8

## @package biopredyn
## $Author$
## $Date$
## $Copyright: 2014, The CoSMo Company, All Rights Reserved $
## $License: BSD 3-Clause $
## $Revision$

import libsbmlsim
import libnuml
from matplotlib import pyplot as plt
import array

## Base class for simulation results
class Result:
  ## @var result
  # Pointer to the output of a simulation run. Results are always stored as
  # a dictionary where keys are names and values are arrays, for instance:
  #
  # keys ---> |  Time  | Species_1 | Species_2 | Error_1 | Error_2 |
  #           |--------|-----------|-----------|---------|---------|
  #           | 0.01   | 1.256e-6  | 5.21e-5   | 2.5e-8  | 1.23e-5 |
  #           | 0.16   | 1.004e-6  | 5.51e-5   | 6.7e-8  | 1.00e-5 |
  # values -> | 0.23   | 8.564e-7  | 5.13e-5   | 5.9e-7  | 8.96e-6 |
  #           | 0.47   | 5.735e-7  | 5.03e-5   | 2.4e-8  | 1.02e-5 |
  #           | 0.59   | 3.246e-7  | 5.45e-5   | 3.1e-8  | 1.17e-5 |

  ## Constructor.
  # @param self The object pointer.
  def __init__(self):
    self.result = dict()

  ## Returns the value of all fluxes in self.result as a vector of values.
  # @param self The object pointer.
  # @return The vector of fluxes, without growth_rate.
  def get_fluxes(self):
    vector = []
    for f in self.result:
      if str(f) != "growth_rate":
        vector.append(self.result[f])
    return vector

  ## Returns the number of time series in self.result; potential "time" series
  ## are not counted as time series.
  # @param self The object pointer.
  # @return The numbr of time series in self.result.
  def get_number_of_series(self):
    number = 0;
    for i in self.result:
      if str.lower(i) != "time":
        number = number + 1
    return number

  ## Returns a list containing all the quantity values for the input species
  ## over time.
  # @param self The object pointer.
  # @param species The species which quantity values are wanted.
  # @return A list of quantity values for the input species over time.
  def get_quantities_per_species(self, species):
    return self.result[species]

  ## Getter. Returns self.result.
  # @param self The object pointer.
  # @return self.result
  def get_result(self):
    return self.result

  ## Returns the list of all time steps in self.result.
  # @param self The object pointer.
  # @return The list of time steps.
  def get_time_steps(self):
    for t in self.result:
      if str.lower(t) == "time":
        return self.result[t]
    sys.exit("Error: no time series found.")

  ## Import numerical values from the output of a cobrapy flux balance analysis
  ## and store them in self.result. The resulting growth rate is stored as the
  ## value of the 'growth_rate' key.
  # @param self The object pointer.
  # @param solution Solution of a cobrapy FBA.
  def import_from_cobrapy_fba(self, solution):
    self.result["growth_rate"] = [solution.f]
    for p in solution.x_dict.iteritems():
      self.result[p[0]] = [p[1]]

  ## Import numerical values from a COPASI.CTimeSeries object.
  # @param self The object pointer.
  # @param time_series A COPASI.CTimeSeries object.
  def import_from_copasi_time_series(self, time_series):
    for i in range(time_series.getNumVariables()):
      self.result[time_series.getTitles()[i]] = time_series.getDataForIndex(i)

  ## Import numerical values from a CSV file and store them in self.result. The
  ## way data is stored in the file (row or column wise) is specified by the
  ## 'alignment' argument.
  # @param self The object pointer.
  # @param address Address of a CSV file (either .csv or .txt).
  # @param manager A ResourceManager instance.
  # @param separator A string indicating the type of separator to be expected
  # between the data values; possible values are ',', ' ', '\\t', ';', '|' and
  # ':' (default '\\t').
  # @param alignment String value indicating the way data is aligned in the
  # input file; possible values are 'row' and 'column' (default 'row').
  # @param header_size Integer value indicating the size of the file header in
  # number of lines (default 0).
  def import_from_csv_file(self, address, manager, separator='\t',
    alignment='row', header_size=0):
    if not separator in (',', ' ', '\t', ';', '|', ':'):
      sys.exit("Invalid separator: " + separator + "\n" +
               "Possible values are: ',', ' ', '\t', ';', '|' and ':'.")
    if address.endswith('csv') or address.endswith('txt'):
      file = manager.get_resource(address)
      # Skipping potential header
      for h in range(header_size):
        file.readline()
      if alignment == 'row':
        for line in file:
          ls = line.split(separator)
          name = str(ls.pop(0))
          f_ls = [float(i) for i in ls]
          self.result[name] = f_ls
      elif alignment == 'column':
        # Initializing items
        names = file.readline().split(separator)
        for n in names:
          self.result[n.rstrip('\n')] = []
        # Filling the values
        for line in file:
          l = line.rstrip('\n')
          values = l.split(separator)
          for v in range(len(values)):
            self.result[names[v]].append(float(values(v)))
      else:
        file.close()
        sys.exit("Invalid alignment: " + alignment + "\n" +
                 "Possible values are: 'row', 'column'.")
    else:
      sys.exit("Invalid file format.")

  ## Import numerical values from the output of a libSBMLSim simulation and
  ## store them in self.result.
  # @param self The object pointer.
  # @param result Result of a libSBMLSim simulation.
  def import_from_libsbmlsim(self, result):
    rows = result.getNumOfRows()
    # Time extraction
    time = []
    for r in range(rows):
      time.append(result.getTimeValueAtIndex(r))
    self.result["time"] = time
    # Species and miscellaneous values
    for s in range(result.getNumOfSpecies()):
      species = []
      name = result.getSpeciesNameAtIndex(s)
      for t in range(rows):
        species.append(result.getSpeciesValueAtIndex(name, t))
      self.result[name] = species

  ## Import numerical values from a NuML file and store them in self.result.
  ## This function expects the following layout for the considered
  ## resultComponent element:
  ##
  ## - resultComponent
  ##   + dimensionDescription
  ##   - dimension
  ##     - compositeValue indexValue=0
  ##       - compositeValue indexValue="time"
  ##         + atomicValue
  ##       - compositeValue indexValue="species_1"
  ##         + atomicValue
  ##       - compositeValue indexValue="species_2"
  ##         + atomicValue
  ##       [...]
  ##       - compositeValue indexValue="species_N"
  ##         + atomicValue
  ##     + compositeValue indexValue=1
  ##     [...]
  ##     + compositeValue indexValue=M
  ##
  ## This file should contain only one resultComponent
  # @param self The object pointer.
  # @param address Address of a NuML file.
  # @param manager A ResourceManager instance.
  # @param component Index of the resultComponent to be considered; default 0.
  def import_from_numl_file(self, address, manager, component=0):
    if address.endswith('xml'):
      file = manager.get_resource(address)
      reader = libnuml.NUMLReader()
      doc = reader.readNUMLFromString(file.read())
      if doc.getNumErrors() > 0:
        print("Error code " + str(doc.getError(0).getErrorId()) + " at line " +
              str(doc.getError(0).getLine()) + " when opening file: " +
              str(doc.getError(0).getShortMessage()))
        sys.exit(2)
      else:
        # Process the file normally
        dim = doc.getResultComponents().get(component).getDimension()
        # Acquiring keys and initializing values
        for k in dim.get(0):
          self.result[k.getIndexValue()] = []
        # Populating values
        for i in dim:
          for v in i:
            self.result[v.getIndexValue()].append(
              v.getAtomicValue().getDoubleValue())
    else:
      sys.exit("Invalid file format.")
<commit_msg>Fixed parsing issues in result.Result.import_from_csv method for column-aligned CSV files; improved robustness for 'time' series detection.<commit_after>#!/usr/bin/env python
# coding=utf-8

## @package biopredyn
## $Author$
## $Date$
## $Copyright: 2014, The CoSMo Company, All Rights Reserved $
## $License: BSD 3-Clause $
## $Revision$

import libsbmlsim
import libnuml
from matplotlib import pyplot as plt
import array

## Base class for simulation results
class Result:
  ## @var result
  # Pointer to the output of a simulation run. Results are always stored as
  # a dictionary where keys are names and values are arrays, for instance:
  #
  # keys ---> |  Time  | Species_1 | Species_2 | Error_1 | Error_2 |
  #           |--------|-----------|-----------|---------|---------|
  #           | 0.01   | 1.256e-6  | 5.21e-5   | 2.5e-8  | 1.23e-5 |
  #           | 0.16   | 1.004e-6  | 5.51e-5   | 6.7e-8  | 1.00e-5 |
  # values -> | 0.23   | 8.564e-7  | 5.13e-5   | 5.9e-7  | 8.96e-6 |
  #           | 0.47   | 5.735e-7  | 5.03e-5   | 2.4e-8  | 1.02e-5 |
  #           | 0.59   | 3.246e-7  | 5.45e-5   | 3.1e-8  | 1.17e-5 |

  ## Constructor.
  # @param self The object pointer.
  def __init__(self):
    self.result = dict()

  ## Returns the value of all fluxes in self.result as a vector of values.
  # @param self The object pointer.
  # @return The vector of fluxes, without growth_rate.
  def get_fluxes(self):
    vector = []
    for f in self.result:
      if str(f) != "growth_rate":
        vector.append(self.result[f])
    return vector

  ## Returns the number of time series in self.result; potential "time" series
  ## are not counted as time series.
  # @param self The object pointer.
  # @return The numbr of time series in self.result.
  def get_number_of_series(self):
    number = 0;
    for i in self.result:
      if str.lower(i) != "time":
        number = number + 1
    return number

  ## Returns a list containing all the quantity values for the input species
  ## over time.
  # @param self The object pointer.
  # @param species The species which quantity values are wanted.
  # @return A list of quantity values for the input species over time.
  def get_quantities_per_species(self, species):
    return self.result[species]

  ## Getter. Returns self.result.
  # @param self The object pointer.
  # @return self.result
  def get_result(self):
    return self.result

  ## Returns the list of all time steps in self.result.
  # @param self The object pointer.
  # @return The list of time steps.
  def get_time_steps(self):
    for t in self.result:
      if str.lower(t).__contains__("time"):
        return self.result[t]
    sys.exit("Error: no time series found.")

  ## Import numerical values from the output of a cobrapy flux balance analysis
  ## and store them in self.result. The resulting growth rate is stored as the
  ## value of the 'growth_rate' key.
  # @param self The object pointer.
  # @param solution Solution of a cobrapy FBA.
  def import_from_cobrapy_fba(self, solution):
    self.result["growth_rate"] = [solution.f]
    for p in solution.x_dict.iteritems():
      self.result[p[0]] = [p[1]]

  ## Import numerical values from a COPASI.CTimeSeries object.
  # @param self The object pointer.
  # @param time_series A COPASI.CTimeSeries object.
  def import_from_copasi_time_series(self, time_series):
    for i in range(time_series.getNumVariables()):
      self.result[time_series.getTitles()[i]] = time_series.getDataForIndex(i)

  ## Import numerical values from a CSV file and store them in self.result. The
  ## way data is stored in the file (row or column wise) is specified by the
  ## 'alignment' argument.
  # @param self The object pointer.
  # @param address Address of a CSV file (either .csv or .txt).
  # @param manager A ResourceManager instance.
  # @param separator A string indicating the type of separator to be expected
  # between the data values; possible values are ',', ' ', '\\t', ';', '|' and
  # ':' (default '\\t').
  # @param alignment String value indicating the way data is aligned in the
  # input file; possible values are 'row' and 'column' (default 'row').
  # @param header_size Integer value indicating the size of the file header in
  # number of lines (default 0).
  def import_from_csv_file(self, address, manager, separator='\t',
    alignment='row', header_size=0):
    if not separator in (',', ' ', '\t', ';', '|', ':'):
      sys.exit("Invalid separator: " + separator + "\n" +
               "Possible values are: ',', ' ', '\t', ';', '|' and ':'.")
    if address.endswith('csv') or address.endswith('txt'):
      file = manager.get_resource(address)
      # Skipping potential header
      for h in range(header_size):
        file.readline()
      if alignment == 'row':
        for line in file:
          ls = line.split(separator)
          name = str(ls.pop(0))
          f_ls = [float(i) for i in ls]
          self.result[name] = f_ls
      elif alignment == 'column':
        # Initializing items
        names = file.readline().rstrip('\n').split(separator)
        for n in names:
          self.result[n.rstrip('\n')] = []
        # Filling the values
        for line in file:
          l = line.rstrip('\n')
          values = l.split(separator)
          for v in range(len(values)):
            self.result[names[v]].append(float(values[v]))
      else:
        file.close()
        sys.exit("Invalid alignment: " + alignment + "\n" +
                 "Possible values are: 'row', 'column'.")
    else:
      sys.exit("Invalid file format.")

  ## Import numerical values from the output of a libSBMLSim simulation and
  ## store them in self.result.
  # @param self The object pointer.
  # @param result Result of a libSBMLSim simulation.
  def import_from_libsbmlsim(self, result):
    rows = result.getNumOfRows()
    # Time extraction
    time = []
    for r in range(rows):
      time.append(result.getTimeValueAtIndex(r))
    self.result["time"] = time
    # Species and miscellaneous values
    for s in range(result.getNumOfSpecies()):
      species = []
      name = result.getSpeciesNameAtIndex(s)
      for t in range(rows):
        species.append(result.getSpeciesValueAtIndex(name, t))
      self.result[name] = species

  ## Import numerical values from a NuML file and store them in self.result.
  ## This function expects the following layout for the considered
  ## resultComponent element:
  ##
  ## - resultComponent
  ##   + dimensionDescription
  ##   - dimension
  ##     - compositeValue indexValue=0
  ##       - compositeValue indexValue="time"
  ##         + atomicValue
  ##       - compositeValue indexValue="species_1"
  ##         + atomicValue
  ##       - compositeValue indexValue="species_2"
  ##         + atomicValue
  ##       [...]
  ##       - compositeValue indexValue="species_N"
  ##         + atomicValue
  ##     + compositeValue indexValue=1
  ##     [...]
  ##     + compositeValue indexValue=M
  ##
  ## This file should contain only one resultComponent
  # @param self The object pointer.
  # @param address Address of a NuML file.
  # @param manager A ResourceManager instance.
  # @param component Index of the resultComponent to be considered; default 0.
  def import_from_numl_file(self, address, manager, component=0):
    if address.endswith('xml'):
      file = manager.get_resource(address)
      reader = libnuml.NUMLReader()
      doc = reader.readNUMLFromString(file.read())
      if doc.getNumErrors() > 0:
        print("Error code " + str(doc.getError(0).getErrorId()) + " at line " +
              str(doc.getError(0).getLine()) + " when opening file: " +
              str(doc.getError(0).getShortMessage()))
        sys.exit(2)
      else:
        # Process the file normally
        dim = doc.getResultComponents().get(component).getDimension()
        # Acquiring keys and initializing values
        for k in dim.get(0):
          self.result[k.getIndexValue()] = []
        # Populating values
        for i in dim:
          for v in i:
            self.result[v.getIndexValue()].append(
              v.getAtomicValue().getDoubleValue())
    else:
      sys.exit("Invalid file format.")
<|endoftext|>


<commit_before>'''
ListAdapter
===========

.. versionadded:: 1.4

Notes:

    - Consider adding the functionality for locking changes to the associated
      data -- whether or not editing of item_view_instances is allowed.
      This could be controlled simply with an is_editable boolean, but would
      involve more work in the implementation, of course.
'''

from kivy.properties import ListProperty, DictProperty, ObjectProperty
from kivy.lang import Builder
from kivy.adapters.adapter import Adapter
from kivy.adapters.mixins.selection import SelectionSupport

class SimpleListAdapter(Adapter):
    '''ListAdapter is an adapter around a simple Python list.

    From Adapter, SimpleListAdapter gets these properties:

        - cls, for the list item class to use to instantiate row view
               instances

        - template, an optional kv template to use instead of a python class
                    (to use instead of cls)

        - args_converter, an optional function to transform data item argument
                          sets, in preparation for either a cls instantiation,
                          or a kv template invocation
    '''

    data = ListProperty([])
    '''The data list property contains a list of objects that will be used
    directly if no args_converter function is provided. If there is an
    args_converter, the data objects will be passed to it, for instantiation
    of item view class (cls) instances from the data.
    '''

    def __init__(self, **kwargs):
        if 'data' not in kwargs:
            raise Exception('list adapter: input must include data argument')
        if type(kwargs['data']) not in (tuple, list):
            raise Exception('list adapter: data must be a tuple or list')
        super(SimpleListAdapter, self).__init__(**kwargs)

    def get_count(self):
        return len(self.data)

    def get_item(self, index):
        if index < 0 or index >= len(self.data):
            return None
        return self.data[index]

class ListAdapter(SelectionSupport, SimpleListAdapter):
    '''From the SelectionSupport mixin, ListAdapter has these properties:

        - selection
        - selection_mode
        - allow_empty_selection

    and several methods used in selection operations.

    If you wish to have a bare-bones list adapter, without selection, use
    SimpleListAdapter.
    '''
    def __init__(self, **kwargs):
        super(ListAdapter, self).__init__(**kwargs)

        # Reset and update selection, in SelectionSupport, if data changes.
        self.bind(data=self.initialize_selection)

    def get_view(self, index):
        '''This method is identical to the one in Adapter and
        SimpleListAdapter, but here we pass self to the list item class (cls)
        instantiation, so that the list item class, required to mix in
        SelectableItem, will have access to ListAdapter for calling to
        SelectionSupport methods.
        '''
        item = self.get_item(index)
        if item is None:
            return None

        item_args = None
        if self.args_converter:
            item_args = self.args_converter(item)
        else:
            item_args = item

        item_args['list_adapter'] = self

        if self.cls:
            print 'CREATE VIEW FOR', index
            instance = self.cls(**item_args)
            return instance
        else:
            return Builder.template(self.template, **item_args)

    def on_selection_change(self, *args):
        '''on_selection_change() is the default handler for the
        on_selection_change event, which is registered in SelectionSupport.
        '''
        pass

class ListsAdapter(ListAdapter):
    '''ListsAdapter is specialized for managing a dict of lists. It has wide
    application, because a list of lists is a common need. ListsAdapter may
    be used for chaining several list_adapters in a "cascade," where selection
    of the first, changes the selection of the next, and so on.
    '''

    lists_dict = DictProperty({})
    '''The selection of the observed_list_adapter, which must be single
    selection here, is the key into lists_dict, which is a dict
    of list item lists.
    '''

    observed_list_adapter = ObjectProperty(None)

    lists_dict = DictProperty({})

    def __init__(self, **kwargs):
        super(ListsAdapter, self).__init__(**kwargs)
        self.observed_list_adapter.bind(
                on_selection_change=self.on_selection_change)

    def on_selection_change(self, *args):
        observed_selection = self.observed_list_adapter.selection

        if len(observed_selection) == 0:
            self.data = []
            return

        self.data = self.lists_dict[str(observed_selection[0])]

class AccumulatingListAdapter(ListAdapter):
    '''AccumulatingListAdapter is a list adapter whose
    data is formed by the selection of an observed list adapter.

    Selection will accumulate as either single or multiple selection
    events happen.
    '''

    observed_list_adapter = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(AccumulatingListAdapter, self).__init__(**kwargs)
        self.observed_list_adapter.bind(
                on_selection_change=self.on_selection_change)

    def on_selection_change(self, *args):
        observed_selection = self.observed_list_adapter.selection

        if len(observed_selection) == 0:
            self.data = []
            return

        self.data = [str(obj.text) for obj in observed_selection]
<commit_msg>Removed duplicate declation of lists_dict.<commit_after>'''
ListAdapter
===========

.. versionadded:: 1.4

Notes:

    - Consider adding the functionality for locking changes to the associated
      data -- whether or not editing of item_view_instances is allowed.
      This could be controlled simply with an is_editable boolean, but would
      involve more work in the implementation, of course.
'''

from kivy.properties import ListProperty, DictProperty, ObjectProperty
from kivy.lang import Builder
from kivy.adapters.adapter import Adapter
from kivy.adapters.mixins.selection import SelectionSupport

class SimpleListAdapter(Adapter):
    '''ListAdapter is an adapter around a simple Python list.

    From Adapter, SimpleListAdapter gets these properties:

        - cls, for the list item class to use to instantiate row view
               instances

        - template, an optional kv template to use instead of a python class
                    (to use instead of cls)

        - args_converter, an optional function to transform data item argument
                          sets, in preparation for either a cls instantiation,
                          or a kv template invocation
    '''

    data = ListProperty([])
    '''The data list property contains a list of objects that will be used
    directly if no args_converter function is provided. If there is an
    args_converter, the data objects will be passed to it, for instantiation
    of item view class (cls) instances from the data.
    '''

    def __init__(self, **kwargs):
        if 'data' not in kwargs:
            raise Exception('list adapter: input must include data argument')
        if type(kwargs['data']) not in (tuple, list):
            raise Exception('list adapter: data must be a tuple or list')
        super(SimpleListAdapter, self).__init__(**kwargs)

    def get_count(self):
        return len(self.data)

    def get_item(self, index):
        if index < 0 or index >= len(self.data):
            return None
        return self.data[index]

class ListAdapter(SelectionSupport, SimpleListAdapter):
    '''From the SelectionSupport mixin, ListAdapter has these properties:

        - selection
        - selection_mode
        - allow_empty_selection

    and several methods used in selection operations.

    If you wish to have a bare-bones list adapter, without selection, use
    SimpleListAdapter.
    '''

    def __init__(self, **kwargs):
        super(ListAdapter, self).__init__(**kwargs)

        # Reset and update selection, in SelectionSupport, if data changes.
        self.bind(data=self.initialize_selection)

    def get_view(self, index):
        '''This method is identical to the one in Adapter and
        SimpleListAdapter, but here we pass self to the list item class (cls)
        instantiation, so that the list item class, required to mix in
        SelectableItem, will have access to ListAdapter for calling to
        SelectionSupport methods.
        '''
        item = self.get_item(index)
        if item is None:
            return None

        item_args = None
        if self.args_converter:
            item_args = self.args_converter(item)
        else:
            item_args = item

        item_args['list_adapter'] = self

        if self.cls:
            print 'CREATE VIEW FOR', index
            instance = self.cls(**item_args)
            return instance
        else:
            return Builder.template(self.template, **item_args)

    def on_selection_change(self, *args):
        '''on_selection_change() is the default handler for the
        on_selection_change event, which is registered in SelectionSupport.
        '''
        pass

class ListsAdapter(ListAdapter):
    '''ListsAdapter is specialized for managing a dict of lists. It has wide
    application, because a list of lists is a common need. ListsAdapter may
    be used for chaining several list_adapters in a "cascade," where selection
    of the first, changes the selection of the next, and so on.
    '''

    lists_dict = DictProperty({})
    '''The selection of the observed_list_adapter, which must be single
    selection here, is the key into lists_dict, which is a dict
    of list item lists.
    '''

    observed_list_adapter = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(ListsAdapter, self).__init__(**kwargs)
        self.observed_list_adapter.bind(
                on_selection_change=self.on_selection_change)

    def on_selection_change(self, *args):
        observed_selection = self.observed_list_adapter.selection

        if len(observed_selection) == 0:
            self.data = []
            return

        self.data = self.lists_dict[str(observed_selection[0])]

class AccumulatingListAdapter(ListAdapter):
    '''AccumulatingListAdapter is a list adapter whose
    data is formed by the selection of an observed list adapter.

    Selection will accumulate as either single or multiple selection
    events happen.
    '''

    observed_list_adapter = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(AccumulatingListAdapter, self).__init__(**kwargs)
        self.observed_list_adapter.bind(
                on_selection_change=self.on_selection_change)

    def on_selection_change(self, *args):
        observed_selection = self.observed_list_adapter.selection

        if len(observed_selection) == 0:
            self.data = []
            return

        self.data = [str(obj.text) for obj in observed_selection]
<|endoftext|>


<commit_before>'''
Camera
======

Core class for acquiring the camera, and convert the input to a
:class:`~kivy.graphics.texture.Texture`.
'''

__all__ = ('CameraBase', 'Camera')

import sys

from kivy.event import EventDispatcher
from kivy.logger import Logger
from kivy.core import core_select_lib

class CameraBase(EventDispatcher):
    '''Abstract Camera Widget class.

    Concrete camera classes must implement initialization and
    frame capturing to buffer that can be uploaded to gpu.

    :Parameters:
        `index`: int
            Source index of the camera.
        `size` : tuple (int, int)
            Size at which the image is drawn. If no size is specified,
            it defaults to resolution of the camera image.
        `resolution` : tuple (int, int)
            Resolution to try to request from the camera.
            Used in the gstreamer pipeline by forcing the appsink caps
            to this resolution. If the camera doesnt support the resolution
            a negotiation error might be thrown.

    :Events:
        `on_load`
            Fired when the camera is loaded, and the texture became available
        `on_frame`
            Fired each time the camera texture is updated
    '''

    def __init__(self, **kwargs):
        kwargs.setdefault('stopped', False)
        kwargs.setdefault('resolution', (640, 480))
        kwargs.setdefault('index', 0)

        self.stopped = kwargs.get('stopped')
        self._resolution = kwargs.get('resolution')
        self._index = kwargs.get('index')
        self._buffer = None
        self._format = 'rgb'
        self._texture = None
        self.capture_device = None
        kwargs.setdefault('size', self._resolution)

        super(CameraBase, self).__init__()

        self.register_event_type('on_load')
        self.register_event_type('on_texture')

        self.init_camera()

        if not self.stopped:
            self.start()

    def _set_resolution(self, res):
        self._resolution = res
        self.init_camera()

    def _get_resolution(self):
        return self._resolution

    resolution = property(lambda self: self._get_resolution(),
                lambda self, x: self._set_resolution(x),
                doc='Resolution of camera capture (width, height)')

    def _set_index(self, x):
        if x == self._index:
            return
        self._index = x
        self.init_camera()

    def _get_index(self):
        return self._x

    index = property(lambda self: self._get_index(),
                lambda self, x: self._set_index(x),
                doc='Source index of the camera')

    def _get_texture(self):
        return self._texture
    texture = property(lambda self: self._get_texture(),
                doc='Return the camera texture with the latest capture')

    def init_camera(self):
        '''Initialise the camera (internal)'''
        pass

    def start(self):
        '''Start the camera acquire'''
        self.stopped = False

    def stop(self):
        '''Release the camera'''
        self.stopped = True

    def _update(self, dt):
        '''Update the camera (internal)'''
        pass

    def _copy_to_gpu(self):
        '''Copy the the buffer into the texture'''
        if self._texture is None:
            Logger.debug('Camera: copy_to_gpu() failed, _texture is None !')
            return
        self._texture.blit_buffer(self._buffer, colorfmt=self._format)
        self._buffer = None
        self.dispatch('on_texture')

    def on_texture(self):
        pass

    def on_load(self):
        pass

# Load the appropriate providers
providers = ()

if sys.platform != 'darwin':
    providers += (('gstreamer', 'camera_gstreamer', 'CameraGStreamer'), )
if sys.platform == 'win32':
    providers += (('videocapture', 'camera_videocapture',
        'CameraVideoCapture'), )

providers += (('opencv', 'camera_opencv', 'CameraOpenCV'), )

Camera = core_select_lib('camera', (providers))
<commit_msg>win32: fix order of preference for the camera provider, use videocapture first<commit_after>'''
Camera
======

Core class for acquiring the camera, and convert the input to a
:class:`~kivy.graphics.texture.Texture`.
'''

__all__ = ('CameraBase', 'Camera')

import sys

from kivy.event import EventDispatcher
from kivy.logger import Logger
from kivy.core import core_select_lib

class CameraBase(EventDispatcher):
    '''Abstract Camera Widget class.

    Concrete camera classes must implement initialization and
    frame capturing to buffer that can be uploaded to gpu.

    :Parameters:
        `index`: int
            Source index of the camera.
        `size` : tuple (int, int)
            Size at which the image is drawn. If no size is specified,
            it defaults to resolution of the camera image.
        `resolution` : tuple (int, int)
            Resolution to try to request from the camera.
            Used in the gstreamer pipeline by forcing the appsink caps
            to this resolution. If the camera doesnt support the resolution
            a negotiation error might be thrown.

    :Events:
        `on_load`
            Fired when the camera is loaded, and the texture became available
        `on_frame`
            Fired each time the camera texture is updated
    '''

    def __init__(self, **kwargs):
        kwargs.setdefault('stopped', False)
        kwargs.setdefault('resolution', (640, 480))
        kwargs.setdefault('index', 0)

        self.stopped = kwargs.get('stopped')
        self._resolution = kwargs.get('resolution')
        self._index = kwargs.get('index')
        self._buffer = None
        self._format = 'rgb'
        self._texture = None
        self.capture_device = None
        kwargs.setdefault('size', self._resolution)

        super(CameraBase, self).__init__()

        self.register_event_type('on_load')
        self.register_event_type('on_texture')

        self.init_camera()

        if not self.stopped:
            self.start()

    def _set_resolution(self, res):
        self._resolution = res
        self.init_camera()

    def _get_resolution(self):
        return self._resolution

    resolution = property(lambda self: self._get_resolution(),
                lambda self, x: self._set_resolution(x),
                doc='Resolution of camera capture (width, height)')

    def _set_index(self, x):
        if x == self._index:
            return
        self._index = x
        self.init_camera()

    def _get_index(self):
        return self._x

    index = property(lambda self: self._get_index(),
                lambda self, x: self._set_index(x),
                doc='Source index of the camera')

    def _get_texture(self):
        return self._texture
    texture = property(lambda self: self._get_texture(),
                doc='Return the camera texture with the latest capture')

    def init_camera(self):
        '''Initialise the camera (internal)'''
        pass

    def start(self):
        '''Start the camera acquire'''
        self.stopped = False

    def stop(self):
        '''Release the camera'''
        self.stopped = True

    def _update(self, dt):
        '''Update the camera (internal)'''
        pass

    def _copy_to_gpu(self):
        '''Copy the the buffer into the texture'''
        if self._texture is None:
            Logger.debug('Camera: copy_to_gpu() failed, _texture is None !')
            return
        self._texture.blit_buffer(self._buffer, colorfmt=self._format)
        self._buffer = None
        self.dispatch('on_texture')

    def on_texture(self):
        pass

    def on_load(self):
        pass

# Load the appropriate providers
providers = ()

if sys.platform == 'win32':
    providers += (('videocapture', 'camera_videocapture',
        'CameraVideoCapture'), )
if sys.platform != 'darwin':
    providers += (('gstreamer', 'camera_gstreamer', 'CameraGStreamer'), )

providers += (('opencv', 'camera_opencv', 'CameraOpenCV'), )

Camera = core_select_lib('camera', (providers))
<|endoftext|>


<commit_before>import sys
sys.path.insert(0, '/home/projekt/homeStruction/website')
import django
import os
from django.utils import timezone
from nanpy import (ArduinoApi, SerialManager)
from time import sleep
from subprocess import call

# Initial setup required
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "homeStruction.settings")
django.setup()

from project.models import Temperature
sensor = 19
powervoltage = 5

connected = False
nrOfTries = 0
logFile = open('errors.log', 'a')
log.write('Trying to connect:' + timezone.now())

while not connected and nrOfTries < 5:
    nrOfTries += 1
    try:
        connection = SerialManager()
        a = ArduinoApi(connection=connection)
        connected = True
    except:
        call['/home/project/homeStruction/website/nanpyupdate']
        log.write('Failed! Trying to reconnect' + str(nrOfTries))
        sleep(1)

if connected:
    tempavg = 0
    for i in range(0, 10):
        sensorValue = a.analogRead(sensor)
        temperature = (sensorValue / 1023.0) * powervoltage * 100
        # print("%d: %2.2f" % (i, temperature))
        tempavg += temperature

    # sleep(.2)
    tempavg /= 10.0
    print("%f" % tempavg)
    print timezone.now()
    Temperature.objects.create(value=tempavg, time_recorded=timezone.now())
else:
    log.write('could NOT connect! aborting...')
log.close()
<commit_msg>lel not logFile but log<commit_after>import sys
sys.path.insert(0, '/home/projekt/homeStruction/website')
import django
import os
from django.utils import timezone
from nanpy import (ArduinoApi, SerialManager)
from time import sleep
from subprocess import call

# Initial setup required
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "homeStruction.settings")
django.setup()

from project.models import Temperature
sensor = 19
powervoltage = 5

connected = False
nrOfTries = 0
log = open('errors.log', 'a')
log.write('Trying to connect:' + timezone.now())

while not connected and nrOfTries < 5:
    nrOfTries += 1
    try:
        connection = SerialManager()
        a = ArduinoApi(connection=connection)
        connected = True
    except:
        call['/home/project/homeStruction/website/nanpyupdate']
        log.write('Failed! Trying to reconnect' + str(nrOfTries))
        sleep(1)

if connected:
    tempavg = 0
    for i in range(0, 10):
        sensorValue = a.analogRead(sensor)
        temperature = (sensorValue / 1023.0) * powervoltage * 100
        # print("%d: %2.2f" % (i, temperature))
        tempavg += temperature

    # sleep(.2)
    tempavg /= 10.0
    print("%f" % tempavg)
    print timezone.now()
    Temperature.objects.create(value=tempavg, time_recorded=timezone.now())
else:
    log.write('could NOT connect! aborting...')
log.close()
<|endoftext|>


<commit_before># A simple Psi 4 input script to compute a SCF reference using second-order optimizations
# Requires scipy numpy 1.7.2+
#
# Created by: Daniel G. A. Smith
# Date: 2/27/15
# License: GPL v3.0
#

import time
import numpy as np
np.set_printoptions(precision=3, linewidth=200, suppress=True)
from helper_HF import *
import psi4

# Memory for Psi4 in GB
psi4.core.set_memory(int(2e9), False)
psi4.core.set_output_file('output.dat', False)

# Memory for numpy in GB
numpy_memory = 2

mol = psi4.geometry("""
O
H 1 1.1
H 1 1.1 2 109
symmetry c1
""")

psi4.set_options({'scf_type':'df',
                  'basis':'aug-cc-pvdz',
                  'e_convergence':1e1,
                  'd_convergence':1e1})

# Knobs
E_conv = 1.e-8
D_conv = 1.e-4
max_macro = 10
max_micro = 3
micro_conv = 1.e-3
micro_print = True

# Build objects
diis = DIIS_helper()
hf = helper_HF(mol, scf_type='DF', guess='CORE')
ndocc = hf.ndocc
nvirt = hf.nvirt

print('\nStart SCF iterations:\n')
t = time.time()
E = 0.0
Eold = 0.0
iter_type = 'CORE'

def SCF_Hx(x, moF, Co, Cv):
    """
    Compute a hessian vector guess where x is a ov matrix of nonredundant operators.
    """
    F  = np.dot(moF[:ndocc, :ndocc], x)
    F -= np.dot(x, moF[ndocc:, ndocc:])

    # Build two electron part, M = -4 (4 G_{mnip} - g_{mpin} - g_{npim}) K_{ip}
    C_right = np.einsum('ia,sa->si', -x, Cv)
    J, K = hf.build_jk(Co, C_right)
    F  += (Co.T).dot(4 * J - K.T - K).dot(Cv)
    F *= -4
    return F

for SCF_ITER in range(1, max_macro):

    # Build new fock matrix
    F = hf.build_fock()

    # DIIS error and update
    diis_e = F.dot(hf.Da).dot(hf.S) - hf.S.dot(hf.Da).dot(F)
    diis_e = (hf.A).dot(diis_e).dot(hf.A)
    diis.add(F, diis_e)

    # SCF energy and update
    scf_e = hf.compute_hf_energy()
    dRMS = np.mean(diis_e ** 2) ** 0.5
    print 'SCF Iteration %3d: Energy = %4.16f   dE = % 1.5E   dRMS = %1.5E   %s' % (SCF_ITER, hf.scf_e, (hf.scf_e - Eold), dRMS, iter_type)
    if (abs(hf.scf_e - Eold) < E_conv) and (dRMS < D_conv):
        break

    Eold = hf.scf_e
    Dold = hf.Da

    # Build MO fock ,matrix and gradient
    Co = hf.Ca[:, :ndocc]
    Cv = hf.Ca[:, ndocc:]
    moF = np.einsum('ui,vj,uv->ij', hf.Ca, hf.Ca, F)
    gradient = -4 * moF[:ndocc, ndocc:]
    grad_dot = np.vdot(gradient, gradient)

    if (np.max(np.abs(gradient)) > 0.2):
        F = diis.extrapolate()
        eps, C = hf.diag(F)
        hf.set_Cleft(C)
        iter_type = 'DIIS'
    else:

        # DIIS helper and initial guess
        sodiis = DIIS_helper()
        eps = np.diag(moF)
        precon = -4 * (eps[:ndocc].reshape(-1, 1) - eps[ndocc:])

        x = gradient / precon
        Ax = SCF_Hx(x, moF, Co, Cv)
        r = gradient - Ax
        z = r / precon
        p = z.copy()
        rms = (np.vdot(r, r) / grad_dot) ** 0.5
        if micro_print:
            print('Micro Iteration Guess: Rel. RMS = %1.5e' %  (rms))

        # CG iterations
        for rot_iter in range(max_micro):
            rz_old = np.vdot(r, z)

            Ap = SCF_Hx(p, moF, Co, Cv)
            alpha = rz_old / np.vdot(Ap, p)

            x += alpha * p
            r -= alpha * Ap
            z = r / precon
            sodiis.add(x, r)

            rms = (np.vdot(r, r) / grad_dot) ** 0.5

            if micro_print:
                print('Micro Iteration %5d: Rel. RMS = %1.5e' %  (rot_iter + 1, rms))
            if rms < micro_conv:
                break

            beta = np.vdot(r, z) / rz_old
            p = z + beta * p

        x = sodiis.extrapolate()
        C = rotate_orbitals(hf.Ca, x)

        hf.set_Cleft(C)
        iter_type = 'SOSCF, nmicro ' + str(rot_iter + 1)

print('Total time taken for SCF iterations: %.3f seconds \n' % (time.time()-t))

print('Final SCF energy:     %.8f hartree' % hf.scf_e)

# Compute w/ Psi4 and compare
psi4.set_options({'e_convergence':1e-7,
                  'd_convergence':1e-7})

SCF_E_psi = psi4.energy('SCF')
psi4.driver.p4util.compare_values(SCF_E_psi, hf.scf_e, 6, 'SCF Energy')

<commit_msg>Updated to SORHF_iterative.py to standard Python dict syntax.<commit_after># A simple Psi 4 input script to compute a SCF reference using second-order optimizations
# Requires scipy numpy 1.7.2+
#
# Created by: Daniel G. A. Smith
# Date: 2/27/15
# License: GPL v3.0
#

import time
import numpy as np
np.set_printoptions(precision=3, linewidth=200, suppress=True)
from helper_HF import *
import psi4

# Memory for Psi4 in GB
psi4.core.set_memory(int(2e9), False)
psi4.core.set_output_file('output.dat', False)

# Memory for numpy in GB
numpy_memory = 2

mol = psi4.geometry("""
O
H 1 1.1
H 1 1.1 2 109
symmetry c1
""")

psi4.set_options({'scf_type': 'df',
                  'basis': 'aug-cc-pvdz',
                  'e_convergence': 1e1,
                  'd_convergence': 1e1})

# Knobs
E_conv = 1.e-8
D_conv = 1.e-4
max_macro = 10
max_micro = 3
micro_conv = 1.e-3
micro_print = True

# Build objects
diis = DIIS_helper()
hf = helper_HF(mol, scf_type='DF', guess='CORE')
ndocc = hf.ndocc
nvirt = hf.nvirt

print('\nStart SCF iterations:\n')
t = time.time()
E = 0.0
Eold = 0.0
iter_type = 'CORE'

def SCF_Hx(x, moF, Co, Cv):
    """
    Compute a hessian vector guess where x is a ov matrix of nonredundant operators.
    """
    F  = np.dot(moF[:ndocc, :ndocc], x)
    F -= np.dot(x, moF[ndocc:, ndocc:])

    # Build two electron part, M = -4 (4 G_{mnip} - g_{mpin} - g_{npim}) K_{ip}
    C_right = np.einsum('ia,sa->si', -x, Cv)
    J, K = hf.build_jk(Co, C_right)
    F  += (Co.T).dot(4 * J - K.T - K).dot(Cv)
    F *= -4
    return F

for SCF_ITER in range(1, max_macro):

    # Build new fock matrix
    F = hf.build_fock()

    # DIIS error and update
    diis_e = F.dot(hf.Da).dot(hf.S) - hf.S.dot(hf.Da).dot(F)
    diis_e = (hf.A).dot(diis_e).dot(hf.A)
    diis.add(F, diis_e)

    # SCF energy and update
    scf_e = hf.compute_hf_energy()
    dRMS = np.mean(diis_e ** 2) ** 0.5
    print 'SCF Iteration %3d: Energy = %4.16f   dE = % 1.5E   dRMS = %1.5E   %s' % (SCF_ITER, hf.scf_e, (hf.scf_e - Eold), dRMS, iter_type)
    if (abs(hf.scf_e - Eold) < E_conv) and (dRMS < D_conv):
        break

    Eold = hf.scf_e
    Dold = hf.Da

    # Build MO fock ,matrix and gradient
    Co = hf.Ca[:, :ndocc]
    Cv = hf.Ca[:, ndocc:]
    moF = np.einsum('ui,vj,uv->ij', hf.Ca, hf.Ca, F)
    gradient = -4 * moF[:ndocc, ndocc:]
    grad_dot = np.vdot(gradient, gradient)

    if (np.max(np.abs(gradient)) > 0.2):
        F = diis.extrapolate()
        eps, C = hf.diag(F)
        hf.set_Cleft(C)
        iter_type = 'DIIS'
    else:

        # DIIS helper and initial guess
        sodiis = DIIS_helper()
        eps = np.diag(moF)
        precon = -4 * (eps[:ndocc].reshape(-1, 1) - eps[ndocc:])

        x = gradient / precon
        Ax = SCF_Hx(x, moF, Co, Cv)
        r = gradient - Ax
        z = r / precon
        p = z.copy()
        rms = (np.vdot(r, r) / grad_dot) ** 0.5
        if micro_print:
            print('Micro Iteration Guess: Rel. RMS = %1.5e' %  (rms))

        # CG iterations
        for rot_iter in range(max_micro):
            rz_old = np.vdot(r, z)

            Ap = SCF_Hx(p, moF, Co, Cv)
            alpha = rz_old / np.vdot(Ap, p)

            x += alpha * p
            r -= alpha * Ap
            z = r / precon
            sodiis.add(x, r)

            rms = (np.vdot(r, r) / grad_dot) ** 0.5

            if micro_print:
                print('Micro Iteration %5d: Rel. RMS = %1.5e' %  (rot_iter + 1, rms))
            if rms < micro_conv:
                break

            beta = np.vdot(r, z) / rz_old
            p = z + beta * p

        x = sodiis.extrapolate()
        C = rotate_orbitals(hf.Ca, x)

        hf.set_Cleft(C)
        iter_type = 'SOSCF, nmicro ' + str(rot_iter + 1)

print('Total time taken for SCF iterations: %.3f seconds \n' % (time.time()-t))

print('Final SCF energy:     %.8f hartree' % hf.scf_e)

# Compute w/ Psi4 and compare
psi4.set_options({'e_convergence': 1e-7,
                  'd_convergence': 1e-7})

SCF_E_psi = psi4.energy('SCF')
psi4.driver.p4util.compare_values(SCF_E_psi, hf.scf_e, 6, 'SCF Energy')

<|endoftext|>


<commit_before>#!/usr/bin/env python
#
# __COPYRIGHT__
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

"""
Verify that we can import and use the contents of Platform and Tool
modules directly.
"""

import os
import re
import sys

import TestSCons

test = TestSCons.TestSCons()

# Before executing the any of the platform or tool modules, add some
# null entries to the environment $PATH variable to make sure there's
# no code that tries to index elements from the list before making sure
# they're non-null.
# (This was a problem in checkpoint release 0.97.d020070809.)
os.environ['PATH'] = os.pathsep + os.environ['PATH'] + \
                     os.pathsep + os.pathsep + '/no/such/dir' + os.pathsep

SConstruct_path = test.workpath('SConstruct')

platforms = [
    'aix',
    'cygwin',
    'darwin',
    'hpux',
    'irix',
    'os2',
    'posix',
    'sunos',
    'win32'
]

for platform in platforms:
    test.write('SConstruct', """
print "Platform %(platform)s"
env = Environment(platform = '%(platform)s')
import SCons.Platform.%(platform)s
x = SCons.Platform.%(platform)s.generate
""" % locals())
    test.run()

tools = [
    # Can't import '386asm' everywhere due to Windows registry dependency.
    'aixc++',
    'aixcc',
    'aixf77',
    'aixlink',
    'applelink',
    'ar',
    'as',
    'bcc32',
    'BitKeeper',
    'c++',
    'cc',
    'cvf',
    'CVS',
    'default',
    'dmd',
    'dvi',
    'dvipdf',
    'dvips',
    'f77',
    'f90',
    'f95',
    'fortran',
    'g++',
    'g77',
    'gas',
    'gcc',
    'gfortran',
    'gnulink',
    'gs',
    'hpc++',
    'hpcc',
    'hplink',
    'icc',
    'icl',
    'ifort',
    'ifl',
    'ilink',
    'ilink32',
    'intelc',
    'jar',
    'javac',
    'javah',
    'latex',
    'lex',
    'link',
    # Can't import 'linkloc' everywhere due to Windows registry dependency.
    'm4',
    'masm',
    'midl',
    'mingw',
    'mslib',
    'mslink',
    'msvc',
    'msvs',
    'mwcc',
    'mwld',
    'nasm',
    'pdf',
    'pdflatex',
    'pdftex',
    'Perforce',
    'qt',
    'RCS',
    'rmic',
    'rpcgen',
    'SCCS',
    'sgiar',
    'sgic++',
    'sgicc',
    'sgilink',
    'Subversion',
    'sunar',
    'sunc++',
    'suncc',
    'sunlink',
    'swig',
    'tar',
    'tex',
    'tlib',
    'yacc',
    'zip',
]

#if sys.platform == 'win32':
# Just comment out (for now?) due to registry dependency.
#    tools.extend([
#        '386asm',
#        'linkloc',
#    ])

# Intel no compiler warning..
intel_no_compiler_warning = """
scons: warning: Failed to find Intel compiler for version='None', abi='[^']*'
""" + TestSCons.file_expr

# Intel no top dir warning.
intel_no_top_dir_warning = """
scons: warning: Can't find Intel compiler top dir for version='None', abi='[^']*'
""" + TestSCons.file_expr

# Intel no license directory warning
intel_license_warning = re.escape("""
scons: warning: Intel license dir was not found.  Tried using the INTEL_LICENSE_FILE environment variable (), the registry () and the default path (C:\Program Files\Common Files\Intel\Licenses).  Using the default path as a last resort.
""") + TestSCons.file_expr

intel_warnings = [
    re.compile(intel_license_warning),
    re.compile(intel_no_compiler_warning),
    re.compile(intel_no_compiler_warning + intel_license_warning),
    re.compile(intel_no_top_dir_warning),
    re.compile(intel_no_top_dir_warning + intel_license_warning),
]

moc = test.where_is('moc')
if moc:
    import os.path

    qtdir = os.path.dirname(os.path.dirname(moc))

    qt_err = r"""
scons: warning: Could not detect qt, using moc executable as a hint \(QTDIR=%(qtdir)s\)
""" % locals()

else:

    qt_err = """
scons: warning: Could not detect qt, using empty QTDIR
"""

qt_warnings = [ re.compile(qt_err + TestSCons.file_expr) ]

error_output = {
    'icl' : intel_warnings,
    'intelc' : intel_warnings,
    'qt' : qt_warnings,
}

# An SConstruct for importing Tool names that have illegal characters
# for Python variable names.
indirect_import = """\
print "Tool %(tool)s (indirect)"
env = Environment(tools = ['%(tool)s'])

SCons = __import__('SCons.Tool.%(tool)s', globals(), locals(), [])
m = getattr(SCons.Tool, '%(tool)s')
env = Environment()
m.generate(env)
"""

# An SConstruct for importing Tool names "normally."
direct_import = """\
print "Tool %(tool)s (direct)"
env = Environment(tools = ['%(tool)s'])

import SCons.Tool.%(tool)s
env = Environment()
SCons.Tool.%(tool)s.exists(env)
SCons.Tool.%(tool)s.generate(env)
"""

failures = []
for tool in tools:
    if tool[0] in '0123456789' or '+' in tool or tool in ('as'):
        test.write('SConstruct', indirect_import % locals())
    else:
        test.write('SConstruct', direct_import % locals())
    test.run(stderr=None)
    stderr = test.stderr()
    if stderr:
        matched = None
        for expression in error_output.get(tool, []):
            if expression.match(stderr):
                matched = 1
                break
        if not matched:
            print "Failed importing '%s', stderr:" % tool
            print stderr
            failures.append(tool)

test.fail_test(len(failures))

test.pass_test()
<commit_msg>Fix intended tuple syntax (for Python 2.2 and before).<commit_after>#!/usr/bin/env python
#
# __COPYRIGHT__
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

"""
Verify that we can import and use the contents of Platform and Tool
modules directly.
"""

import os
import re
import sys

import TestSCons

test = TestSCons.TestSCons()

# Before executing the any of the platform or tool modules, add some
# null entries to the environment $PATH variable to make sure there's
# no code that tries to index elements from the list before making sure
# they're non-null.
# (This was a problem in checkpoint release 0.97.d020070809.)
os.environ['PATH'] = os.pathsep + os.environ['PATH'] + \
                     os.pathsep + os.pathsep + '/no/such/dir' + os.pathsep

SConstruct_path = test.workpath('SConstruct')

platforms = [
    'aix',
    'cygwin',
    'darwin',
    'hpux',
    'irix',
    'os2',
    'posix',
    'sunos',
    'win32'
]

for platform in platforms:
    test.write('SConstruct', """
print "Platform %(platform)s"
env = Environment(platform = '%(platform)s')
import SCons.Platform.%(platform)s
x = SCons.Platform.%(platform)s.generate
""" % locals())
    test.run()

tools = [
    # Can't import '386asm' everywhere due to Windows registry dependency.
    'aixc++',
    'aixcc',
    'aixf77',
    'aixlink',
    'applelink',
    'ar',
    'as',
    'bcc32',
    'BitKeeper',
    'c++',
    'cc',
    'cvf',
    'CVS',
    'default',
    'dmd',
    'dvi',
    'dvipdf',
    'dvips',
    'f77',
    'f90',
    'f95',
    'fortran',
    'g++',
    'g77',
    'gas',
    'gcc',
    'gfortran',
    'gnulink',
    'gs',
    'hpc++',
    'hpcc',
    'hplink',
    'icc',
    'icl',
    'ifort',
    'ifl',
    'ilink',
    'ilink32',
    'intelc',
    'jar',
    'javac',
    'javah',
    'latex',
    'lex',
    'link',
    # Can't import 'linkloc' everywhere due to Windows registry dependency.
    'm4',
    'masm',
    'midl',
    'mingw',
    'mslib',
    'mslink',
    'msvc',
    'msvs',
    'mwcc',
    'mwld',
    'nasm',
    'pdf',
    'pdflatex',
    'pdftex',
    'Perforce',
    'qt',
    'RCS',
    'rmic',
    'rpcgen',
    'SCCS',
    'sgiar',
    'sgic++',
    'sgicc',
    'sgilink',
    'Subversion',
    'sunar',
    'sunc++',
    'suncc',
    'sunlink',
    'swig',
    'tar',
    'tex',
    'tlib',
    'yacc',
    'zip',
]

#if sys.platform == 'win32':
# Just comment out (for now?) due to registry dependency.
#    tools.extend([
#        '386asm',
#        'linkloc',
#    ])

# Intel no compiler warning..
intel_no_compiler_warning = """
scons: warning: Failed to find Intel compiler for version='None', abi='[^']*'
""" + TestSCons.file_expr

# Intel no top dir warning.
intel_no_top_dir_warning = """
scons: warning: Can't find Intel compiler top dir for version='None', abi='[^']*'
""" + TestSCons.file_expr

# Intel no license directory warning
intel_license_warning = re.escape("""
scons: warning: Intel license dir was not found.  Tried using the INTEL_LICENSE_FILE environment variable (), the registry () and the default path (C:\Program Files\Common Files\Intel\Licenses).  Using the default path as a last resort.
""") + TestSCons.file_expr

intel_warnings = [
    re.compile(intel_license_warning),
    re.compile(intel_no_compiler_warning),
    re.compile(intel_no_compiler_warning + intel_license_warning),
    re.compile(intel_no_top_dir_warning),
    re.compile(intel_no_top_dir_warning + intel_license_warning),
]

moc = test.where_is('moc')
if moc:
    import os.path

    qtdir = os.path.dirname(os.path.dirname(moc))

    qt_err = r"""
scons: warning: Could not detect qt, using moc executable as a hint \(QTDIR=%(qtdir)s\)
""" % locals()

else:

    qt_err = """
scons: warning: Could not detect qt, using empty QTDIR
"""

qt_warnings = [ re.compile(qt_err + TestSCons.file_expr) ]

error_output = {
    'icl' : intel_warnings,
    'intelc' : intel_warnings,
    'qt' : qt_warnings,
}

# An SConstruct for importing Tool names that have illegal characters
# for Python variable names.
indirect_import = """\
print "Tool %(tool)s (indirect)"
env = Environment(tools = ['%(tool)s'])

SCons = __import__('SCons.Tool.%(tool)s', globals(), locals(), [])
m = getattr(SCons.Tool, '%(tool)s')
env = Environment()
m.generate(env)
"""

# An SConstruct for importing Tool names "normally."
direct_import = """\
print "Tool %(tool)s (direct)"
env = Environment(tools = ['%(tool)s'])

import SCons.Tool.%(tool)s
env = Environment()
SCons.Tool.%(tool)s.exists(env)
SCons.Tool.%(tool)s.generate(env)
"""

failures = []
for tool in tools:
    if tool[0] in '0123456789' or '+' in tool or tool in ('as',):
        test.write('SConstruct', indirect_import % locals())
    else:
        test.write('SConstruct', direct_import % locals())
    test.run(stderr=None)
    stderr = test.stderr()
    if stderr:
        matched = None
        for expression in error_output.get(tool, []):
            if expression.match(stderr):
                matched = 1
                break
        if not matched:
            print "Failed importing '%s', stderr:" % tool
            print stderr
            failures.append(tool)

test.fail_test(len(failures))

test.pass_test()
<|endoftext|>


<commit_before>'''

@author: frank
'''
import sys, os, os.path
from zstacklib.utils import log
from zstacklib.utils import linux
import zstacklib.utils.iptables as iptables

pidfile = '/var/run/zstack/kvmagent.pid'
log.configure_log('/var/log/zstack/zstack-kvmagent.log')
logger = log.get_logger(__name__)

import kvmagent

def prepare_pid_dir(path):
    pdir = os.path.dirname(path)
    if not os.path.isdir(pdir):
        os.makedirs(pdir)

def main():
    usage = 'usage: python -c "from kvmagent import kdaemon; kdaemon.main()" start|stop|restart'
    if len(sys.argv) != 2 or not sys.argv[1] in ['start', 'stop', 'restart']:
        print usage
        sys.exit(1)

    global pidfile
    prepare_pid_dir(pidfile)

    try:
        iptc = iptables.from_iptables_save()
        iptc.add_rule('-A INPUT -p tcp -m tcp --dport 7070 -j ACCEPT')
        # open vnc ports
        iptc.add_rule('-A INPUT -p tcp -m tcp --dport 5900:6200 -j ACCEPT')
        iptc.iptable_restore()

        cmd = sys.argv[1]
        agentdaemon = kvmagent.KvmDaemon(pidfile)
        if cmd == 'start':
            logger.debug('zstack-kvmagent starts')
            agentdaemon.start()
        elif cmd == 'stop':
            logger.debug('zstack-kvmagent stops')
            agentdaemon.stop()
        elif cmd == 'restart':
            logger.debug('zstack-kvmagent restarts')
            agentdaemon.restart()
        sys.exit(0)
    except Exception:
        logger.warning(linux.get_exception_stacktrace())
        sys.exit(1)

if __name__ == '__main__':
    main()
<commit_msg>fix https://github.com/zxwing/premium/issues/119<commit_after>'''

@author: frank
'''
import sys, os, os.path
from zstacklib.utils import log
from zstacklib.utils import linux
import zstacklib.utils.iptables as iptables

pidfile = '/var/run/zstack/kvmagent.pid'
log.configure_log('/var/log/zstack/zstack-kvmagent.log')
logger = log.get_logger(__name__)

import kvmagent

def prepare_pid_dir(path):
    pdir = os.path.dirname(path)
    if not os.path.isdir(pdir):
        os.makedirs(pdir)

def main():
    usage = 'usage: python -c "from kvmagent import kdaemon; kdaemon.main()" start|stop|restart'
    if len(sys.argv) != 2 or not sys.argv[1] in ['start', 'stop', 'restart']:
        print usage
        sys.exit(1)

    global pidfile
    prepare_pid_dir(pidfile)

    try:
        iptc = iptables.from_iptables_save()
        iptc.add_rule('-A INPUT -p tcp -m tcp --dport 7070 -j ACCEPT')
        # open vnc ports
        iptc.add_rule('-A INPUT -p tcp -m tcp --dport 5900:6200 -j ACCEPT')
        iptc.add_rule('-A INPUT -p tcp -m tcp --dport 16509 -j ACCEPT')
        iptc.iptable_restore()

        cmd = sys.argv[1]
        agentdaemon = kvmagent.KvmDaemon(pidfile)
        if cmd == 'start':
            logger.debug('zstack-kvmagent starts')
            agentdaemon.start()
        elif cmd == 'stop':
            logger.debug('zstack-kvmagent stops')
            agentdaemon.stop()
        elif cmd == 'restart':
            logger.debug('zstack-kvmagent restarts')
            agentdaemon.restart()
        sys.exit(0)
    except Exception:
        logger.warning(linux.get_exception_stacktrace())
        sys.exit(1)

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


<commit_before>'''
 ====================================================================
 Copyright (c) 2016 Barry A Scott.  All rights reserved.

 This software is licensed as described in the file LICENSE.txt,
 which you should have received as part of this distribution.

 ====================================================================

    wb_git_askpass_client_unix.py

    called with argv[1:] as the prompt
    expects a single line output as response.

'''
import sys
import os
import pathlib
import pwd
import stat
import tempfile
import select

class WbGitAskPass:
    fifo_name_client = '.ScmWorkbench.client'
    fifo_name_server = '.ScmWorkbench.server'

    def __init__( self ):
        pass

    def askPass( self, prompt ):
        e = pwd.getpwuid( os.geteuid() )
        fifo_dir = pathlib.Path( tempfile.gettempdir() ) / e.pw_name

        client_fifo = fifo_dir / self.fifo_name_client
        server_fifo = fifo_dir / self.fifo_name_server

        try:
            error_msg = 'Error: failed to open fifo - %s'
            fd_server = os.open( str(server_fifo), os.O_WRONLY|os.O_NONBLOCK )

            error_msg = 'Error: failed to open fifo - %s'
            fd_client = os.open( str(client_fifo), os.O_RDONLY|os.O_NONBLOCK )

            prompt = prompt.encode( 'utf-8' )

            error_msg = 'Error: failed to write cmd to server - %s'
            size = os.write( fd_server, prompt )
            if size != len(prompt):
                print( 'Error: failed to write cmd to server' )
                return 1

            error_msg = 'Error: failed to close server fifo - %s'
            os.close( fd_server )

            error_msg = 'Error: failed to read client fifo - %s'

            poll = select.poll()
            poll.register( fd_client, select.POLLIN )
            poll.poll()

            reply = os.read( fd_client, 1024 )

            os.close( fd_client )

            reply = reply.decode( 'utf-8' )
            print( reply[1:] )
            return int(reply[0])

        except IOError as e:
            print( error_msg % (e,) )
            return 1

if __name__ == '__main__':
    sys.exit( WbGitAskPass().askPass( sys.argv[1] ) )
<commit_msg>allow to work on python2.7 and python3<commit_after>'''
 ====================================================================
 Copyright (c) 2016 Barry A Scott.  All rights reserved.

 This software is licensed as described in the file LICENSE.txt,
 which you should have received as part of this distribution.

 ====================================================================

    wb_git_askpass_client_unix.py

    called with argv[1:] as the prompt
    expects a single line output as response.

    On macOS will be run by python2.7
    On Linux we can use python3

'''
import sys
import os
import os.path
import pwd
import stat
import tempfile
import select

class WbGitAskPass:
    fifo_name_client = '.ScmWorkbench.client'
    fifo_name_server = '.ScmWorkbench.server'

    def __init__( self ):
        pass

    def askPass( self, prompt ):
        e = pwd.getpwuid( os.geteuid() )
        fifo_dir = os.path.join( tempfile.gettempdir(), e.pw_name )

        client_fifo = os.path.join( fifo_dir, self.fifo_name_client )
        server_fifo = os.path.join( fifo_dir, self.fifo_name_server )

        try:
            error_msg = 'Error: failed to open fifo - %s'
            fd_server = os.open( server_fifo, os.O_WRONLY|os.O_NONBLOCK )

            error_msg = 'Error: failed to open fifo - %s'
            fd_client = os.open( client_fifo, os.O_RDONLY|os.O_NONBLOCK )

            prompt = prompt.encode( 'utf-8' )

            error_msg = 'Error: failed to write cmd to server - %s'
            size = os.write( fd_server, prompt )
            if size != len(prompt):
                print( 'Error: failed to write cmd to server' )
                return 1

            error_msg = 'Error: failed to close server fifo - %s'
            os.close( fd_server )

            error_msg = 'Error: failed to read client fifo - %s'

            # wait for write to fd
            select.select( [fd_client], [], [], None )

            reply = os.read( fd_client, 1024 )

            os.close( fd_client )

            reply = reply.decode( 'utf-8' )
            print( reply[1:] )
            return int(reply[0])

        except IOError as e:
            print( error_msg % (e,) )
            return 1

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


<commit_before>import logging
import libvirt
from xml.dom.minidom import parseString
from celery.result import AsyncResult
from django.core.exceptions import ObjectDoesNotExist
from vnc.models import Vnc
from .tasks import start_websock

class LibvirtBackend(object):
    def __init__(self):
        self.logger = logging.getLogger(__name__)

    def __getattribute__(self, item):
        # self.conn as singleton (makes initialisation without libvirt rights possible)
        # this is usefull for development without libvirt rights)
        if item == 'conn' and not hasattr(self, 'conn'):
            self.conn = libvirt.open('qemu:///system')
        return super(LibvirtBackend, self).__getattribute__(item)

    def _get_domain(self, name):
        '''
        Return a domain object associated with the host
        '''
        try:
            domain = self.conn.lookupByName(name)
            return domain
        except libvirt.libvirtError as e:
            print 'libvirt failed for host ' + name + ' with:'
            print str(e.get_error_code()) + ': ' + e.get_error_message()

    def terminate_vnc(self, host):
        '''
        Terminates the VNC Websocket process attached to a Host object
        '''
        try:
            host.vnc.delete()
            host.save()
        except ObjectDoesNotExist as e:
            self.logger.info('tried deleting a nonexistant vnc database entry')

    def set_name(self, host, old_name):
        #TODO make critical error less critical
        '''
        renames a libvirt domain by redefining it
        :returns: 1 if no change was required, 0 if successful, -1 if there was an error
        '''
        if old_name == host.name:
            self.logger.warning('unnesccessary set_name for %s' % host.name)
            return 1
        old_domain = self._get_domain(old_name)
        if old_domain.isActive():
            self.logger.error('cannot change the name of a running domain')
            host.name = old_name
            host.save()
            return -1
        else:
            try:
                #xml = xmltodict.parse(domain.XMLDesc(0))
                old_domain.undefine()
                xml.replace('<name>' + old_name + '</name>', '<name>' + host.name + '</name>')
                libvirt.defineXML(xml)
            except libvirt.libvirtError as e:
                self.logger.critical('set_name %s (old: %s) failed' % (host.name, old_name))
                self.logger.critical("check the domain definition's integrity on the hypervisor")
                self.logger.critical(e.get_error_message())
                return -1
        return 0 # all is fine

    def set_state(self, host):
        '''
        Adapt a domains status to the is_on value in the database
        :returns: 1 if no change was required, 0 if successful, -1 if there was an error
        '''
        domain = self._get_domain(host.name)
        if host.is_on and not domain.isActive():
            return self.start(host)
        elif not host.is_on and domain.isActive():
            return self.shutdown(host)
        else:
            self.logger.warning('unnesccessary set_state for %s' % host.name)
            return 1

    def set_vcpus(self, host):
        pass

    def set_memory(self, host):
        '''
        sets the amount of memory in kibibyte available for the specified host
        :returns: 1 if there is no change, 0 if done, -1 if there was an error
        '''
        domain = self._get_domain(host.name)
        if domain.maxMemory() == host.memory:
            self.logger.warning('unnesccessary set_memory for %s' % host.name)
            return 1
        else:
            try:
                domain.setMaxMemory(host.memory)
            except libvirt.libvirtError as e:
                self.logger.error('setting memory failed for %s with:' % host.name)
                self.logger.error(e.get_error_message())
                return -1
        self.logger.info('set_memory run for %s' % host.name)
        return 0 # all is fine

    def set_autostart(self, host):
        '''
        sets the autostart for the specified host
        :returns: 1 if there is no change, 0 if done, -1 if there was an error
        '''
        domain = self._get_domain(host.name)
        if domain.autostart() == host.autostart:
            self.logger.warning('unnesccessary set_autostart for %s' % host.name)
            return 1
        else:
            try:
                domain.setAutostart(host.autostart)
            except libvirt.libvirtError as e:
                self.logger.error('setting autostart failed for %s with:' % host.name)
                self.logger.error(e.get_error_message())
                return -1
        self.logger.info('set_autostart run for %s' % host.name)
        return 0 # all is fine

    def set_persistent(self, host):
        pass

    def start(self, host):
        '''
        Boots a domain
        :returns: 1 if the domain is already running, 0 if successful, -1 if there was an error
        '''
        domain = self._get_domain(host.name)
        if domain.isActive():
            self.logger.warning('unnesccessary start for %s' % host.name)
            return 1
        else:
            try:
                domain.create()
            except libvirt.libvirtError as e:
                self.logger.error('start failed for %s with:' % host.name)
                self.logger.error(e.get_error_message())
                return -1
        self.logger.info('start run for %s' % host.name)
        return 0 # all is fine

    def reboot(self, host):
        '''
        Reboots a domain
        :returns: 1 if the domain is not running, 0 if the reboot was successful, -1 if there was an error
        '''
        domain = self._get_domain(host.name)
        if not domain.isActive():
            self.logger.warning('unnesccessary reboot for %s' % host.name)
            return 1
        else:
            try:
                domain.reboot(0)
            except libvirt.libvirtError as e:
                self.logger.error('reboot failed for %s with:' % host.name)
                self.logger.error(e.get_error_message())
                return -1
            self.terminate_vnc(host)
        self.logger.info('reboot run for %s' % host.name)
        return 0 # all is fine

    def shutdown(self, host):
        '''
        shuts a domain down
        :returns: 1 if the domain is already stopped, 0 if successful, -1 if there was an error
        '''
        domain = self._get_domain(host.name)
        if not domain.isActive():
            self.logger.warning('unnesccessary shutdown for %s' % host.name)
            return 1
        else:
            try:
                domain.shutdown()
            except libvirt.libvirtError as e:
                self.logger.error('shutdown failed for %s with:' % host.name)
                self.logger.error(e.get_error_message())
                return -1
        self.logger.info('shutdown run for %s' % host.name)
        return 0 # all is fine

    def destroy(self, host):
        '''
        Destroys a domain matched by its hostname
        :returns: 1 if it was not running, 0 if the domain has been destroyed, -1 if there was an error
        '''
        domain = self._get_domain(host.name)
        if not domain.isActive():
            self.logger.warning('unnesccessary destroy for %s' % host.name)
            return 1
        else:
            try:
                domain.destroy()
            except libvirt.libvirtError as e:
                self.logger.error('destroy failed for %s with:' % host.name)
                self.logger.error(e.get_error_message())
                return -1
            self.terminate_vnc(host)
        self.logger.info('destroy run for %s' % host.name)
        return 0 # all is fine

    def attach_or_create_websock(self, user, host):
        '''
        Creates a websocket for the specified host
        :returns: 1 if the host is not running, 0 and the port if the websocket is running, -1 if
        there is no VNC support for this domain
        '''
        domain = self._get_domain(host.name)
        if domain is None:
            self.logger.warning('vnc requested for the host %s, which does not exist in libvirt (yet)' % host.name)
            return 2
        elif not domain.isActive():
            self.logger.warning('vnc requested for the shutdown host %s' % host.name)
            return 1
        else:
            if hasattr(host, 'vnc'):
                if not AsyncResult(host.vnc.pid).ready():
                    self.logger.info('session of %s is blocked for %s by another user' % (host.name, user))
                    return -2
                else:
                    host.vnc.delete()
            self.logger.info('initializing vnc for %s' % host.name)
            doc = parseString(domain.XMLDesc(0))
            domain_node = doc.getElementsByTagName('domain')[0]
            graphics_node = domain_node.getElementsByTagName('graphics')[0]
            if graphics_node is None or graphics_node.getAttribute('type') != u'vnc':
                # vm does not support vnc
                return -1
            target_port = graphics_node.getAttribute('port')
            port = int(graphics_node.getAttribute('port')) + 10000
            t = start_websock.delay(str(target_port), str(port))
            vnc = Vnc(host=host, port=port, pid=t.id)
            vnc.save()
            self.logger.info('websocket started for %s on port %d' % (host.name, host.vnc.port))
            host.vnc = vnc
            host.save()
            return 0 # all is fine
<commit_msg>maybe fix open file leak?<commit_after>import logging
import libvirt
from xml.dom.minidom import parseString
from celery.result import AsyncResult
from django.core.exceptions import ObjectDoesNotExist
from vnc.models import Vnc
from .tasks import start_websock

class LibvirtBackend(object):
    def __init__(self):
        self.logger = logging.getLogger(__name__)
        try:
            self.conn = libvirt.open('qemu:///system')
        except OSError, e:
            logging.error('Error when initializing qemu connection: %s' % e)

    def _get_domain(self, name):
        '''
        Return a domain object associated with the host
        '''
        try:
            domain = self.conn.lookupByName(name)
            return domain
        except libvirt.libvirtError as e:
            print 'libvirt failed for host ' + name + ' with:'
            print str(e.get_error_code()) + ': ' + e.get_error_message()

    def terminate_vnc(self, host):
        '''
        Terminates the VNC Websocket process attached to a Host object
        '''
        try:
            host.vnc.delete()
            host.save()
        except ObjectDoesNotExist as e:
            self.logger.info('tried deleting a nonexistant vnc database entry')

    def set_name(self, host, old_name):
        #TODO make critical error less critical
        '''
        renames a libvirt domain by redefining it
        :returns: 1 if no change was required, 0 if successful, -1 if there was an error
        '''
        if old_name == host.name:
            self.logger.warning('unnesccessary set_name for %s' % host.name)
            return 1
        old_domain = self._get_domain(old_name)
        if old_domain.isActive():
            self.logger.error('cannot change the name of a running domain')
            host.name = old_name
            host.save()
            return -1
        else:
            try:
                #xml = xmltodict.parse(domain.XMLDesc(0))
                old_domain.undefine()
                xml.replace('<name>' + old_name + '</name>', '<name>' + host.name + '</name>')
                libvirt.defineXML(xml)
            except libvirt.libvirtError as e:
                self.logger.critical('set_name %s (old: %s) failed' % (host.name, old_name))
                self.logger.critical("check the domain definition's integrity on the hypervisor")
                self.logger.critical(e.get_error_message())
                return -1
        return 0 # all is fine

    def set_state(self, host):
        '''
        Adapt a domains status to the is_on value in the database
        :returns: 1 if no change was required, 0 if successful, -1 if there was an error
        '''
        domain = self._get_domain(host.name)
        if host.is_on and not domain.isActive():
            return self.start(host)
        elif not host.is_on and domain.isActive():
            return self.shutdown(host)
        else:
            self.logger.warning('unnesccessary set_state for %s' % host.name)
            return 1

    def set_vcpus(self, host):
        pass

    def set_memory(self, host):
        '''
        sets the amount of memory in kibibyte available for the specified host
        :returns: 1 if there is no change, 0 if done, -1 if there was an error
        '''
        domain = self._get_domain(host.name)
        if domain.maxMemory() == host.memory:
            self.logger.warning('unnesccessary set_memory for %s' % host.name)
            return 1
        else:
            try:
                domain.setMaxMemory(host.memory)
            except libvirt.libvirtError as e:
                self.logger.error('setting memory failed for %s with:' % host.name)
                self.logger.error(e.get_error_message())
                return -1
        self.logger.info('set_memory run for %s' % host.name)
        return 0 # all is fine

    def set_autostart(self, host):
        '''
        sets the autostart for the specified host
        :returns: 1 if there is no change, 0 if done, -1 if there was an error
        '''
        domain = self._get_domain(host.name)
        if domain.autostart() == host.autostart:
            self.logger.warning('unnesccessary set_autostart for %s' % host.name)
            return 1
        else:
            try:
                domain.setAutostart(host.autostart)
            except libvirt.libvirtError as e:
                self.logger.error('setting autostart failed for %s with:' % host.name)
                self.logger.error(e.get_error_message())
                return -1
        self.logger.info('set_autostart run for %s' % host.name)
        return 0 # all is fine

    def set_persistent(self, host):
        pass

    def start(self, host):
        '''
        Boots a domain
        :returns: 1 if the domain is already running, 0 if successful, -1 if there was an error
        '''
        domain = self._get_domain(host.name)
        if domain.isActive():
            self.logger.warning('unnesccessary start for %s' % host.name)
            return 1
        else:
            try:
                domain.create()
            except libvirt.libvirtError as e:
                self.logger.error('start failed for %s with:' % host.name)
                self.logger.error(e.get_error_message())
                return -1
        self.logger.info('start run for %s' % host.name)
        return 0 # all is fine

    def reboot(self, host):
        '''
        Reboots a domain
        :returns: 1 if the domain is not running, 0 if the reboot was successful, -1 if there was an error
        '''
        domain = self._get_domain(host.name)
        if not domain.isActive():
            self.logger.warning('unnesccessary reboot for %s' % host.name)
            return 1
        else:
            try:
                domain.reboot(0)
            except libvirt.libvirtError as e:
                self.logger.error('reboot failed for %s with:' % host.name)
                self.logger.error(e.get_error_message())
                return -1
            self.terminate_vnc(host)
        self.logger.info('reboot run for %s' % host.name)
        return 0 # all is fine

    def shutdown(self, host):
        '''
        shuts a domain down
        :returns: 1 if the domain is already stopped, 0 if successful, -1 if there was an error
        '''
        domain = self._get_domain(host.name)
        if not domain.isActive():
            self.logger.warning('unnesccessary shutdown for %s' % host.name)
            return 1
        else:
            try:
                domain.shutdown()
            except libvirt.libvirtError as e:
                self.logger.error('shutdown failed for %s with:' % host.name)
                self.logger.error(e.get_error_message())
                return -1
        self.logger.info('shutdown run for %s' % host.name)
        return 0 # all is fine

    def destroy(self, host):
        '''
        Destroys a domain matched by its hostname
        :returns: 1 if it was not running, 0 if the domain has been destroyed, -1 if there was an error
        '''
        domain = self._get_domain(host.name)
        if not domain.isActive():
            self.logger.warning('unnesccessary destroy for %s' % host.name)
            return 1
        else:
            try:
                domain.destroy()
            except libvirt.libvirtError as e:
                self.logger.error('destroy failed for %s with:' % host.name)
                self.logger.error(e.get_error_message())
                return -1
            self.terminate_vnc(host)
        self.logger.info('destroy run for %s' % host.name)
        return 0 # all is fine

    def attach_or_create_websock(self, user, host):
        '''
        Creates a websocket for the specified host
        :returns: 1 if the host is not running, 0 and the port if the websocket is running, -1 if
        there is no VNC support for this domain
        '''
        domain = self._get_domain(host.name)
        if domain is None:
            self.logger.warning('vnc requested for the host %s, which does not exist in libvirt (yet)' % host.name)
            return 2
        elif not domain.isActive():
            self.logger.warning('vnc requested for the shutdown host %s' % host.name)
            return 1
        else:
            if hasattr(host, 'vnc'):
                if not AsyncResult(host.vnc.pid).ready():
                    self.logger.info('session of %s is blocked for %s by another user' % (host.name, user))
                    return -2
                else:
                    host.vnc.delete()
            self.logger.info('initializing vnc for %s' % host.name)
            doc = parseString(domain.XMLDesc(0))
            domain_node = doc.getElementsByTagName('domain')[0]
            graphics_node = domain_node.getElementsByTagName('graphics')[0]
            if graphics_node is None or graphics_node.getAttribute('type') != u'vnc':
                # vm does not support vnc
                return -1
            target_port = graphics_node.getAttribute('port')
            port = int(graphics_node.getAttribute('port')) + 10000
            t = start_websock.delay(str(target_port), str(port))
            vnc = Vnc(host=host, port=port, pid=t.id)
            vnc.save()
            self.logger.info('websocket started for %s on port %d' % (host.name, host.vnc.port))
            host.vnc = vnc
            host.save()
            return 0 # all is fine
<|endoftext|>


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

import os
import sys
import argparse
import traceback

sys.path.insert(0, "/home/ben/programming/BPModule/install/modules")
sys.path.insert(0, "/home/ben/programming/ambit/install/lib")

import bpmodule as bp

def Run():
    try:
      # Load the python modules
      #             supermodule   module name   key
      bp.LoadModule("testtensor", "testtensor", "TESTTENSOR")

      bp.CommitOptions()
      bp.DumpModuleInfo()
      bp.SanityCheck()

      b1 = bp.mst.GetModule_Test("TESTTENSOR")

      inp = bp.datastore.CalcData()
      inp.Set("TEST_INPUT_1", 10.00)
      b1_r = b1.CalcTest(inp)
      bp.output.Output("B1 results: %1%\n", b1_r.GetCopy("TEST_RESULT_1"))

      bp.output.Output("Has OUTMAT: %1%\n", b1_r.Has("OUTMAT"))
      mat = b1_r.GetCopy("OUTMAT")
      print("MAT: {}".format(type(mat)))
      mat.printf()

      bp.output.Output("\nDone testing\n")

    except Exception as e:
      bp.output.Output("Caught exception in main handler\n")
      #traceback.print_exc()
      bp.output.Error("\n")
      bp.output.Error(str(e))
      bp.output.Error("\n")

bp.Init(sys.argv, out = "stdout", color = True, debug = True)
Run()
bp.Finalize()
<commit_msg>Remove unused line from test<commit_after>#!/usr/bin/env python3

import os
import sys
import argparse
import traceback

sys.path.insert(0, "/home/ben/programming/BPModule/install/modules")
sys.path.insert(0, "/home/ben/programming/ambit/install/lib")

import bpmodule as bp

def Run():
    try:
      # Load the python modules
      #             supermodule   module name   key
      bp.LoadModule("testtensor", "testtensor", "TESTTENSOR")

      bp.DumpModuleInfo()
      bp.SanityCheck()

      b1 = bp.mst.GetModule_Test("TESTTENSOR")

      inp = bp.datastore.CalcData()
      inp.Set("TEST_INPUT_1", 10.00)
      b1_r = b1.CalcTest(inp)
      bp.output.Output("B1 results: %1%\n", b1_r.GetCopy("TEST_RESULT_1"))

      bp.output.Output("Has OUTMAT: %1%\n", b1_r.Has("OUTMAT"))
      mat = b1_r.GetCopy("OUTMAT")
      print("MAT: {}".format(type(mat)))
      mat.printf()

      bp.output.Output("\nDone testing\n")

    except Exception as e:
      bp.output.Output("Caught exception in main handler\n")
      #traceback.print_exc()
      bp.output.Error("\n")
      bp.output.Error(str(e))
      bp.output.Error("\n")

bp.Init(sys.argv, out = "stdout", color = True, debug = True)
Run()
bp.Finalize()
<|endoftext|>