text:
layout: post
title:  What is good software? Part 1 - Woof
date:   2018-09-26
author: Paschalis Ts
tags:   [software, industry, unix]
mathjax: false
description: "What constitutes 'good' software?"
---

Wikipedia states that
> "The term *engineering* is derived from the Latin ingenium, meaning "cleverness" and ingeniare, meaning "to contrive, devise".

And as Software Engineers, we often think, discuss, and fight about the 'cleverness' and 'excellence' of software, in the very same way people would argue about MJ vs LBJ, Ronaldo vs Messi or Pizza vs Tacos.
And while there is definitely no "metric" which you could use to quantify that, there's a piece of software that I've been recently using that I truly believe is *excellent*.

That software is [woof](http://www.home.unix-ag.org/simon/woof.html), by Simon Budig. It's a 600-lines Python script from 2009, used to.. send  a file over a network. The code is completely grok-able, and it's something that most people could reproduce given a spec sheet, but bear with me! It's 2018 and it's still *surprisingly unconvenient* to reliably and quickly transfer some little data from one point to another. Most of the times, you'd try one of the bazillion ways that your shop has sanctioned, end up trying to share credentials, and in the end either do it via USB or plain-old unencrypted chain-email attachments, right? :P

Woof (Web Offer One File), creates a simple stupid webserver, and serves the file or directory you want. When the file is tranferred, `woof` quits.

Poof. *Simple. Beautiful. Genius*. No maintaining software, long-running daemons, transferring keys, agreeing on installed software/stack, or using three different interfaces.

```
$ woof filename

Now serving on http://193.169.10.112:8080/filename
```

The enemy of all beauty and simplicity are corner cases, but when you actually plan and design, things become easier!

* Woof has an option to 'offer itself', so the other person can send you something back
* If a directory is specified, a tar of that directory is served
* It allows to customize the IP/port (to share under specific network), and how many times your file can be shared
* You can set values of preset configurations, for easier access.
* If you call it by `woof -U` it allows you to select and upload directly from your browser

I'd like to believe this is a prime example of Unix-philosophy, and good overall design

[x] It has a specified goal, it gets it done; it's clean, fast, reliable, stable
[x] Lacks 'bloat' and gazillions of features
[x] It is portable
[x] Is easy to understand and use
[x] Needs no maintenance

It might not win any awards, use the sexiest tech, or give you another 10 imaginary CV points, but it warms a good engineer's heart, and points where we should strive towards.

PS1. I'm going to reproduce it in Golang this weekend

PS2. Before you ask, yes, I do love GNU coreutils, and you should check [Gow - Gnu on Windows](https://github.com/bmatzelle/gow/wiki)


text:
package com.mantralabsglobal.cashin.ui.view;

import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mantralabsglobal.cashin.R;
import com.mantralabsglobal.cashin.businessobjects.BankProvider;
import com.mantralabsglobal.cashin.service.PrimaryBankService;
import com.mantralabsglobal.cashin.service.TransactionsService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pk on 6/21/2015.
 */
public class TransactionsView extends LinearLayout  {

    private TextView transactionDescription;
    private Button convertToEMI;

    private TransactionsService.TransactionDescriptionMessage transactionDescMsg;

    public TransactionsView(Context context) {
        this(context, null);
    }

    public TransactionsView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TransactionsView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        LayoutInflater inflater = LayoutInflater.from(getContext());
        inflater.inflate(R.layout.view_transactions, this);
        loadViews();
    }

    private void loadViews() {
        this.transactionDescription = (TextView)findViewWithTag("iv_transaction_description");
        this.convertToEMI = (Button)findViewWithTag("convert_to_emi");
    }

    @Override
    protected Parcelable onSaveInstanceState() {

        Bundle bundle = new Bundle();
        bundle.putParcelable("instanceState", super.onSaveInstanceState());
        bundle.putString("currentEdit", transactionDescription.getText().toString());
        bundle.putBoolean("isFocused", transactionDescription.hasFocus());
        return bundle;

    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {

        if (state instanceof Bundle) {
            Bundle bundle = (Bundle) state;
            transactionDescription.setText(bundle.getString("currentEdit"));
            if (bundle.getBoolean("isFocused")) {
                transactionDescription.requestFocus();
            }
            super.onRestoreInstanceState(bundle.getParcelable("instanceState"));
            return;
        }

        super.onRestoreInstanceState(state);
    }

    public TransactionsService.TransactionDescriptionMessage getTransactionDescMsg() {
        return transactionDescMsg;
    }

    public void setTransactionDescMsg(TransactionsService.TransactionDescriptionMessage transactionDescMsg) {
        this.transactionDescMsg = transactionDescMsg;
    }

    public void updateUI()
    {
        if(transactionDescMsg != null) {
            this.transactionDescription.setText(transactionDescMsg.getTransactionDescription());

        }
    }

}

redpajama_set_name: RedPajamaGithub


text:
'use strict';
module.exports = function(grunt) {

	/*
	 * grunt 								: Run default development server
	 * grunt cluster 						: Run default development cluster server with 2 nodes
	 * grunt cluster --nodes=N 				: Run default development cluster server with N nodes
	 * grunt minijs							: Minify client js files
	 * grunt less							: Compile less sources to css
	 * grunt mini							: Minify js files and compile less
	 * grunt jshint							: Analyze js files to check style and best practices
	 * grunt prod							: Run server in production mode
	 *
	 *  The server reloads if you change something from server side.
	 *  The minified and css are compiled when client side is changed.
	 *
	*/

	var getGruntOption = function(id,def) {
		return grunt.option(id) || def;
	};

	var generateOptions = function(arg) {
		return {
            args: arg,
            watch: ['log.js','proxy.js','server.js','webrtc.io.js','app'],
            debug: true,
            delayTime: 1,
            cwd: __dirname
        };
	};

	var checkMongoOff = function() {
	    var mongodb = getGruntOption('mongodb','on');
	    // Do not run mongo in openshift environment
	    if (process.env.OPENSHIFT_NODEJS_PORT || process.env.MONGOHQ_URL || mongodb==='off') {
	    	grunt.config.data.concurrent.prod.tasks.splice(0,1);
	    	grunt.config.data.concurrent.default.tasks.splice(0,1);
	    	grunt.config.data.concurrent.cluster.tasks.splice(0,1);
	    }
	};

	var generateNodeShell = function(port) {
		return {
			command: 'node server.js '+port+' test',
			options: {
				async: true,
				stdout: true,
				stderr: true
			}
		};
	};

	var generateTestNode = function(nodes) {
		var taskName = 't';
		var tasks = [];
		var mongodb = getGruntOption('mongodb','on');
		if (!process.env.OPENSHIFT_NODEJS_PORT && mongodb!=='off') {
			tasks.push('shell:mongo');
			taskName = 'n';
		}
		for (var j=1; j<nodes; j+=1) {
			// In test one server is running with jasmine
			if (!grunt.config.data.shell['node'+j]) {
				grunt.config.data.shell['node'+j] = generateNodeShell(Number(process.env.LOOWID_BASE_PORT)+j+1);
			}
			tasks.push('shell:node'+j);
		}
	    tasks.push('jasmine_node:'+taskName+nodes);
		return {
            tasks: tasks,
            options: {
                logConcurrentOutput: true
            }
    	};
	};

	var addTestNodesToTasks = function(tasks,tests) {
		for (var x=0; x<tests.length; x+=1) {
			grunt.config.data.concurrent['test'+x] = generateTestNode(tests[x]);
			tasks.push('concurrent:test'+x);
		}
	};

	var addClusterNodesToTasks = function(nodes) {
	    // Create nodemon tasks for cluster
	    for (var k=0; k<nodes; k+=1) {
	    	grunt.config.data.nodemon['dev'+k] = { script: 'server.js', options: generateOptions([Number(process.env.LOOWID_BASE_PORT)+k+1]) };
	    	grunt.config.data.concurrent.cluster.tasks.push('nodemon:dev'+k);
	    }
	};

    // Project Configuration
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        watch: {
            jade: {
                files: ['app/views/**'],
                tasks: ['jshint'],
                options: {
                    livereload: true,
                },
            },
            js: {
                files: ['client/js/**/*js','oembed.js'],
                tasks: ['minicheck'],
                options: {
                    livereload: true,
                },
            },
            html: {
                files: ['public/views/**'],
                options: {
                    livereload: true,
                },
            },
            css: {
                files: ['client/less/**'],
                tasks: ['less'],
                options: {
                    livereload: true
                }
            }
        },
        jshint: {
            all: {
            	files: {
            		src: ['*.js',
            		      'app/**/*.js',
            		      'spec/**/*.js',
            		      'client/js/*.js',
            		      'client/js/services/*.js',
            		      'client/js/controllers/*.js',
            		      '!client/js/lib/**/*.js'
            		      ]
            	},
            	options: {
            		jshintrc: '.jshintrc'
            	}
            }
        },
        shell: {
        	mongo: {
        		command: 'mongod --dbpath ./data',
        		options: {
        			async: true,
        			stdout: false,
        			stderr: true
        		}
        	}
        },
        nodemon: {
            prod: {
            	script: 'server.js',
            	options: {
            		watch: 'nothing.js'
            	}
            },
            dev: {
            	script: 'server.js'
            },
        	proxy: {
	        	script: 'proxy.js'
	        }
        },
        concurrent: {
        	default: {
	            tasks: ['shell:mongo','nodemon:dev', 'watch'],
	            options: {
	                logConcurrentOutput: true
	            }
        	},
        	prod: {
	            tasks: ['shell:mongo','nodemon:prod'],
	            options: {
	                logConcurrentOutput: true
	            }
        	},
        	cluster: {
	            tasks: ['shell:mongo', 'nodemon:proxy', 'watch'],
	            options: {
	                logConcurrentOutput: true
	            }
        	}
        },
        uglify: {
        	options: {
        	   mangle: false,
        	   sourceMap: true,
        	   compress: false
        	},
        	build: {
        		files: {
        			'public/js/loowid.min.js':[
						'client/js/lib/angular/angular.min.js',
						'client/js/lib/angular/angular-*.js',
						'client/js/lib/angular/ng-*.js',
						'client/js/lib/angular/ui-*.js',
						'client/js/lib/underscore/*.js',
						'client/js/lib/sigma/*.js',
						'client/js/*.js',
						'client/js/services/*.js',
						'client/js/controllers/*.js',
						'oembed.js'
					]
        		}
        	}
        },
        less: {
        	prod: {
        		options: {
        			paths:['client/less'],
        			cleancss:true
        		},
            	files: {
            		'public/css/loowid.min.css':'client/less/loowid.less'
            	}
        	}
        },
        'jasmine_node': {
            options: {
              forceExit: true,
              matchall: false,
              extensions: 'js',
              specNameMatcher: 'spec',
              junitreport: {
                report: true,
                savePath : './reports/',
                useDotNotation: true,
                consolidate: true
              }
            },
            coverage: {
                options : {
                    failTask: false,
                    branches : 100 ,
                    functions: 100,
                    statements:100,
                    lines:100
                }
            },
            all: ['spec/']
        }
    });

    //Load NPM tasks
    grunt.loadNpmTasks('grunt-shell');
    grunt.loadNpmTasks('grunt-nodemon');
    grunt.loadNpmTasks('grunt-concurrent');

    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-less');
    grunt.loadNpmTasks('grunt-jasmine-node');
    grunt.loadNpmTasks('grunt-jasmine-node-istanbul');

    //Making grunt default to force in order not to break the project.
    grunt.option('force', true);

    checkMongoOff();

    if (!process.env.OPENSHIFT_NODEJS_PORT) {
        process.env.LOOWID_HTTP_PORT = getGruntOption('port',80);
        process.env.LOOWID_HTTPS_PORT = getGruntOption('sport',443);
        process.env.LOOWID_BASE_PORT = getGruntOption('bport',8000);
    }

    // Cluster local configuration N nodes BasePort + 1, BasePort + 2,...
    // grunt cluster --nodes=N
    var nodes = getGruntOption('nodes',2);
    addClusterNodesToTasks(nodes);

    // Setting number of cluster nodes
    grunt.config.data.nodemon.proxy.options = generateOptions([nodes]);
    grunt.config.data.nodemon.dev.options = generateOptions([]);

    //Default task(s).
    grunt.registerTask('default', ['minicheck','concurrent:default']);

    grunt.registerTask('cluster', ['minicheck','concurrent:cluster']);
    grunt.registerTask('prod', ['mini','concurrent:prod']);

    // Minify tasks (generate min files)
    grunt.registerTask('minijs', ['uglify']);

    // Minify tasks (generate min files)
    grunt.registerTask('mini', ['minijs','less']);
    grunt.registerTask('minicheck', ['jshint','minijs','less']);

    var testingTasks = ['jshint'];
    // By default add 3 cluster tests with 3, 2 and 1 nodes. End with single node test to get max coberture report last
    var testNodes = (grunt.option('nodes')?[Number(grunt.option('nodes'))]:[3,2,1]);
    addTestNodesToTasks(testingTasks,testNodes);

    process.env.LOOWID_TEST_CASE = getGruntOption('testcase','');

    // Run tests
    grunt.registerTask('test', testingTasks);

};


