Instruction:
Here you have a function in Python 3, explain what it does
def call_listen(self, chunks, running):
        '''
        Find all of the listen routines and call the associated mod_watch runs
        '''
        listeners = []
        crefs = {}
        for chunk in chunks:
            crefs[(chunk['state'], chunk['__id__'], chunk['name'])] = chunk
            if 'listen' in chunk:
                listeners.append({(chunk['state'], chunk['__id__'], chunk['name']): chunk['listen']})
            if 'listen_in' in chunk:
                for l_in in chunk['listen_in']:
                    for key, val in six.iteritems(l_in):
                        listeners.append({(key, val, 'lookup'): [{chunk['state']: chunk['__id__']}]})
        mod_watchers = []
        errors = {}
        for l_dict in listeners:
            for key, val in six.iteritems(l_dict):
                for listen_to in val:
                    if not isinstance(listen_to, dict):
                        found = False
                        for chunk in chunks:
                            if chunk['__id__'] == listen_to or \
                               chunk['name'] == listen_to:
                                listen_to = {chunk['state']: chunk['__id__']}
                                found = True
                        if not found:
                            continue
                    for lkey, lval in six.iteritems(listen_to):
                        if not any(lkey == cref[0] and lval in cref for cref in crefs):
                            rerror = {_l_tag(lkey, lval):
                                      {
                                          'comment': 'Referenced state {0}: {1} does not exist'.format(lkey, lval),
                                          'name': 'listen_{0}:{1}'.format(lkey, lval),
                                          'result': False,
                                          'changes': {}
                                      }}
                            errors.update(rerror)
                            continue
                        to_tags = [
                            _gen_tag(data) for cref, data in six.iteritems(crefs) if lkey == cref[0] and lval in cref
                        ]
                        for to_tag in to_tags:
                            if to_tag not in running:
                                continue
                            if running[to_tag]['changes']:
                                if not any(key[0] == cref[0] and key[1] in cref for cref in crefs):
                                    rerror = {_l_tag(key[0], key[1]):
                                                 {'comment': 'Referenced state {0}: {1} does not exist'.format(key[0], key[1]),
                                                  'name': 'listen_{0}:{1}'.format(key[0], key[1]),
                                                  'result': False,
                                                  'changes': {}}}
                                    errors.update(rerror)
                                    continue

                                new_chunks = [data for cref, data in six.iteritems(crefs) if key[0] == cref[0] and key[1] in cref]
                                for chunk in new_chunks:
                                    low = chunk.copy()
                                    low['sfun'] = chunk['fun']
                                    low['fun'] = 'mod_watch'
                                    low['__id__'] = 'listener_{0}'.format(low['__id__'])
                                    for req in STATE_REQUISITE_KEYWORDS:
                                        if req in low:
                                            low.pop(req)
                                    mod_watchers.append(low)
        ret = self.call_chunks(mod_watchers)
        running.update(ret)
        for err in errors:
            errors[err]['__run_num__'] = self.__run_num
            self.__run_num += 1
        running.update(errors)
        return running

Response: Find all of the listen routines and call the associated mod_watch runs.


Instruction:
Here you have a function in Python 3, explain what it does
def inject_default_call(self, high):
        '''
        Sets .call function to a state, if not there.

        :param high:
        :return:
        '''
        for chunk in high:
            state = high[chunk]
            if not isinstance(state, collections.Mapping):
                continue
            for state_ref in state:
                needs_default = True
                if not isinstance(state[state_ref], list):
                    continue
                for argset in state[state_ref]:
                    if isinstance(argset, six.string_types):
                        needs_default = False
                        break
                if needs_default:
                    state[state_ref].insert(-1, '__call__')

Response: Injects a default call function to a state if not there.


Instruction:
Can you tell what is the following Python 3 function doing
def call_high(self, high, orchestration_jid=None):
        '''
        Process a high data call and ensure the defined states.
        '''
        self.inject_default_call(high)
        errors = []
        # If there is extension data reconcile it
        high, ext_errors = self.reconcile_extend(high)
        errors.extend(ext_errors)
        errors.extend(self.verify_high(high))
        if errors:
            return errors
        high, req_in_errors = self.requisite_in(high)
        errors.extend(req_in_errors)
        high = self.apply_exclude(high)
        # Verify that the high data is structurally sound
        if errors:
            return errors
        # Compile and verify the raw chunks
        chunks = self.compile_high_data(high, orchestration_jid)

        # If there are extensions in the highstate, process them and update
        # the low data chunks
        if errors:
            return errors
        ret = self.call_chunks(chunks)
        ret = self.call_listen(chunks, ret)

        def _cleanup_accumulator_data():
            accum_data_path = os.path.join(
                get_accumulator_dir(self.opts['cachedir']),
                self.instance_id
            )
            try:
                os.remove(accum_data_path)
                log.debug('Deleted accumulator data file %s', accum_data_path)
            except OSError:
                log.debug('File %s does not exist, no need to cleanup', accum_data_path)
        _cleanup_accumulator_data()
        if self.jid is not None:
            pause_path = os.path.join(self.opts['cachedir'], 'state_pause', self.jid)
            if os.path.isfile(pause_path):
                try:
                    os.remove(pause_path)
                except OSError:
                    # File is not present, all is well
                    pass

        return ret

Response: Process a high data call and ensure the defined states.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def call_template(self, template):
        '''
        Enforce the states in a template
        '''
        high = compile_template(template,
                                self.rend,
                                self.opts['renderer'],
                                self.opts['renderer_blacklist'],
                                self.opts['renderer_whitelist'])
        if not high:
            return high
        high, errors = self.render_template(high, template)
        if errors:
            return errors
        return self.call_high(high)

Response: Call the template and return the result.


Instruction:
Implement a Python 3 function for
enforcing the states in a template pass the template as a string

Response:
def call_template_str(self, template):
        '''
        Enforce the states in a template, pass the template as a string
        '''
        high = compile_template_str(template,
                                    self.rend,
                                    self.opts['renderer'],
                                    self.opts['renderer_blacklist'],
                                    self.opts['renderer_whitelist'])
        if not high:
            return high
        high, errors = self.render_template(high, '<template-str>')
        if errors:
            return errors
        return self.call_high(high)


Instruction:
Write a Python 3 function for
generating the options used by the high state object.

Response:
def __gen_opts(self, opts):
        '''
        The options used by the High State object are derived from options
        on the minion and the master, or just the minion if the high state
        call is entirely local.
        '''
        # If the state is intended to be applied locally, then the local opts
        # should have all of the needed data, otherwise overwrite the local
        # data items with data from the master
        if 'local_state' in opts:
            if opts['local_state']:
                return opts
        mopts = self.client.master_opts()
        if not isinstance(mopts, dict):
            # An error happened on the master
            opts['renderer'] = 'jinja|yaml'
            opts['failhard'] = False
            opts['state_top'] = salt.utils.url.create('top.sls')
            opts['nodegroups'] = {}
            opts['file_roots'] = {'base': [syspaths.BASE_FILE_ROOTS_DIR]}
        else:
            opts['renderer'] = mopts['renderer']
            opts['failhard'] = mopts.get('failhard', False)
            if mopts['state_top'].startswith('salt://'):
                opts['state_top'] = mopts['state_top']
            elif mopts['state_top'].startswith('/'):
                opts['state_top'] = salt.utils.url.create(mopts['state_top'][1:])
            else:
                opts['state_top'] = salt.utils.url.create(mopts['state_top'])
            opts['state_top_saltenv'] = mopts.get('state_top_saltenv', None)
            opts['nodegroups'] = mopts.get('nodegroups', {})
            opts['state_auto_order'] = mopts.get(
                'state_auto_order',
                opts['state_auto_order'])
            opts['file_roots'] = mopts['file_roots']
            opts['top_file_merging_strategy'] = mopts.get('top_file_merging_strategy',
                                                           opts.get('top_file_merging_strategy'))
            opts['env_order'] = mopts.get('env_order', opts.get('env_order', []))
            opts['default_top'] = mopts.get('default_top', opts.get('default_top'))
            opts['state_events'] = mopts.get('state_events')
            opts['state_aggregate'] = mopts.get('state_aggregate', opts.get('state_aggregate', False))
            opts['jinja_env'] = mopts.get('jinja_env', {})
            opts['jinja_sls_env'] = mopts.get('jinja_sls_env', {})
            opts['jinja_lstrip_blocks'] = mopts.get('jinja_lstrip_blocks', False)
            opts['jinja_trim_blocks'] = mopts.get('jinja_trim_blocks', False)
        return opts


Instruction:
Can you implement a function in Python 3 that
gets the file server environments out of the master options and the master client options.

Response:
def _get_envs(self):
        '''
        Pull the file server environments out of the master options
        '''
        envs = ['base']
        if 'file_roots' in self.opts:
            envs.extend([x for x in list(self.opts['file_roots'])
                         if x not in envs])
        env_order = self.opts.get('env_order', [])
        # Remove duplicates while preserving the order
        members = set()
        env_order = [env for env in env_order if not (env in members or members.add(env))]
        client_envs = self.client.envs()
        if env_order and client_envs:
            return [env for env in env_order if env in client_envs]

        elif env_order:
            return env_order
        else:
            envs.extend([env for env in client_envs if env not in envs])
            return envs


