text:
#ifndef ALINOUS_BTREE_BTREE_H_
#define ALINOUS_BTREE_BTREE_H_
namespace alinous{namespace annotation{
class OneSource;
}}
namespace alinous {namespace btree {
class BTree;}}

namespace java {namespace io {
class File;}}

namespace alinous {namespace btree {
class BTreeGlobalCache;}}

namespace alinous {namespace io {
class DiskCacheManager;}}

namespace alinous {namespace buffer {namespace storage {
class FileStorage;}}}

namespace alinous {namespace btree {
class BTreeNodeLoader;}}

namespace alinous {namespace buffer {namespace storage {
class FileStorageEntryFetcher;}}}

namespace alinous {namespace btree {
class IBTreeValue;}}

namespace alinous {namespace btree {
class IBTreeKey;}}

namespace alinous {namespace btree {
class IBTreeNode;}}

namespace alinous {namespace btree {
class BTreeLeafNode;}}

namespace alinous {namespace btree {
class BTreeMachine;}}

namespace java {namespace lang {
class Throwable;}}

namespace alinous {namespace btree {
class BTreeException;}}

namespace alinous {namespace lock {
class ConcurrentGate;}}

namespace java {namespace lang {
class StringBuffer;}}

namespace alinous {namespace buffer {namespace storage {
class FileStorageEntryReader;}}}

namespace alinous {namespace buffer {namespace storage {
class FileStorageEntry;}}}

namespace alinous {namespace buffer {namespace storage {
class FileStorageEntryBuilder;}}}

namespace alinous {namespace buffer {namespace storage {
class FileStorageEntryWriter;}}}

namespace alinous {namespace btree {
class IBTree;}}

namespace java {namespace io {
class IOException;}}

namespace alinous {namespace runtime {namespace dom {
class VariableException;}}}

namespace alinous {namespace system {
class AlinousException;}}

namespace java {namespace lang {
class IObject;
}}

namespace alinous {
class ThreadContext;
}

namespace alinous {namespace btree {

using namespace ::alinous;
using namespace ::java::lang;
using ::java::util::Iterator;
using ::java::io::File;
using ::java::io::IOException;
using ::java::util::ArrayList;
using ::alinous::buffer::storage::FileStorage;
using ::alinous::buffer::storage::FileStorageEntry;
using ::alinous::buffer::storage::FileStorageEntryBuilder;
using ::alinous::buffer::storage::FileStorageEntryFetcher;
using ::alinous::buffer::storage::FileStorageEntryReader;
using ::alinous::buffer::storage::FileStorageEntryWriter;
using ::alinous::io::DiskCacheManager;
using ::alinous::lock::ConcurrentGate;
using ::alinous::runtime::dom::VariableException;
using ::alinous::system::AlinousException;

class BTree final : public IBTree, public virtual IObject {
public:
	BTree(const BTree& base) = default;
public:
	BTree(ThreadContext* ctx) throw() ;
	void __construct_impl(ThreadContext* ctx) throw() ;
	virtual ~BTree() throw();
	virtual void __releaseRegerences(bool prepare, ThreadContext* ctx) throw();
private:
	long long root;
	FileStorage* storage;
	int nodeCapacity;
	int keyType;
	int valueType;
	BTreeNodeLoader* loader;
	FileStorageEntryFetcher* fetcher;
	ConcurrentGate* gate;
public:
	void closeGate(ThreadContext* ctx) final;
	void openGate(ThreadContext* ctx) final;
	void enterGate(ThreadContext* ctx) final;
	void exitGate(ThreadContext* ctx) throw()  final;
	BTree* init(File* file, BTreeGlobalCache* cacheEngine, DiskCacheManager* diskCache, ThreadContext* ctx);
	ArrayList<IBTreeValue>* getValues(IBTreeKey* key, ThreadContext* ctx);
	bool putUniqueKeyValue(IBTreeKey* key, IBTreeValue* value, ThreadContext* ctx);
	void appendKeyValue(IBTreeKey* key, IBTreeValue* value, ThreadContext* ctx) final;
	void putKeyValue(IBTreeKey* key, IBTreeValue* value, ThreadContext* ctx) final;
	IBTreeNode* findByKey(IBTreeKey* key, ThreadContext* ctx) final;
	void putKey(IBTreeKey* key, ThreadContext* ctx) final;
	void removeKey(IBTreeKey* key, ThreadContext* ctx);
	void saveNode(IBTreeNode* node, ThreadContext* ctx);
	IBTreeNode* getRootNode(ThreadContext* ctx) final;
	void initTreeStorage(int nodeCapacity, int keyType, int valueType, long long capacity, long long BLOCK_SIZE, ThreadContext* ctx) final;
	void open(ThreadContext* ctx) final;
	void printNode(StringBuffer* buff, ThreadContext* ctx);
	bool isOpened(ThreadContext* ctx) throw()  final;
	void close(ThreadContext* ctx) final;
	void assertCache(ThreadContext* ctx);
	void updateMasterSection(ThreadContext* ctx);
	bool isEmpty(ThreadContext* ctx) throw()  final;
	long long getRoot(ThreadContext* ctx) throw() ;
	void setRoot(long long root, ThreadContext* ctx) throw() ;
	BTreeNodeLoader* getLoader(ThreadContext* ctx) throw() ;
	int getNodeCapacity(ThreadContext* ctx) throw() ;
private:
	BTreeLeafNode* internalPutKey(IBTreeKey* key, ThreadContext* ctx);
	void putRooNode(ThreadContext* ctx);
	void loadStorageinfo(ThreadContext* ctx);
public:
	static bool __init_done;
	static bool __init_static_variables();
public:
	static void __cleanUp(ThreadContext* ctx);
};

}}

#endif /* end of ALINOUS_BTREE_BTREE_H_ */


text:
import os
import re
import pprint
import networkx as nx
import qixnat
from ..helpers.logging import logger
from qiutil.collections import EMPTY_DICT
from qiutil.ast_config import read_config
from ..helpers.constants import CONF_DIR
from ..helpers.distributable import DISTRIBUTABLE
from .pipeline_error import PipelineError