text:
package curl

import (
	"context"
	"errors"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"net/url"
	"testing"

	"github.com/cenkalti/backoff/v4"
	"github.com/u-root/u-root/pkg/uio"
)

var (
	errTest = errors.New("Test error")
	testURL = &url.URL{
		Scheme: "fooftp",
		Host:   "192.168.0.1",
		Path:   "/foo/pxelinux.cfg/default",
	}
)

var tests = []struct {
	name string
	// scheme returns a scheme for testing and a MockScheme to
	// confirm number of calls to Fetch. The distinction is useful
	// when MockScheme is decorated by a SchemeWithRetries. In many
	// cases, the same value is returned twice.
	scheme         func() (FileScheme, *MockScheme)
	url            *url.URL
	err            error
	want           string
	wantFetchCount uint
}{
	{
		name: "successful fetch",
		scheme: func() (FileScheme, *MockScheme) {
			s := NewMockScheme("fooftp")
			s.Add("192.168.0.1", "/foo/pxelinux.cfg/default", "haha")
			return s, s
		},
		url:            testURL,
		want:           "haha",
		wantFetchCount: 1,
	},
	{
		name: "scheme does not exist",
		scheme: func() (FileScheme, *MockScheme) {
			s := NewMockScheme("fooftp")
			return s, s
		},
		url: &url.URL{
			Scheme: "nosuch",
		},
		err:            ErrNoSuchScheme,
		wantFetchCount: 0,
	},
	{
		name: "host does not exist",
		scheme: func() (FileScheme, *MockScheme) {
			s := NewMockScheme("fooftp")
			return s, s
		},
		url: &url.URL{
			Scheme: "fooftp",
			Host:   "someotherplace",
		},
		err:            ErrNoSuchHost,
		wantFetchCount: 1,
	},
	{
		name: "file does not exist",
		scheme: func() (FileScheme, *MockScheme) {
			s := NewMockScheme("fooftp")
			s.Add("somehost", "somefile", "somecontent")
			return s, s
		},
		url: &url.URL{
			Scheme: "fooftp",
			Host:   "somehost",
			Path:   "/someotherfile",
		},
		err:            ErrNoSuchFile,
		wantFetchCount: 1,
	},
	{
		name: "always err",
		scheme: func() (FileScheme, *MockScheme) {
			s := NewMockScheme("fooftp")
			s.Add("192.168.0.1", "/foo/pxelinux.cfg/default", "haha")
			s.SetErr(errTest, 9999)
			return s, s
		},
		url:            testURL,
		err:            errTest,
		wantFetchCount: 1,
	},
	{
		name: "retries but not necessary",
		scheme: func() (FileScheme, *MockScheme) {
			s := NewMockScheme("fooftp")
			s.Add("192.168.0.1", "/foo/pxelinux.cfg/default", "haha")
			r := &SchemeWithRetries{
				Scheme: s,
				// backoff.ZeroBackOff so unit tests run fast.
				BackOff: backoff.WithMaxRetries(&backoff.ZeroBackOff{}, 10),
			}
			return r, s
		},
		url:            testURL,
		want:           "haha",
		wantFetchCount: 1,
	},
	{
		name: "not enough retries",
		scheme: func() (FileScheme, *MockScheme) {
			s := NewMockScheme("fooftp")
			s.Add("192.168.0.1", "/foo/pxelinux.cfg/default", "haha")
			s.SetErr(errTest, 9999)
			r := &SchemeWithRetries{
				Scheme: s,
				// backoff.ZeroBackOff so unit tests run fast.
				BackOff: backoff.WithMaxRetries(&backoff.ZeroBackOff{}, 10),
			}
			return r, s
		},
		url:            testURL,
		err:            errTest,
		wantFetchCount: 11,
	},
	{
		name: "sufficient retries",
		scheme: func() (FileScheme, *MockScheme) {
			s := NewMockScheme("fooftp")
			s.Add("192.168.0.1", "/foo/pxelinux.cfg/default", "haha")
			s.SetErr(errTest, 5)
			r := &SchemeWithRetries{
				Scheme: s,
				// backoff.ZeroBackOff so unit tests run fast.
				BackOff: backoff.WithMaxRetries(&backoff.ZeroBackOff{}, 10),
			}
			return r, s
		},
		url:            testURL,
		want:           "haha",
		wantFetchCount: 6,
	},
	{
		name: "retry filter",
		scheme: func() (FileScheme, *MockScheme) {
			s := NewMockScheme("fooftp")
			s.Add("192.168.0.1", "/foo/pxelinux.cfg/default", "haha")
			s.SetErr(errTest, 5)
			r := &SchemeWithRetries{
				DoRetry: func(u *url.URL, err error) bool {
					return err != errTest
				},
				Scheme: s,
				// backoff.ZeroBackOff so unit tests run fast.
				BackOff: backoff.WithMaxRetries(&backoff.ZeroBackOff{}, 10),
			}
			return r, s
		},
		url:            testURL,
		err:            errTest,
		wantFetchCount: 1,
	},
}

func TestFetchWithoutCache(t *testing.T) {
	for i, tt := range tests {
		t.Run(fmt.Sprintf("Test #%02d: %s", i, tt.name), func(t *testing.T) {
			var r io.Reader
			var err error

			fs, ms := tt.scheme()
			s := make(Schemes)
			s.Register(ms.Scheme, fs)

			r, err = s.FetchWithoutCache(context.TODO(), tt.url)
			if uErr, ok := err.(*URLError); ok && uErr.Err != tt.err {
				t.Errorf("FetchWithoutCache() = %v, want %v", uErr.Err, tt.err)
			} else if !ok && err != tt.err {
				t.Errorf("FetchWithoutCache() = %v, want %v", err, tt.err)
			}

			// Check number of calls before reading the file.
			numCalled := ms.NumCalled(tt.url)
			if numCalled != tt.wantFetchCount {
				t.Errorf("number times Fetch() called = %v, want %v",
					ms.NumCalled(tt.url), tt.wantFetchCount)
			}
			if err != nil {
				return
			}

			// Read the entire file.
			content, err := io.ReadAll(r)
			if err != nil {
				t.Errorf("bytes.Buffer read returned an error? %v", err)
			}
			if got, want := string(content), tt.want; got != want {
				t.Errorf("Fetch() = %v, want %v", got, want)
			}

			// Check number of calls after reading the file.
			numCalled = ms.NumCalled(tt.url)
			if numCalled != tt.wantFetchCount {
				t.Errorf("number times Fetch() called = %v, want %v",
					ms.NumCalled(tt.url), tt.wantFetchCount)
			}
		})
	}
}

func TestFetch(t *testing.T) {
	for i, tt := range tests {
		t.Run(fmt.Sprintf("Test #%02d: %s", i, tt.name), func(t *testing.T) {
			var r io.ReaderAt
			var err error

			fs, ms := tt.scheme()
			s := make(Schemes)
			s.Register(ms.Scheme, fs)

			r, err = s.Fetch(context.TODO(), tt.url)
			if uErr, ok := err.(*URLError); ok && uErr.Err != tt.err {
				t.Errorf("Fetch() = %v, want %v", uErr.Err, tt.err)
			} else if !ok && err != tt.err {
				t.Errorf("Fetch() = %v, want %v", err, tt.err)
			}

			// Check number of calls before reading the file.
			numCalled := ms.NumCalled(tt.url)
			if numCalled != tt.wantFetchCount {
				t.Errorf("number times Fetch() called = %v, want %v",
					ms.NumCalled(tt.url), tt.wantFetchCount)
			}
			if err != nil {
				return
			}

			// Read the entire file.
			content, err := io.ReadAll(uio.Reader(r))
			if err != nil {
				t.Errorf("bytes.Buffer read returned an error? %v", err)
			}
			if got, want := string(content), tt.want; got != want {
				t.Errorf("Fetch() = %v, want %v", got, want)
			}

			// Check number of calls after reading the file.
			numCalled = ms.NumCalled(tt.url)
			if numCalled != tt.wantFetchCount {
				t.Errorf("number times Fetch() called = %v, want %v",
					ms.NumCalled(tt.url), tt.wantFetchCount)
			}
		})
	}
}

func TestLazyFetch(t *testing.T) {
	for i, tt := range tests {
		t.Run(fmt.Sprintf("Test #%02d: %s", i, tt.name), func(t *testing.T) {
			var r io.ReaderAt
			var err error

			fs, ms := tt.scheme()
			s := make(Schemes)
			s.Register(ms.Scheme, fs)

			r, err = s.LazyFetch(tt.url)
			// Errors are deferred to when file is read except for ErrNoSuchScheme.
			if tt.err == ErrNoSuchScheme {
				if uErr, ok := err.(*URLError); ok && uErr.Err != ErrNoSuchScheme {
					t.Errorf("LazyFetch() = %v, want %v", uErr.Err, tt.err)
				}
			} else if err != nil {
				t.Errorf("LazyFetch() = %v, want nil", err)
			}

			// Check number of calls before reading the file.
			numCalled := ms.NumCalled(tt.url)
			if numCalled != 0 {
				t.Errorf("number times Fetch() called = %v, want 0", numCalled)
			}
			if err != nil {
				return
			}

			// Read the entire file.
			content, err := io.ReadAll(uio.Reader(r))
			if uErr, ok := err.(*URLError); ok && uErr.Err != tt.err {
				t.Errorf("ReadAll() = %v, want %v", uErr.Err, tt.err)
			} else if !ok && err != tt.err {
				t.Errorf("ReadAll() = %v, want %v", err, tt.err)
			}
			if got, want := string(content), tt.want; got != want {
				t.Errorf("ReadAll() = %v, want %v", got, want)
			}

			// Check number of calls after reading the file.
			numCalled = ms.NumCalled(tt.url)
			if numCalled != tt.wantFetchCount {
				t.Errorf("number times Fetch() called = %v, want %v",
					ms.NumCalled(tt.url), tt.wantFetchCount)
			}
		})
	}
}

