text:
from common_fixtures import *  # NOQA
test_network_policy = os.environ.get(
    'TEST_NETWORK_POLICY', "False")

np_reason = \
    'Intended to not execute this network policy test'

if_network_policy = pytest.mark.skipif(test_network_policy != "ALL",
                                       reason=np_reason)
if_network_policy_none = pytest.mark.skipif(
    test_network_policy != "NONE",
    reason=np_reason)
if_network_policy_within_stack = pytest.mark.skipif(
    test_network_policy != "WITHIN_STACK",
    reason=np_reason)
if_network_policy_within_service = pytest.mark.skipif(
    test_network_policy != "WITHIN_SERVICE",
    reason=np_reason)
if_network_policy_within_linked = pytest.mark.skipif(
    test_network_policy != "WITHIN_LINKED",
    reason=np_reason)
if_network_policy_groupby = pytest.mark.skipif(
    test_network_policy != "WITHIN_GROUPBY",
    reason=np_reason)

NETWORKPOLICY_SUBDIR = \
    os.path.join(os.path.dirname(os.path.realpath(__file__)),
                 'resources/networkpolicy')
policy_within_stack = {"within": "stack", "action": "allow"}
policy_groupby = {"between": {"groupBy": "com.rancher.stack.location"},
                  "action": "allow"}
policy_within_service = {"within": "service", "action": "allow"}
policy_within_linked = {"within": "linked", "action": "allow"}
shared_environment = {"env": []}

@pytest.fixture(scope='session', autouse=True)
def create_env_for_network_policy(request, client, socat_containers):
    assert check_for_network_policy_manager(client)
    env2 = create_stack_with_service(client, "test2", NETWORKPOLICY_SUBDIR,
                                     "stack2.yml", "stack2-rc.yml")
    assert len(env2.services()) == 6
    env1 = create_stack_with_service(client, "test1", NETWORKPOLICY_SUBDIR,
                                     "stack1.yml", "stack1-rc.yml")
    assert len(env1.services()) == 11

    create_standalone_containers(client)
    time.sleep(sleep_interval)
    populate_env_details(client)

    def fin():
        to_delete = [env1, env2]
        delete_all(client, to_delete)
        delete_all(client, shared_environment["containers"])
        delete_all(client, shared_environment["containers_with_label"])

    request.addfinalizer(fin)

def populate_env_details(client):
    env = client.list_stack(name="test1").data
    assert len(env) == 1
    env1 = env[0]
    env = client.list_stack(name="test2").data
    assert len(env) == 1
    env2 = env[0]

    shared_environment["env"].append(env1)
    shared_environment["env"].append(env2)
    shared_environment["stack1_test1allow"] = \
        get_service_by_name(client, env1,  "test1allow")
    shared_environment["stack1_test2allow"] = \
        get_service_by_name(client, env1,  "test2allow")
    shared_environment["stack1_test3deny"] = \
        get_service_by_name(client, env1,  "test3deny")
    shared_environment["stack1_test4deny"] = \
        get_service_by_name(client, env1,  "test4deny")
    shared_environment["stack1_lbwithinstack"] = \
        get_service_by_name(client, env1,  "lbwithininstack")
    shared_environment["stack1_lbcrossstack"] = \
        get_service_by_name(client, env1,  "lbcrossstack")
    shared_environment["stack1_servicewithlinks"] = \
        get_service_by_name(client, env1,  "servicewithlinks")
    shared_environment["stack1_servicecrosslinks"] = \
        get_service_by_name(client, env1,  "servicecrosslinks")
    shared_environment["stack1_servicelinktosidekick"] = \
        get_service_by_name(client, env1,  "servicelinktosidekick")
    shared_environment["stack1_linktowebservice"] = \
        get_service_by_name(client, env1,  "linktowebservice")

    shared_environment["stack2_test1allow"] = \
        get_service_by_name(client, env2,  "test1allow")
    shared_environment["stack2_test2allow"] = \
        get_service_by_name(client, env2,  "test2allow")
    shared_environment["stack2_test3deny"] = \
        get_service_by_name(client, env2,  "test3deny")
    shared_environment["stack2_test4deny"] = \
        get_service_by_name(client, env2,  "test4deny")

    service_with_sidekick = {}
    service_with_sidekick["p_con1"] = \
        get_container_by_name(client, "test2-testp1-1")
    service_with_sidekick["p_con2"] = \
        get_container_by_name(client, "test2-testp1-2")
    service_with_sidekick["s1_con1"] = \
        get_container_by_name(client, "test2-testp1-tests1-1")
    service_with_sidekick["s1_con2"] = \
        get_container_by_name(client, "test2-testp1-tests1-2")
    service_with_sidekick["s2_con1"] = \
        get_container_by_name(client, "test2-testp1-tests2-1")
    service_with_sidekick["s2_con2"] = \
        get_container_by_name(client, "test2-testp1-tests2-2")
    shared_environment["stack2_sidekick"] = service_with_sidekick
    time.sleep(sleep_interval)

def validate_default_network_action_deny_networkpolicy_allow_within_stacks(
        client):
    # Validate that standalone containers are not able reach any
    # service containers
    for container in shared_environment["containers"]:
        validate_connectivity_between_con_to_services(
            client, container,
            [shared_environment["stack1_test2allow"],
             shared_environment["stack2_test4deny"]],
            connection="deny")
    # Validate that there connectivity between containers of different
    # services within the same stack is allowed
    validate_connectivity_between_services(
        client, shared_environment["stack1_test1allow"],
        [shared_environment["stack1_test2allow"],
         shared_environment["stack1_test3deny"],
         shared_environment["stack1_test4deny"]],
        connection="allow")
    # Validate that there is no connectivity between containers of different
    # services across stacks
    validate_connectivity_between_services(
        client, shared_environment["stack1_test1allow"],
        [shared_environment["stack2_test1allow"],
         shared_environment["stack2_test2allow"],
         shared_environment["stack2_test3deny"],
         shared_environment["stack2_test4deny"]],
        connection="deny")
    # Validate that LB is able reach all targets which are in the same stack as
    # Lb
    validate_lb_service(client,
                        shared_environment["stack1_lbwithinstack"],
                        "9091",
                        [shared_environment["stack1_test1allow"]])
    # Validate that LB is able reach all targets which are in the same stack as
    # Lb
    validate_linked_service(client,
                            shared_environment["stack1_servicewithlinks"],
                            [shared_environment["stack1_test1allow"]],
                            "99")
    # Cross stacks access for links should be denied
    validate_linked_service(client,
                            shared_environment["stack1_servicecrosslinks"],
                            [shared_environment["stack2_test2allow"]],
                            "98", linkName="test2allow.test2",
                            not_reachable=True)
    # Cross stacks access for LBs should be denied
    validate_lb_service_for_no_access(
        client, shared_environment["stack1_lbcrossstack"], "9090")

def validate_default_network_action_deny_networkpolicy_none(
        client):
    # Validate that standalone containers are not able reach any
    # service containers
    for container in shared_environment["containers"]:
        validate_connectivity_between_con_to_services(
            client, container,
            [shared_environment["stack1_test2allow"],
             shared_environment["stack2_test4deny"]],
            connection="deny")
    # Validate that there is no connectivity between containers of different
    # services across stacks and within stacks
    validate_connectivity_between_services(
        client, shared_environment["stack1_test1allow"],
        [shared_environment["stack1_test2allow"],
         shared_environment["stack1_test3deny"],
         shared_environment["stack1_test4deny"],
         shared_environment["stack2_test1allow"],
         shared_environment["stack2_test2allow"],
         shared_environment["stack2_test3deny"],
         shared_environment["stack2_test4deny"]],
        connection="deny")
    # Validate that Lb service is not able to reach targets within the
    # same stack and cross stacks
    validate_lb_service_for_no_access(
        client, shared_environment["stack1_lbwithinstack"], "9091")
    validate_lb_service_for_no_access(
        client, shared_environment["stack1_lbcrossstack"], "9090")

    # Validate that connectivity between linked service is denied within the
    # same stack and  cross stacks
    validate_linked_service(client,
                            shared_environment["stack1_servicewithlinks"],
                            [shared_environment["stack1_test1allow"]],
                            "99", not_reachable=True)
    validate_linked_service(client,
                            shared_environment["stack1_servicecrosslinks"],
                            [shared_environment["stack2_test2allow"]],
                            "98", linkName="test2allow.test2",
                            not_reachable=True)

def validate_default_network_action_deny_networkpolicy_groupby(
        client):
    # Validate that containers that do not have the labels defined
    # in group by policy are not allowed to communicate with other
    # service containers
    for container in shared_environment["containers"]:
        validate_connectivity_between_con_to_services(
            client, container,
            [shared_environment["stack1_test2allow"],
             shared_environment["stack2_test4deny"]],
            connection="deny")
    # Validate that stand alone  containers that have the labels defined
    # in group by policy are allowed to communicate with service containers
    # having the same labels
    for container in shared_environment["containers_with_label"]:
        validate_connectivity_between_con_to_services(
            client, container,
            [shared_environment["stack1_test2allow"],
             shared_environment["stack2_test1allow"],
             shared_environment["stack2_test2allow"]],
            connection="allow")

    # Validate that service containers that have matching labels defined
    # in group by policy are allowed to communicate with each other
    validate_connectivity_between_services(
        client, shared_environment["stack1_test1allow"],
        [shared_environment["stack1_test2allow"],
         shared_environment["stack2_test1allow"],
         shared_environment["stack2_test2allow"]],
        connection="allow")

    # Validate that all service containers within the same service that has
    # group by labels are able to communicate with each other
    validate_connectivity_between_services(
        client, shared_environment["stack1_test3deny"],
        [shared_environment["stack2_test3deny"]],
        connection="allow")

    # Validate that service containers that do not have matching labels defined
    # in group by policy are not allowed to communicate with each other
    validate_connectivity_between_services(
        client, shared_environment["stack1_test1allow"],
        [shared_environment["stack1_test3deny"],
         shared_environment["stack1_test4deny"],
         shared_environment["stack2_test3deny"],
         shared_environment["stack2_test4deny"]],
        connection="deny")
    validate_connectivity_between_services(
        client, shared_environment["stack1_test3deny"],
        [shared_environment["stack1_test1allow"],
         shared_environment["stack1_test2allow"],
         shared_environment["stack1_test4deny"],
         shared_environment["stack2_test1allow"],
         shared_environment["stack2_test2allow"],
         shared_environment["stack2_test4deny"]],
        connection="deny")

