<commit_before># -*- coding: utf-8 -*-
'''
daemontools service module. This module will create daemontools type
service watcher.

This module is compatible with the :mod:`service <salt.states.service>` states,
so it can be used to maintain services using the ``provider`` argument:

.. code-block:: yaml

    myservice:
      service.running:
        - provider: daemontools
'''
from __future__ import absolute_import

# Import python libs
import os
import os.path
import re

# Import salt libs
from salt.exceptions import CommandExecutionError

# Function alias to not shadow built-ins.
__func_alias__ = {
    'reload_': 'reload'
}

VALID_SERVICE_DIRS = [
    '/service',
    '/var/service',
    '/etc/service',
]
SERVICE_DIR = None
for service_dir in VALID_SERVICE_DIRS:
    if os.path.exists(service_dir):
        SERVICE_DIR = service_dir
        break

def _service_path(name):
    '''
    build service path
    '''
    if not SERVICE_DIR:
        raise CommandExecutionError("Could not find service directory.")
    return '{0}/{1}'.format(SERVICE_DIR, name)

#-- states.service  compatible args
def start(name):
    '''
    Starts service via daemontools

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.start <service name>
    '''
    __salt__['file.remove']('{0}/down'.format(_service_path(name)))
    cmd = 'svc -u {0}'.format(_service_path(name))
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

#-- states.service compatible args
def stop(name):
    '''
    Stops service via daemontools

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.stop <service name>
    '''
    __salt__['file.touch']('{0}/down'.format(_service_path(name)))
    cmd = 'svc -d {0}'.format(_service_path(name))
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

def term(name):
    '''
    Send a TERM to service via daemontools

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.term <service name>
    '''
    cmd = 'svc -t {0}'.format(_service_path(name))
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

#-- states.service compatible
def reload_(name):
    '''
    Wrapper for term()

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.reload <service name>
    '''
    term(name)

#-- states.service compatible
def restart(name):
    '''
    Restart service via daemontools. This will stop/start service

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.restart <service name>
    '''
    ret = 'restart False'
    if stop(name) and start(name):
        ret = 'restart True'
    return ret

#-- states.service compatible
def full_restart(name):
    '''
    Calls daemontools.restart() function

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.full_restart <service name>
    '''
    restart(name)

#-- states.service compatible
def status(name, sig=None):
    '''
    Return the status for a service via daemontools, return pid if running

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.status <service name>
    '''
    cmd = 'svstat {0}'.format(_service_path(name))
    out = __salt__['cmd.run_stdout'](cmd, python_shell=False)
    try:
        pid = re.search(r'\(pid (\d+)\)', out).group(1)
    except AttributeError:
        pid = ''
    return pid

def available(name):
    '''
    Returns ``True`` if the specified service is available, otherwise returns
    ``False``.

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.available foo
    '''
    return name in get_all()

def missing(name):
    '''
    The inverse of daemontools.available.
    Returns ``True`` if the specified service is not available, otherwise returns
    ``False``.

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.missing foo
    '''
    return name not in get_all()

def get_all():
    '''
    Return a list of all available services

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.get_all
    '''
    if not SERVICE_DIR:
        raise CommandExecutionError("Could not find service directory.")
    #- List all daemontools services in
    return sorted(os.listdir(SERVICE_DIR))

def enabled(name, **kwargs):
    '''
    Return True if the named service is enabled, false otherwise
    A service is considered enabled if in your service directory:
    - an executable ./run file exist
    - a file named "down" does not exist

    name
        Service name

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.enabled <service name>
    '''
    if not available(name):
        log.error('Service {0} not found'.format(name))
        return False

    run_file = os.path.join(SERVICE_DIR, name, 'run')
    down_file = os.path.join(SERVICE_DIR, name, 'down')

    return (
        os.path.isfile(run_file)
        and os.access(run_file, os.X_OK)
        and not os.path.isfile(down_file)
    )

def disabled(name):
    '''
    Return True if the named service is enabled, false otherwise

    CLI Example:

    .. code-block:: bash

        salt '*' service.daemontools <service name>
    '''
    return not enabled(name)

<commit_msg>log was missing<commit_after># -*- coding: utf-8 -*-
'''
daemontools service module. This module will create daemontools type
service watcher.

This module is compatible with the :mod:`service <salt.states.service>` states,
so it can be used to maintain services using the ``provider`` argument:

.. code-block:: yaml

    myservice:
      service.running:
        - provider: daemontools
'''
from __future__ import absolute_import

# Import python libs
import os
import os.path
import re

# Import salt libs
from salt.exceptions import CommandExecutionError

# Function alias to not shadow built-ins.
__func_alias__ = {
    'reload_': 'reload'
}

log = logging.getLogger(__name__)

VALID_SERVICE_DIRS = [
    '/service',
    '/var/service',
    '/etc/service',
]
SERVICE_DIR = None
for service_dir in VALID_SERVICE_DIRS:
    if os.path.exists(service_dir):
        SERVICE_DIR = service_dir
        break

def _service_path(name):
    '''
    build service path
    '''
    if not SERVICE_DIR:
        raise CommandExecutionError("Could not find service directory.")
    return '{0}/{1}'.format(SERVICE_DIR, name)

#-- states.service  compatible args
def start(name):
    '''
    Starts service via daemontools

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.start <service name>
    '''
    __salt__['file.remove']('{0}/down'.format(_service_path(name)))
    cmd = 'svc -u {0}'.format(_service_path(name))
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

#-- states.service compatible args
def stop(name):
    '''
    Stops service via daemontools

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.stop <service name>
    '''
    __salt__['file.touch']('{0}/down'.format(_service_path(name)))
    cmd = 'svc -d {0}'.format(_service_path(name))
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

def term(name):
    '''
    Send a TERM to service via daemontools

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.term <service name>
    '''
    cmd = 'svc -t {0}'.format(_service_path(name))
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

#-- states.service compatible
def reload_(name):
    '''
    Wrapper for term()

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.reload <service name>
    '''
    term(name)

#-- states.service compatible
def restart(name):
    '''
    Restart service via daemontools. This will stop/start service

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.restart <service name>
    '''
    ret = 'restart False'
    if stop(name) and start(name):
        ret = 'restart True'
    return ret

#-- states.service compatible
def full_restart(name):
    '''
    Calls daemontools.restart() function

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.full_restart <service name>
    '''
    restart(name)

#-- states.service compatible
def status(name, sig=None):
    '''
    Return the status for a service via daemontools, return pid if running

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.status <service name>
    '''
    cmd = 'svstat {0}'.format(_service_path(name))
    out = __salt__['cmd.run_stdout'](cmd, python_shell=False)
    try:
        pid = re.search(r'\(pid (\d+)\)', out).group(1)
    except AttributeError:
        pid = ''
    return pid

def available(name):
    '''
    Returns ``True`` if the specified service is available, otherwise returns
    ``False``.

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.available foo
    '''
    return name in get_all()

def missing(name):
    '''
    The inverse of daemontools.available.
    Returns ``True`` if the specified service is not available, otherwise returns
    ``False``.

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.missing foo
    '''
    return name not in get_all()

def get_all():
    '''
    Return a list of all available services

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.get_all
    '''
    if not SERVICE_DIR:
        raise CommandExecutionError("Could not find service directory.")
    #- List all daemontools services in
    return sorted(os.listdir(SERVICE_DIR))

def enabled(name, **kwargs):
    '''
    Return True if the named service is enabled, false otherwise
    A service is considered enabled if in your service directory:
    - an executable ./run file exist
    - a file named "down" does not exist

    name
        Service name

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.enabled <service name>
    '''
    if not available(name):
        log.error('Service {0} not found'.format(name))
        return False

    run_file = os.path.join(SERVICE_DIR, name, 'run')
    down_file = os.path.join(SERVICE_DIR, name, 'down')

    return (
        os.path.isfile(run_file)
        and os.access(run_file, os.X_OK)
        and not os.path.isfile(down_file)
    )

def disabled(name):
    '''
    Return True if the named service is enabled, false otherwise

    CLI Example:

    .. code-block:: bash

        salt '*' service.daemontools <service name>
    '''
    return not enabled(name)

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
'''
daemontools service module. This module will create daemontools type
service watcher.

This module is compatible with the :mod:`service <salt.states.service>` states,
so it can be used to maintain services using the ``provider`` argument:

.. code-block:: yaml

    myservice:
      service.running:
        - provider: daemontools
'''
from __future__ import absolute_import

# Import python libs
import logging
import os
import os.path
import re

# Import salt libs
from salt.exceptions import CommandExecutionError

# Function alias to not shadow built-ins.
__func_alias__ = {
    'reload_': 'reload'
}

log = logging.getLogger(__name__)

VALID_SERVICE_DIRS = [
    '/service',
    '/var/service',
    '/etc/service',
]
SERVICE_DIR = None
for service_dir in VALID_SERVICE_DIRS:
    if os.path.exists(service_dir):
        SERVICE_DIR = service_dir
        break

def _service_path(name):
    '''
    build service path
    '''
    if not SERVICE_DIR:
        raise CommandExecutionError("Could not find service directory.")
    return '{0}/{1}'.format(SERVICE_DIR, name)

#-- states.service  compatible args
def start(name):
    '''
    Starts service via daemontools

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.start <service name>
    '''
    __salt__['file.remove']('{0}/down'.format(_service_path(name)))
    cmd = 'svc -u {0}'.format(_service_path(name))
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

#-- states.service compatible args
def stop(name):
    '''
    Stops service via daemontools

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.stop <service name>
    '''
    __salt__['file.touch']('{0}/down'.format(_service_path(name)))
    cmd = 'svc -d {0}'.format(_service_path(name))
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

def term(name):
    '''
    Send a TERM to service via daemontools

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.term <service name>
    '''
    cmd = 'svc -t {0}'.format(_service_path(name))
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

#-- states.service compatible
def reload_(name):
    '''
    Wrapper for term()

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.reload <service name>
    '''
    term(name)

#-- states.service compatible
def restart(name):
    '''
    Restart service via daemontools. This will stop/start service

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.restart <service name>
    '''
    ret = 'restart False'
    if stop(name) and start(name):
        ret = 'restart True'
    return ret

#-- states.service compatible
def full_restart(name):
    '''
    Calls daemontools.restart() function

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.full_restart <service name>
    '''
    restart(name)