func TestHttpFetches(t *testing.T) {
	c := "fetch content"
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, c)
	}))
	defer ts.Close()

	fURL, err := url.Parse(ts.URL)
	if err != nil {
		t.Fatalf("url.Parse(%s) = %v, want no error", ts.URL, err)
	}

	// Fetch need to fetch the content as is.
	fetchFile, err := Fetch(context.Background(), fURL)
	if err != nil {
		t.Errorf("Fetch(context.Background(), %s) = %v, want no error", fURL, err)
	}
	got, err := io.ReadAll(io.NewSectionReader(fetchFile, 0, int64(len(c))))
	if err != nil {
		t.Errorf("io.ReadAll(%v) = %v, want no error", fetchFile, err)
	}
	if string(got) != c {
		t.Errorf("got %s, want %s", got, c)
	}

	// FetchWithoutCache need to fetch the content as is.
	fetchFileNoCache, err := FetchWithoutCache(context.Background(), fURL)
	if err != nil {
		t.Errorf("FetchWithoutCache(context.Background(), %s) = %v, want no error", fURL, err)
	}
	got, err = io.ReadAll(fetchFileNoCache)
	if err != nil {
		t.Errorf("io.ReadAll(%s) = %v, want no error", fetchFileNoCache, err)
	}
	if string(got) != c {
		t.Errorf("got %s, want %s", got, c)
	}
}


text:
package thrift

import "context"

// ProcessorMiddleware is a function that can be passed to WrapProcessor to wrap the
// TProcessorFunctions for that TProcessor.
//
// Middlewares are passed in the name of the function as set in the processor
// map of the TProcessor.
type ProcessorMiddleware func(name string, next TProcessorFunction) TProcessorFunction

// WrapProcessor takes an existing TProcessor and wraps each of its inner
// TProcessorFunctions with the middlewares passed in and returns it.
//
// Middlewares will be called in the order that they are defined:
//
//		1. Middlewares[0]
//		2. Middlewares[1]
//		...
//		N. Middlewares[n]
func WrapProcessor(processor TProcessor, middlewares ...ProcessorMiddleware) TProcessor {
	for name, processorFunc := range processor.ProcessorMap() {
		wrapped := processorFunc
		// Add middlewares in reverse so the first in the list is the outermost.
		for i := len(middlewares) - 1; i >= 0; i-- {
			wrapped = middlewares[i](name, wrapped)
		}
		processor.AddToProcessorMap(name, wrapped)
	}
	return processor
}

// WrappedTProcessorFunction is a convenience struct that implements the
// TProcessorFunction interface that can be used when implementing custom
// Middleware.
type WrappedTProcessorFunction struct {
	// Wrapped is called by WrappedTProcessorFunction.Process and should be a
	// "wrapped" call to a base TProcessorFunc.Process call.
	Wrapped func(ctx context.Context, seqId int32, in, out TProtocol) (bool, TException)
}

// Process implements the TProcessorFunction interface using p.Wrapped.
func (p WrappedTProcessorFunction) Process(ctx context.Context, seqID int32, in, out TProtocol) (bool, TException) {
	return p.Wrapped(ctx, seqID, in, out)
}

// verify that WrappedTProcessorFunction implements TProcessorFunction
var (
	_ TProcessorFunction = WrappedTProcessorFunction{}
	_ TProcessorFunction = (*WrappedTProcessorFunction)(nil)
)

// ClientMiddleware can be passed to WrapClient in order to wrap TClient calls
// with custom middleware.
type ClientMiddleware func(TClient) TClient

// WrappedTClient is a convenience struct that implements the TClient interface
// using inner Wrapped function.
//
// This is provided to aid in developing ClientMiddleware.
type WrappedTClient struct {
	Wrapped func(ctx context.Context, method string, args, result TStruct) error
}

// Call implements the TClient interface by calling and returning c.Wrapped.
func (c WrappedTClient) Call(ctx context.Context, method string, args, result TStruct) error {
	return c.Wrapped(ctx, method, args, result)
}

// verify that WrappedTClient implements TClient
var (
	_ TClient = WrappedTClient{}
	_ TClient = (*WrappedTClient)(nil)
)

// WrapClient wraps the given TClient in the given middlewares.
//
// Middlewares will be called in the order that they are defined:
//
//		1. Middlewares[0]
//		2. Middlewares[1]
//		...
//		N. Middlewares[n]
func WrapClient(client TClient, middlewares ...ClientMiddleware) TClient {
	// Add middlewares in reverse so the first in the list is the outermost.
	for i := len(middlewares) - 1; i >= 0; i-- {
		client = middlewares[i](client)
	}
	return client
}


text:
---
title: Rule no-magic-numbers
layout: doc
---
<!-- Note: No pull requests accepted for this file. See README.md in the root directory for details. -->
# Disallow Magic Numbers (no-magic-numbers)

'Magic numbers' are numbers that occur multiple time in code without an explicit meaning.
They should preferably be replaced by named constants.

```js
var now = Date.now(),
    inOneHour = now + (60 * 60 * 1000);
```

## Rule Details

The `no-magic-numbers` rule aims to make code more readable and refactoring easier by ensuring that special numbers
are declared as constants to make their meaning explicit.

The following pattern is considered a problem:

```js
/*eslint no-magic-numbers: 2*/

var dutyFreePrice = 100,
    finalPrice = dutyFreePrice + (dutyFreePrice * 0.25); /*error No magic number: 0.25*/
```

The following pattern is considered okay:

```js
/*eslint no-magic-numbers: 2*/

var TAX_PERCENTAGE = 0.25;

var dutyFreePrice = 100,
    finalPrice = dutyFreePrice + (dutyFreePrice * TAX_PERCENTAGE);
```

## Options

### ignore

An array of numbers to ignore. It's set to `[0, 1, 2]` by default.
If provided, it must be an `Array`.

### enforceConst

A boolean to specify if we should check for the const keyword in variable declaration of numbers. `false` by default.

### detectObjects

A boolean to specify if we should detect numbers when setting object properties for example. `false` by default.

## Version

This rule was introduced in ESLint 1.7.0.

## Resources

* [Rule source](https://github.com/eslint/eslint/tree/master/lib/rules/no-magic-numbers.js)
* [Documentation source](https://github.com/eslint/eslint/tree/master/docs/rules/no-magic-numbers.md)


text:
<?php
namespace galvao\DataAbstract;

/**
 * Abstraction for Data objects
 * @package DataAbstract
 * @author Er Galvão Abbott <galvao@galvao.eti.br>
 */

abstract class Data
{
    private $type;
    private $value;

    public function __construct($desiredType = false, $value)
    {
        try {
            $this->setType($desiredType, $value);
        } catch(\Exception $e) {
            die($e->getMessage());
        }

        $this->setValue($value);
    }

    public function getType()
    {
        return $this->type;
    }

    public function getValue()
    {
        return $this->value;
    }

    public function setType($desiredType, $value)
    {
        if ($desiredType) {
            $fault = false;

            switch($desiredType) {
                case 'int':
                case 'integer':
                    if (!preg_match('/^\d+$/', $value)) {
                        $fault = true;
                    }

                    break;

                case 'float':
                case 'double':
                    if (!preg_match('/^\d+(\.?\d*)$/', $value)) {
                        $fault = true;
                    }

                    break;

                case 'boolean':
                    if (!in_array($value, array('true', 'false', 0, 1))) {
                        $fault = true;
                    }

                    break;
            }

            if ($fault) {
                throw new \Exception("Can't convert >$value< to $desiredType");
            }

            settype($value, $desiredType);
        }

        $this->type = gettype($value);
    }

    public function setValue($value)
    {
        $this->value = $value;
    }
}


text:
package core

import (
	"github.com/fcavani/gormethods/loging"
	log "github.com/fcavani/slog"
)

func init() {
	log.SetOutput(loging.Domain, loging.Level, loging.Writter, nil, nil, 100)
}


text:
"use strict";

import SSEConnection from "../main/sse-connection"
import Feedback      from "../main/feedback"
import activate      from "../main/activator"

function TestShow(){
    let dataToShow
    function show(data){
        dataToShow = data
    }
    function checkActivate(){
        document.body.textContent = dataToShow
    }
    const receivers = ({show})
    return ({receivers,checkActivate})
}
const feedback = Feedback(localStorage,sessionStorage,document.location,fetch,setTimeout)
window.onhashchange = () => feedback.pong()
const testShow = TestShow()
const receiversList = [feedback.receivers, testShow.receivers]
const createEventSource = () => new EventSource(location.protocol+"//"+location.host+"/sse")
const connection = SSEConnection(createEventSource, receiversList, 5000)
activate(requestAnimationFrame, [connection.checkActivate,testShow.checkActivate])


text:
from ytdata import YTData

# instantiate
cnn_data = YTData('UCupvZG-5ko_eiXAupbDfxWw',  # CNN's YouTube channel
                  fields=['videoId', 'title', 'description',
                          'viewCount', 'duration', 'publishedAt'],
                  max_results=1000,
                  verbose=True)

# request and select
cnn_data.fetch()

# peek
print('Most recent videos:')
for i, item in enumerate(cnn_data.items[:10]):
    print('  %d. %s' % (i+1, item['title']))
print()

# store
cnn_data.dump('cnn.json')


text:
//
//  AppDelegate.m
//  XMLolGames
//
//  Created by montnets on 2016/12/22.
//  Copyright © 2016年 montnets. All rights reserved.
//

#import "AppDelegate.h"

#import "XMSideViewController.h"
#import "XMLeftViewController.h"
#import "XMMainViewController.h"
#import "XMTabbarController.h"
#import "XMUserInfo.h"
#import "XMNavViewController.h"
#import "XMDataStorage.h"
#import "XMHttpOperation.h"
#import "XMProgressView.h"
#import "BBobjectProgress.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    XMLeftViewController *leftVC = [[XMLeftViewController alloc]init];
    XMTabbarController *tababr = [[XMTabbarController alloc]init];
    XMSideViewController *sideVC = [XMSideViewController SideViewControllerWithLeftController:leftVC andMainController:tababr];
    [self setStatusStyle];
    self.window.rootViewController = sideVC;

    //推送通知
    if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
        //>=ios8
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil]];
        [application registerForRemoteNotifications];
    }
//    else{
//        //<ios8
//        [application registerForRemoteNotificationTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge];
//    }

    return YES;
}

/** 设置状态栏颜色 */
-(void)setStatusStyle{
    //需要在info.plist文件里面设置 View controller-based status bar appearance    为NO
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
/** 获取deviceToken */
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData *)deviceToken{
    XMLog(@"token - %@",deviceToken);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    XMLog(@"获取deviceToken失败的原因 - %@",error);
}

/** 程序进入后台 */
-(void)applicationDidEnterBackground:(UIApplication *)application{
    //进入后台后开始
    [XMUserInfo shareManager].enterBackgroundTime = [NSDate date];
    XMLog(@"程序进入后台,开始计时");
}
/** 程序进入前台.处于使用状态 */
-(void)applicationDidBecomeActive:(UIApplication *)application{

    NSString *isLogin = [XMDataStorage readinfoWithKey:@"XMinfo"];
    //已经登录了
    if (isLogin.length!=0) {
        XMLog(@"已经登录了");
        NSDate *date = [XMUserInfo shareManager].enterBackgroundTime;
        if (date) {
            NSCalendar *cal = [NSCalendar currentCalendar];

            unsigned int timeflags = NSCalendarUnitYear|
            NSCalendarUnitMonth |
            NSCalendarUnitDay |
            NSCalendarUnitHour |
            NSCalendarUnitMinute |
            NSCalendarUnitSecond;
            NSDateComponents *d =[cal components:timeflags fromDate:date toDate:[NSDate date] options:0];

            //得到进入后台多少时间。。。单位是秒
            long sec = [d hour]*3600 + [d minute]*60 +[d second];
            if (sec>=10) {
                XMLog(@"超过1分钟了,请重新登录");
//                XMMainViewController *mainVC = [[XMMainViewController alloc]init];
//                XMNavViewController *nav = [[XMNavViewController alloc]initWithRootViewController:mainVC];
//                [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:nav animated:YES completion:nil];
                //发送自动登录的信息
                [[NSNotificationCenter defaultCenter] postNotificationName:XMautoLoginNotification object:nil];

            }
        }
    }

}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end


