text:
from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['stableinterface'],
                    'supported_by': 'core'}

DOCUMENTATION = r'''
---
module: service
version_added: "0.1"
short_description:  Manage services
description:
    - Controls services on remote hosts. Supported init systems include BSD init,
      OpenRC, SysV, Solaris SMF, systemd, upstart.
    - For Windows targets, use the M(win_service) module instead.
options:
    name:
        description:
        - Name of the service.
        type: str
        required: true
    state:
        description:
          - C(started)/C(stopped) are idempotent actions that will not run
            commands unless necessary.
          - C(restarted) will always bounce the service.
          - C(reloaded) will always reload.
          - B(At least one of state and enabled are required.)
          - Note that reloaded will start the service if it is not already started,
            even if your chosen init system wouldn't normally.
        type: str
        choices: [ reloaded, restarted, started, stopped ]
    sleep:
        description:
        - If the service is being C(restarted) then sleep this many seconds
          between the stop and start command.
        - This helps to work around badly-behaving init scripts that exit immediately
          after signaling a process to stop.
        - Not all service managers support sleep, i.e when using systemd this setting will be ignored.
        type: int
        version_added: "1.3"
    pattern:
        description:
        - If the service does not respond to the status command, name a
          substring to look for as would be found in the output of the I(ps)
          command as a stand-in for a status result.
        - If the string is found, the service will be assumed to be started.
        type: str
        version_added: "0.7"
    enabled:
        description:
        - Whether the service should start on boot.
        - B(At least one of state and enabled are required.)
        type: bool
    runlevel:
        description:
        - For OpenRC init scripts (e.g. Gentoo) only.
        - The runlevel that this service belongs to.
        type: str
        default: default
    arguments:
        description:
        - Additional arguments provided on the command line.
        type: str
        aliases: [ args ]
    use:
        description:
        - The service module actually uses system specific modules, normally through auto detection, this setting can force a specific module.
        - Normally it uses the value of the 'ansible_service_mgr' fact and falls back to the old 'service' module when none matching is found.
        type: str
        default: auto
        version_added: 2.2
notes:
    - For AIX, group subsystem names can be used.
seealso:
- module: win_service
author:
    - Ansible Core Team
    - Michael DeHaan
'''

EXAMPLES = r'''
- name: Start service httpd, if not started
  service:
    name: httpd
    state: started

- name: Stop service httpd, if started
  service:
    name: httpd
    state: stopped

- name: Restart service httpd, in all cases
  service:
    name: httpd
    state: restarted

- name: Reload service httpd, in all cases
  service:
    name: httpd
    state: reloaded

- name: Enable service httpd, and not touch the state
  service:
    name: httpd
    enabled: yes

- name: Start service foo, based on running process /usr/bin/foo
  service:
    name: foo
    pattern: /usr/bin/foo
    state: started

- name: Restart network service for interface eth0
  service:
    name: network
    state: restarted
    args: eth0
'''

import glob
import json
import os
import platform
import re
import select
import shlex
import string
import subprocess
import tempfile
import time

# The distutils module is not shipped with SUNWPython on Solaris.
# It's in the SUNWPython-devel package which also contains development files
# that don't belong on production boxes.  Since our Solaris code doesn't
# depend on LooseVersion, do not import it on Solaris.
if platform.system() != 'SunOS':
    from distutils.version import LooseVersion

from ansible.module_utils.basic import AnsibleModule, load_platform_subclass
from ansible.module_utils.service import fail_if_missing
from ansible.module_utils.six import PY2, b
from ansible.module_utils._text import to_bytes, to_text

class Service(object):
    """
    This is the generic Service manipulation class that is subclassed
    based on platform.

    A subclass should override the following action methods:-
      - get_service_tools
      - service_enable
      - get_service_status
      - service_control

    All subclasses MUST define platform and distribution (which may be None).
    """

    platform = 'Generic'
    distribution = None

    def __new__(cls, *args, **kwargs):
        return load_platform_subclass(Service, args, kwargs)

    def __init__(self, module):
        self.module = module
        self.name = module.params['name']
        self.state = module.params['state']
        self.sleep = module.params['sleep']
        self.pattern = module.params['pattern']
        self.enable = module.params['enabled']
        self.runlevel = module.params['runlevel']
        self.changed = False
        self.running = None
        self.crashed = None
        self.action = None
        self.svc_cmd = None
        self.svc_initscript = None
        self.svc_initctl = None
        self.enable_cmd = None
        self.arguments = module.params.get('arguments', '')
        self.rcconf_file = None
        self.rcconf_key = None
        self.rcconf_value = None
        self.svc_change = False

    # ===========================================
    # Platform specific methods (must be replaced by subclass).

    def get_service_tools(self):
        self.module.fail_json(msg="get_service_tools not implemented on target platform")

    def service_enable(self):
        self.module.fail_json(msg="service_enable not implemented on target platform")

    def get_service_status(self):
        self.module.fail_json(msg="get_service_status not implemented on target platform")

    def service_control(self):
        self.module.fail_json(msg="service_control not implemented on target platform")

    # ===========================================
    # Generic methods that should be used on all platforms.

    def execute_command(self, cmd, daemonize=False):

        # Most things don't need to be daemonized
        if not daemonize:
            # chkconfig localizes messages and we're screen scraping so make
            # sure we use the C locale
            lang_env = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C')
            return self.module.run_command(cmd, environ_update=lang_env)

        # This is complex because daemonization is hard for people.
        # What we do is daemonize a part of this module, the daemon runs the
        # command, picks up the return code and output, and returns it to the
        # main process.
        pipe = os.pipe()
        pid = os.fork()
        if pid == 0:
            os.close(pipe[0])
            # Set stdin/stdout/stderr to /dev/null
            fd = os.open(os.devnull, os.O_RDWR)
            if fd != 0:
                os.dup2(fd, 0)
            if fd != 1:
                os.dup2(fd, 1)
            if fd != 2:
                os.dup2(fd, 2)
            if fd not in (0, 1, 2):
                os.close(fd)

            # Make us a daemon. Yes, that's all it takes.
            pid = os.fork()
            if pid > 0:
                os._exit(0)
            os.setsid()
            os.chdir("/")
            pid = os.fork()
            if pid > 0:
                os._exit(0)

            # Start the command
            if PY2:
                # Python 2.6's shlex.split can't handle text strings correctly
                cmd = to_bytes(cmd, errors='surrogate_or_strict')
                cmd = shlex.split(cmd)
            else:
                # Python3.x shex.split text strings.
                cmd = to_text(cmd, errors='surrogate_or_strict')
                cmd = [to_bytes(c, errors='surrogate_or_strict') for c in shlex.split(cmd)]
            # In either of the above cases, pass a list of byte strings to Popen

            # chkconfig localizes messages and we're screen scraping so make
            # sure we use the C locale
            lang_env = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C')
            p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=lang_env, preexec_fn=lambda: os.close(pipe[1]))
            stdout = b("")
            stderr = b("")
            fds = [p.stdout, p.stderr]
            # Wait for all output, or until the main process is dead and its output is done.
            while fds:
                rfd, wfd, efd = select.select(fds, [], fds, 1)
                if not (rfd + wfd + efd) and p.poll() is not None:
                    break
                if p.stdout in rfd:
                    dat = os.read(p.stdout.fileno(), 4096)
                    if not dat:
                        fds.remove(p.stdout)
                    stdout += dat
                if p.stderr in rfd:
                    dat = os.read(p.stderr.fileno(), 4096)
                    if not dat:
                        fds.remove(p.stderr)
                    stderr += dat
            p.wait()
            # Return a JSON blob to parent
            blob = json.dumps([p.returncode, to_text(stdout), to_text(stderr)])
            os.write(pipe[1], to_bytes(blob, errors='surrogate_or_strict'))
            os.close(pipe[1])
            os._exit(0)
        elif pid == -1:
            self.module.fail_json(msg="unable to fork")
        else:
            os.close(pipe[1])
            os.waitpid(pid, 0)
            # Wait for data from daemon process and process it.
            data = b("")
            while True:
                rfd, wfd, efd = select.select([pipe[0]], [], [pipe[0]])
                if pipe[0] in rfd:
                    dat = os.read(pipe[0], 4096)
                    if not dat:
                        break
                    data += dat
            return json.loads(to_text(data, errors='surrogate_or_strict'))

    def check_ps(self):
        # Set ps flags
        if platform.system() == 'SunOS':
            psflags = '-ef'
        else:
            psflags = 'auxww'

        # Find ps binary
        psbin = self.module.get_bin_path('ps', True)

        (rc, psout, pserr) = self.execute_command('%s %s' % (psbin, psflags))
        # If rc is 0, set running as appropriate
        if rc == 0:
            self.running = False
            lines = psout.split("\n")
            for line in lines:
                if self.pattern in line and "pattern=" not in line:
                    # so as to not confuse ./hacking/test-module
                    self.running = True
                    break

    def check_service_changed(self):
        if self.state and self.running is None:
            self.module.fail_json(msg="failed determining service state, possible typo of service name?")
        # Find out if state has changed
        if not self.running and self.state in ["reloaded", "started"]:
            self.svc_change = True
        elif self.running and self.state in ["reloaded", "stopped"]:
            self.svc_change = True
        elif self.state == "restarted":
            self.svc_change = True
        if self.module.check_mode and self.svc_change:
            self.module.exit_json(changed=True, msg='service state changed')

    def modify_service_state(self):

        # Only do something if state will change
        if self.svc_change:
            # Control service
            if self.state in ['started']:
                self.action = "start"
            elif not self.running and self.state == 'reloaded':
                self.action = "start"
            elif self.state == 'stopped':
                self.action = "stop"
            elif self.state == 'reloaded':
                self.action = "reload"
            elif self.state == 'restarted':
                self.action = "restart"

            if self.module.check_mode:
                self.module.exit_json(changed=True, msg='changing service state')

            return self.service_control()

        else:
            # If nothing needs to change just say all is well
            rc = 0
            err = ''
            out = ''
            return rc, out, err

    def service_enable_rcconf(self):
        if self.rcconf_file is None or self.rcconf_key is None or self.rcconf_value is None:
            self.module.fail_json(msg="service_enable_rcconf() requires rcconf_file, rcconf_key and rcconf_value")

        self.changed = None
        entry = '%s="%s"\n' % (self.rcconf_key, self.rcconf_value)
        RCFILE = open(self.rcconf_file, "r")
        new_rc_conf = []

        # Build a list containing the possibly modified file.
        for rcline in RCFILE:
            # Parse line removing whitespaces, quotes, etc.
            rcarray = shlex.split(rcline, comments=True)
            if len(rcarray) >= 1 and '=' in rcarray[0]:
                (key, value) = rcarray[0].split("=", 1)
                if key == self.rcconf_key:
                    if value.upper() == self.rcconf_value:
                        # Since the proper entry already exists we can stop iterating.
                        self.changed = False
                        break
                    else:
                        # We found the key but the value is wrong, replace with new entry.
                        rcline = entry
                        self.changed = True

            # Add line to the list.
            new_rc_conf.append(rcline.strip() + '\n')

        # We are done with reading the current rc.conf, close it.
        RCFILE.close()

        # If we did not see any trace of our entry we need to add it.
        if self.changed is None:
            new_rc_conf.append(entry)
            self.changed = True

        if self.changed is True:

            if self.module.check_mode:
                self.module.exit_json(changed=True, msg="changing service enablement")

            # Create a temporary file next to the current rc.conf (so we stay on the same filesystem).
            # This way the replacement operation is atomic.
            rcconf_dir = os.path.dirname(self.rcconf_file)
            rcconf_base = os.path.basename(self.rcconf_file)
            (TMP_RCCONF, tmp_rcconf_file) = tempfile.mkstemp(dir=rcconf_dir, prefix="%s-" % rcconf_base)

            # Write out the contents of the list into our temporary file.
            for rcline in new_rc_conf:
                os.write(TMP_RCCONF, rcline)

            # Close temporary file.
            os.close(TMP_RCCONF)

            # Replace previous rc.conf.
            self.module.atomic_move(tmp_rcconf_file, self.rcconf_file)