#-- states.service compatible
def status(name, sig=None):
    '''
    Return the status for a service via daemontools, return pid if running

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.status <service name>
    '''
    cmd = 'svstat {0}'.format(_service_path(name))
    out = __salt__['cmd.run_stdout'](cmd, python_shell=False)
    try:
        pid = re.search(r'\(pid (\d+)\)', out).group(1)
    except AttributeError:
        pid = ''
    return pid

def available(name):
    '''
    Returns ``True`` if the specified service is available, otherwise returns
    ``False``.

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.available foo
    '''
    return name in get_all()

def missing(name):
    '''
    The inverse of daemontools.available.
    Returns ``True`` if the specified service is not available, otherwise returns
    ``False``.

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.missing foo
    '''
    return name not in get_all()

def get_all():
    '''
    Return a list of all available services

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.get_all
    '''
    if not SERVICE_DIR:
        raise CommandExecutionError("Could not find service directory.")
    #- List all daemontools services in
    return sorted(os.listdir(SERVICE_DIR))

def enabled(name, **kwargs):
    '''
    Return True if the named service is enabled, false otherwise
    A service is considered enabled if in your service directory:
    - an executable ./run file exist
    - a file named "down" does not exist

    name
        Service name

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.enabled <service name>
    '''
    if not available(name):
        log.error('Service {0} not found'.format(name))
        return False

    run_file = os.path.join(SERVICE_DIR, name, 'run')
    down_file = os.path.join(SERVICE_DIR, name, 'down')

    return (
        os.path.isfile(run_file) and
        os.access(run_file, os.X_OK) and not
        os.path.isfile(down_file)
    )

def disabled(name):
    '''
    Return True if the named service is enabled, false otherwise

    CLI Example:

    .. code-block:: bash

        salt '*' service.daemontools <service name>
    '''
    return not enabled(name)

<commit_msg>fix pylint issue<commit_after># -*- coding: utf-8 -*-
'''
daemontools service module. This module will create daemontools type
service watcher.

This module is compatible with the :mod:`service <salt.states.service>` states,
so it can be used to maintain services using the ``provider`` argument:

.. code-block:: yaml

    myservice:
      service.running:
        - provider: daemontools
'''
from __future__ import absolute_import

# Import python libs
import logging
import os
import os.path
import re

# Import salt libs
from salt.exceptions import CommandExecutionError

# Function alias to not shadow built-ins.
__func_alias__ = {
    'reload_': 'reload'
}

log = logging.getLogger(__name__)

VALID_SERVICE_DIRS = [
    '/service',
    '/var/service',
    '/etc/service',
]
SERVICE_DIR = None
for service_dir in VALID_SERVICE_DIRS:
    if os.path.exists(service_dir):
        SERVICE_DIR = service_dir
        break

def _service_path(name):
    '''
    build service path
    '''
    if not SERVICE_DIR:
        raise CommandExecutionError("Could not find service directory.")
    return '{0}/{1}'.format(SERVICE_DIR, name)

#-- states.service  compatible args
def start(name):
    '''
    Starts service via daemontools

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.start <service name>
    '''
    __salt__['file.remove']('{0}/down'.format(_service_path(name)))
    cmd = 'svc -u {0}'.format(_service_path(name))
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

#-- states.service compatible args
def stop(name):
    '''
    Stops service via daemontools

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.stop <service name>
    '''
    __salt__['file.touch']('{0}/down'.format(_service_path(name)))
    cmd = 'svc -d {0}'.format(_service_path(name))
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

def term(name):
    '''
    Send a TERM to service via daemontools

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.term <service name>
    '''
    cmd = 'svc -t {0}'.format(_service_path(name))
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

#-- states.service compatible
def reload_(name):
    '''
    Wrapper for term()

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.reload <service name>
    '''
    term(name)

#-- states.service compatible
def restart(name):
    '''
    Restart service via daemontools. This will stop/start service

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.restart <service name>
    '''
    ret = 'restart False'
    if stop(name) and start(name):
        ret = 'restart True'
    return ret

#-- states.service compatible
def full_restart(name):
    '''
    Calls daemontools.restart() function

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.full_restart <service name>
    '''
    restart(name)

#-- states.service compatible
def status(name, sig=None):
    '''
    Return the status for a service via daemontools, return pid if running

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.status <service name>
    '''
    cmd = 'svstat {0}'.format(_service_path(name))
    out = __salt__['cmd.run_stdout'](cmd, python_shell=False)
    try:
        pid = re.search(r'\(pid (\d+)\)', out).group(1)
    except AttributeError:
        pid = ''
    return pid

def available(name):
    '''
    Returns ``True`` if the specified service is available, otherwise returns
    ``False``.

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.available foo
    '''
    return name in get_all()

def missing(name):
    '''
    The inverse of daemontools.available.
    Returns ``True`` if the specified service is not available, otherwise returns
    ``False``.

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.missing foo
    '''
    return name not in get_all()

def get_all():
    '''
    Return a list of all available services

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.get_all
    '''
    if not SERVICE_DIR:
        raise CommandExecutionError("Could not find service directory.")
    #- List all daemontools services in
    return sorted(os.listdir(SERVICE_DIR))

def enabled(name, **kwargs):
    '''
    Return True if the named service is enabled, false otherwise
    A service is considered enabled if in your service directory:
    - an executable ./run file exist
    - a file named "down" does not exist

    name
        Service name

    CLI Example:

    .. code-block:: bash

        salt '*' daemontools.enabled <service name>
    '''
    if not available(name):
        log.error('Service {0} not found'.format(name))
        return False

    run_file = os.path.join(SERVICE_DIR, name, 'run')
    down_file = os.path.join(SERVICE_DIR, name, 'down')

    return (
        os.path.isfile(run_file) and
        os.access(run_file, os.X_OK) and not
        os.path.isfile(down_file)
    )

def disabled(name):
    '''
    Return True if the named service is enabled, false otherwise

    CLI Example:

    .. code-block:: bash

        salt '*' service.daemontools <service name>
    '''
    return not enabled(name)
<|endoftext|>


<commit_before>from pyramid.i18n import TranslationStringFactory
_ = TranslationStringFactory('alexandria')

import colander

from ..models import Domain

class DomainSchema(colander.Schema):
    """The schema for a domain"""

    @classmethod
    def create_schema(cls, request):
        return cls().bind(request=request)

    id = colander.SchemaNode(colander.String(), missing=colander.drop)
    owner_id = colander.SchemaNode(colander.String(), missing=colander.drop)
    domain = colander.SchemaNode(colander.String(), validator=colander.Length(max=256))
    primary_ns = colander.SchemaNode(colander.String(), validator=colander.Length(max=256))
    hostmaster = colander.SchemaNode(colander.String(), validator=colander.Length(max=256))
    serial = colander.SchemaNode(colander.Integer(), missing=colander.drop)
    refresh = colander.SchemaNode(colander.Integer(), missing=colander.drop)
    retry = colander.SchemaNode(colander.Integer(), missing=colander.drop)
    expiration = colander.SchemaNode(colander.Integer(), missing=colander.drop)
    min_ttl = colander.SchemaNode(colander.Integer(), missing=colander.drop)
    created = colander.SchemaNode(colander.DateTime(), missing=colander.drop)
    updated = colander.SchemaNode(colander.DateTime(), missing=colander.drop)

class DomainsSchema(colander.SequenceSchema):
    """The domains schema form"""

    domains = DomainSchema()
<commit_msg>Add full schema validation<commit_after>from pyramid.i18n import TranslationStringFactory
_ = TranslationStringFactory('alexandria')

import colander

from .. import models as m

@colander.deferred
def _new_domain(node, kw):
    request = kw.get('request')

    if request is None:
        raise KeyError('Require bind: request')

    def new_domain(form, value):
        d = m.DBSession.query(m.Domain).filter(m.Domain.domain == value['domain']).first()

        if d is not None:
            exc = colander.Invalid(form)
            exc['domain'] = _("Domain already exists")

            if 'id' in value:
                if d.id != value['id']:
                    raise exc
            else:
                raise exc

    return new_domain

class DomainSchema(colander.Schema):
    """The schema for a domain"""

    @classmethod
    def create_schema(cls, request):
        return cls(validator=_new_domain).bind(request=request)

    id = colander.SchemaNode(colander.String(), missing=colander.drop)
    owner_id = colander.SchemaNode(colander.String(), missing=colander.drop)
    domain = colander.SchemaNode(colander.String(), validator=colander.Length(max=256))
    primary_ns = colander.SchemaNode(colander.String(), validator=colander.Length(max=256))
    hostmaster = colander.SchemaNode(colander.String(), validator=colander.Length(max=256))
    serial = colander.SchemaNode(colander.Integer(), missing=colander.drop)
    refresh = colander.SchemaNode(colander.Integer(), missing=colander.drop)
    retry = colander.SchemaNode(colander.Integer(), missing=colander.drop)
    expiration = colander.SchemaNode(colander.Integer(), missing=colander.drop)
    min_ttl = colander.SchemaNode(colander.Integer(), missing=colander.drop)
    created = colander.SchemaNode(colander.DateTime(), missing=colander.drop)
    updated = colander.SchemaNode(colander.DateTime(), missing=colander.drop)

class DomainsSchema(colander.SequenceSchema):
    """The domains schema form"""

    domains = DomainSchema()
<|endoftext|>


<commit_before>import numpy as np

from data import *

# Import APOGEE/sample packages
from apogee.tools import bitmask
import window as wn
from read_clusterdata import read_caldata
import apogee.spec.plot as aplt

# Import fitting and analysis packages
import access_spectrum
reload(access_spectrum)
import access_spectrum as acs
import reduce_dataset as rd
import polyfit as pf

# Example star filter function
def starFilter(data):
    """
    Returns True where stellar properties match conditions

    """
    return (data['TEFF'] > 4500) & (data['TEFF'] < 4800)

def basicIronFilter(data):
    return (data['FE_H'] > -0.2) & (data['FE_H'] < -0.1)

def bitsNotSet(bitmask,maskbits):
    goodLocs_bool = np.ones(bitmask.shape).astype(bool)
    for m in maskbits:
        bitind = bitmask.bit_set(m,bitmask)
        goodLocs_bool[bitind] = False
    return goodLocs_bool

# Example mask filter function
def maskFilter(sample):
    """
    Returns True where sample properties match conditions

    """
    maskbits = bitmask.bits_set(badcombpixmask)
    return (sample._SNR > 50.) & (sample._SNR < 200.) & bitsNotSet(sample._bitmasks,maskbits)