text:
  <form name="configForm">

Graph Controls

Reset to defaults

      Zoom:
        <input class="form-control" type="number" name="basesPerGraph"
               style="width:5em;"
               min="100" required
               ng-model="gc.basesPerGraph"
               ng-model-options="{ updateOn: 'default blur', debounce: { 'default': 600, 'blur': 0 } }">
        bases/line

Cannot zoom in that far.

Must specify a zoom level

      Range:
      <input class="form-control" type="number" name="startBase" style="width:6em;"
             min="0" max="{{gc.length}}"
             ng-model="gc.startBase"
             ng-model-options="{ updateOn: 'default blur', debounce: { 'default': 600, 'blur': 0 } }">
      to
      <input class="form-control" type="number" name="endBase" style="width:6em;"
             min="0" max="{{gc.length}}"
             ng-model="gc.endBase"
             ng-model-options="{ updateOn: 'default blur', debounce: { 'default': 600, 'blur': 0 } }">

      Goto:
        <input class="form-control" type="number" name="gotoBase" style="width:6em;"
               min="0" max="{{gc.length}}"
               ng-model="gc.gotoBase"
               ng-model-options="{ updateOn: 'default blur', debounce: { 'default': 600, 'blur': 0 } }">

      Find ORF by name:

        <input class="form-control" type="text" name="findORF" style="width:9em;"
               ng-model="gc.findORF"
               ng-model-options="{ updateOn: 'default blur', debounce: { 'default': 1200, 'blur': 0 } }"
               npact-orf-finder
               >

       0">
      <button ng-click="resultsIndex = resultsIndex - 1" class="arrowbutton"><</button>
      <button ng-click="resultsIndex = resultsIndex + 1" class="arrowbutton">></button>
      {{resultsIndex + 1}} of {{results.length}}

        No results found

        <input type="checkbox" ng-model="gc.colorBlindFriendly"
               ng-change="gc.cookiePersist()">
        Color-blind friendly

        <input type="checkbox" ng-model="gc.mycoplasma">
        Mycoplasma

Tracks

          <input ng-model="t.active" type="checkbox" /> {{t.name}}

N-Profile Options

        Base types profiled:

        <checkbox-list id="profile-options" ng-model="gc.nucleotides"
                       title="nucleotide type(s) represented in frame analysis"
                       options="['A', 'C', 'G', 'T']">

  </form>


text:
package eu.hyvar.context.contextValidity.resource.hyvalidityformula;

import java.util.Collection;

public interface IHyvalidityformulaProblem {
	public String getMessage();
	public eu.hyvar.context.contextValidity.resource.hyvalidityformula.HyvalidityformulaEProblemSeverity getSeverity();
	public eu.hyvar.context.contextValidity.resource.hyvalidityformula.HyvalidityformulaEProblemType getType();
	public Collection<eu.hyvar.context.contextValidity.resource.hyvalidityformula.IHyvalidityformulaQuickFix> getQuickFixes();
}


text:
typedef NS_ENUM(NSUInteger, FunctionSelet) {
    LBS_GAME = 0,
    LBS_EAT,
    LBS_REST,
    LBS_BUSSINESS,
    LBS_SHOPPING,
    LBS_OTHER1,
    LBS_OTHER2,
    LBS_OTHER3,
    LBS_OTHER4,
};

@interface ViewController ()<UISearchBarDelegate>

@property (nonatomic, strong) UISearchBar * searchBar;

@end

@implementation ViewController

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

    self.view.backgroundColor = [UIColor orangeColor];

    //搜索框
    _searchBar = [[UISearchBar alloc]init];
    [_searchBar setDelegate:self];
    [_searchBar setPlaceholder:@"请输入想要查询的位置"];
    [_searchBar setBackgroundImage:[self CreateImage:[UIColor orangeColor]imageSize:CGSizeMake(self.view.frame.size.width, 50)]];
    [_searchBar setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:_searchBar];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_searchBar]|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:NSDictionaryOfVariableBindings(_searchBar)]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[_searchBar]"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:NSDictionaryOfVariableBindings(_searchBar)]];

    //九宫格按钮
    NSUInteger dataArr[] = {LBS_GAME,LBS_EAT,LBS_REST,LBS_BUSSINESS,LBS_SHOPPING,LBS_OTHER1,LBS_OTHER2,LBS_OTHER3,LBS_OTHER4};

    CGFloat btnSize = 80;
    CGFloat spacing = (self.view.frame.size.width - 3*btnSize) / 4;

    for (int i =0 ; i<9; i++) {
        UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTranslatesAutoresizingMaskIntoConstraints:NO];
        [btn setTag:dataArr[i]];
        [btn addTarget:self action:@selector(theBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
//        [btn setBackgroundColor:[UIColor cyanColor]];
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d",i+1]] forState:UIControlStateNormal];
        [self.view addSubview:btn];

        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-btnH-[btn(btnW)]"
                                                                          options:0
                                                                          metrics:@{@"btnH":@(i%3*(btnSize+spacing)+spacing),
                                                                                @"btnW":@(btnSize)}
                                                                            views:NSDictionaryOfVariableBindings(btn)]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-btnV-[btn(btnH)]"
                                                                          options:0
                                                                          metrics:@{@"btnV":@(80+i/3*(btnSize+spacing)+spacing),
                                                                                @"btnH":@(btnSize)}
                                                                            views:NSDictionaryOfVariableBindings(btn)]];
    }

}

//跳转到下一页
-(void) theBtnPressed:(id)sender{
    UIButton *btn = (UIButton *)sender;
    NSString *userSelectString = nil;
    switch (btn.tag) {
        case LBS_GAME:
            userSelectString = @"休息";
            NSLog(@"rest");
            break;
        case LBS_EAT:
            userSelectString = @"餐饮";
            NSLog(@"eat");
            break;
        case LBS_REST:
            userSelectString = @"娱乐";
            NSLog(@"rest");
            break;
        case LBS_BUSSINESS:
            userSelectString = @"商业";
            NSLog(@"bussiness");
            break;
        case LBS_SHOPPING:
            userSelectString = @"购物";
            NSLog(@"bussiness");
            break;
        default:
            userSelectString = @"停车场";
            NSLog(@"other");
            break;
    }

    [self gotoShowDataView:userSelectString];
}

//携带数据跳转下一页
- (void) gotoShowDataView:(NSString *)str{
    ShowDataTableViewController *showDataController = [[ShowDataTableViewController alloc]init];
    showDataController.searchString = str;
    [self.navigationController pushViewController:showDataController animated:YES];

}

-(UIImage *) CreateImage:(UIColor *) color imageSize:(CGSize) size
{
    CGRect frame = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, frame);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

#pragma mark _searchBarDelegate

-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [_searchBar resignFirstResponder];
    NSString *tempStr = [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (tempStr.length == 0 ) {
        return;
    }
    [self gotoShowDataView:tempStr];
}

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_searchBar resignFirstResponder];
}

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

@end


text:
struct edge_flow {
  int capacity;
  int flow;
};
struct node_excess {
  int excess = 0;
  int height = 0;
};

//Typedefs for edges, nodes and graph to shorten code
typedef Graph<edge_flow, node_excess> MaxFlowGraph;
typedef Node<edge_flow, node_excess> MaxFlowNode;
typedef Edge<edge_flow, node_excess> MaxFlowEdge;

int excess(MaxFlowNode* node, const size_t& start) {
  if (node->id == start) {
    return std::numeric_limits<int>::max();
  }
  else {
    return node->extra.excess;
  }
}

bool bfs(const std::vector<std::vector<int> >& rGraph, int s, int t, std::vector<int>& parent, const vector<set<int>>&edges)
{
  const size_t& V = rGraph.size();
  // Create a visited array and mark all vertices as not visited
  std::vector<bool> visited(V, false);

  // Create a queue, enqueue source vertex and mark source vertex
  // as visited
  std::queue<int> q;
  q.push(s);
  visited[s] = true;
  parent[s] = -1;

  // Standard BFS Loop
  while (!q.empty())
  {
    int u = q.front();
    q.pop();

    //for (int v = 0; v < V; v++)
    for (auto v : edges[u])
    {
      if (visited[v] == false && rGraph[u][v] > 0)
      {
        q.push(v);
        parent[v] = u;
        visited[v] = true;
      }
    }
  }

  // If we reached sink in BFS starting from source, then return
  // true, else false
  return (visited[t] == true);
}

// Returns the maximum flow from s to t in the given graph
int fordFulkerson(const std::vector<std::vector<int> >& graph, int s, int t, std::vector<std::vector<int> >& flowGraph, const vector<set<int>>&edges)
{
  const size_t& V = graph.size();
  int u, v;

  // Create a residual graph and fill the residual graph with
  // given capacities in the original graph as residual capacities
  // in residual graph
  std::vector<std::vector<int> > rGraph(V, std::vector<int>(V)); // Residual graph where rGraph[i][j] indicates
  // residual capacity of edge from i to j (if there
  // is an edge. If rGraph[i][j] is 0, then there is not)
  for (u = 0; u < V; u++)
    for (v = 0; v < V; v++)
      rGraph[u][v] = graph[u][v];

  std::vector<int> parent(V);  // This array is filled by BFS and to store path

  int max_flow = 0;  // There is no flow initially

  // Augment the flow while there is path from source to sink
  while (bfs(rGraph, s, t, parent, edges))
  {
    // Find minimum residual capacity of the edges along the
    // path filled by BFS. Or we can say find the maximum flow
    // through the path found.
    int path_flow = std::numeric_limits<int>::max();
    for (v = t; v != s; v = parent[v])
    {
      u = parent[v];
      path_flow = std::min(path_flow, rGraph[u][v]);
    }

    // update residual capacities of the edges and reverse edges
    // along the path
    for (v = t; v != s; v = parent[v])
    {
      u = parent[v];
      rGraph[u][v] -= path_flow;
      rGraph[v][u] += path_flow;
    }

    // Add path flow to overall flow
    max_flow += path_flow;
  }

  //Construct flow

  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      flowGraph[i][j] = std::max(graph[i][j] - rGraph[i][j], 0);
    }
  }

  // Return the overall flow
  return max_flow;
}


text:
require 'spec_helper'

describe Node, 'valid' do
  it { should belong_to :tree }
  it { should belong_to :name }
  it { should validate_presence_of :tree_id }

  it { should have_many :bookmarks }
  it { should have_many :synonyms }
  it { should have_many :vernacular_names }

  it 'supports constructing a hierarchy' do
    root = create(:node)
    child = create(:node,      parent: root)
    grandchild = create(:node, parent: child)

    root.children.should == [child]
    grandchild.parent.parent.should == root
  end

  it 'should by default be a child of the root node' do
    root = create(:node)
    child = create(:node, parent: root)
    real_root = root.tree.root
    real_root.children.should == [root]
    root.children.should == [child]
  end