Instruction:
Here you have a function in Python 3, explain what it does
def get_tops(self):
        '''
        Gather the top files
        '''
        tops = DefaultOrderedDict(list)
        include = DefaultOrderedDict(list)
        done = DefaultOrderedDict(list)
        found = 0  # did we find any contents in the top files?
        # Gather initial top files
        merging_strategy = self.opts['top_file_merging_strategy']
        if merging_strategy == 'same' and not self.opts['saltenv']:
            if not self.opts['default_top']:
                raise SaltRenderError(
                    'top_file_merging_strategy set to \'same\', but no '
                    'default_top configuration option was set'
                )

        if self.opts['saltenv']:
            contents = self.client.cache_file(
                self.opts['state_top'],
                self.opts['saltenv']
            )
            if contents:
                found = 1
                tops[self.opts['saltenv']] = [
                    compile_template(
                        contents,
                        self.state.rend,
                        self.state.opts['renderer'],
                        self.state.opts['renderer_blacklist'],
                        self.state.opts['renderer_whitelist'],
                        saltenv=self.opts['saltenv']
                    )
                ]
            else:
                tops[self.opts['saltenv']] = [{}]

        else:
            found = 0
            state_top_saltenv = self.opts.get('state_top_saltenv', False)
            if state_top_saltenv \
                    and not isinstance(state_top_saltenv, six.string_types):
                state_top_saltenv = six.text_type(state_top_saltenv)

            for saltenv in [state_top_saltenv] if state_top_saltenv \
                    else self._get_envs():
                contents = self.client.cache_file(
                    self.opts['state_top'],
                    saltenv
                )
                if contents:
                    found = found + 1
                    tops[saltenv].append(
                        compile_template(
                            contents,
                            self.state.rend,
                            self.state.opts['renderer'],
                            self.state.opts['renderer_blacklist'],
                            self.state.opts['renderer_whitelist'],
                            saltenv=saltenv
                        )
                    )
                else:
                    tops[saltenv].append({})
                    log.debug('No contents loaded for saltenv \'%s\'', saltenv)

            if found > 1 and merging_strategy == 'merge' and not self.opts.get('env_order', None):
                log.warning(
                    'top_file_merging_strategy is set to \'%s\' and '
                    'multiple top files were found. Merging order is not '
                    'deterministic, it may be desirable to either set '
                    'top_file_merging_strategy to \'same\' or use the '
                    '\'env_order\' configuration parameter to specify the '
                    'merging order.', merging_strategy
                )

        if found == 0:
            log.debug(
                'No contents found in top file. If this is not expected, '
                'verify that the \'file_roots\' specified in \'etc/master\' '
                'are accessible. The \'file_roots\' configuration is: %s',
                repr(self.state.opts['file_roots'])
            )

        # Search initial top files for includes
        for saltenv, ctops in six.iteritems(tops):
            for ctop in ctops:
                if 'include' not in ctop:
                    continue
                for sls in ctop['include']:
                    include[saltenv].append(sls)
                ctop.pop('include')
        # Go through the includes and pull out the extra tops and add them
        while include:
            pops = []
            for saltenv, states in six.iteritems(include):
                pops.append(saltenv)
                if not states:
                    continue
                for sls_match in states:
                    for sls in fnmatch.filter(self.avail[saltenv], sls_match):
                        if sls in done[saltenv]:
                            continue
                        tops[saltenv].append(
                            compile_template(
                                self.client.get_state(
                                    sls,
                                    saltenv
                                ).get('dest', False),
                                self.state.rend,
                                self.state.opts['renderer'],
                                self.state.opts['renderer_blacklist'],
                                self.state.opts['renderer_whitelist'],
                                saltenv
                            )
                        )
                        done[saltenv].append(sls)
            for saltenv in pops:
                if saltenv in include:
                    include.pop(saltenv)
        return tops

Response: Gather the top files and their contents


Instruction:
Can you tell what is the following Python 3 function doing
def _merge_tops_merge(self, tops):
        '''
        The default merging strategy. The base env is authoritative, so it is
        checked first, followed by the remaining environments. In top files
        from environments other than "base", only the section matching the
        environment from the top file will be considered, and it too will be
        ignored if that environment was defined in the "base" top file.
        '''
        top = DefaultOrderedDict(OrderedDict)

        # Check base env first as it is authoritative
        base_tops = tops.pop('base', DefaultOrderedDict(OrderedDict))
        for ctop in base_tops:
            for saltenv, targets in six.iteritems(ctop):
                if saltenv == 'include':
                    continue
                try:
                    for tgt in targets:
                        top[saltenv][tgt] = ctop[saltenv][tgt]
                except TypeError:
                    raise SaltRenderError('Unable to render top file. No targets found.')

        for cenv, ctops in six.iteritems(tops):
            for ctop in ctops:
                for saltenv, targets in six.iteritems(ctop):
                    if saltenv == 'include':
                        continue
                    elif saltenv != cenv:
                        log.debug(
                            'Section for saltenv \'%s\' in the \'%s\' '
                            'saltenv\'s top file will be ignored, as the '
                            'top_file_merging_strategy is set to \'merge\' '
                            'and the saltenvs do not match',
                            saltenv, cenv
                        )
                        continue
                    elif saltenv in top:
                        log.debug(
                            'Section for saltenv \'%s\' in the \'%s\' '
                            'saltenv\'s top file will be ignored, as this '
                            'saltenv was already defined in the \'base\' top '
                            'file', saltenv, cenv
                        )
                        continue
                    try:
                        for tgt in targets:
                            top[saltenv][tgt] = ctop[saltenv][tgt]
                    except TypeError:
                        raise SaltRenderError('Unable to render top file. No targets found.')
        return top

Response: Internal method to merge the top files into a single top file.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _merge_tops_same(self, tops):
        '''
        For each saltenv, only consider the top file from that saltenv. All
        sections matching a given saltenv, which appear in a different
        saltenv's top file, will be ignored.
        '''
        top = DefaultOrderedDict(OrderedDict)
        for cenv, ctops in six.iteritems(tops):
            if all([x == {} for x in ctops]):
                # No top file found in this env, check the default_top
                default_top = self.opts['default_top']
                fallback_tops = tops.get(default_top, [])
                if all([x == {} for x in fallback_tops]):
                    # Nothing in the fallback top file
                    log.error(
                        'The \'%s\' saltenv has no top file, and the fallback '
                        'saltenv specified by default_top (%s) also has no '
                        'top file', cenv, default_top
                    )
                    continue

                for ctop in fallback_tops:
                    for saltenv, targets in six.iteritems(ctop):
                        if saltenv != cenv:
                            continue
                        log.debug(
                            'The \'%s\' saltenv has no top file, using the '
                            'default_top saltenv (%s)', cenv, default_top
                        )
                        for tgt in targets:
                            top[saltenv][tgt] = ctop[saltenv][tgt]
                        break
                    else:
                        log.error(
                            'The \'%s\' saltenv has no top file, and no '
                            'matches were found in the top file for the '
                            'default_top saltenv (%s)', cenv, default_top
                        )

                continue

            else:
                for ctop in ctops:
                    for saltenv, targets in six.iteritems(ctop):
                        if saltenv == 'include':
                            continue
                        elif saltenv != cenv:
                            log.debug(
                                'Section for saltenv \'%s\' in the \'%s\' '
                                'saltenv\'s top file will be ignored, as the '
                                'top_file_merging_strategy is set to \'same\' '
                                'and the saltenvs do not match',
                                saltenv, cenv
                            )
                            continue

                        try:
                            for tgt in targets:
                                top[saltenv][tgt] = ctop[saltenv][tgt]
                        except TypeError:
                            raise SaltRenderError('Unable to render top file. No targets found.')
        return top

Response: Merge the top files from the given tops with the same environment.


Instruction:
Can you tell what is the following Python 3 function doing
def _merge_tops_merge_all(self, tops):
        '''
        Merge the top files into a single dictionary
        '''
        def _read_tgt(tgt):
            match_type = None
            states = []
            for item in tgt:
                if isinstance(item, dict):
                    match_type = item
                if isinstance(item, six.string_types):
                    states.append(item)
            return match_type, states

        top = DefaultOrderedDict(OrderedDict)
        for ctops in six.itervalues(tops):
            for ctop in ctops:
                for saltenv, targets in six.iteritems(ctop):
                    if saltenv == 'include':
                        continue
                    try:
                        for tgt in targets:
                            if tgt not in top[saltenv]:
                                top[saltenv][tgt] = ctop[saltenv][tgt]
                                continue
                            m_type1, m_states1 = _read_tgt(top[saltenv][tgt])
                            m_type2, m_states2 = _read_tgt(ctop[saltenv][tgt])
                            merged = []
                            match_type = m_type2 or m_type1
                            if match_type is not None:
                                merged.append(match_type)
                            merged.extend(m_states1)
                            merged.extend([x for x in m_states2 if x not in merged])
                            top[saltenv][tgt] = merged
                    except TypeError:
                        raise SaltRenderError('Unable to render top file. No targets found.')
        return top

Response: Merge the top files into a single dictionary.


Instruction:
Here you have a function in Python 3, explain what it does
def verify_tops(self, tops):
        '''
        Verify the contents of the top file data
        '''
        errors = []
        if not isinstance(tops, dict):
            errors.append('Top data was not formed as a dict')
            # No further checks will work, bail out
            return errors
        for saltenv, matches in six.iteritems(tops):
            if saltenv == 'include':
                continue
            if not isinstance(saltenv, six.string_types):
                errors.append(
                    'Environment {0} in top file is not formed as a '
                    'string'.format(saltenv)
                )
            if saltenv == '':
                errors.append('Empty saltenv statement in top file')
            if not isinstance(matches, dict):
                errors.append(
                    'The top file matches for saltenv {0} are not '
                    'formatted as a dict'.format(saltenv)
                )
            for slsmods in six.itervalues(matches):
                if not isinstance(slsmods, list):
                    errors.append('Malformed topfile (state declarations not '
                                  'formed as a list)')
                    continue
                for slsmod in slsmods:
                    if isinstance(slsmod, dict):
                        # This value is a match option
                        for val in six.itervalues(slsmod):
                            if not val:
                                errors.append(
                                    'Improperly formatted top file matcher '
                                    'in saltenv {0}: {1} file'.format(
                                        slsmod,
                                        val
                                    )
                                )
                    elif isinstance(slsmod, six.string_types):
                        # This is a sls module
                        if not slsmod:
                            errors.append(
                                'Environment {0} contains an empty sls '
                                'index'.format(saltenv)
                            )

        return errors