class starInfo(object):
    """
    Each instance of starInfo holds information about a given star
    """
    def __init__(self,data):
        """
        For a given data index, assign stellar properties.

        """
        self.LOC = data['LOCATION_ID']
        self.APO = data['APOGEE_ID']
        self.TEFF = data['TEFF']
        self.LOGG = data['LOGG']
        self.FE_H = data['FE_H']
        self.TEFF_ERR = data['TEFF_ERR']
        self.LOGG_ERR = data['LOGG_ERR']
        self.FE_H_ERR = data['FE_H_ERR']

    def getData(self):
        """
        Create arrays of data for this star.

        """
        # Independent variables
        self._teff = np.ma.masked_array([self.TEFF]*aspcappix)
        self._logg = np.ma.masked_array([self.LOGG]*aspcappix)
        self._fe_h = np.ma.masked_array([self.FE_H]*aspcappix)

        # Independent variable uncertainty
        self._teff_err = np.ma.masked_array([self.TEFF_ERR]*aspcappix)
        self._logg_err = np.ma.masked_array([self.LOGG_ERR]*aspcappix)
        self._fe_h_err = np.ma.masked_array([self.FE_H_ERR]*aspcappix)

        # Spectral data
        self.spectrum = apread.aspcapStar(self.LOC,self.APO,ext=1,header=False,
                                          aspcapWavegrid=True)
        self.spectrum_err = apread.aspcapStar(self.LOC,self.APO,ext=2,header=False,
                                              aspcapWavegrid=True)
        self._bitmask = apread.apStar(self.LOC,self.APO,
                                     ext=3, header=False, aspcapWavegrid=True)[1]

class starSample(object):
    def __init__(self,sampleType):
        """
        Get properties for all stars that match the sample type
        """
        self._sampleType = sampleType
        self._getProperties()

    def _getProperties(self):
        """
        Get properties of all possible stars to be used.
        """
        self.data = readfn[self._sampleType]()

    def getStars(self,data):
        """
        Create a starInfo object for each star in data.

        """
        self.allStars = []
        self.numberStars = len(data)
        for star in range(len(data)):
            newStar = starInfo(data[star])
            newStar.getData()
            self.allStars.append(newStar)

    def makeArrays(self,data=None):
        """
        Create arrays across all stars in the sample with shape aspcappix by number of stars.
        If data set is given make stars for that set first.

        """

        if data:
            self.getStars(self,data)

        self.teff = np.ma.masked_array([self.allStars[i]._teff
                                        for i in range(len(self.allStars))])
        self.logg = np.ma.masked_array([self.allStars[i]._logg
                                        for i in range(len(self.allStars))])
        self.fe_h = np.ma.masked_array([self.allStars[i]._fe_h
                                        for i in range(len(self.allStars))])

        self.teff_err = np.ma.masked_array([self.allStars[i]._teff_err
                                        for i in range(len(self.allStars))])
        self.logg_err = np.ma.masked_array([self.allStars[i]._logg_err
                                        for i in range(len(self.allStars))])
        self.fe_h_err = np.ma.masked_array([self.allStars[i]._fe_h_err
                                        for i in range(len(self.allStars))])

        self.spectra = np.ma.masked_array([self.allStars[i].spectrum
                                        for i in range(len(self.allStars))])
        self.spectra_errs = np.ma.masked_array([self.allStars[i].spectrum_err
                                        for i in range(len(self.allStars))])
        self._bitmasks = np.ma.masked_array([self.allStars[i]._bitmask
                                        for i in range(len(self.allStars))])

class subStarSample(starSample):
    def __init__(self,sampleType,starFilter):
        """
        Create a subsample according to a starFilter function
        starSample call:

        subStarSample(red_clump,lambda data: data['TEFF'] > 4500)
        subStarSample(red_clump,starFilter)

        """
        starSample.__init__(self,sampleType)
        self._matchingStars = starFilter(self.data)
        self.matchingData = self.data[self._matchingStars]
        self.getStars(self.matchingData)
        self.makeArrays()

class mask(subStarSample):
    """
    Define a mask given a conditions in the form of maskConditions
    """
    def __init__(self,sampleType,starFilter,maskConditions):
        subStarSample.__init__(self,sampleType,starFilter)
        self._SNR = self.spectra/self.spectra_errs
        self.masked = np.zeros(self.spectra.shape)
        self.unmasked = np.ones(self.spectra.shape)
        self._maskHere = maskConditions(self)
        self.masked[_maskHere]= 1
        self.unmasked[_maskHere] = 0
        self.applyMask()

    def applyMask(self):
        """
        Mask all arrays according to maskConditions

        """
        self.teff.mask = self.masked
        self.logg.mask = self.masked
        self.fe_h.mask = self.masked

        self.teff_err.mask = self.masked
        self.logg_err.mask = self.masked
        self.fe_h_err.mask = self.masked

        self.spectra.mask = self.masked
        self.spectra_errs.mask = self.masked

#class fit(mask)

#class residuals(fit)

#class uncertainty(fit)
<commit_msg>Moving for editing<commit_after>import numpy as np

from data import *

# Import APOGEE/sample packages
from apogee.tools import bitmask
import window as wn
from read_clusterdata import read_caldata
import apogee.spec.plot as aplt

# Import fitting and analysis packages
import access_spectrum
reload(access_spectrum)
import access_spectrum as acs
import reduce_dataset as rd
import polyfit as pf

# Example star filter function
def starFilter(data):
    """
    Returns True where stellar properties match conditions

    """
    return (data['TEFF'] > 4500) & (data['TEFF'] < 4800)

def basicIronFilter(data):
    return (data['FE_H'] > -0.15) & (data['FE_H'] < -0.1)

def bitsNotSet(bitmask,maskbits):
    goodLocs_bool = np.ones(bitmask.shape).astype(bool)
    for m in maskbits:
        bitind = bitmask.bit_set(m,bitmask)
        goodLocs_bool[bitind] = False
    return goodLocs_bool

# Example mask filter function
def maskFilter(sample):
    """
    Returns True where sample properties match conditions

    """
    maskbits = bitmask.bits_set(badcombpixmask)
    return (sample._SNR > 50.) & (sample._SNR < 200.) & bitsNotSet(sample._bitmasks,maskbits)

class starInfo(object):
    """
    Each instance of starInfo holds information about a given star
    """
    def __init__(self,data):
        """
        For a given data index, assign stellar properties.

        """
        self.LOC = data['LOCATION_ID']
        self.APO = data['APOGEE_ID']
        self.TEFF = data['TEFF']
        self.LOGG = data['LOGG']
        self.FE_H = data['FE_H']
        self.TEFF_ERR = data['TEFF_ERR']
        self.LOGG_ERR = data['LOGG_ERR']
        self.FE_H_ERR = data['FE_H_ERR']

    def getData(self):
        """
        Create arrays of data for this star.

        """
        # Independent variables
        self._teff = np.ma.masked_array([self.TEFF]*aspcappix)
        self._logg = np.ma.masked_array([self.LOGG]*aspcappix)
        self._fe_h = np.ma.masked_array([self.FE_H]*aspcappix)

        # Independent variable uncertainty
        self._teff_err = np.ma.masked_array([self.TEFF_ERR]*aspcappix)
        self._logg_err = np.ma.masked_array([self.LOGG_ERR]*aspcappix)
        self._fe_h_err = np.ma.masked_array([self.FE_H_ERR]*aspcappix)

        # Spectral data
        self.spectrum = apread.aspcapStar(self.LOC,self.APO,ext=1,header=False,
                                          aspcapWavegrid=True)
        self.spectrum_err = apread.aspcapStar(self.LOC,self.APO,ext=2,header=False,
                                              aspcapWavegrid=True)
        self._bitmask = apread.apStar(self.LOC,self.APO,
                                     ext=3, header=False, aspcapWavegrid=True)[1]

class starSample(object):
    def __init__(self,sampleType):
        """
        Get properties for all stars that match the sample type
        """
        self._sampleType = sampleType
        self._getProperties()

    def _getProperties(self):
        """
        Get properties of all possible stars to be used.
        """
        self.data = readfn[self._sampleType]()

    def getStars(self,data):
        """
        Create a starInfo object for each star in data.

        """
        self.allStars = []
        self.numberStars = len(data)
        for star in range(len(data)):
            newStar = starInfo(data[star])
            newStar.getData()
            self.allStars.append(newStar)

    def makeArrays(self,data=None):
        """
        Create arrays across all stars in the sample with shape aspcappix by number of stars.
        If data set is given make stars for that set first.

        """

        self.teff = np.ma.masked_array([self.allStars[i]._teff
                                        for i in range(len(self.allStars))])
        self.logg = np.ma.masked_array([self.allStars[i]._logg
                                        for i in range(len(self.allStars))])
        self.fe_h = np.ma.masked_array([self.allStars[i]._fe_h
                                        for i in range(len(self.allStars))])

        self.teff_err = np.ma.masked_array([self.allStars[i]._teff_err
                                        for i in range(len(self.allStars))])
        self.logg_err = np.ma.masked_array([self.allStars[i]._logg_err
                                        for i in range(len(self.allStars))])
        self.fe_h_err = np.ma.masked_array([self.allStars[i]._fe_h_err
                                        for i in range(len(self.allStars))])

        self.spectra = np.ma.masked_array([self.allStars[i].spectrum
                                        for i in range(len(self.allStars))])
        self.spectra_errs = np.ma.masked_array([self.allStars[i].spectrum_err
                                        for i in range(len(self.allStars))])
        self._bitmasks = np.ma.masked_array([self.allStars[i]._bitmask
                                        for i in range(len(self.allStars))])

        if data:

class subStarSample(starSample):
    def __init__(self,sampleType,starFilter):
        """
        Create a subsample according to a starFilter function
        starSample call:

        subStarSample(red_clump,lambda data: data['TEFF'] > 4500)
        subStarSample(red_clump,starFilter)

        """
        starSample.__init__(self,sampleType)
        self._matchingStars = starFilter(self.data)
        self.matchingData = self.data[self._matchingStars]
        print 'Found matching stars'
        print 'Number of stars: ',len(self.matchingData)
        self.getStars(self.matchingData)
        print 'Got stars'
        self.makeArrays()