def validate_default_network_action_deny_networkpolicy_within_service(
        client):
    # Validate that standalone containers are not able reach any
    # service containers
    for container in shared_environment["containers"]:
        validate_connectivity_between_con_to_services(
            client, container,
            [shared_environment["stack1_test1allow"],
             shared_environment["stack2_test4deny"]],
            connection="deny")

    # Validate that containers belonging to the same service are able to
    # communicate with each other
    validate_connectivity_between_services(
        client, shared_environment["stack1_test1allow"],
        [shared_environment["stack1_test1allow"]],
        connection="allow")

    # Validate that containers belonging to the different services within
    # the same stack or cross stack are not able to communicate with each other
    validate_connectivity_between_services(
        client, shared_environment["stack1_test1allow"],
        [shared_environment["stack1_test2allow"],
         shared_environment["stack2_test1allow"],
         shared_environment["stack2_test2allow"]],
        connection="deny")

    # Validate that Lb services has no access to targets with in
    # same stacks or cross stacks
    validate_lb_service_for_no_access(
        client, shared_environment["stack1_lbcrossstack"], "9090")
    validate_lb_service_for_no_access(
        client, shared_environment["stack1_lbwithinstack"], "9091")

    # Validate that connectivity between linked service is denied within the
    # same stack and  cross stacks
    validate_linked_service(
        client, shared_environment["stack1_servicewithlinks"],
        [shared_environment["stack1_test1allow"]], "99", not_reachable=True)
    validate_linked_service(client,
                            shared_environment["stack1_servicecrosslinks"],
                            [shared_environment["stack2_test2allow"]],
                            "98", linkName="test2allow.test2",
                            not_reachable=True)

def validate_default_network_action_deny_networkpolicy_within_service_for_sk(
        client):
    # Validate that containers of primary services are able to connect with
    # other containers in the same service and containers in other sidekick
    # services
    validate_connectivity_between_container_list(
        client,
        shared_environment["stack2_sidekick"]["p_con1"],
        [shared_environment["stack2_sidekick"]["p_con2"],
         shared_environment["stack2_sidekick"]["s1_con1"],
         shared_environment["stack2_sidekick"]["s1_con2"],
         shared_environment["stack2_sidekick"]["s2_con1"],
         shared_environment["stack2_sidekick"]["s2_con2"]],
        "allow")

    # Validate that containers of sidekick services are able to connect with
    # other containers in the same service and containers in other sidekick
    # services and primary service

    validate_connectivity_between_container_list(
        client,
        shared_environment["stack2_sidekick"]["s1_con1"],
        [shared_environment["stack2_sidekick"]["p_con1"],
         shared_environment["stack2_sidekick"]["p_con2"],
         shared_environment["stack2_sidekick"]["s1_con2"],
         shared_environment["stack2_sidekick"]["s2_con1"],
         shared_environment["stack2_sidekick"]["s2_con2"]],
        "allow")

    validate_connectivity_between_container_list(
        client,
        shared_environment["stack2_sidekick"]["s2_con1"],
        [shared_environment["stack2_sidekick"]["p_con1"],
         shared_environment["stack2_sidekick"]["p_con2"],
         shared_environment["stack2_sidekick"]["s1_con1"],
         shared_environment["stack2_sidekick"]["s1_con1"],
         shared_environment["stack2_sidekick"]["s2_con2"]],
        "allow")

def validate_default_network_action_deny_networkpolicy_within_linked(
        client):
    # Validate that standalone containers are not able reach any
    # service containers
    for container in shared_environment["containers"]:
        validate_connectivity_between_con_to_services(
            client, container,
            [shared_environment["stack1_test2allow"],
             shared_environment["stack2_test4deny"]],
            connection="deny")
    # Validate that containers belonging to a service are not able to
    # communicate with other containers in the same service or different
    # service
    validate_connectivity_between_services(
        client, shared_environment["stack1_test1allow"],
        [shared_environment["stack1_test1allow"],
         shared_environment["stack1_test2allow"],
         shared_environment["stack2_test1allow"],
         shared_environment["stack2_test2allow"]],
        connection="deny")

    # Validate that Lb services has access to targets with in
    # same stacks
    validate_lb_service(client,
                        shared_environment["stack1_lbwithinstack"],
                        "9091",
                        [shared_environment["stack1_test1allow"]])

    # Validate that Lb services has access to targets cross stacks
    validate_lb_service(client,
                        shared_environment["stack1_lbcrossstack"],
                        "9090",
                        [shared_environment["stack2_test1allow"]])

    service_with_links = shared_environment["stack1_servicewithlinks"]
    linked_service = [shared_environment["stack1_test1allow"]]
    validate_dna_deny_np_within_linked_for_linked_service(
        client, service_with_links, linked_service, "99")

    service_with_links = shared_environment["stack1_servicecrosslinks"]
    linked_service = [shared_environment["stack2_test1allow"]]
    validate_dna_deny_np_within_linked_for_linked_service(
        client, service_with_links, linked_service, "98", "mylink")

def validate_dna_deny_np_within_linked_for_linked_service(
        client, service_with_links, linked_service, port, linkName=None):

    # Validate that all containers of a service with link has access to
    # the containers of the service that it is linked to
    validate_connectivity_between_services(
        client,
        service_with_links,
        linked_service,
        connection="allow")

    # Validate that all containers of a service that is linked by other service
    # has no access to the containers of the service that it is linked by
    # (s1 -> s2) containers of s2 have no access to s1
    for l_service in linked_service:
        validate_connectivity_between_services(
            client,
            l_service,
            [service_with_links],
            connection="deny")

    # Validate that containers are reachable using their link name
    validate_linked_service(client,
                            service_with_links,
                            linked_service,
                            port,
                            linkName=linkName)

def validate_default_network_action_deny_networkpolicy_within_linked_for_sk(
        client):
    containers = get_service_container_list(
        client, shared_environment["stack1_servicelinktosidekick"])
    # Validate connectivity between containers of linked services to linked
    # service with sidekick
    for con in containers:
        validate_connectivity_between_container_list(
            client,
            con,
            shared_environment["stack2_sidekick"].values(),
            "allow")
    for linked_con in shared_environment["stack2_sidekick"].values():
        for con in containers:
            validate_connectivity_between_containers(
                client, linked_con, con, "deny")

def validate_dna_deny_np_within_linked_for_servicealias(
        client):
    # Validate connectivity between containers of linked services to services
    # linked to webservice
    validate_connectivity_between_services(
        client, shared_environment["stack1_linktowebservice"],
        [shared_environment["stack1_test4deny"],
         shared_environment["stack2_test3deny"]],
        connection="allow")

    validate_connectivity_between_services(
        client, shared_environment["stack1_test4deny"],
        [shared_environment["stack1_linktowebservice"]],
        connection="deny")

    validate_connectivity_between_services(
        client, shared_environment["stack2_tes34deny"],
        [shared_environment["stack1_linktowebservice"]],
        connection="deny")

@if_network_policy
def test_default_network_action_deny_networkpolicy_allow_within_stacks(
        client):
    set_network_policy(client, "deny", policy_within_stack)
    validate_default_network_action_deny_networkpolicy_allow_within_stacks(
        client)

@if_network_policy_within_stack
def test_dna_deny_np_allow_within_stacks_stop_service(
        client, socat_containers):
    set_network_policy(client, "deny", policy_within_stack)
    stop_service_instances(client, shared_environment["env"][0],
                           shared_environment["stack1_test1allow"], [1])
    validate_default_network_action_deny_networkpolicy_allow_within_stacks(
        client)

@if_network_policy_within_stack
def test_dna_deny_np_allow_within_stacks_delete_service(
        client, socat_containers):
    set_network_policy(client, "deny", policy_within_stack)
    delete_service_instances(client, shared_environment["env"][0],
                             shared_environment["stack1_test1allow"], [1])
    validate_default_network_action_deny_networkpolicy_allow_within_stacks(
        client)

@if_network_policy_within_stack
def test_dna_deny_np_allow_within_stacks_restart_service(
        client, socat_containers):
    set_network_policy(client, "deny", policy_within_stack)
    restart_service_instances(client, shared_environment["env"][0],
                              shared_environment["stack1_test1allow"], [1])
    validate_default_network_action_deny_networkpolicy_allow_within_stacks(
        client)

@if_network_policy
def test_default_network_action_deny_networkpolicy_none(client):
    set_network_policy(client, "deny")
    validate_default_network_action_deny_networkpolicy_none(
        client)

@if_network_policy_none
def test_dna_deny_np_none_stop_service(
        client, socat_containers):
    set_network_policy(client, "deny")
    stop_service_instances(client, shared_environment["env"][0],
                           shared_environment["stack1_test1allow"], [1])
    validate_default_network_action_deny_networkpolicy_none(
        client)

@if_network_policy_none
def test_dna_deny_np_none_delete_service(
        client, socat_containers):
    set_network_policy(client, "deny")
    delete_service_instances(client, shared_environment["env"][0],
                             shared_environment["stack1_test1allow"], [1])
    validate_default_network_action_deny_networkpolicy_none(
        client)

@if_network_policy_none
def test_dna_deny_np_none_restart_service(
        client, socat_containers):
    set_network_policy(client, "deny")
    restart_service_instances(client, shared_environment["env"][0],
                              shared_environment["stack1_test1allow"], [1])
    validate_default_network_action_deny_networkpolicy_none(
        client)

@if_network_policy
def test_default_network_action_deny_networkpolicy_groupby(
        client):
    set_network_policy(client, "deny", policy_groupby)
    validate_default_network_action_deny_networkpolicy_groupby(
        client)

@if_network_policy_groupby
def test_dna_deny_np_groupby_stop_service(
        client, socat_containers):
    set_network_policy(client, "deny", policy_groupby)
    stop_service_instances(client, shared_environment["env"][0],
                           shared_environment["stack1_test1allow"], [1])
    validate_default_network_action_deny_networkpolicy_groupby(
        client)

@if_network_policy_groupby
def test_dna_deny_np_groupby_delete_service(
        client, socat_containers):
    set_network_policy(client, "deny", policy_groupby)
    delete_service_instances(client, shared_environment["env"][0],
                             shared_environment["stack1_test1allow"], [1])
    validate_default_network_action_deny_networkpolicy_groupby(
        client)

@if_network_policy_groupby
def test_dna_deny_np_groupby_restart_service(
        client, socat_containers):
    set_network_policy(client, "deny", policy_groupby)
    restart_service_instances(client, shared_environment["env"][0],
                              shared_environment["stack1_test1allow"], [1])
    validate_default_network_action_deny_networkpolicy_groupby(
        client)

@if_network_policy
def test_default_network_action_deny_networkpolicy_allow_within_service(
        client):
    set_network_policy(client, "deny", policy_within_service)
    validate_default_network_action_deny_networkpolicy_within_service(
        client)

@if_network_policy_within_service
def test_dna_deny_np_allow_within_service_delete_service(
        client):
    set_network_policy(client, "deny", policy_within_service)
    delete_service_instances(client, shared_environment["env"][0],
                             shared_environment["stack1_test1allow"], [1])
    delete_service_instances(client, shared_environment["env"][0],
                             shared_environment["stack1_lbcrossstack"], [1])
    delete_service_instances(client, shared_environment["env"][0],
                             shared_environment["stack1_lbwithinstack"], [1])
    delete_service_instances(
        client, shared_environment["env"][0],
        shared_environment["stack1_servicewithlinks"], [1])
    validate_default_network_action_deny_networkpolicy_within_service(
        client)