Response: Verify the contents of the top file data.


Instruction:
Can you implement a function in Python 3 that
returns the high data derived from the top file

Response:
def get_top(self):
        '''
        Returns the high data derived from the top file
        '''
        try:
            tops = self.get_tops()
        except SaltRenderError as err:
            log.error('Unable to render top file: %s', err.error)
            return {}
        return self.merge_tops(tops)


Instruction:
Can you write a function in Python 3 where it
searches through the top high data for matches and return the states that this minion needs to execute.

Response:
def top_matches(self, top):
        '''
        Search through the top high data for matches and return the states
        that this minion needs to execute.

        Returns:
        {'saltenv': ['state1', 'state2', ...]}
        '''
        matches = DefaultOrderedDict(OrderedDict)
        # pylint: disable=cell-var-from-loop
        for saltenv, body in six.iteritems(top):
            if self.opts['saltenv']:
                if saltenv != self.opts['saltenv']:
                    continue
            for match, data in six.iteritems(body):
                def _filter_matches(_match, _data, _opts):
                    if isinstance(_data, six.string_types):
                        _data = [_data]
                    if self.matchers['confirm_top.confirm_top'](
                            _match,
                            _data,
                            _opts
                            ):
                        if saltenv not in matches:
                            matches[saltenv] = []
                        for item in _data:
                            if 'subfilter' in item:
                                _tmpdata = item.pop('subfilter')
                                for match, data in six.iteritems(_tmpdata):
                                    _filter_matches(match, data, _opts)
                            if isinstance(item, six.string_types):
                                matches[saltenv].append(item)
                            elif isinstance(item, dict):
                                env_key, inc_sls = item.popitem()
                                if env_key not in self.avail:
                                    continue
                                if env_key not in matches:
                                    matches[env_key] = []
                                matches[env_key].append(inc_sls)
                _filter_matches(match, data, self.opts['nodegroups'])
        ext_matches = self._master_tops()
        for saltenv in ext_matches:
            top_file_matches = matches.get(saltenv, [])
            if self.opts.get('master_tops_first'):
                first = ext_matches[saltenv]
                second = top_file_matches
            else:
                first = top_file_matches
                second = ext_matches[saltenv]
            matches[saltenv] = first + [x for x in second if x not in first]

        # pylint: enable=cell-var-from-loop
        return matches


Instruction:
Write a Python 3 function for
loading the dynamic modules from the given matches.

Response:
def load_dynamic(self, matches):
        '''
        If autoload_dynamic_modules is True then automatically load the
        dynamic modules
        '''
        if not self.opts['autoload_dynamic_modules']:
            return
        syncd = self.state.functions['saltutil.sync_all'](list(matches),
                                                          refresh=False)
        if syncd['grains']:
            self.opts['grains'] = salt.loader.grains(self.opts)
            self.state.opts['pillar'] = self.state._gather_pillar()
        self.state.module_refresh()


Instruction:
Implement a Python 3 function for
rendering a state file and retrieve all of the include states

Response:
def render_state(self, sls, saltenv, mods, matches, local=False):
        '''
        Render a state file and retrieve all of the include states
        '''
        errors = []
        if not local:
            state_data = self.client.get_state(sls, saltenv)
            fn_ = state_data.get('dest', False)
        else:
            fn_ = sls
            if not os.path.isfile(fn_):
                errors.append(
                    'Specified SLS {0} on local filesystem cannot '
                    'be found.'.format(sls)
                )
        state = None
        if not fn_:
            errors.append(
                'Specified SLS {0} in saltenv {1} is not '
                'available on the salt master or through a configured '
                'fileserver'.format(sls, saltenv)
            )
        else:
            try:
                state = compile_template(fn_,
                                         self.state.rend,
                                         self.state.opts['renderer'],
                                         self.state.opts['renderer_blacklist'],
                                         self.state.opts['renderer_whitelist'],
                                         saltenv,
                                         sls,
                                         rendered_sls=mods
                                         )
            except SaltRenderError as exc:
                msg = 'Rendering SLS \'{0}:{1}\' failed: {2}'.format(
                    saltenv, sls, exc
                )
                log.critical(msg)
                errors.append(msg)
            except Exception as exc:
                msg = 'Rendering SLS {0} failed, render error: {1}'.format(
                    sls, exc
                )
                log.critical(
                    msg,
                    # Show the traceback if the debug logging level is enabled
                    exc_info_on_loglevel=logging.DEBUG
                )
                errors.append('{0}\n{1}'.format(msg, traceback.format_exc()))
            try:
                mods.add('{0}:{1}'.format(saltenv, sls))
            except AttributeError:
                pass

        if state:
            if not isinstance(state, dict):
                errors.append(
                    'SLS {0} does not render to a dictionary'.format(sls)
                )
            else:
                include = []
                if 'include' in state:
                    if not isinstance(state['include'], list):
                        err = ('Include Declaration in SLS {0} is not formed '
                               'as a list'.format(sls))
                        errors.append(err)
                    else:
                        include = state.pop('include')

                self._handle_extend(state, sls, saltenv, errors)
                self._handle_exclude(state, sls, saltenv, errors)
                self._handle_state_decls(state, sls, saltenv, errors)

                for inc_sls in include:
                    # inc_sls may take the form of:
                    #   'sls.to.include' <- same as {<saltenv>: 'sls.to.include'}
                    #   {<env_key>: 'sls.to.include'}
                    #   {'_xenv': 'sls.to.resolve'}
                    xenv_key = '_xenv'

                    if isinstance(inc_sls, dict):
                        env_key, inc_sls = inc_sls.popitem()
                    else:
                        env_key = saltenv

                    if env_key not in self.avail:
                        msg = ('Nonexistent saltenv \'{0}\' found in include '
                               'of \'{1}\' within SLS \'{2}:{3}\''
                               .format(env_key, inc_sls, saltenv, sls))
                        log.error(msg)
                        errors.append(msg)
                        continue

                    if inc_sls.startswith('.'):
                        match = re.match(r'^(\.+)(.*)$', inc_sls)
                        if match:
                            levels, include = match.groups()
                        else:
                            msg = ('Badly formatted include {0} found in include '
                                    'in SLS \'{2}:{3}\''
                                    .format(inc_sls, saltenv, sls))
                            log.error(msg)
                            errors.append(msg)
                            continue
                        level_count = len(levels)
                        p_comps = sls.split('.')
                        if state_data.get('source', '').endswith('/init.sls'):
                            p_comps.append('init')
                        if level_count > len(p_comps):
                            msg = ('Attempted relative include of \'{0}\' '
                                   'within SLS \'{1}:{2}\' '
                                   'goes beyond top level package '
                                   .format(inc_sls, saltenv, sls))
                            log.error(msg)
                            errors.append(msg)
                            continue
                        inc_sls = '.'.join(p_comps[:-level_count] + [include])

                    if env_key != xenv_key:
                        if matches is None:
                            matches = []
                        # Resolve inc_sls in the specified environment
                        if env_key in matches or fnmatch.filter(self.avail[env_key], inc_sls):
                            resolved_envs = [env_key]
                        else:
                            resolved_envs = []
                    else:
                        # Resolve inc_sls in the subset of environment matches
                        resolved_envs = [
                            aenv for aenv in matches
                            if fnmatch.filter(self.avail[aenv], inc_sls)
                        ]

                    # An include must be resolved to a single environment, or
                    # the include must exist in the current environment
                    if len(resolved_envs) == 1 or saltenv in resolved_envs:
                        # Match inc_sls against the available states in the
                        # resolved env, matching wildcards in the process. If
                        # there were no matches, then leave inc_sls as the
                        # target so that the next recursion of render_state
                        # will recognize the error.
                        sls_targets = fnmatch.filter(
                            self.avail[saltenv],
                            inc_sls
                        ) or [inc_sls]

                        for sls_target in sls_targets:
                            r_env = resolved_envs[0] if len(resolved_envs) == 1 else saltenv
                            mod_tgt = '{0}:{1}'.format(r_env, sls_target)
                            if mod_tgt not in mods:
                                nstate, err = self.render_state(
                                    sls_target,
                                    r_env,
                                    mods,
                                    matches
                                )
                                if nstate:
                                    self.merge_included_states(state, nstate, errors)
                                    state.update(nstate)
                                if err:
                                    errors.extend(err)
                    else:
                        msg = ''
                        if not resolved_envs:
                            msg = ('Unknown include: Specified SLS {0}: {1} is not available on the salt '
                                   'master in saltenv(s): {2} '
                                   ).format(env_key,
                                            inc_sls,
                                            ', '.join(matches) if env_key == xenv_key else env_key)
                        elif len(resolved_envs) > 1:
                            msg = ('Ambiguous include: Specified SLS {0}: {1} is available on the salt master '
                                   'in multiple available saltenvs: {2}'
                                   ).format(env_key,
                                            inc_sls,
                                            ', '.join(resolved_envs))
                        log.critical(msg)
                        errors.append(msg)
                try:
                    self._handle_iorder(state)
                except TypeError:
                    log.critical('Could not render SLS %s. Syntax error detected.', sls)
        else:
            state = {}
        return state, errors