class mask(subStarSample):
    """
    Define a mask given a conditions in the form of maskConditions
    """
    def __init__(self,sampleType,starFilter,maskConditions):
        subStarSample.__init__(self,sampleType,starFilter)
        self._SNR = self.spectra/self.spectra_errs
        self.masked = np.zeros(self.spectra.shape)
        self.unmasked = np.ones(self.spectra.shape)
        self._maskHere = maskConditions(self)
        self.masked[_maskHere]= 1
        self.unmasked[_maskHere] = 0
        self.applyMask()

    def applyMask(self):
        """
        Mask all arrays according to maskConditions

        """
        self.teff.mask = self.masked
        self.logg.mask = self.masked
        self.fe_h.mask = self.masked

        self.teff_err.mask = self.masked
        self.logg_err.mask = self.masked
        self.fe_h_err.mask = self.masked

        self.spectra.mask = self.masked
        self.spectra_errs.mask = self.masked

#class fit(mask)

#class residuals(fit)

#class uncertainty(fit)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
'''
Windows Service module.
'''

# Import python libs
import time
import salt.utils

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

def __virtual__():
    '''
    Only works on Windows systems
    '''
    if salt.utils.is_windows():
        return __virtualname__
    return False

def get_enabled():
    '''
    Return the enabled services

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_enabled
    '''
    ret = set()
    services = []
    cmd = 'sc query type= service state= all'
    lines = __salt__['cmd.run'](cmd).splitlines()
    for line in lines:
        if 'SERVICE_NAME:' in line:
            comps = line.split(':', 1)
            if not len(comps) > 1:
                continue
            services.append(comps[1].strip())
    for service in services:
        cmd2 = 'sc qc "{0}"'.format(service)
        lines = __salt__['cmd.run'](cmd2).splitlines()
        for line in lines:
            if 'AUTO_START' in line:
                ret.add(service)
    return sorted(ret)

def get_disabled():
    '''
    Return the disabled services

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_disabled
    '''
    ret = set()
    services = []
    cmd = 'sc query type= service state= all'
    lines = __salt__['cmd.run'](cmd).splitlines()
    for line in lines:
        if 'SERVICE_NAME:' in line:
            comps = line.split(':', 1)
            if not len(comps) > 1:
                continue
            services.append(comps[1].strip())
    for service in services:
        cmd2 = 'sc qc "{0}"'.format(service)
        lines = __salt__['cmd.run'](cmd2).splitlines()
        for line in lines:
            if 'DEMAND_START' in line:
                ret.add(service)
            elif 'DISABLED' in line:
                ret.add(service)
    return sorted(ret)

def available(name):
    '''
    Returns ``True`` if the specified service is available, otherwise returns
    ``False``.

    CLI Example:

    .. code-block:: bash

        salt '*' service.available <service name>
    '''
    return name in get_all()

def get_all():
    '''
    Return all installed services

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_all
    '''
    return sorted(get_enabled() + get_disabled())

def get_service_name(*args):
    '''
    The Display Name is what is displayed in Windows when services.msc is
    executed.  Each Display Name has an associated Service Name which is the
    actual name of the service.  This function allows you to discover the
    Service Name by returning a dictionary of Display Names and Service Names,
    or filter by adding arguments of Display Names.

    If no args are passed, return a dict of all services where the keys are the
    service Display Names and the values are the Service Names.

    If arguments are passed, create a dict of Display Names and Service Names

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_service_name
        salt '*' service.get_service_name 'Google Update Service (gupdate)' 'DHCP Client'
    '''
    ret = {}
    services = []
    display_names = []
    cmd = 'sc query type= service state= all'
    lines = __salt__['cmd.run'](cmd).splitlines()
    for line in lines:
        if 'SERVICE_NAME:' in line:
            comps = line.split(':', 1)
            if not len(comps) > 1:
                continue
            services.append(comps[1].strip())
        if 'DISPLAY_NAME:' in line:
            comps = line.split(':', 1)
            if not len(comps) > 1:
                continue
            display_names.append(comps[1].strip())
    if len(services) == len(display_names):
        service_dict = dict(zip(display_names, services))
    else:
        return 'Service Names and Display Names mismatch'
    if len(args) == 0:
        return service_dict
    for arg in args:
        if arg in service_dict:
            ret[arg] = service_dict[arg]
    return ret

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

    CLI Example:

    .. code-block:: bash

        salt '*' service.start <service name>
    '''
    cmd = 'sc start "{0}"'.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 = 'sc stop "{0}"'.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>
    '''
    stop(name)
    for idx in xrange(5):
        if status(name):
            time.sleep(2)
            continue
        return start(name)
    return False

def status(name, sig=None):
    '''
    Return the status for a service, returns the PID or an empty string if the
    service is running or not, pass a signature to use to find the service via
    ps

    CLI Example:

    .. code-block:: bash

        salt '*' service.status <service name> [service signature]
    '''
    cmd = 'sc query "{0}"'.format(name)
    statuses = __salt__['cmd.run'](cmd).splitlines()
    for line in statuses:
        if 'RUNNING' in line:
            return True
        elif 'STOP_PENDING' in line:
            return True
    return False

def getsid(name):
    '''
    Return the sid for this windows service

    CLI Example:

    .. code-block:: bash

        salt '*' service.getsid <service name>
    '''
    cmd = 'sc showsid "{0}"'.format(name)
    lines = __salt__['cmd.run'](cmd).splitlines()
    for line in lines:
        if 'SERVICE SID:' in line:
            comps = line.split(':', 1)
            if comps[1] > 1:
                return comps[1].strip()
            else:
                return None

def enable(name, **kwargs):
    '''
    Enable the named service to start at boot

    CLI Example:

    .. code-block:: bash

        salt '*' service.enable <service name>
    '''
    cmd = 'sc config "{0}" start= auto'.format(name)
    return not __salt__['cmd.retcode'](cmd)

def disable(name, **kwargs):
    '''
    Disable the named service to start at boot

    CLI Example:

    .. code-block:: bash

        salt '*' service.disable <service name>
    '''
    cmd = 'sc config "{0}" start= demand'.format(name)
    return not __salt__['cmd.retcode'](cmd)

def enabled(name):
    '''
    Check to see if the named service is enabled to start on boot

    CLI Example:

    .. code-block:: bash

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

def disabled(name):
    '''
    Check to see if the named service is disabled to start on boot

    CLI Example:

    .. code-block:: bash

        salt '*' service.disabled <service name>
    '''
    return name in get_disabled()
<commit_msg>Set buffer to 5000 for some Windows sc commands<commit_after># -*- coding: utf-8 -*-
'''
Windows Service module.
'''

# Import python libs
import time
import salt.utils

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

BUFFSIZE = 5000

def __virtual__():
    '''
    Only works on Windows systems
    '''
    if salt.utils.is_windows():
        return __virtualname__
    return False

def get_enabled():
    '''
    Return the enabled services

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_enabled
    '''
    ret = set()
    services = []
    cmd = 'sc query type= service state= all bufsize= {0}'.format(BUFFSIZE)
    lines = __salt__['cmd.run'](cmd).splitlines()
    for line in lines:
        if 'SERVICE_NAME:' in line:
            comps = line.split(':', 1)
            if not len(comps) > 1:
                continue
            services.append(comps[1].strip())
    for service in services:
        cmd2 = 'sc qc "{0} {1}"'.format(service, BUFFSIZE)
        lines = __salt__['cmd.run'](cmd2).splitlines()
        for line in lines:
            if 'AUTO_START' in line:
                ret.add(service)
    return sorted(ret)

def get_disabled():
    '''
    Return the disabled services

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_disabled
    '''
    ret = set()
    services = []
    cmd = 'sc query type= service state= all bufsize= {0}'.format(BUFFSIZE)
    lines = __salt__['cmd.run'](cmd).splitlines()
    for line in lines:
        if 'SERVICE_NAME:' in line:
            comps = line.split(':', 1)
            if not len(comps) > 1:
                continue
            services.append(comps[1].strip())
    for service in services:
        cmd2 = 'sc qc "{0} {1}"'.format(service, BUFFSIZE)
        lines = __salt__['cmd.run'](cmd2).splitlines()
        for line in lines:
            if 'DEMAND_START' in line:
                ret.add(service)
            elif 'DISABLED' in line:
                ret.add(service)
    return sorted(ret)

def available(name):
    '''
    Returns ``True`` if the specified service is available, otherwise returns
    ``False``.

    CLI Example:

    .. code-block:: bash

        salt '*' service.available <service name>
    '''
    return name in get_all()

def get_all():
    '''
    Return all installed services

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_all
    '''
    return sorted(get_enabled() + get_disabled())

def get_service_name(*args):
    '''
    The Display Name is what is displayed in Windows when services.msc is
    executed.  Each Display Name has an associated Service Name which is the
    actual name of the service.  This function allows you to discover the
    Service Name by returning a dictionary of Display Names and Service Names,
    or filter by adding arguments of Display Names.

    If no args are passed, return a dict of all services where the keys are the
    service Display Names and the values are the Service Names.

    If arguments are passed, create a dict of Display Names and Service Names

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_service_name
        salt '*' service.get_service_name 'Google Update Service (gupdate)' 'DHCP Client'
    '''
    ret = {}
    services = []
    display_names = []
    cmd = 'sc query type= service state= all bufsize= {0}'.format(BUFFSIZE)
    lines = __salt__['cmd.run'](cmd).splitlines()
    for line in lines:
        if 'SERVICE_NAME:' in line:
            comps = line.split(':', 1)
            if not len(comps) > 1:
                continue
            services.append(comps[1].strip())
        if 'DISPLAY_NAME:' in line:
            comps = line.split(':', 1)
            if not len(comps) > 1:
                continue
            display_names.append(comps[1].strip())
    if len(services) == len(display_names):
        service_dict = dict(zip(display_names, services))
    else:
        return 'Service Names and Display Names mismatch'
    if len(args) == 0:
        return service_dict
    for arg in args:
        if arg in service_dict:
            ret[arg] = service_dict[arg]
    return ret

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

    CLI Example:

    .. code-block:: bash

        salt '*' service.start <service name>
    '''
    cmd = 'sc start "{0}"'.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 = 'sc stop "{0}"'.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>
    '''
    stop(name)
    for idx in xrange(5):
        if status(name):
            time.sleep(2)
            continue
        return start(name)
    return False

def status(name, sig=None):
    '''
    Return the status for a service, returns the PID or an empty string if the
    service is running or not, pass a signature to use to find the service via
    ps

    CLI Example:

    .. code-block:: bash

        salt '*' service.status <service name> [service signature]
    '''
    cmd = 'sc query "{0} {1}"'.format(name, BUFFSIZE)
    statuses = __salt__['cmd.run'](cmd).splitlines()
    for line in statuses:
        if 'RUNNING' in line:
            return True
        elif 'STOP_PENDING' in line:
            return True
    return False

def getsid(name):
    '''
    Return the sid for this windows service

    CLI Example:

    .. code-block:: bash

        salt '*' service.getsid <service name>
    '''
    cmd = 'sc showsid "{0}"'.format(name)
    lines = __salt__['cmd.run'](cmd).splitlines()
    for line in lines:
        if 'SERVICE SID:' in line:
            comps = line.split(':', 1)
            if comps[1] > 1:
                return comps[1].strip()
            else:
                return None

def enable(name, **kwargs):
    '''
    Enable the named service to start at boot

    CLI Example:

    .. code-block:: bash

        salt '*' service.enable <service name>
    '''
    cmd = 'sc config "{0}" start= auto'.format(name)
    return not __salt__['cmd.retcode'](cmd)

def disable(name, **kwargs):
    '''
    Disable the named service to start at boot

    CLI Example:

    .. code-block:: bash

        salt '*' service.disable <service name>
    '''
    cmd = 'sc config "{0}" start= demand'.format(name)
    return not __salt__['cmd.retcode'](cmd)

def enabled(name):
    '''
    Check to see if the named service is enabled to start on boot

    CLI Example:

    .. code-block:: bash

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