class LinuxService(Service):
    """
    This is the Linux Service manipulation class - it is currently supporting
    a mixture of binaries and init scripts for controlling services started at
    boot, as well as for controlling the current state.
    """

    platform = 'Linux'
    distribution = None

    def get_service_tools(self):

        paths = ['/sbin', '/usr/sbin', '/bin', '/usr/bin']
        binaries = ['service', 'chkconfig', 'update-rc.d', 'rc-service', 'rc-update', 'initctl', 'systemctl', 'start', 'stop', 'restart', 'insserv']
        initpaths = ['/etc/init.d']
        location = dict()

        for binary in binaries:
            location[binary] = self.module.get_bin_path(binary, opt_dirs=paths)

        for initdir in initpaths:
            initscript = "%s/%s" % (initdir, self.name)
            if os.path.isfile(initscript):
                self.svc_initscript = initscript

        def check_systemd():

            # tools must be installed
            if location.get('systemctl', False):

                # this should show if systemd is the boot init system
                # these mirror systemd's own sd_boot test http://www.freedesktop.org/software/systemd/man/sd_booted.html
                for canary in ["/run/systemd/system/", "/dev/.run/systemd/", "/dev/.systemd/"]:
                    if os.path.exists(canary):
                        return True

                # If all else fails, check if init is the systemd command, using comm as cmdline could be symlink
                try:
                    f = open('/proc/1/comm', 'r')
                except IOError:
                    # If comm doesn't exist, old kernel, no systemd
                    return False

                for line in f:
                    if 'systemd' in line:
                        return True

            return False

        # Locate a tool to enable/disable a service
        if check_systemd():
            # service is managed by systemd
            self.__systemd_unit = self.name
            self.svc_cmd = location['systemctl']
            self.enable_cmd = location['systemctl']

        elif location.get('initctl', False) and os.path.exists("/etc/init/%s.conf" % self.name):
            # service is managed by upstart
            self.enable_cmd = location['initctl']
            # set the upstart version based on the output of 'initctl version'
            self.upstart_version = LooseVersion('0.0.0')
            try:
                version_re = re.compile(r'\(upstart (.*)\)')
                rc, stdout, stderr = self.module.run_command('%s version' % location['initctl'])
                if rc == 0:
                    res = version_re.search(stdout)
                    if res:
                        self.upstart_version = LooseVersion(res.groups()[0])
            except Exception:
                pass  # we'll use the default of 0.0.0

            self.svc_cmd = location['initctl']

        elif location.get('rc-service', False):
            # service is managed by OpenRC
            self.svc_cmd = location['rc-service']
            self.enable_cmd = location['rc-update']
            return  # already have service start/stop tool too!

        elif self.svc_initscript:
            # service is managed by with SysV init scripts
            if location.get('update-rc.d', False):
                # and uses update-rc.d
                self.enable_cmd = location['update-rc.d']
            elif location.get('insserv', None):
                # and uses insserv
                self.enable_cmd = location['insserv']
            elif location.get('chkconfig', False):
                # and uses chkconfig
                self.enable_cmd = location['chkconfig']

        if self.enable_cmd is None:
            fail_if_missing(self.module, False, self.name, msg='host')

        # If no service control tool selected yet, try to see if 'service' is available
        if self.svc_cmd is None and location.get('service', False):
            self.svc_cmd = location['service']

        # couldn't find anything yet
        if self.svc_cmd is None and not self.svc_initscript:
            self.module.fail_json(msg='cannot find \'service\' binary or init script for service,  possible typo in service name?, aborting')

        if location.get('initctl', False):
            self.svc_initctl = location['initctl']

    def get_systemd_service_enabled(self):
        def sysv_exists(name):
            script = '/etc/init.d/' + name
            return os.access(script, os.X_OK)

        def sysv_is_enabled(name):
            return bool(glob.glob('/etc/rc?.d/S??' + name))

        service_name = self.__systemd_unit
        (rc, out, err) = self.execute_command("%s is-enabled %s" % (self.enable_cmd, service_name,))
        if rc == 0:
            return True
        elif out.startswith('disabled'):
            return False
        elif sysv_exists(service_name):
            return sysv_is_enabled(service_name)
        else:
            return False

    def get_systemd_status_dict(self):

        # Check status first as show will not fail if service does not exist
        (rc, out, err) = self.execute_command("%s show '%s'" % (self.enable_cmd, self.__systemd_unit,))
        if rc != 0:
            self.module.fail_json(msg='failure %d running systemctl show for %r: %s' % (rc, self.__systemd_unit, err))
        elif 'LoadState=not-found' in out:
            self.module.fail_json(msg='systemd could not find the requested service "%r": %s' % (self.__systemd_unit, err))

        key = None
        value_buffer = []
        status_dict = {}
        for line in out.splitlines():
            if '=' in line:
                if not key:
                    key, value = line.split('=', 1)
                    # systemd fields that are shell commands can be multi-line
                    # We take a value that begins with a "{" as the start of
                    # a shell command and a line that ends with "}" as the end of
                    # the command
                    if value.lstrip().startswith('{'):
                        if value.rstrip().endswith('}'):
                            status_dict[key] = value
                            key = None
                        else:
                            value_buffer.append(value)
                    else:
                        status_dict[key] = value
                        key = None
                else:
                    if line.rstrip().endswith('}'):
                        status_dict[key] = '\n'.join(value_buffer)
                        key = None
                    else:
                        value_buffer.append(value)
            else:
                value_buffer.append(value)

        return status_dict

    def get_systemd_service_status(self):
        d = self.get_systemd_status_dict()
        if d.get('ActiveState') == 'active':
            # run-once services (for which a single successful exit indicates
            # that they are running as designed) should not be restarted here.
            # Thus, we are not checking d['SubState'].
            self.running = True
            self.crashed = False
        elif d.get('ActiveState') == 'failed':
            self.running = False
            self.crashed = True
        elif d.get('ActiveState') is None:
            self.module.fail_json(msg='No ActiveState value in systemctl show output for %r' % (self.__systemd_unit,))
        else:
            self.running = False
            self.crashed = False
        return self.running

    def get_service_status(self):
        if self.svc_cmd and self.svc_cmd.endswith('systemctl'):
            return self.get_systemd_service_status()

        self.action = "status"
        rc, status_stdout, status_stderr = self.service_control()

        # if we have decided the service is managed by upstart, we check for some additional output...
        if self.svc_initctl and self.running is None:
            # check the job status by upstart response
            initctl_rc, initctl_status_stdout, initctl_status_stderr = self.execute_command("%s status %s %s" % (self.svc_initctl, self.name, self.arguments))
            if "stop/waiting" in initctl_status_stdout:
                self.running = False
            elif "start/running" in initctl_status_stdout:
                self.running = True

        if self.svc_cmd and self.svc_cmd.endswith("rc-service") and self.running is None:
            openrc_rc, openrc_status_stdout, openrc_status_stderr = self.execute_command("%s %s status" % (self.svc_cmd, self.name))
            self.running = "started" in openrc_status_stdout
            self.crashed = "crashed" in openrc_status_stderr

        # Prefer a non-zero return code. For reference, see:
        # http://refspecs.linuxbase.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
        if self.running is None and rc in [1, 2, 3, 4, 69]:
            self.running = False

        # if the job status is still not known check it by status output keywords
        # Only check keywords if there's only one line of output (some init
        # scripts will output verbosely in case of error and those can emit
        # keywords that are picked up as false positives
        if self.running is None and status_stdout.count('\n') <= 1:
            # first transform the status output that could irritate keyword matching
            cleanout = status_stdout.lower().replace(self.name.lower(), '')
            if "stop" in cleanout:
                self.running = False
            elif "run" in cleanout:
                self.running = not ("not " in cleanout)
            elif "start" in cleanout and "not " not in cleanout:
                self.running = True
            elif 'could not access pid file' in cleanout:
                self.running = False
            elif 'is dead and pid file exists' in cleanout:
                self.running = False
            elif 'dead but subsys locked' in cleanout:
                self.running = False
            elif 'dead but pid file exists' in cleanout:
                self.running = False

        # if the job status is still not known and we got a zero for the
        # return code, assume here that the service is running
        if self.running is None and rc == 0:
            self.running = True

        # if the job status is still not known check it by special conditions
        if self.running is None:
            if self.name == 'iptables' and "ACCEPT" in status_stdout:
                # iptables status command output is lame
                # TODO: lookup if we can use a return code for this instead?
                self.running = True

        return self.running

    def service_enable(self):

        if self.enable_cmd is None:
            self.module.fail_json(msg='cannot detect command to enable service %s, typo or init system potentially unknown' % self.name)

        self.changed = True
        action = None

        #
        # Upstart's initctl
        #
        if self.enable_cmd.endswith("initctl"):
            def write_to_override_file(file_name, file_contents, ):
                override_file = open(file_name, 'w')
                override_file.write(file_contents)
                override_file.close()

            initpath = '/etc/init'
            if self.upstart_version >= LooseVersion('0.6.7'):
                manreg = re.compile(r'^manual\s*$', re.M | re.I)
                config_line = 'manual\n'
            else:
                manreg = re.compile(r'^start on manual\s*$', re.M | re.I)
                config_line = 'start on manual\n'
            conf_file_name = "%s/%s.conf" % (initpath, self.name)
            override_file_name = "%s/%s.override" % (initpath, self.name)

            # Check to see if files contain the manual line in .conf and fail if True
            with open(conf_file_name) as conf_file_fh:
                conf_file_content = conf_file_fh.read()
            if manreg.search(conf_file_content):
                self.module.fail_json(msg="manual stanza not supported in a .conf file")

            self.changed = False
            if os.path.exists(override_file_name):
                with open(override_file_name) as override_fh:
                    override_file_contents = override_fh.read()
                # Remove manual stanza if present and service enabled
                if self.enable and manreg.search(override_file_contents):
                    self.changed = True
                    override_state = manreg.sub('', override_file_contents)
                # Add manual stanza if not present and service disabled
                elif not (self.enable) and not (manreg.search(override_file_contents)):
                    self.changed = True
                    override_state = '\n'.join((override_file_contents, config_line))
                # service already in desired state
                else:
                    pass
            # Add file with manual stanza if service disabled
            elif not (self.enable):
                self.changed = True
                override_state = config_line
            else:
                # service already in desired state
                pass

            if self.module.check_mode:
                self.module.exit_json(changed=self.changed)

            # The initctl method of enabling and disabling services is much
            # different than for the other service methods.  So actually
            # committing the change is done in this conditional and then we
            # skip the boilerplate at the bottom of the method
            if self.changed:
                try:
                    write_to_override_file(override_file_name, override_state)
                except Exception:
                    self.module.fail_json(msg='Could not modify override file')

            return

        #
        # SysV's chkconfig
        #
        if self.enable_cmd.endswith("chkconfig"):
            if self.enable:
                action = 'on'
            else:
                action = 'off'

            (rc, out, err) = self.execute_command("%s --list %s" % (self.enable_cmd, self.name))
            if 'chkconfig --add %s' % self.name in err:
                self.execute_command("%s --add %s" % (self.enable_cmd, self.name))
                (rc, out, err) = self.execute_command("%s --list %s" % (self.enable_cmd, self.name))
            if self.name not in out:
                self.module.fail_json(msg="service %s does not support chkconfig" % self.name)
            # TODO: look back on why this is here
            # state = out.split()[-1]

            # Check if we're already in the correct state
            if "3:%s" % action in out and "5:%s" % action in out:
                self.changed = False
                return

        #
        # Systemd's systemctl
        #
        if self.enable_cmd.endswith("systemctl"):
            if self.enable:
                action = 'enable'
            else:
                action = 'disable'

            # Check if we're already in the correct state
            service_enabled = self.get_systemd_service_enabled()

            # self.changed should already be true
            if self.enable == service_enabled:
                self.changed = False
                return

        #
        # OpenRC's rc-update
        #
        if self.enable_cmd.endswith("rc-update"):
            if self.enable:
                action = 'add'
            else:
                action = 'delete'

            (rc, out, err) = self.execute_command("%s show" % self.enable_cmd)
            for line in out.splitlines():
                service_name, runlevels = line.split('|')
                service_name = service_name.strip()
                if service_name != self.name:
                    continue
                runlevels = re.split(r'\s+', runlevels)
                # service already enabled for the runlevel
                if self.enable and self.runlevel in runlevels:
                    self.changed = False
                # service already disabled for the runlevel
                elif not self.enable and self.runlevel not in runlevels:
                    self.changed = False
                break
            else:
                # service already disabled altogether
                if not self.enable:
                    self.changed = False

            if not self.changed:
                return

        #
        # update-rc.d style
        #
        if self.enable_cmd.endswith("update-rc.d"):

            enabled = False
            slinks = glob.glob('/etc/rc?.d/S??' + self.name)
            if slinks:
                enabled = True

            if self.enable != enabled:
                self.changed = True

                if self.enable:
                    action = 'enable'
                    klinks = glob.glob('/etc/rc?.d/K??' + self.name)
                    if not klinks:
                        if not self.module.check_mode:
                            (rc, out, err) = self.execute_command("%s %s defaults" % (self.enable_cmd, self.name))
                            if rc != 0:
                                if err:
                                    self.module.fail_json(msg=err)
                                else:
                                    self.module.fail_json(msg=out) % (self.enable_cmd, self.name, action)
                else:
                    action = 'disable'

                if not self.module.check_mode:
                    (rc, out, err) = self.execute_command("%s %s %s" % (self.enable_cmd, self.name, action))
                    if rc != 0:
                        if err:
                            self.module.fail_json(msg=err)
                        else:
                            self.module.fail_json(msg=out) % (self.enable_cmd, self.name, action)
            else:
                self.changed = False

            return

        #
        # insserv (Debian <=7, SLES, others)
        #
        if self.enable_cmd.endswith("insserv"):
            if self.enable:
                (rc, out, err) = self.execute_command("%s -n -v %s" % (self.enable_cmd, self.name))
            else:
                (rc, out, err) = self.execute_command("%s -n -r -v %s" % (self.enable_cmd, self.name))

            self.changed = False
            for line in err.splitlines():
                if self.enable and line.find('enable service') != -1:
                    self.changed = True
                    break
                if not self.enable and line.find('remove service') != -1:
                    self.changed = True
                    break

            if self.module.check_mode:
                self.module.exit_json(changed=self.changed)

            if not self.changed:
                return

            if self.enable:
                (rc, out, err) = self.execute_command("%s %s" % (self.enable_cmd, self.name))
                if (rc != 0) or (err != ''):
                    self.module.fail_json(msg=("Failed to install service. rc: %s, out: %s, err: %s" % (rc, out, err)))
                return (rc, out, err)
            else:
                (rc, out, err) = self.execute_command("%s -r %s" % (self.enable_cmd, self.name))
                if (rc != 0) or (err != ''):
                    self.module.fail_json(msg=("Failed to remove service. rc: %s, out: %s, err: %s" % (rc, out, err)))
                return (rc, out, err)

        #
        # If we've gotten to the end, the service needs to be updated
        #
        self.changed = True

        # we change argument order depending on real binary used:
        # rc-update and systemctl need the argument order reversed

        if self.enable_cmd.endswith("rc-update"):
            args = (self.enable_cmd, action, self.name + " " + self.runlevel)
        elif self.enable_cmd.endswith("systemctl"):
            args = (self.enable_cmd, action, self.__systemd_unit)
        else:
            args = (self.enable_cmd, self.name, action)

        if self.module.check_mode:
            self.module.exit_json(changed=self.changed)

        (rc, out, err) = self.execute_command("%s %s %s" % args)
        if rc != 0:
            if err:
                self.module.fail_json(msg="Error when trying to %s %s: rc=%s %s" % (action, self.name, rc, err))
            else:
                self.module.fail_json(msg="Failure for %s %s: rc=%s %s" % (action, self.name, rc, out))

        return (rc, out, err)

    def service_control(self):

        # Decide what command to run
        svc_cmd = ''
        arguments = self.arguments
        if self.svc_cmd:
            if not self.svc_cmd.endswith("systemctl"):
                if self.svc_cmd.endswith("initctl"):
                    # initctl commands take the form <cmd> <action> <name>
                    svc_cmd = self.svc_cmd
                    arguments = "%s %s" % (self.name, arguments)
                else:
                    # SysV and OpenRC take the form <cmd> <name> <action>
                    svc_cmd = "%s %s" % (self.svc_cmd, self.name)
            else:
                # systemd commands take the form <cmd> <action> <name>
                svc_cmd = self.svc_cmd
                arguments = "%s %s" % (self.__systemd_unit, arguments)
        elif self.svc_cmd is None and self.svc_initscript:
            # upstart
            svc_cmd = "%s" % self.svc_initscript

        # In OpenRC, if a service crashed, we need to reset its status to
        # stopped with the zap command, before we can start it back.
        if self.svc_cmd and self.svc_cmd.endswith('rc-service') and self.action == 'start' and self.crashed:
            self.execute_command("%s zap" % svc_cmd, daemonize=True)

        if self.action != "restart":
            if svc_cmd != '':
                # upstart or systemd or OpenRC
                rc_state, stdout, stderr = self.execute_command("%s %s %s" % (svc_cmd, self.action, arguments), daemonize=True)
            else:
                # SysV
                rc_state, stdout, stderr = self.execute_command("%s %s %s" % (self.action, self.name, arguments), daemonize=True)
        elif self.svc_cmd and self.svc_cmd.endswith('rc-service'):
            # All services in OpenRC support restart.
            rc_state, stdout, stderr = self.execute_command("%s %s %s" % (svc_cmd, self.action, arguments), daemonize=True)
        else:
            # In other systems, not all services support restart. Do it the hard way.
            if svc_cmd != '':
                # upstart or systemd
                rc1, stdout1, stderr1 = self.execute_command("%s %s %s" % (svc_cmd, 'stop', arguments), daemonize=True)
            else:
                # SysV
                rc1, stdout1, stderr1 = self.execute_command("%s %s %s" % ('stop', self.name, arguments), daemonize=True)

            if self.sleep:
                time.sleep(self.sleep)

            if svc_cmd != '':
                # upstart or systemd
                rc2, stdout2, stderr2 = self.execute_command("%s %s %s" % (svc_cmd, 'start', arguments), daemonize=True)
            else:
                # SysV
                rc2, stdout2, stderr2 = self.execute_command("%s %s %s" % ('start', self.name, arguments), daemonize=True)

            # merge return information
            if rc1 != 0 and rc2 == 0:
                rc_state = rc2
                stdout = stdout2
                stderr = stderr2
            else:
                rc_state = rc1 + rc2
                stdout = stdout1 + stdout2
                stderr = stderr1 + stderr2

        return (rc_state, stdout, stderr)