end

describe Node, '#deep_copy_to' do
  it 'produces a deep copy of its subtree, with node names, synonyms, and vernacular names' do
    tree        = create(:reference_tree)
    root        = create(:node, tree: tree, name: create(:name, name_string: 'Root'))
    child       = create(:node, tree: tree, parent: root, name: create(:name, name_string: 'Child'))
    grandchild  = create(:node, tree: tree, parent: child, name: create(:name, name_string: 'Grandchild'))
    master_tree = create(:master_tree)

    synonym         = create(:synonym, node: grandchild)
    vernacular_name = create(:vernacular_name, node: child)

    another_root = root.deep_copy_to(master_tree)
    another_root.name_string.should == 'Root'
    another_root.tree.should == master_tree

    another_root.children.size.should == 1
    another_root.children.first.name_string.should == 'Child'
    another_root.children.first.tree.should == master_tree
    another_root.children.first.vernacular_names.size.should == 1
    another_root.children.first.vernacular_names.first.name.should == vernacular_name.name

    another_root.children.first.children.size.should == 1
    another_root.children.first.children.first.name_string.should == 'Grandchild'
    another_root.children.first.children.first.tree.should == master_tree
    another_root.children.first.children.first.synonyms.size.should == 1
    another_root.children.first.children.first.synonyms.first.name.should == synonym.name
  end
end

describe Node, 'name' do
  let(:name) { create(:name) }
  subject { create(:node, name: name) }

  it 'delegates name to the Name model' do
    subject.name_string.should == name.name_string
    name.name_string = 'a new name'
    subject.name_string.should == 'a new name'
  end
end

describe Node, '#rank_string' do
  it 'returns None if rank is nil' do
    node = create(:node, rank: nil)
    node.rank_string.should == 'None'
  end

  it 'returns None if rank is empty' do
    node = create(:node, rank: '  ')
    node.rank_string.should == 'None'
  end

  it 'returns rank if present' do
    node = create(:node, rank: 'Family')
    node.rank_string.should == 'Family'
  end
end

describe Node, '#synonym_data for a synonym' do
  let(:node) { create(:node) }

  before do
    @synonym = create(:synonym, node: node)
  end

  it 'returns data for a synonym' do
    node.synonym_data[0][:name_string].should == @synonym.name.name_string
    node.synonym_data[0][:metadata][:status] == @synonym.status
  end
end

describe Node, '#synonym_data with no synonyms' do
  let(:node) { create(:node) }

  it 'returns None' do
    node.synonym_data.should == []
  end
end

describe Node, '#vernacular_data for a vernacular' do
  let(:node) { create(:node) }
  let(:language) { create(:language, name: 'English', iso_639_1: 'enz', iso_639_2: 'engz', iso_639_3: 'engz', native: 'English') }

  before do
    @vernacular = create(:vernacular_name, node: node, language: language)
  end

  it 'returns data for a vernacular name' do
    node.vernacular_data[0][:name_string].should == @vernacular.name.name_string
    node.vernacular_data[0][:metadata][:language][:name] == @vernacular.language.name
  end
end

describe Node, '#vernacular_data with no vernaculars' do
  let(:node) { create(:node) }

  it 'returns None' do
    node.vernacular_data.should == []
  end
end

describe Node, "#children" do
  let (:parent) { create(:node) }
  let (:names) do
    %w{ Gossleriellaceae
      Stictodiscaceae
      Arachnoidiscaceae
      Leptocylindraceae
      Corethraceae
      Heliopeltaceae
      Ethmodiscaceae }.map { |n| create(:name, name_string: n) }
  end
  let (:children_nodes) { names.map{ |name| create(:node, tree_id: parent.tree_id, parent_id: parent.id, name: name) } }

  it 'should sort names by alphabet' do
    unsorted_names = children_nodes.map { |node| node.name.name_string }
    unsorted_names.should_not == unsorted_names.sort
    parent.children.map { |node| node.name.name_string }.should == names.map { |name| name.name_string }.sort
  end

  it 'should work with a subset of fields' do
    children_nodes #won't load implicitly
    first_child = parent.reload.children('id')[0]
    first_child_attributes = first_child.attributes.keys
    first_child_attributes.should_not include('tree_id')
    first_child_attributes.should_not include('name_id')
    first_child.id.is_a?(Fixnum).should be_truthy
    first_child_full = parent.children[0]
    first_child_full_attributes = first_child_full.attributes.keys
    first_child_full_attributes.should include('tree_id')
    first_child_full_attributes.should include('name_id')
    first_child_full.id.should_not be_nil
  end
end

describe Node, '#destroy_with_children' do
  it 'deletes the node and its descendents, related synonyms and vernacular names' do
    tree        = create(:reference_tree)
    root        = create(:node, tree: tree, name: create(:name, name_string: 'Root'))
    child       = create(:node, tree: tree, parent: root, name: create(:name, name_string: 'Child'))
    grandchild  = create(:node, tree: tree, parent: child, name: create(:name, name_string: 'Grandchild'))
    master_tree = create(:master_tree)

    synonym         = create(:synonym, node: grandchild)
    vernacular_name = create(:vernacular_name, node: child)
    nodes_count = Node.count
    vern_count = VernacularName.count
    syn_count = Synonym.count
    root.destroy_with_children
    (nodes_count - Node.count).should == 3
    (vern_count - VernacularName.count).should == 1
    (syn_count - Synonym.count).should == 1
  end
end


text:
#ifndef __SYM_EVAL_HH__
#define __SYM_EVAL_HH__

#include "llvm/IR/Instruction.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Debug.h"
#include "llvm/ADT/APInt.h"

#include "crab/cfg/cfg.hpp"
#include "crab_llvm/MemAnalysis.hh"

namespace crab_llvm
{

  using namespace llvm;
  using namespace crab::cfg;

  //! Map llvm values to symbolic integer linear expressions
  template<typename VariableFactory, typename ZLinExp>
  class SymEval {

    VariableFactory& m_vfac;
    MemAnalysis& m_mem;
    tracked_precision m_tracklev;

   public:

    typedef typename VariableFactory::varname_t varname_t;

   public:

    SymEval (VariableFactory &vfac, MemAnalysis& mem, tracked_precision tracklev)
        : m_vfac (vfac), m_mem (mem), m_tracklev(tracklev) { }

    MemAnalysis& getMem () { return m_mem;}

    VariableFactory& getVarFac () { return m_vfac; }

    tracked_precision getTrackLevel () const { return m_tracklev;}

    bool isTracked (const llvm::Value &v) {

      // -- ignore any shadow variable created by seahorn
      if (v.getName().startswith ("shadow.mem"))
        return false;

      // -- a pointer
      if (v.getType ()->isPointerTy ())
        return (m_tracklev >= PTR);

      // -- always track integer registers
      return v.getType ()->isIntegerTy ();
    }

    varname_t symVar (const Value &v) {
      assert (isTracked (v));
      return m_vfac [v];
    }

    varname_t symVar (const region_t& r) {
      return m_vfac.get (r.get_id ());
    }

    bool isVar (ZLinExp e) {
      auto v = e.get_variable ();
      bool res = (v ? true: false);
      return res;
    }

    boost::optional<ZLinExp> lookup (const Value &v) {
      if (isa<const UndefValue> (&v)) {
        return boost::optional<ZLinExp> ();
      }

      if (isa<ConstantPointerNull> (&v)) {
        return ZLinExp (0);
      }

      if (const ConstantInt *c = dyn_cast<const ConstantInt> (&v)) {
        if (c->getType ()->isIntegerTy(1)) {
          return ZLinExp (c->getZExtValue ());
        }
        else if (c->getValue ().getMinSignedBits () <= 64) {
          return ZLinExp (c->getSExtValue ());
        }
        else {
          errs () << "Warning: "
                  <<  toMpz (c->getValue ()).get_str ()
                  << " does not fit in int64_t.\n";
        }
      }

      if (isTracked(v) && !isa<ConstantExpr> (v)) {
        return ZLinExp (symVar (v));
      }

      return boost::optional<ZLinExp> ();
    }

    /** Converts v to mpz_class. Assumes that v is signed */
    inline mpz_class toMpz (const APInt &v) {
      // Based on:
      // https://llvm.org/svn/llvm-project/polly/trunk/lib/Support/GICHelper.cpp
      // return v.getSExtValue ();

      APInt abs;
      abs = v.isNegative () ? v.abs () : v;

      const uint64_t *rawdata = abs.getRawData ();
      unsigned numWords = abs.getNumWords ();

      // TODO: Check if this is true for all platforms.
      mpz_class res;
      mpz_import(res.get_mpz_t (), numWords, 1,
                 sizeof (uint64_t), 0, 0, rawdata);

      return v.isNegative () ? mpz_class(-res) : res;
    }
  };
}

#endif


text:
<?php

namespace Pstk\Paystack\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\Order;

class ObserverBeforeSalesOrderPlace implements ObserverInterface
{

    public function __construct() {

    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        //Observer execution code...
        /** @var \Magento\Sales\Model\Order $order **/
        $order = $observer->getEvent()->getOrder();

        if ($order) {
            $order->setCanSendNewEmailFlag(false)
                    ->setCustomerNoteNotify(false);
        }
    }
}


text:
import netaddr
import json
import httplib

from neutron.db import l3_db
from neutron.db import models_v2
from neutron.openstack.common import log as logging
from neutron.plugins.ml2 import models as ml2_db
from neutron.plugins.ml2.drivers.fortinet.common import resources as resources
from neutron.plugins.ml2.drivers.fortinet.common import constants as const
from neutron.plugins.ml2.drivers.fortinet.tasks import constants as t_consts
from neutron.plugins.ml2.drivers.fortinet.db import models as fortinet_db
from neutron.plugins.ml2.drivers.fortinet.api_client import exception

LOG = logging.getLogger(__name__)

def add_record(obj, context, cls, **kwargs):
    res = cls.add_record(context, **kwargs)
    if not res:
        return res
    if res.get('rollback', {}):
        obj.task_manager.add(getid(context), **res['rollback'])
    return res.get('result', None)

def op(obj, context, func, **data):
    res = func(obj._driver, data)
    if res.get('rollback', {}):
        obj.task_manager.add(getid(context), **res['rollback'])
    return res.get('result', res)

def check(obj, context, vdom, resource=resources.VlanInterface):
    vlink_vlan = fortinet_db.query_record(context,
                     fortinet_db.Fortinet_Vlink_Vlan_Allocation, vdom=vdom)
    if vlink_vlan:
        try:
            op(obj, context, resource.get,
               vdom=vdom, name=vlink_vlan.inf_name_int_vdom)
            op(obj, context, resource.get,
               vdom=const.EXT_VDOM, name=vlink_vlan.inf_name_ext_vdom)
        except exception.ResourceNotFound as e:
            import inspect
            caller = inspect.stack()[1][3]
            LOG.debug(_("#### Check vlink interface failed on the %(func)s."),
            {'func': caller})
            resources.Exinfo(e)

def getid(context):
    id = getattr(context, 'request_id', None)
    if not id:
        raise ValueError("not get request_id")
    return id

def get_mac(obj, context, interface=None):
    if not interface:
        interface = obj._fortigate['int_interface']
    res = op(obj, context, resources.VlanInterface.get, name=interface)
    if httplib.OK == res['http_status']:
        return res['results'][0]['macaddr']
    return None

def getip(ipsubnet, place):
    return "%s %s" % (get_ipaddr(ipsubnet, place), get_netmask(ipsubnet))

def get_ipaddr(ip_subnet, place=1):
    return str(netaddr.IPNetwork(ip_subnet)[place])