Instruction:
Make a summary of the following Python 3 code
def _handle_iorder(self, state):
        '''
        Take a state and apply the iorder system
        '''
        if self.opts['state_auto_order']:
            for name in state:
                for s_dec in state[name]:
                    if not isinstance(s_dec, six.string_types):
                        # PyDSL OrderedDict?
                        continue

                    if not isinstance(state[name], dict):
                        # Include's or excludes as lists?
                        continue
                    if not isinstance(state[name][s_dec], list):
                        # Bad syntax, let the verify seq pick it up later on
                        continue

                    found = False
                    if s_dec.startswith('_'):
                        continue

                    for arg in state[name][s_dec]:
                        if isinstance(arg, dict):
                            if arg:
                                if next(six.iterkeys(arg)) == 'order':
                                    found = True
                    if not found:
                        if not isinstance(state[name][s_dec], list):
                            # quite certainly a syntax error, managed elsewhere
                            continue
                        state[name][s_dec].append(
                                {'order': self.iorder}
                                )
                        self.iorder += 1
        return state

Response:
Take a state and apply the iorder system
AttributeNames


Instruction:
Here you have a function in Python 3, explain what it does
def _handle_state_decls(self, state, sls, saltenv, errors):
        '''
        Add sls and saltenv components to the state
        '''
        for name in state:
            if not isinstance(state[name], dict):
                if name == '__extend__':
                    continue
                if name == '__exclude__':
                    continue

                if isinstance(state[name], six.string_types):
                    # Is this is a short state, it needs to be padded
                    if '.' in state[name]:
                        comps = state[name].split('.')
                        state[name] = {'__sls__': sls,
                                       '__env__': saltenv,
                                       comps[0]: [comps[1]]}
                        continue
                errors.append(
                    'ID {0} in SLS {1} is not a dictionary'.format(name, sls)
                )
                continue
            skeys = set()
            for key in list(state[name]):
                if key.startswith('_'):
                    continue
                if not isinstance(state[name][key], list):
                    continue
                if '.' in key:
                    comps = key.split('.')
                    # Salt doesn't support state files such as:
                    #
                    #     /etc/redis/redis.conf:
                    #       file.managed:
                    #         - source: salt://redis/redis.conf
                    #         - user: redis
                    #         - group: redis
                    #         - mode: 644
                    #       file.comment:
                    #           - regex: ^requirepass
                    if comps[0] in skeys:
                        errors.append(
                            'ID \'{0}\' in SLS \'{1}\' contains multiple state '
                            'declarations of the same type'.format(name, sls)
                        )
                        continue
                    state[name][comps[0]] = state[name].pop(key)
                    state[name][comps[0]].append(comps[1])
                    skeys.add(comps[0])
                    continue
                skeys.add(key)
            if '__sls__' not in state[name]:
                state[name]['__sls__'] = sls
            if '__env__' not in state[name]:
                state[name]['__env__'] = saltenv

Response: Handle the state of the state files


Instruction:
Write a Python 3 script to
handle the extend dec out of the state and apply to the highstate global dec

Response:
def _handle_extend(self, state, sls, saltenv, errors):
        '''
        Take the extend dec out of state and apply to the highstate global
        dec
        '''
        if 'extend' in state:
            ext = state.pop('extend')
            if not isinstance(ext, dict):
                errors.append(('Extension value in SLS \'{0}\' is not a '
                               'dictionary').format(sls))
                return
            for name in ext:
                if not isinstance(ext[name], dict):
                    errors.append(('Extension name \'{0}\' in SLS \'{1}\' is '
                                   'not a dictionary'
                                   .format(name, sls)))
                    continue
                if '__sls__' not in ext[name]:
                    ext[name]['__sls__'] = sls
                if '__env__' not in ext[name]:
                    ext[name]['__env__'] = saltenv
                for key in list(ext[name]):
                    if key.startswith('_'):
                        continue
                    if not isinstance(ext[name][key], list):
                        continue
                    if '.' in key:
                        comps = key.split('.')
                        ext[name][comps[0]] = ext[name].pop(key)
                        ext[name][comps[0]].append(comps[1])
            state.setdefault('__extend__', []).append(ext)


Instruction:
How would you explain what the following Python 3 function does
def _handle_exclude(self, state, sls, saltenv, errors):
        '''
        Take the exclude dec out of the state and apply it to the highstate
        global dec
        '''
        if 'exclude' in state:
            exc = state.pop('exclude')
            if not isinstance(exc, list):
                err = ('Exclude Declaration in SLS {0} is not formed '
                       'as a list'.format(sls))
                errors.append(err)
            state.setdefault('__exclude__', []).extend(exc)

Response:
Handle the exclude declaration in the state and add it to the highstate
AttributeNames global dec
AttributeNames


Instruction:
Can you write a function in Python 3 where it
gathers the state files and render them into a single unified salt high data structure.

Response:
def render_highstate(self, matches):
        '''
        Gather the state files and render them into a single unified salt
        high data structure.
        '''
        highstate = self.building_highstate
        all_errors = []
        mods = set()
        statefiles = []
        for saltenv, states in six.iteritems(matches):
            for sls_match in states:
                if saltenv in self.avail:
                    statefiles = fnmatch.filter(self.avail[saltenv], sls_match)
                elif '__env__' in self.avail:
                    statefiles = fnmatch.filter(self.avail['__env__'], sls_match)
                else:
                    all_errors.append(
                        'No matching salt environment for environment '
                        '\'{0}\' found'.format(saltenv)
                    )
                # if we did not found any sls in the fileserver listing, this
                # may be because the sls was generated or added later, we can
                # try to directly execute it, and if it fails, anyway it will
                # return the former error
                if not statefiles:
                    statefiles = [sls_match]

                for sls in statefiles:
                    r_env = '{0}:{1}'.format(saltenv, sls)
                    if r_env in mods:
                        continue
                    state, errors = self.render_state(
                        sls, saltenv, mods, matches)
                    if state:
                        self.merge_included_states(highstate, state, errors)
                    for i, error in enumerate(errors[:]):
                        if 'is not available' in error:
                            # match SLS foobar in environment
                            this_sls = 'SLS {0} in saltenv'.format(
                                sls_match)
                            if this_sls in error:
                                errors[i] = (
                                    'No matching sls found for \'{0}\' '
                                    'in env \'{1}\''.format(sls_match, saltenv))
                    all_errors.extend(errors)

        self.clean_duplicate_extends(highstate)
        return highstate, all_errors


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _check_pillar(self, force=False):
        '''
        Check the pillar for errors, refuse to run the state if there are
        errors in the pillar and return the pillar errors
        '''
        if force:
            return True
        if '_errors' in self.state.opts['pillar']:
            return False
        return True

Response: Check the pillar for errors and return the pillar errors


Instruction:
Here you have a function in Python 3, explain what it does
def matches_whitelist(self, matches, whitelist):
        '''
        Reads over the matches and returns a matches dict with just the ones
        that are in the whitelist
        '''
        if not whitelist:
            return matches
        ret_matches = {}
        if not isinstance(whitelist, list):
            whitelist = whitelist.split(',')
        for env in matches:
            for sls in matches[env]:
                if sls in whitelist:
                    ret_matches[env] = ret_matches[env] if env in ret_matches else []
                    ret_matches[env].append(sls)
        return ret_matches

Response:
Reads over the matches and returns a matches dict with just the ones
        that are in the whitelist


Instruction:
Can you create a Python 3 function that
executes the salt highstate for this minion

Response:
def call_highstate(self, exclude=None, cache=None, cache_name='highstate',
                       force=False, whitelist=None, orchestration_jid=None):
        '''
        Run the sequence to execute the salt highstate for this minion
        '''
        # Check that top file exists
        tag_name = 'no_|-states_|-states_|-None'
        ret = {tag_name: {
                'result': False,
                'comment': 'No states found for this minion',
                'name': 'No States',
                'changes': {},
                '__run_num__': 0,
        }}
        cfn = os.path.join(
                self.opts['cachedir'],
                '{0}.cache.p'.format(cache_name)
        )

        if cache:
            if os.path.isfile(cfn):
                with salt.utils.files.fopen(cfn, 'rb') as fp_:
                    high = self.serial.load(fp_)
                    return self.state.call_high(high, orchestration_jid)
        # File exists so continue
        err = []
        try:
            top = self.get_top()
        except SaltRenderError as err:
            ret[tag_name]['comment'] = 'Unable to render top file: '
            ret[tag_name]['comment'] += six.text_type(err.error)
            return ret
        except Exception:
            trb = traceback.format_exc()
            err.append(trb)
            return err
        err += self.verify_tops(top)
        matches = self.top_matches(top)
        if not matches:
            msg = ('No Top file or master_tops data matches found. Please see '
                   'master log for details.')
            ret[tag_name]['comment'] = msg
            return ret
        matches = self.matches_whitelist(matches, whitelist)
        self.load_dynamic(matches)
        if not self._check_pillar(force):
            err += ['Pillar failed to render with the following messages:']
            err += self.state.opts['pillar']['_errors']
        else:
            high, errors = self.render_highstate(matches)
            if exclude:
                if isinstance(exclude, six.string_types):
                    exclude = exclude.split(',')
                if '__exclude__' in high:
                    high['__exclude__'].extend(exclude)
                else:
                    high['__exclude__'] = exclude
            err += errors
        if err:
            return err
        if not high:
            return ret
        with salt.utils.files.set_umask(0o077):
            try:
                if salt.utils.platform.is_windows():
                    # Make sure cache file isn't read-only
                    self.state.functions['cmd.run'](
                        ['attrib', '-R', cfn],
                        python_shell=False,
                        output_loglevel='quiet')
                with salt.utils.files.fopen(cfn, 'w+b') as fp_:
                    try:
                        self.serial.dump(high, fp_)
                    except TypeError:
                        # Can't serialize pydsl
                        pass
            except (IOError, OSError):
                log.error('Unable to write to "state.highstate" cache file %s', cfn)

        return self.state.call_high(high, orchestration_jid)