class FreeBsdService(Service):
    """
    This is the FreeBSD Service manipulation class - it uses the /etc/rc.conf
    file for controlling services started at boot and the 'service' binary to
    check status and perform direct service manipulation.
    """

    platform = 'FreeBSD'
    distribution = None

    def get_service_tools(self):
        self.svc_cmd = self.module.get_bin_path('service', True)
        if not self.svc_cmd:
            self.module.fail_json(msg='unable to find service binary')

        self.sysrc_cmd = self.module.get_bin_path('sysrc')

    def get_service_status(self):
        rc, stdout, stderr = self.execute_command("%s %s %s %s" % (self.svc_cmd, self.name, 'onestatus', self.arguments))
        if self.name == "pf":
            self.running = "Enabled" in stdout
        else:
            if rc == 1:
                self.running = False
            elif rc == 0:
                self.running = True

    def service_enable(self):
        if self.enable:
            self.rcconf_value = "YES"
        else:
            self.rcconf_value = "NO"

        rcfiles = ['/etc/rc.conf', '/etc/rc.conf.local', '/usr/local/etc/rc.conf']
        for rcfile in rcfiles:
            if os.path.isfile(rcfile):
                self.rcconf_file = rcfile

        rc, stdout, stderr = self.execute_command("%s %s %s %s" % (self.svc_cmd, self.name, 'rcvar', self.arguments))
        try:
            rcvars = shlex.split(stdout, comments=True)
        except Exception:
            # TODO: add a warning to the output with the failure
            pass

        if not rcvars:
            self.module.fail_json(msg="unable to determine rcvar", stdout=stdout, stderr=stderr)

        # In rare cases, i.e. sendmail, rcvar can return several key=value pairs
        # Usually there is just one, however.  In other rare cases, i.e. uwsgi,
        # rcvar can return extra uncommented data that is not at all related to
        # the rcvar.  We will just take the first key=value pair we come across
        # and hope for the best.
        for rcvar in rcvars:
            if '=' in rcvar:
                self.rcconf_key, default_rcconf_value = rcvar.split('=', 1)
                break

        if self.rcconf_key is None:
            self.module.fail_json(msg="unable to determine rcvar", stdout=stdout, stderr=stderr)

        if self.sysrc_cmd:  # FreeBSD >= 9.2

            rc, current_rcconf_value, stderr = self.execute_command("%s -n %s" % (self.sysrc_cmd, self.rcconf_key))
            # it can happen that rcvar is not set (case of a system coming from the ports collection)
            # so we will fallback on the default
            if rc != 0:
                current_rcconf_value = default_rcconf_value

            if current_rcconf_value.strip().upper() != self.rcconf_value:

                self.changed = True

                if self.module.check_mode:
                    self.module.exit_json(changed=True, msg="changing service enablement")

                rc, change_stdout, change_stderr = self.execute_command("%s %s=\"%s\"" % (self.sysrc_cmd, self.rcconf_key, self.rcconf_value))
                if rc != 0:
                    self.module.fail_json(msg="unable to set rcvar using sysrc", stdout=change_stdout, stderr=change_stderr)

                # sysrc does not exit with code 1 on permission error => validate successful change using service(8)
                rc, check_stdout, check_stderr = self.execute_command("%s %s %s" % (self.svc_cmd, self.name, "enabled"))
                if self.enable != (rc == 0):  # rc = 0 indicates enabled service, rc = 1 indicates disabled service
                    self.module.fail_json(msg="unable to set rcvar: sysrc did not change value", stdout=change_stdout, stderr=change_stderr)

            else:
                self.changed = False

        else:  # Legacy (FreeBSD < 9.2)
            try:
                return self.service_enable_rcconf()
            except Exception:
                self.module.fail_json(msg='unable to set rcvar')

    def service_control(self):

        if self.action == "start":
            self.action = "onestart"
        if self.action == "stop":
            self.action = "onestop"
        if self.action == "reload":
            self.action = "onereload"

        ret = self.execute_command("%s %s %s %s" % (self.svc_cmd, self.name, self.action, self.arguments))

        if self.sleep:
            time.sleep(self.sleep)

        return ret