def disabled(name):
    '''
    Check to see if the named service is disabled to start on boot

    CLI Example:

    .. code-block:: bash

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


<commit_before># -*- coding: utf-8 -*-
'''
Windows Service module.
'''

# Import python libs
from __future__ import absolute_import
import salt.utils
import time
import logging
from subprocess import list2cmdline
from salt.ext.six.moves import zip
from salt.ext.six.moves import range
try:
    from shlex import quote as _cmd_quote  # pylint: disable=E0611
except ImportError:
    from pipes import quote as _cmd_quote

log = logging.getLogger(__name__)

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

BUFFSIZE = 5000
SERVICE_STOP_DELAY_SECONDS = 15
SERVICE_STOP_POLL_MAX_ATTEMPTS = 5

def __virtual__():
    '''
    Only works on Windows systems
    '''
    if salt.utils.is_windows():
        return __virtualname__
    return False

def has_powershell():
    '''
    Confirm if Powershell is available

    CLI Example:

    .. code-block:: bash

        salt '*' service.has_powershell
    '''
    return 'powershell' in __salt__['cmd.run'](
            ['where', 'powershell'], python_shell=False
        )

def get_enabled():
    '''
    Return the enabled services

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_enabled
    '''
    return sorted([service for service in get_all() if enabled(service)])

def get_disabled():
    '''
    Return the disabled services

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_disabled
    '''
    return sorted([service for service in get_all() if disabled(service)])

def available(name):
    '''
    Returns ``True`` if the specified service is available, otherwise returns
    ``False``.

    CLI Example:

    .. code-block:: bash

        salt '*' service.available <service name>
    '''
    return name in get_all()

def missing(name):
    '''
    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 <service name>
    '''
    return name not in get_all()

def get_all():
    '''
    Return all installed services

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_all
    '''
    ret = set()
    cmd = list2cmdline(['sc', 'query', 'type=', 'service', 'state=', 'all', 'bufsize=', str(BUFFSIZE)])
    lines = __salt__['cmd.run'](cmd).splitlines()
    for line in lines:
        if 'SERVICE_NAME:' in line:
            comps = line.split(':', 1)
            if not len(comps) > 1:
                continue
            ret.add(comps[1].strip())
    return sorted(ret)

def get_service_name(*args):
    '''
    The Display Name is what is displayed in Windows when services.msc is
    executed.  Each Display Name has an associated Service Name which is the
    actual name of the service.  This function allows you to discover the
    Service Name by returning a dictionary of Display Names and Service Names,
    or filter by adding arguments of Display Names.

    If no args are passed, return a dict of all services where the keys are the
    service Display Names and the values are the Service Names.

    If arguments are passed, create a dict of Display Names and Service Names

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_service_name
        salt '*' service.get_service_name 'Google Update Service (gupdate)' 'DHCP Client'
    '''
    ret = {}
    services = []
    display_names = []
    cmd = ['sc', 'query', 'type=', 'service', 'state=', 'all', 'bufsize=', str(BUFFSIZE)]
    lines = __salt__['cmd.run'](cmd, python_shell=False).splitlines()
    for line in lines:
        if 'SERVICE_NAME:' in line:
            comps = line.split(':', 1)
            if not len(comps) > 1:
                continue
            services.append(comps[1].strip())
        if 'DISPLAY_NAME:' in line:
            comps = line.split(':', 1)
            if not len(comps) > 1:
                continue
            display_names.append(comps[1].strip())
    if len(services) == len(display_names):
        service_dict = dict(zip(display_names, services))
    else:
        return 'Service Names and Display Names mismatch'
    if len(args) == 0:
        return service_dict
    for arg in args:
        if arg in service_dict:
            ret[arg] = service_dict[arg]
    return ret

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

    CLI Example:

    .. code-block:: bash

        salt '*' service.start <service name>
    '''
    cmd = ['net', 'start', name]
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

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

    CLI Example:

    .. code-block:: bash

        salt '*' service.stop <service name>
    '''
    # net stop issues a stop command and waits briefly (~30s), but will give
    # up if the service takes too long to stop with a misleading
    # "service could not be stopped" message and RC 0.

    cmd = ['net', 'stop', name]
    res = __salt__['cmd.run'](cmd, python_shell=False)
    if 'service was stopped' in res:
        return True

    # we requested a stop, but the service is still thinking about it.
    # poll for the real status
    for attempt in range(SERVICE_STOP_POLL_MAX_ATTEMPTS):
        if not status(name):
            return True
        log.debug('Waiting for %s to stop', name)
        time.sleep(SERVICE_STOP_DELAY_SECONDS)

    log.warning('Giving up on waiting for service `%s` to stop', name)
    return False

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

    CLI Example:

    .. code-block:: bash

        salt '*' service.restart <service name>
    '''
    if has_powershell():
        if 'salt-minion' in name:
            create_win_salt_restart_task()
            return execute_salt_restart_task()
        cmd = 'Restart-Service {0}'.format(_cmd_quote(name))
        return not __salt__['cmd.retcode'](cmd, shell='powershell', python_shell=True)
    return stop(name) and start(name)

def create_win_salt_restart_task():
    '''

    Create a task in Windows task scheduler to enable restarting the salt-minion

    CLI Example:

    .. code-block:: bash

        salt '*' service.create_win_salt_restart_task()
    '''
    cmd = 'schtasks /RU "System" /Create /TN restart-salt-minion /TR "powershell Restart-Service salt-minion" /sc ONCE /sd 01/01/1975 /st 01:00 /F'

    return __salt__['cmd.shell'](cmd)

def execute_salt_restart_task():
    '''
    Run the Windows Salt restart task

    CLI Example:

    .. code-block:: bash

        salt '*' service.execute_salt_restart_task()
    '''
    cmd = 'schtasks /Run /TN restart-salt-minion'
    return __salt__['cmd.shell'](cmd)

def status(name, sig=None):
    '''
    Return the status for a service, returns the PID or an empty string if the
    service is running or not, pass a signature to use to find the service via
    ps

    CLI Example:

    .. code-block:: bash

        salt '*' service.status <service name> [service signature]
    '''
    cmd = ['sc', 'query', name]
    statuses = __salt__['cmd.run'](cmd, python_shell=False).splitlines()
    for line in statuses:
        if 'RUNNING' in line:
            return True
        elif 'STOP_PENDING' in line:
            return True
    return False

def getsid(name):
    '''
    Return the sid for this windows service

    CLI Example:

    .. code-block:: bash

        salt '*' service.getsid <service name>
    '''
    cmd = ['sc', 'showsid', name]
    lines = __salt__['cmd.run'](cmd, python_shell=False).splitlines()
    for line in lines:
        if 'SERVICE SID:' in line:
            comps = line.split(':', 1)
            if comps[1] > 1:
                return comps[1].strip()
            else:
                return None

def enable(name, **kwargs):
    '''
    Enable the named service to start at boot

    CLI Example:

    .. code-block:: bash

        salt '*' service.enable <service name>
    '''
    cmd = ['sc', 'config', name, 'start=', 'auto']
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

def disable(name, **kwargs):
    '''
    Disable the named service to start at boot

    CLI Example:

    .. code-block:: bash

        salt '*' service.disable <service name>
    '''
    cmd = ['sc', 'config', name, 'start=', 'disabled']
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

def enabled(name, **kwargs):
    '''
    Check to see if the named service is enabled to start on boot

    CLI Example:

    .. code-block:: bash

        salt '*' service.enabled <service name>
    '''
    cmd = list2cmdline(['sc', 'qc', name])
    lines = __salt__['cmd.run'](cmd).splitlines()
    for line in lines:
        if 'AUTO_START' in line:
            return True
    return False

def disabled(name):
    '''
    Check to see if the named service is disabled to start on boot

    CLI Example:

    .. code-block:: bash

        salt '*' service.disabled <service name>
    '''
    cmd = list2cmdline(['sc', 'qc', name])
    lines = __salt__['cmd.run'](cmd).splitlines()
    for line in lines:
        if 'DEMAND_START' in line:
            return True
        elif 'DISABLED' in line:
            return True
    return False
<commit_msg>Added /V1 /Z to remove scheduled task after run<commit_after># -*- coding: utf-8 -*-
'''
Windows Service module.
'''

# Import python libs
from __future__ import absolute_import
import salt.utils
import time
import logging
from subprocess import list2cmdline
from salt.ext.six.moves import zip
from salt.ext.six.moves import range
try:
    from shlex import quote as _cmd_quote  # pylint: disable=E0611
except ImportError:
    from pipes import quote as _cmd_quote

log = logging.getLogger(__name__)

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

BUFFSIZE = 5000
SERVICE_STOP_DELAY_SECONDS = 15
SERVICE_STOP_POLL_MAX_ATTEMPTS = 5

