<commit_before># -*- coding: utf-8 -*-
'''
Beacon to announce via avahi (zeroconf)

.. versionadded:: 2016.11.0

Dependencies
============

- python-avahi
- dbus-python

'''
# Import Python libs
from __future__ import absolute_import
import logging
import time

# Import 3rd Party libs
try:
    import avahi
    HAS_PYAVAHI = True
except ImportError:
    HAS_PYAVAHI = False

try:
    import dbus
    HAS_DBUS = True
except ImportError:
    HAS_DBUS = False

log = logging.getLogger(__name__)

__virtualname__ = 'avahi_announce'

LAST_GRAINS = {}
BUS = dbus.SystemBus()
SERVER = dbus.Interface(BUS.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER),
                        avahi.DBUS_INTERFACE_SERVER)
GROUP = dbus.Interface(BUS.get_object(avahi.DBUS_NAME, SERVER.EntryGroupNew()),
                       avahi.DBUS_INTERFACE_ENTRY_GROUP)

def __virtual__():
    if HAS_PYAVAHI:
        if HAS_DBUS:
            return __virtualname__
        return False, 'The {0} beacon cannot be loaded. The ' \
                      '\'python-dbus\' dependency is missing.'.format(__virtualname__)
    return False, 'The {0} beacon cannot be loaded. The ' \
                  '\'python-avahi\' dependency is missing.'.format(__virtualname__)

def __validate__(config):
    '''
    Validate the beacon configuration
    '''
    if not isinstance(config, dict):
        return False, ('Configuration for avahi_announcement '
                       'beacon must be a dictionary')
    elif not all(x in list(config.keys()) for x in ('servicetype', 'port', 'txt')):
        return False, ('Configuration for avahi_announce beacon '
                       'must contain servicetype, port and txt items')
    return True, 'Valid beacon configuration'

def beacon(config):
    '''
    Broadcast values via zeroconf

    If the announced values are static, it is adviced to set run_once: True
    (do not poll) on the beacon configuration.

    The following are required configuration settings:
        'servicetype': The service type to announce.
        'port': The port of the service to announce.
        'txt': The TXT record of the service being announced as a dict.
               Grains can be used to define TXT values using the syntax:
                   grains.<grain_name>
               or:
                   grains.<grain_name>[i]
               where i is an integer representing the index of the grain to
               use. If the grain is not a list, the index is ignored.

    The following are optional configuration settings:
        'servicename': Set the name of the service. Will use the hostname from
                       __grains__['host'] if not set.
        'reset_on_change': If true and there is a change in TXT records
                           detected, it will stop announcing the service and
                           then restart announcing the service. This
                           interruption in service announcement may be
                           desirable if the client relies on changes in the
                           browse records to update its cache of the TXT
                           records.
                           Defaults to False.
        'reset_wait': The number of seconds to wait after announcement stops
                      announcing and before it restarts announcing in the
                      case where there is a change in TXT records detected
                      and 'reset_on_change' is True.
                      Defaults to 0.
        'copy_grains': If set to True, it will copy the grains passed into
                       the beacon when it backs them up to check for changes
                       on the next iteration. Normally, instead of copy, it
                       would use straight value assignment. This will allow
                       detection of changes to grains where the grains are
                       modified in-place instead of completely replaced.
                       In-place grains changes are not currently done in the
                       main Salt code but may be done due to a custom
                       plug-in.
                       Defaults to False.

    Example Config

    .. code-block:: yaml

       beacons:
         avahi_announce:
           run_once: True
           servicetype: _demo._tcp
           port: 1234
           txt:
             ProdName: grains.productname
             SerialNo: grains.serialnumber
             Comments: 'this is a test'
    '''
    ret = []
    changes = {}
    txt = {}

    global LAST_GRAINS

    _validate = __validate__(config)
    if not _validate[0]:
        log.warning('Beacon {0} configuration invalid, '
                    'not adding. {1}'.format(__virtualname__, _validate[1]))
        return ret

    if 'servicename' in config:
        servicename = config['servicename']
    else:
        servicename = __grains__['host']

    for item in config['txt']:
        if config['txt'][item].startswith('grains.'):
            grain = config['txt'][item][7:]
            grain_index = None
            square_bracket = grain.find('[')
            if square_bracket != -1 and grain[-1] == ']':
                grain_index = int(grain[square_bracket+1:-1])
                grain = grain[:square_bracket]

            grain_value = __grains__.get(grain, '')
            if isinstance(grain_value, list):
                if grain_index is not None:
                    grain_value = grain_value[grain_index]
                else:
                    grain_value = ','.join(grain_value)
            txt[item] = grain_value
            if LAST_GRAINS and (LAST_GRAINS.get(grain, '') != __grains__.get(grain, '')):
                changes[str('txt.' + item)] = txt[item]
        else:
            txt[item] = config['txt'][item]

        if not LAST_GRAINS:
            changes[str('txt.' + item)] = txt[item]

    if changes:
        if not LAST_GRAINS:
            changes['servicename'] = servicename
            changes['servicetype'] = config['servicetype']
            changes['port'] = config['port']
            GROUP.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0),
                             servicename, config['servicetype'], '', '',
                             dbus.UInt16(config['port']), avahi.dict_to_txt_array(txt))
            GROUP.Commit()
        elif config.get('reset_on_change', False):
            GROUP.Reset()
            reset_wait = config.get('reset_wait', 0)
            if reset_wait > 0:
                time.sleep(reset_wait)
            GROUP.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0),
                             servicename, config['servicetype'], '', '',
                             dbus.UInt16(config['port']), avahi.dict_to_txt_array(txt))
            GROUP.Commit()
        else:
            GROUP.UpdateServiceTxt(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0),
                                servicename, config['servicetype'], '',
                                avahi.dict_to_txt_array(txt))

        ret.append({'tag': 'result', 'changes': changes})

    if config.get('copy_grains', False):
        LAST_GRAINS = __grains__.copy()
    else:
        LAST_GRAINS = __grains__

    return ret
<commit_msg>Properly gate dbus definitions<commit_after># -*- coding: utf-8 -*-
'''
Beacon to announce via avahi (zeroconf)

.. versionadded:: 2016.11.0

Dependencies
============

- python-avahi
- dbus-python

'''
# Import Python libs
from __future__ import absolute_import
import logging
import time

# Import 3rd Party libs
try:
    import avahi
    HAS_PYAVAHI = True
except ImportError:
    HAS_PYAVAHI = False

try:
    import dbus
    from dbus import DBusException
    BUS = dbus.SystemBus()
    SERVER = dbus.Interface(BUS.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER),
                            avahi.DBUS_INTERFACE_SERVER)
    GROUP = dbus.Interface(BUS.get_object(avahi.DBUS_NAME, SERVER.EntryGroupNew()),
                           avahi.DBUS_INTERFACE_ENTRY_GROUP)
    HAS_DBUS = True
except ImportError:
    HAS_DBUS = False
except DBusException:
    HAS_DBUS = False

log = logging.getLogger(__name__)

__virtualname__ = 'avahi_announce'

LAST_GRAINS = {}

def __virtual__():
    if HAS_PYAVAHI:
        if HAS_DBUS:
            return __virtualname__
        return False, 'The {0} beacon cannot be loaded. The ' \
                      '\'python-dbus\' dependency is missing.'.format(__virtualname__)
    return False, 'The {0} beacon cannot be loaded. The ' \
                  '\'python-avahi\' dependency is missing.'.format(__virtualname__)

def __validate__(config):
    '''
    Validate the beacon configuration
    '''
    if not isinstance(config, dict):
        return False, ('Configuration for avahi_announcement '
                       'beacon must be a dictionary')
    elif not all(x in list(config.keys()) for x in ('servicetype', 'port', 'txt')):
        return False, ('Configuration for avahi_announce beacon '
                       'must contain servicetype, port and txt items')
    return True, 'Valid beacon configuration'

def beacon(config):
    '''
    Broadcast values via zeroconf

    If the announced values are static, it is adviced to set run_once: True
    (do not poll) on the beacon configuration.

    The following are required configuration settings:
        'servicetype': The service type to announce.
        'port': The port of the service to announce.
        'txt': The TXT record of the service being announced as a dict.
               Grains can be used to define TXT values using the syntax:
                   grains.<grain_name>
               or:
                   grains.<grain_name>[i]
               where i is an integer representing the index of the grain to
               use. If the grain is not a list, the index is ignored.

    The following are optional configuration settings:
        'servicename': Set the name of the service. Will use the hostname from
                       __grains__['host'] if not set.
        'reset_on_change': If true and there is a change in TXT records
                           detected, it will stop announcing the service and
                           then restart announcing the service. This
                           interruption in service announcement may be
                           desirable if the client relies on changes in the
                           browse records to update its cache of the TXT
                           records.
                           Defaults to False.
        'reset_wait': The number of seconds to wait after announcement stops
                      announcing and before it restarts announcing in the
                      case where there is a change in TXT records detected
                      and 'reset_on_change' is True.
                      Defaults to 0.
        'copy_grains': If set to True, it will copy the grains passed into
                       the beacon when it backs them up to check for changes
                       on the next iteration. Normally, instead of copy, it
                       would use straight value assignment. This will allow
                       detection of changes to grains where the grains are
                       modified in-place instead of completely replaced.
                       In-place grains changes are not currently done in the
                       main Salt code but may be done due to a custom
                       plug-in.
                       Defaults to False.

    Example Config

    .. code-block:: yaml

       beacons:
         avahi_announce:
           run_once: True
           servicetype: _demo._tcp
           port: 1234
           txt:
             ProdName: grains.productname
             SerialNo: grains.serialnumber
             Comments: 'this is a test'
    '''
    ret = []
    changes = {}
    txt = {}

    global LAST_GRAINS

    _validate = __validate__(config)
    if not _validate[0]:
        log.warning('Beacon {0} configuration invalid, '
                    'not adding. {1}'.format(__virtualname__, _validate[1]))
        return ret

    if 'servicename' in config:
        servicename = config['servicename']
    else:
        servicename = __grains__['host']

    for item in config['txt']:
        if config['txt'][item].startswith('grains.'):
            grain = config['txt'][item][7:]
            grain_index = None
            square_bracket = grain.find('[')
            if square_bracket != -1 and grain[-1] == ']':
                grain_index = int(grain[square_bracket+1:-1])
                grain = grain[:square_bracket]

            grain_value = __grains__.get(grain, '')
            if isinstance(grain_value, list):
                if grain_index is not None:
                    grain_value = grain_value[grain_index]
                else:
                    grain_value = ','.join(grain_value)
            txt[item] = grain_value
            if LAST_GRAINS and (LAST_GRAINS.get(grain, '') != __grains__.get(grain, '')):
                changes[str('txt.' + item)] = txt[item]
        else:
            txt[item] = config['txt'][item]

        if not LAST_GRAINS:
            changes[str('txt.' + item)] = txt[item]

    if changes:
        if not LAST_GRAINS:
            changes['servicename'] = servicename
            changes['servicetype'] = config['servicetype']
            changes['port'] = config['port']
            GROUP.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0),
                             servicename, config['servicetype'], '', '',
                             dbus.UInt16(config['port']), avahi.dict_to_txt_array(txt))
            GROUP.Commit()
        elif config.get('reset_on_change', False):
            GROUP.Reset()
            reset_wait = config.get('reset_wait', 0)
            if reset_wait > 0:
                time.sleep(reset_wait)
            GROUP.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0),
                             servicename, config['servicetype'], '', '',
                             dbus.UInt16(config['port']), avahi.dict_to_txt_array(txt))
            GROUP.Commit()
        else:
            GROUP.UpdateServiceTxt(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0),
                                servicename, config['servicetype'], '',
                                avahi.dict_to_txt_array(txt))

        ret.append({'tag': 'result', 'changes': changes})

    if config.get('copy_grains', False):
        LAST_GRAINS = __grains__.copy()
    else:
        LAST_GRAINS = __grains__

    return ret