class DragonFlyBsdService(FreeBsdService):
    """
    This is the DragonFly BSD Service manipulation class - it uses the /etc/rc.conf
    file for controlling services started at boot and the 'service' binary to
    check status and perform direct service manipulation.
    """

    platform = 'DragonFly'
    distribution = None

    def service_enable(self):
        if self.enable:
            self.rcconf_value = "YES"
        else:
            self.rcconf_value = "NO"

        rcfiles = ['/etc/rc.conf']  # Overkill?
        for rcfile in rcfiles:
            if os.path.isfile(rcfile):
                self.rcconf_file = rcfile

        self.rcconf_key = "%s" % string.replace(self.name, "-", "_")

        return self.service_enable_rcconf()

class OpenBsdService(Service):
    """
    This is the OpenBSD Service manipulation class - it uses rcctl(8) or
    /etc/rc.d scripts for service control. Enabling a service is
    only supported if rcctl is present.
    """

    platform = 'OpenBSD'
    distribution = None

    def get_service_tools(self):
        self.enable_cmd = self.module.get_bin_path('rcctl')

        if self.enable_cmd:
            self.svc_cmd = self.enable_cmd
        else:
            rcdir = '/etc/rc.d'

            rc_script = "%s/%s" % (rcdir, self.name)
            if os.path.isfile(rc_script):
                self.svc_cmd = rc_script

        if not self.svc_cmd:
            self.module.fail_json(msg='unable to find svc_cmd')

    def get_service_status(self):
        if self.enable_cmd:
            rc, stdout, stderr = self.execute_command("%s %s %s" % (self.svc_cmd, 'check', self.name))
        else:
            rc, stdout, stderr = self.execute_command("%s %s" % (self.svc_cmd, 'check'))

        if stderr:
            self.module.fail_json(msg=stderr)

        if rc == 1:
            self.running = False
        elif rc == 0:
            self.running = True

    def service_control(self):
        if self.enable_cmd:
            return self.execute_command("%s -f %s %s" % (self.svc_cmd, self.action, self.name))
        else:
            return self.execute_command("%s -f %s" % (self.svc_cmd, self.action))

    def service_enable(self):
        if not self.enable_cmd:
            return super(OpenBsdService, self).service_enable()

        rc, stdout, stderr = self.execute_command("%s %s %s %s" % (self.enable_cmd, 'getdef', self.name, 'flags'))

        if stderr:
            self.module.fail_json(msg=stderr)

        getdef_string = stdout.rstrip()

        # Depending on the service the string returned from 'getdef' may be
        # either a set of flags or the boolean YES/NO
        if getdef_string == "YES" or getdef_string == "NO":
            default_flags = ''
        else:
            default_flags = getdef_string

        rc, stdout, stderr = self.execute_command("%s %s %s %s" % (self.enable_cmd, 'get', self.name, 'flags'))

        if stderr:
            self.module.fail_json(msg=stderr)

        get_string = stdout.rstrip()

        # Depending on the service the string returned from 'get' may be
        # either a set of flags or the boolean YES/NO
        if get_string == "YES" or get_string == "NO":
            current_flags = ''
        else:
            current_flags = get_string

        # If there are arguments from the user we use these as flags unless
        # they are already set.
        if self.arguments and self.arguments != current_flags:
            changed_flags = self.arguments
        # If the user has not supplied any arguments and the current flags
        # differ from the default we reset them.
        elif not self.arguments and current_flags != default_flags:
            changed_flags = ' '
        # Otherwise there is no need to modify flags.
        else:
            changed_flags = ''

        rc, stdout, stderr = self.execute_command("%s %s %s %s" % (self.enable_cmd, 'get', self.name, 'status'))

        if self.enable:
            if rc == 0 and not changed_flags:
                return

            if rc != 0:
                status_action = "set %s status on" % (self.name)
            else:
                status_action = ''
            if changed_flags:
                flags_action = "set %s flags %s" % (self.name, changed_flags)
            else:
                flags_action = ''
        else:
            if rc == 1:
                return

            status_action = "set %s status off" % self.name
            flags_action = ''

        # Verify state assumption
        if not status_action and not flags_action:
            self.module.fail_json(msg="neither status_action or status_flags is set, this should never happen")

        if self.module.check_mode:
            self.module.exit_json(changed=True, msg="changing service enablement")

        status_modified = 0
        if status_action:
            rc, stdout, stderr = self.execute_command("%s %s" % (self.enable_cmd, status_action))

            if rc != 0:
                if stderr:
                    self.module.fail_json(msg=stderr)
                else:
                    self.module.fail_json(msg="rcctl failed to modify service status")

            status_modified = 1

        if flags_action:
            rc, stdout, stderr = self.execute_command("%s %s" % (self.enable_cmd, flags_action))

            if rc != 0:
                if stderr:
                    if status_modified:
                        error_message = "rcctl modified service status but failed to set flags: " + stderr
                    else:
                        error_message = stderr
                else:
                    if status_modified:
                        error_message = "rcctl modified service status but failed to set flags"
                    else:
                        error_message = "rcctl failed to modify service flags"

                self.module.fail_json(msg=error_message)

        self.changed = True

class NetBsdService(Service):
    """
    This is the NetBSD Service manipulation class - it uses the /etc/rc.conf
    file for controlling services started at boot, check status and perform
    direct service manipulation. Init scripts in /etc/rcd are used for
    controlling services (start/stop) as well as for controlling the current
    state.
    """

    platform = 'NetBSD'
    distribution = None

    def get_service_tools(self):
        initpaths = ['/etc/rc.d']  # better: $rc_directories - how to get in here? Run: sh -c '. /etc/rc.conf ; echo $rc_directories'

        for initdir in initpaths:
            initscript = "%s/%s" % (initdir, self.name)
            if os.path.isfile(initscript):
                self.svc_initscript = initscript

        if not self.svc_initscript:
            self.module.fail_json(msg='unable to find rc.d script')

    def service_enable(self):
        if self.enable:
            self.rcconf_value = "YES"
        else:
            self.rcconf_value = "NO"

        rcfiles = ['/etc/rc.conf']  # Overkill?
        for rcfile in rcfiles:
            if os.path.isfile(rcfile):
                self.rcconf_file = rcfile

        self.rcconf_key = "%s" % string.replace(self.name, "-", "_")

        return self.service_enable_rcconf()

    def get_service_status(self):
        self.svc_cmd = "%s" % self.svc_initscript
        rc, stdout, stderr = self.execute_command("%s %s" % (self.svc_cmd, 'onestatus'))
        if rc == 1:
            self.running = False
        elif rc == 0:
            self.running = True

    def service_control(self):
        if self.action == "start":
            self.action = "onestart"
        if self.action == "stop":
            self.action = "onestop"

        self.svc_cmd = "%s" % self.svc_initscript
        return self.execute_command("%s %s" % (self.svc_cmd, self.action), daemonize=True)