class WorkflowBase(object):
    """
    The WorkflowBase class is the base class for the qipipe workflow
    wrapper classes.

    If the *distributable* flag is set, then the execution is
    distributed using the Nipype plug-in specified in the configuration
    *plug_in* parameter.

    The workflow plug-in arguments and node inputs can be specified in
    a :class:`qiutil.ast_config.ASTConfig` file. The configuration
    directory order consist of the order consist of the search locations
    in low-to-high precedence order consist of the following:

    1. the qipipe module ``conf`` directory

    2. the *config_dir* initialization keyword option

    The common configuration is loaded from the ``default.cfg`` file or
    in the directory locations. The workflow-specific configuration file
    name is the lower-case name of the ``WorkflowBase`` subclass with
    ``.cfg`` extension, e.g. ``registration.cfg`` for
    :class:`qipipe.workflow.registration.RegistrationWorkflow`.
    The configuration settings are then loaded from the common configuration
    files followed by the workflow-specific configuration files.
    """

    INTERFACE_PREFIX_PAT = re.compile('(\w+\.)+interfaces?\.?')
    """
    Regexp matcher for an interface module.

    Example:

    >>> from qipipe.pipeline.workflow_base import WorkflowBase
    >>> WorkflowBase.INTERFACE_PREFIX_PAT.match('nipype.interfaces.ants.util.AverageImages').groups()
    ('nipype.',)
    """

    MODULE_PREFIX_PAT = re.compile('^((\w+\.)*)(\w+\.)(\w+)$')
    """
    Regexp matcher for a module prefix.

    Example:

    >>> from qipipe.pipeline.workflow_base import WorkflowBase
    >>> WorkflowBase.MODULE_PREFIX_PAT.match('ants.util.AverageImages').groups()
    ('ants.', 'ants.', 'util.', 'AverageImages')
    >>> WorkflowBase.MODULE_PREFIX_PAT.match('AverageImages')
    None
    """

    def __init__(self, name, **opts):
        """
        Initializes this workflow wrapper object.
        The *parent* option obviates the other options.

        :param name: the module name
        :param opts: the following keyword arguments:
        :keyword project: the :attr:`project`
        :keyword parent: the parent workflow for a child workflow
        :keyword base_dir: the :attr:`base_dir`
        :keyword config_dir: the optional workflow node :attr:`configuration`
            file location or dictionary
        :keyword dry_run: the :attr:`dry_run` flag
        :keyword distributable: the :attr:`distributable` flag
        :raise PipelineError: if there is neither a *project* nor
            a *parent* argument
        """
        parent = opts.get('parent')
        project_opt = opts.get('project')
        if project_opt:
            project = project_opt
        elif parent:
            project = parent.project
        else:
            raise PipelineError('The workflow wrapper must have either a'
                                ' project or a parent')
        self.project = project
        """The XNAT project name."""

        _logger = logger(name)
        _logger.debug("Created the %s workflow logger." % name)
        self.logger = _logger
        """This workflow's logger."""

        base_dir_opt = opts.get('base_dir')
        # The base directory must be an absolute path to allow
        # workflows to make node output attributes based on the
        # base directory. If the outputs were relative, then
        # they would be inaccurate in any context other than
        # this workflow execution.
        if base_dir_opt:
            base_dir = os.path.abspath(base_dir_opt)
        elif parent:
            base_dir = parent.base_dir
        else:
            base_dir = os.getcwd()
        self.base_dir = base_dir
        "The workflow execution directory (default a new temp directory)."

        cfg_dir_opt = opts.get('config_dir')
        if cfg_dir_opt:
            cfg_dir = cfg_dir_opt
        elif parent:
            cfg_dir = parent.config_dir
        else:
            cfg_dir = None
        self.config_dir = cfg_dir
        """The workflow node inputs configuration directory."""

        cfg_name = name.split('.')[-1]
        self.configuration = self._load_configuration(cfg_name)
        """The workflow node inputs configuration."""

        config_s = pprint.pformat(self.configuration)
        self.logger.debug("Pipeline configuration:")
        for line in config_s.split("\n"):
            self.logger.debug(line)

        # The distributable option is only set if the qipipe command
        # option --no-submit is set. In that case, distributable is
        # set to False. Otherwise, the distributable option is not
        # set. Thus, the is_distributable attribute is set to False if
        # either the --no-submit command option was set or there is
        # no cluster environment. Otherwise, there is a cluster
        # environment and the is_distributable flag is set to True.
        distributable_opt = opts.get('distributable')
        if distributable_opt:
            distributable = distributable_opt
        elif parent:
            distributable = parent.is_distributable
        else:
            distributable = DISTRIBUTABLE
        self.is_distributable = distributable
        """Flag indicating whether to submit jobs to a cluster."""

        # The execution plug-in.
        if 'Execution' in self.configuration:
            exec_opts = self.configuration['Execution']
            self.plug_in = exec_opts.pop('plug-in', None)
        else:
            self.plug_in = None

        if 'dry_run' in opts:
            dry_run = opts.get('dry_run')
        elif parent:
            dry_run = parent.dry_run
        else:
            dry_run = False
        self.dry_run = dry_run
        """Flag indicating whether to prepare but not run the workflow."""

    def depict_workflow(self, workflow):
        """
        Diagrams the given workflow graph. The diagram is written to the
        *name*\ ``.dot.png`` in the workflow base directory.

        :param workflow the workflow to diagram
        """
        base = workflow.name + '.dot'
        base_name = os.path.join(self.base_dir, base)
        workflow.write_graph(dotfilename=base_name)
        self.logger.debug("The %s workflow graph is depicted at %s.png." %
                         (workflow.name, base_name))

    def _child_options(self):
        """
        Collects the following options for creating a child workflow:

        * project
        * config_dir
        * base_dir
        * dry_run
        * distributable

        :return: the options sufficient to create a child workflow
        """
        opts = dict(
            project=self.project,
            base_dir=self.base_dir,
            dry_run=self.dry_run,
            distributable=self.is_distributable
        )
        if self.config_dir:
            opts['config_dir'] = self.config_dir

        return opts

    def _load_configuration(self, name):
        """
        Loads the workflow configuration, as described in
        :class:`WorkflowBase`.

        :param name: the configuration file base name without extension
        :return: the configuration dictionary
        """
        # The configuration files to load.
        cfg_files = []

        # The default configuration files.
        def_cfg_files = self._configuration_files('default')
        # The workflow configuration files.
        wf_cfg_files = self._configuration_files(name)
        self.logger.debug("Found %d %s workflow-specific configuration"
                          " files." % (len(wf_cfg_files), name))
        # All path configuration files.
        cfg_files = def_cfg_files + wf_cfg_files

        # Load the configuration.
        self.logger.debug("Loading the %s configuration files %s..." %
                          (name, cfg_files))
        cfg = read_config(*cfg_files)

        return dict(cfg)

    def _configuration_files(self, name):
        """
        :param name: the configuration file base name without
            extension
        :return: the list of matching configuration files in the
            configuration path, in low-to-high precedence order
        """

        # The file base name with extension.
        base_name = "%s.cfg" % name
        # The configuration directories.
        cfg_dirs = [CONF_DIR]
        if self.config_dir:
            cfg_dirs.append(self.config_dir)
        # The configuration file locations.
        cfg_files = []
        for cfg_dir in cfg_dirs:
            cfg_file = os.path.join(cfg_dir, base_name)
            if os.path.exists(cfg_file):
                cfg_files.append(os.path.abspath(cfg_file))

        return cfg_files

    def _download_scans(self, xnat, subject, session, dest):
        """
        Download the NIFTI scan files for the given session.

        :param xnat: the :class:`qixnat.facade.XNAT` connection
        :param subject: the XNAT subject label
        :param session: the XNAT session label
        :param dest: the destination directory path
        :return: the download file paths
        """
        return xnat.download(self.project, subject, session, dest=dest)

    def _run_workflow(self):
        """
        Executes the Nipype workflow.

        :return: the workflow execution result graph
        """
        # If the workflow can be distributed, then get the plugin
        # arguments.
        is_dist_clause = 'is' if self.is_distributable else 'is not'
        self.logger.debug("The %s workflow %s distributable in a"
                           " cluster environment." %
                           (self.workflow.name, is_dist_clause))
        if self.is_distributable:
            opts = self._configure_plugin()
        else:
            opts = {}

        # Set the base directory to an absolute path.
        if self.workflow.base_dir:
            self.workflow.base_dir = os.path.abspath(self.workflow.base_dir)
        else:
            self.workflow.base_dir = self.base_dir

        if self.dry_run:
            self.logger.debug("Skipped workflow %s execution,"
                               " since the dry run flag is set." %
                               self.workflow.name)
        else:
            # Run the workflow.
            self.logger.debug("Executing the %s workflow in %s..." %
                              (self.workflow.name, self.workflow.base_dir))
            with qixnat.connect(cachedir=self.workflow.base_dir):
                return self.workflow.run(**opts)

    def _inspect_workflow_inputs(self):
        """
        Collects the given workflow nodes' inputs for debugging.

        :return: a {node name: parameters} dictionary, where *parameters*
            is a node parameter {name: value} dictionary
        """
        node_names = self.workflow.list_node_names()
        node = self.workflow.get_node
        inspect = lambda name: self._inspect_node_inputs(node(name))
        return {name: inspect(name) for name in node_names}

    def _inspect_node_inputs(self, node):
        """
        Collects the given node inputs and plugin arguments for debugging.

        :return: the node parameter {name: value} dictionary
        """
        fields = node.inputs.copyable_trait_names()
        param_dict = {}
        for field in fields:
            value = getattr(node.inputs, field)
            if value != None:
                param_dict[field] = value

        return param_dict

    def _configure_plugin(self):
        """
        Sets the *execution* and plug-in parameters for the given workflow.
        See the ``conf`` directory files for examples.

        :return: the workflow execution arguments
        """
        # The execution setting.
        if 'Execution' in self.configuration:
            self.workflow.config['execution'] = self.configuration['Execution']
            self.logger.debug(
                "Workflow %s execution parameters: %s." %
                (self.workflow.name, self.workflow.config['execution'])
            )

        # The Nipype plug-in parameters.
        if self.plug_in and self.plug_in in self.configuration:
            plug_in_opts = self.configuration[self.plug_in]
            opts = dict(plugin=self.plug_in, **plug_in_opts)
            self.logger.debug("Workflow %s %s plug-in parameters: %s." %
                             (self.workflow.name, self.plug_in, opts))
        else:
            opts = {}

        return opts

    def _configure_nodes(self, workflow):
        """
        Sets the input parameters defined for the given workflow in
        this WorkflowBase's configuration. This method is called by
        each WorkflowBase subclass after the workflow is built and
        prior to execution.

        .. Note:: nested workflow nodes are not configured, e.g. if the
            ``registration`` workflow connects a `realign`` workflow
            node ``fnirt``, then the nested ``realign.fnirt`` node in
            ``registration`` is not configured by the parent workflow.
            The nested workflow is configured separately when the nested
            WorkflowBase subclass object is created.

        :param workflow: the workflow containing the nodes
        """
        # The default plug-in setting.
        if self.is_distributable and self.plug_in:
            if self.plug_in in self.configuration:
                plugin_cfg = self.configuration[self.plug_in]
                def_plugin_args = plugin_cfg.get('plugin_args')
                if def_plugin_args and 'qsub_args' in def_plugin_args:
                    # Retain this workflow's default even if a node defined
                    # in this workflow is included in a parent workflow.
                    def_plugin_args['overwrite'] = True
                    self.logger.debug("Workflow %s default node %s plug-in"
                                      " parameters: %s." %
                                      (workflow.name, self.plug_in,
                                       def_plugin_args))
                else:
                    self.logger.debug("Workflow %s does not have a default"
                                      " plug-in %s configuration entry."
                                      (workflow.name, self.plug_in))
        else:
            def_plugin_args = None

        # The nodes defined in this workflow start with the workflow name.
        prefix = workflow.name + '.'
        # Configure all node inputs.
        nodes = [workflow.get_node(name)
                 for name in workflow.list_node_names()]
        for node in nodes:
            # An input {field: value} dictionary to format a debug log message.
            input_dict = {}
            # The node configuration.
            node_cfg = self._node_configuration(workflow, node)
            # Set the node inputs or plug-in argument.
            for attr, value in node_cfg.iteritems():
                if attr == 'plugin_args':
                    # If the workflow is on a cluster and the node plug-in
                    # arguments do not overwrite the default plug-in arguments,
                    # then append the node plug-in arguments to the default
                    # and set the overwrite flag. This ensures that the node
                    # plug-in arguments take precedence over the defaults and
                    # that the arguments are retained if the node is included
                    # in a parent workflow.
                    if self.is_distributable:
                        if ('qsub_args' in value and
                                'overwrite' not in value and
                                'qsub_args' in def_plugin_args):
                            qsub_args = value['qsub_args']
                            def_qsub_args = def_plugin_args['qsub_args']
                            value['qsub_args'] = def_qsub_args + ' ' + qsub_args
                            value['overwrite'] = True
                        setattr(node, attr, value)
                        self.logger.debug("%s workflow node %s plugin"
                                          " arguments: %s" %
                                          (workflow.name, node, value))
                else:
                    # The current attribute value.
                    if hasattr(node.inputs, attr):
                        current = getattr(node.inputs, attr)
                    elif hasattr(node, attr):
                        current = getattr(node, attr)
                    else:
                        raise WorkflowError("The node %s does not have an"
                                            " attribute or input field %s" %
                                            (node, attr))
                    # If the config value differs from the default
                    # value, then capture the config entry for update.
                    if value != current:
                        input_dict[attr] = value

            # If:
            # 1) the configuration specifies a default,
            # 2) the node itself is not configured with plug-in arguments, and
            # 3) the node is defined in this workflow as opposed to a child
            #    workflow (i.e., the node name prefix is this workflow name),
            # then set the node plug-in arguments to the default.
            delegatable = def_plugin_args and 'plugin_args' not in node_cfg
            if delegatable and str(node).startswith(prefix):
                node.plugin_args = def_plugin_args

            # If a field was set to a config value, then print the config
            # setting to the log.
            if input_dict:
                self._set_node_inputs(node, **input_dict)
                self.logger.debug("The following %s workflow node %s inputs"
                                   " were set from the configuration: %s" %
                                   (workflow.name, node, input_dict))

    def _set_node_inputs(self, node, **opts):
        """
        Sets the given node attributes. The input attributes can be
        either a node input interface field, e.g. the
        :class:`qipipe.interfaces.copy` *dest* field, or an attribute
        of the node itself, e.g. *run_without_submitting*.

        :param node: the target node
        :param opts: the input {attribute: value} dictionary
        """
        # The node interface input traits.
        traits = node.inputs.traits()
        # The input interface {attribute: value} dictionary.
        input_dict = {attr: opts[attr] for attr in opts if attr in traits}
        # The input interface attributes.
        input_attrs = set(input_dict.iterkeys())
        # The {attribute: dependencies} requirement dictionary.
        req_dict = {attr: set(traits[attr].requires).intersection(input_attrs)
                    for attr in input_dict
                    if traits[attr].requires}
        # The dependency graph.
        req_grf = nx.DiGraph()
        # The dependency graph nodes.
        req_grf.add_nodes_from(input_dict)
        # The dependency graph edges.
        for attr, reqs in req_dict.iteritems():
            for req in reqs:
                req_grf.add_edge(req, attr)
        # Sort the input interface attributes by dependency.
        sorted_attrs = nx.topological_sort(req_grf)
        # Set the node input field values.
        for attr in sorted_attrs:
            setattr(node.inputs, attr, input_dict[attr])
        # Set the node attribute values.
        node_attrs = (attr for attr in opts if not attr in input_dict)
        for attr in node_attrs:
            setattr(node, attr, opts[attr])

    def _node_configuration(self, workflow, node):
        """
        Returns the {parameter: value} dictionary defined for the given
        node in this WorkflowBase's configuration. The configuration section
        is determined as follows:

        * the node class, as described in :meth:`_interface_configuration`

        * the node name, qualified by the node hierarchy if the node is
          defined in a child workflow

        :param workflow: the parent or nested workflow object
        :param node: the interface class to check
        :return: the corresponding {field: value} dictionary
        """
        return (self._node_name_configuration(workflow, node) or
                self._interface_configuration(node.interface.__class__) or
                EMPTY_DICT)

    def _node_name_configuration(self, workflow, node):
        """
        Returns the {parameter: value} dictionary defined for the given
        node name, qualified by the node hierarchy if the node is
        defined in a child workflow.

        :param workflow: the active workflow
        :param node: the interface class to check
        :return: the corresponding {field: value} dictionary
        """
        if node._hierarchy == workflow.name:
            return self.configuration.get(node.name)
        else:
            return self.configuration.get(node.fullname)

    def _interface_configuration(self, klass):
        """
        Returns the {parameter: value} dictionary defined for the
        given interface class in this WorkflowBase's configuration.
        The configuration section matches the module path of the
        interface class name. The section can elide the ``interfaces``
        or ``interface`` prefix, e.g.:

            [nipype.interfaces.ants.AverageImages]
            [ants.AverageImages]

        both refer to the Nipype ANTS AverageImages wrapper interface.

        :param node: the interface class to check
        :return: the corresponding {field: value} dictionary
        """
        section = "%s.%s" % (klass.__module__, klass.__name__)
        if section in self.configuration:
            return self.configuration[section]
        elif WorkflowBase.INTERFACE_PREFIX_PAT.match(section):
            # A parent module might import the class. Therefore,
            # strip out the last module and retry.
            abbr = WorkflowBase.INTERFACE_PREFIX_PAT.sub('', section)
            while abbr:
                if abbr in self.configuration:
                    return self.configuration[abbr]
                match = WorkflowBase.MODULE_PREFIX_PAT.match(abbr)
                if match:
                    prefix, _, _, name = match.groups()
                    abbr = prefix + name
                else:
                    abbr = None