<|endoftext|>


<commit_before># Copyright 2013 IBM Corp.
#
#    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 os
import shutil
import subprocess
import tempfile

from tempest.tests import base

DEVNULL = open(os.devnull, 'wb')

class TestWrappers(base.TestCase):
    def setUp(self):
        super(TestWrappers, self).setUp()
        # Setup test dirs
        self.directory = tempfile.mkdtemp(prefix='tempest-unit')
        self.test_dir = os.path.join(self.directory, 'tests')
        os.mkdir(self.test_dir)
        # Setup Test files
        self.testr_conf_file = os.path.join(self.directory, '.testr.conf')
        self.setup_cfg_file = os.path.join(self.directory, 'setup.cfg')
        self.passing_file = os.path.join(self.test_dir, 'test_passing.py')
        self.failing_file = os.path.join(self.test_dir, 'test_failing.py')
        self.init_file = os.path.join(self.test_dir, '__init__.py')
        self.setup_py = os.path.join(self.directory, 'setup.py')
        shutil.copy('tempest/tests/files/testr-conf', self.testr_conf_file)
        shutil.copy('tempest/tests/files/passing-tests', self.passing_file)
        shutil.copy('tempest/tests/files/failing-tests', self.failing_file)
        shutil.copy('setup.py', self.setup_py)
        shutil.copy('tempest/tests/files/setup.cfg', self.setup_cfg_file)
        shutil.copy('tempest/tests/files/__init__.py', self.init_file)

    def test_pretty_tox(self):
        # Copy wrapper script and requirements:
        pretty_tox = os.path.join(self.directory, 'pretty_tox.sh')
        shutil.copy('tools/pretty_tox.sh', pretty_tox)
        # Change directory, run wrapper and check result
        self.addCleanup(os.chdir, os.path.abspath(os.curdir))
        os.chdir(self.directory)
        # Git init is required for the pbr testr command. pbr requires a git
        # version or an sdist to work. so make the test directory a git repo
        # too.
        subprocess.call(['git', 'init'])
        exit_code = subprocess.call('bash pretty_tox.sh tests.passing',
                                    shell=True, stdout=DEVNULL, stderr=DEVNULL)
        self.assertEqual(exit_code, 0)

    def test_pretty_tox_fails(self):
        # Copy wrapper script and requirements:
        pretty_tox = os.path.join(self.directory, 'pretty_tox.sh')
        shutil.copy('tools/pretty_tox.sh', pretty_tox)
        # Change directory, run wrapper and check result
        self.addCleanup(os.chdir, os.path.abspath(os.curdir))
        os.chdir(self.directory)
        # Git init is required for the pbr testr command. pbr requires a git
        # version or an sdist to work. so make the test directory a git repo
        # too.
        subprocess.call(['git', 'init'])
        exit_code = subprocess.call('bash pretty_tox.sh', shell=True,
                                    stdout=DEVNULL, stderr=DEVNULL)
        self.assertEqual(exit_code, 1)

    def test_pretty_tox_serial(self):
        # Copy wrapper script and requirements:
        pretty_tox = os.path.join(self.directory, 'pretty_tox_serial.sh')
        shutil.copy('tools/pretty_tox_serial.sh', pretty_tox)
        # Change directory, run wrapper and check result
        self.addCleanup(os.chdir, os.path.abspath(os.curdir))
        os.chdir(self.directory)
        exit_code = subprocess.call('bash pretty_tox_serial.sh tests.passing',
                                    shell=True, stdout=DEVNULL, stderr=DEVNULL)
        self.assertEqual(exit_code, 0)

    def test_pretty_tox_serial_fails(self):
        # Copy wrapper script and requirements:
        pretty_tox = os.path.join(self.directory, 'pretty_tox_serial.sh')
        shutil.copy('tools/pretty_tox_serial.sh', pretty_tox)
        # Change directory, run wrapper and check result
        self.addCleanup(os.chdir, os.path.abspath(os.curdir))
        os.chdir(self.directory)
        exit_code = subprocess.call('bash pretty_tox_serial.sh', shell=True,
                                    stdout=DEVNULL, stderr=DEVNULL)
        self.assertEqual(exit_code, 1)
<commit_msg>stop leaking tempdirs<commit_after># Copyright 2013 IBM Corp.
#
#    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 os
import shutil
import subprocess
import tempfile

from tempest.tests import base

DEVNULL = open(os.devnull, 'wb')

class TestWrappers(base.TestCase):
    def setUp(self):
        super(TestWrappers, self).setUp()
        # Setup test dirs
        self.directory = tempfile.mkdtemp(prefix='tempest-unit')
        self.addCleanup(shutil.rmtree, self.directory)
        self.test_dir = os.path.join(self.directory, 'tests')
        os.mkdir(self.test_dir)
        # Setup Test files
        self.testr_conf_file = os.path.join(self.directory, '.testr.conf')
        self.setup_cfg_file = os.path.join(self.directory, 'setup.cfg')
        self.passing_file = os.path.join(self.test_dir, 'test_passing.py')
        self.failing_file = os.path.join(self.test_dir, 'test_failing.py')
        self.init_file = os.path.join(self.test_dir, '__init__.py')
        self.setup_py = os.path.join(self.directory, 'setup.py')
        shutil.copy('tempest/tests/files/testr-conf', self.testr_conf_file)
        shutil.copy('tempest/tests/files/passing-tests', self.passing_file)
        shutil.copy('tempest/tests/files/failing-tests', self.failing_file)
        shutil.copy('setup.py', self.setup_py)
        shutil.copy('tempest/tests/files/setup.cfg', self.setup_cfg_file)
        shutil.copy('tempest/tests/files/__init__.py', self.init_file)

    def test_pretty_tox(self):
        # Copy wrapper script and requirements:
        pretty_tox = os.path.join(self.directory, 'pretty_tox.sh')
        shutil.copy('tools/pretty_tox.sh', pretty_tox)
        # Change directory, run wrapper and check result
        self.addCleanup(os.chdir, os.path.abspath(os.curdir))
        os.chdir(self.directory)
        # Git init is required for the pbr testr command. pbr requires a git
        # version or an sdist to work. so make the test directory a git repo
        # too.
        subprocess.call(['git', 'init'])
        exit_code = subprocess.call('bash pretty_tox.sh tests.passing',
                                    shell=True, stdout=DEVNULL, stderr=DEVNULL)
        self.assertEqual(exit_code, 0)

    def test_pretty_tox_fails(self):
        # Copy wrapper script and requirements:
        pretty_tox = os.path.join(self.directory, 'pretty_tox.sh')
        shutil.copy('tools/pretty_tox.sh', pretty_tox)
        # Change directory, run wrapper and check result
        self.addCleanup(os.chdir, os.path.abspath(os.curdir))
        os.chdir(self.directory)
        # Git init is required for the pbr testr command. pbr requires a git
        # version or an sdist to work. so make the test directory a git repo
        # too.
        subprocess.call(['git', 'init'])
        exit_code = subprocess.call('bash pretty_tox.sh', shell=True,
                                    stdout=DEVNULL, stderr=DEVNULL)
        self.assertEqual(exit_code, 1)

    def test_pretty_tox_serial(self):
        # Copy wrapper script and requirements:
        pretty_tox = os.path.join(self.directory, 'pretty_tox_serial.sh')
        shutil.copy('tools/pretty_tox_serial.sh', pretty_tox)
        # Change directory, run wrapper and check result
        self.addCleanup(os.chdir, os.path.abspath(os.curdir))
        os.chdir(self.directory)
        exit_code = subprocess.call('bash pretty_tox_serial.sh tests.passing',
                                    shell=True, stdout=DEVNULL, stderr=DEVNULL)
        self.assertEqual(exit_code, 0)

    def test_pretty_tox_serial_fails(self):
        # Copy wrapper script and requirements:
        pretty_tox = os.path.join(self.directory, 'pretty_tox_serial.sh')
        shutil.copy('tools/pretty_tox_serial.sh', pretty_tox)
        # Change directory, run wrapper and check result
        self.addCleanup(os.chdir, os.path.abspath(os.curdir))
        os.chdir(self.directory)
        exit_code = subprocess.call('bash pretty_tox_serial.sh', shell=True,
                                    stdout=DEVNULL, stderr=DEVNULL)
        self.assertEqual(exit_code, 1)
<|endoftext|>


<commit_before>import sublime, sublime_plugin
import os
import re
from subprocess import call
import time
import json

from .mistune import markdown

import gettext

PACKAGE_PATH = os.path.dirname(__file__)
LANG_PATH = PACKAGE_PATH + '/lang'

DEFAULT_LANG_CODE = 'eu'

ASSISTANTS_INPUT_MESSAGE = 'Write the meeting assistant list, separated with commas'
LOGO_INPUT_MESSAGE = 'Write the logo path'
LANGUAGE_INPUT_MESSAGE = 'Write the language code'