class SunOSService(Service):
    """
    This is the SunOS Service manipulation class - it uses the svcadm
    command for controlling services, and svcs command for checking status.
    It also tries to be smart about taking the service out of maintenance
    state if necessary.
    """
    platform = 'SunOS'
    distribution = None

    def get_service_tools(self):
        self.svcs_cmd = self.module.get_bin_path('svcs', True)

        if not self.svcs_cmd:
            self.module.fail_json(msg='unable to find svcs binary')

        self.svcadm_cmd = self.module.get_bin_path('svcadm', True)

        if not self.svcadm_cmd:
            self.module.fail_json(msg='unable to find svcadm binary')

        if self.svcadm_supports_sync():
            self.svcadm_sync = '-s'
        else:
            self.svcadm_sync = ''

    def svcadm_supports_sync(self):
        # Support for synchronous restart/refresh is only supported on
        # Oracle Solaris >= 11.2
        for line in open('/etc/release', 'r').readlines():
            m = re.match(r'\s+Oracle Solaris (\d+\.\d+).*', line.rstrip())
            if m and m.groups()[0] >= 11.2:
                return True

    def get_service_status(self):
        status = self.get_sunos_svcs_status()
        # Only 'online' is considered properly running. Everything else is off
        # or has some sort of problem.
        if status == 'online':
            self.running = True
        else:
            self.running = False

    def get_sunos_svcs_status(self):
        rc, stdout, stderr = self.execute_command("%s %s" % (self.svcs_cmd, self.name))
        if rc == 1:
            if stderr:
                self.module.fail_json(msg=stderr)
            else:
                self.module.fail_json(msg=stdout)

        lines = stdout.rstrip("\n").split("\n")
        status = lines[-1].split(" ")[0]
        # status is one of: online, offline, degraded, disabled, maintenance, uninitialized
        # see man svcs(1)
        return status

    def service_enable(self):
        # Get current service enablement status
        rc, stdout, stderr = self.execute_command("%s -l %s" % (self.svcs_cmd, self.name))

        if rc != 0:
            if stderr:
                self.module.fail_json(msg=stderr)
            else:
                self.module.fail_json(msg=stdout)

        enabled = False
        temporary = False

        # look for enabled line, which could be one of:
        #    enabled   true (temporary)
        #    enabled   false (temporary)
        #    enabled   true
        #    enabled   false
        for line in stdout.split("\n"):
            if line.startswith("enabled"):
                if "true" in line:
                    enabled = True
                if "temporary" in line:
                    temporary = True

        startup_enabled = (enabled and not temporary) or (not enabled and temporary)

        if self.enable and startup_enabled:
            return
        elif (not self.enable) and (not startup_enabled):
            return

        if not self.module.check_mode:
            # Mark service as started or stopped (this will have the side effect of
            # actually stopping or starting the service)
            if self.enable:
                subcmd = "enable -rs"
            else:
                subcmd = "disable -s"

            rc, stdout, stderr = self.execute_command("%s %s %s" % (self.svcadm_cmd, subcmd, self.name))

            if rc != 0:
                if stderr:
                    self.module.fail_json(msg=stderr)
                else:
                    self.module.fail_json(msg=stdout)

        self.changed = True

    def service_control(self):
        status = self.get_sunos_svcs_status()

        # if starting or reloading, clear maintenance states
        if self.action in ['start', 'reload', 'restart'] and status in ['maintenance', 'degraded']:
            rc, stdout, stderr = self.execute_command("%s clear %s" % (self.svcadm_cmd, self.name))
            if rc != 0:
                return rc, stdout, stderr
            status = self.get_sunos_svcs_status()

        if status in ['maintenance', 'degraded']:
            self.module.fail_json(msg="Failed to bring service out of %s status." % status)

        if self.action == 'start':
            subcmd = "enable -rst"
        elif self.action == 'stop':
            subcmd = "disable -st"
        elif self.action == 'reload':
            subcmd = "refresh %s" % (self.svcadm_sync)
        elif self.action == 'restart' and status == 'online':
            subcmd = "restart %s" % (self.svcadm_sync)
        elif self.action == 'restart' and status != 'online':
            subcmd = "enable -rst"

        return self.execute_command("%s %s %s" % (self.svcadm_cmd, subcmd, self.name))

class AIX(Service):
    """
    This is the AIX Service (SRC) manipulation class - it uses lssrc, startsrc, stopsrc
    and refresh for service control. Enabling a service is currently not supported.
    Would require to add an entry in the /etc/inittab file (mkitab, chitab and rmitab
    commands)
    """

    platform = 'AIX'
    distribution = None

    def get_service_tools(self):
        self.lssrc_cmd = self.module.get_bin_path('lssrc', True)

        if not self.lssrc_cmd:
            self.module.fail_json(msg='unable to find lssrc binary')

        self.startsrc_cmd = self.module.get_bin_path('startsrc', True)

        if not self.startsrc_cmd:
            self.module.fail_json(msg='unable to find startsrc binary')

        self.stopsrc_cmd = self.module.get_bin_path('stopsrc', True)

        if not self.stopsrc_cmd:
            self.module.fail_json(msg='unable to find stopsrc binary')

        self.refresh_cmd = self.module.get_bin_path('refresh', True)

        if not self.refresh_cmd:
            self.module.fail_json(msg='unable to find refresh binary')

    def get_service_status(self):
        status = self.get_aix_src_status()
        # Only 'active' is considered properly running. Everything else is off
        # or has some sort of problem.
        if status == 'active':
            self.running = True
        else:
            self.running = False

    def get_aix_src_status(self):
        # Check subsystem status
        rc, stdout, stderr = self.execute_command("%s -s %s" % (self.lssrc_cmd, self.name))
        if rc == 1:
            # If check for subsystem is not ok, check if service name is a
            # group subsystem
            rc, stdout, stderr = self.execute_command("%s -g %s" % (self.lssrc_cmd, self.name))
            if rc == 1:
                if stderr:
                    self.module.fail_json(msg=stderr)
                else:
                    self.module.fail_json(msg=stdout)
            else:
                # Check all subsystem status, if one subsystem is not active
                # the group is considered not active.
                lines = stdout.splitlines()
                for state in lines[1:]:
                    if state.split()[-1].strip() != "active":
                        status = state.split()[-1].strip()
                        break
                else:
                    status = "active"

                # status is one of: active, inoperative
                return status
        else:
            lines = stdout.rstrip("\n").split("\n")
            status = lines[-1].split(" ")[-1]

            # status is one of: active, inoperative
            return status

    def service_control(self):

        # Check if service name is a subsystem of a group subsystem
        rc, stdout, stderr = self.execute_command("%s -a" % (self.lssrc_cmd))
        if rc == 1:
            if stderr:
                self.module.fail_json(msg=stderr)
            else:
                self.module.fail_json(msg=stdout)
        else:
            lines = stdout.splitlines()
            subsystems = []
            groups = []
            for line in lines[1:]:
                subsystem = line.split()[0].strip()
                group = line.split()[1].strip()
                subsystems.append(subsystem)
                if group:
                    groups.append(group)

            # Define if service name parameter:
            # -s subsystem or -g group subsystem
            if self.name in subsystems:
                srccmd_parameter = "-s"
            elif self.name in groups:
                srccmd_parameter = "-g"

        if self.action == 'start':
            srccmd = self.startsrc_cmd
        elif self.action == 'stop':
            srccmd = self.stopsrc_cmd
        elif self.action == 'reload':
            srccmd = self.refresh_cmd
        elif self.action == 'restart':
            self.execute_command("%s %s %s" % (self.stopsrc_cmd, srccmd_parameter, self.name))
            srccmd = self.startsrc_cmd

        if self.arguments and self.action == 'start':
            return self.execute_command("%s -a \"%s\" %s %s" % (srccmd, self.arguments, srccmd_parameter, self.name))
        else:
            return self.execute_command("%s %s %s" % (srccmd, srccmd_parameter, self.name))

# ===========================================
# Main control flow

def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(type='str', required=True),
            state=dict(type='str', choices=['started', 'stopped', 'reloaded', 'restarted']),
            sleep=dict(type='int'),
            pattern=dict(type='str'),
            enabled=dict(type='bool'),
            runlevel=dict(type='str', default='default'),
            arguments=dict(type='str', default='', aliases=['args']),
        ),
        supports_check_mode=True,
        required_one_of=[['state', 'enabled']],
    )

    service = Service(module)

    module.debug('Service instantiated - platform %s' % service.platform)
    if service.distribution:
        module.debug('Service instantiated - distribution %s' % service.distribution)

    rc = 0
    out = ''
    err = ''
    result = {}
    result['name'] = service.name

    # Find service management tools
    service.get_service_tools()

    # Enable/disable service startup at boot if requested
    if service.module.params['enabled'] is not None:
        # FIXME: ideally this should detect if we need to toggle the enablement state, though
        # it's unlikely the changed handler would need to fire in this case so it's a minor thing.
        service.service_enable()
        result['enabled'] = service.enable

    if module.params['state'] is None:
        # Not changing the running state, so bail out now.
        result['changed'] = service.changed
        module.exit_json(**result)

    result['state'] = service.state

    # Collect service status
    if service.pattern:
        service.check_ps()
    else:
        service.get_service_status()

    # Calculate if request will change service state
    service.check_service_changed()

    # Modify service state if necessary
    (rc, out, err) = service.modify_service_state()

    if rc != 0:
        if err and "Job is already running" in err:
            # upstart got confused, one such possibility is MySQL on Ubuntu 12.04
            # where status may report it has no start/stop links and we could
            # not get accurate status
            pass
        else:
            if err:
                module.fail_json(msg=err)
            else:
                module.fail_json(msg=out)

    result['changed'] = service.changed | service.svc_change
    if service.module.params['enabled'] is not None:
        result['enabled'] = service.module.params['enabled']

    if not service.module.params['state']:
        status = service.get_service_status()
        if status is None:
            result['state'] = 'absent'
        elif status is False:
            result['state'] = 'started'
        else:
            result['state'] = 'stopped'
    else:
        # as we may have just bounced the service the service command may not
        # report accurate state at this moment so just show what we ran
        if service.module.params['state'] in ['reloaded', 'restarted', 'started']:
            result['state'] = 'started'
        else:
            result['state'] = 'stopped'

    module.exit_json(**result)

if __name__ == '__main__':
    main()


text:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MarketplaceThemingMode : ThemingMode {
	#region ThemingMode implementation

	List<Material> solidColorMaterials = new List<Material>();

	public MarketplaceThemingMode() {
		solidColorMaterials.Add ((Material) Resources.Load ("SolidColorMaterials/White", typeof(Material)));
		solidColorMaterials.Add ((Material) Resources.Load ("SolidColorMaterials/Gelb", typeof(Material)));
		solidColorMaterials.Add ((Material) Resources.Load ("SolidColorMaterials/Green", typeof(Material)));
		solidColorMaterials.Add ((Material) Resources.Load ("SolidColorMaterials/Red", typeof(Material)));
		solidColorMaterials.Add ((Material) Resources.Load ("SolidColorMaterials/Blue", typeof(Material)));
	}

	public override string getTerrainName() {
		return "TerrainCity";
	}

	private Material getRandomSolidColorMaterial() {
		return solidColorMaterials [Random.Range (0, solidColorMaterials.Count)];
	}

	public override Material getOpenWallMaterial () {
		return (Material) Resources.Load ("SolidColorMaterials/Silver", typeof(Material));
	}

	public override Material getWallMaterial () {
		return getRandomSolidColorMaterial ();
	}

	public override Material getBoxMaterial () {
		return null;
	}

	public override Material getHouseMaterial () {
		return (Material) Resources.Load("House", typeof(Material));
	}

	public override Material getRoofMaterial () {
		return (Material) Resources.Load("Roof", typeof(Material));
	}

	public override Vector2 getTextureScaleForHeight (float height) {
		float y = 0;
		if (height < 5)
			y = height / 0.44f;
		else if
			(height < 7) y = height / 0.67f;
		else {
			int fulltextures = (int) height / 7;
			y = height / fulltextures;
		}
		return new Vector2 (0.5f, 1 / y);
	}

	#endregion
}