Instruction:
Write a Python 3 function that can
return just the highstate or the errors

Response:
def compile_highstate(self):
        '''
        Return just the highstate or the errors
        '''
        err = []
        top = self.get_top()
        err += self.verify_tops(top)
        matches = self.top_matches(top)
        high, errors = self.render_highstate(matches)
        err += errors

        if err:
            return err

        return high


Instruction:
Write a Python 3 function that can
compile the state usage for the minion based on the top match data

Response:
def compile_state_usage(self):
        '''
        Return all used and unused states for the minion based on the top match data
        '''
        err = []
        top = self.get_top()
        err += self.verify_tops(top)

        if err:
            return err

        matches = self.top_matches(top)
        state_usage = {}

        for saltenv, states in self.avail.items():
            env_usage = {
                'used': [],
                'unused': [],
                'count_all': 0,
                'count_used': 0,
                'count_unused': 0
            }

            env_matches = matches.get(saltenv)

            for state in states:
                env_usage['count_all'] += 1
                if state in env_matches:
                    env_usage['count_used'] += 1
                    env_usage['used'].append(state)
                else:
                    env_usage['count_unused'] += 1
                    env_usage['unused'].append(state)

            state_usage[saltenv] = env_usage

        return state_usage


Instruction:
Write a Python 3 function that can
load the modules into the state

Response:
def load_modules(self, data=None, proxy=None):
        '''
        Load the modules into the state
        '''
        log.info('Loading fresh modules for state activity')
        # Load a modified client interface that looks like the interface used
        # from the minion, but uses remote execution
        #
        self.functions = salt.client.FunctionWrapper(
                self.opts,
                self.opts['id']
                )
        # Load the states, but they should not be used in this class apart
        # from inspection
        self.utils = salt.loader.utils(self.opts)
        self.serializers = salt.loader.serializers(self.opts)
        self.states = salt.loader.states(self.opts, self.functions, self.utils, self.serializers)
        self.rend = salt.loader.render(self.opts, self.functions, states=self.states, context=self.state_con)


Instruction:
Implement a function in Python 3 to
return the state data from the master

Response:
def compile_master(self):
        '''
        Return the state data from the master
        '''
        load = {'grains': self.grains,
                'opts': self.opts,
                'cmd': '_master_state'}
        try:
            return self.channel.send(load, tries=3, timeout=72000)
        except SaltReqTimeoutError:
            return {}


Instruction:
Create a Python 3 function for
outputting the data in lines very nice for running commands

Response:
def output(data, **kwargs):  # pylint: disable=unused-argument
    '''
    Output the data in lines, very nice for running commands
    '''
    ret = ''
    if hasattr(data, 'keys'):
        for key in data:
            value = data[key]
            # Don't blow up on non-strings
            try:
                for line in value.splitlines():
                    ret += '{0}: {1}\n'.format(key, line)
            except AttributeError:
                ret += '{0}: {1}\n'.format(key, value)
    else:
        try:
            ret += data + '\n'
        except TypeError:
            # For non-dictionary, non-string data, just use print
            ret += '{0}\n'.format(pprint.pformat(data))

    return ret


Instruction:
Given the following Python 3 function, write the documentation
def compare_and_update_config(config, update_config, changes, namespace=''):
    '''
    Recursively compare two configs, writing any needed changes to the
    update_config and capturing changes in the changes dict.
    '''
    if isinstance(config, dict):
        if not update_config:
            if config:
                # the updated config is more valid--report that we are using it
                changes[namespace] = {
                    'new': config,
                    'old': update_config,
                }
            return config
        elif not isinstance(update_config, dict):
            # new config is a dict, other isn't--new one wins
            changes[namespace] = {
                'new': config,
                'old': update_config,
            }
            return config
        else:
            # compare each key in the base config with the values in the
            # update_config, overwriting the values that are different but
            # keeping any that are not defined in config
            for key, value in six.iteritems(config):
                _namespace = key
                if namespace:
                    _namespace = '{0}.{1}'.format(namespace, _namespace)
                update_config[key] = compare_and_update_config(
                    value,
                    update_config.get(key, None),
                    changes,
                    namespace=_namespace,
                )
            return update_config

    elif isinstance(config, list):
        if not update_config:
            if config:
                # the updated config is more valid--report that we are using it
                changes[namespace] = {
                    'new': config,
                    'old': update_config,
                }
            return config
        elif not isinstance(update_config, list):
            # new config is a list, other isn't--new one wins
            changes[namespace] = {
                'new': config,
                'old': update_config,
            }
            return config
        else:
            # iterate through config list, ensuring that each index in the
            # update_config list is the same
            for idx, item in enumerate(config):
                _namespace = '[{0}]'.format(idx)
                if namespace:
                    _namespace = '{0}{1}'.format(namespace, _namespace)
                _update = None
                if len(update_config) > idx:
                    _update = update_config[idx]
                if _update:
                    update_config[idx] = compare_and_update_config(
                        config[idx],
                        _update,
                        changes,
                        namespace=_namespace,
                    )
                else:
                    changes[_namespace] = {
                        'new': config[idx],
                        'old': _update,
                    }
                    update_config.append(config[idx])

            if len(update_config) > len(config):
                # trim any items in update_config that are not in config
                for idx, old_item in enumerate(update_config):
                    if idx < len(config):
                        continue
                    _namespace = '[{0}]'.format(idx)
                    if namespace:
                        _namespace = '{0}{1}'.format(namespace, _namespace)
                    changes[_namespace] = {
                        'new': None,
                        'old': old_item,
                    }
                del update_config[len(config):]
            return update_config

    else:
        if config != update_config:
            changes[namespace] = {
                'new': config,
                'old': update_config,
            }
        return config

Response: Recursively compare two configs writing any needed changes to the update_config and capturing changes in the changes dict.


Instruction:
Make a summary of the following Python 3 code
def managed(name,
            value=None,
            host=DEFAULT_HOST,
            port=DEFAULT_PORT,
            time=DEFAULT_TIME,
            min_compress_len=DEFAULT_MIN_COMPRESS_LEN):
    '''
    Manage a memcached key.

    name
        The key to manage

    value
        The value to set for that key

    host
        The memcached server IP address

    port
        The memcached server port

    .. code-block:: yaml

        foo:
          memcached.managed:
            - value: bar
    '''
    ret = {'name': name,
           'changes': {},
           'result': False,
           'comment': ''}

    try:
        cur = __salt__['memcached.get'](name, host, port)
    except CommandExecutionError as exc:
        ret['comment'] = six.text_type(exc)
        return ret

    if cur == value:
        ret['result'] = True
        ret['comment'] = 'Key \'{0}\' does not need to be updated'.format(name)
        return ret

    if __opts__['test']:
        ret['result'] = None
        if cur is None:
            ret['comment'] = 'Key \'{0}\' would be added'.format(name)
        else:
            ret['comment'] = 'Value of key \'{0}\' would be changed'.format(name)
        return ret

    try:
        ret['result'] = __salt__['memcached.set'](
            name, value, host, port, time, min_compress_len
        )
    except (CommandExecutionError, SaltInvocationError) as exc:
        ret['comment'] = six.text_type(exc)
    else:
        if ret['result']:
            ret['comment'] = 'Successfully set key \'{0}\''.format(name)
            if cur is not None:
                ret['changes'] = {'old': cur, 'new': value}
            else:
                ret['changes'] = {'key added': name, 'value': value}
        else:
            ret['comment'] = 'Failed to set key \'{0}\''.format(name)
    return ret

Response: Manage a memcached key.


Instruction:
Here you have a function in Python 3, explain what it does
def parse_hosts(hostsfile='/etc/hosts', hosts=None):
    '''
    Parse /etc/hosts file.

    CLI Example:

    .. code-block:: bash

        salt '*' dnsutil.parse_hosts
    '''
    if not hosts:
        try:
            with salt.utils.files.fopen(hostsfile, 'r') as fp_:
                hosts = salt.utils.stringutils.to_unicode(fp_.read())
        except Exception:
            return 'Error: hosts data was not found'

    hostsdict = {}
    for line in hosts.splitlines():
        if not line:
            continue
        if line.startswith('#'):
            continue
        comps = line.split()
        ip = comps[0]
        aliases = comps[1:]
        hostsdict.setdefault(ip, []).extend(aliases)

    return hostsdict

Response: Parse the hosts file and return a dict of the hosts that are available in the hosts file.


Instruction:
Can you tell what is the following Python 3 function doing
def hosts_append(hostsfile='/etc/hosts', ip_addr=None, entries=None):
    '''
    Append a single line to the /etc/hosts file.

    CLI Example:

    .. code-block:: bash

        salt '*' dnsutil.hosts_append /etc/hosts 127.0.0.1 ad1.yuk.co,ad2.yuk.co
    '''
    host_list = entries.split(',')
    hosts = parse_hosts(hostsfile=hostsfile)
    if ip_addr in hosts:
        for host in host_list:
            if host in hosts[ip_addr]:
                host_list.remove(host)

    if not host_list:
        return 'No additional hosts were added to {0}'.format(hostsfile)

    append_line = '\n{0} {1}'.format(ip_addr, ' '.join(host_list))
    with salt.utils.files.fopen(hostsfile, 'a') as fp_:
        fp_.write(salt.utils.stringutils.to_str(append_line))

    return 'The following line was added to {0}:{1}'.format(hostsfile,
                                                            append_line)