def get_netmask(ip_subnet):
    return str(netaddr.IPNetwork(ip_subnet).netmask)

def get_segmentation_id(context, network_id):
    ml2_net_seg = fortinet_db.query_record(context, ml2_db.NetworkSegment,
                                           network_id=network_id)
    return getattr(ml2_net_seg, 'segmentation_id', None)

def get_intf(context, network_id):
    vlanid = get_segmentation_id(context, network_id=network_id)
    return const.PREFIX['inf'] + str(vlanid) if vlanid else None

def backup_fields(record, **kwargs):
    rbkwargs = {}
    for key in kwargs:
        if hasattr(record, key):
            rbkwargs.setdefault(key, record.key)
    return rbkwargs

def update_status(obj, context, status):
    obj.task_manager.update_status(getid(context), status)

def _rollback_on_err(obj, context, err):
    update_status(obj, context, t_consts.TaskStatus.ROLLBACK)
    resources.Exinfo(err)

def add_by_keys(obj, context, cls, resource, *keys, **kwargs):
    record = add_record(obj, context, cls, **kwargs)
    add_resource_with_keys(obj, context, record, resource, *keys, **kwargs)
    return record

def delete_by_keys(obj, context, cls, resource, *keys, **kwargs):
    record = fortinet_db.query_record(context, cls, **kwargs)
    delete_resource_with_keys(obj, context, record, resource, *keys)
    return fortinet_db.delete_record(context, cls, **kwargs)

def add_resource_with_keys(obj, context, record, resource, *keys, **kwargs):
    if record:
        try:
            params = {key: getattr(record, key, None) for key in keys
                                              if getattr(record, key, None)}
            op(obj, context, resource.get, **params)
        except exception.ResourceNotFound as e:
            op(obj, context, resource.add, **kwargs)

def delete_resource_with_keys(obj, context, record, resource, *keys):
    if record:
        try:
            params = {key: getattr(record, key, None) for key in keys
                                              if getattr(record, key, None)}
            op(obj, context, resource.get, **params)
            op(obj, context, resource.delete, **params)
        except exception.ResourceNotFound as e:
            resources.Exinfo(e)
            pass

def add_resource_with_name(obj, context, record, resource, **kwargs):
    return add_resource_with_keys(obj, context, record, resource,
                                  'vdom', 'name', **kwargs)

def delete_resource_with_name(obj, context, record, resource):
    return delete_resource_with_keys(obj, context, record, resource,
                                     'vdom', 'name')

def add_resource_with_id(obj, context, record, resource, **kwargs):
    if getattr(record, 'edit_id', None):
        try:
            res = op(obj, context, resource.get,
                     vdom=record.vdom, id=record.edit_id)
            return res
        except exception.ResourceNotFound:
            pass
    return op(obj, context, resource.add, **kwargs)

def delete_resource_with_id(obj, context, record, resource):
    if getattr(record, 'edit_id', None):
        try:
            op(obj, context, resource.get, vdom=record.vdom, id=record.edit_id)
            op(obj, context, resource.delete,
               vdom=record.vdom, id=record.edit_id)
            return True
        except Exception as e:
            resources.Exinfo(e)
    return False

def add_by_name(obj, context, cls, resource, **kwargs):
    return add_by_keys(obj, context, cls, resource, 'vdom', 'name', **kwargs)

def delete_by_name(obj, context, cls, resource, **kwargs):
    return delete_by_keys(obj, context, cls, resource,
                          'vdom', 'name', **kwargs)

def add_by_id(obj, context, cls, resource, **kwargs):
    record = add_record(obj, context, cls, **kwargs)
    res = add_resource_with_id(obj, context, record, resource, **kwargs)
    if not getattr(record, 'edit_id'):
        cls.update_record(context, record, edit_id=res['results']['mkey'])
    return record

def delete_by_id(obj, context, cls, resource, **kwargs):
    record = fortinet_db.query_record(context, cls, **kwargs)
    delete_resource_with_id(obj, context, record, resource)
    fortinet_db.delete_record(context, cls, **kwargs)

def add_vdom(obj, context, **kwargs):
    namespace = add_record(obj, context, fortinet_db.Fortinet_ML2_Namespace,
                           **kwargs)
    try:
        op(obj, context, resources.Vdom.get, name=namespace.vdom)
    except exception.ResourceNotFound:
        op(obj, context, resources.Vdom.add, name=namespace.vdom)
    return namespace

def delete_vdom(obj, context, **kwargs):
    cls = fortinet_db.Fortinet_ML2_Namespace
    namespace = fortinet_db.query_record(context, cls, **kwargs)
    if namespace:
        try:
            op(obj, context, resources.Vdom.get, name=namespace.vdom)
            op(obj, context, resources.Vdom.delete, name=namespace.vdom)
        except Exception as e:
            resources.Exinfo(e)
        fortinet_db.delete_record(context, cls, **kwargs)
    return namespace

def add_vdomlink(obj, context, **kwargs):
    cls = fortinet_db.Fortinet_Vdom_Vlink
    record = add_record(obj, context, cls, **kwargs)
    add_resource_with_keys(obj, context, record, resources.VdomLink,
                           'name', name=getattr(record, 'name', None))
    return record

def delete_vdomlink(obj, context, **kwargs):
    return delete_by_keys(obj, context, fortinet_db.Fortinet_Vdom_Vlink,
                          resources.VdomLink, 'name', **kwargs)

def add_vlanintf(obj, context, **kwargs):
    if kwargs.has_key('alias') and kwargs.get('alias', None):
        kwargs['alias'] = kwargs['alias'][:32]
    return add_by_name(obj, context,
                       fortinet_db.Fortinet_Interface,
                       resources.VlanInterface,
                       **kwargs)

def set_vlanintf(obj, context, **kwargs):
    cls = fortinet_db.Fortinet_Interface
    record = fortinet_db.query_record(context, cls, **kwargs)
    rbkwargs = backup_fields(record, **kwargs)
    cls.update_record(context, record, **kwargs)
    pass

def delete_vlanintf(obj, context, **kwargs):
    return delete_by_name(obj, context,
                          fortinet_db.Fortinet_Interface,
                          resources.VlanInterface,
                          **kwargs)

def add_dhcpserver(obj, context, **kwargs):
    cls = fortinet_db.Fortinet_ML2_Subnet
    record = add_record(obj, context, cls, **kwargs)
    kwargs.pop('subnet_id', None)
    res = add_resource_with_id(obj, context, record,
                               resources.DhcpServer, **kwargs)
    if not record.edit_id:
        cls.update_record(context, record, edit_id=res['results']['mkey'])

def delete_dhcpserver(obj, context, **kwargs):
    return delete_by_id(obj, context, fortinet_db.Fortinet_ML2_Subnet,
                        resources.DhcpServer, **kwargs)

def add_reservedip(obj, context, **kwargs):
    cls = fortinet_db.Fortinet_ML2_ReservedIP
    reserved_ip = add_record(obj, context, cls, **kwargs)
    db_reservedips = fortinet_db.query_records(context, cls,
                                        subnet_id=kwargs.get('subnet_id'))
    db_subnet = fortinet_db.query_record(context,
                                         fortinet_db.Fortinet_ML2_Subnet,
                                         subnet_id=kwargs.get('subnet_id'))
    if db_subnet:
        reserved_addresses = []
        for rsrvdip in db_reservedips:
            reserved_addresses.append({ 'id': rsrvdip.edit_id,
                                        'ip': rsrvdip.ip,
                                        'mac': rsrvdip.mac })

        op(obj, context, resources.DhcpServerRsvAddr.set,
           id=db_subnet.edit_id,
           vdom=kwargs.get('vdom'),
           reserved_address=json.dumps(reserved_addresses))
        # TODO: add rollback of dhcpserver set

def delete_reservedip(obj, context, **kwargs):
    cls = fortinet_db.Fortinet_ML2_ReservedIP
    reserved_ip = fortinet_db.query_record(context, cls, **kwargs)

    if reserved_ip:
        db_reservedips = fortinet_db.query_records(context, cls,
                                        subnet_id=reserved_ip.subnet_id)
        db_reservedips.remove(reserved_ip)
        reserved_addresses = []
        for rsrvdip in db_reservedips:
            reserved_addresses.append({ 'id': rsrvdip.edit_id,
                                        'ip': rsrvdip.ip,
                                        'mac': rsrvdip.mac })
        db_subnet = fortinet_db.query_record(context,
                                             fortinet_db.Fortinet_ML2_Subnet,
                                             subnet_id=reserved_ip.subnet_id)
        if db_subnet:
            op(obj, context, resources.DhcpServerRsvAddr.set,
               id=db_subnet.edit_id,
               vdom=reserved_ip.vdom,
               reserved_address=json.dumps(reserved_addresses))
        fortinet_db.delete_record(context, cls, **kwargs)

def add_fwaddress(obj, context, **kwargs):
    return add_by_name(obj, context,
                       fortinet_db.Fortinet_Firewall_Address,
                       resources.FirewallAddress,
                       **kwargs)

def delete_fwaddress(obj, context, **kwargs):
    return delete_by_name(obj, context,
                          fortinet_db.Fortinet_Firewall_Address,
                          resources.FirewallAddress,
                          **kwargs)

def add_fwippool(obj, context, **kwargs):
    return add_by_name(obj, context,
                       fortinet_db.Fortinet_Firewall_IPPool,
                       resources.FirewallIppool,
                       **kwargs)

def delete_fwippool(obj, context, **kwargs):
    return delete_by_name(obj, context,
                          fortinet_db.Fortinet_Firewall_IPPool,
                          resources.FirewallIppool,
                          **kwargs)

def add_fwpolicy(obj, context, **kwargs):
    return add_by_id(obj, context,
                     fortinet_db.Fortinet_Firewall_Policy,
                     resources.FirewallPolicy,
                     **kwargs)

def delete_fwpolicy(obj, context, **kwargs):
    return delete_by_id(obj, context,
                        fortinet_db.Fortinet_Firewall_Policy,
                        resources.FirewallPolicy,
                        **kwargs)

def delete_fwpolicies(obj, context, **kwargs):
    records = fortinet_db.query_records(context,
                                        fortinet_db.Fortinet_Firewall_Policy,
                                        **kwargs)
    for record in records:
        delete_by_id(obj, context, fortinet_db.Fortinet_Firewall_Policy,
                     resources.FirewallPolicy,
                     vdom=record.vdom, edit_id=record.edit_id)

def head_firewall_policy(obj, context, **kwargs):
    """
    :param obj:
    :param context:
    :param kwargs: {
        'vdom': osvdmxx,
        'id': 5
    }
    :return:
    """
    res = op(obj, context, resources.FirewallPolicy.get, vdom=kwargs['vdom'])
    if res.get('results'):
        head = res['results'][0]['policyid']
        op(obj, context, resources.FirewallPolicy.move, before=head, **kwargs)

def add_vip(obj, context, **kwargs):
    return add_by_name(obj, context,
                       fortinet_db.Fortinet_Firewall_VIP,
                       resources.FirewallVip,
                       **kwargs)

def delete_vip(obj, context, **kwargs):
    return delete_by_name(obj, context,
                          fortinet_db.Fortinet_Firewall_VIP,
                          resources.FirewallVip,
                          **kwargs)

def add_routerstatic(obj, context, **kwargs):
    cls = fortinet_db.Fortinet_Static_Router
    record = add_record(obj, context, cls, **kwargs)
    kwargs.pop('subnet_id', None)
    res = add_resource_with_id(obj, context, record,
                               resources.RouterStatic, **kwargs)
    if not getattr(record, 'edit_id'):
        cls.update_record(context, record, edit_id=res['results']['mkey'])
    return record