redpajama_set_name: RedPajamaGithub


text:
/**
 * This package contains interfaces for handling database metadata.
 **/
package interdroid.vdb.content.metadata;


text:
"use strict";

var Database = require("./database.js");
var db = new Database("MockDB");
var pr = require("../promise-ring.js");

pr.applyBound(db, db.query, [ "promise-ring" ])
  .then(function(result) {
    console.log(result);
  });


text:
package com.paypal.selion.appium.ios.sample;

import com.paypal.selion.annotations.MobileTest;
import com.paypal.selion.platform.mobile.elements.MobileTextField;
import com.paypal.selion.platform.mobile.ios.UIANavigationBar;
import com.paypal.selion.platform.mobile.ios.UIATextField;
import com.paypal.selion.platform.utilities.WebDriverWaitUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

/*
 * DEVNOTE Tests in this class exist primarily for demonstration purposes and as a basic sanity checks.
 */
public class AppiumIOSTextFieldTest {

    private static final String TEXT_FIELD_LOCATOR = "xpath=//UIAApplication[1]/UIAWindow[1]/UIATextField[2]";

    @Test
    @MobileTest(appPath = "src/test/resources/apps/PageObjects.app")
    public void testMobileTextField() {
        UIANavigationBar navigationBar = new UIANavigationBar(
                "xpath=//UIAApplication[1]/UIAWindow[1]/UIANavigationBar[1]");
        navigationBar.clickRightButton("//UIAApplication[1]/UIAWindow[1]/UIAButton[2]");
        MobileTextField textField = new UIATextField(TEXT_FIELD_LOCATOR);
        WebDriverWaitUtils.waitUntilElementIsVisible(TEXT_FIELD_LOCATOR);
        textField.setText("Selion");
        Assert.assertEquals(textField.getValue(), "Selion", "Set edit text value does not match");
        textField.clearText();
        Assert.assertEquals(textField.getValue(), "", "Set edit text value does not match");
        textField.sendKeys("Selion");
        Assert.assertEquals(textField.getValue(), "Selion", "Set edit text value does not match");
        textField.setText("123");
        Assert.assertEquals(textField.getValue(), "123", "Set edit text value does not match");
        textField.sendKeys("456");
        Assert.assertEquals(textField.getValue(), "123456", "Set edit text value does not match");
    }
}


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<title>ApiParams</title>
<meta name="date" content="2017-04-01">
<link rel="stylesheet" type="text/css" href="../../../../stylesheet.css" title="Style">

JavaScript is disabled on your browser.

Skip navigation links

Overview

Package

Class

Tree

Deprecated

Index

Help

Prev Class

Next Class

Frames

No Frames

All Classes

Summary:

Nested |

Field |

Constr |

Method

Detail:

Field |

Constr |

Method

cz.petrkubes.ioweyou.Pojos

Class ApiParams

java.lang.Object

cz.petrkubes.ioweyou.Pojos.ApiParams

public class ApiParams
extends java.lang.Object

Author:

Petr Kubes

Field Summary

Fields

Modifier and Type
Field and Description

SimpleCallback
callback

org.json.JSONObject
jsonToSend

Constructor Summary

Constructors

Constructor and Description

ApiParams()

Method Summary

Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Detail

jsonToSend

public org.json.JSONObject jsonToSend

callback

public SimpleCallback callback

Constructor Detail

ApiParams

public ApiParams()

Skip navigation links

Overview

Package

Class

Tree

Deprecated

Index

Help

Prev Class

Next Class

Frames

No Frames

All Classes

Summary:

Nested |

Field |

Constr |

Method

Detail:

Field |

Constr |

Method


text:
class SingleUndefined : public suic::Object
{
public:

    RTTIOfClass(SingleUndefined)

    SingleUndefined()
    {
    }

    SingleUndefined(const suic::String& name, const suic::String& val)
    {
        _name = name;
        _value = val;
    }

    suic::String GetName()
    {
        return _name;
    }

    suic::String GetValue()
    {
        return _value;
    }

private:

    suic::String _name;
    suic::String _value;
};

class NodeUndefined : public suic::Object
{
public:

    RTTIOfClass(NodeUndefined)

    NodeUndefined()
    {
    }

    NodeUndefined(suic::IXamlNode* pNode)
    {
        _holdItem.InitNode(pNode);
    }

    void CloneTo(NodeUndefined* obj);

    suic::String GetName()
    {
        return _holdItem.GetName();
    }

    String GetResXml(const String& offset)
    {
        return _holdItem.GetResXml(offset);
    }

private:

    HoldItem _holdItem;
};

class SingleUndefinedResNode : public ResNode
{
public:

    RTTIOfClass(SingleUndefinedResNode)

    SingleUndefinedResNode();
    SingleUndefinedResNode(SingleUndefined* val);

    virtual ~SingleUndefinedResNode();

    virtual void CloneNode(ResNodePtr& obj);
    virtual bool IsSingleValue();
    virtual void SetValue(suic::Object* val);
    virtual suic::Object* GetValue();

    virtual suic::String GetSingleXml();
    virtual suic::String GetResXml(const String& offset);

private:

    SingleUndefined* _value;
};

class NodeUndefinedResNode : public ResNode
{
public:

    RTTIOfClass(NodeUndefinedResNode)

    NodeUndefinedResNode();
    NodeUndefinedResNode(NodeUndefined* val);

    virtual ~NodeUndefinedResNode();

    virtual void CloneNode(ResNodePtr& obj);
    virtual bool IsSingleValue();
    virtual void SetValue(suic::Object* val);
    virtual suic::Object* GetValue();

    virtual suic::String GetSingleXml();
    virtual suic::String GetResXml(const String& offset);

private:

    NodeUndefined* _value;
};

#endif


text:
author: robmyers
comments: true
date: 2007-08-26 10:20:33+00:00
layout: post
slug: links-for-2007-08-25
title: links for 2007-08-25
wordpress_id: 1467
categories:
- links
---

  *