def __virtual__():
    '''
    Only works on Windows systems
    '''
    if salt.utils.is_windows():
        return __virtualname__
    return False

def has_powershell():
    '''
    Confirm if Powershell is available

    CLI Example:

    .. code-block:: bash

        salt '*' service.has_powershell
    '''
    return 'powershell' in __salt__['cmd.run'](
            ['where', 'powershell'], python_shell=False
        )

def get_enabled():
    '''
    Return the enabled services

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_enabled
    '''
    return sorted([service for service in get_all() if enabled(service)])

def get_disabled():
    '''
    Return the disabled services

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_disabled
    '''
    return sorted([service for service in get_all() if disabled(service)])

def available(name):
    '''
    Returns ``True`` if the specified service is available, otherwise returns
    ``False``.

    CLI Example:

    .. code-block:: bash

        salt '*' service.available <service name>
    '''
    return name in get_all()

def missing(name):
    '''
    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 <service name>
    '''
    return name not in get_all()

def get_all():
    '''
    Return all installed services

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_all
    '''
    ret = set()
    cmd = list2cmdline(['sc', 'query', 'type=', 'service', 'state=', 'all', 'bufsize=', str(BUFFSIZE)])
    lines = __salt__['cmd.run'](cmd).splitlines()
    for line in lines:
        if 'SERVICE_NAME:' in line:
            comps = line.split(':', 1)
            if not len(comps) > 1:
                continue
            ret.add(comps[1].strip())
    return sorted(ret)

def get_service_name(*args):
    '''
    The Display Name is what is displayed in Windows when services.msc is
    executed.  Each Display Name has an associated Service Name which is the
    actual name of the service.  This function allows you to discover the
    Service Name by returning a dictionary of Display Names and Service Names,
    or filter by adding arguments of Display Names.

    If no args are passed, return a dict of all services where the keys are the
    service Display Names and the values are the Service Names.

    If arguments are passed, create a dict of Display Names and Service Names

    CLI Example:

    .. code-block:: bash

        salt '*' service.get_service_name
        salt '*' service.get_service_name 'Google Update Service (gupdate)' 'DHCP Client'
    '''
    ret = {}
    services = []
    display_names = []
    cmd = ['sc', 'query', 'type=', 'service', 'state=', 'all', 'bufsize=', str(BUFFSIZE)]
    lines = __salt__['cmd.run'](cmd, python_shell=False).splitlines()
    for line in lines:
        if 'SERVICE_NAME:' in line:
            comps = line.split(':', 1)
            if not len(comps) > 1:
                continue
            services.append(comps[1].strip())
        if 'DISPLAY_NAME:' in line:
            comps = line.split(':', 1)
            if not len(comps) > 1:
                continue
            display_names.append(comps[1].strip())
    if len(services) == len(display_names):
        service_dict = dict(zip(display_names, services))
    else:
        return 'Service Names and Display Names mismatch'
    if len(args) == 0:
        return service_dict
    for arg in args:
        if arg in service_dict:
            ret[arg] = service_dict[arg]
    return ret

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

    CLI Example:

    .. code-block:: bash

        salt '*' service.start <service name>
    '''
    cmd = ['net', 'start', name]
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

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

    CLI Example:

    .. code-block:: bash

        salt '*' service.stop <service name>
    '''
    # net stop issues a stop command and waits briefly (~30s), but will give
    # up if the service takes too long to stop with a misleading
    # "service could not be stopped" message and RC 0.

    cmd = ['net', 'stop', name]
    res = __salt__['cmd.run'](cmd, python_shell=False)
    if 'service was stopped' in res:
        return True

    # we requested a stop, but the service is still thinking about it.
    # poll for the real status
    for attempt in range(SERVICE_STOP_POLL_MAX_ATTEMPTS):
        if not status(name):
            return True
        log.debug('Waiting for %s to stop', name)
        time.sleep(SERVICE_STOP_DELAY_SECONDS)

    log.warning('Giving up on waiting for service `%s` to stop', name)
    return False

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

    CLI Example:

    .. code-block:: bash

        salt '*' service.restart <service name>
    '''
    if has_powershell():
        if 'salt-minion' in name:
            create_win_salt_restart_task()
            return execute_salt_restart_task()
        cmd = 'Restart-Service {0}'.format(_cmd_quote(name))
        return not __salt__['cmd.retcode'](cmd, shell='powershell', python_shell=True)
    return stop(name) and start(name)

def create_win_salt_restart_task():
    '''

    Create a task in Windows task scheduler to enable restarting the salt-minion

    CLI Example:

    .. code-block:: bash

        salt '*' service.create_win_salt_restart_task()
    '''
    cmd = 'schtasks /RU "System" /Create /TN restart-salt-minion /TR "powershell Restart-Service salt-minion" /sc ONCE /sd 01/01/1975 /st 01:00 /F /V1 /Z'

    return __salt__['cmd.shell'](cmd)

def execute_salt_restart_task():
    '''
    Run the Windows Salt restart task

    CLI Example:

    .. code-block:: bash

        salt '*' service.execute_salt_restart_task()
    '''
    cmd = 'schtasks /Run /TN restart-salt-minion'
    return __salt__['cmd.shell'](cmd)

def status(name, sig=None):
    '''
    Return the status for a service, returns the PID or an empty string if the
    service is running or not, pass a signature to use to find the service via
    ps

    CLI Example:

    .. code-block:: bash

        salt '*' service.status <service name> [service signature]
    '''
    cmd = ['sc', 'query', name]
    statuses = __salt__['cmd.run'](cmd, python_shell=False).splitlines()
    for line in statuses:
        if 'RUNNING' in line:
            return True
        elif 'STOP_PENDING' in line:
            return True
    return False

def getsid(name):
    '''
    Return the sid for this windows service

    CLI Example:

    .. code-block:: bash

        salt '*' service.getsid <service name>
    '''
    cmd = ['sc', 'showsid', name]
    lines = __salt__['cmd.run'](cmd, python_shell=False).splitlines()
    for line in lines:
        if 'SERVICE SID:' in line:
            comps = line.split(':', 1)
            if comps[1] > 1:
                return comps[1].strip()
            else:
                return None

def enable(name, **kwargs):
    '''
    Enable the named service to start at boot

    CLI Example:

    .. code-block:: bash

        salt '*' service.enable <service name>
    '''
    cmd = ['sc', 'config', name, 'start=', 'auto']
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

def disable(name, **kwargs):
    '''
    Disable the named service to start at boot

    CLI Example:

    .. code-block:: bash

        salt '*' service.disable <service name>
    '''
    cmd = ['sc', 'config', name, 'start=', 'disabled']
    return not __salt__['cmd.retcode'](cmd, python_shell=False)

def enabled(name, **kwargs):
    '''
    Check to see if the named service is enabled to start on boot

    CLI Example:

    .. code-block:: bash

        salt '*' service.enabled <service name>
    '''
    cmd = list2cmdline(['sc', 'qc', name])
    lines = __salt__['cmd.run'](cmd).splitlines()
    for line in lines:
        if 'AUTO_START' in line:
            return True
    return False

def disabled(name):
    '''
    Check to see if the named service is disabled to start on boot

    CLI Example:

    .. code-block:: bash

        salt '*' service.disabled <service name>
    '''
    cmd = list2cmdline(['sc', 'qc', name])
    lines = __salt__['cmd.run'](cmd).splitlines()
    for line in lines:
        if 'DEMAND_START' in line:
            return True
        elif 'DISABLED' in line:
            return True
    return False
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
'''
Python renderer that includes a Pythonic Object based interface

:maintainer: Evan Borgstrom <evan@borgstrom.ca>

Let's take a look at how you use pyobjects in a state file. Here's a quick
example that ensures the ``/tmp`` directory is in the correct state.

.. code-block:: python
   :linenos:

    #!pyobjects

    File.managed("/tmp", user='root', group='root', mode='1777')

Nice and Pythonic!

By using the "shebang" syntax to switch to the pyobjects renderer we can now
write our state data using an object based interface that should feel at home
to python developers. You can import any module and do anything that you'd
like (with caution, importing sqlalchemy, django or other large frameworks has
not been tested yet). Using the pyobjects renderer is exactly the same as
using the built-in Python renderer with the exception that pyobjects provides
you with an object based interface for generating state data.

Creating state data
^^^^^^^^^^^^^^^^^^^
Pyobjects takes care of creating an object for each of the available states on
the minion. Each state is represented by an object that is the CamelCase
version of its name (i.e. ``File``, ``Service``, ``User``, etc), and these
objects expose all of their available state functions (i.e. ``File.managed``,
``Service.running``, etc).

The name of the state is split based upon underscores (``_``), then each part
is capitalized and finally the parts are joined back together.

Some examples:

* ``postgres_user`` becomes ``PostgresUser``
* ``ssh_known_hosts`` becomes ``SshKnownHosts``

Context Managers and requisites
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
How about something a little more complex. Here we're going to get into the
core of how to use pyobjects to write states.

.. code-block:: python
   :linenos:

    #!pyobjects

    with Pkg.installed("nginx"):
        Service.running("nginx", enable=True)

        with Service("nginx", "watch_in"):
            File.managed("/etc/nginx/conf.d/mysite.conf",
                         owner='root', group='root', mode='0444',
                         source='salt://nginx/mysite.conf')

The objects that are returned from each of the magic method calls are setup to
be used a Python context managers (``with``) and when you use them as such all
declarations made within the scope will **automatically** use the enclosing
state as a requisite!

The above could have also been written use direct requisite statements as.

.. code-block:: python
   :linenos:

    #!pyobjects

    Pkg.installed("nginx")
    Service.running("nginx", enable=True, require=Pkg("nginx"))
    File.managed("/etc/nginx/conf.d/mysite.conf",
                 owner='root', group='root', mode='0444',
                 source='salt://nginx/mysite.conf',
                 watch_in=Service("nginx"))

You can use the direct requisite statement for referencing states that are
generated outside of the current file.

.. code-block:: python
   :linenos:

    #!pyobjects

    # some-other-package is defined in some other state file
    Pkg.installed("nginx", require=Pkg("some-other-package"))

The last thing that direct requisites provide is the ability to select which
of the SaltStack requisites you want to use (require, require_in, watch,
watch_in, use & use_in) when using the requisite as a context manager.