def delete_routerstatic(obj, context, **kwargs):
    return delete_by_id(obj, context,
                        fortinet_db.Fortinet_Static_Router,
                        resources.RouterStatic,
                        **kwargs)

def delete_routerstatics(obj, context, **kwargs):
    records = fortinet_db.query_records(context,
                                        fortinet_db.Fortinet_Static_Router,
                                        **kwargs)
    for record in records:
        delete_routerstatic(obj, context,
                            vdom=record.vdom,
                            edit_id=record.edit_id)

def add_addrgrp(obj, context, **kwargs):
    """
    :param context:
    :param kwargs:
     {
        "name": "addrgrp_osvdm1",
        "vdom": "osvdm1",
        "members": ["192.168.33.0"]
     }
    :return:
    """
    cls = fortinet_db.Fortinet_Firewall_Address
    records = fortinet_db.query_records(context, cls, group=kwargs['name'])
    for name in kwargs['members']:
        record = fortinet_db.query_record(context, cls,
                                          name=name, vdom=kwargs['vdom'])
        if not record.group:
            cls.update_record(context, record, group=kwargs['name'])
            # TODO: need to add a rollback action to taskmanager
        else:
            LOG.debug(_("The member %(record)s already joined a group"),
                      {"record": record})
    for record in records:
        kwargs['members'].append(record.name)
    try:
        op(obj, context, resources.FirewallAddrgrp.get,
           name=kwargs['name'], vdom=kwargs['vdom'])
        # TODO: need to add a rollback action to taskmanager
        op(obj, context, resources.FirewallAddrgrp.set, **kwargs)
    except exception.ResourceNotFound:
        op(obj, context, resources.FirewallAddrgrp.add, **kwargs)

def delete_addrgrp(obj, context, **kwargs):
    """
    :param context: for database
    :param kwargs:
        example format
        {
            "name": "addrgrp_osvdm1",
            "vdom": "osvdm1",
            "members": ["192.168.10.0", "192.168.33.0"]
        }
        each member of members is the address name to be deleted in
        the specific firewall address group in FGT.
    """
    cls = fortinet_db.Fortinet_Firewall_Address
    records = fortinet_db.query_records(context, cls, group=kwargs['name'])
    if not records:
        LOG.debug(_("There is not any record in db"))
        return

    members = [record.name for record in records
                           if record.name not in kwargs['members']]
    if members:
        kwargs['members'] = members
        op(obj, context, resources.FirewallAddrgrp.set, **kwargs)
    else:
        delete_fwpolicy(obj, context, vdom=kwargs.get('vdom'),
                        srcintf='any', srcaddr=kwargs['name'],
                        dstintf='any', nat='disable')
        try:
            del kwargs['members']
            op(obj, context, resources.FirewallAddrgrp.delete, **kwargs)
        except Exception as e:
            resources.Exinfo(e)
    for record in records:
        if record.name not in members:
            record.update_record(context, record, group=None)

def add_vlink_intf(obj, context, vlink_vlan, vlink_ip):
    vdom = getattr(vlink_vlan, 'vdom')
    ipsubnet = netaddr.IPNetwork(vlink_ip.vlink_ip_subnet)
    if obj._fortigate.get('npu_available'):
        intf_ext, intf_int = 'npu0_vlink0', 'npu0_vlink1'
        add_vlanintf(obj, context,
                     name=vlink_vlan.inf_name_ext_vdom,
                     vdom=const.EXT_VDOM,
                     vlanid=vlink_vlan.vlanid,
                     interface=intf_ext,
                     ip=getip(ipsubnet, 1))
        add_vlanintf(obj, context,
                     name=vlink_vlan.inf_name_int_vdom,
                     vdom=vdom,
                     vlanid=vlink_vlan.vlanid,
                     interface=intf_int,
                     ip=getip(ipsubnet, 2))
    else:
        add_vdomlink(obj, context, vdom=vdom)
        op(obj, context, resources.VlanInterface.set,
                     name=vlink_vlan.inf_name_ext_vdom,
                     vdom=const.EXT_VDOM,
                     ip=getip(ipsubnet, 1))
        op(obj, context, resources.VlanInterface.set,
                     name=vlink_vlan.inf_name_int_vdom,
                     vdom=vdom,
                     ip=getip(ipsubnet, 2))

def get_vlink_intf(obj, context, **kwargs):
    vlink_vlan = fortinet_db.query_record(context,
                        fortinet_db.Fortinet_Vlink_Vlan_Allocation,
                        **kwargs)
    if not vlink_vlan and kwargs.get('vdom', None):
        return add_vlink(obj, context, kwargs['vdom'])
    return (vlink_vlan.inf_name_int_vdom, vlink_vlan.inf_name_ext_vdom)

def delete_vlink_intf(obj, context, vlink_vlan):
    vdom = getattr(vlink_vlan, 'vdom')
    if obj._fortigate.get('npu_available'):
        delete_vlanintf(obj, context,
                        name=vlink_vlan.inf_name_int_vdom,
                        vdom=vdom)
        delete_vlanintf(obj, context,
                        name=vlink_vlan.inf_name_ext_vdom,
                        vdom=const.EXT_VDOM)
    else:
        delete_vdomlink(obj, context, vdom=vdom)

def add_vlink(obj, context, vdom):
    vlink_vlan = add_record(obj, context,
                        fortinet_db.Fortinet_Vlink_Vlan_Allocation,
                        vdom=vdom)
    vlink_ip = add_record(obj, context,
                        fortinet_db.Fortinet_Vlink_IP_Allocation,
                        vdom=vdom,
                        vlink_id=vlink_vlan.id)
    if vlink_ip:
        add_vlink_intf(obj, context, vlink_vlan, vlink_ip)
        add_routerstatic(obj, context,
                         vdom=vdom,
                         dst=const.EXT_DEF_DST,
                         device=vlink_vlan.inf_name_int_vdom,
                         gateway=const.DEF_GW)
        #add_fwpolicy(obj, context,
        #             vdom=const.EXT_VDOM,
        #             srcintf=vlink_vlan.inf_name_ext_vdom,
        #             dstintf=obj._fortigate['ext_interface'],
        #             nat='enable')
    return (vlink_vlan.inf_name_int_vdom, vlink_vlan.inf_name_ext_vdom)

def delete_vlink(obj, context, tenant_id):
    vdom = fortinet_db.query_record(context,
                                    fortinet_db.Fortinet_ML2_Namespace,
                                   tenant_id=tenant_id).vdom
    vlink_vlan = fortinet_db.query_record(context,
                            fortinet_db.Fortinet_Vlink_Vlan_Allocation,
                            vdom=vdom,
                            allocated=True)
    if not vlink_vlan:
        return False
    vlink_ip = fortinet_db.query_record(context,
                              fortinet_db.Fortinet_Vlink_IP_Allocation,
                              vdom=vdom,
                              vlink_id=vlink_vlan.id,
                              allocated=True)
    if not vlink_ip:
        return False
    """
    delete_fwpolicy(obj, context,
                    vdom=const.EXT_VDOM,
                    srcintf=vlink_vlan.inf_name_ext_vdom,
                    dstintf=obj._fortigate['ext_interface'],
                    nat='enable')"""
    delete_routerstatic(obj, context,
                        vdom=vdom,
                        dst=const.EXT_DEF_DST,
                        device=vlink_vlan.inf_name_int_vdom,
                        gateway=const.DEF_GW)
    delete_vlink_intf(obj, context, vlink_vlan)
    fortinet_db.delete_record(context,
                        fortinet_db.Fortinet_Vlink_IP_Allocation,
                        vdom=vdom,
                        vlink_id=vlink_vlan.id)
    fortinet_db.delete_record(context,
                        fortinet_db.Fortinet_Vlink_Vlan_Allocation,
                        id=vlink_vlan.id)
    return True

def add_interface_ip(obj, context, **kwargs):
    """
    :param context:
    :param kwargs: example format as below
        {
            "ip": "10.160.37.20 255.255.255.0",
            "name": "port37",
            "vdom": "root"
        }
    :return:
    """
    inf_db = fortinet_db.query_record(context,
                            fortinet_db.Fortinet_Interface,
                            name=kwargs.get('name'))
    if const.EXT_DEF_DST in getattr(inf_db, 'ip'):
        inf_db.update_record(context, inf_db, **kwargs)
        op(obj, context, resources.VlanInterface.set, **kwargs)
    else:
        records = fortinet_db.query_records(context,
                                  fortinet_db.Fortinet_Interface_subip,
                                  name=kwargs.get('name'))
        org_subips = [getattr(record, 'ip') for record in records]

        if kwargs.get('ip') in org_subips:
            return
        add_record(obj, context,
                   fortinet_db.Fortinet_Interface_subip, **kwargs)

        org_subips.append(kwargs.get('ip'))
        op(obj, context, resources.VlanInterface.set,
           name=kwargs.get('name'),
           vdom=kwargs.get('vdom'),
           secondaryips=org_subips)

def delete_interface_ip(obj, context, **kwargs):
    """
    :param context:
    :param kwargs: example format as below
        {
            "ip": "10.160.37.20 255.255.255.0",
            "name": "port37",
            "vdom": "root"
        }
    :return:
    """
    records = fortinet_db.query_records(context,
                                        fortinet_db.Fortinet_Interface_subip,
                                        name=kwargs.get('name'))
    org_subips = [getattr(record, 'ip') for record in records]
    if kwargs.get('ip') in org_subips:
        org_subips.remove(kwargs["ip"])
        op(obj, context, resources.VlanInterface.set,
           name=kwargs.get('name'),
           vdom=kwargs.get('vdom'),
           secondaryips=org_subips)
        fortinet_db.delete_record(context,
                                  fortinet_db.Fortinet_Interface_subip,
                                  **kwargs)
    else:
        inf_db = fortinet_db.query_record(context,
                                          fortinet_db.Fortinet_Interface,
                                          **kwargs)
        if not inf_db:
            return
        if org_subips:
            kwargs['ip'] = org_subips.pop()
            op(obj, context, resources.VlanInterface.set,
               name=kwargs.get('name'),
               vdom=kwargs.get('vdom'),
               secondaryips=org_subips)
            fortinet_db.delete_record(context,
                                  fortinet_db.Fortinet_Interface_subip,
                                  **kwargs)
        else:
            kwargs['ip'] = const.EXT_DEF_DST

        op(obj, context, resources.VlanInterface.set, **kwargs)
        inf_db.update_record(context, inf_db, ip=kwargs['ip'])

def add_secondaryip(obj, context, **kwargs):
    """
    :param obj:
    :param context:
    :param kwargs:
            'name': vl_ext_xx,
            'vdom': const.EXT_VDOM,
            'ip': 'x.x.x.x x.x.x.x'
    :return:
    """
    records = fortinet_db.query_records(context,
                                fortinet_db.Fortinet_FloatingIP_Allocation,
                                vdom=kwargs['vdom'],
                                allocated=True)
    secondaryips = []
    for record in records:
        secondaryips.append(getip(record.ip_subnet,1))

    if op(obj, context, resources.VlanInterface.set, name=kwargs['name'],
          vdom=const.EXT_VDOM, secondaryips=secondaryips):
        secondaryips.remove(kwargs['ip'])
        rollback = {
            'params': (
                obj._driver,
                {
                    'name': kwargs['name'],
                    'vdom': const.EXT_VDOM,
                    'secondaryips': secondaryips
                }
            ),
            'func': resources.VlanInterface.set
        }
        obj.task_manager.add(getid(context), **rollback)