[Digital Productions: The Patron's Jukebox](http://www.digitalproductions.co.uk/index.php?id=82)

Some excellent music economics futurology from Crosbie.

(tags: [music](http://del.icio.us/robmyers/music) [futurology](http://del.icio.us/robmyers/futurology) [notes-towards-free-culture](http://del.icio.us/robmyers/notes-towards-free-culture) [free-cultur](http://del.icio.us/robmyers/free-cultur))

  *

[Digital Productions: Raining on CC & Buma/Stemra's parade](http://www.digitalproductions.co.uk/index.php?id=86)

A skeleton at the music collecting society feast. (I personally believe that voluntary reform can bear on unreformed projects and societies, so it is still a net positive.)

(tags: [collecting-societies](http://del.icio.us/robmyers/collecting-societies) [free-culture](http://del.icio.us/robmyers/free-culture) [critique](http://del.icio.us/robmyers/critique))

  *

[The Foundation for P2P Alternatives - P2P Foundation](http://www.p2pfoundation.net/Main_Page)

"We study the impact of Peer to Peer technology and thought on society"

(tags: [free-culture](http://del.icio.us/robmyers/free-culture) [p2p](http://del.icio.us/robmyers/p2p) [philosophy](http://del.icio.us/robmyers/philosophy) [studies](http://del.icio.us/robmyers/studies))

  *

[Conversation Agent: The Mass Participation Economy and Your Brand](http://www.conversationagent.com/2007/08/the-mass-partic.html)

"mass user-driven innovation is reshaping organizations, with users becoming increasingly involved as co-creators of products and services"

(tags: [user-innovation](http://del.icio.us/robmyers/user-innovation) [participatory](http://del.icio.us/robmyers/participatory) [brands](http://del.icio.us/robmyers/brands))

  *

[We-think: The power of mass creativity - Charles Leadbeater](http://www.wethinkthebook.net/home.aspx)

"We-Think: the power of mass creativity is about what the rise of the likes of Wikipedia and Youtube, Linux and Craigslist means for the way we organise ourselves, not just in digital businesses but in schools and hospitals, cities and mainstream corporat

(tags: [free-culture](http://del.icio.us/robmyers/free-culture) [brands](http://del.icio.us/robmyers/brands) [collaborative](http://del.icio.us/robmyers/collaborative) [consumer-innovation](http://del.icio.us/robmyers/consumer-innovation))

  *

[Boing Boing: Microsoft WGA servers down; all XP and Vista installs being marked as counterfeit](http://www.boingboing.net/2007/08/25/microsoft_wga_server.html)

"Every single Windows XP and Vista installation -- except possibly those with volume license keys -- is being marked as counterfeit"

(tags: [windows](http://del.icio.us/robmyers/windows) [drm](http://del.icio.us/robmyers/drm) [notes](http://del.icio.us/robmyers/notes) [free-software](http://del.icio.us/robmyers/free-software) [hahahahahahaha](http://del.icio.us/robmyers/hahahahahahaha))


text:
"use strict";

TopJs.namespace("TopJs.eventmanager");

/**
 * @class
 * @classdesc
 *
 * Interface indicating that an object composes or can compose a
 * SharedEventManagerInterface instance.
 */
class SharedEventsCapableInterface {
    /**
     * Retrieve the shared event manager, if composed.
     *
     * @return {null|TopJs.eventmanager.SharedEventManagerInterface}
     */
    getSharedManager()
    {
    }
}

TopJs.registerClass("TopJs.eventmanager.SharedEventsCapableInterface", SharedEventsCapableInterface);
module.exports = SharedEventsCapableInterface;


text:
package net.anotheria.moskito.web.filters;

import net.anotheria.moskito.web.MoskitoFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * This filter counts the requests by the domain.
 * @author lrosenberg
 * @deprecated use GenericMonitoringFilter instead.
 *
 */
@Deprecated
public class DomainFilter extends MoskitoFilter {

	@Override
	protected String extractCaseName(ServletRequest req, ServletResponse res) {
		return req.getServerName();
	}
}


text:
using System.Collections.Generic;
using GtfsNet.Entities;
using GtfsNet.Visitors;

namespace GtfsNet.Collections
{
	public class StopCollection : HashSet<Stop>, IFeedElement
	{
		public StopCollection()
		{
		}

		public StopCollection(IEnumerable<Stop> collection) : base(collection)
		{
		}

		public void Accept(IFeedVisitor visitor)
		{
			visitor.Visit(this);
		}
	}
}


text:
require 'array/window'

describe 'Array window' do

  describe 'basic scenarios' do

    it "should perform the right calculation for an odd window size" do
      (0..199).to_a.window(5, 100).should == (98..102).to_a
    end

  end

  describe 'even size resolution' do

    it "should perform the right calculation for an even window size when prioritizing greater elements" do
      (0..653).to_a.window(10, 320, :prioritize_greater).should == (316..325).to_a
    end

    it "should perform the right calculation for an even window size when prioritizing lower elements" do
      (0..653).to_a.window(10, 320, :prioritize_lower).should == (315..324).to_a
    end

  end

  describe 'edge window size scenarios' do

    it "should return an empty array when the size is zero" do
      (0..1023).to_a.window(0, 680).should == []
    end

    it "should return a single element array when the size is one" do
      (0..1023).to_a.window(1, 680).should == [680]
    end

    it "should perform the right calculation when the window size is two" do
      (0..15).to_a.window(2, 10).should == [10, 11]
    end

    it "should return the whole array when the window size is greater than the array size" do
      (0..3).to_a.window(8, 0).should == (0..3).to_a
    end

  end

  describe 'edge windows scenarios' do

    it "should return the latest elements when the center is too close to the end of the array" do
      (0..19).to_a.window(9, 17).should == (11..19).to_a
    end

    it "should return the first elements when the center is too close to the beginning of the array" do
      (0..19).to_a.window(9, 2).should == (0..8).to_a
    end

  end

end


text:
package com.adaptris.core.marshaller.xstream;

import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.javabean.BeanProvider;
import com.thoughtworks.xstream.converters.javabean.JavaBeanConverter;
import com.thoughtworks.xstream.converters.javabean.JavaBeanProvider;
import com.thoughtworks.xstream.converters.reflection.MissingFieldException;
import com.thoughtworks.xstream.core.util.FastField;
import com.thoughtworks.xstream.io.ExtendedHierarchicalStreamWriterHelper;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.CannotResolveClassException;
import com.thoughtworks.xstream.mapper.Mapper;

/**
 * XStream converter class for our registered JavaBean classes defined in javabeans.properties.
 * <p>
 * This class Provides the same functionality as {@link AliasedElementReflectionConverter} but for javabeans.
 * </p>
 *
 * @author bklair
 */
public class AliasedJavaBeanConverter extends JavaBeanConverter {

  protected transient Logger log = LoggerFactory.getLogger(AliasedJavaBeanConverter.class);

  public AliasedJavaBeanConverter(Mapper mapper) {
    super(mapper);
  }

  public AliasedJavaBeanConverter(Mapper mapper, Class type) {
      super(mapper, new BeanProvider(), type);
  }

  /**
   * Marshal the given class to XML representation
   */
  @Override
  public void marshal(final Object source, final HierarchicalStreamWriter writer, final MarshallingContext context) {
    final String classAttributeName = mapper.aliasForSystemAttribute("class");

    beanProvider.visitSerializableProperties(source,
        new JavaBeanProvider.Visitor() {
          public boolean shouldVisit(String name, Class definedIn) {
            return mapper.shouldSerializeMember(definedIn, name);
          }

          public void visit(String propertyName, Class fieldType, Class definedIn, Object newObj) {
            if (newObj != null) {
              writeField(propertyName, fieldType, newObj, definedIn);
            }
          }

//          private void writeFieldOld(String propertyName, Class fieldType, Object newObj, Class definedIn) {
//            Class actualType = newObj.getClass();
//            Class defaultType = mapper.defaultImplementationOf(fieldType);
//            String serializedMember = mapper.serializedMember(
//                source.getClass(), propertyName);
//
//            ExtendedHierarchicalStreamWriterHelper.startNode(writer, serializedMember, actualType);
//            if (!actualType.equals(defaultType) && classAttributeName != null) {
//              writer.addAttribute(classAttributeName, mapper.serializedClass(actualType));
//            }
//            context.convertAnother(newObj);
//
//            writer.endNode();
//          }

          // Modified version of method from that super class
          private void writeField(String propertyName, Class fieldType, Object newObj, Class definedIn) {
            Class actualType = newObj.getClass();
            Class defaultType = mapper.defaultImplementationOf(fieldType);
            String serializedMember = mapper.serializedMember(source.getClass(), propertyName);

            // Now where the super class would have written out the field name
            // followed by a class attribute that contained the subclass name,
            // we will write out the subclass alias name as the element with no
            // class attribute at all.
            boolean writeOutSubclassAsElementInsteadOfAttribute = false;
            if (!actualType.equals(defaultType) && classAttributeName != null) {
              writeOutSubclassAsElementInsteadOfAttribute = true;
            }

            if (writeOutSubclassAsElementInsteadOfAttribute) {
              String serializedClassName = mapper.serializedClass(actualType); // Get the alias value of the subclass
              ExtendedHierarchicalStreamWriterHelper.startNode(writer, serializedClassName, actualType);
            }
            else {
              ExtendedHierarchicalStreamWriterHelper.startNode(writer, serializedMember, actualType);
            }
            context.convertAnother(newObj);

            writer.endNode();
          }
        });
  }

  /**
   * Unmarshall the XML data from the reader and create the object instance.
   */
  @Override
  public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
    final Object result = instantiateNewInstance(context);
    final Set<FastField> seenProperties = new HashSet() {
        public boolean add(Object e) {
            if (!super.add(e)) {
                throw new DuplicatePropertyException(((FastField)e).getName());
            }
            return true;
        }
    };

    Class<?> parentClass = result.getClass();
    Class<?> fieldClass = null;
    while (reader.hasMoreChildren()) {
      reader.moveDown();
      boolean propertyProcessed = false;

      String currentNodeName = reader.getNodeName();
      String propertyName = mapper.realMember(parentClass, currentNodeName);

      log.trace("Unmarshalling xml element: {}, processing as property name: {} for class: {}", currentNodeName, propertyName, parentClass.getSimpleName());
      if (mapper.shouldSerializeMember(parentClass, propertyName)) {
        // Is this a valid property for the current class
        boolean propertyExistsInClass = false;
        try {
          propertyExistsInClass = beanProvider.propertyDefinedInClass(propertyName, parentClass);
        } catch (MissingFieldException mfe) {
          propertyExistsInClass = false;
        }

        // Handle the normal case where we have a valid property and process it's value
        if (propertyExistsInClass) {
          Class<?> type = determineType(reader, result, propertyName);
          Object value = context.convertAnother(result, type);
          beanProvider.writeProperty(result, propertyName, value);
          seenProperties.add(new FastField(parentClass, propertyName));
          propertyProcessed = true;
        }
      }

      // If the current element has not been matched then it might be of the new format: where the subclass is used as the element node instead of the field name
      if (!propertyProcessed) {
        log.trace("Property: {} not valid for class: {}, attempting resolving property name", propertyName, parentClass.getSimpleName());

        // Try to resolve the current node as an aliases field
        try {
//          String fieldName = mapper.realMember(parentClass, currentNodeName);
          fieldClass = mapper.realClass(propertyName);
        } catch (CannotResolveClassException e) {
          // Try to resolve the current node as an aliased class instead
          fieldClass = mapper.realClass(currentNodeName);
        }

        Field matchedField = XStreamUtils.getMatchedFieldFromClass(parentClass, fieldClass, seenProperties);
        if (matchedField != null) {
          String fieldCapturedName = matchedField.getName();
          Class<?> type = fieldClass;
           Object value = context.convertAnother(result, type);
//          Object value = beanProvider.newInstance(type);
          log.trace("Setting field: {} to class: {}", fieldCapturedName, value.getClass());
          beanProvider.writeProperty(result, fieldCapturedName, value);
          seenProperties.add(new FastField(parentClass, fieldCapturedName));
        }
        // OK we've tried all we can, clearly this must be an invalid field in the xml - abort
        else {
          throw new MissingFieldException(parentClass.getName(), propertyName);
        }
      }
      reader.moveUp();
    }
    return result;
  }

  private Object instantiateNewInstance(UnmarshallingContext context) {
    Object result = context.currentObject();
    if (result == null) {
      result = beanProvider.newInstance(context.getRequiredType());
    }
    return result;
  }

  private Class determineType(HierarchicalStreamReader reader, Object result, String fieldName) {
    final String classAttributeName = mapper.aliasForSystemAttribute("class");
    String classAttribute = classAttributeName == null ? null : reader.getAttribute(classAttributeName);
    if (classAttribute != null) {
      return mapper.realClass(classAttribute);
    }
    else {
      return mapper.defaultImplementationOf(beanProvider.getPropertyType(result, fieldName));
    }
  }

}


text:
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    vector<int> sig1, sig2;
public:
    void sequence(vector<int>& seq, TreeNode *root) {
        if (root == nullptr) {return;}
        if (root->left == nullptr && root->right == nullptr) {seq.push_back(root->val);}
        sequence(seq, root->left);
        sequence(seq, root->right);
    }

    bool leafSimilar(TreeNode* root1, TreeNode* root2) {
        sequence(sig1, root1);
        sequence(sig2, root2);
        return (sig1 == sig2);
    }
};


text:
skip_files = []
excluded = ["c4log", "c4vlog", "c4error_getMessageC", "c4str", "c4log_getDomain","c4repl_parseURL","c4SliceEqual","c4slice_free"]
force_no_bridge = ["c4repl_getResponseHeaders","c4repl_new","c4socket_gotHTTPResponse"]
default_param_name = {"C4SliceResult":"slice","C4WriteStream*":"stream","C4ReadStream*":"stream","C4Error*":"outError",
"C4BlobStore*":"store","C4BlobKey":"key","C4Slice":"slice","C4Key*":"key","bool":"b","double":"d","C4KeyReader*":"reader",
"C4DatabaseObserver*":"observer","C4DocumentObserver*":"observer","C4View*":"view","C4OnCompactCallback":"callback",
"C4Database*":"db","C4SequenceNumber":"sequence","C4StringResult":"str","C4String":"str","C4Query*":"query","C4CertNameAttributeID":"y"}
param_bridge_types = ["C4Slice", "size_t", "size_t*","C4Slice[]", "C4String", "C4String[]"]
return_bridge_types = ["C4SliceResult", "C4Slice", "size_t", "byte*", "C4StringResult", "C4String"]
type_map = {"int32_t":"int","uint32_t":"uint","int64_t":"long","uint64_t":"ulong","size_t":"UIntPtr",
            "size_t*":"UIntPtr*","C4SequenceNumber":"ulong","C4SequenceNumber*":"ulong*","unsigned":"uint",
            "FLSharedKeys":"FLSharedKeys*","char*":"byte*","FLEncoder":"FLEncoder*","C4LogDomain":"C4LogDomain*",
            "FLDict":"FLDict*","C4FullTextID":"ulong","C4RemoteID":"uint","C4String*":"FLSlice*","FLDoc":"FLDoc*",
            "C4Timestamp": "long", "C4Timestamp*": "long*","FLMutableArray":"FLMutableArray*", "uint16_t":"ushort",
			"unsigned*":"uint*","C4CertNameAttributeID":"FLSlice"}
reserved = ["string","params","ref"]


text:
package mixr.demo.driver.picproc;

import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.List;
import mixr.MixR;
import mixr.components.GoalTransformer;
import mixr.isabelle.terms.StringFormat;
import mixr.logic.AutomatedInferenceRule;
import mixr.logic.Formula;
import mixr.logic.FormulaRepresentation;
import mixr.logic.GoalTransformationResult;
import mixr.logic.InferenceRule;
import mixr.logic.InferenceRuleDescriptor;
import mixr.logic.InferenceStepResult;
import mixr.logic.InferenceTarget;
import mixr.logic.InferenceTargets;
import mixr.logic.Sentence;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;

/**
 * A simple inference rule that extracts the shape of the object from the image.
 *
 *
This inference rule counts the number of non-transparent pixels (with
 * opacity lower than 50%) and returns one of the three:
 *
 *

 *
 * <il>Triangle (equilateral)</il>
 *
 * <il>Square</il>
 *
 * <il>Circle</il>
 *
 *

 *
 *

 *
 * @author Matej Urbas [matej.urbas@gmail.com]
 */
public class AreaInference implements InferenceRuleDescriptor, InferenceRule, AutomatedInferenceRule {

    private final GoalTransformer owner;

    AreaInference(GoalTransformer owner) {
        this.owner = owner;
    }

    @Override
    @NbBundle.Messages({
        "AI_name=Get object area"
    })
    public String getName() {
        return Bundle.AI_name();
    }

    @Override
    @NbBundle.Messages({
        "AI_description=Extracts the area of the single object in the image."
    })
    public String getDescription() {
        return Bundle.AI_description();
    }

    @Override
    public boolean isFullyAutomated() {
        return true;
    }

    @Override
    public GoalTransformer getOwner() {
        return owner;
    }

    @Override
    public void applyInferenceRule(InferenceTargets targets) {
        InferenceStepResult isr = applyAutomatedInferenceRule(targets);
        // Put the result back to the master reasoner:
        Lookup.getDefault().lookup(MixR.class).getGoalManager().commitTransformedGoals(isr);
    }

    @Override
    public InferenceStepResult applyAutomatedInferenceRule(InferenceTargets targets) {
        final int targetsCount = targets.getInferenceTargetsCount();
        List<Sentence>[] transformedSentences = new List[targetsCount];
        InferenceTarget[] transformedTargets = new InferenceTarget[targetsCount];
        for (int i = 0; i < targetsCount; i++) {
            Sentence s = targets.getSentenceAt(i);
            try {
                Formula f = s.asFormula();
                if (f != null) {
                    // Apply the inference rule on this formula:
                    FormulaRepresentation imageRepresentation = f.getRepresentation(ImageUrlFormat.getInstance());
                    if (imageRepresentation != null && imageRepresentation.getFormula() instanceof ImageUrlFormula) {
                        ImageUrlFormula imageUrlFormula = (ImageUrlFormula) imageRepresentation.getFormula();
                        int area = extractArea(imageUrlFormula);
                        Formula inferedFormula = new Formula(StringFormat.createFormula("AreaOf " + getFreeVariableName(imageRepresentation, imageUrlFormula) + " = " + area), f.getRole());
                        transformedSentences[i] = Arrays.asList((Sentence) inferedFormula);
                        transformedTargets[i] = targets.getInferenceTargets().get(i);
                    }
                }
            } catch (UnsupportedOperationException uoe) {
            }
        }
        GoalTransformationResult goalTransformationResult = GoalTransformationResult.create(getOwner(), targets, transformedTargets, transformedSentences);
        return goalTransformationResult;
    }

    private int extractArea(ImageUrlFormula imageUrlFormula) {
        BufferedImage image = imageUrlFormula.getImage();
        int insidePixels = 0;
        int outlinePixels = 0;
        for (int i = 0; i < image.getWidth(); i++) {
            for (int j = 0; j < image.getHeight(); j++) {
                int pixelColour = image.getRGB(i, j);
                // Get the alpha component:
                int alpha = (pixelColour & 0xff000000) >>> 24;
                if (alpha >= 0xf9) {
                    insidePixels++;
                } else if (alpha > 0x05) {
                    outlinePixels++;
                }
            }
        }
        return insidePixels + outlinePixels;
    }

    public static String getFreeVariableName(ImageUrlFormula imageUrlFormula) {
        return imageUrlFormula.getName();
    }

    public static String getFreeVariableName(FormulaRepresentation imageRepresentation, ImageUrlFormula imageUrlFormula) {
        if (imageRepresentation.getFreeVariables() == null || imageRepresentation.getFreeVariables().isEmpty())
            return getFreeVariableName(imageUrlFormula);
        else
            return imageRepresentation.getFreeVariables().iterator().next().getName();
    }
}


text:
package terminals

import (
	"fmt"
	"os"
	"os/exec"
	"strings"

	"github.com/giancosta86/caravel"
)

/*
BashTerminal is a terminal provided by the Bash shell
*/
type BashTerminal struct {
	AnsiTerminal
}

/*
NewBashTerminal creates an ANSI terminal implemented by the Bash shell running the program
*/
func NewBashTerminal() *BashTerminal {
	rows, columns := getBashTerminalSize()

	return &BashTerminal{
		AnsiTerminal{
			rows:    rows,
			columns: columns,
		},
	}
}

func getBashTerminalSize() (rows int, columns int) {
	const defaultRows = 25
	const defaultColumns = 80

	sizeCommand := exec.Command("stty", "size")
	sizeCommand.Stdin = os.Stdin

	sizeOutputString, err := caravel.GetCommandOutputString(sizeCommand)
	if err != nil {
		return defaultRows, defaultColumns
	}

	sizeOutputString = strings.TrimSpace(sizeOutputString)
	_, err = fmt.Sscanf(sizeOutputString, "%d %d", &rows, &columns)
	if err != nil {
		return defaultRows, defaultColumns
	}

	return rows, columns
}


text:
module.exports = {
  description: "",
  ns: "react-material-ui",
  type: "ReactNode",
  dependencies: {
    npm: {
      "material-ui/svg-icons/image/add-to-photos": require('material-ui/svg-icons/image/add-to-photos')
    }
  },
  name: "ImageAddToPhotos",
  ports: {
    input: {},
    output: {
      component: {
        title: "ImageAddToPhotos",
        type: "Component"
      }
    }
  }
}


text:
@interface ViewController ()

@property (nonatomic, strong) RHAudioRecorder *recorder;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

//    RHAudioMp3Output *output = [[RHAudioMp3Output alloc] init];
//    RHAudioAmrOutput *output = [[RHAudioAmrOutput alloc] init];
    RHAudioCafOutput *output = [[RHAudioCafOutput alloc] init];

    _recorder = [[RHAudioRecorder alloc] init];
    _recorder.output = output;
    [_recorder start];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


text:
package jp.ac.titech.cs.se.nlp.db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import jp.ac.titech.cs.se.nlp.entity.CaseFrame;

/**
 * 日本語共起関係テーブルのDAO
 *
 * @author rtakizawa
 */
public class JCPDao {

    public static final String ID = "id";
    public static final String NAME_COL = "name";
    public static final String ACT_NAME_COL = "act_name";
    public static final String ACT_CONCEPT_COL = "act_concept";
    public static final String SLOT_COL = "slotinfo";

    /**
     * コンセプトIDのリストに対し、候補となる格フレームのリストを返す。
     *
     * @param conceptIds
     * @return
     */
    public List<CaseFrame> getCaseFrameList(List<Integer> conceptIds) {
        List<CaseFrame> frames = new ArrayList<CaseFrame>();
        try (ResourceManagedConnection con = DBManager.connect()) {
            String query = "select " + ACT_NAME_COL + ", " + SLOT_COL + " from jcp where "
                    + ACT_CONCEPT_COL + " = ?;";
            PreparedStatement ps = con.prepareStatement(query);
            for (int id : conceptIds) {
                ps.setInt(1, id);
                ResultSet result = ps.executeQuery();
                while (result.next()) {
                    String verb = result.getString(ACT_NAME_COL);
                    String slot = result.getString(SLOT_COL);
                    frames.add(new CaseFrame(verb, slot));
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return frames;
    }
}


text:
using System;
using System.Threading;
using NServiceBus;
using NServiceBus.Unicast.Transport;
using StructureMap;

namespace TestRunner {
    class Program {
        static void Main(string[] args) {
            System.Transactions.TransactionManager.DistributedTransactionStarted += TransactionManager_DistributedTransactionStarted;

            var bus = Configure.With()
                .Log4Net()
                .StructureMapBuilder()
                .XmlSerializer()
                .UnicastBus()
                    .DoNotAutoSubscribe()
                    .LoadMessageHandlers()
                .ServiceBrokerTransport()
                    .ConnectionString(@"Server=.\SQLEXPRESS;Database=ServiceBroker_HelloWorld;Trusted_Connection=True;")
                    .ErrorService("ErrorService")
                      .CreateBus()
                .Start();

            bus.Send("ServiceA", new TestMessage() {
                Content = "Hello World - Send()",
            }).Register<TestMessage>(Console.WriteLine);

            bus.SendLocal(new TestMessage() {
                Content = "Hello World - SendLocal()",
            });

            while (true)
                Thread.Sleep(100);
        }

        static void TransactionManager_DistributedTransactionStarted(object sender, System.Transactions.TransactionEventArgs e) {
            Console.WriteLine("Distributed Transaction Started");
        }
    }

    [Serializable]
    public class TestMessage : IMessage {
        public string Content { get; set; }
    }

    public class TestMessageHandler : IMessageHandler<TestMessage>
    {

        public IBus Bus { get; set; }

        public void Handle(TestMessage message)
        {
            Bus.Return(42);
            throw new Exception("Testing Exception Management");
        }
    }

}