ASSISTANTS_FILE_NAME = '/attendees.sublime-meetings'
LOGO_FILE_NAME = '/logo.sublime-meetings'
LANG_FILE_NAME = '/language.sublime-meetings'

HTML_START = '<!DOCTYPE html><meta charset="utf-8">'
BODY_END = ''
HTML_END = ''

class CreateMinuteCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		html_source = []
		html_source.append(HTML_START)

		header_source = self.create_header()
		html_source.append(header_source)

		region = sublime.Region(0, self.view.size())
		md_source = self.view.substr(region)
		md_source.encode(encoding='UTF-8',errors='strict')
		html_source.append(markdown(md_source))
		html_source.append(BODY_END)

		css_file = "/home/txarli/.config/sublime-text-3/Packages/sublimetext-meeting-minutes/style.css"
		with open(css_file) as file_:
			css_source = file_.read()
		html_source.append('%s' % (css_source, HTML_END))

		file_name = self.view.file_name()
		html_file = self.change_extension(file_name, ".html")
		html_source_code = ''.join(html_source)
		with open(html_file, 'w+') as file_:
			file_.write(html_source_code)

		self.save_pdf(html_file)

		print('Created minute.')

	def change_extension(self,file_name, new_ext):
		f, ext = os.path.splitext(file_name)
		return '%s%s' % (f, new_ext)

	def save_pdf(self, html_file):
		pdf_file = self.change_extension(html_file, ".pdf")
		call(["wkhtmltopdf",html_file,pdf_file])

	def create_header(self):
		markdown_file = self.view.file_name()
		markdown_dir = os.path.dirname(markdown_file)

		language_file = markdown_dir + LANG_FILE_NAME
		if os.path.isfile(language_file):
			with open(language_file) as file_:
				lang_code = file_.read()
		else:
			lang_code = DEFAULT_LANG_CODE

		lang = gettext.translation('MeetingMinutes', localedir=LANG_PATH, languages=[lang_code])
		lang.install()

		header_source = []
		header_source.append('

')
		header_source.append('%s: ' % _('Date'))
		date_format = _('%d/%m/%Y')
		meeting_date = time.strftime(date_format)
		header_source.append(meeting_date)
		header_source.append('

%s:

' % _('Atendees'))

		assistants_file = markdown_dir + ASSISTANTS_FILE_NAME
		with open(assistants_file) as file_:
			meeting_assistants_list = file_.read().splitlines()

		meeting_assistants = []
		for assistant in meeting_assistants_list:
			meeting_assistants.append('
%s
' % assistant)

		header_source.append(''.join(meeting_assistants))
		header_source.append('

')

		logo_file_path = '%s%s' % (markdown_dir, LOGO_FILE_NAME)

		if os.path.isfile(logo_file_path):
			with open(logo_file_path) as file_:
				logo_path = file_.read()
			print(logo_path)
			header_source.append('' % logo_path)

		header_source.append('

')

		return ''.join(header_source)

class WriteAssistantsCommand (sublime_plugin.TextCommand):
	def run(self, edit):
		window = self.view.window()

		assistants_file = self.get_assistants_file()
		if os.path.isfile(assistants_file):
			with open(assistants_file) as file_:
				assistants = file_.read()
		else:
			assistants = None

		if assistants:
			initial_text = re.sub('\n', ', ', assistants)
		else:
			initial_text = ''

		window.show_input_panel(ASSISTANTS_INPUT_MESSAGE, initial_text, self.save_assistants, self.save_assistants, self.cancel_assistants)

	def save_assistants(self, assistants_list):
		assistants = assistants_list.split(',')
		assistants_doc = ''
		for assistant in assistants:
			assistant = re.sub('^(^[\s]*)(\s)', '', assistant)
			if assistant != '':
				assistant += '\n'
				assistants_doc += assistant

		assistants_file = self.get_assistants_file()
		with open(assistants_file, 'w+') as file_:
				file_.write(assistants_doc)

	def cancel_assistants(self):
		pass

	def get_assistants_file(self):
		markdown_file = self.view.file_name()
		assistants_directory = os.path.dirname(markdown_file)
		assistants_file = assistants_directory + ASSISTANTS_FILE_NAME

		return assistants_file

class WriteLogoCommand (sublime_plugin.TextCommand):
	def run(self, edit):
		window = self.view.window()

		logo_file = self.get_logo_file()
		if os.path.isfile(logo_file):
			with open(logo_file) as file_:
				logo_path = file_.read()
		else:
			logo_path = ''

		window.show_input_panel(LOGO_INPUT_MESSAGE, logo_path, self.save_logo, self.save_logo, self.cancel_assistants)

	def save_logo(self, logo_path):
		logo_file = self.get_logo_file()
		with open(logo_file, 'w+') as file_:
				file_.write(logo_path)

	def cancel_assistants(self):
		pass

	def get_logo_file(self):
		markdown_file = self.view.file_name()
		logo_directory = os.path.dirname(markdown_file)
		logo_file = logo_directory + LOGO_FILE_NAME

		return logo_file

class ChangeLanguageCommand(sublime_plugin.TextCommand):
	"""docstring for ChangeLanguageCommand"""
	def run(self, edit):
		window = self.view.window()

		language_file = self.get_language_file()
		if os.path.isfile(language_file):
			with open(language_file) as file_:
				lang = file_.read()
		else:
			lang = ''

		window.show_input_panel(LANGUAGE_INPUT_MESSAGE, lang, self.save_language, self.save_language, self.cancel_language)

	def save_language(self, lang):
		language_file = self.get_language_file()
		with open(language_file, 'w+') as file_:
			file_.write(lang)

	def cancel_language(self):
		pass

	def get_language_file(self):
		markdown_file = self.view.file_name()
		lang_directory = os.path.dirname(markdown_file)
		lang_file = lang_directory + LANG_FILE_NAME

		return lang_file
<commit_msg>Convert change_extension global method.<commit_after>import sublime, sublime_plugin
import os
import re
from subprocess import call
import time
import json

from .mistune import markdown

import gettext

PACKAGE_PATH = os.path.dirname(__file__)
LANG_PATH = PACKAGE_PATH + '/lang'

DEFAULT_LANG_CODE = 'eu'

ASSISTANTS_INPUT_MESSAGE = 'Write the meeting assistant list, separated with commas'
LOGO_INPUT_MESSAGE = 'Write the logo path'
LANGUAGE_INPUT_MESSAGE = 'Write the language code'

ASSISTANTS_FILE_NAME = '/attendees.sublime-meetings'
LOGO_FILE_NAME = '/logo.sublime-meetings'
LANG_FILE_NAME = '/language.sublime-meetings'

HTML_START = '<!DOCTYPE html><meta charset="utf-8">'
BODY_END = ''
HTML_END = ''

def change_extension(file_name, new_ext):
	f, ext = os.path.splitext(file_name)
	return '%s%s' % (f, new_ext)

class CreateMinuteCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		html_source = []
		html_source.append(HTML_START)

		header_source = self.create_header()
		html_source.append(header_source)

		region = sublime.Region(0, self.view.size())
		md_source = self.view.substr(region)
		md_source.encode(encoding='UTF-8',errors='strict')
		html_source.append(markdown(md_source))
		html_source.append(BODY_END)

		css_file = "/home/txarli/.config/sublime-text-3/Packages/sublimetext-meeting-minutes/style.css"
		with open(css_file) as file_:
			css_source = file_.read()
		html_source.append('%s' % (css_source, HTML_END))

		file_name = self.view.file_name()
		html_file = change_extension(file_name, ".html")
		html_source_code = ''.join(html_source)
		with open(html_file, 'w+') as file_:
			file_.write(html_source_code)

		self.save_pdf(html_file)

		print('Created minute.')

	def save_pdf(self, html_file):
		pdf_file = change_extension(html_file, ".pdf")
		call(["wkhtmltopdf",html_file,pdf_file])

	def create_header(self):
		markdown_file = self.view.file_name()
		markdown_dir = os.path.dirname(markdown_file)

		language_file = markdown_dir + LANG_FILE_NAME
		if os.path.isfile(language_file):
			with open(language_file) as file_:
				lang_code = file_.read()
		else:
			lang_code = DEFAULT_LANG_CODE

		lang = gettext.translation('MeetingMinutes', localedir=LANG_PATH, languages=[lang_code])
		lang.install()

		header_source = []
		header_source.append('

')
		header_source.append('%s: ' % _('Date'))
		date_format = _('%d/%m/%Y')
		meeting_date = time.strftime(date_format)
		header_source.append(meeting_date)
		header_source.append('

%s:

' % _('Atendees'))

		assistants_file = markdown_dir + ASSISTANTS_FILE_NAME
		with open(assistants_file) as file_:
			meeting_assistants_list = file_.read().splitlines()

		meeting_assistants = []
		for assistant in meeting_assistants_list:
			meeting_assistants.append('
%s
' % assistant)

		header_source.append(''.join(meeting_assistants))
		header_source.append('

')

		logo_file_path = '%s%s' % (markdown_dir, LOGO_FILE_NAME)

		if os.path.isfile(logo_file_path):
			with open(logo_file_path) as file_:
				logo_path = file_.read()
			print(logo_path)
			header_source.append('' % logo_path)

		header_source.append('

')

		return ''.join(header_source)

class WriteAssistantsCommand (sublime_plugin.TextCommand):
	def run(self, edit):
		window = self.view.window()

		assistants_file = self.get_assistants_file()
		if os.path.isfile(assistants_file):
			with open(assistants_file) as file_:
				assistants = file_.read()
		else:
			assistants = None

		if assistants:
			initial_text = re.sub('\n', ', ', assistants)
		else:
			initial_text = ''

		window.show_input_panel(ASSISTANTS_INPUT_MESSAGE, initial_text, self.save_assistants, self.save_assistants, self.cancel_assistants)

	def save_assistants(self, assistants_list):
		assistants = assistants_list.split(',')
		assistants_doc = ''
		for assistant in assistants:
			assistant = re.sub('^(^[\s]*)(\s)', '', assistant)
			if assistant != '':
				assistant += '\n'
				assistants_doc += assistant

		assistants_file = self.get_assistants_file()
		with open(assistants_file, 'w+') as file_:
				file_.write(assistants_doc)

	def cancel_assistants(self):
		pass

	def get_assistants_file(self):
		markdown_file = self.view.file_name()
		assistants_directory = os.path.dirname(markdown_file)
		assistants_file = assistants_directory + ASSISTANTS_FILE_NAME

		return assistants_file

class WriteLogoCommand (sublime_plugin.TextCommand):
	def run(self, edit):
		window = self.view.window()

		logo_file = self.get_logo_file()
		if os.path.isfile(logo_file):
			with open(logo_file) as file_:
				logo_path = file_.read()
		else:
			logo_path = ''

		window.show_input_panel(LOGO_INPUT_MESSAGE, logo_path, self.save_logo, self.save_logo, self.cancel_assistants)

	def save_logo(self, logo_path):
		logo_file = self.get_logo_file()
		with open(logo_file, 'w+') as file_:
				file_.write(logo_path)

	def cancel_assistants(self):
		pass

	def get_logo_file(self):
		markdown_file = self.view.file_name()
		logo_directory = os.path.dirname(markdown_file)
		logo_file = logo_directory + LOGO_FILE_NAME

		return logo_file

class ChangeLanguageCommand(sublime_plugin.TextCommand):
	"""docstring for ChangeLanguageCommand"""
	def run(self, edit):
		window = self.view.window()

		language_file = self.get_language_file()
		if os.path.isfile(language_file):
			with open(language_file) as file_:
				lang = file_.read()
		else:
			lang = ''

		window.show_input_panel(LANGUAGE_INPUT_MESSAGE, lang, self.save_language, self.save_language, self.cancel_language)

	def save_language(self, lang):
		language_file = self.get_language_file()
		with open(language_file, 'w+') as file_:
			file_.write(lang)

	def cancel_language(self):
		pass

	def get_language_file(self):
		markdown_file = self.view.file_name()
		lang_directory = os.path.dirname(markdown_file)
		lang_file = lang_directory + LANG_FILE_NAME

		return lang_file
<|endoftext|>


<commit_before>from time import sleep
from typing import List

import usb.core
import usb.util
import threading

from treehopper.api.HardwarePwmManager import HardwarePwmManager
from treehopper.api.HardwareSpi import HardwareSpi
from treehopper.api.HardwareUart import HardwareUart
from treehopper.utils.EventHandler import EventHandler
from treehopper.api.Pin import Pin
from treehopper.api.HardwarePwm import HardwarePwm
from treehopper.api.HardwareI2c import HardwareI2c
from treehopper.api.DeviceCommands import DeviceCommands
from treehopper.api.SoftPwmManager import SoftPwmManager

class TreehopperUsb:
    """
    The core class for communicating with Treehopper USB boards.

    This class represents a Treehopper board. You'll access all the pins, peripherals, and board functions through
    this object, which will automatically create all peripheral instances for you.

    Warning:
        Do not attempt to create a TreehopperUsb instance manually; always obtain references to boards from the
        treehopper.api.find_boards() function.

    Examples:
        >>> board = find_boards()[0]        # Get the first board.
        >>> board.connect()                 # Be sure to connect before doing anything else.
        >>> board.led = True                # Turn on the board's LED.
        >>> imu = Mpu9250.probe(board.i2c)  # Connect an MPU9250 9-DoF IMU to the I2C interface on the board.
        >>> print(imu.accelerometer)        # Print the acceleration reading from the sensor.
        '[0.95, 0.01, 0.03]'
        >>> board.disconnect()              # Disconnect from the board when finished.

    Attributes:
        pins (list[DigitalPin]): Collection of pins that belong to this board (List[Pin.Pin]).
        spi (HardwareSpi): The SPI peripheral (HardwareSpi.HardwareSpi).
        i2c (HardwareI2c): The I2C peripheral (HardwareI2c.HardwareI2c).
        uart (HardwareUart): The UART peripheral (HardwareUart.HardwareUart).
        pwm1 (HardwarePwm): The PWM1 peripheral (HardwarePwm.HardwarePwm).
        pwm2 (HardwarePwm): The PWM2 peripheral (HardwarePwm.HardwarePwm).
        pwm3 (HardwarePwm): The PWM3 peripheral (HardwarePwm.HardwarePwm).
        hardware_pwm_manager (HardwarePwmManager): The hardware PWM manager (HardwarePwmManager.HardwarePwmManager).

    """
    def __init__(self, dev: usb.core.Device):
        self._pin_listener_thread = threading.Thread(target=self._pin_listener)
        self._dev = dev
        self._comms_lock = threading.Lock()

        self._pin_report_endpoint = 0x81
        self._peripheral_response_endpoint = 0x82
        self._pin_config_endpoint = 0x01
        self._peripheral_config_endpoint = 0x02

        self.pins = []  # type: List[Pin]

        for i in range(20):
            self.pins.append(Pin(self, i))

        self.pins[0].name = "Pin 0 (SCK)"
        self.pins[1].name = "Pin 1 (MISO)"
        self.pins[2].name = "Pin 2 (MOSI)"
        self.pins[3].name = "Pin 3 (SDA)"
        self.pins[4].name = "Pin 4 (SCL)"
        self.pins[5].name = "Pin 5 (TX)"
        self.pins[6].name = "Pin 6 (RX)"
        self.pins[7].name = "Pin 7 (PWM1)"
        self.pins[8].name = "Pin 8 (PWM2)"
        self.pins[9].name = "Pin 9 (PWM3)"

        self.spi = HardwareSpi(self)
        self.i2c = HardwareI2c(self)
        self.uart = HardwareUart(self)

        self.pwm1 = HardwarePwm(self.pins[7])
        self.pwm2 = HardwarePwm(self.pins[8])
        self.pwm3 = HardwarePwm(self.pins[9])

        self.hardware_pwm_manager = HardwarePwmManager(self)
        self._soft_pwm_manager = SoftPwmManager(self)

        self._led = False
        self._connected = False
        self._pin_report_received = EventHandler(self)

    def connect(self):
        """
        Connect to the board.

        Calling this method will connect to the board and start the pin listener update thread. Repeated calls
        to this method are ignored.

        Warning:
            You must connect to the board before performing any operations (other than querying the name()
            or serial_number() of the board).

        Returns:
            None

        """
        if self._connected:
            return

        self._dev.set_configuration()
        self._connected = True
        self._pin_listener_thread.start()

    def disconnect(self):
        """Disconnect from the board.

        This method disconnects from the board and stops the pin listener update thread. Repeated calls to this
        method are ignored."""
        if not self._connected:
            return

        self._connected = False
        self._pin_listener_thread.join()
        usb.util.dispose_resources(self._dev)

    def reboot(self):
        """
        Reboots the board.

        Calling this method will automatically call the disconnect() method, and no further communication will
        be possible until the board is reopened.

        Returns:
            None
        """
        self._send_peripheral_config_packet([DeviceCommands.Reboot])
        self.disconnect()

    def reboot_into_bootloader(self):
        """
        Reboots the board into bootloader mode.

        Calling this method will automatically call the disconnect() method, and no further communication will
        be possible. You can load new firmware onto the board once in bootloader mode, or if you wish to
        return to normal operation, replug the board to reset it.

        Returns:
            None
        """
        self._send_peripheral_config_packet([DeviceCommands.EnterBootloader])
        self.disconnect()

    def update_serial_number(self, serial_number: str):
        """
        Update the serial number on the device.

        While the new serial number is immediately available from the SerialNumber property, the changes
        will not take effect in other applications until the device is reset. This can be done by calling
        reboot().

        Args:
            serial_number (str): a 60-character (or fewer) string containing the new serial number

        Returns:
            None

        Examples:
        >>> board = find_boards()[0]                 # Get the first board.
        >>> board.connect()                          # Be sure to connect before doing anything else.
        >>> board.update_serial_number("a3bY392")    # Change the serial number.
        >>> board.reboot()                           # Reboot the board so the OS picks up the changes.
        """
        length = len(serial_number)
        if length > 60:
            raise RuntimeError("String must be 60 characters or less")

        data_to_send = [DeviceCommands.FirmwareUpdateSerial, length] + list(serial_number.encode())
        self._send_peripheral_config_packet(data_to_send)
        sleep(0.1)

    def update_device_name(self, device_name: str):
        """
        Update the device name on the device.

        While the new serial number is immediately available from the SerialNumber property, the changes
        will not take effect in other applications until the device is reset. This can be done by calling
        reboot().

        Note:
            Microsoft Windows caches the device name in the registry when it is first attached to the board, so even
            if you change the device name with this function, you won't see the new device name --- even after
            replugging the board. To force Windows to update the registry with the new name, make sure to change the
            serial number, too (see update_serial_number()).

        Args:
            device_name (str): a 60-character (or fewer) string containing the new device name.

        Returns:
            None

        Examples:
        >>> board = find_boards()[0]                 # Get the first board.
        >>> board.connect()                          # Be sure to connect before doing anything else.
        >>> board.update_device_name("Acme Widget")  # Update the device name.
        >>> board.update_serial_number("a3bY392")    # Change the serial number to force Windows refresh.
        >>> board.reboot()                           # Reboot the board so the OS picks up the changes.

        """
        length = len(device_name)
        if length > 60:
            raise RuntimeError("String must be 60 characters or less")

        data_to_send = [DeviceCommands.FirmwareUpdateName, length] + list(device_name.encode())
        self._send_peripheral_config_packet(data_to_send)
        sleep(0.1)

    @property
    def serial_number(self):
        """
        [Property] Gets the serial number of the board.

        This property is available to read even without connecting to the board. If you wish to change the serial number,
        use update_serial_number().

        Note:
            While you do not need to connect() to the board before querying its serial number, you will not be able
            to retrieve the serial number of a board to which another application is connected.

            Treehopper's Python USB backend calls into LibUSB, which doesn't support querying the OS for device info,
            so LibUSB implicitly connects to the board, queries the string descriptor, and disconnects. Since
            concurrent operation isn't supported by LibUSB, this operation will error if the board is already open.

        Returns:
            str: The serial number.

        """
        return self._dev.serial_number

    @property
    def name(self):
        """
        [Property] Gets the device name of the board.

        This property is available to read even without connecting to the board. If you wish to change the device name,
        use update_device_name().

        Note:
            While you do not need to connect() to the board before querying its device name, you will not be able
            to retrieve the device name of a board to which another application is connected.

            Treehopper's Python USB backend calls into LibUSB, which doesn't support querying the OS for device info,
            so LibUSB implicitly connects to the board, queries the string descriptor, and disconnects. Since
            concurrent operation isn't supported by LibUSB, this operation will error if the board is already open.

        Returns:
            str: The device name.

        """
        return self._dev.product

    def _pin_listener(self):
        while self._connected:
            try:
                data = self._dev.read(self._pin_report_endpoint, 41, 1000)
                i = 1
                if data[0] == 0x02:
                    for pin in self.pins:
                        pin._update_value(data[i], data[i+1])
                        i += 2
            except:
                pass
        return

    def _send_pin_config(self, data: List[int]):
        self._dev.write(self._pin_config_endpoint, data)

    def _send_peripheral_config_packet(self, data: List[int]):
        self._dev.write(self._peripheral_config_endpoint, data)

    def _receive_comms_response_packet(self, num_bytes_to_read: int) -> List[int]:
        return self._dev.read(self._peripheral_response_endpoint, num_bytes_to_read)

    @property
    def led(self) -> bool:
        """
        [Property] Gets the state of the LED.

        Returns:
            bool: The current state of the LED.

        """
        return self._led

    @led.setter
    def led(self, val: bool) -> None:
        """
        [Property] Sets the state of the LED.

        Args:
            val (bool): The new state of the LED.

        Returns:
            None
        """
        self._led = val
        data = [DeviceCommands.LedConfig, self._led]
        self._dev.write(self._peripheral_config_endpoint, data)

    @property
    def connected(self):
        """
        [Property] Gets whether the board is connected.

        Returns:
            bool: Whether the board is connected

        Note:
            The board will automatically disconnect if an unrecoverable error is encountered (which includes a disconnect),
            so this is a useful property to consult to determine if a board is physically attached to a computer.
        """
        return self._connected
<commit_msg>Python board disconnect notification by sniffing for USB errors<commit_after>from time import sleep
from typing import List

import usb.core
import usb.util
import threading

from treehopper.api.HardwarePwmManager import HardwarePwmManager
from treehopper.api.HardwareSpi import HardwareSpi
from treehopper.api.HardwareUart import HardwareUart
from treehopper.utils.EventHandler import EventHandler
from treehopper.api.Pin import Pin
from treehopper.api.HardwarePwm import HardwarePwm
from treehopper.api.HardwareI2c import HardwareI2c
from treehopper.api.DeviceCommands import DeviceCommands
from treehopper.api.SoftPwmManager import SoftPwmManager

class TreehopperUsb:
    """
    The core class for communicating with Treehopper USB boards.

    This class represents a Treehopper board. You'll access all the pins, peripherals, and board functions through
    this object, which will automatically create all peripheral instances for you.

    Warning:
        Do not attempt to create a TreehopperUsb instance manually; always obtain references to boards from the
        treehopper.api.find_boards() function.

    Examples:
        >>> board = find_boards()[0]        # Get the first board.
        >>> board.connect()                 # Be sure to connect before doing anything else.
        >>> board.led = True                # Turn on the board's LED.
        >>> imu = Mpu9250.probe(board.i2c)  # Connect an MPU9250 9-DoF IMU to the I2C interface on the board.
        >>> print(imu.accelerometer)        # Print the acceleration reading from the sensor.
        '[0.95, 0.01, 0.03]'
        >>> board.disconnect()              # Disconnect from the board when finished.

    Attributes:
        pins (list[DigitalPin]): Collection of pins that belong to this board (List[Pin.Pin]).
        spi (HardwareSpi): The SPI peripheral (HardwareSpi.HardwareSpi).
        i2c (HardwareI2c): The I2C peripheral (HardwareI2c.HardwareI2c).
        uart (HardwareUart): The UART peripheral (HardwareUart.HardwareUart).
        pwm1 (HardwarePwm): The PWM1 peripheral (HardwarePwm.HardwarePwm).
        pwm2 (HardwarePwm): The PWM2 peripheral (HardwarePwm.HardwarePwm).
        pwm3 (HardwarePwm): The PWM3 peripheral (HardwarePwm.HardwarePwm).
        hardware_pwm_manager (HardwarePwmManager): The hardware PWM manager (HardwarePwmManager.HardwarePwmManager).

    """
    def __init__(self, dev: usb.core.Device):
        self._pin_listener_thread = threading.Thread(target=self._pin_listener)
        self._dev = dev
        self._comms_lock = threading.Lock()

        self._pin_report_endpoint = 0x81
        self._peripheral_response_endpoint = 0x82
        self._pin_config_endpoint = 0x01
        self._peripheral_config_endpoint = 0x02

        self.pins = []  # type: List[Pin]

        for i in range(20):
            self.pins.append(Pin(self, i))

        self.pins[0].name = "Pin 0 (SCK)"
        self.pins[1].name = "Pin 1 (MISO)"
        self.pins[2].name = "Pin 2 (MOSI)"
        self.pins[3].name = "Pin 3 (SDA)"
        self.pins[4].name = "Pin 4 (SCL)"
        self.pins[5].name = "Pin 5 (TX)"
        self.pins[6].name = "Pin 6 (RX)"
        self.pins[7].name = "Pin 7 (PWM1)"
        self.pins[8].name = "Pin 8 (PWM2)"
        self.pins[9].name = "Pin 9 (PWM3)"

        self.spi = HardwareSpi(self)
        self.i2c = HardwareI2c(self)
        self.uart = HardwareUart(self)

        self.pwm1 = HardwarePwm(self.pins[7])
        self.pwm2 = HardwarePwm(self.pins[8])
        self.pwm3 = HardwarePwm(self.pins[9])

        self.hardware_pwm_manager = HardwarePwmManager(self)
        self._soft_pwm_manager = SoftPwmManager(self)

        self._led = False
        self._connected = False
        self._pin_report_received = EventHandler(self)

    def connect(self):
        """
        Connect to the board.

        Calling this method will connect to the board and start the pin listener update thread. Repeated calls
        to this method are ignored.

        Warning:
            You must connect to the board before performing any operations (other than querying the name()
            or serial_number() of the board).

        Returns:
            None

        """
        if self._connected:
            return

        self._dev.set_configuration()
        self._connected = True
        self._pin_listener_thread.start()

    def disconnect(self):
        """Disconnect from the board.

        This method disconnects from the board and stops the pin listener update thread. Repeated calls to this
        method are ignored."""
        if not self._connected:
            return

        self._connected = False
        self._pin_listener_thread.join()
        usb.util.dispose_resources(self._dev)

    def reboot(self):
        """
        Reboots the board.

        Calling this method will automatically call the disconnect() method, and no further communication will
        be possible until the board is reopened.

        Returns:
            None
        """
        self._send_peripheral_config_packet([DeviceCommands.Reboot])
        self.disconnect()

    def reboot_into_bootloader(self):
        """
        Reboots the board into bootloader mode.

        Calling this method will automatically call the disconnect() method, and no further communication will
        be possible. You can load new firmware onto the board once in bootloader mode, or if you wish to
        return to normal operation, replug the board to reset it.

        Returns:
            None
        """
        self._send_peripheral_config_packet([DeviceCommands.EnterBootloader])
        self.disconnect()

    def update_serial_number(self, serial_number: str):
        """
        Update the serial number on the device.

        While the new serial number is immediately available from the SerialNumber property, the changes
        will not take effect in other applications until the device is reset. This can be done by calling
        reboot().

        Args:
            serial_number (str): a 60-character (or fewer) string containing the new serial number

        Returns:
            None

        Examples:
        >>> board = find_boards()[0]                 # Get the first board.
        >>> board.connect()                          # Be sure to connect before doing anything else.
        >>> board.update_serial_number("a3bY392")    # Change the serial number.
        >>> board.reboot()                           # Reboot the board so the OS picks up the changes.
        """
        length = len(serial_number)
        if length > 60:
            raise RuntimeError("String must be 60 characters or less")

        data_to_send = [DeviceCommands.FirmwareUpdateSerial, length] + list(serial_number.encode())
        self._send_peripheral_config_packet(data_to_send)
        sleep(0.1)

    def update_device_name(self, device_name: str):
        """
        Update the device name on the device.

        While the new serial number is immediately available from the SerialNumber property, the changes
        will not take effect in other applications until the device is reset. This can be done by calling
        reboot().

        Note:
            Microsoft Windows caches the device name in the registry when it is first attached to the board, so even
            if you change the device name with this function, you won't see the new device name --- even after
            replugging the board. To force Windows to update the registry with the new name, make sure to change the
            serial number, too (see update_serial_number()).

        Args:
            device_name (str): a 60-character (or fewer) string containing the new device name.

        Returns:
            None

        Examples:
        >>> board = find_boards()[0]                 # Get the first board.
        >>> board.connect()                          # Be sure to connect before doing anything else.
        >>> board.update_device_name("Acme Widget")  # Update the device name.
        >>> board.update_serial_number("a3bY392")    # Change the serial number to force Windows refresh.
        >>> board.reboot()                           # Reboot the board so the OS picks up the changes.

        """
        length = len(device_name)
        if length > 60:
            raise RuntimeError("String must be 60 characters or less")

        data_to_send = [DeviceCommands.FirmwareUpdateName, length] + list(device_name.encode())
        self._send_peripheral_config_packet(data_to_send)
        sleep(0.1)

    @property
    def serial_number(self):
        """
        [Property] Gets the serial number of the board.

        This property is available to read even without connecting to the board. If you wish to change the serial number,
        use update_serial_number().

        Note:
            While you do not need to connect() to the board before querying its serial number, you will not be able
            to retrieve the serial number of a board to which another application is connected.

            Treehopper's Python USB backend calls into LibUSB, which doesn't support querying the OS for device info,
            so LibUSB implicitly connects to the board, queries the string descriptor, and disconnects. Since
            concurrent operation isn't supported by LibUSB, this operation will error if the board is already open.

        Returns:
            str: The serial number.

        """
        return self._dev.serial_number

    @property
    def name(self):
        """
        [Property] Gets the device name of the board.

        This property is available to read even without connecting to the board. If you wish to change the device name,
        use update_device_name().

        Note:
            While you do not need to connect() to the board before querying its device name, you will not be able
            to retrieve the device name of a board to which another application is connected.

            Treehopper's Python USB backend calls into LibUSB, which doesn't support querying the OS for device info,
            so LibUSB implicitly connects to the board, queries the string descriptor, and disconnects. Since
            concurrent operation isn't supported by LibUSB, this operation will error if the board is already open.

        Returns:
            str: The device name.

        """
        return self._dev.product

    def _pin_listener(self):
        while self._connected:
            try:
                data = self._dev.read(self._pin_report_endpoint, 41, 1000)
                i = 1
                if data[0] == 0x02:
                    for pin in self.pins:
                        pin._update_value(data[i], data[i+1])
                        i += 2
            except usb.USBError as e:
                if e.errno == 10060:  # timeout win32
                    pass
                elif e.errno == 110:  # timeout libusb
                    pass
                elif e.errno == 60:   # timeout macos
                    pass
                else:
                    self._connected = False
        return

    def _send_pin_config(self, data: List[int]):
        try:
            self._dev.write(self._pin_config_endpoint, data)
        except usb.USBError:
            self._connected = False

    def _send_peripheral_config_packet(self, data: List[int]):
        try:
            self._dev.write(self._peripheral_config_endpoint, data)
        except usb.USBError:
            self._connected = False

    def _receive_comms_response_packet(self, num_bytes_to_read: int) -> List[int]:
        try:
            return self._dev.read(self._peripheral_response_endpoint, num_bytes_to_read)
        except usb.USBError:
            self._connected = False
            return [0] * num_bytes_to_read

    @property
    def led(self) -> bool:
        """
        [Property] Gets the state of the LED.

        Returns:
            bool: The current state of the LED.

        """
        return self._led

    @led.setter
    def led(self, val: bool) -> None:
        """
        [Property] Sets the state of the LED.

        Args:
            val (bool): The new state of the LED.

        Returns:
            None
        """
        self._led = val
        data = [DeviceCommands.LedConfig, self._led]
        self._send_peripheral_config_packet(data)

    @property
    def connected(self):
        """
        [Property] Gets whether the board is connected.

        Returns:
            bool: Whether the board is connected

        Note:
            The board will automatically disconnect if an unrecoverable error is encountered (which includes a disconnect),
            so this is a useful property to consult to determine if a board is physically attached to a computer.
        """
        return self._connected
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
'''
The service module for OpenBSD
'''

# Import python libs
from __future__ import absolute_import
import os
import logging

# Import 3rd-party libs
import salt.ext.six as six
from salt.ext.six.moves import map  # pylint: disable=import-error,redefined-builtin

# Import Salt libs
import salt.utils

log = logging.getLogger(__name__)

# XXX enable/disable support would be nice

# Define the module's virtual name
__virtualname__ = 'service'

__func_alias__ = {
    'reload_': 'reload'
}

def __virtual__():
    '''
    Only work on OpenBSD
    '''
    if __grains__['os'] == 'OpenBSD' and os.path.exists('/etc/rc.d/rc.subr'):
        krel = list(list(map(int, __grains__['kernelrelease'].split('.'))))
        # The -f flag, used to force a script to run even if disabled,
        # was added after the 5.0 release.
        # the rcctl(8) command is the preferred way to manage services.
        if krel[0] > 5 or (krel[0] == 5 and krel[1] > 0):
            if not os.path.exists('/usr/sbin/rcctl'):
                return __virtualname__
    return False

def start(name):
    '''
    Start the specified service

    CLI Example:

    .. code-block:: bash

        salt '*' service.start <service name>
    '''
    cmd = '/etc/rc.d/{0} -f start'.format(name)
    return not __salt__['cmd.retcode'](cmd)

def stop(name):
    '''
    Stop the specified service

    CLI Example:

    .. code-block:: bash

        salt '*' service.stop <service name>
    '''
    cmd = '/etc/rc.d/{0} -f stop'.format(name)
    return not __salt__['cmd.retcode'](cmd)

def restart(name):
    '''
    Restart the named service

    CLI Example:

    .. code-block:: bash

        salt '*' service.restart <service name>
    '''
    cmd = '/etc/rc.d/{0} -f restart'.format(name)
    return not __salt__['cmd.retcode'](cmd)

def status(name, sig=None):
    '''
    Return the status for a service, returns a bool whether the service is
    running.

    CLI Example:

    .. code-block:: bash

        salt '*' service.status <service name>
    '''
    if sig:
        return bool(__salt__['status.pid'](sig))
    cmd = '/etc/rc.d/{0} -f check'.format(name)
    return not __salt__['cmd.retcode'](cmd)

def reload_(name):
    '''
    .. versionadded:: 2014.7.0

    Reload the named service

    CLI Example:

    .. code-block:: bash

        salt '*' service.reload <service name>
    '''
    cmd = '/etc/rc.d/{0} -f reload'.format(name)
    return not __salt__['cmd.retcode'](cmd)

import re
service_flags_regex = re.compile(r'^\s*(\w[\d\w]*)_flags=(?:(NO)|.*)$')
pkg_scripts_regex = re.compile(r'^\s*pkg_scripts=\'(.*)\'$')
start_daemon_call_regex = re.compile(r'(\s*start_daemon(?!\(\)))')
start_daemon_parameter_regex = re.compile(r'(?:\s+(\w[\w\d]*))')

def _get_rc():
    '''
    Returns a dict where the key is the daemon's name and
    the value a boolean indicating its status (True: enabled or False: disabled).
    Check the daemons started by the system in /etc/rc and
    configured in /etc/rc.conf and /etc/rc.conf.local.
    Also add to the dict all the localy enabled daemons via $pkg_scripts.
    '''
    daemons_flags = {}

    try:
        # now read the system startup script /etc/rc
        # to know what are the system enabled daemons
        with salt.utils.fopen('/etc/rc', 'r') as handle:
            lines = handle.readlines()
    except IOError:
        log.error('Unable to read /etc/rc')
    else:
        for line in lines:
            match = start_daemon_call_regex.match(line)
            if match:
                # the matched line is a call to start_daemon()
                # we remove the function name
                line = line[len(match.group(1)):]
                # we retrieve each daemon name from the parameters of start_daemon()
                for daemon in start_daemon_parameter_regex.findall(line):
                    # mark it as enabled
                    daemons_flags[daemon] = True

    # this will execute rc.conf and rc.conf.local
    # used in /etc/rc at boot to start the daemons
    variables = __salt__['cmd.run']('(. /etc/rc.conf && set)',
                                    clean_env=True,
                                    output_loglevel='quiet',
                                    python_shell=True).split('\n')
    for var in variables:
        match = service_flags_regex.match(var)
        if match:
            # the matched var look like daemon_name_flags=, we test its assigned value
            # NO: disabled, everything else: enabled
            # do not create a new key if the service hasn't been found in /etc/rc, see $pkg_scripts
            if match.group(2) == 'NO':
                daemons_flags[match.group(1)] = False
        else:
            match = pkg_scripts_regex.match(var)
            if match:
                # the matched var is pkg_scripts
                # we can retrieve the name of each localy enabled daemon that wasn't hand started via /etc/rc
                for daemon in match.group(1).split():
                    # create a new key and mark it as enabled
                    daemons_flags[daemon] = True

    return daemons_flags

def available(name):
    '''
    .. versionadded:: 2014.7.0

    Returns ``True`` if the specified service is available, otherwise returns
    ``False``.

    CLI Example:

    .. code-block:: bash

        salt '*' service.available sshd
    '''
    path = '/etc/rc.d/{0}'.format(name)
    return os.path.isfile(path) and os.access(path, os.X_OK)

def missing(name):
    '''
    .. versionadded:: 2014.7.0

    The inverse of service.available.
    Returns ``True`` if the specified service is not available, otherwise returns
    ``False``.

    CLI Example:

    .. code-block:: bash

        salt '*' service.missing sshd
    '''
    return not available(name)

def get_all():
    '''
    .. versionadded:: 2014.7.0

    Return all available boot services

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_all
    '''
    services = []
    if not os.path.isdir('/etc/rc.d'):
        return services
    for service in os.listdir('/etc/rc.d'):
        # this will remove rc.subr and all non executable files
        if available(service):
            services.append(service)
    return sorted(services)

def get_enabled():
    '''
    .. versionadded:: 2014.7.0

    Return a list of service that are enabled on boot

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_enabled
    '''
    services = []
    for daemon, is_enabled in six.iteritems(_get_rc()):
        if is_enabled:
            services.append(daemon)
    return sorted(set(get_all()) & set(services))

def enabled(name, **kwargs):
    '''
    .. versionadded:: 2014.7.0

    Return True if the named service is enabled, false otherwise

    CLI Example:

    .. code-block:: bash

        salt '*' service.enabled <service name>
    '''
    return name in get_enabled()

def get_disabled():
    '''
    .. versionadded:: 2014.7.0

    Return a set of services that are installed but disabled

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_disabled
    '''
    services = []
    for daemon, is_enabled in six.iteritems(_get_rc()):
        if not is_enabled:
            services.append(daemon)
    return sorted(set(get_all()) & set(services))

def disabled(name):
    '''
    .. versionadded:: 2014.7.0

    Return True if the named service is disabled, false otherwise

    CLI Example:

    .. code-block:: bash

        salt '*' service.disabled <service name>
    '''
    return name in get_disabled()
<commit_msg>modules.openbsdservice: __virtual__ return err msg.<commit_after># -*- coding: utf-8 -*-
'''
The service module for OpenBSD
'''

# Import python libs
from __future__ import absolute_import
import os
import logging

# Import 3rd-party libs
import salt.ext.six as six
from salt.ext.six.moves import map  # pylint: disable=import-error,redefined-builtin

# Import Salt libs
import salt.utils

log = logging.getLogger(__name__)

# XXX enable/disable support would be nice

# Define the module's virtual name
__virtualname__ = 'service'

__func_alias__ = {
    'reload_': 'reload'
}

def __virtual__():
    '''
    Only work on OpenBSD
    '''
    if __grains__['os'] == 'OpenBSD' and os.path.exists('/etc/rc.d/rc.subr'):
        krel = list(list(map(int, __grains__['kernelrelease'].split('.'))))
        # The -f flag, used to force a script to run even if disabled,
        # was added after the 5.0 release.
        # the rcctl(8) command is the preferred way to manage services.
        if krel[0] > 5 or (krel[0] == 5 and krel[1] > 0):
            if not os.path.exists('/usr/sbin/rcctl'):
                return __virtualname__
    return (False, 'The openbsdservice execution module cannot be loaded: '
            'only available on OpenBSD systems.')

def start(name):
    '''
    Start the specified service

    CLI Example:

    .. code-block:: bash

        salt '*' service.start <service name>
    '''
    cmd = '/etc/rc.d/{0} -f start'.format(name)
    return not __salt__['cmd.retcode'](cmd)

def stop(name):
    '''
    Stop the specified service

    CLI Example:

    .. code-block:: bash

        salt '*' service.stop <service name>
    '''
    cmd = '/etc/rc.d/{0} -f stop'.format(name)
    return not __salt__['cmd.retcode'](cmd)

def restart(name):
    '''
    Restart the named service

    CLI Example:

    .. code-block:: bash

        salt '*' service.restart <service name>
    '''
    cmd = '/etc/rc.d/{0} -f restart'.format(name)
    return not __salt__['cmd.retcode'](cmd)

def status(name, sig=None):
    '''
    Return the status for a service, returns a bool whether the service is
    running.

    CLI Example:

    .. code-block:: bash

        salt '*' service.status <service name>
    '''
    if sig:
        return bool(__salt__['status.pid'](sig))
    cmd = '/etc/rc.d/{0} -f check'.format(name)
    return not __salt__['cmd.retcode'](cmd)

def reload_(name):
    '''
    .. versionadded:: 2014.7.0

    Reload the named service

    CLI Example:

    .. code-block:: bash

        salt '*' service.reload <service name>
    '''
    cmd = '/etc/rc.d/{0} -f reload'.format(name)
    return not __salt__['cmd.retcode'](cmd)

import re
service_flags_regex = re.compile(r'^\s*(\w[\d\w]*)_flags=(?:(NO)|.*)$')
pkg_scripts_regex = re.compile(r'^\s*pkg_scripts=\'(.*)\'$')
start_daemon_call_regex = re.compile(r'(\s*start_daemon(?!\(\)))')
start_daemon_parameter_regex = re.compile(r'(?:\s+(\w[\w\d]*))')

def _get_rc():
    '''
    Returns a dict where the key is the daemon's name and
    the value a boolean indicating its status (True: enabled or False: disabled).
    Check the daemons started by the system in /etc/rc and
    configured in /etc/rc.conf and /etc/rc.conf.local.
    Also add to the dict all the localy enabled daemons via $pkg_scripts.
    '''
    daemons_flags = {}

    try:
        # now read the system startup script /etc/rc
        # to know what are the system enabled daemons
        with salt.utils.fopen('/etc/rc', 'r') as handle:
            lines = handle.readlines()
    except IOError:
        log.error('Unable to read /etc/rc')
    else:
        for line in lines:
            match = start_daemon_call_regex.match(line)
            if match:
                # the matched line is a call to start_daemon()
                # we remove the function name
                line = line[len(match.group(1)):]
                # we retrieve each daemon name from the parameters of start_daemon()
                for daemon in start_daemon_parameter_regex.findall(line):
                    # mark it as enabled
                    daemons_flags[daemon] = True

    # this will execute rc.conf and rc.conf.local
    # used in /etc/rc at boot to start the daemons
    variables = __salt__['cmd.run']('(. /etc/rc.conf && set)',
                                    clean_env=True,
                                    output_loglevel='quiet',
                                    python_shell=True).split('\n')
    for var in variables:
        match = service_flags_regex.match(var)
        if match:
            # the matched var look like daemon_name_flags=, we test its assigned value
            # NO: disabled, everything else: enabled
            # do not create a new key if the service hasn't been found in /etc/rc, see $pkg_scripts
            if match.group(2) == 'NO':
                daemons_flags[match.group(1)] = False
        else:
            match = pkg_scripts_regex.match(var)
            if match:
                # the matched var is pkg_scripts
                # we can retrieve the name of each localy enabled daemon that wasn't hand started via /etc/rc
                for daemon in match.group(1).split():
                    # create a new key and mark it as enabled
                    daemons_flags[daemon] = True

    return daemons_flags

def available(name):
    '''
    .. versionadded:: 2014.7.0

    Returns ``True`` if the specified service is available, otherwise returns
    ``False``.

    CLI Example:

    .. code-block:: bash

        salt '*' service.available sshd
    '''
    path = '/etc/rc.d/{0}'.format(name)
    return os.path.isfile(path) and os.access(path, os.X_OK)

def missing(name):
    '''
    .. versionadded:: 2014.7.0

    The inverse of service.available.
    Returns ``True`` if the specified service is not available, otherwise returns
    ``False``.

    CLI Example:

    .. code-block:: bash

        salt '*' service.missing sshd
    '''
    return not available(name)

def get_all():
    '''
    .. versionadded:: 2014.7.0

    Return all available boot services

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_all
    '''
    services = []
    if not os.path.isdir('/etc/rc.d'):
        return services
    for service in os.listdir('/etc/rc.d'):
        # this will remove rc.subr and all non executable files
        if available(service):
            services.append(service)
    return sorted(services)

def get_enabled():
    '''
    .. versionadded:: 2014.7.0

    Return a list of service that are enabled on boot

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_enabled
    '''
    services = []
    for daemon, is_enabled in six.iteritems(_get_rc()):
        if is_enabled:
            services.append(daemon)
    return sorted(set(get_all()) & set(services))

def enabled(name, **kwargs):
    '''
    .. versionadded:: 2014.7.0

    Return True if the named service is enabled, false otherwise

    CLI Example:

    .. code-block:: bash

        salt '*' service.enabled <service name>
    '''
    return name in get_enabled()

def get_disabled():
    '''
    .. versionadded:: 2014.7.0

    Return a set of services that are installed but disabled

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_disabled
    '''
    services = []
    for daemon, is_enabled in six.iteritems(_get_rc()):
        if not is_enabled:
            services.append(daemon)
    return sorted(set(get_all()) & set(services))

def disabled(name):
    '''
    .. versionadded:: 2014.7.0

    Return True if the named service is disabled, false otherwise

    CLI Example:

    .. code-block:: bash

        salt '*' service.disabled <service name>
    '''
    return name in get_disabled()
<|endoftext|>


<commit_before>import xgboost as xgb
import numpy as np

from sknn.platform import gpu32
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.svm import SVC
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import normalize

from keras.wrappers.scikit_learn import KerasClassifier
from keras.models import Sequential

import lasagne
from lasagne.layers import DenseLayer
from lasagne.layers import InputLayer
from lasagne.layers import DropoutLayer
from lasagne.updates import adagrad, nesterov_momentum
from lasagne.nonlinearities import softmax, tanh
from lasagne.objectives import binary_crossentropy
from nolearn.lasagne import NeuralNet
from nolearn.lasagne import TrainSplit

from joblib import Parallel, delayed

SEED = 42
np.random.seed(SEED)

def _predict_proba(cls, preproc, est, weight, x):
	p = est.predict_proba(cls.applyPreproc(preproc, x))
	r = weight*p
	return r

class MetaClassifierException(Exception):
	def __init__(self, e):
		self.exception = e
	def __str__(self):
		return self.exception

class MetaClassifier(object):

	def __init__(self, weights=list(), parallel=False, verbose=False):
		self.__estimators = list()
		self.__weights = weights
		self.__parallel = parallel
		self.__verbose = verbose

		self.feature_importances_ = list()
		self.standardScaler = StandardScaler()

	def fit(self, X, y):
		X = X.astype(np.float32)
		y = y.astype(np.int32)

		self.standardScaler.fit(X)

		for name, preproc, est in self.__estimators:
			if self.__verbose: print "Fitting estimator %s" % name
			est.fit(self.applyPreproc(preproc, X), y)

		self.getFeatureImportance()

	def predict_proba(self, x):
		if not list(self.__weights):
			self.__weights = np.ones(len(self.__estimators))

		if len(self.__weights) != len(self.__estimators):
			raise MetaClassifierException("Number of weights to estimator mismatch!")

		predictions = list()
		weights = self.__weights/np.sum(self.__weights)

		if self.__parallel:
			estList = list()
			for (name, preproc, est), weight in zip(self.__estimators, weights):
				estList.append((self, preproc, est, weight, x))

			with Parallel(n_jobs=len(self.__estimators), backend="threading") as parallel:
				predictions += parallel(
					delayed(_predict_proba)(*job) for job in estList
				)
		else:
			for (name, preproc, est), weight in zip(self.__estimators, weights):
				probs = est.predict_proba(self.applyPreproc(preproc, x))
				predictions.append(probs*weight)

		return np.sum(predictions, axis=0)

	def setWeights(self, weights):
		self.__weights = weights

	def setVerbose(self, verbose):
		self.__verbose = verbose

	def getFeatureImportance(self):
		featImportance = list()
		for (name, preproc, est) in self.__estimators:
			if hasattr(est, 'feature_importances_'):
				featImportance.append(est.feature_importances_)
		if featImportance:
			featImportance = np.mean(np.array(featImportance), axis=0)
		return featImportance

	def getEstimatorList(self):
		return self.__estimators

	def resetEstimatorList(self):
		self.__estimators = list()

	def applyPreproc(self, preproc, x):
		if preproc == 'scale':
			if self.__verbose: print "preproc: StandardScaler"
			x_ = self.standardScaler.transform(x)
			return x_.astype(np.float32)
		if preproc:
			if self.__verbose: print "preproc:", preproc
			x_ = np.copy(x)
			x_ = preproc(x_)
			return x_.astype(np.float32)
		else:
			return x.astype(np.float32)

	def addRFC(self, preproc=None, params={}):
		name = 'RFC'
		self.getEstimatorList().append((name, preproc, RandomForestClassifier(**params)))

	def addETC(self, preproc=None, params={}):
		name = 'ETC'
		self.getEstimatorList().append((name, preproc, ExtraTreesClassifier(**params)))

	def addLR(self, preproc=None, params={}):
		name = 'LR'
		self.getEstimatorList().append((name, preproc, LogisticRegression(**params)))

	def addGBC(self, preproc=None, params={}):
		name = 'GBC'
		self.getEstimatorList().append((name, preproc, GradientBoostingClassifier(**params)))

	def addXGBC(self, preproc=None, params={}):
		name = 'XGBC'
		self.getEstimatorList().append((name, preproc, xgb.XGBClassifier(**params)))

	def addKNC(self, preproc=None, params={}):
		name = 'KNC'
		self.getEstimatorList().append((name, preproc, KNeighborsClassifier(**params)))

	def addMLPC(self, preproc=None, params={}):
		name = 'MLPC'
		self.getEstimatorList().append((name, preproc, MLPClassifier(**params)))

	def addSVC(self, preproc=None, params={}):
		name = 'SVC'
		self.getEstimatorList().append((name, preproc, SVC(**params)))

	def addKNN(self, preproc=None, params={}):
		name = 'KNN'

		est = KerasClassifier(
			build_fn=params['build_fn'],
			nb_epoch=params['nb_epoch'],
			batch_size=64, #params['batch_size'],
			verbose=0
		)

		self.getEstimatorList().append((name, preproc, est))

	def addLNN(self, preproc=None, params={}):
		name = 'LNN'

		lasagne.random.set_rng(np.random.RandomState(SEED))

		layers = [
			('input', InputLayer),
			('dense0', DenseLayer),
			('dropout0', DropoutLayer),
			('dense1', DenseLayer),
			('dropout1', DropoutLayer),
			('dense2', DenseLayer),
			('dropout2', DropoutLayer),
			('output', DenseLayer)
		]
		input_shape = params['input_shape']
		est = NeuralNet(layers=layers,
			input_shape=(None, input_shape),
			dense0_num_units=params['dense0_num_units'],
			dense0_nonlinearity=tanh,
			dropout0_p=params['dropout0_p'],
			dense1_num_units=params['dense1_num_units'],
			dense1_nonlinearity=tanh,
			dropout1_p=params['dropout1_p'],
			dense2_num_units=params['dense2_num_units'],
			dense2_nonlinearity=tanh,
			dropout2_p=params['dropout2_p'],
			output_num_units=params['output_shape'],
			output_nonlinearity=softmax,
			update=adagrad,
			update_learning_rate=params['update_learning_rate'],
			#train_split=TrainSplit(params['train_split']),
			max_epochs=params['max_epochs'],
			verbose=1,
		)

		self.getEstimatorList().append((name, preproc, est))
<commit_msg>Merging predict, import error passive<commit_after>import numpy as np
from joblib import Parallel, delayed

from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import LabelBinarizer
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.svm import SVC
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import normalize

try:
	import xgboost as xgb
except ImportError:
	pass

try:
	from sknn.platform import gpu32
except ImportError:
	pass

try:
	from keras.wrappers.scikit_learn import KerasClassifier
	from keras.models import Sequential
except ImportError:
	pass

try:
	import lasagne
	from lasagne.layers import DenseLayer
	from lasagne.layers import InputLayer
	from lasagne.layers import DropoutLayer
	from lasagne.updates import adagrad, nesterov_momentum
	from lasagne.nonlinearities import softmax, tanh
	from lasagne.objectives import binary_crossentropy
	from nolearn.lasagne import NeuralNet
	from nolearn.lasagne import TrainSplit
except ImportError:
	pass

SEED = 42
np.random.seed(SEED)

def _predict_proba(cls, preproc, est, weight, x):
	proba = est.predict_proba(cls.applyPreproc(preproc, x))
	result = weight*proba
	return result

class MetaClassifierException(Exception):
	def __init__(self, e):
		self.exception = e
	def __str__(self):
		return self.exception

class MetaClassifier(object):

	def __init__(self, weights=list(), parallel=False, verbose=False):
		self.__estimators = list()
		self.__weights = weights
		self.__parallel = parallel
		self.__verbose = verbose

		self.feature_importances_ = list()
		self.classes_ = list()

		self.labelBinarizer = LabelBinarizer()
		self.standardScaler = StandardScaler()

	def fit(self, X, y):
		X = X.astype(np.float32)
		y = y.astype(np.int32)

		self.standardScaler.fit(X)
		self.labelBinarizer.fit(y)

		for name, preproc, est in self.__estimators:
			if self.__verbose: print "Fitting estimator %s" % name
			est.fit(self.applyPreproc(preproc, X), y)

		self.getFeatureImportance()

	def predict(self, x):
		probas = self.predict_proba(x)
		indices = probas.argmax(axis=1)
		return self.classes_[indices]

	def predict_proba(self, x):
		if not list(self.__weights):
			self.__weights = np.ones(len(self.__estimators))

		if len(self.__weights) != len(self.__estimators):
			raise MetaClassifierException("Number of weights to estimator mismatch!")

		predictions = list()
		weights = self.__weights/np.sum(self.__weights)

		if self.__parallel:
			estList = list()
			for (name, preproc, est), weight in zip(self.__estimators, weights):
				estList.append((self, preproc, est, weight, x))

			with Parallel(n_jobs=len(self.__estimators), backend="threading") as parallel:
				predictions += parallel(
					delayed(_predict_proba)(*job) for job in estList
				)
		else:
			for (name, preproc, est), weight in zip(self.__estimators, weights):
				probs = est.predict_proba(self.applyPreproc(preproc, x))
				predictions.append(probs*weight)

		return np.sum(predictions, axis=0)

	def setWeights(self, weights):
		self.__weights = weights

	def setVerbose(self, verbose):
		self.__verbose = verbose

	def getFeatureImportance(self):
		featImportance = list()
		for (name, preproc, est) in self.__estimators:
			if hasattr(est, 'feature_importances_'):
				featImportance.append(est.feature_importances_)
		if featImportance:
			featImportance = np.mean(np.array(featImportance), axis=0)
		return featImportance

	def getEstimatorList(self):
		return self.__estimators

	def resetEstimatorList(self):
		self.__estimators = list()

	def applyPreproc(self, preproc, x):
		if preproc == 'scale':
			if self.__verbose: print "preproc: StandardScaler"
			x_ = self.standardScaler.transform(x)
			return x_.astype(np.float32)
		if preproc:
			if self.__verbose: print "preproc:", preproc
			x_ = np.copy(x)
			x_ = preproc(x_)
			return x_.astype(np.float32)
		else:
			return x.astype(np.float32)

	def addRFC(self, preproc=None, params={}):
		name = 'RFC'
		self.getEstimatorList().append((name, preproc, RandomForestClassifier(**params)))

	def addETC(self, preproc=None, params={}):
		name = 'ETC'
		self.getEstimatorList().append((name, preproc, ExtraTreesClassifier(**params)))

	def addLR(self, preproc=None, params={}):
		name = 'LR'
		self.getEstimatorList().append((name, preproc, LogisticRegression(**params)))

	def addGBC(self, preproc=None, params={}):
		name = 'GBC'
		self.getEstimatorList().append((name, preproc, GradientBoostingClassifier(**params)))

	def addXGBC(self, preproc=None, params={}):
		name = 'XGBC'
		self.getEstimatorList().append((name, preproc, xgb.XGBClassifier(**params)))

	def addKNC(self, preproc=None, params={}):
		name = 'KNC'
		self.getEstimatorList().append((name, preproc, KNeighborsClassifier(**params)))

	def addMLPC(self, preproc=None, params={}):
		name = 'MLPC'
		self.getEstimatorList().append((name, preproc, MLPClassifier(**params)))

	def addSVC(self, preproc=None, params={}):
		name = 'SVC'
		self.getEstimatorList().append((name, preproc, SVC(**params)))

	def addKNN(self, preproc=None, params={}):
		name = 'KNN'

		est = KerasClassifier(
			build_fn=params['build_fn'],
			nb_epoch=params['nb_epoch'],
			batch_size=64, #params['batch_size'],
			verbose=0
		)

		self.getEstimatorList().append((name, preproc, est))

	def addLNN(self, preproc=None, params={}):
		name = 'LNN'

		lasagne.random.set_rng(np.random.RandomState(SEED))

		layers = [
			('input', InputLayer),
			('dense0', DenseLayer),
			('dropout0', DropoutLayer),
			('dense1', DenseLayer),
			('dropout1', DropoutLayer),
			('dense2', DenseLayer),
			('dropout2', DropoutLayer),
			('output', DenseLayer)
		]
		input_shape = params['input_shape']
		est = NeuralNet(layers=layers,
			input_shape=(None, input_shape),
			dense0_num_units=params['dense0_num_units'],
			dense0_nonlinearity=tanh,
			dropout0_p=params['dropout0_p'],
			dense1_num_units=params['dense1_num_units'],
			dense1_nonlinearity=tanh,
			dropout1_p=params['dropout1_p'],
			dense2_num_units=params['dense2_num_units'],
			dense2_nonlinearity=tanh,
			dropout2_p=params['dropout2_p'],
			output_num_units=params['output_shape'],
			output_nonlinearity=softmax,
			update=adagrad,
			update_learning_rate=params['update_learning_rate'],
			#train_split=TrainSplit(params['train_split']),
			max_epochs=params['max_epochs'],
			verbose=1,
		)

		self.getEstimatorList().append((name, preproc, est))
<|endoftext|>


<commit_before>##input: /Users/metagenomics/Documents/Databases/uniprot_tremble.dat or uniprot_sprot.dat

import sys
import os
from Bio import SeqIO
#import gzip

uniprot = open(sys.argv[1], "rU")

taxa_to_print=[]
for record in SeqIO.parse(uniprot, "swiss"):
#	print record
#	id = record.id
	taxa_to_print.append(sys.argv[1].split(".")[0])
	taxa_to_print.append(record.annotations['organism'])
	for taxa in record.annotations['taxonomy']:
		taxa_to_print.append(taxa)
print '\t'.join(taxa_to_print)
	#if record.annotations['taxonomy'] >1:
	#	domain = record.annotations['taxonomy'][1]
	#	if kingdom != 'Eukaryota':
	#		print "%s\t%s" % (id, kingdom)
	#	else:
	#		if domain == 'Fungi':
#	print '\t'.joint(strain, taxa_to_print)
<commit_msg>wrote get taxa into a function, removed fungi code from before<commit_after>##input: /Users/metagenomics/Documents/Databases/uniprot_tremble.dat or uniprot_sprot.dat

import sys
import os
from Bio import SeqIO
#import gzip

#This script uses biopython to get taxonomy from uniprot .dat file
'''
record.annotations         record.letter_annotations
record.dbxrefs             record.lower
record.description         record.name
record.features            record.reverse_complement
record.format              record.seq
record.id                  record.upper
'''

def get_taxa(uniprot_dat):
	taxa_to_print=[]
	for record in SeqIO.parse(uniprot_dat, "swiss"):
#		taxa_to_print.append(record.id)
		taxa_to_print.append(uniprot_dat.split(".")[0])
		taxa_to_print.append(record.annotations['organism'])
		for taxa in record.annotations['taxonomy']:
			taxa_to_print.append(taxa)
	return '\t'.join(taxa_to_print)

uniprot_dat = sys.argv[1:]

for f in uniprot_dat:
	taxa = get_taxa(f)
	print taxa
<|endoftext|>