def delete_secondaryip(obj, context, **kwargs):
    """
    :param obj:
    :param context:
    :param kwargs:
            'name': vl_ext_xx,
            'vdom': const.EXT_VDOM,
            'ip': 'x.x.x.x x.x.x.x'
    :return:
    """
    records = fortinet_db.query_records(context,
                                fortinet_db.Fortinet_FloatingIP_Allocation,
                                vdom=kwargs['vdom'],
                                allocated=True)
    secondaryips = []
    for record in records:
        secondaryip = getip(record.ip_subnet, 1)
        if secondaryip == kwargs.get('ip'):
            continue
        secondaryips.append(secondaryip)

    op(obj, context, resources.VlanInterface.set, name=kwargs['name'],
          vdom=kwargs['vdom'], secondaryips=secondaryips)

def set_ext_gw(obj, context, port):
    """
    :param context:
    :param port: example format
     port = {
        'status': 'DOWN',
        'binding:host_id': '',
        'allowed_address_pairs': [],
        'device_owner': 'network:router_gateway',
        'binding:profile': {},
        'fixed_ips': [{
            'subnet_id': u'09855a84-edfd-474d-b641-38a2bc63466a',
            'ip_address': u'10.160.37.111'
        }],
        'id': '6e68efc0-c0ca-40a2-a502-c2bf19304317',
        'security_groups': [],
        'device_id': u'8312d7a2-cae5-4e87-9c04-782c4a34bb8c',
        'name': '',
        'admin_state_up': True,
        'network_id': u'95eb736c-dd3b-4bf5-940a-8fa8e707a376',
        'tenant_id': '',
        'binding:vif_details': {},
        'binding:vnic_type': 'normal',
        'binding:vif_type': 'unbound',
        'mac_address': 'fa:16:3e:95:02:ab'
    }
    :return:
    """
    router_db = fortinet_db.query_record(context, l3_db.Router,
                                         id=port['device_id'])
    tenant_id = router_db.get('tenant_id', None)
    if not tenant_id:
        raise ValueError

    namespace = add_vdom(obj, context, tenant_id=tenant_id)
    add_vlink(obj, context, namespace.vdom)
    vlink_db = fortinet_db.query_record(context,
                                fortinet_db.Fortinet_Vlink_Vlan_Allocation,
                                vdom=namespace.vdom)

    ip_address = port['fixed_ips'][0]['ip_address']

    add_fwippool(obj, context, vdom=const.EXT_VDOM,
                 name=ip_address, startip=ip_address)

    add_fwpolicy(obj, context,
                 vdom=const.EXT_VDOM,
                 srcintf=vlink_db.inf_name_ext_vdom,
                 dstintf=obj._fortigate['ext_interface'],
                 poolname=ip_address)
    subnet_db = fortinet_db.query_record(context, models_v2.Subnet,
                                id=port['fixed_ips'][0]['subnet_id'])
    if subnet_db:
        netmask = netaddr.IPNetwork(subnet_db.cidr).netmask
        add_interface_ip(obj, context,
                         name=obj._fortigate['ext_interface'],
                         vdom=const.EXT_VDOM,
                         ip="%s %s" % (ip_address, netmask))

def clr_ext_gw(obj, context, port):
    ip_address = port['fixed_ips'][0]['ip_address']
    subnetv2_db = fortinet_db.query_record(context, models_v2.Subnet,
                                    id=port['fixed_ips'][0]['subnet_id'])
    netmask = netaddr.IPNetwork(subnetv2_db.cidr).netmask
    ip = "%s %s" % (ip_address, netmask)
    delete_interface_ip(obj, context,
                        name=obj._fortigate['ext_interface'],
                        vdom=const.EXT_VDOM,
                        ip=ip)
    delete_fwpolicy(obj, context, vdom=const.EXT_VDOM, poolname=ip_address)
    delete_fwippool(obj, context, vdom=const.EXT_VDOM, name=ip_address)
    router_db = fortinet_db.query_record(context, l3_db.Router,
                                         id=port['device_id'])
    tenant_id = router_db.get('tenant_id', None)
    if tenant_id:
        delete_vlink(obj, context, tenant_id=tenant_id)
        if not [ getattr(record, 'gw_port_id', None) for record in
                 fortinet_db.query_records(context, l3_db.Router,
                                           tenant_id=tenant_id)
                         if getattr(record, 'gw_port_id', None)] and \
           not fortinet_db.query_count(context, models_v2.Network,
                                       tenant_id=tenant_id):
            delete_vdom(obj, context, tenant_id=tenant_id)


text:
<?php

/**
 * Menu class
 *
 * @author Yoram de Langen <yoramdelangen@gmail.com>
 */
class Menu extends Corcel\Menu
{
}


text:
package io.crate.jobs;

import com.google.common.base.Throwables;
import io.crate.Streamer;
import io.crate.action.job.SharedShardContexts;
import io.crate.breaker.RamAccountingContext;
import io.crate.operation.PageDownstream;
import io.crate.operation.collect.JobCollectContext;
import io.crate.operation.collect.MapSideDataCollectOperation;
import io.crate.operation.collect.StatsTables;
import io.crate.operation.projectors.RowReceiver;
import io.crate.planner.node.dql.CollectPhase;
import io.crate.test.integration.CrateUnitTest;
import io.crate.types.IntegerType;
import org.elasticsearch.threadpool.ThreadPool;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.util.UUID;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.elasticsearch.test.ElasticsearchTestCase.assertBusy;
import static org.hamcrest.core.Is.is;
import static org.mockito.Mockito.*;

public class JobExecutionContextTest extends CrateUnitTest {

    @Rule
    public ExpectedException expectedException = ExpectedException.none();
    private ThreadPool threadPool;

    @Before
    public void before() throws Exception {
        threadPool = new org.elasticsearch.threadpool.ThreadPool("dummy");
    }

    @After
    public void after() throws Exception {
        threadPool.shutdown();
        threadPool.awaitTermination(500, TimeUnit.MILLISECONDS);
    }

    @Test
    public void testAddTheSameContextTwiceThrowsAnError() throws Exception {
        JobExecutionContext.Builder builder =
                new JobExecutionContext.Builder(UUID.randomUUID(), threadPool, mock(StatsTables.class));
        builder.addSubContext(new AbstractExecutionSubContextTest.TestingExecutionSubContext());
        expectedException.expect(IllegalArgumentException.class);
        expectedException.expectMessage("ExecutionSubContext for 0 already added");
        builder.addSubContext(new AbstractExecutionSubContextTest.TestingExecutionSubContext());
    }

    @Test
    public void testKillPropagatesToSubContexts() throws Exception {
        JobExecutionContext.Builder builder =
                new JobExecutionContext.Builder(UUID.randomUUID(), threadPool, mock(StatsTables.class));

        AbstractExecutionSubContextTest.TestingExecutionSubContext ctx1 = new AbstractExecutionSubContextTest.TestingExecutionSubContext(1);
        AbstractExecutionSubContextTest.TestingExecutionSubContext ctx2 = new AbstractExecutionSubContextTest.TestingExecutionSubContext(2);

        builder.addSubContext(ctx1);
        builder.addSubContext(ctx2);
        JobExecutionContext jobExecutionContext = builder.build();

        assertThat(jobExecutionContext.kill(), is(2L));
        assertThat(jobExecutionContext.kill(), is(0L)); // second call is ignored, only killed once

        assertThat(ctx1.numKill.get(), is(1));
        assertThat(ctx2.numKill.get(), is(1));
    }

    @Test
    public void testErrorMessageIsIncludedInStatsTableOnFailure() throws Exception {
        StatsTables statsTables = mock(StatsTables.class);
        JobExecutionContext.Builder builder =
                new JobExecutionContext.Builder(UUID.randomUUID(), threadPool, statsTables);

        SubExecutionContextFuture future = new SubExecutionContextFuture();
        ExecutionSubContext executionSubContext = mock(ExecutionSubContext.class);
        when(executionSubContext.future()).thenReturn(future);
        builder.addSubContext(executionSubContext);
        builder.build();

        future.close(new IllegalStateException("dummy"));

        verify(statsTables).operationFinished(anyInt(), eq("dummy"), anyLong());
    }

    @Test
    public void testFailureClosesAllSubContexts() throws Exception {
        JobExecutionContext.Builder builder =
                new JobExecutionContext.Builder(UUID.randomUUID(), threadPool, mock(StatsTables.class));

        JobCollectContext jobCollectContext = new JobCollectContext(
                mock(CollectPhase.class),
                mock(MapSideDataCollectOperation.class),
                mock(RamAccountingContext.class),
                mock(RowReceiver.class),
                mock(SharedShardContexts.class));
        PageDownstreamContext pageDownstreamContext = spy(new PageDownstreamContext(
                2, "dummy",
                mock(PageDownstream.class),
                new Streamer[]{IntegerType.INSTANCE.streamer()},
                mock(RamAccountingContext.class),
                1,
                null));

        builder.addSubContext(jobCollectContext);
        builder.addSubContext(pageDownstreamContext);
        JobExecutionContext jobExecutionContext = builder.build();

        Exception failure = new Exception("failure!");
        jobCollectContext.closeDueToFailure(failure);
        // other contexts must be killed with same failure
        verify(pageDownstreamContext, times(1)).innerKill(failure);

        final Field subContexts = JobExecutionContext.class.getDeclaredField("subContexts");
        subContexts.setAccessible(true);
        int size = ((ConcurrentMap<Integer, ExecutionSubContext>) subContexts.get(jobExecutionContext)).size();

        assertThat(size, is(0));
    }

    @Test
    public void testParallelKillReturnsJoined() throws Exception {
        final Field subContexts = JobExecutionContext.class.getDeclaredField("subContexts");
        subContexts.setAccessible(true);

        JobExecutionContext.Builder builder =
                new JobExecutionContext.Builder(UUID.randomUUID(), threadPool, mock(StatsTables.class));
        SlowKillExecutionSubContext slowKillExecutionSubContext = new SlowKillExecutionSubContext();
        builder.addSubContext(slowKillExecutionSubContext);
        final JobExecutionContext jobExecutionContext = builder.build();
        try {
            jobExecutionContext.start();
        } catch (Throwable throwable) {
            fail();
        }
        Thread killThread = new Thread(new Runnable() {
            @Override
            public void run() {
                jobExecutionContext.kill();
            }
        });
        killThread.start();

        // wait until kill is started
        final Field closed = JobExecutionContext.class.getDeclaredField("closed");
        closed.setAccessible(true);
        assertBusy(new Runnable() {
            @Override
            public void run() {
                try {
                    assertThat(((AtomicBoolean) closed.get(jobExecutionContext)).get(), is(true));
                } catch (Throwable t) {
                    throw Throwables.propagate(t);
                }
            }
        }, 100, TimeUnit.MILLISECONDS);

        // call kill again, because the first kill is still in progress nothing is done here, but this kill should
        // not return before every subContext is killed
        jobExecutionContext.kill();
        int size = ((ConcurrentMap<Integer, ExecutionSubContext>) subContexts.get(jobExecutionContext)).size();
        assertThat(size, is(0));

        killThread.join(500);
    }

    private static class SlowKillExecutionSubContext extends AbstractExecutionSubContext {

        public SlowKillExecutionSubContext() {
            super(1);
        }

        @Override
        public void innerKill(@Nonnull Throwable throwable) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw Throwables.propagate(e);
            }
        }

        @Override
        public String name() {
            return "SlowKilLExecutionSubContext";
        }
    }
}