@if_network_policy_within_service
def test_dna_deny_np_allow_within_service_scale_service(
        client):
    set_network_policy(client, "deny", policy_within_service)
    scale_service(shared_environment["stack1_test1allow"], client, 3)
    scale_service(shared_environment["stack1_lbcrossstack"], client, 3)
    scale_service(shared_environment["stack1_lbwithinstack"], client, 3)
    scale_service(shared_environment["stack1_servicewithlinks"], client, 3)
    populate_env_details(client)
    validate_default_network_action_deny_networkpolicy_within_service(
        client)
    scale_service(shared_environment["stack1_test1allow"], client, 2)
    scale_service(shared_environment["stack1_lbcrossstack"], client, 2)
    scale_service(shared_environment["stack1_lbwithinstack"], client, 2)
    scale_service(shared_environment["stack1_servicewithlinks"], client, 2)

@if_network_policy_within_service
def test_dna_deny_np_allow_within_service_stop_service(
        client):
    set_network_policy(client, "deny", policy_within_service)
    validate_default_network_action_deny_networkpolicy_within_service(
        client)
    stop_service_instances(client, shared_environment["env"][0],
                           shared_environment["stack1_test1allow"], [1])
    stop_service_instances(client, shared_environment["env"][0],
                           shared_environment["stack1_lbcrossstack"], [1])
    stop_service_instances(client, shared_environment["env"][0],
                           shared_environment["stack1_lbwithinstack"], [1])
    stop_service_instances(
        client, shared_environment["env"][0],
        shared_environment["stack1_servicewithlinks"], [1])

    validate_default_network_action_deny_networkpolicy_within_service(
        client)

@if_network_policy
def test_dna_deny_np_allow_within_service_check_sidekicks(
        client):
    set_network_policy(client, "deny", policy_within_service)
    validate_default_network_action_deny_networkpolicy_within_service_for_sk(
        client)

@if_network_policy
def test_default_network_action_deny_networkpolicy_allow_within_linked(
        client):
    set_network_policy(client, "deny", policy_within_linked)
    validate_default_network_action_deny_networkpolicy_within_linked(
        client)

@if_network_policy
def test_dna_deny_np_allow_within_linked_for_sk(
        client):
    set_network_policy(client, "deny", policy_within_linked)
    validate_default_network_action_deny_networkpolicy_within_linked_for_sk(
        client)

@if_network_policy
def test_dna_deny_np_allow_within_linked_for_sa(
        client):
    set_network_policy(client, "deny", policy_within_linked)
    validate_dna_deny_np_within_linked_for_servicealias(
        client)

@if_network_policy_within_linked
def test_dna_deny_np_allow_within_linked_after_scaleup(
        client):
    set_network_policy(client, "deny", policy_within_linked)

    service_with_links = shared_environment["stack1_servicewithlinks"]
    linked_service = shared_environment["stack1_test1allow"]
    validate_dna_deny_np_within_linked_for_linked_service(
        client, service_with_links, [linked_service], "99")

    scale_service(linked_service, client, 3)
    shared_environment["stack1_test1allow"] = \
        get_service_by_name(client,
                            shared_environment["env"][0],
                            "test1allow")
    linked_service = shared_environment["stack1_test1allow"]

    validate_dna_deny_np_within_linked_for_linked_service(
        client, service_with_links, [linked_service], "99")

    scale_service(linked_service, client, 2)
    shared_environment["stack1_test1allow"] = \
        get_service_by_name(client,
                            shared_environment["env"][0],
                            "test1allow")
    linked_service = shared_environment["stack1_test1allow"]

    scale_service(service_with_links, client, 3)
    shared_environment["stack1_servicewithlinks"] = \
        get_service_by_name(client,
                            shared_environment["env"][0],
                            "servicewithlinks")
    service_with_links = shared_environment["stack1_servicewithlinks"]

    validate_dna_deny_np_within_linked_for_linked_service(
        client, service_with_links, [linked_service], "99")
    scale_service(service_with_links, client, 2)
    shared_environment["stack1_servicewithlinks"] = \
        get_service_by_name(client,
                            shared_environment["env"][0],
                            "servicewithlinks")

@if_network_policy_within_linked
def test_dna_deny_np_allow_within_linked_after_adding_removing_links(
        client):
    set_network_policy(client, "deny", policy_within_linked)

    service_with_links = shared_environment["stack1_servicewithlinks"]
    linked_service = [shared_environment["stack1_test1allow"]]
    validate_dna_deny_np_within_linked_for_linked_service(
        client, service_with_links, linked_service, "99")

    # Add another service link
    service_with_links.setservicelinks(
        serviceLinks=[
            {"serviceId": shared_environment["stack1_test1allow"].id},
            {"serviceId": shared_environment["stack1_test2allow"].id}])

    validate_dna_deny_np_within_linked_for_linked_service(
        client, service_with_links,
        [shared_environment["stack1_test1allow"]], "99")
    validate_dna_deny_np_within_linked_for_linked_service(
        client, service_with_links,
        [shared_environment["stack1_test2allow"]], "99")

    # Remove existing service link
    service_with_links.setservicelinks(
        serviceLinks=[
            {"serviceId": shared_environment["stack1_test1allow"].id}])
    linked_service = [shared_environment["stack1_test1allow"]]
    validate_dna_deny_np_within_linked_for_linked_service(
        client, service_with_links, linked_service, "99")
    validate_connectivity_between_services(
        client, service_with_links,
        [shared_environment["stack1_test2allow"]],
        connection="deny")
    validate_connectivity_between_services(
        client, shared_environment["stack1_test2allow"],
        [service_with_links],
        connection="deny")

def scale_service(service, client, final_scale):
    service = client.update(service, name=service.name, scale=final_scale)
    service = client.wait_success(service, 300)
    assert service.state == "active"
    assert service.scale == final_scale
    check_container_in_service(client, service)

def set_network_policy(client, defaultPolicyAction="allow", policy=None):
    networks = client.list_network(name='ipsec').data
    assert len(networks) == 1
    network = networks[0]
    network = client.update(
        network, defaultPolicyAction=defaultPolicyAction, policy=policy)
    network = wait_success(client, network)
    assert network.defaultPolicyAction == defaultPolicyAction
    populate_env_details(client)

def check_for_network_policy_manager(client):
    np_manager = False
    env = client.list_stack(name="network-policy-manager").data
    if len(env) == 1:
        service = get_service_by_name(client, env[0],
                                      "network-policy-manager")
        if service.state == "active":
            np_manager = True
    return np_manager

def create_standalone_containers(client):
    hosts = client.list_host(kind='docker', removed_null=True).data
    cons = []
    cons_with_label = []
    for host in hosts:
        con_name = random_str()
        con = client.create_container(
            name=con_name,
            ports=['3001:22'],
            imageUuid=HEALTH_CHECK_IMAGE_UUID,
            networkMode=MANAGED_NETWORK,
            requestedHostId=host.id)
        con = client.wait_success(con)
        assert con.state == "running"
        cons.append(con)
    shared_environment["containers"] = cons
    for host in hosts:
        con_name = random_str()
        con = client.create_container(
            name=con_name,
            ports=['3002:22'],
            imageUuid=HEALTH_CHECK_IMAGE_UUID,
            networkMode=MANAGED_NETWORK,
            requestedHostId=host.id,
            labels={"com.rancher.stack.location": "east"})
        con = client.wait_success(con)
        assert con.state == "running"
        cons_with_label.append(con)
    shared_environment["containers_with_label"] = cons_with_label


text:
class PackageNode;

class QEPackageCommand : public DAVA::Command
{
public:
    QEPackageCommand(PackageNode* package, const DAVA::String& description = "");

protected:
    DAVA::RefPtr<PackageNode> package;
};

redpajama_set_name: RedPajamaGithub


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hoopoe.SVG.Graphics.Stroke
{
    public class hStrokeCorner : hGraphic
    {
        public enum StrokeCorner { bevel, miter, round, inherit };
        public StrokeCorner PathCorner = StrokeCorner.round;

        public hStrokeCorner()
        {
        }

        public hStrokeCorner(StrokeCorner PathCornerJoin)
        {
            PathCorner = PathCornerJoin;
            Value = "stroke-linejoin=\"" + PathCorner.ToString() + "\"";
        }

    }
}


text:
PintoStack from DataArt is an open source Docker container system. It is very easy to set-up and to run and it offers an elegant solution for enterprise computing or for Big Data processing.

A new approach to running and managing distributed systems, PintoStack gives you immutable container infrastructure, with service discovery and continuous logging. Simply put, PintoStack is an easy, reliable and complete solution to get your cloud up and running.

This tutorial is based on processing big data from ```http://data.dft.gov.uk``` and extracting valueable infromation from huge amount of publicly available data.

You can download this archive to your local machine and see the file we working with ```Accidents7904.csv```:

```
http://data.dft.gov.uk/road-accidents-safety-data/Stats19-Data1979-2004.zip
```

## Install JuJu

### Install JuJu on Ubuntu

```bash
sudo apt-get install python-software-properties pip
sudo add-apt-repository ppa:juju/stable
sudo apt-get update
sudo apt-get install juju-quickstart juju-core
sudo pip install charm-tools
```

### Install JuJu on Apple OSX