.. code-block:: python
   :linenos:

    #!pyobjects

    with Service("my-service", "watch_in"):
        ...

The above example would cause all declarations inside the scope of the context
manager to automatically have their ``watch_in`` set to
``Service("my-service")``.

Including and Extending
^^^^^^^^^^^^^^^^^^^^^^^

To include other states use the ``include()`` function. It takes one name per
state to include.

To extend another state use the ``extend()`` function on the name when creating
a state.

.. code-block:: python
   :linenos:

    #!pyobjects

    include('http', 'ssh')

    Service.running(extend('apache'),
                    watch=[File('/etc/httpd/extra/httpd-vhosts.conf')])

Importing from other state files
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Like any Python project that grows you will likely reach a point where you want
to create reusability in your state tree and share objects between state files,
Map Data (described below) is a perfect example of this.

To facilitate this Python's ``import`` statement has been augmented to allow
for a special case when working with a Salt state tree. If you specify a Salt
url (``salt://...``) as the target for importing from then the pyobjects
renderer will take care of fetching the file for you, parsing it with all of
the pyobjects features available and then place the requested objects in the
global scope of the template being rendered.

This works for both types of import statements, ``import X`` and
``from X import Y``.

.. code-block:: python
   :linenos:

    #!pyobjects

    import salt://myfile.sls
    from salt://something/data.sls import Object

See the Map Data section for a more practical use.

Caveats:

* You cannot use the ``as`` syntax, you can only import objects using their
  existing name.

* Imported objects are ALWAYS put into the global scope of your template,
  regardless of where your import statement is.

Salt object
^^^^^^^^^^^
In the spirit of the object interface for creating state data pyobjects also
provides a simple object interface to the ``__salt__`` object.

A function named ``salt`` exists in scope for your sls files and will dispatch
its attributes to the ``__salt__`` dictionary.

The following lines are functionally equivalent:

.. code-block:: python
   :linenos:

    #!pyobjects

    ret = salt.cmd.run(bar)
    ret = __salt__['cmd.run'](bar)

Pillar, grain, mine & config data
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Pyobjects provides shortcut functions for calling ``pillar.get``,
``grains.get``, ``mine.get`` & ``config.get`` on the ``__salt__`` object. This
helps maintain the readability of your state files.

Each type of data can be access by a function of the same name: ``pillar()``,
``grains()``, ``mine()`` and ``config()``.

The following pairs of lines are functionally equivalent:

.. code-block:: python
   :linenos:

    #!pyobjects

    value = pillar('foo:bar:baz', 'qux')
    value = __salt__['pillar.get']('foo:bar:baz', 'qux')

    value = grains('pkg:apache')
    value = __salt__['grains.get']('pkg:apache')

    value = mine('os:Fedora', 'network.interfaces', 'grain')
    value = __salt__['mine.get']('os:Fedora', 'network.interfaces', 'grain')

    value = config('foo:bar:baz', 'qux')
    value = __salt__['config.get']('foo:bar:baz', 'qux')

Map Data
^^^^^^^^
When building complex states or formulas you often need a way of building up a
map of data based on grain data. The most common use of this is tracking the
package and service name differences between distributions.

To build map data using pyobjects we provide a class named Map that you use to
build your own classes with inner classes for each set of values for the
different grain matches.

.. code-block:: python
   :linenos:

    #!pyobjects

    class Samba(Map):
        merge = 'samba:lookup'

        class Debian:
            server = 'samba'
            client = 'samba-client'
            service = 'samba'

        class Ubuntu:
            __grain__ = 'os'
            service = 'smbd'

        class RedHat:
            server = 'samba'
            client = 'samba'
            service = 'smb'

To use this new data you can import it into your state file and then access
your attributes. To access the data in the map you simply access the attribute
name on the base class that is extending Map. Assuming the above Map was in the
file ``samba/map.sls``, you could do the following.

.. code-block:: python
   :linenos:

    #!pyobjects

    from salt://samba/map.sls import Samba

    with Pkg.installed("samba", names=[Samba.server, Samba.client]):
        Service.running("samba", name=Samba.service)

TODO
^^^^
* Interface for working with reactor files
'''

from __future__ import absolute_import

import logging
import re
from salt.ext.six import exec_

import salt.utils
from salt.loader import _create_loader
from salt.fileclient import get_file_client
from salt.utils.pyobjects import Registry, StateFactory, SaltObject, Map

# our import regexes
FROM_RE = r'^\s*from\s+(salt:\/\/.*)\s+import (.*)$'
IMPORT_RE = r'^\s*import\s+(salt:\/\/.*)$'

log = logging.getLogger(__name__)

try:
    __context__['pyobjects_loaded'] = True
except NameError:
    __context__ = {}

def load_states():
    '''
    This loads our states into the salt __context__
    '''
    states = {}

    # the loader expects to find pillar & grian data
    __opts__['grains'] = __grains__
    __opts__['pillar'] = __pillar__

    # we need to build our own loader so that we can process the virtual names
    # in our own way.
    load = _create_loader(__opts__, 'states', 'states')
    load.load_modules()
    for mod in load.modules:
        module_name = mod.__name__.rsplit('.', 1)[-1]

        (virtual_ret, virtual_name, _) = load.process_virtual(mod, module_name)

        # if the module returned a True value and a new name use that
        # otherwise use the default module name
        if virtual_ret and virtual_name != module_name:
            module_name = virtual_name

        # load our functions from the module, pass None in as the module_name
        # so that our function names come back unprefixed
        states[module_name] = load.load_functions(mod, None)

    __context__['pyobjects_states'] = states

def render(template, saltenv='base', sls='', salt_data=True, **kwargs):
    if 'pyobjects_states' not in __context__:
        load_states()

    # these hold the scope that our sls file will be executed with
    _locals = {}
    _globals = {}

    # create our StateFactory objects
    mod_globals = {'StateFactory': StateFactory}
    for mod in __context__['pyobjects_states']:
        mod_locals = {}
        mod_camel = ''.join([
            part.capitalize()
            for part in mod.split('_')
        ])
        valid_funcs = "','".join(
            __context__['pyobjects_states'][mod]
        )
        mod_cmd = "{0} = StateFactory('{1!s}', valid_funcs=['{2}'])".format(
            mod_camel,
            mod,
            valid_funcs
        )
        exec_(mod_cmd, mod_globals, mod_locals)

        _globals[mod_camel] = mod_locals[mod_camel]

    # add our include and extend functions
    _globals['include'] = Registry.include
    _globals['extend'] = Registry.make_extend

    # add our map class
    Map.__salt__ = __salt__
    _globals['Map'] = Map

    # add some convenience methods to the global scope as well as the "dunder"
    # format of all of the salt objects
    try:
        _globals.update({
            # salt, pillar & grains all provide shortcuts or object interfaces
            'salt': SaltObject(__salt__),
            'pillar': __salt__['pillar.get'],
            'grains': __salt__['grains.get'],
            'mine': __salt__['mine.get'],
            'config': __salt__['config.get'],

            # the "dunder" formats are still available for direct use
            '__salt__': __salt__,
            '__pillar__': __pillar__,
            '__grains__': __grains__
        })
    except NameError:
        pass

    # if salt_data is not True then we just return the global scope we've
    # built instead of returning salt data from the registry
    if not salt_data:
        return _globals

    # this will be used to fetch any import files
    client = get_file_client(__opts__)

    # process our sls imports
    #
    # we allow pyobjects users to use a special form of the import statement
    # so that they may bring in objects from other files. while we do this we
    # disable the registry since all we're looking for here is python objects,
    # not salt state data
    template_data = []
    Registry.enabled = False
    for line in template.readlines():
        line = line.rstrip('\r\n')
        matched = False
        for RE in (IMPORT_RE, FROM_RE):
            matches = re.match(RE, line)
            if not matches:
                continue

            import_file = matches.group(1).strip()
            try:
                imports = matches.group(2).split(',')
            except IndexError:
                # if we don't have a third group in the matches object it means
                # that we're importing everything
                imports = None

            state_file = client.cache_file(import_file, saltenv)
            if not state_file:
                raise ImportError("Could not find the file {0!r}".format(import_file))

            with salt.utils.fopen(state_file) as f:
                state_contents = f.read()

            state_locals = {}
            exec_(state_contents, _globals, state_locals)

            if imports is None:
                imports = list(state_locals.keys())

            for name in imports:
                name = name.strip()
                if name not in state_locals:
                    raise ImportError("{0!r} was not found in {1!r}".format(
                        name,
                        import_file
                    ))
                _globals[name] = state_locals[name]

            matched = True
            break

        if not matched:
            template_data.append(line)

    final_template = "\n".join(template_data)

    # re-enable the registry
    Registry.enabled = True

    # now exec our template using our created scopes
    exec_(final_template, _globals, _locals)

    return Registry.salt_data()
<commit_msg>created support from from module import object as alias in pyobjects renderer<commit_after># -*- coding: utf-8 -*-
'''
Python renderer that includes a Pythonic Object based interface

:maintainer: Evan Borgstrom <evan@borgstrom.ca>

Let's take a look at how you use pyobjects in a state file. Here's a quick
example that ensures the ``/tmp`` directory is in the correct state.

.. code-block:: python
   :linenos:

    #!pyobjects

    File.managed("/tmp", user='root', group='root', mode='1777')

Nice and Pythonic!

By using the "shebang" syntax to switch to the pyobjects renderer we can now
write our state data using an object based interface that should feel at home
to python developers. You can import any module and do anything that you'd
like (with caution, importing sqlalchemy, django or other large frameworks has
not been tested yet). Using the pyobjects renderer is exactly the same as
using the built-in Python renderer with the exception that pyobjects provides
you with an object based interface for generating state data.

Creating state data
^^^^^^^^^^^^^^^^^^^
Pyobjects takes care of creating an object for each of the available states on
the minion. Each state is represented by an object that is the CamelCase
version of its name (i.e. ``File``, ``Service``, ``User``, etc), and these
objects expose all of their available state functions (i.e. ``File.managed``,
``Service.running``, etc).

The name of the state is split based upon underscores (``_``), then each part
is capitalized and finally the parts are joined back together.

Some examples:

* ``postgres_user`` becomes ``PostgresUser``
* ``ssh_known_hosts`` becomes ``SshKnownHosts``

Context Managers and requisites
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
How about something a little more complex. Here we're going to get into the
core of how to use pyobjects to write states.

.. code-block:: python
   :linenos:

    #!pyobjects

    with Pkg.installed("nginx"):
        Service.running("nginx", enable=True)

        with Service("nginx", "watch_in"):
            File.managed("/etc/nginx/conf.d/mysite.conf",
                         owner='root', group='root', mode='0444',
                         source='salt://nginx/mysite.conf')

The objects that are returned from each of the magic method calls are setup to
be used a Python context managers (``with``) and when you use them as such all
declarations made within the scope will **automatically** use the enclosing
state as a requisite!

The above could have also been written use direct requisite statements as.

.. code-block:: python
   :linenos:

    #!pyobjects

    Pkg.installed("nginx")
    Service.running("nginx", enable=True, require=Pkg("nginx"))
    File.managed("/etc/nginx/conf.d/mysite.conf",
                 owner='root', group='root', mode='0444',
                 source='salt://nginx/mysite.conf',
                 watch_in=Service("nginx"))

You can use the direct requisite statement for referencing states that are
generated outside of the current file.

.. code-block:: python
   :linenos:

    #!pyobjects

    # some-other-package is defined in some other state file
    Pkg.installed("nginx", require=Pkg("some-other-package"))

The last thing that direct requisites provide is the ability to select which
of the SaltStack requisites you want to use (require, require_in, watch,
watch_in, use & use_in) when using the requisite as a context manager.

.. code-block:: python
   :linenos:

    #!pyobjects

    with Service("my-service", "watch_in"):
        ...

The above example would cause all declarations inside the scope of the context
manager to automatically have their ``watch_in`` set to
``Service("my-service")``.

Including and Extending
^^^^^^^^^^^^^^^^^^^^^^^

To include other states use the ``include()`` function. It takes one name per
state to include.

To extend another state use the ``extend()`` function on the name when creating
a state.

.. code-block:: python
   :linenos:

    #!pyobjects

    include('http', 'ssh')

    Service.running(extend('apache'),
                    watch=[File('/etc/httpd/extra/httpd-vhosts.conf')])

Importing from other state files
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Like any Python project that grows you will likely reach a point where you want
to create reusability in your state tree and share objects between state files,
Map Data (described below) is a perfect example of this.

To facilitate this Python's ``import`` statement has been augmented to allow
for a special case when working with a Salt state tree. If you specify a Salt
url (``salt://...``) as the target for importing from then the pyobjects
renderer will take care of fetching the file for you, parsing it with all of
the pyobjects features available and then place the requested objects in the
global scope of the template being rendered.

This works for both types of import statements, ``import X`` and
``from X import Y``.

.. code-block:: python
   :linenos:

    #!pyobjects

    import salt://myfile.sls
    from salt://something/data.sls import Object

See the Map Data section for a more practical use.

Caveats:

* You cannot use the ``as`` syntax, you can only import objects using their
  existing name.

* Imported objects are ALWAYS put into the global scope of your template,
  regardless of where your import statement is.

Salt object
^^^^^^^^^^^
In the spirit of the object interface for creating state data pyobjects also
provides a simple object interface to the ``__salt__`` object.

A function named ``salt`` exists in scope for your sls files and will dispatch
its attributes to the ``__salt__`` dictionary.

The following lines are functionally equivalent:

.. code-block:: python
   :linenos:

    #!pyobjects

    ret = salt.cmd.run(bar)
    ret = __salt__['cmd.run'](bar)

Pillar, grain, mine & config data
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Pyobjects provides shortcut functions for calling ``pillar.get``,
``grains.get``, ``mine.get`` & ``config.get`` on the ``__salt__`` object. This
helps maintain the readability of your state files.

Each type of data can be access by a function of the same name: ``pillar()``,
``grains()``, ``mine()`` and ``config()``.

The following pairs of lines are functionally equivalent:

.. code-block:: python
   :linenos:

    #!pyobjects

    value = pillar('foo:bar:baz', 'qux')
    value = __salt__['pillar.get']('foo:bar:baz', 'qux')

    value = grains('pkg:apache')
    value = __salt__['grains.get']('pkg:apache')

    value = mine('os:Fedora', 'network.interfaces', 'grain')
    value = __salt__['mine.get']('os:Fedora', 'network.interfaces', 'grain')

    value = config('foo:bar:baz', 'qux')
    value = __salt__['config.get']('foo:bar:baz', 'qux')

Map Data
^^^^^^^^
When building complex states or formulas you often need a way of building up a
map of data based on grain data. The most common use of this is tracking the
package and service name differences between distributions.

To build map data using pyobjects we provide a class named Map that you use to
build your own classes with inner classes for each set of values for the
different grain matches.

.. code-block:: python
   :linenos:

    #!pyobjects

    class Samba(Map):
        merge = 'samba:lookup'

        class Debian:
            server = 'samba'
            client = 'samba-client'
            service = 'samba'

        class Ubuntu:
            __grain__ = 'os'
            service = 'smbd'

        class RedHat:
            server = 'samba'
            client = 'samba'
            service = 'smb'

To use this new data you can import it into your state file and then access
your attributes. To access the data in the map you simply access the attribute
name on the base class that is extending Map. Assuming the above Map was in the
file ``samba/map.sls``, you could do the following.

.. code-block:: python
   :linenos:

    #!pyobjects

    from salt://samba/map.sls import Samba

    with Pkg.installed("samba", names=[Samba.server, Samba.client]):
        Service.running("samba", name=Samba.service)

TODO
^^^^
* Interface for working with reactor files
'''

from __future__ import absolute_import

import logging
import re
from salt.ext.six import exec_

import salt.utils
from salt.loader import _create_loader
from salt.fileclient import get_file_client
from salt.utils.pyobjects import Registry, StateFactory, SaltObject, Map

# our import regexes
FROM_RE = r'^\s*from\s+(salt:\/\/.*)\s+import (.*)$'
IMPORT_RE = r'^\s*import\s+(salt:\/\/.*)$'
FROM_AS_RE = r'^\(.*) as (.*)$'

log = logging.getLogger(__name__)

try:
    __context__['pyobjects_loaded'] = True
except NameError:
    __context__ = {}

def load_states():
    '''
    This loads our states into the salt __context__
    '''
    states = {}

    # the loader expects to find pillar & grian data
    __opts__['grains'] = __grains__
    __opts__['pillar'] = __pillar__

    # we need to build our own loader so that we can process the virtual names
    # in our own way.
    load = _create_loader(__opts__, 'states', 'states')
    load.load_modules()
    for mod in load.modules:
        module_name = mod.__name__.rsplit('.', 1)[-1]

        (virtual_ret, virtual_name, _) = load.process_virtual(mod, module_name)

        # if the module returned a True value and a new name use that
        # otherwise use the default module name
        if virtual_ret and virtual_name != module_name:
            module_name = virtual_name

        # load our functions from the module, pass None in as the module_name
        # so that our function names come back unprefixed
        states[module_name] = load.load_functions(mod, None)

    __context__['pyobjects_states'] = states

def render(template, saltenv='base', sls='', salt_data=True, **kwargs):
    if 'pyobjects_states' not in __context__:
        load_states()

    # these hold the scope that our sls file will be executed with
    _locals = {}
    _globals = {}

    # create our StateFactory objects
    mod_globals = {'StateFactory': StateFactory}
    for mod in __context__['pyobjects_states']:
        mod_locals = {}
        mod_camel = ''.join([
            part.capitalize()
            for part in mod.split('_')
        ])
        valid_funcs = "','".join(
            __context__['pyobjects_states'][mod]
        )
        mod_cmd = "{0} = StateFactory('{1!s}', valid_funcs=['{2}'])".format(
            mod_camel,
            mod,
            valid_funcs
        )
        exec_(mod_cmd, mod_globals, mod_locals)

        _globals[mod_camel] = mod_locals[mod_camel]

    # add our include and extend functions
    _globals['include'] = Registry.include
    _globals['extend'] = Registry.make_extend

    # add our map class
    Map.__salt__ = __salt__
    _globals['Map'] = Map

    # add some convenience methods to the global scope as well as the "dunder"
    # format of all of the salt objects
    try:
        _globals.update({
            # salt, pillar & grains all provide shortcuts or object interfaces
            'salt': SaltObject(__salt__),
            'pillar': __salt__['pillar.get'],
            'grains': __salt__['grains.get'],
            'mine': __salt__['mine.get'],
            'config': __salt__['config.get'],

            # the "dunder" formats are still available for direct use
            '__salt__': __salt__,
            '__pillar__': __pillar__,
            '__grains__': __grains__
        })
    except NameError:
        pass

    # if salt_data is not True then we just return the global scope we've
    # built instead of returning salt data from the registry
    if not salt_data:
        return _globals

    # this will be used to fetch any import files
    client = get_file_client(__opts__)

    # process our sls imports
    #
    # we allow pyobjects users to use a special form of the import statement
    # so that they may bring in objects from other files. while we do this we
    # disable the registry since all we're looking for here is python objects,
    # not salt state data
    template_data = []
    Registry.enabled = False
    for line in template.readlines():
        line = line.rstrip('\r\n')
        matched = False
        for RE in (IMPORT_RE, FROM_RE):
            matches = re.match(RE, line)
            if not matches:
                continue

            import_file = matches.group(1).strip()
            try:
                imports = matches.group(2).split(',')
            except IndexError:
                # if we don't have a third group in the matches object it means
                # that we're importing everything
                imports = None

            state_file = client.cache_file(import_file, saltenv)
            if not state_file:
                raise ImportError("Could not find the file {0!r}".format(import_file))

            with salt.utils.fopen(state_file) as f:
                state_contents = f.read()

            state_locals = {}
            exec_(state_contents, _globals, state_locals)

            if imports is None:
                imports = list(state_locals.keys())

            for name in imports:
                name = name.strip()

                matches = re.match(FROM_AS_RE, name)
                if matches is not None:
                    name = matches.group(1).strip()
                    alias = matches.group(2).strip()
                else:
                    alais = name

                if name not in state_locals:
                    raise ImportError("{0!r} was not found in {1!r}".format(
                        name,
                        import_file
                    ))
                _globals[alias] = state_locals[name]

            matched = True
            break

        if not matched:
            template_data.append(line)

    final_template = "\n".join(template_data)

    # re-enable the registry
    Registry.enabled = True

    # now exec our template using our created scopes
    exec_(final_template, _globals, _locals)

    return Registry.salt_data()
<|endoftext|>