Response: Append a single line to the hosts file.


Instruction:
Here you have a function in Python 3, explain what it does
def hosts_remove(hostsfile='/etc/hosts', entries=None):
    '''
    Remove a host from the /etc/hosts file. If doing so will leave a line
    containing only an IP address, then the line will be deleted. This function
    will leave comments and blank lines intact.

    CLI Examples:

    .. code-block:: bash

        salt '*' dnsutil.hosts_remove /etc/hosts ad1.yuk.co
        salt '*' dnsutil.hosts_remove /etc/hosts ad2.yuk.co,ad1.yuk.co
    '''
    with salt.utils.files.fopen(hostsfile, 'r') as fp_:
        hosts = salt.utils.stringutils.to_unicode(fp_.read())

    host_list = entries.split(',')
    with salt.utils.files.fopen(hostsfile, 'w') as out_file:
        for line in hosts.splitlines():
            if not line or line.strip().startswith('#'):
                out_file.write(salt.utils.stringutils.to_str('{0}\n'.format(line)))
                continue
            comps = line.split()
            for host in host_list:
                if host in comps[1:]:
                    comps.remove(host)
            if len(comps) > 1:
                out_file.write(salt.utils.stringutils.to_str(' '.join(comps)))
                out_file.write(salt.utils.stringutils.to_str('\n'))

Response: Remove a host from the hosts file.


Instruction:
Implement a Python 3 function for
parsing a zone file and returns a list of dicts.

Response:
def parse_zone(zonefile=None, zone=None):
    '''
    Parses a zone file. Can be passed raw zone data on the API level.

    CLI Example:

    .. code-block:: bash

        salt ns1 dnsutil.parse_zone /var/lib/named/example.com.zone
    '''
    if zonefile:
        try:
            with salt.utils.files.fopen(zonefile, 'r') as fp_:
                zone = salt.utils.stringutils.to_unicode(fp_.read())
        except Exception:
            pass

    if not zone:
        return 'Error: Zone data was not found'

    zonedict = {}
    mode = 'single'
    for line in zone.splitlines():
        comps = line.split(';')
        line = comps[0].strip()
        if not line:
            continue
        comps = line.split()
        if line.startswith('$'):
            zonedict[comps[0].replace('$', '')] = comps[1]
            continue
        if '(' in line and ')' not in line:
            mode = 'multi'
            multi = ''
        if mode == 'multi':
            multi += ' {0}'.format(line)
            if ')' in line:
                mode = 'single'
                line = multi.replace('(', '').replace(')', '')
            else:
                continue
        if 'ORIGIN' in zonedict:
            comps = line.replace('@', zonedict['ORIGIN']).split()
        else:
            comps = line.split()
        if 'SOA' in line:
            if comps[1] != 'IN':
                comps.pop(1)
            zonedict['ORIGIN'] = comps[0]
            zonedict['NETWORK'] = comps[1]
            zonedict['SOURCE'] = comps[3]
            zonedict['CONTACT'] = comps[4].replace('.', '@', 1)
            zonedict['SERIAL'] = comps[5]
            zonedict['REFRESH'] = _to_seconds(comps[6])
            zonedict['RETRY'] = _to_seconds(comps[7])
            zonedict['EXPIRE'] = _to_seconds(comps[8])
            zonedict['MINTTL'] = _to_seconds(comps[9])
            continue
        if comps[0] == 'IN':
            comps.insert(0, zonedict['ORIGIN'])
        if not comps[0].endswith('.') and 'NS' not in line:
            comps[0] = '{0}.{1}'.format(comps[0], zonedict['ORIGIN'])
        if comps[2] == 'NS':
            zonedict.setdefault('NS', []).append(comps[3])
        elif comps[2] == 'MX':
            if 'MX' not in zonedict:
                zonedict.setdefault('MX', []).append({'priority': comps[3],
                                                      'host': comps[4]})
        elif comps[3] in ('A', 'AAAA'):
            zonedict.setdefault(comps[3], {})[comps[0]] = {
                'TARGET': comps[4],
                'TTL': comps[1],
            }
        else:
            zonedict.setdefault(comps[2], {})[comps[0]] = comps[3]
    return zonedict


Instruction:
Make a summary of the following Python 3 code
def _to_seconds(timestr):
    '''
    Converts a time value to seconds.

    As per RFC1035 (page 45), max time is 1 week, so anything longer (or
    unreadable) will be set to one week (604800 seconds).
    '''
    timestr = timestr.upper()
    if 'H' in timestr:
        seconds = int(timestr.replace('H', '')) * 3600
    elif 'D' in timestr:
        seconds = int(timestr.replace('D', '')) * 86400
    elif 'W' in timestr:
        seconds = 604800
    else:
        try:
            seconds = int(timestr)
        except ValueError:
            seconds = 604800
    if seconds > 604800:
        seconds = 604800
    return seconds

Response: Converts a time value to seconds.


Instruction:
How would you code a function in Python 3 to
return the A record(s ) for host.

Response:
def A(host, nameserver=None):
    '''
    Return the A record(s) for ``host``.

    Always returns a list.

    CLI Example:

    .. code-block:: bash

        salt ns1 dnsutil.A www.google.com
    '''
    if _has_dig():
        return __salt__['dig.A'](host, nameserver)
    elif nameserver is None:
        # fall back to the socket interface, if we don't care who resolves
        try:
            addresses = [sock[4][0] for sock in socket.getaddrinfo(host, None, socket.AF_INET, 0, socket.SOCK_RAW)]
            return addresses
        except socket.gaierror:
            return 'Unable to resolve {0}'.format(host)

    return 'This function requires dig, which is not currently available'


Instruction:
Create a Python 3 function to
return the AAAA record for host.

Response:
def AAAA(host, nameserver=None):
    '''
    Return the AAAA record(s) for ``host``.

    Always returns a list.

    .. versionadded:: 2014.7.5

    CLI Example:

    .. code-block:: bash

        salt ns1 dnsutil.AAAA www.google.com
    '''
    if _has_dig():
        return __salt__['dig.AAAA'](host, nameserver)
    elif nameserver is None:
        # fall back to the socket interface, if we don't care who resolves
        try:
            addresses = [sock[4][0] for sock in socket.getaddrinfo(host, None, socket.AF_INET6, 0, socket.SOCK_RAW)]
            return addresses
        except socket.gaierror:
            return 'Unable to resolve {0}'.format(host)

    return 'This function requires dig, which is not currently available'


Instruction:
Can you tell what is the following Python 3 function doing
def NS(domain, resolve=True, nameserver=None):
    '''
    Return a list of IPs of the nameservers for ``domain``

    If 'resolve' is False, don't resolve names.

    CLI Example:

    .. code-block:: bash

        salt ns1 dnsutil.NS google.com

    '''
    if _has_dig():
        return __salt__['dig.NS'](domain, resolve, nameserver)

    return 'This function requires dig, which is not currently available'

Response: Return a list of IPs of the nameserver for the given domain


Instruction:
Make a summary of the following Python 3 code
def MX(domain, resolve=False, nameserver=None):
    '''
    Return a list of lists for the MX of ``domain``.

    If the 'resolve' argument is True, resolve IPs for the servers.

    It's limited to one IP, because although in practice it's very rarely a
    round robin, it is an acceptable configuration and pulling just one IP lets
    the data be similar to the non-resolved version. If you think an MX has
    multiple IPs, don't use the resolver here, resolve them in a separate step.

    CLI Example:

    .. code-block:: bash

        salt ns1 dnsutil.MX google.com
    '''
    if _has_dig():
        return __salt__['dig.MX'](domain, resolve, nameserver)

    return 'This function requires dig, which is not currently available'

Response: Return a list of lists for the MX of the given domain.


Instruction:
Explain what the following Python 3 code does
def serial(zone='', update=False):
    '''
    Return, store and update a dns serial for your zone files.

    zone: a keyword for a specific zone

    update: store an updated version of the serial in a grain

    If ``update`` is False, the function will retrieve an existing serial or
    return the current date if no serial is stored. Nothing will be stored

    If ``update`` is True, the function will set the serial to the current date
    if none exist or if the existing serial is for a previous date. If a serial
    for greater than the current date is already stored, the function will
    increment it.

    This module stores the serial in a grain, you can explicitly set the
    stored value as a grain named ``dnsserial_<zone_name>``.

    CLI Example:

    .. code-block:: bash

        salt ns1 dnsutil.serial example.com
    '''
    grains = {}
    key = 'dnsserial'
    if zone:
        key += '_{0}'.format(zone)
    stored = __salt__['grains.get'](key=key)
    present = time.strftime('%Y%m%d01')
    if not update:
        return stored or present
    if stored and stored >= present:
        current = six.text_type(int(stored) + 1)
    else:
        current = present
    __salt__['grains.setval'](key=key, val=current)
    return current

Response: Return a dns serial for your zone files.


Instruction:
Can you write a function in Python 3 where it
evaluates accepted keys and create a msgpack file which contains a list

Response:
def handle_key_cache(self):
        '''
        Evaluate accepted keys and create a msgpack file
        which contains a list
        '''
        if self.opts['key_cache'] == 'sched':
            keys = []
            #TODO DRY from CKMinions
            if self.opts['transport'] in ('zeromq', 'tcp'):
                acc = 'minions'
            else:
                acc = 'accepted'

            for fn_ in os.listdir(os.path.join(self.opts['pki_dir'], acc)):
                if not fn_.startswith('.') and os.path.isfile(os.path.join(self.opts['pki_dir'], acc, fn_)):
                    keys.append(fn_)
            log.debug('Writing master key cache')
            # Write a temporary file securely
            if six.PY2:
                with salt.utils.atomicfile.atomic_open(os.path.join(self.opts['pki_dir'], acc, '.key_cache')) as cache_file:
                    self.serial.dump(keys, cache_file)
            else:
                with salt.utils.atomicfile.atomic_open(os.path.join(self.opts['pki_dir'], acc, '.key_cache'), mode='wb') as cache_file:
                    self.serial.dump(keys, cache_file)