>IMPORTANT: Before all you need ```Latest Python 3 Release``` installer from [here](https://www.python.org/downloads/mac-osx/) or run ```brew install python3```. In both cases do not forget to close terminal and open a new one after install, to have new settings applied.

To install Juju on OS X without Juju quickstart, follow [the manual install instructions](https://jujucharms.com/docs/getting-started#mac-osx).

Then run this:

```bash
sudo pip install charm-tools
```

### Install JuJu on Windows

To install Juju on Windows without Juju quickstart, follow [the manual install instructions](https://jujucharms.com/docs/getting-started#windows).

>IMPORTANT: You need to install Python 3 and run the commabd below in bash shell.

```bash
sudo pip install charm-tools
```

## Configuring EC2 for your cluster

### Region

Before starting the deployment process you might select a region where it would reside we sugest you to use default one from ```config.yaml.tmpl``` file ```AWS_REGION='us-west-2'```. But EC2 allows you to select from number of available regions.
Configuration would require from you the name of region you selected, lilke, us-west-1, or eu-east-2 put in ```source.aws``` file like ```AWS_REGION='us-west-2'```

> IMPORTANT: Remember that AWS_AMI and AWS_SECURITY_GROUPS are region specific.

### SSH Keys or so called in AWS Console ```[Network & Security] > [Key Pairs]```

The keys are used to replace password based authentication. EC2 requires you to setup a keypair in management console. If you do not have keypair, it's easy to create new one open ```[AWS Console] > [EC2]``` then in left pane ```[Network & Security] > [Key Pairs]``` in case there are existing key pairs in your AWS account you can use it as well. We sugest you to name a new key pair ```PintoStack```. And save the key file in save place and put the path to you key file in ```source.aws``` for example it looks like this ```SSH_KEY_FILE='~/Downloads/PintoStack.pem.txt'``` and ```AWS_KEYPAIR_NAME='PintoStack'```.

> IMPORTANT: Please note that keypairs are also region specific and to read more please follow the [this link](http://docs.aws.amazon.com/opsworks/latest/userguide/security-ssh-access.html).

> NOTICE: For more information on AWS Security Groups look [here](http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_SecurityGroups.html).

### API Access key and secret

> INFO: If you already have working ```AWS Access Key ID``` and ```AWS Secret Access Key``` you can use it, but make sure it is valid for the ```AWS Region```, and skip this paragraph.

 * Open [the IAM console](https://console.aws.amazon.com/iam/home?#home)
 * In the navigation pane, choose Users.
 * Choose your IAM user name (not the check box).
 * Choose the Security Credentials tab and then choose Create Access Key.

To see your access key, choose Show User Security Credentials. Your credentials will look something like this:

```
  Access Key ID: AKIAIOSFODNN7EXAMPLE
  Secret Access Key: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
```

Choose Download Credentials, and store the keys in the ```config.yaml``` file in ```AWS_KEY_ID='Change me'``` and ```AWS_ACCESS_KEY='Change me'```.

>NOTICE: For more information on Amazon Access keys look [here](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSGettingStartedGuide/AWSCredentials.html)

### AMI and user name (can skip this if using defaults)

AMI name is the name of image that is used as a source for your virtual instances. So if you chose a different region than we sugested in default ```source.aws``` like ```AWS_REGION='us-west-2'``` you need to change default ```AWS_AMI='ami-5189a661'```. To do so, open this [link](https://cloud-images.ubuntu.com/locator/ec2/) and type for example if you would like to find out the AMI-ID for the latest release of "LTS" Ubuntu to run on a "64″ bit "ebs" instance in the "us-east" region, you would search for ```lts 64 us-east ebs```.

>NOTICE: You can try other options like [archlinux](https://www.uplinklabs.net/projects/arch-linux-on-ec2/), or even [BSD](http://www.daemonology.net/freebsd-on-ec2/) but it was not tested.

## Provisioning your cluster with JUJU

### JuJu environment configuration

Edit ```~/.juju/environments.yaml``` put your ```AWS_KEY_ID``` and ```AWS_KEY``` to ```amazon``` subsection. Below is the example of what you need.

```yaml
default: amazon
environments:
   amazon:
        type: ec2
        # region specifies the EC2 region. It defaults to us-east-1.
        #
        region: us-west-2
        admin-secret: 'nothingButPintostack'
        # access-key holds the EC2 access key. It defaults to the
        # environment variable AWS_ACCESS_KEY_ID.
        #
        access-key: AKIAI5KB5GZEXAMPLE

        # secret-key holds the EC2 secret key. It defaults to the
        # environment variable AWS_SECRET_ACCESS_KEY.
        #
        secret-key: 9MkZ+FvxOSrk2I2SE56zEXAMPLEwW8N8uz

        # image-stream chooses a simplestreams stream from which to select
        # OS images, for example daily or released images (or any other stream
        # available on simplestreams).
        #
        image-stream: "released"
```

Than open terminal window, go to ```examples/uk-juju``` and edit ```config.yaml.tmpl``` and save it to ```config.yaml```. After editing it sould look like this example below:

> REMEMBER: This file below is YAML formated so it is very important to preserv line spaces and leading TABs, you can check your syntax [online](http://www.yamllint.com/).

```yaml
---
pintostack:
    resource-provider-config: |
        AWS_KEY_ID='AKIAI5KB5GZEXAMPLE'
        AWS_ACCESS_KEY='9MkZ+FvxOSrk2I2SE56zEXAMPLEwW8N8uz'
        AWS_KEYPAIR_NAME=PintoStack
        AWS_ROOT_PARTITION_SIZE=20
        AWS_AMI=ami-5189a661
        AWS_INSTANCE_TYPE=t2.medium
        AWS_REGION=us-west-2
        AWS_SECURITY_GROUPS='juju-amazon,juju-amazon-0'
        AWS_SSH_USERNAME=ubuntu
        AWS_TERMINATE_ON_SHUTDOWN=true
    slaves-number: 6
    ssh-private-key: |
      -----BEGIN RSA PRIVATE KEY-----
      MIIEpQIBAAKCAQEA4s9/wZS02ds3AYO2j+riPAxIdD8KTe7Em4okE7VE8P5hwxmT94wFVNs11JP6
      jxb1MLdbcWcKVH4JIBsdYW94C0R2XI7XYJ7Znetxhh0H3R97jQM9TIF6wYKt1FAAfADh0a76mRxq
      C2GfAg+sGHZzzty5ooPnBhpfMuLsWcWLrf3LpCelYzFl1XG+2ZSLX5O1rA5oLV6INHQIOjDhCQ46
      MV1HydJhlBHPMGxhGGtc2X1kBjiyXoMiOTkyGEE+kzLBv9Ea0BxLJmnXbQ5eeWaMcrBPWMm38IgW
      Ehn6owHr+UQPS+qOunI4xhz6Af1p60ahiCP8By52JiQEQY0htrHOCQIDAQABAoIBAQCTqJb5xgA/
      vJlTxFLaaZ/e+kJ4ohJ17cEKoHueN80z3G47yTN25dJmAZjgkr3MGWebJUyMmERki9/doG+bOaD0
      OIfT/6zv8OZ1TOusFHWSJpIgKK80umxOHl7ALGURAIQjc7knTht7btx2Ik+SKlwrmJwFDAE=
      -----END RSA PRIVATE KEY-----

```

### Before begining:

* Make sure you use same ```AWS_REGION``` as in ```~/.juju/environments.yaml```.
* Make sure ```AWS_SECURITY_GROUPS``` contains ```juju-amazon```, ```juju-amazon-0```
* Make sure ```AWS_AMI``` you using is available in this ```AWS_REGION```, if you are not sure use defaults.

### Deploying with JUJU

Open terminal window, go to ```examples/uk-juju``` subfolder and run:

```
$ deploy_uk_demo.sh
```

This will return you an IP of Juju-GUI you can open it with browser. Remember to accept SSL Exception. Login with user: ```admin``` password: ```nothingButPintostack```. And than you can watch while build all charms and deploys all cluster components.

Juju GUI Login

![](images/01-login-juju-gui.jpg)

Juju GUI

![](images/02-juju-gui.jpg)

After all pending items will disapeare go back to the same terminal window.

>INFO: Monitor the status of ```pintostack/0``` unit ```$ juju stat```. To get access to pintostack context use ```$ juju ssh pintostack/0```

### Starting using PintoStack

After all pending items will disapeare in juju-gui go back to the same terminal window and run:
```
./open_webui.sh
```
It will give you the similar reply
```
You are running inside docker could not open browser.
Mesos: http://some-host-1.us-west-2.compute.amazonaws.com:5050
Marathon: http://some-host-1.us-west-2.compute.amazonaws.com:8080
Consul: http://some-host-1.us-west-2.compute.amazonaws.com:8500
Connection to closed.
```
Copy the Consul URL ```http://some-host-1.us-west-2.compute.amazonaws.com:8500``` and open it in your browser.

Consul UI

![](images/03-consul-gui.jpg)

Here you can navigate information about all nodes and services.

* Find the service named ```ipythonnb``` click it and see the node it is running on (```slave-N```).
* Navigate to node information and find ```WAN Adderess``` below you will see public adress (like this ```ec2-some-slave-host.us-west-2.compute.amazonaws.com```) copy it.
* Open your browser with copied URL with port 31080 (like this ```http://ec2-some-slave-host.us-west-2.compute.amazonaws.com:31080```)

Now you will get a standart iPython NoteBook interface.

### Running Python NoteBook

Open Marathon URL from previews paragraph (```http://some-host-1.us-west-2.compute.amazonaws.com:8080```) and make sure all applications are in state ```Running``` and there is no application ```/pintostack/upload-uk-data-to-hdfs``` or wait while it finishes.

>INFO: Juju Web UI do nto work well in Safari, so please use ```Mozzila``` based browser under OSX.

![](images/04-ipython-notebook.jpg)

Than go back to the  iPython NoteBook interface we opened in previews paragrath
* Create a notebook
* Put the code below to the cell
* Press ```Run Cell```

```python
import time

start_time = int(round(time.time() * 1000))

import DNS

hdfs_serevice="hdfs-rpc.service.consul"

DNS.ParseResolvConf()
srv_req = DNS.Request(qtype = 'SRV')
srv_result = srv_req.req(hdfs_serevice)
hdfs_port=srv_result.answers[0]['data'][2]

# now we have a file
text_file = sc.textFile("hdfs://"+str(hdfs_serevice)+":"+str(hdfs_port)+"/Accidents7904.csv")

# getting the header as an array
header = text_file.first().split(",")

# getting data
data = text_file \
   .map(lambda line: line.split(",")) \
   .filter(lambda w: w[header.index('Date')] != 'Date')
output = data.filter(lambda row: len(row[header.index('Date')].strip().split("/")) == 3) \
   .map(lambda row: row[header.index('Date')].strip().split("/")[2]) \
   .map(lambda word: (word, 1)) \
   .reduceByKey(lambda a, b: a + b) \
   .sortByKey(True) \
   .collect()
for (line, count) in output:
        print("%s: %i" % (line, count))
print ("Duration is '%i' ms" % (int(round(time.time() * 1000)) - start_time))

%matplotlib inline

import matplotlib
import numpy as np
import matplotlib.pyplot as plt

plt.plot([str(x[0]) for x in output], [str(x[1]) for x in output])
```
As a result after proccessing done, you will get the plot below, demonstrating UK traffic accidents statistics by year from 1979 to 2004.

![](images/06-ipython-uk-stat-plots.jpg)

> IMPORTANT: You can monitor the execution of tasks in Mesos UI ```Frameworks > pyspark-shell >
Active Tasks```

![](images/05-mesos-pyspark-tasks.jpg)

## Using PintoStack actions (only for advanced users requires knowledge of underlying software)

```
$ juju action defined  pintostack
docker-push: Push Dockerfile to be build in DockerImage and put to docker-registry.service.consul:5000.
marathon-push: Push JSON task description to marathon.
run: Run any comand in pintostack context.
```

### Docker Push
```
$ juju action do pintostack/0 docker-push dockername="hdfs"
```

### Marathon Push
```
$ juju action do pintostack/0 marathon-push jsonfile="hdfs-nn.json"
```


text:
<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use common\models\Region;

/* @var $this yii\web\View */
/* @var $model common\models\Subregion */
/* @var $form yii\widgets\ActiveForm */
?>

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'short_title')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'region_id')->dropDownList(
        ArrayHelper::map(Region::find()->all(), 'id', 'title')
    ) ?>

    <?= $form->field($model, 'status')->dropDownList([
        $model::STATUS_ACTIVE => 'Active',
        $model::STATUS_DISABLED => 'Disabled',
        $model::STATUS_DELETED => 'Deleted',
    ]) ?>

        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    <?php ActiveForm::end(); ?>


text:
	Version 6.x.x

	 Source

		frappe.desk.calendar.get_event_conditions
        (doctype, filters=None)

Returns SQL conditions with user permissions and filters for event queries

Public API

/api/method/frappe.desk.calendar.update_event

		frappe.desk.calendar.update_event
        (args, field_map)

Updates Event (called via calendar) based on passed field_map


text:
var should = require('should');
var util = require('util');
var testUtils = require('../util/util');
var CLITest = require('../framework/cli-test');
var vmTestUtil = require('../util/asmVMTestUtil');
// A common VM used by multiple tests
var suite;
var vmPrefix = 'clitestvm';
var createdVms = [];
var testPrefix = 'cli.vm.extension_set-custom_disable';
var requiredEnvironment = [{
  name: 'AZURE_VM_TEST_LOCATION',
  defaultValue: 'West US'
}];
describe('cli', function() {
  describe('vm', function() {
    var vmUtil = new vmTestUtil();
    var vmName,
      location,
      username = 'azureuser',
      password = 'PassW0rd$',
      retry = 5,
      customScript = 'test/data/customScript.json',
      customextension = 'CustomScriptExtension',
      custompublisher = 'Microsoft.Compute',
      customversion = '1.*',
      timeout;
    testUtils.TIMEOUT_INTERVAL = 5000;

    before(function(done) {
      suite = new CLITest(this, testPrefix, requiredEnvironment);
      suite.setupSuite(function() {
        vmName = suite.generateId(vmPrefix, createdVms);
        location = process.env.AZURE_VM_TEST_LOCATION;
        timeout = suite.isPlayback() ? 0 : testUtils.TIMEOUT_INTERVAL;
        done();
      });
    });
    after(function(done) {
      if (!suite.isPlayback()) {
        vmUtil.deleteVM(vmName, timeout, suite, function() {
          suite.teardownSuite(done);
        });
      } else {
        suite.teardownSuite(done);
      }
    });
    beforeEach(function(done) {
      suite.setupTest(done);
    });
    afterEach(function(done) {
      setTimeout(function() {
        suite.teardownTest(done);
      }, timeout);
    });
    //Set custom extensions
    describe('extension:', function() {
      it('should set custom extensions and disable', function(done) {
        vmUtil.createWindowsVM(vmName, username, password, location, timeout, suite, function() {
          var cmd = util.format('vm extension set -c %s %s %s %s %s --json', customScript, vmName, customextension, custompublisher, customversion).split(' ');
          testUtils.executeCommand(suite, retry, cmd, function(result) {
            result.exitStatus.should.equal(0);
            cmd = util.format('vm extension set -b %s %s %s %s --json', vmName, customextension, custompublisher, customversion).split(' ');
            testUtils.executeCommand(suite, retry, cmd, function(result) {
              result.exitStatus.should.equal(0);
              cmd = util.format('vm extension get %s --json', vmName).split(' ');
              testUtils.executeCommand(suite, retry, cmd, function(result) {
                result.exitStatus.should.equal(0);
                var Extensions = JSON.parse(result.text);
                Extensions[0].state.should.equal('Disable');
                done();
              });
            });
          });
        });
      });
    });
  });
});


text:
<!DOCTYPE html>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Nikola Internals | Pollard's Rho</title>
<link rel="stylesheet" href="https://use.typekit.net/fne7jql.css">
<link rel="stylesheet" href="../../assets/halfmoon/css/halfmoon-variables.min.css">
<link rel="stylesheet" href="../../assets/halfmoon/css/halfmoon-highlight-js-theme.css">
<link href="../../assets/css/baguetteBox.min.css" rel="stylesheet" type="text/css">
<link href="../../assets/css/rst_base.css" rel="stylesheet" type="text/css">
<link href="../../assets/css/nikola_rst.css" rel="stylesheet" type="text/css">
<link href="../../assets/css/code.css" rel="stylesheet" type="text/css">
<link href="../../assets/css/theme.css" rel="stylesheet" type="text/css">
<link href="../../assets/css/etica.css" rel="stylesheet" type="text/css">
<meta name="theme-color" content="#5670d4">
<meta name="generator" content="Nikola (getnikola.com)">
<link rel="alternate" type="application/rss+xml" title="RSS" hreflang="en" href="../../rss.xml">
<link rel="canonical" href="https://nicholas-miklaucic.github.io/pages/internals/">
<link href="https://css.gg/yinyang.css" rel="stylesheet">
<link href="https://css.gg/copy.css" rel="stylesheet">
<link href="https://css.gg/menu.css" rel="stylesheet">
<link rel="stylesheet" href="//unpkg.com/@highlightjs/cdn-assets@11.2.0/styles/default.min.css">
<meta name="author" content="The Nikola Team">
<meta property="og:site_name" content="Pollard's Rho">
<meta property="og:title" content="Nikola Internals">
<meta property="og:url" content="https://nicholas-miklaucic.github.io/pages/internals/">
<meta property="og:description" content="When trying to guide someone into adding a feature in Nikola, it hit me that
while the way it's structured makes sense to me it is far from obvious.
So, this is a short document explaining what each p">
<meta property="og:type" content="article">
<meta property="article:published_time" content="2012-03-30T23:00:00-03:00">

    Skip to main content

            Pollard’s Rho

About Me

Archive

Tags

RSS feed

        <button class="btn" data-toggle="dropdown" type="button">

        </button>

About Me

Archive

Tags

RSS feed

            <button class="btn" type="button" onclick="halfmoon.toggleDarkMode()"></button>

Nikola Internals

When trying to guide someone into adding a feature in Nikola, it hit me that
while the way it’s structured makes sense to me it is far from obvious.

So, this is a short document explaining what each piece of Nikola does and
how it all fits together.

Nikola is a Pile of Plugins

Most of Nikola is implemented as plugins using Yapsy.
You can ignore that they are plugins and just think of them as regular python
modules and packages with a funny little .plugin file next to them.

So, 90% of the time, what you want to do is either write a new plugin or extend
an existing one.

There are several kinds of plugins, all implementing interfaces defined in
nikola/plugin_categories.py and documented in
Extending Nikola

If your plugin has a dependency, please make sure it doesn’t make Nikola
throw an exception when the dependency is missing. Try to fail gracefully
with an informative message.

Commands are plugins

When you use nikola foo you are using the plugin command/foo. Those are
used to extend Nikola’s command line. Their interface is defined in the Command
class. They take options and arguments and do whatever you want, so go wild.

The build command is special

The build command triggers a whole lot of things, and is the core of Nikola
because it’s the one that you use to build sites. So it deserves its own section.

The Build Command

Nikola’s goal is similar, deep at heart, to a Makefile. Take sources, compile them
into something, in this case a website. Instead of a Makefile, Nikola uses
doit

Doit has the concept of “tasks”. The 1 minute summary of tasks is that they have:

actions

What the task does. For example, convert a markdown document into HTML.

dependencies

If this file changes, then we need to redo the actions. If this configuration
option changes, redo it, etc.

targets

Files that the action generates. No two actions can have the same targets.

basename:name

Each task is identified by either a name or a basename:name pair.

More about tasks

If you ever want to do your own tasks, you really should read the doit
documentation on tasks

So, what Nikola does, when you use the build command, is to read the configuration conf.py from the current folder, instantiate the Nikola class, and have it generate a whole list of tasks for doit
to process. Then doit will decide which tasks need doing, and do them, in
the right order.

The place where the tasks are generated is in Nikola.gen_tasks, which collects tasks
from all the plugins inheriting BaseTask, massages them a bit, then passes them
to doit.

So, if you want things to happen on build you want to create a Task plugin, or extend
one of the existing ones.

Tests

While Nikola is not a hardcore TDD project, we like tests. So, please add them if you can.
You can write unit tests or integration tests. (Doctests are not supported
anymore due to fragility.)

Posts and Pages

Nikola has a concept of posts and pages. Both are more or less the same thing, except
posts are added into RSS feeds and pages are not. All of them are in a list called
“the timeline” formed by objects of class Post.

When you are creating a task that needs the list of posts and/or pages (for example,
the RSS creation plugin) on task execution time, your plugin should call self.site.scan_posts()
in gen_tasks to ensure the timeline is created and available in
self.site.timeline. You should not modify the timeline, because it will cause consistency issues.

scan_posts

The Nikola.scan_posts function can be used in plugins to force the
timeline creation, for example, while creating the tasks.

Your plugin can use the timeline to generate “stuff” (technical term). For example,
Nikola comes with plugins that use the timeline to create a website (surprised?).

The workflow included with nikola is as follows (incomplete!):

The post is assigned a compiler based on its extension and the COMPILERS option.

The compiler is applied to the post data and a “HTML fragment” is produced. That
fragment is stored in a cache (the posts plugin).

The configured theme has templates (and a template engine), which are applied to the post’s
HTML fragment and metadata (the pages plugin).

The original sources for the post are copied to some accessible place (the sources plugin).

If the post is tagged, some pages and RSS feeds for each tag are updated (the tags plugin).

If the post is new, it’s included in the blog’s RSS feed (the rss plugin).

The post is added in the right place in the index pages for the blog (the indexes plugin).

CSS/JS/Images for the theme are put in the right places (the copy_assets and bundles plugins).

A File describing the whole site is created (the sitemap plugin).

You can add whatever you want to that list: just create a plugin for it.

You can also expand Nikola’s capabilities at several points:

compilers

Nikola supports a variety of markups. If you want to add another one, you need to create a Compiler plugin.

templates

Nikola’s themes can use Jinja2 or Mako templates. If you prefer another template system,
you have to create a TemplateSystem plugin.

themes

To change how the generated site looks, you can create custom themes.

And of course, you can also replace or extend each of the existing plugins.

Nikola Architecture

Contents © 2021         Nicholas Miklaucic - Powered by         Nikola


text:
import React from "react";
import styled, { keyframes } from "styled-components";
import { TransitionGroup, CSSTransition } from "react-transition-group";

import Page from "../components/page";

const Contact = () => {
  const chatMessages = [
    "Hello!",
    "I prefer people reach out to me via email.",
    "If you'd like to get in touch with me about a new project I'm currently only working on open source stuff in my spare time.",
    "If you're trying to contact me about a job opportunity I'm currently employed with VanNoppen Marketing.",
    "If you just want to chat then I'm almost always on Discord!",
  ];

  return (
    <Page
      title="Contact"
      description="How to get in contact with me."
      gridArea="1 / 1 / 4 / 7"
    >
      <Background />
      <Grid>
        <GridLeft>
          <ContactWrapper>
            <Heading>Contact Me</Heading>
            <ShoutOut>I love chatting</ShoutOut>
            <ShoutOut>with other developers!</ShoutOut>
            <ContactList>
              <ContactKey>Email</ContactKey>
              <ContactValue>
                <ContactLink href="mailto:isaac@bythewood.me">
                  isaac@bythewood.com
                </ContactLink>
              </ContactValue>
              <ContactKey>LinkedIn</ContactKey>
              <ContactValue>
                <ContactLink
                  href="https://www.linkedin.com/in/isaac-bythewood/"
                  rel="noopener noreferrer"
                  target="_blank"
                >
                  Isaac Bythewood
                </ContactLink>
              </ContactValue>
              <ContactKey>GitHub</ContactKey>
              <ContactValue>
                <ContactLink
                  href="https://github.com/overshard"
                  rel="noopener noreferrer"
                  target="_blank"
                >
                  /overshard
                </ContactLink>
              </ContactValue>
              <ContactKey>Discord</ContactKey>
              <ContactValue>
                <ContactLink
                  href="https://discordapp.com/"
                  rel="noopener noreferrer"
                  target="_blank"
                >
                  Overshard#4907
                </ContactLink>
              </ContactValue>
            </ContactList>
          </ContactWrapper>
        </GridLeft>
        <GridRight>
          <Chat>
            <TransitionGroup component={null}>
              {chatMessages.map((message, index) => {
                const transitionTimeout = (index + 2) * 2000;
                const transitionDelay = (index + 1) * 2000;
                return (
                  <CSSTransition
                    key={index}
                    appear
                    timeout={{ appear: transitionTimeout }}
                    classNames="fade"
                  >
                    <ChatLine
                      style={{ transitionDelay: `${transitionDelay}ms` }}
                    >
                      <ChatAvatar
                        src="/static/images/avatar.webp"
                        alt="Chat Avatar"
                      />
                      <ChatMessage>
                        {message}
                        Isaac
                      </ChatMessage>
                    </ChatLine>
                  </CSSTransition>
                );
              })}
            </TransitionGroup>
          </Chat>
        </GridRight>
      </Grid>
    </Page>
  );
};

export default Contact;

const SlideUp = keyframes`
  from {
    opacity: 0;
    transform: translateX(-100px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
`;

const TransformRight = keyframes`
  from {
    transform: scaleX(0);
  }

  to {
    transform: scaleX(1);
  }
`;

const TransformLeft = keyframes`
  from {
    transform: scaleX(1);
  }

  to {
    transform: scaleX(0);
  }
`;

const Background = styled.div`
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: white;
  z-index: -2;
`;

const Grid = styled.div`
  display: grid;
  grid-template-columns: 50vw 1fr;
  grid-template-rows: auto;
  grid-template-areas: "left right";
  margin-left: 60px;
  transform: translateX(-100vw);
  animation: ${SlideUp} 750ms 500ms forwards;

  @media (${(props) => props.theme.breakpoints.tablet}) {
    margin-left: 0;
    grid-template-columns: 1fr;
    grid-template-rows: auto auto;
    margin-top: 60px;
  }
`;

const GridColumn = styled.div`
  min-height: 100vh;
  display: flex;
  align-items: center;

  @media (${(props) => props.theme.breakpoints.tablet}) {
    min-height: auto;
  }
`;

const GridLeft = styled(GridColumn)`
  grid-area: left;
  background-color: rgba(0, 0, 0, 0.9);

  @media (${(props) => props.theme.breakpoints.tablet}) {
    grid-column: 1;
    grid-row: 1;
  }
`;

const GridRight = styled(GridColumn)`
  grid-area: right;

  @media (${(props) => props.theme.breakpoints.tablet}) {
    grid-column: 1;
    grid-row: 2;
  }
`;

const ContactWrapper = styled.div`
  padding-left: 60px;

  @media (${(props) => props.theme.breakpoints.tablet}) {
    min-height: auto;
    padding-top: 30px;
    padding-bottom: 30px;
  }
`;

const Heading = styled.h1`
  font-size: 3em;
  margin-top: 10px;
  margin-bottom: 20px;

  &::before {
    content: "";
    display: block;
    width: 75px;
    height: 5px;
    margin-bottom: 10px;
    background-color: ${(props) => props.theme.colors.blue};
  }

  @media (${(props) => props.theme.breakpoints.mobile}) {
    margin-bottom: 10px;
  }
`;

const ShoutOut = styled.h2`
  text-transform: uppercase;
  padding: 5px;
  margin: 0 0 2px 0;
  float: left;
  clear: left;
  font-size: 1.4em;
  background-image: linear-gradient(
    to right,
    ${(props) => props.theme.colors.blue} 0,
    ${(props) => props.theme.colors.purple} 100%
  );

  @media (${(props) => props.theme.breakpoints.mobile}) {
    font-size: 1.1em;
  }
`;

const ContactList = styled.dl`
  padding-top: 30px;
  clear: both;
`;

const ContactKey = styled.dt`
  text-transform: uppercase;
  font-size: 0.8em;
  font-weight: 700;
  opacity: 0.7;
`;

const ContactValue = styled.dd`
  font-size: 1.5em;
  margin-left: 0;
  margin-bottom: 30px;
`;

const ContactLink = styled.a`
  color: white;
  text-decoration: none;
  position: relative;

  &::before {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 2px;
    background: rgba(255, 255, 255, 1);
    transform-origin: left;
    animation: ${TransformLeft} 300ms normal forwards;
  }

  &::after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 2px;
    background: rgba(255, 255, 255, 0.2);
  }

  &:hover {
    &::before {
      animation: ${TransformRight} 300ms normal forwards;
    }
  }
`;

const Chat = styled.div`
  margin: 30px 0;
  display: flex;
  flex-direction: column;
`;

const ChatLine = styled.div`
  display: flex;
  margin-bottom: 20px;
  justify-content: center;
  opacity: 0;

  &.fade-appear {
    opacity: 0;
    transform: translateX(-100px);
  }

  &.fade-appear-active {
    opacity: 1;
    transform: translateX(0);
    transition-duration: 250ms;
    transition-property: opacity, transform;
  }

  &.fade-appear-done {
    opacity: 1;
  }
`;

const ChatMessage = styled.div`
  font-size: 1.2em;
  padding: 10px 20px;
  background-color: rgba(0, 0, 0, 0.7);
  width: 65%;

  @media (${(props) => props.theme.breakpoints.mobile}) {
    font-size: 1em;
    width: 70%;
  }

  & span {
    display: block;
    font-size: 0.5em;
    text-transform: uppercase;
    margin-top: 5px;
    color: rgba(255, 255, 255, 0.7);
    letter-spacing: 1px;
    font-weight: 800;
  }
`;

const ChatAvatar = styled.img`
  margin-right: 20px;
  width: 50px;
  height: 50px;

  @media (${(props) => props.theme.breakpoints.mobile}) {
    margin-right: 10px;
    width: 40px;
    height: 40px;
  }
`;


text:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.jwu5.habittracker.HabitCompletionActivity">

    <ListView
        android:id="@+id/completionList"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>
</LinearLayout>


text:
import * as React from 'react';
import { expect } from 'chai';

import MultiSelectUnstyled from '@mui/base/MultiSelectUnstyled';
import { selectUnstyledClasses } from '@mui/base/SelectUnstyled';
import OptionUnstyled from '@mui/base/OptionUnstyled';
import OptionGroupUnstyled from '@mui/base/OptionGroupUnstyled';
import {
  createMount,
  createRenderer,
  describeConformanceUnstyled,
  userEvent,
  act,
  fireEvent,
} from 'test/utils';

describe('MultiSelectUnstyled', () => {
  const mount = createMount();
  const { render } = createRenderer();

  const componentToTest = (
    <MultiSelectUnstyled defaultListboxOpen>
      <OptionGroupUnstyled label="Group">
        <OptionUnstyled value={1}>1</OptionUnstyled>
      </OptionGroupUnstyled>
    </MultiSelectUnstyled>
  );

  describeConformanceUnstyled(componentToTest, () => ({
    inheritComponent: 'button',
    render,
    mount,
    refInstanceof: window.HTMLButtonElement,
    testComponentPropWith: 'span',
    muiName: 'MuiSelect',
    slots: {
      root: {
        expectedClassName: selectUnstyledClasses.root,
      },
      listbox: {
        expectedClassName: selectUnstyledClasses.listbox,
        testWithElement: 'ul',
      },
      popper: {
        expectedClassName: selectUnstyledClasses.popper,
        testWithElement: null,
      },
    },
  }));

  describe('keyboard navigation', () => {
    ['Enter', 'ArrowDown', 'ArrowUp'].forEach((key) => {
      it(`opens the dropdown when the "${key}" key is down on the button`, () => {
        // can't use the default native `button` as it doesn't treat enter or space press as a click
        const { getByRole } = render(<MultiSelectUnstyled components={{ Root: 'div' }} />);
        const button = getByRole('button');
        act(() => {
          button.focus();
        });
        fireEvent.keyDown(button, { key });

        expect(button).to.have.attribute('aria-expanded', 'true');
        expect(getByRole('listbox')).not.to.equal(null);
        expect(document.activeElement).to.equal(getByRole('listbox'));
      });
    });

    it(`opens the dropdown when the " " key is let go on the button`, () => {
      // can't use the default native `button` as it doesn't treat enter or space press as a click
      const { getByRole } = render(<MultiSelectUnstyled components={{ Root: 'div' }} />);
      const button = getByRole('button');
      act(() => {
        button.focus();
      });
      fireEvent.keyUp(button, { key: ' ' });

      expect(button).to.have.attribute('aria-expanded', 'true');
      expect(getByRole('listbox')).not.to.equal(null);
      expect(document.activeElement).to.equal(getByRole('listbox'));
    });

    describe('item selection', () => {
      ['Enter', ' '].forEach((key) =>
        it(`selects a highlighted item using the "${key}" key`, () => {
          const { getByRole } = render(
            <MultiSelectUnstyled>
              <OptionUnstyled value={1}>1</OptionUnstyled>
              <OptionUnstyled value={2}>2</OptionUnstyled>
            </MultiSelectUnstyled>,
          );

          const button = getByRole('button');
          act(() => {
            button.click();
          });

          const listbox = getByRole('listbox');

          userEvent.keyPress(listbox, { key: 'ArrowDown' }); // highlights '1'
          userEvent.keyPress(listbox, { key: 'ArrowDown' }); // highlights '2'
          userEvent.keyPress(listbox, { key });

          expect(button).to.have.text('2');
        }),
      );
    });

    it('closes the listbox without selecting an option when "Escape" is pressed', () => {
      const { getByRole, queryByRole } = render(
        <MultiSelectUnstyled defaultValue={[1]}>
          <OptionUnstyled value={1}>1</OptionUnstyled>
          <OptionUnstyled value={2}>2</OptionUnstyled>
        </MultiSelectUnstyled>,
      );

      const button = getByRole('button');

      act(() => {
        button.click();
      });

      const listbox = getByRole('listbox');
      userEvent.keyPress(listbox, { key: 'ArrowDown' }); // highlights '2'
      userEvent.keyPress(listbox, { key: 'Escape' });

      expect(button).to.have.attribute('aria-expanded', 'false');
      expect(button).to.have.text('1');
      expect(queryByRole('listbox')).to.equal(null);
    });
  });

  it('closes the listbox without selecting an option when focus is lost', () => {
    const { getByRole, getByTestId } = render(

        <MultiSelectUnstyled defaultValue={[1]}>
          <OptionUnstyled value={1}>1</OptionUnstyled>
          <OptionUnstyled value={2}>2</OptionUnstyled>
        </MultiSelectUnstyled>

          focus target

,
    );

    const button = getByRole('button');

    act(() => {
      button.click();
    });

    const listbox = getByRole('listbox');
    userEvent.keyPress(listbox, { key: 'ArrowDown' }); // highlights '2'

    const focusTarget = getByTestId('focus-target');
    act(() => {
      focusTarget.focus();
    });

    expect(button).to.have.attribute('aria-expanded', 'false');
    expect(button).to.have.text('1');
  });

  it('focuses the listbox after it is opened', () => {
    const { getByRole } = render(
      <MultiSelectUnstyled>
        <OptionUnstyled value={1}>1</OptionUnstyled>
      </MultiSelectUnstyled>,
    );

    const button = getByRole('button');
    act(() => {
      button.click();
    });

    expect(document.activeElement).to.equal(getByRole('listbox'));
  });
});


text:
**MesHighlightBundle** is based on [scrivo/highlight.php][1] library, a port of [highlight.js][2] by Ivan Sagalaev to PHP.

[![SensioLabsInsight](https://insight.sensiolabs.com/projects/f5ce9d83-a2f4-4441-a028-2fa7b8f59ba4/mini.png)](https://insight.sensiolabs.com/projects/f5ce9d83-a2f4-4441-a028-2fa7b8f59ba4)
[![Build Status](https://scrutinizer-ci.com/g/Carteni/highlight-bundle/badges/build.png?b=master)](https://scrutinizer-ci.com/g/Carteni/highlight-bundle/build-status/master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Carteni/highlight-bundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Carteni/highlight-bundle/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/Carteni/highlight-bundle/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/Carteni/highlight-bundle/?branch=master)
[![Latest Unstable Version](https://poser.pugx.org/carteni/highlight-bundle/v/unstable)](https://packagist.org/packages/carteni/highlight-bundle)
[![Latest Stable Version](https://poser.pugx.org/carteni/highlight-bundle/v/stable)](https://packagist.org/packages/carteni/highlight-bundle)
[![License](https://poser.pugx.org/carteni/crypto-bundle/license)][1]

Step 1: Download the Bundle
---------------------------

```console
$ composer require carteni/highlight-bundle
```

Step 2: Enable the Bundle
-------------------------

```php
<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new \Mes\Misc\HighlightBundle\MesHighlightBundle(),
        );

        // ...
    }

    // ...
}
```

Step 3: Install styles
----------------------

```console
$ bin/console assets:install --symlink
```

Step 4: Configure the Bundle (optional)
---------------------------------------

```yaml
mes_highlight:
    supported_languages: ["php", "xml", "twig", "javascript", "sql", "json"]
    root_path: "%kernel.root_dir%/Resources/"
    left_delimiter: "{{"
    right_delimiter: "}}"
```

Examples
--------

[See the Documentation][3]

License
-------

This bundle is under the MIT license. See the complete license [in the bundle](LICENSE)

Reporting an issue
------------------

Issues are tracked in the [Github issue tracker][4].

### Enjoy!

###### ♥ ☕ m|e|s

[1]: https://github.com/scrivo/highlight.php
[2]: http://www.highlightjs.org/
[3]: ./Resources/doc/index.md
[4]: https://github.com/Carteni/highlight-bundle/issues
[5]: https://packagist.org/packages/carteni/highlight-bundle


text:
using System;

namespace Azure.Search.Documents.Models
{
    internal static partial class SearchQueryTypeExtensions
    {
        public static string ToSerialString(this SearchQueryType value) => value switch
        {
            SearchQueryType.Simple => "simple",
            SearchQueryType.Full => "full",
            _ => throw new ArgumentOutOfRangeException(nameof(value), value, "Unknown SearchQueryType value.")
        };

        public static SearchQueryType ToSearchQueryType(this string value)
        {
            if (string.Equals(value, "simple", StringComparison.InvariantCultureIgnoreCase)) return SearchQueryType.Simple;
            if (string.Equals(value, "full", StringComparison.InvariantCultureIgnoreCase)) return SearchQueryType.Full;
            throw new ArgumentOutOfRangeException(nameof(value), value, "Unknown SearchQueryType value.");
        }
    }
}


text:
'use strict';

import * as angular from 'angular';

export const AZURE_SERVERGROUP_CONFIGURE_WIZARD_ADVANCEDSETTINGS_ADVANCEDSETTINGSSELECTOR_DIRECTIVE =
  'spinnaker.azure.serverGroup.configure.wizard.advancedSettings.selector.directive';
export const name = AZURE_SERVERGROUP_CONFIGURE_WIZARD_ADVANCEDSETTINGS_ADVANCEDSETTINGSSELECTOR_DIRECTIVE; // for backwards compatibility
angular
  .module(AZURE_SERVERGROUP_CONFIGURE_WIZARD_ADVANCEDSETTINGS_ADVANCEDSETTINGSSELECTOR_DIRECTIVE, [])
  .directive('azureServerGroupAdvancedSettingsSelector', function () {
    return {
      restrict: 'E',
      templateUrl: require('./advancedSettingsSelector.directive.html'),
      scope: {},
      bindToController: {
        command: '=',
      },
      controllerAs: 'adv',
      controller: 'azureServerGroupAdvancedSettingsSelectorCtrl',
    };
  })
  .controller('azureServerGroupAdvancedSettingsSelectorCtrl', function () {
    this.addDataDisk = () => {
      const newDataDisks = angular.copy(this.command.dataDisks);
      this.command.dataDisks = newDataDisks.concat([
        {
          lun: 0,
          managedDisk: {
            storageAccountType: 'Standard_LRS',
          },
          diskSizeGB: 1,
          caching: 'None',
          createOption: 'Empty',
        },
      ]);
    };

    this.removeDataDisk = (index) => {
      const newDataDisks = angular.copy(this.command.dataDisks);
      newDataDisks.splice(index, 1);
      this.command.dataDisks = newDataDisks;
    };
  });


text:
In the recent [Secure coding (and Application Security) must be invisible to developers](http://diniscruz.blogspot.co.uk/2012/04/secure-coding-and-application-security.html) post Joshbw posted this great comment on the reasons why we end up with 'insecure by default Frameworks'

_'...On top of that the frameworks are being developed with the same mindset of all of the other products out there - "What makes the customer happy first, and then maybe if security doesn't interfere with that". **A great example is that MVCs really should employ declaritive binding rather than auto binding; **it really is a marginal hit to development and ensures that the only fields that can be set are those explicitely exposed by the dev. **Despite this problem being known for years even MS has taken the stance that devs should opt into declaritive binding despite the fact that MVCs are default allow**....'_

In a way Microsoft's (and Spring's) decision to chose the insecure default (see Spring [MVC Autobinding Vulnerability](http://diniscruz.blogspot.co.uk/2011/07/current-o2-support-for-analyzing-spring.html)) is probably the correct business decision.

Choosing the secure alternative, would introduce a hit to development (Joshbw thinks this would be marginal, but I'm not so sure), which these Framework developers felt could create a bad user experience (their users are the developers) and could potentially drive away users into other frameworks that had such 'by default' AutoBinding features.

This is why I say that [We need Security-focused SAST/Static-Analysis rules](http://diniscruz.blogspot.co.uk/2012/04/we-need-security-focused-saststatic.html) so that we can deal with the cases where AutoBinding creates vulnerabilities.

In fact, a good example of security adding value to developers would be if those SAST rules (via an security engine) would actually automatically introduce those 'Declarative Bindings' without requiring a developer's intervention (this could happen behind the scenes , 'invisible' to a developer, unless that was creating business impact (i.e. side-effect).

To see an example of what this could look like, take a look at [The future of secure code? Fixing/Encoding .NET code in real time (in this case Response.Write)](http://diniscruz.blogspot.co.uk/2011/11/future-of-secure-code-fixingencoding.html)

On the commercial SAST front, as far as I know, only [Armorize ](http://armorize.com/)ventures into this 'code fixing territory'


text:
package org.spongepowered.common.data.processor.data.entity;

import net.minecraft.entity.passive.EntitySheep;
import net.minecraft.item.EnumDyeColor;
import org.spongepowered.api.data.DataTransactionResult;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.manipulator.immutable.ImmutableDyeableData;
import org.spongepowered.api.data.manipulator.mutable.DyeableData;
import org.spongepowered.api.data.type.DyeColor;
import org.spongepowered.api.data.type.DyeColors;
import org.spongepowered.api.data.value.ValueContainer;
import org.spongepowered.api.data.value.immutable.ImmutableValue;
import org.spongepowered.api.data.value.mutable.Value;
import org.spongepowered.common.data.manipulator.mutable.SpongeDyeableData;
import org.spongepowered.common.data.processor.common.AbstractEntitySingleDataProcessor;
import org.spongepowered.common.data.value.SpongeValueFactory;

import java.util.Optional;

public class SheepDyeColorDataProcessor extends AbstractEntitySingleDataProcessor<EntitySheep, DyeColor, Value<DyeColor>, DyeableData,
        ImmutableDyeableData> {

    public SheepDyeColorDataProcessor() {
        super(EntitySheep.class, Keys.DYE_COLOR);
    }

    @Override
    protected Value<DyeColor> constructValue(DyeColor actualValue) {
        return SpongeValueFactory.getInstance().createValue(Keys.DYE_COLOR, actualValue, DyeColors.BLACK);
    }

    @Override
    protected boolean set(EntitySheep container, DyeColor value) {
        container.setFleeceColor((EnumDyeColor) (Object) value);
        return true;
    }

    @Override
    protected Optional<DyeColor> getVal(EntitySheep container) {
        return Optional.of((DyeColor) (Object) container.getFleeceColor());
    }

    @Override
    protected ImmutableValue<DyeColor> constructImmutableValue(DyeColor value) {
        return constructValue(value).asImmutable();
    }

    @Override
    public DataTransactionResult removeFrom(ValueContainer<?> container) {
        return DataTransactionResult.failNoData();
    }

    @Override
    protected DyeableData createManipulator() {
        return new SpongeDyeableData();
    }
}


text:
.. _custom_viz:

.. currentmodule:: surfer

Customizing the Visualization
=============================

One advantage to PySurfer over Tksurfer is that you are not
limited to a single look for the visualization. Of course, being
built on Mayavi, PySurfer is in theory completely customizable.
However, we also offer a few preset options so that you do not
have to delve into the underlying engine to get a different look.

Changing the display background
-------------------------------

The display background can take any valid matplotlib color (i.e.,
it can be a tuple of rgb values, an rgb hex string, or a named HTML
color).

Changing the display size
-------------------------

The default display window is 800px by 800px, but this can be configured
using the ``size`` keyword argument in the Brain constructor. ``size``
should either be a single number to make a square window, or a pair of
values, ``(width, height)``, to make a rectangular window.

Changing the curvature color scheme
-----------------------------------

By default, a new :class:`Brain` instance displays the binarized
cortical curvature values, so you can see which patches of cortex
are gyri and which are sulci (pass ``curv=False`` to the
:class:`Brain` constructor, or use the ``-no-curv`` switch in the
command-line interface to turn this off). There are four preset
themes for the curvature color scheme, which you can pass to the
``cortex`` parameter in the :class:`Brain` constructor: ``classic``,
``bone``, ``high_contrast``, and ``low_contrast``:

.. image:: ../_static/cortex_options.png

Note that, in each theme, the darker color signifies sulci.

It's also possible to customize this further by passing the name of
a mayavi colormap or a colormap name along with the endpoints of the
colormap and whether it should be reversed.

Additionally, you can load a continuous curvature map with the
:meth:`Brain.add_morphometry` method.

How to use these themes
-----------------------

These options can be selected either as keyword arguments to the
:class:`Brain` constructor,

.. code-block:: python

    >>> from surfer import Brain
    >>> b = Brain('fsaverage', 'lh', 'inflated', cortex='bone')

or as options in the command-line interface::

.. code-block:: bash

    $ pysurfer fsaverage lh inflated -background slategray -size 400


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace login_system
{
    class Database
    {

        private string server;

        private string database;

        private string uid;

        private string password;

        string connectionString = null;

        string querry = null;

        MySqlConnection sqlConnection;
        MySqlCommand sqlCommand;
       // MySqlDataReader sqlReader = null;

        public void ConnectionToDatabase()
        {
            server = "tsuts.tskoli.is";
            database = "1612982129_gru";
            uid = "1612982129";
            password = "mypassword";

            connectionString = "server= " + server + ";userid= " + uid + ";password= " + password + ";database= " + database;

            sqlConnection = new MySqlConnection(connectionString);

        }

        public bool OpenConnection()
        {
            try
            {

                sqlConnection.Open();
                return true;

            }
            catch (MySqlException ex)
            {

                throw ex;
            }
        }

        public void InputIntoDatabase(string name, string home, string post, string age)
        {

            if (OpenConnection() == true)
            {
                querry = "INSERT INTO tableHlutaprof (name, home, post, age) VALUES ('" + name + "','" + home + "','" + post + "','" + age + "')";
                sqlCommand = new MySqlCommand(querry, sqlConnection);

                sqlCommand.ExecuteNonQuery();
                CloseConnection();
            }
        }

        /*  public string FindUserAndShow(string name)
          {

          }

          public void Delete(string name)
          {
              if (OpenConnection() == true)
              {
                  querry = "Delete FROM tableHlutaprof where name = '" + name + "'";
                  sqlCommand = new MySqlCommand(querry, sqlConnection);

                  sqlCommand.ExecuteNonQuery();
                  CloseConnection();
              }
          }
          */
        public void Update(string name, string home, int post, int age)
        {
            if (OpenConnection() == true)
            {
                querry = "Update tableHlutaprof set name ='" + name + "', home = '" + home + "', post = " + post + ", age = " + age;

                sqlCommand = new MySqlCommand(querry, sqlConnection);
                sqlCommand.ExecuteNonQuery();
                CloseConnection();
            }
        }

       /* public List<string> ReadFromDatabase()
        {
            List<string> values = new List<string>();
            string line = null;
            if (OpenConnection() == true)
            {
                querry = "SELECT name, home, post, age FROM tableHlutaprof";
                sqlCommand = new MySqlCommand(querry, sqlConnection);

                sqlReader = sqlCommand.ExecuteReader();
                while (sqlReader.Read())
                {
                    for (int i = 0; i < sqlReader.FieldCount; i++)
                    {
                        line += (sqlReader.GetValue(i).ToString()) + ":";
                    }
                    values.Add(line);
                    line = null;
                }
                CloseConnection();
                return values;
            }
        }*/

        private bool CloseConnection()
        {
            try
            {

                sqlConnection.Close();
                return true;

            }
            catch (MySqlException ex)
            {

                throw ex;
            }
        }
    }
}


text:
#import <Kiwi/Kiwi.h>
#import "SPXNonEmptyDataValidator.h"

SPEC_BEGIN(SPXNonEmptyDataValidatorSpec)

describe(@"SPXNonEmptyDataValidatorSpec", ^{

  SPXNonEmptyDataValidator *validator = [SPXNonEmptyDataValidator new];

  context(@"NSString", ^{
    NSString *value = @"value";

    it(@"should pass with non-empty value", ^{
      BOOL isValid = [validator validateValue:value error:nil];
      [[theValue(isValid) should] equal:theValue(YES)];
    });

    value = @"";

    it(@"should fail with empty value", ^{
      BOOL isValid = [validator validateValue:value error:nil];
      [[theValue(isValid) should] equal:theValue(NO)];
    });
  });

  context(@"NSAttributedString", ^{
    NSAttributedString *value = [[NSAttributedString alloc] initWithString:@"value"];

    it(@"should pass with non-empty value", ^{
      BOOL isValid = [validator validateValue:value error:nil];
      [[theValue(isValid) should] equal:theValue(YES)];
    });

    value = [[NSAttributedString alloc] initWithString:@""];

    it(@"should fail with empty value", ^{
      BOOL isValid = [validator validateValue:value error:nil];
      [[theValue(isValid) should] equal:theValue(NO)];
    });
  });

  context(@"Random Object", ^{
    id value = nil;

    it(@"should fail with nil object", ^{
      BOOL isValid = [validator validateValue:value error:nil];
      [[theValue(isValid) should] equal:theValue(NO)];
    });

    value = [NSObject new];

    it(@"should pass with non-nil object", ^{
      BOOL isValid = [validator validateValue:value error:nil];
      [[theValue(isValid) should] equal:theValue(YES)];
    });
  });

});

SPEC_END


text:
package org.apache.rave.portal.web.tag;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.rave.portal.model.Region;
import org.apache.rave.portal.model.RegionWidget;
import org.apache.rave.portal.model.Widget;
import org.apache.rave.portal.web.renderer.RenderScope;
import org.apache.rave.portal.web.renderer.RenderService;
import org.apache.rave.portal.web.renderer.ScriptLocation;
import org.apache.rave.portal.web.renderer.ScriptManager;
import org.apache.rave.portal.web.renderer.model.RenderContext;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import static org.easymock.EasyMock.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

/**

 */
public class RegionWidgetTagTest {

    private static final String WIDGET_TYPE = "TEST";
    private static final String RENDERED = "RENDERED";
    private RegionWidgetTag tag;
    private RenderService service;
    private PageContext pageContext;
    private RenderContext context;
    private ScriptManager scriptManager;

    @Before
    public void setup() throws JspException {
        context = new RenderContext();
        service = createNiceMock(RenderService.class);
        scriptManager = createMock(ScriptManager.class);
        WebApplicationContext wContext = createNiceMock(WebApplicationContext.class);
        expect(wContext.getBean(RenderService.class)).andReturn(service).anyTimes();
        expect(wContext.getBean(ScriptManager.class)).andReturn(scriptManager).anyTimes();
        replay(wContext);
        ServletContext servletContext = createNiceMock(ServletContext.class);
        expect(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)).andReturn(wContext).anyTimes();
        replay(servletContext);
        ServletRequest request = new MockHttpServletRequest();
        request.setAttribute("_RENDER_CONTEXT", context);
        pageContext = createNiceMock(PageContext.class);
        expect(pageContext.getServletContext()).andReturn(servletContext).anyTimes();
        expect(pageContext.getRequest()).andReturn(request).anyTimes();
        tag = new RegionWidgetTag();
        tag.setPageContext(pageContext);
    }

    @Test
    public void doStartTag_valid() throws IOException, JspException {
        RegionWidget regionWidget = new RegionWidget();
        Widget widget = new Widget();
        regionWidget.setWidget(widget);
        widget.setType(WIDGET_TYPE);

        Set<String> strings = new HashSet<String>();
        strings.add(WIDGET_TYPE);

        expect(service.getSupportedWidgetTypes()).andReturn(strings);
        expect(service.render(regionWidget, context)).andReturn(RENDERED);
        replay(service);

        JspWriter writer = createNiceMock(JspWriter.class);
        writer.print(RENDERED);
        expectLastCall();
        replay(writer);

        expect(pageContext.getOut()).andReturn(writer);
        replay(pageContext);

        tag.setRegionWidget(regionWidget);
        int result = tag.doStartTag();
        assertThat(result, is(equalTo(1)));
        verify(writer);
    }

    @Test(expected = JspException.class)
    public void doStartTag_nullWidget() throws JspException {
        replay(pageContext);
        tag.doStartTag();
    }

    @Test(expected = JspException.class)
    public void doStartTag_IOException() throws JspException, IOException {

        RegionWidget regionWidget = new RegionWidget();
        Widget widget = new Widget();
        regionWidget.setWidget(widget);
        widget.setType("INVALID");

        Set<String> strings = new HashSet<String>();
        strings.add(WIDGET_TYPE);

        expect(service.getSupportedWidgetTypes()).andReturn(strings);
        replay(service);

        JspWriter writer = createNiceMock(JspWriter.class);
        writer.print(RENDERED);
        expectLastCall().andThrow(new IOException());
        replay(writer);

        replay(pageContext);

        tag.doStartTag();
        tag.doStartTag();
    }

    @Test(expected = JspException.class)
    public void doStartTag_unsupportedWidget() throws JspException {
        replay(pageContext);

        RegionWidget regionWidget = new RegionWidget();
        Region region = new Region(25L);
        Widget widget = new Widget();
        regionWidget.setWidget(widget);
        regionWidget.setRegion(region);
        widget.setType("INVALID");

        Set<String> strings = new HashSet<String>();
        strings.add(WIDGET_TYPE);

        expect(service.getSupportedWidgetTypes()).andReturn(strings);
        replay(service);

        tag.setRegionWidget(regionWidget);
        tag.doStartTag();
    }

    @Test
    public void doStartTag_disabledWidget() throws IOException, JspException {
        final String DISABLED_WIDGET_MESSAGE = "THIS IS DISABLED";

        Widget widget = new Widget();
        widget.setEntityId(8L);
        widget.setType(WIDGET_TYPE);
        widget.setDisableRendering(true);
        widget.setDisableRenderingMessage(DISABLED_WIDGET_MESSAGE);

        RegionWidget regionWidget = new RegionWidget();
        regionWidget.setEntityId(99L);
        regionWidget.setWidget(widget);
        regionWidget.setRegion(new Region(2L));

        Set<String> strings = new HashSet<String>();
        strings.add(WIDGET_TYPE);

        expect(service.getSupportedWidgetTypes()).andReturn(strings);
        scriptManager.registerScriptBlock(anyObject(String.class), anyObject(ScriptLocation.AFTER_RAVE.getClass()), anyObject(RenderScope.CURRENT_REQUEST.getClass()), anyObject(RenderContext.class));
        expectLastCall();
        replay(service, scriptManager);

        JspWriter writer = createNiceMock(JspWriter.class);
        expect(pageContext.getOut()).andReturn(writer);
        replay(pageContext, writer);

        tag.setRegionWidget(regionWidget);
        int result = tag.doStartTag();
        assertThat(result, is(equalTo(1)));
        verify(writer);
    }

    @Test
    public void getRegionWidget() throws IOException, JspException {
        RegionWidget regionWidget = new RegionWidget();
        tag.setRegionWidget(regionWidget);
        assertThat(tag.getRegionWidget(), sameInstance(regionWidget));
    }
}