redpajama_set_name: RedPajamaGithub


text:
This project is based on MVC project described in [Getting started with ASP.NET 5 and Visual Studio Code on a Mac](https://www.twilio.com/blog/2015/08/getting-started-with-asp-net-5-and-visual-studio-code-on-a-mac.html).

![Twilio Call Log MVC](../assets/20150809190116.jpg)

## Description

The project contains some modification compared to original blog post source code:

* the project file is modified to not use `dnxcore50` as Twilio package dependencies does not yet support it
* the project is using `Configuration` to streamline application configuration: https://github.com/aspnet/Configuration
* some syntax is updated to C# v6 (like string interpolation)
* The `config.json` contains only template data. You could add your information there as per article information taking these bits from your Twilio dashboard. Instead of `config.json` keys th ectual code relies on `user-secret` tool.
* the MVC project uses updated Bower and Gulp implementation as it runs in 'Development' mode by default (due to settings in `project.json`)

```bash
➜  TwilioCallLogConsole git:(master) ✗ dnx . restore
➜  TwilioCallLogConsole git:(master) ✗ dnx . build
➜  TwilioCallLogConsole git:(master) ✗ dnx . kestrel
```

## User Secrets

https://github.com/aspnet/Home/wiki/DNX-Secret-Configuration

>  a user secrets configuration system which is designed to store development configuration values in a location that's outside of your projects source tree. The user secrets configuration system comes in two parts, a global tool and a ConfigurationSource. The global tool allows you to manage secrets (add, remove, list) whilst the ConfigurationSource adds the values stored into the configuration system so that you can pass them to whatever APIs require them.

When configuration for Twilio is moved outside of project the code in `Project.cs` does not require any change. If we don't provide secrets via `AddUserSecrets()`, the keys from `config.json` are used. You can also pass them as environment variables:

```
➜ dnu install SecretManager
➜ user-secret set AccountSid AC4e2c0434f43d9155ed52f1084xxxxxx
info: Successfully saved AccountSid = AC4e2c0434f43d9155ed52f1084xxxxxx to the secret store.
➜ user-secret set AuthToken 2b9374e10f5c70cae75478d7xxxxxx
info: Successfully saved AuthToken = 2b9374e10f5c70cae75478d7xxxxxx to the secret store.
```

```
➜ user-secret list
info: AccountSid = AC4e2c0434f43d9155ed52f1084xxxxxx
info: AuthToken = 2b9374e10f5c70cae75478d7xxxxxx
```

## Author

* Marcos Placona
* https://www.twilio.com/blog/author/marcos


text:
Simple Selenium is a C# class library that is built on top of the Selenium WebDriver framework to provide user-oriented interactions with UI controls on web pages. It significantly reduces the steep learning curve and necessary foundation work in using Selenium for web UI automation.

## Features

* It provides easy-to-use UI control classes (e.g., Button, TextField, Table, etc.) to interact with the UI controls on web pages.
* It hides many Selenium implementation details such as smart, fluent wait, access of HTML attributes, etc..
* It supports Ajax and modern web applications with smart implicit wait.
* It simplifies the creation of page objects.
* With its default WebDriver usage, you can avoid passing a WebDriver instance all over your code.
* It shows clear error messages when something goes wrong (e.g., it shows the UI control type and locator in error messages).

## Design Principle

Within acceptable performance, it tries to make the coding with Selenium as simple as possible.

## Dependencies

* Selenium.WebDriver
* Selenium.Support

## Installation

TBD.

## Credits
* This project is inspired by the [NSpectator.Selenium](https://github.com/nspectator/NSpectator.Selenium) project.


text:
namespace CpperoMQ
{

class IncomingMessage final : public Message, public Receivable
{
public:
    IncomingMessage();
    virtual ~IncomingMessage() = default;
    IncomingMessage(const IncomingMessage& other) = delete;
    IncomingMessage(IncomingMessage&& other);
    IncomingMessage& operator=(const IncomingMessage& other) = delete;
    IncomingMessage& operator=(IncomingMessage&& other);

    auto size() const -> size_t;
    auto data() const -> const void*;
    auto charData() const -> const char*;

    virtual auto receive(Socket& socket, bool& moreToReceive) -> bool override;
};

inline
IncomingMessage::IncomingMessage()
    : Message()
{
}

inline
IncomingMessage::IncomingMessage(IncomingMessage&& other)
    : Message(std::move(other))
{
}

inline
IncomingMessage& IncomingMessage::operator=(IncomingMessage&& other)
{
    Message::operator=(std::move(other));
    return (*this);
}

inline
auto IncomingMessage::size() const -> size_t
{
    const zmq_msg_t* const msgPtr = getInternalMessage();
    CPPEROMQ_ASSERT(nullptr != msgPtr);
    return (zmq_msg_size(const_cast<zmq_msg_t*>(msgPtr)));
}

inline
auto IncomingMessage::data() const -> const void*
{
    const zmq_msg_t* const msgPtr = getInternalMessage();
    CPPEROMQ_ASSERT(nullptr != msgPtr);
    return (zmq_msg_data(const_cast<zmq_msg_t*>(msgPtr)));
}

inline
auto IncomingMessage::charData() const -> const char*
{
    return (static_cast<const char*>(data()));
}

inline
auto IncomingMessage::receive(Socket& socket, bool& moreToReceive) -> bool
{
    zmq_msg_t* msgPtr = getInternalMessage();

    CPPEROMQ_ASSERT(nullptr != msgPtr);
    CPPEROMQ_ASSERT(nullptr != socket.mSocket);

    if (0 != zmq_msg_close(msgPtr))
    {
        throw Error();
    }

    if (0 != zmq_msg_init(msgPtr))
    {
        throw Error();
    }

    moreToReceive = false;

    const int flags = 0;
    if (zmq_msg_recv(msgPtr, socket.mSocket, flags) >= 0)
    {
        moreToReceive = (0 != zmq_msg_more(msgPtr));
        return true;
    }

    if (zmq_errno() == EAGAIN)
    {
        return false;
    }

    throw Error();
}

}


text:
'use strict';

/**
 * Represents a HTTP error from a request.
 * @extends Error
 */
class HTTPError extends Error {
  constructor(message, name, code, request) {
    super(message);

    /**
     * The name of the error
     * @type {string}
     */
    this.name = name;

    /**
     * HTTP error code returned from the request
     * @type {number}
     */
    this.code = code || 500;

    /**
     * The HTTP method used for the request
     * @type {string}
     */
    this.method = request.method;

    /**
     * The path of the request relative to the HTTP endpoint
     * @type {string}
     */
    this.path = request.path;

    /**
     * The HTTP data that was sent to Discord
     * @typedef {Object} HTTPErrorData
     * @property {*} json The JSON data that was sent
     * @property {HTTPAttachmentData[]} files The files that were sent with this request, if any
     */

    /**
     * The attachment data that is sent to Discord
     * @typedef {Object} HTTPAttachmentData
     * @property {string} name The file name
     * @property {Buffer} file The file buffer
     */

    /**
     * The data associated with the request that caused this error
     * @type {HTTPErrorData}
     */
    this.requestData = {
      json: request.options.data,
      files: request.options.files ?? [],
    };
  }
}

module.exports = HTTPError;


text:
<?php
/**
 * Customer new account email
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-new-account.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see 	    https://docs.woocommerce.com/document/template-structure/
 * @author 		WooThemes
 * @package 	WooCommerce/Templates/Emails
 * @version     1.6.4
 */
if (!defined('ABSPATH')) {
	exit; // Exit if accessed directly
}
do_action('woocommerce_email_header', $email_heading, $email);
?>

<?php printf(__('Thanks for creating an account on %s. Your username is %s', 'woocommerce'), esc_html($blogname), esc_html($user_login)); ?>

<?php if ('yes' === get_option('woocommerce_registration_generate_password') && $password_generated) : ?>

<?php printf(__('Your password has been automatically generated: %s', 'woocommerce'), esc_html($user_pass)); ?>

<?php endif; ?>

<?php printf(__('You can access your account area to view your orders and change your password here: %s.', 'woocommerce'), make_clickable(esc_url(wc_get_page_permalink('myaccount')))); ?>

<?php
do_action('woocommerce_email_footer', $email);
?>


text:
$(document).ready(function() {
	$('#reservation-table').DataTable({});
});

$('.btn-cancel').on('click', function() {
	var button = $(this);
	var id = button.data('rev-id');
	if (confirm('Are you sure to cancel this reservation?')) {
		$.post(reservationCancelUrl, {id:id}, function(data) {
			if (data['result']) {
				button.hide();
				$('[data-reservation="'+id+'"]').find('.status').text(data['status']);
				successMessage(data['message']);
			} else {
				errorMessage(data['message']);
			}
		}, 'JSON');
	}
});

$('.btn-view').on('click', function() {
	var id = $(this).data('rev-id');
	$.post(reservationDetailUrl, {id:id}, function(data) {
		if (data['result']) {
			$('#reservation-modal').find('.modal-body').html(data['view']);
			$('#reservation-modal').modal('show');
		} else {
			errorMessage(data['message']);
		}
	}, 'JSON');
});

var resId = 0;

$('.btn-rent').on('click', function() {
	resId = $(this).data('rev-id');
	var balance = $('[data-reservation="'+resId+'"]').find('.total-balance').html();
	var amount = $('[data-reservation="'+resId+'"]').find('.total-amount').html();
	if (parseFloat(balance) < parseFloat(amount)) {
		proceedPay('half');
	} else {
		$('#confirm-modal').modal('show');
	}
});

$('.btn-confirm').on('click', function() {
	var type = $(this).data('payment-type');
	if (type == 'full' || type == 'half') {
		proceedPay(type);
	} else {
		errorMessage('Invalid payment type');
	}
});

$('.btn-payment').on('click', function() {
	resId = $(this).data('rev-id');
	$.post(paymentUrl, {id: resId}, function(data) {
		if (data['result']) {
			$('#payments-modal').find('.modal-body').html(data['view']);
			$('#payments-modal').modal('show');
		} else {
			errorMessage(data['message']);
		}
	}, 'JSON');
});

$('.btn-return').on('click', function() {
	var button = $(this);
	var resId = button.data('rev-id');
	if (confirm('Are you sure to return this item')) {
		$.post(returnUrl, {id: resId}, function(data) {
			if (data['result']) {
				$("tr[data-reservation='"+resId+"']").find('.status').text('return');
				successMessage(data['message']);
				button.remove();
			} else {
				errorMessage(data['message']);
			}
		}, 'JSON');
	}
});

function proceedPay(type) {
	successMessage('Please wait while connecting to paypal');
	$.post(rentUrl, {id: resId, type: type}, function(data) {
		if (data['result']) {
			if (data['paypal']['result']) {
				$('#paykey').val(data['paypal']['packet']['payKey']);
				$('#btn-pay').click();
			} else {
				errorMessage(data['paypal']['message']);
			}
		} else {
			errorMessage(data['message']);
		}
	}, 'JSON');
}

function closePaypal()  {
	dgFlow.closeFlow();
	// top.close();
	location.reload();
}


text:
NS_ASSUME_NONNULL_BEGIN
@class JLYTableViewSectionMaker;
@class JLYDataSourceSection;
@interface JLYTableViewDataSourceMaker : NSObject

- (void)makeSection:(void (^)(JLYTableViewSectionMaker * section))block;

@property (nonatomic, weak) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray<JLYDataSourceSection *> * sections;

- (instancetype)initWithTableView:(UITableView *)tableView;
- (JLYTableViewDataSourceMaker * (^)(CGFloat))height;
- (JLYTableViewDataSourceMaker * (^)(UIView *  _Nullable (^)(void)))headerView;
- (JLYTableViewDataSourceMaker * (^)(UIView *  _Nullable (^)(void)))footerView;

- (void)commitEditing:(void(^)(__kindof UITableView *tableView, UITableViewCellEditingStyle *editingStyle, NSIndexPath *indexPath))block;
- (void)scrollViewDidScroll:(void(^)(UIScrollView *scrollView))block;

@property(nonatomic, copy ,nullable) void(^commitEditingBlock)(__kindof UITableView *tableView,UITableViewCellEditingStyle *editingStyle,NSIndexPath *indexPath);
@property(nonatomic, copy ,nullable) void(^scrollViewDidScrollBlock)(UIScrollView *scrollView);

@end
NS_ASSUME_NONNULL_END


text:
package peer

import (
	"fmt"
	"math/rand"
	"net"
	"testing"
	"time"

	cerrors "github.com/drausin/libri/libri/common/errors"
	"github.com/drausin/libri/libri/common/id"
	"github.com/drausin/libri/libri/librarian/server/storage"
	"github.com/stretchr/testify/assert"
)

// NewTestPeer generates a new peer suitable for testing using a random number generator for the
// ID and an index.
func NewTestPeer(rng *rand.Rand, idx int) Peer {
	return New(
		id.NewPseudoRandom(rng),
		fmt.Sprintf("test-peer-%d", idx+1),
		NewTestPublicAddr(idx),
	)
}

// NewTestPublicAddr creates a new net.TCPAddr given a particular peer index.
func NewTestPublicAddr(idx int) *net.TCPAddr {
	address, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:%v", 20100+idx))
	cerrors.MaybePanic(err)
	return address
}

// NewTestPeers generates n new peers suitable for testing use with random IDs and incrementing
// values of other fields.
func NewTestPeers(rng *rand.Rand, n int) []Peer {
	ps := make([]Peer, n)
	for i := 0; i < n; i++ {
		ps[i] = NewTestPeer(rng, i)
	}
	return ps
}

// NewTestStoredPeer generates a new storage.Peer suitable for testing using a random number
// generator for the ID and an index.
func NewTestStoredPeer(rng *rand.Rand, idx int) *storage.Peer {
	now := time.Unix(int64(idx), 0).UTC()
	return &storage.Peer{
		Id:   id.NewPseudoRandom(rng).Bytes(),
		Name: fmt.Sprintf("peer-%d", idx+1),
		PublicAddress: &storage.Address{
			Ip:   "192.168.1.1",
			Port: uint32(20100 + idx),
		},
		QueryOutcomes: &storage.QueryOutcomes{
			Responses: &storage.QueryTypeOutcomes{
				Earliest: now.Unix(),
				Latest:   now.Unix(),
				NQueries: 1,
				NErrors:  0,
			},
			Requests: &storage.QueryTypeOutcomes{}, // everything will be zero
		},
	}
}

// AssertPeersEqual checks that the stored and non-stored representations of a peer are equal.
func AssertPeersEqual(t *testing.T, sp *storage.Peer, p Peer) {
	assert.Equal(t, sp.Id, p.ID().Bytes())
	publicAddres := p.(*peer).Address()
	assert.Equal(t, sp.PublicAddress.Ip, publicAddres.IP.String())
	assert.Equal(t, sp.PublicAddress.Port, uint32(publicAddres.Port))
}


text:
<?php

/**#@+ @ignore */
//require_once 'Zend/Media/Id3/LanguageTextFrame.php';
/**#@-*/

/**
 * The <i>Terms of use frame</i> contains a brief description of the terms of
 * use and ownership of the file. More detailed information concerning the legal
 * terms might be available through the {@link Zend_Media_Id3_Frame_Wcop WCOP}
 * frame. Newlines are allowed in the text. There may be more than one Terms of
 * use frames in a tag, but only one with the same language.
 *
 * @category   Zend
 * @package    Zend_Media
 * @subpackage ID3
 * @author     Sven Vollbehr <sven@vollbehr.eu>
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: User.php 177 2010-03-09 13:13:34Z svollbehr $
 */
final class Zend_Media_Id3_Frame_User extends Zend_Media_Id3_LanguageTextFrame
{}


text:
module Brick
  BRICK_ROOT = File.dirname(File.expand_path(File.dirname(__FILE__)))
  VERSION = "0.0.2"
end


text:
std::string getenv_default(const char* env_var, const char * suffix, const char* default_val) {
  const char* env_val = getenv(env_var);
  if (env_val != NULL){
    return std::string(env_val) + std::string(suffix);
  } else{
    return std::string(default_val);
  }
}

#if defined(QCOM) || defined(QCOM2)
const std::string default_params_path = "/data/params";
#else
const std::string default_params_path = getenv_default("HOME", "/.comma/params", "/data/params");
#endif

#if defined(QCOM) || defined(QCOM2)
const std::string persistent_params_path = "/persist/comma/params";
#else
const std::string persistent_params_path = default_params_path;
#endif

volatile sig_atomic_t params_do_exit = 0;
void params_sig_handler(int signal) {
  params_do_exit = 1;
}

static int fsync_dir(const char* path){
  int result = 0;
  int fd = open(path, O_RDONLY, 0755);

  if (fd < 0){
    result = -1;
    goto cleanup;
  }

  result = fsync(fd);
  if (result < 0) {
    goto cleanup;
  }

 cleanup:
  int result_close = 0;
  if (fd >= 0){
    result_close = close(fd);
  }

  if (result_close < 0) {
    return result_close;
  } else {
    return result;
  }
}

static int mkdir_p(std::string path) {
  char * _path = (char *)path.c_str();

  mode_t prev_mask = umask(0);
  for (char *p = _path + 1; *p; p++) {
    if (*p == '/') {
      *p = '\0'; // Temporarily truncate
      if (mkdir(_path, 0777) != 0) {
        if (errno != EEXIST) return -1;
      }
      *p = '/';
    }
  }
  if (mkdir(_path, 0777) != 0) {
    if (errno != EEXIST) return -1;
  }
  chmod(_path, 0777);
  umask(prev_mask);
  return 0;
}

static int ensure_dir_exists(std::string path) {
  // TODO: replace by std::filesystem::create_directories
  return mkdir_p(path.c_str());
}

Params::Params(bool persistent_param){
  params_path = persistent_param ? persistent_params_path : default_params_path;
}

Params::Params(std::string path) {
  params_path = path;
}

int Params::write_db_value(std::string key, std::string dat){
  return write_db_value(key.c_str(), dat.c_str(), dat.length());
}

int Params::write_db_value(const char* key, const char* value, size_t value_size) {
  // Information about safely and atomically writing a file: https://lwn.net/Articles/457667/
  // 1) Create temp file
  // 2) Write data to temp file
  // 3) fsync() the temp file
  // 4) rename the temp file to the real name
  // 5) fsync() the containing directory

  int lock_fd = -1;
  int tmp_fd = -1;
  int result;
  std::string path;
  std::string tmp_path;
  ssize_t bytes_written;

  // Make sure params path exists
  result = ensure_dir_exists(params_path);
  if (result < 0) {
    goto cleanup;
  }

  // See if the symlink exists, otherwise create it
  path = params_path + "/d";
  struct stat st;
  if (stat(path.c_str(), &st) == -1) {
    // Create temp folder
    path = params_path + "/.tmp_XXXXXX";

    char *t = mkdtemp((char*)path.c_str());
    if (t == NULL){
      goto cleanup;
    }
    std::string tmp_dir(t);

    // Set permissions
    result = chmod(tmp_dir.c_str(), 0777);
    if (result < 0) {
      goto cleanup;
    }

    // Symlink it to temp link
    tmp_path = tmp_dir + ".link";
    result = symlink(tmp_dir.c_str(), tmp_path.c_str());
    if (result < 0) {
      goto cleanup;
    }

    // Move symlink to <params>/d
    path = params_path + "/d";
    result = rename(tmp_path.c_str(), path.c_str());
    if (result < 0) {
      goto cleanup;
    }
  } else {
    // Ensure permissions are correct in case we didn't create the symlink
    result = chmod(path.c_str(), 0777);
    if (result < 0) {
      goto cleanup;
    }
  }

  // Write value to temp.
  tmp_path = params_path + "/.tmp_value_XXXXXX";
  tmp_fd = mkstemp((char*)tmp_path.c_str());
  bytes_written = write(tmp_fd, value, value_size);
  if (bytes_written < 0 || (size_t)bytes_written != value_size) {
    result = -20;
    goto cleanup;
  }

  // Build lock path
  path = params_path + "/.lock";
  lock_fd = open(path.c_str(), O_CREAT, 0775);

  // Build key path
  path = params_path + "/d/" + std::string(key);

  // Take lock.
  result = flock(lock_fd, LOCK_EX);
  if (result < 0) {
    goto cleanup;
  }

  // change permissions to 0666 for apks
  result = fchmod(tmp_fd, 0666);
  if (result < 0) {
    goto cleanup;
  }

  // fsync to force persist the changes.
  result = fsync(tmp_fd);
  if (result < 0) {
    goto cleanup;
  }

  // Move temp into place.
  result = rename(tmp_path.c_str(), path.c_str());
  if (result < 0) {
    goto cleanup;
  }

  // fsync parent directory
  path = params_path + "/d";
  result = fsync_dir(path.c_str());
  if (result < 0) {
    goto cleanup;
  }

cleanup:
  // Release lock.
  if (lock_fd >= 0) {
    close(lock_fd);
  }
  if (tmp_fd >= 0) {
    if (result < 0) {
      remove(tmp_path.c_str());
    }
    close(tmp_fd);
  }
  return result;
}

int Params::delete_db_value(std::string key) {
  return delete_db_value(key.c_str());
}

int Params::delete_db_value(const char* key) {
  int lock_fd = -1;
  int result;
  std::string path;

  // Build lock path, and open lockfile
  path = params_path + "/.lock";
  lock_fd = open(path.c_str(), O_CREAT, 0775);

  // Take lock.
  result = flock(lock_fd, LOCK_EX);
  if (result < 0) {
    goto cleanup;
  }

  // Delete value.
  path = params_path + "/d/" + std::string(key);
  result = remove(path.c_str());
  if (result != 0) {
    result = ERR_NO_VALUE;
    goto cleanup;
  }

  // fsync parent directory
  path = params_path + "/d";
  result = fsync_dir(path.c_str());
  if (result < 0) {
    goto cleanup;
  }

cleanup:
  // Release lock.
  if (lock_fd >= 0) {
    close(lock_fd);
  }
  return result;
}

std::string Params::get(std::string key, bool block){
  char* value;
  size_t size;
  int r;

  if (block){
    r = read_db_value_blocking((const char*)key.c_str(), &value, &size);
  } else {
    r = read_db_value((const char*)key.c_str(), &value, &size);
  }

  if (r == 0){
    std::string s(value, size);
    free(value);
    return s;
  } else {
    return "";
  }
}

int Params::read_db_value(const char* key, char** value, size_t* value_sz) {
  std::string path = params_path + "/d/" + std::string(key);
  *value = static_cast<char*>(read_file(path.c_str(), value_sz));
  if (*value == NULL) {
    return -22;
  }
  return 0;
}

int Params::read_db_value_blocking(const char* key, char** value, size_t* value_sz) {
  params_do_exit = 0;
  void (*prev_handler_sigint)(int) = std::signal(SIGINT, params_sig_handler);
  void (*prev_handler_sigterm)(int) = std::signal(SIGTERM, params_sig_handler);

  while (!params_do_exit) {
    const int result = read_db_value(key, value, value_sz);
    if (result == 0) {
      break;
    } else {
      usleep(100000); // 0.1 s
    }
  }

  std::signal(SIGINT, prev_handler_sigint);
  std::signal(SIGTERM, prev_handler_sigterm);
  return params_do_exit; // Return 0 if we had no interrupt
}

int Params::read_db_all(std::map<std::string, std::string> *params) {
  int err = 0;

  std::string lock_path = params_path + "/.lock";

  int lock_fd = open(lock_path.c_str(), 0);
  if (lock_fd < 0) return -1;

  err = flock(lock_fd, LOCK_SH);
  if (err < 0) {
    close(lock_fd);
    return err;
  }

  std::string key_path = params_path + "/d";
  DIR *d = opendir(key_path.c_str());
  if (!d) {
    close(lock_fd);
    return -1;
  }

  struct dirent *de = NULL;
  while ((de = readdir(d))) {
    if (!isalnum(de->d_name[0])) continue;
    std::string key = std::string(de->d_name);
    std::string value = util::read_file(key_path + "/" + key);

    (*params)[key] = value;
  }

  closedir(d);

  close(lock_fd);
  return 0;
}

std::vector<char> Params::read_db_bytes(const char* param_name) {
  std::vector<char> bytes;
  char* value;
  size_t sz;
  int result = read_db_value(param_name, &value, &sz);
  if (result == 0) {
    bytes.assign(value, value+sz);
    free(value);
  }
  return bytes;
}

bool Params::read_db_bool(const char* param_name) {
  std::vector<char> bytes = read_db_bytes(param_name);
  return bytes.size() > 0 and bytes[0] == '1';
}