Instruction:
How would you implement a function in Python 3 that
rotates the AES key rotation

Response:
def handle_key_rotate(self, now):
        '''
        Rotate the AES key rotation
        '''
        to_rotate = False
        dfn = os.path.join(self.opts['cachedir'], '.dfn')
        try:
            stats = os.stat(dfn)
            # Basic Windows permissions don't distinguish between
            # user/group/all. Check for read-only state instead.
            if salt.utils.platform.is_windows() and not os.access(dfn, os.W_OK):
                to_rotate = True
                # Cannot delete read-only files on Windows.
                os.chmod(dfn, stat.S_IRUSR | stat.S_IWUSR)
            elif stats.st_mode == 0o100400:
                to_rotate = True
            else:
                log.error('Found dropfile with incorrect permissions, ignoring...')
            os.remove(dfn)
        except os.error:
            pass

        if self.opts.get('publish_session'):
            if now - self.rotate >= self.opts['publish_session']:
                to_rotate = True

        if to_rotate:
            log.info('Rotating master AES key')
            for secret_key, secret_map in six.iteritems(SMaster.secrets):
                # should be unnecessary-- since no one else should be modifying
                with secret_map['secret'].get_lock():
                    secret_map['secret'].value = salt.utils.stringutils.to_bytes(secret_map['reload']())
                self.event.fire_event({'rotate_{0}_key'.format(secret_key): True}, tag='key')
            self.rotate = now
            if self.opts.get('ping_on_rotate'):
                # Ping all minions to get them to pick up the new key
                log.debug('Pinging all connected minions '
                          'due to key rotation')
                salt.utils.master.ping_all_connected_minions(self.opts)


Instruction:
How would you explain what the following Python 3 function does
def handle_schedule(self):
        '''
        Evaluate the scheduler
        '''
        try:
            self.schedule.eval()
            # Check if scheduler requires lower loop interval than
            # the loop_interval setting
            if self.schedule.loop_interval < self.loop_interval:
                self.loop_interval = self.schedule.loop_interval
        except Exception as exc:
            log.error('Exception %s occurred in scheduled job', exc)

Response: Evaluate the scheduler and update the loop_interval property


Instruction:
How would you explain what the following Python 3 function does
def handle_presence(self, old_present):
        '''
        Fire presence events if enabled
        '''
        # On the first run it may need more time for the EventPublisher
        # to come up and be ready. Set the timeout to account for this.
        if self.presence_events and self.event.connect_pull(timeout=3):
            present = self.ckminions.connected_ids()
            new = present.difference(old_present)
            lost = old_present.difference(present)
            if new or lost:
                # Fire new minions present event
                data = {'new': list(new),
                        'lost': list(lost)}
                self.event.fire_event(data, tagify('change', 'presence'))
            data = {'present': list(present)}
            self.event.fire_event(data, tagify('present', 'presence'))
            old_present.clear()
            old_present.update(present)

Response:
Fire presence events if enabled
        is True


Instruction:
Can you tell what is the following Python 3 function doing
def fill_buckets(self):
        '''
        Get the configured backends and the intervals for any backend which
        supports them, and set up the update "buckets". There will be one
        bucket for each thing being updated at a given interval.
        '''
        update_intervals = self.fileserver.update_intervals()
        self.buckets = {}
        for backend in self.fileserver.backends():
            fstr = '{0}.update'.format(backend)
            try:
                update_func = self.fileserver.servers[fstr]
            except KeyError:
                log.debug(
                    'No update function for the %s filserver backend',
                    backend
                )
                continue
            if backend in update_intervals:
                # Variable intervals are supported for this backend
                for id_, interval in six.iteritems(update_intervals[backend]):
                    if not interval:
                        # Don't allow an interval of 0
                        interval = DEFAULT_INTERVAL
                        log.debug(
                            'An update_interval of 0 is not supported, '
                            'falling back to %s', interval
                        )
                    i_ptr = self.buckets.setdefault(interval, OrderedDict())
                    # Backend doesn't technically need to be present in the
                    # key, all we *really* need is the function reference, but
                    # having it there makes it easier to provide meaningful
                    # debug logging in the update threads.
                    i_ptr.setdefault((backend, update_func), []).append(id_)
            else:
                # Variable intervals are not supported for this backend, so
                # fall back to the global interval for that fileserver. Since
                # this backend doesn't support variable updates, we have
                # nothing to pass to the backend's update func, so we'll just
                # set the value to None.
                try:
                    interval_key = '{0}_update_interval'.format(backend)
                    interval = self.opts[interval_key]
                except KeyError:
                    interval = DEFAULT_INTERVAL
                    log.warning(
                        '%s key missing from configuration. Falling back to '
                        'default interval of %d seconds',
                        interval_key, interval
                    )
                self.buckets.setdefault(
                    interval, OrderedDict())[(backend, update_func)] = None

Response: Get the configured backends and intervals and set up the update buckets.


Instruction:
Create a Python 3 function to
thread target which handles all updates for a given interval

Response:
def update_fileserver(self, interval, backends):
        '''
        Threading target which handles all updates for a given wait interval
        '''
        def _do_update():
            log.debug(
                'Performing fileserver updates for items with an update '
                'interval of %d', interval
            )
            for backend, update_args in six.iteritems(backends):
                backend_name, update_func = backend
                try:
                    if update_args:
                        log.debug(
                            'Updating %s fileserver cache for the following '
                            'targets: %s', backend_name, update_args
                        )
                        args = (update_args,)
                    else:
                        log.debug('Updating %s fileserver cache', backend_name)
                        args = ()

                    update_func(*args)
                except Exception as exc:
                    log.exception(
                        'Uncaught exception while updating %s fileserver '
                        'cache', backend_name
                    )

            log.debug(
                'Completed fileserver updates for items with an update '
                'interval of %d, waiting %d seconds', interval, interval
            )

        condition = threading.Condition()
        _do_update()
        while True:
            with condition:
                condition.wait(interval)
            _do_update()


Instruction:
Write a Python 3 script to
start the update threads

Response:
def run(self):
        '''
        Start the update threads
        '''
        salt.utils.process.appendproctitle(self.__class__.__name__)
        # Clean out the fileserver backend cache
        salt.daemons.masterapi.clean_fsbackend(self.opts)

        for interval in self.buckets:
            self.update_threads[interval] = threading.Thread(
                target=self.update_fileserver,
                args=(interval, self.buckets[interval]),
            )
            self.update_threads[interval].start()

        # Keep the process alive
        while True:
            time.sleep(60)


Instruction:
Make a summary of the following Python 3 code
def _pre_flight(self):
        '''
        Run pre flight checks. If anything in this method fails then the master
        should not start up.
        '''
        errors = []
        critical_errors = []

        try:
            os.chdir('/')
        except OSError as err:
            errors.append(
                'Cannot change to root directory ({0})'.format(err)
            )

        if self.opts.get('fileserver_verify_config', True):
            # Avoid circular import
            import salt.fileserver
            fileserver = salt.fileserver.Fileserver(self.opts)
            if not fileserver.servers:
                errors.append(
                    'Failed to load fileserver backends, the configured backends '
                    'are: {0}'.format(', '.join(self.opts['fileserver_backend']))
                )
            else:
                # Run init() for all backends which support the function, to
                # double-check configuration
                try:
                    fileserver.init()
                except salt.exceptions.FileserverConfigError as exc:
                    critical_errors.append('{0}'.format(exc))

        if not self.opts['fileserver_backend']:
            errors.append('No fileserver backends are configured')

        # Check to see if we need to create a pillar cache dir
        if self.opts['pillar_cache'] and not os.path.isdir(os.path.join(self.opts['cachedir'], 'pillar_cache')):
            try:
                with salt.utils.files.set_umask(0o077):
                    os.mkdir(os.path.join(self.opts['cachedir'], 'pillar_cache'))
            except OSError:
                pass

        if self.opts.get('git_pillar_verify_config', True):
            try:
                git_pillars = [
                    x for x in self.opts.get('ext_pillar', [])
                    if 'git' in x
                    and not isinstance(x['git'], six.string_types)
                ]
            except TypeError:
                git_pillars = []
                critical_errors.append(
                    'Invalid ext_pillar configuration. It is likely that the '
                    'external pillar type was not specified for one or more '
                    'external pillars.'
                )
            if git_pillars:
                try:
                    new_opts = copy.deepcopy(self.opts)
                    import salt.pillar.git_pillar
                    for repo in git_pillars:
                        new_opts['ext_pillar'] = [repo]
                        try:
                            git_pillar = salt.utils.gitfs.GitPillar(
                                new_opts,
                                repo['git'],
                                per_remote_overrides=salt.pillar.git_pillar.PER_REMOTE_OVERRIDES,
                                per_remote_only=salt.pillar.git_pillar.PER_REMOTE_ONLY,
                                global_only=salt.pillar.git_pillar.GLOBAL_ONLY)
                        except salt.exceptions.FileserverConfigError as exc:
                            critical_errors.append(exc.strerror)
                finally:
                    del new_opts

        if errors or critical_errors:
            for error in errors:
                log.error(error)
            for error in critical_errors:
                log.critical(error)
            log.critical('Master failed pre flight checks, exiting\n')
            sys.exit(salt.defaults.exitcodes.EX_GENERIC)

Response: Run pre flight checks.


Instruction:
How would you code a function in Python 3 to
start the master server.

Response:
def start(self):
        '''
        Turn on the master server components
        '''
        self._pre_flight()
        log.info('salt-master is starting as user \'%s\'', salt.utils.user.get_user())

        enable_sigusr1_handler()
        enable_sigusr2_handler()

        self.__set_max_open_files()

        # Reset signals to default ones before adding processes to the process
        # manager. We don't want the processes being started to inherit those
        # signal handlers
        with salt.utils.process.default_signals(signal.SIGINT, signal.SIGTERM):

            # Setup the secrets here because the PubServerChannel may need
            # them as well.
            SMaster.secrets['aes'] = {
                'secret': multiprocessing.Array(
                    ctypes.c_char,
                    salt.utils.stringutils.to_bytes(
                        salt.crypt.Crypticle.generate_key_string()
                    )
                ),
                'reload': salt.crypt.Crypticle.generate_key_string
            }
            log.info('Creating master process manager')
            # Since there are children having their own ProcessManager we should wait for kill more time.
            self.process_manager = salt.utils.process.ProcessManager(wait_for_kill=5)
            pub_channels = []
            log.info('Creating master publisher process')
            log_queue = salt.log.setup.get_multiprocessing_logging_queue()
            for _, opts in iter_transport_opts(self.opts):
                chan = salt.transport.server.PubServerChannel.factory(opts)
                chan.pre_fork(self.process_manager, kwargs={'log_queue': log_queue})
                pub_channels.append(chan)

            log.info('Creating master event publisher process')
            self.process_manager.add_process(salt.utils.event.EventPublisher, args=(self.opts,))

            if self.opts.get('reactor'):
                if isinstance(self.opts['engines'], list):
                    rine = False
                    for item in self.opts['engines']:
                        if 'reactor' in item:
                            rine = True
                            break
                    if not rine:
                        self.opts['engines'].append({'reactor': {}})
                else:
                    if 'reactor' not in self.opts['engines']:
                        log.info('Enabling the reactor engine')
                        self.opts['engines']['reactor'] = {}

            salt.engines.start_engines(self.opts, self.process_manager)

            # must be after channels
            log.info('Creating master maintenance process')
            self.process_manager.add_process(Maintenance, args=(self.opts,))

            if self.opts.get('event_return'):
                log.info('Creating master event return process')
                self.process_manager.add_process(salt.utils.event.EventReturn, args=(self.opts,))

            ext_procs = self.opts.get('ext_processes', [])
            for proc in ext_procs:
                log.info('Creating ext_processes process: %s', proc)
                try:
                    mod = '.'.join(proc.split('.')[:-1])
                    cls = proc.split('.')[-1]
                    _tmp = __import__(mod, globals(), locals(), [cls], -1)
                    cls = _tmp.__getattribute__(cls)
                    self.process_manager.add_process(cls, args=(self.opts,))
                except Exception:
                    log.error('Error creating ext_processes process: %s', proc)

            if HAS_HALITE and 'halite' in self.opts:
                log.info('Creating master halite process')
                self.process_manager.add_process(Halite, args=(self.opts['halite'],))

            # TODO: remove, or at least push into the transport stuff (pre-fork probably makes sense there)
            if self.opts['con_cache']:
                log.info('Creating master concache process')
                self.process_manager.add_process(salt.utils.master.ConnectedCache, args=(self.opts,))
                # workaround for issue #16315, race condition
                log.debug('Sleeping for two seconds to let concache rest')
                time.sleep(2)

            log.info('Creating master request server process')
            kwargs = {}
            if salt.utils.platform.is_windows():
                kwargs['log_queue'] = log_queue
                kwargs['log_queue_level'] = salt.log.setup.get_multiprocessing_logging_level()
                kwargs['secrets'] = SMaster.secrets

            self.process_manager.add_process(
                ReqServer,
                args=(self.opts, self.key, self.master_key),
                kwargs=kwargs,
                name='ReqServer')

            self.process_manager.add_process(
                FileserverUpdate,
                args=(self.opts,))

            # Fire up SSDP discovery publisher
            if self.opts['discovery']:
                if salt.utils.ssdp.SSDPDiscoveryServer.is_available():
                    self.process_manager.add_process(salt.utils.ssdp.SSDPDiscoveryServer(
                        port=self.opts['discovery']['port'],
                        listen_ip=self.opts['interface'],
                        answer={'mapping': self.opts['discovery'].get('mapping', {})}).run)
                else:
                    log.error('Unable to load SSDP: asynchronous IO is not available.')
                    if sys.version_info.major == 2:
                        log.error('You are using Python 2, please install "trollius" module to enable SSDP discovery.')

        # Install the SIGINT/SIGTERM handlers if not done so far
        if signal.getsignal(signal.SIGINT) is signal.SIG_DFL:
            # No custom signal handling was added, install our own
            signal.signal(signal.SIGINT, self._handle_signals)

        if signal.getsignal(signal.SIGTERM) is signal.SIG_DFL:
            # No custom signal handling was added, install our own
            signal.signal(signal.SIGTERM, self._handle_signals)

        self.process_manager.run()


Instruction:
Write a Python 3 script for
binding the reply server to the master process manager.

Response:
def __bind(self):
        '''
        Binds the reply server
        '''
        if self.log_queue is not None:
            salt.log.setup.set_multiprocessing_logging_queue(self.log_queue)
        if self.log_queue_level is not None:
            salt.log.setup.set_multiprocessing_logging_level(self.log_queue_level)
        salt.log.setup.setup_multiprocessing_logging(self.log_queue)
        if self.secrets is not None:
            SMaster.secrets = self.secrets

        dfn = os.path.join(self.opts['cachedir'], '.dfn')
        if os.path.isfile(dfn):
            try:
                if salt.utils.platform.is_windows() and not os.access(dfn, os.W_OK):
                    # Cannot delete read-only files on Windows.
                    os.chmod(dfn, stat.S_IRUSR | stat.S_IWUSR)
                os.remove(dfn)
            except os.error:
                pass

        # Wait for kill should be less then parent's ProcessManager.
        self.process_manager = salt.utils.process.ProcessManager(name='ReqServer_ProcessManager',
                                                                 wait_for_kill=1)

        req_channels = []
        tcp_only = True
        for transport, opts in iter_transport_opts(self.opts):
            chan = salt.transport.server.ReqServerChannel.factory(opts)
            chan.pre_fork(self.process_manager)
            req_channels.append(chan)
            if transport != 'tcp':
                tcp_only = False

        kwargs = {}
        if salt.utils.platform.is_windows():
            kwargs['log_queue'] = self.log_queue
            kwargs['log_queue_level'] = self.log_queue_level
            # Use one worker thread if only the TCP transport is set up on
            # Windows and we are using Python 2. There is load balancer
            # support on Windows for the TCP transport when using Python 3.
            if tcp_only and six.PY2 and int(self.opts['worker_threads']) != 1:
                log.warning('TCP transport supports only 1 worker on Windows '
                            'when using Python 2.')
                self.opts['worker_threads'] = 1

        # Reset signals to default ones before adding processes to the process
        # manager. We don't want the processes being started to inherit those
        # signal handlers
        with salt.utils.process.default_signals(signal.SIGINT, signal.SIGTERM):
            for ind in range(int(self.opts['worker_threads'])):
                name = 'MWorker-{0}'.format(ind)
                self.process_manager.add_process(MWorker,
                                                 args=(self.opts,
                                                       self.master_key,
                                                       self.key,
                                                       req_channels,
                                                       name),
                                                 kwargs=kwargs,
                                                 name=name)
        self.process_manager.run()


Instruction:
How would you explain what the following Python 3 function does
def __bind(self):
        '''
        Bind to the local port
        '''
        # using ZMQIOLoop since we *might* need zmq in there
        install_zmq()
        self.io_loop = ZMQDefaultLoop()
        self.io_loop.make_current()
        for req_channel in self.req_channels:
            req_channel.post_fork(self._handle_payload, io_loop=self.io_loop)  # TODO: cleaner? Maybe lazily?
        try:
            self.io_loop.start()
        except (KeyboardInterrupt, SystemExit):
            # Tornado knows what to do
            pass

Response: Bind to the local port


Instruction:
Write a Python 3 function that can
fire events with stat info if it s time

Response:
def _post_stats(self, stats):
        '''
        Fire events with stat info if it's time
        '''
        end_time = time.time()
        if end_time - self.stat_clock > self.opts['master_stats_event_iter']:
            # Fire the event with the stats and wipe the tracker
            self.aes_funcs.event.fire_event({'time': end_time - self.stat_clock, 'worker': self.name, 'stats': stats}, tagify(self.name, 'stats'))
            self.stats = collections.defaultdict(lambda: {'mean': 0, 'latency': 0, 'runs': 0})
            self.stat_clock = end_time


Instruction:
Can you tell what is the following Python 3 function doing
def _handle_clear(self, load):
        '''
        Process a cleartext command

        :param dict load: Cleartext payload
        :return: The result of passing the load to a function in ClearFuncs corresponding to
                 the command specified in the load's 'cmd' key.
        '''
        log.trace('Clear payload received with command %s', load['cmd'])
        cmd = load['cmd']
        if cmd.startswith('__'):
            return False
        if self.opts['master_stats']:
            start = time.time()
        ret = getattr(self.clear_funcs, cmd)(load), {'fun': 'send_clear'}
        if self.opts['master_stats']:
            stats = salt.utils.event.update_stats(self.stats, start, load)
            self._post_stats(stats)
        return ret

Response:
Process a cleartext command and pass the load to a function in ClearFuncs corresponding to
                .