text:
    #   Python Menu Code to call the various procedures to plot EQ data and forecasts
    #
    #   This code downloads data from various web sites and uses matplotlib to construct plots

import sys
import os
import numpy as np
from array import array

from EQUtilities import *

    #.....................................
    #
    # Read pre-defined locations file
    #
    #.....................................

input_file = open("locations.txt", "r")
i=0
for line in input_file:
    i +=  1
input_file.close()  # Put the file pointer back at top

number_locations = i

    # Create arrays of length i filled with zeros

Location_file   = ["" for x in range(i)]
MinLat_file     = np.zeros(i)
MaxLat_file     = np.zeros(i)
MinLng_file     = np.zeros(i)
MaxLng_file     = np.zeros(i)

input_file = open("locations.txt", "r")

i=-1
for line in input_file:
    i+=1
    line    = line.strip()
    items   = line.split(',')

    items_array = np.asarray(items)

    Location_file[i]   = items_array[0]
    MinLat_file[i]     = items_array[1]
    MaxLat_file[i]     = items_array[2]
    MinLng_file[i]     = items_array[3]
    MaxLng_file[i]     = items_array[4]

Location    = Location_file[0]
SWLat       = MinLat_file[0]
NELat       = MaxLat_file[0]
SWLng       = MinLng_file[0]
NELng       = MaxLng_file[0]

Last_location = Location

input_file.close()  # Put the file pointer back at top

    #.....................................

MagLo = 5.0 # Initial default minimum magnitude

    #.....................................

#   Assume initially that the local region is a circle

completeness_mag    =   2.99
Circle_Location     =   'None'
Circle_Lat          =   0.0
Circle_Lng          =   0.0
Radius_float        =   0.0
earthquake_depth    =   1000.0
region_type         =   'Circle'

settings_params     =   []
settings_params.append(region_type)
settings_params.append(completeness_mag)
settings_params.append(earthquake_depth)
settings_params.append(Circle_Location)
settings_params.append(Circle_Lat)
settings_params.append(Circle_Lng)
settings_params.append(Radius_float)

save_settings(settings_params)

settings_params = get_settings()

    #.....................................

#   Set the intial values of completeness_mag and Circle_Location here
#   Write these to a file "current_settings.txt"

    #.....................................
print ' '
print 'Downloading default data set'
os.system("python generate_catalog_file.py {0} {1} {2} {3} {4} {5}".format(NELat, NELng, SWLat, SWLng, MagLo, Location))

    #.....................................

rmenu =  ''
print ' '
print ' This is the basic code to set up the menu of choices'

while rmenu != 'Z':
    print ' '
    print '     ...........................................'
    print ' '
    print '     Current location is: ', Location
    print ' '
    print '     A: Input Location and Catalog Parameters'
    print '     B: Generate or Re-Generate Working Catalog File'
    print '     C: Edit Working Catalog File (for Macs Only)'
    print '     D: Plot EQ Magnitude vs. Time'
    print '     E: Plot EQ Epicenters on a Map'
    print '     F: Plot NTW Earthquake Forecast on a Map'
    print '     G: Plot Gutenberg-Richter Relation for an Inter-Earthquake Sequence'
    print '     H: Plot Earthquake NowCast for Regional Area'
    print '     I: Plot Earthquake NowCast for Circle within Regional Area'
    print '     J: Plot Earthquake NowCast for Polygon within Regional Area'
    print '     K: Plot Proxy Strain vs. Time in the Region'
    print '     L: Plot Proxy Strain vs. Time in the Circle'
#    print '     M: Plot Earthquake Forecast vs. Time in the Circle'
#    print '     N: Plot Filtered Earthquake NowCast for Regional Area (Same as H but Log10-Linear)'
    print ' '
    print '     Z: Get Me Out of Here!'
    print ' '
    print '     (You must choose ... but choose wisely!'
    print '     For a true choice will bring you joy,'
    print '     but a poor choice will take it from you...)'
    print ' '
    rmenu = ''
    rmenu = raw_input("Enter a Choice: \n")
    print ' '

    #.....................................

    if rmenu == 'B':
        print 'Choice B: Generate or Re-Generate Working Catalog File'
        os.system("python generate_catalog_file.py {0} {1} {2} {3} {4} {5}".format(NELat, NELng, SWLat, SWLng, MagLo, Location))

    if rmenu == 'C':
        print 'Choice C: Open Working Catalog File in Text Editor'
#       os.system("open -t EQ_Working_Catalog.txt") # Only for Macs.  For other systems, substitute something like "gedit EQ_Working_Catalog.txt"
        os.system("open -e EQ_Working_Catalog.txt") # Forces file open with Mac app Textedit

    if rmenu == 'D':
        print 'Choice D: Plot EQ Magnitude vs. Time'
        os.system("python plot_ANSS_seismicity.py {0} {1} {2} {3} {4} {5}".format(NELat, NELng, SWLat, SWLng, MagLo, Location))

    if rmenu == 'E':
        print 'Choice E: Plot EQ Epicenters on a Map'
        os.system("python plot_ANSS_epicenters.py {0} {1} {2} {3} {4} {5}".format(NELat, NELng, SWLat, SWLng, MagLo, Location))

    if rmenu == 'F':
        print 'Choice F: Plot NTW EQ Forecast on a Map'
        os.system("python contour_eq_probs.py {0} {1} {2} {3} {4} {5}".format(NELat, NELng, SWLat, SWLng, MagLo, Location))

    if rmenu == 'G':
        print 'Choice G: Plot Gutenberg-Richter relation (Cumulative frequency-magnitude)'
        os.system("python plot_GR_relation.py {0} {1} {2} {3} {4} {5}".format(NELat, NELng, SWLat, SWLng, MagLo, Location))

    if rmenu == 'H':
        print 'Choice H: Plot Earthquake EPS in Region (Defined by histogram of small earthquake counts)'
        os.system("python plot_EQ_EPS_Region.py {0} {1} {2} {3} {4} {5}".format(NELat, NELng, SWLat, SWLng, MagLo, Location))

    if rmenu == 'I':
        print 'Choice I: Plot Earthquake EPS in Circle within Region (Defined by histogram of small earthquake counts)'
        os.system("python plot_EQ_EPS_Region_Circle.py {0} {1} {2} {3} {4} {5}".format(NELat, NELng, SWLat, SWLng, MagLo, Location))

    if rmenu == 'J':
        print 'Choice J: Plot Earthquake EPS in Polygon within Region (Defined by histogram of small earthquake counts)'
        os.system("python plot_EQ_EPS_Region_Polygon.py {0} {1} {2} {3} {4} {5}".format(NELat, NELng, SWLat, SWLng, MagLo, Location))

    if rmenu == 'K':
        print 'Choice K: Plot Proxy Strain vs. Time in the Region'
        os.system("python plot_proxy_strain_vs_time_region.py {0} {1} {2} {3} {4} {5}".format(NELat, NELng, SWLat, SWLng, MagLo, Location))

    if rmenu == 'L':
        print 'Choice L: Plot Proxy Strain vs. Time in the Circle'
        os.system("python plot_proxy_strain_vs_time_circle.py {0} {1} {2} {3} {4} {5}".format(NELat, NELng, SWLat, SWLng, MagLo, Location))

    if rmenu == 'M':
        print 'Choice M: Plot Forecast in Circle within Region (Defined by histogram of small earthquake counts)'
        os.system("python plot_Forecast_EPS_Region_Circle.py {0} {1} {2} {3} {4} {5}".format(NELat, NELng, SWLat, SWLng, MagLo, Location))

    if rmenu == 'N':
        print 'Choice N: Plot Filtered Earthquake EPS in Region (Defined by histogram of small earthquake counts)'
        os.system("python plot_Filtered_EQ_EPS_Region.py {0} {1} {2} {3} {4} {5}".format(NELat, NELng, SWLat, SWLng, MagLo, Location))

    if rmenu == 'A':
        print 'Choice A: Enter the map and plot parameters (Default is California)'
        print ' '
        print ' Enter a different predefined location or large earthquake magnitude? (y/n)'
        respl = raw_input()
        if respl == 'y':
    #......................................

            print ' '
            print '     Current pre-defined Locations are: '
            print ' '
            for j in range(number_locations):
                print '        ', Location_file[j]

    #......................................

            print ' '
            print ' Current location is: ', Location
            print ' Enter new location (Case sensitive: Overrides previous parameter set):'
            Location = raw_input()

            location_flag = 0
            for j in range(number_locations):
                if Location == Location_file[j]:
                    SWLat = MinLat_file[j]
                    NELat = MaxLat_file[j]
                    SWLng = MinLng_file[j]
                    NELng = MaxLng_file[j]
                    location_flag = 1
                    Last_location = Location

            if location_flag == 0:
                print ' '
                print '     Invalid location, try again...'
                Location = Last_location

        print ' '
        print ' Minimum magnitude (for large earthquakes) is currently set at: ', MagLo
        print ' Enter new minimum magnitude? (y/n)'
        respm = raw_input()
        if respm == 'y':
            print ' '
            print ' Current minimum magnitude is: ', MagLo
            print ' Enter new minimum magnitude (must be M>5.0):'
            MagLo = raw_input()

        if respl !='y':
            print ' '
            print ' Enter parameters (Lats, Longs, Location Name?) (y/n)'
            respp = raw_input()
            if respp == 'y':
                print ' '
                print ' Enter Min Lat, Max Lat, Min Long, Max Long, Location Name'
                print '>>>>>>   (Requires 5 parameters separated by commas)'
                items = raw_input().split(',')
                print ' '
                print ' You Entered: ', items
                print ' '
                SWLat       = items[0]
                NELat       = items[1]
                SWLng       = items[2]
                NELng       = items[3]
                Location    = items[4]

        print 'Downloading catalog data for new location and/or with new minimum magnitude'
        os.system("python generate_catalog_file.py {0} {1} {2} {3} {4} {5}".format(NELat, NELng, SWLat, SWLng, MagLo, Location))


text:
import React, { PropTypes } from 'react';
import classNames from 'classnames';
import Button from './Button';
import Icon from './Icon';

class IconButton extends React.Component {
    static propTypes = {
        className: PropTypes.string,
        name: PropTypes.string.isRequired
    }

    render() {
        var { className, name, ...otherProps } = this.props;

        var classes = classNames('mdl-button--icon', className);

        return (
            <Button className={classes} {...otherProps}>
                <Icon name={name} />
            </Button>
        );
    }
}

export default IconButton;

redpajama_set_name: RedPajamaGithub


text:
using Abot2.Core;
using Abot2.Poco;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Http;
using System.Text;

namespace Abot2.Tests.Unit.Core
{
    [TestClass]
    public class CrawlDecisionMakerTest
    {
        CrawlDecisionMaker _unitUnderTest;
        CrawlContext _crawlContext;
        CrawledPage _validCrawledPage;
        Mock<IScheduler> _fakeScheduler;

        [TestInitialize]
        public void SetUp()
        {
            _fakeScheduler = new Mock<IScheduler>();
            _crawlContext = new CrawlContext();
            _crawlContext.CrawlConfiguration = new CrawlConfiguration { UserAgentString = "aaa" };
            _crawlContext.Scheduler = _fakeScheduler.Object;
            _unitUnderTest = new CrawlDecisionMaker();

            _validCrawledPage = GetValidCrawledPage();
        }

        [TestMethod]
        public void ShouldCrawlPage_NullPageToCrawl_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldCrawlPage(null, _crawlContext);

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("Null page to crawl", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPage_NullCrawlContext_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldCrawlPage(new PageToCrawl(new Uri("http://a.com/")), null);

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("Null crawl context", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPage_NonDuplicate_ReturnsTrue()
        {
            _crawlContext.CrawlStartDate = DateTime.Now;

            var result = _unitUnderTest.ShouldCrawlPage(
                new PageToCrawl(new Uri("http://a.com/"))
                {
                    IsInternal = true
                },
                _crawlContext);

            Assert.IsTrue(result.Allow);
            Assert.AreEqual("", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);

        }

        [TestMethod]
        public void ShouldCrawlPage_NonHttpOrHttpsSchemes_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldCrawlPage(new PageToCrawl(new Uri("file:///C:/Users/")), _crawlContext);
            Assert.IsFalse(result.Allow);
            Assert.AreEqual("Scheme does not begin with http", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);

            result = _unitUnderTest.ShouldCrawlPage(new PageToCrawl(new Uri("mailto:user@yourdomainname.com")), _crawlContext);
            Assert.IsFalse(result.Allow);
            Assert.AreEqual("Scheme does not begin with http", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);

            result = _unitUnderTest.ShouldCrawlPage(new PageToCrawl(new Uri("ftp://user@yourdomainname.com")), _crawlContext);
            Assert.IsFalse(result.Allow);
            Assert.AreEqual("Scheme does not begin with http", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);

            result = _unitUnderTest.ShouldCrawlPage(new PageToCrawl(new Uri("callto:+1234567")), _crawlContext);
            Assert.IsFalse(result.Allow);
            Assert.AreEqual("Scheme does not begin with http", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);

            result = _unitUnderTest.ShouldCrawlPage(new PageToCrawl(new Uri("tel:+1234567")), _crawlContext);
            Assert.IsFalse(result.Allow);
            Assert.AreEqual("Scheme does not begin with http", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPage_OverMaxPageToCrawlLimit_ReturnsFalse()
        {
            _crawlContext.CrawlConfiguration.MaxPagesToCrawl = 100;
            _crawlContext.CrawledCount = 100;

            var result = _unitUnderTest.ShouldCrawlPage(new PageToCrawl(new Uri("http://a.com/b")) { IsInternal = true }, _crawlContext);

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("MaxPagesToCrawl limit of [100] has been reached", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPage_OverMaxPageToCrawlLimitByScheduler_ReturnsFalse()
        {
            _crawlContext.CrawlConfiguration.MaxPagesToCrawl = 100;
            _crawlContext.CrawledCount = 1;

            _fakeScheduler.Setup(f => f.Count).Returns(100);

            var result = _unitUnderTest.ShouldCrawlPage(new PageToCrawl(new Uri("http://a.com/b")) { IsInternal = true }, _crawlContext);

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("MaxPagesToCrawl limit of [100] has been reached", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPage_ZeroMaxPageToCrawlLimit_ReturnsTrue()
        {
            var crawlContext = new CrawlContext
            {
                CrawlConfiguration = new CrawlConfiguration
                {
                    MaxPagesToCrawl = 0
                },
                CrawledCount = 100
            };

            var result = _unitUnderTest.ShouldCrawlPage(new PageToCrawl(new Uri("http://a.com/")) { IsInternal = true }, crawlContext);

            Assert.IsTrue(result.Allow);
        }

        [TestMethod]
        public void ShouldCrawlPage_IsExternalPageCrawlingEnabledFalse_PageIsExternal_ReturnsFalse()
        {
            _crawlContext.CrawlStartDate = DateTime.Now.AddSeconds(-100);
            _crawlContext.CrawlConfiguration.CrawlTimeoutSeconds = 0;
            var result = _unitUnderTest.ShouldCrawlPage(
                new PageToCrawl(new Uri("http://a.com/"))
                {
                    IsInternal = false
                },
                _crawlContext);

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("Link is external", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPage_IsExternalPageCrawlingEnabledTrue_PageIsExternal_ReturnsTrue()
        {
            _crawlContext.CrawlConfiguration.IsExternalPageCrawlingEnabled = true;
            _crawlContext.CrawlStartDate = DateTime.Now;

            var result = _unitUnderTest.ShouldCrawlPage(
                new PageToCrawl(new Uri("http://a.com/"))
                {
                    IsInternal = false
                },
                _crawlContext);

            Assert.IsTrue(result.Allow);
            Assert.AreEqual("", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPage_IsExternalPageCrawlingEnabledFalse_PageIsInternal_ReturnsTrue()
        {
            _crawlContext.CrawlConfiguration.IsExternalPageCrawlingEnabled = false;
            _crawlContext.CrawlStartDate = DateTime.Now;

            var result = _unitUnderTest.ShouldCrawlPage(
                new PageToCrawl(new Uri("http://a.com/"))
                {
                    IsInternal = true
                },
                _crawlContext);

            Assert.IsTrue(result.Allow);
            Assert.AreEqual("", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPage_IsExternalPageCrawlingEnabledTrue_PageIsInternal_ReturnsTrue()
        {
            _crawlContext.CrawlConfiguration.IsExternalPageCrawlingEnabled = true;
            _crawlContext.CrawlStartDate = DateTime.Now;

            var result = _unitUnderTest.ShouldCrawlPage(
                new PageToCrawl(new Uri("http://a.com/"))
                {
                    IsInternal = true
                },
                _crawlContext);

            Assert.IsTrue(result.Allow);
            Assert.AreEqual("", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPage_OverMaxPagesToCrawlPerDomain_ReturnsFalse()
        {
            var uri = new Uri("http://a.com/");

            var countByDomain = new ConcurrentDictionary<string,int>();
            countByDomain.TryAdd(uri.Authority, 100);

            _crawlContext.CrawlStartDate = DateTime.Now;
            _crawlContext.CrawlCountByDomain = countByDomain;
            _crawlContext.CrawlConfiguration.MaxPagesToCrawlPerDomain = 100;

            var result = _unitUnderTest.ShouldCrawlPage(
                new PageToCrawl(new Uri(uri.AbsoluteUri + "anotherpage"))
                {
                    IsInternal = true
                },
                _crawlContext);

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("MaxPagesToCrawlPerDomain limit of [100] has been reached for domain [a.com]", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPage_OverMaxPagesToCrawlPerDomain_IsRetry_ReturnsTrue()
        {
            var uri = new Uri("http://a.com/");
            var config = new CrawlConfiguration
            {
                MaxPagesToCrawlPerDomain = 100
            };
            var countByDomain = new ConcurrentDictionary<string, int>();
            countByDomain.TryAdd(uri.Authority, 100);
            var crawlContext = new CrawlContext
            {
                CrawlConfiguration = config,
                CrawlStartDate = DateTime.Now,
                CrawlCountByDomain = countByDomain
            };

            var result = _unitUnderTest.ShouldCrawlPage(
                new PageToCrawl(new Uri(uri.AbsoluteUri + "anotherpage"))
                {
                    IsRetry = true,
                    IsInternal = true
                },
                crawlContext);

            Assert.IsTrue(result.Allow);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPage_OverMaxCrawlDepth_ReturnsFalse()
        {
            var crawlContext = new CrawlContext
            {
                CrawlConfiguration = new CrawlConfiguration
                {
                    MaxCrawlDepth = 2
                }
            };

            var result = _unitUnderTest.ShouldCrawlPage(
                new PageToCrawl(new Uri("http://a.com/"))
                {
                    IsInternal = true,
                    CrawlDepth = 3
                },
                crawlContext);

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("Crawl depth is above max", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPage_EqualToMaxCrawlDepth_ReturnsTrue()
        {
            _crawlContext.CrawlConfiguration.MaxCrawlDepth = 2;

            var result = _unitUnderTest.ShouldCrawlPage(
                new PageToCrawl(new Uri("http://a.com/"))
                {
                    IsInternal = true,
                    CrawlDepth = 2
                },
                _crawlContext);

            Assert.IsTrue(result.Allow);
            Assert.AreEqual("", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPage_RedirectChainOverMax_ReturnsFalse()
        {
            _crawlContext.CrawlConfiguration.HttpRequestMaxAutoRedirects = 7;

            var result = _unitUnderTest.ShouldCrawlPage(
                new PageToCrawl(new Uri("http://a.com/"))
                {
                    IsInternal = true,
                    RedirectedFrom = new CrawledPage(new Uri("http://Doesntmatter.com")),
                    RedirectPosition = 8
                },
                _crawlContext);

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("HttpRequestMaxAutoRedirects limit of [7] has been reached", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPage_RedirectChainUnderMax_ReturnsTrue()
        {
            _crawlContext.CrawlConfiguration.HttpRequestMaxAutoRedirects = 7;

            var result = _unitUnderTest.ShouldCrawlPage(
                new PageToCrawl(new Uri("http://a.com/"))
                {
                    IsInternal = true,
                    RedirectedFrom = new CrawledPage(new Uri("http://Doesntmatter.com")),
                    RedirectPosition = 6
                },
                _crawlContext);

            Assert.IsTrue(result.Allow);
            Assert.AreEqual("", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPage_RedirectChainEqualToMax_ReturnsTrue()
        {
            _crawlContext.CrawlConfiguration.HttpRequestMaxAutoRedirects = 7;

            var result = _unitUnderTest.ShouldCrawlPage(
                new PageToCrawl(new Uri("http://a.com/"))
                {
                    IsInternal = true,
                    RedirectedFrom = new CrawledPage(new Uri("http://Doesntmatter.com")),
                    RedirectPosition = 7
                },
                _crawlContext);

            Assert.IsTrue(result.Allow);
            Assert.AreEqual("", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPageLinks_NullCrawledPage_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldCrawlPageLinks(null, new CrawlContext());

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("Null crawled page", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPageLinks_NullCrawlContext_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldCrawlPageLinks(_validCrawledPage, null);

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("Null crawl context", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPageLinks_NullHtmlContent_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldCrawlPageLinks(new CrawledPage(new Uri("http://a.com/"))
                {
                    Content = new PageContent
                    {
                        Text = null
                    }
                }, new CrawlContext());

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("Page has no content", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPageLinks_WhitespaceHtmlContent_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldCrawlPageLinks(new CrawledPage(new Uri("http://a.com/"))
                {
                    Content = new PageContent
                    {
                        Text = "     "
                    }
                }, new CrawlContext());

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("Page has no content", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPageLinks_EmptyHtmlContent_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldCrawlPageLinks(new CrawledPage(new Uri("http://a.com/"))
                {
                    Content = new PageContent
                    {
                        Text = ""
                    }
                }, new CrawlContext());

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("Page has no content", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPageLinks_IsExternalPageLinksCrawlingEnabledFalse_ExternalLink_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldCrawlPageLinks(
                new CrawledPage(new Uri("http://b.com/a.html"))
                {
                    Content = new PageContent
                    {
                        Text = "aaaa"
                    },
                    IsInternal = false
                },
                new CrawlContext
                {
                    RootUri = new Uri("http://a.com/ "),
                    CrawlConfiguration = new CrawlConfiguration
                    {
                        IsExternalPageLinksCrawlingEnabled = false
                    }
                });
            Assert.AreEqual(false, result.Allow);
            Assert.AreEqual("Link is external", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPageLinks_IsExternalPageLinksCrawlingEnabledTrue_InternalLink_ReturnsTrue()
        {
            var result = _unitUnderTest.ShouldCrawlPageLinks(
                new CrawledPage(new Uri("http://b.com/a.html"))
                {
                    Content = new PageContent
                    {
                        Text = "aaaa"
                    },
                    IsInternal = true
                },
                new CrawlContext
                {
                    RootUri = new Uri("http://a.com/ "),
                    CrawlConfiguration = new CrawlConfiguration
                    {
                        IsExternalPageLinksCrawlingEnabled = true
                    }
                });
            Assert.AreEqual(true, result.Allow);
            Assert.AreEqual("", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPageLinks_IsExternalPageLinksCrawlingEnabledFalse_InternalLink_ReturnsTrue()
        {
            var result = _unitUnderTest.ShouldCrawlPageLinks(
                new CrawledPage(new Uri("http://b.com/a.html"))
                {
                    Content = new PageContent
                    {
                        Text = "aaaa"
                    },
                    IsInternal = true
                },
                new CrawlContext
                {
                    RootUri = new Uri("http://a.com/ "),
                    CrawlConfiguration = new CrawlConfiguration
                    {
                        IsExternalPageLinksCrawlingEnabled = false
                    }
                });
            Assert.AreEqual(true, result.Allow);
            Assert.AreEqual("", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPageLinks_IsEqualToMaxCrawlDepth_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldCrawlPageLinks(
                new CrawledPage(new Uri("http://b.com/a.html"))
                {
                    Content = new PageContent
                    {
                        Text = "aaaa"
                    },
                    IsInternal = true,
                    CrawlDepth = 2
                },
                new CrawlContext
                {
                    RootUri = new Uri("http://a.com/ "),
                    CrawlConfiguration = new CrawlConfiguration
                    {
                        MaxCrawlDepth = 2
                    }
                });
            Assert.AreEqual(false, result.Allow);
            Assert.AreEqual("Crawl depth is above max", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldCrawlPageLinks_IsAboveMaxCrawlDepth_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldCrawlPageLinks(
                new CrawledPage(new Uri("http://b.com/a.html"))
                {
                    Content = new PageContent
                    {
                        Text = "aaaa"
                    },
                    IsInternal = true,
                    CrawlDepth = 3
                },
                new CrawlContext
                {
                    RootUri = new Uri("http://a.com/ "),
                    CrawlConfiguration = new CrawlConfiguration
                    {
                        MaxCrawlDepth = 2
                    }
                });
            Assert.AreEqual(false, result.Allow);
            Assert.AreEqual("Crawl depth is above max", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldDownloadPageContent_DownloadablePage_ReturnsTrue()
        {
            var result = _unitUnderTest.ShouldDownloadPageContent(_validCrawledPage, _crawlContext);

            Assert.AreEqual(true, result.Allow);
            Assert.AreEqual("", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldDownloadPageContent_NullCrawledPage_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldDownloadPageContent(null, new CrawlContext());
            Assert.AreEqual(false, result.Allow);
            Assert.AreEqual("Null crawled page", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldDownloadPageContent_NullCrawlContext_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldDownloadPageContent(new CrawledPage(new Uri("http://a.com/a.html")), null);

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("Null crawl context", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldDownloadPageContent_NullHttpWebResponse_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldDownloadPageContent(
                new CrawledPage(new Uri("http://b.com/a.html"))
                {
                    HttpResponseMessage = null
                },
                new CrawlContext
                {
                    RootUri = new Uri("http://a.com/ ")
                });
            Assert.AreEqual(false, result.Allow);
            Assert.AreEqual("Null HttpWebResponse", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldDownloadPageContent_HttpStatusNon200_ReturnsFalse()
        {
            _validCrawledPage.HttpResponseMessage.StatusCode = HttpStatusCode.Forbidden;

            var result = _unitUnderTest.ShouldDownloadPageContent(_validCrawledPage, _crawlContext);

            Assert.AreEqual(false, result.Allow);
            Assert.AreEqual("HttpStatusCode is not 200", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldDownloadPageContent_NonHtmlPage_ReturnsFalse()
        {
            _validCrawledPage.HttpResponseMessage.Content = new StringContent("aaa", Encoding.UTF8, "image/jpg");

            var result = _unitUnderTest.ShouldDownloadPageContent(_validCrawledPage, _crawlContext);

            Assert.AreEqual(false, result.Allow);
            Assert.AreEqual("Content type is not any of the following: text/html", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldDownloadPageContent_NonHtmlPage_DownloadableContentTypesWithSpaces_ReturnsFalse()
        {
            _validCrawledPage.HttpResponseMessage.Content = new StringContent("aaa", Encoding.UTF8, "image/jpg");

            _crawlContext.CrawlConfiguration.DownloadableContentTypes = "text/hmtl, ,    , application/pdf";
            var result = _unitUnderTest.ShouldDownloadPageContent(_validCrawledPage, _crawlContext);

            Assert.AreEqual(false, result.Allow);
            Assert.AreEqual("Content type is not any of the following: text/hmtl,application/pdf", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldDownloadPageContent_DownloadableContenttypesWithSpaces_TrimsSpaces_ReturnsTrue()
        {
            var crawlConfiguration = new CrawlConfiguration
            {
                UserAgentString = "aaa",
                DownloadableContentTypes = "text/html  , application/pdf, ,somethingelse"
            };
            _crawlContext.CrawlConfiguration = crawlConfiguration;

            var result = _unitUnderTest.ShouldDownloadPageContent(_validCrawledPage, _crawlContext);

            Assert.AreEqual(true, result.Allow);
            Assert.AreEqual("", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldDownloadPageContent_AboveMaxPageSize_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldDownloadPageContent(_validCrawledPage,
                new CrawlContext
                {
                    CrawlConfiguration = new CrawlConfiguration
                    {
                        MaxPageSizeInBytes = 5
                    }
                });

            Assert.IsFalse(result.Allow);
            //Assert.AreEqual("Page size of [1298] bytes is above the max allowable of [5] bytes", result.Reason); //This fluctuates depending on the .net version runtime installed
            Assert.IsTrue(result.Reason.StartsWith("Page size of ["));
            Assert.IsTrue(result.Reason.EndsWith("] bytes is above the max allowable of [5] bytes"));
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldDownloadPageContent_MaxPageSizeInBytesZero_ReturnsTrue()
        {
            var result = _unitUnderTest.ShouldDownloadPageContent(_validCrawledPage,
                new CrawlContext
                {
                    CrawlConfiguration = new CrawlConfiguration
                    {
                        MaxPageSizeInBytes = 0
                    }
                });

            Assert.IsTrue(result.Allow);
            Assert.AreEqual("", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldRecrawlPage_RetryablePage_ReturnsTrue()
        {
            _validCrawledPage.HttpRequestException = new HttpRequestException("Oh no");
            _validCrawledPage.RetryCount = 1;
            _crawlContext.CrawlConfiguration.MaxRetryCount = 5;

            var result = _unitUnderTest.ShouldRecrawlPage(_validCrawledPage, _crawlContext);

            Assert.IsTrue(result.Allow);
            Assert.AreEqual("", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);

        }

        [TestMethod]
        public void ShouldRecrawlPage_NullPageToCrawl_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldRecrawlPage(null, _crawlContext);

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("Null crawled page", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldRecrawlPage_NullCrawlContext_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldRecrawlPage(new CrawledPage(new Uri("http://a.com/")), null);

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("Null crawl context", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);
        }

        [TestMethod]
        public void ShouldRecrawlPage_NullWebException_ReturnsFalse()
        {
            var result = _unitUnderTest.ShouldRecrawlPage(_validCrawledPage, _crawlContext);

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("WebException did not occur", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);

        }

        [TestMethod]
        public void ShouldRecrawlPage_MaxRetryCountBelow1_ReturnsFalse()
        {
            _crawlContext.CrawlConfiguration.MaxRetryCount = 0;
            _validCrawledPage.HttpRequestException = new HttpRequestException("something bad");
            var result = _unitUnderTest.ShouldRecrawlPage(_validCrawledPage, _crawlContext);

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("MaxRetryCount is less than 1", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);

        }

        [TestMethod]
        public void ShouldRecrawlPage_MaxRetryCountBelowAboveMax_ReturnsFalse()
        {
            _crawlContext.CrawlConfiguration.MaxRetryCount = 5;
            _validCrawledPage.HttpRequestException = new HttpRequestException("something bad");
            _validCrawledPage.RetryCount = 5;

            var result = _unitUnderTest.ShouldRecrawlPage(_validCrawledPage, _crawlContext);

            Assert.IsFalse(result.Allow);
            Assert.AreEqual("MaxRetryCount has been reached", result.Reason);
            Assert.IsFalse(result.ShouldHardStopCrawl);
            Assert.IsFalse(result.ShouldStopCrawl);

        }

        private CrawledPage GetValidCrawledPage()
        {
            var valid200StatusUri = new Uri("http://localhost.fiddler:1111/");
            var crawledPage = new CrawledPage(valid200StatusUri)
            {
                Content = GetValidContent(),
                HttpResponseMessage = GetValidHttpResponseMessage()
            };

            return crawledPage;
        }

        private PageContent GetValidContent()
        {
            return new PageContent()
            {
                Encoding = Encoding.UTF8,
                Text = "AAAAA",
                Charset = "whatever",
                Bytes = Encoding.UTF8.GetBytes("AAAAA")
            };
        }

        private HttpResponseMessage GetValidHttpResponseMessage()
        {
            return new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content = new StringContent("Doesn't matter", Encoding.UTF8, "text/html")
            };
        }

    }
}


text:
<?xml version='1.0' encoding='UTF-8'?>
<resources>

    <!-- login -->
    <string name="prompt_blog_url">Blogadresse</string>
    <string name="prompt_email">Email</string>
    <string name="prompt_password">Passwort</string>
    <string name="action_sign_in">Anmelden</string>

    <string name="error_invalid_email">Diese Emailadresse ist ungültig</string>
    <string name="error_field_required">Bitte fülle dieses Feld aus</string>

    <string name="no_such_blog">Es konnte kein Ghost Admin-Panel in %s/ghost gefunden werden</string>
    <string name="login_unexpected_error">Unerwarteter Fehler beim Anmelden. Bitte versuche es erneut.</string>
    <string name="login_connection_error">Es konnte keine Verbindung zu %s/ghost erstellt werden. Bitte versuche es erneut.</string>
    <string name="login_ssl_unsupported">Dein Blog unterstützt leider kein https:// (SSL)</string>
    <string name="logout">Abmelden</string>
    <string name="credentials_expired">Hat sich dein Passwort geändert? Anmeldung mit diesem Passwort nicht möglich.</string>
    <string name="unsynced_changes_msg">Alle nicht synchronisierten Änderungen gehen verloren, wenn du dich jetzt abmeldest</string>
    <string name="dont_logout">Nicht abmelden</string>

    <!-- posts -->
    <string name="status_published">Veröffentlicht %s</string>
    <string name="status_draft">Entwurf</string>
    <string name="status_scheduled">Geplant - %s</string>
    <string name="status_offline_changes">Auf dem Gerät gespeichert, wartet auf eine Verbindung…</string>
    <string name="status_published_auto_saved">Änderungen automatisch gespeichert - noch nicht veröffentlicht</string>
    <string name="status_marked_for_deletion">Zum Löschen markiert, wartet auf eine Verbindung…</string>
    <string name="status_marked_for_deletion_open_error">Dieser Artikel wurde zum Löschen markiert, du kannst ihn nicht öffnen</string>
    <string name="send_feedback">Sende Feedback…</string>

    <string name="preview">Vorschau</string>
    <string name="edit">Bearbeiten</string>
    <string name="publish">Veröffentlichen</string>
    <string name="update_post">Aktualisieren</string>
    <string name="unpublish">Unveröffentlichen</string>
    <string name="alert_publish">Entwurf veröffentlichen?</string>
    <string name="alert_unpublish">Artikel unveröffentlichen?</string>
    <string name="delete_draft">Entwurf löschen</string>
    <string name="alert_delete_draft_title">Diesen Entwurf löschen?</string>
    <string name="alert_delete_draft_msg">Dieser Schritt kann nicht rückgängig gemacht werden!</string>
    <string name="alert_delete_yes">Löschen</string>
    <string name="alert_delete_no">Behalten</string>
    <string name="post_deleted">Artikel gelöscht</string>
    <string name="alert_save_msg">Artikel mit Änderungen aktualisieren?</string>
    <string name="alert_save_yes">Ja, aktualisieren</string>
    <string name="alert_save_no">Nicht jetzt</string>
    <string name="discard_changes">Verwerfe Änderungen</string>
    <string name="alert_discard_changes_title">Sollen alle Veränderungen verworfen werden seit du diesen Artikel geöffnet hast?</string>
    <string name="alert_discard_changes_msg">Dieser Schritt kann nicht rückgängig gemacht werden!</string>
    <string name="discard">Verwerfen</string>
    <string name="view_homepage">Homepage ansehen</string>
    <string name="view_post_on_blog">Artikel auf dem Blog anschauen</string>
    <string name="post_settings">Artikeleinstellungen</string>

    <string name="save_post_progress">Artikel wird gespeichert…</string>
    <string name="save_post_failed">Artikel konnte nicht gespeichert werden</string>
    <string name="save_post_view">Auf dem Blog anschauen</string>
    <string name="save_post_timeout">Auf dem Gerät gespeichert</string>

    <string name="save_scenario_unknown">Gespeichert</string>
    <string name="save_scenario_publish_draft">Artikel veröffentlicht!</string>
    <string name="save_scenario_unpublish_published_post">Artikel unveröffentlicht</string>
    <string name="save_scenario_auto_save_scheduled_or_published_post">Änderungen wurden auf dem Gerät gespeichert</string>
    <string name="save_scenario_explicitly_update_scheduled_or_published_post">Artikel aktualisiert</string>
    <string name="save_scenario_auto_save_draft">Entwurf gespeichert</string>
    <string name="save_scenario_explicitly_save_draft">Entwurf gespeichert</string>

    <string name="post_title_hint">Gib deinem Artikel einen Titel…</string>
    <string name="post_image_hint">Füge ein Artikelbild hinzu</string>
    <string name="post_tags_hint">Füge Tags hinzu…</string>
    <string name="post_tags_format_hint">Trenne Tags mit Kommata</string>
    <string name="post_body_hint">Schreibe hier…</string>
    <string name="post_image_load_error">Fehler beim Laden des Artikelbildes</string>
    <string name="post_feature_hint">Feature diesen Artikel</string>

    <!-- image insert / upload -->
    <string name="insert_image">Bild einfügen</string>
    <string name="insert_image_url">Url einfügen</string>
    <string name="image_url">Bild Url</string>
    <string name="insert_image_upload">Bild hochladen</string>
    <string name="uploading">Lädt…</string>
    <string name="image_upload_failed">Bild konnte nicht hochgeladen werden</string>
    <string name="remove_image">Dieses Bild löschen</string>

    <!-- about -->
    <string name="about_action">Über…</string>
    <string name="about">Über</string>

    <string name="version">Version</string>

    <string name="category_support_development">Entwicklung unterstützen</string>
    <string name="play_store">Im Play Store bewerten</string>
    <string name="play_store_subtitle">Vielen Dank für Deine Unterstützung!</string>

    <!-- network -->
    <string name="no_internet_connection">Keine Internetverbindung</string>
    <string name="network_timeout">Verbindung unterbrochen</string>
    <string name="refresh_failed">Daten konnten nicht aktualisiert werden</string>

    <!-- generic -->
    <string name="done">Fertig</string>
    <string name="save">Speichern</string>
    <string name="refresh">Aktualisieren</string>
    <string name="share">Teilen</string>
    <string name="loading">Lädt…</string>
    <string name="insert">Einfügen</string>
    <string name="intent_no_apps">Es konnte keine App gefunden werde, die diese Aufgabe ausführen kann</string>
    <string name="next">Nächster</string>

</resources>


text:
#include "postgres.h"

#include <fcntl.h>
#include <unistd.h>

#include "access/heapam.h"
#include "access/xact.h"
#include "catalog/catalog.h"
#include "catalog/namespace.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_database.h"
#include "catalog/pg_tablespace.h"
#include "libpq/hba.h"
#include "libpq/libpq-be.h"
#include "cdb/cdbvars.h"
#include "cdb/cdbutil.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/autovacuum.h"
#include "postmaster/postmaster.h"
#include "storage/backendid.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "storage/procsignal.h"
#include "storage/sinvaladt.h"
#include "storage/smgr.h"
#include "utils/acl.h"
#include "utils/flatfiles.h"
#include "utils/guc.h"
#include "utils/relcache.h"
#include "utils/resscheduler.h"
#include "utils/syscache.h"
#include "utils/tqual.h"  		/* SharedSnapshot */
#include "utils/portal.h"
#include "pgstat.h"
#include "resourcemanager/communication/rmcomm_QD2RM.h"
#include "cdb/cdbtmpdir.h"
#include "utils/session_state.h"

bool FindMyDatabase(const char *name, Oid *db_id, Oid *db_tablespace);
static bool FindMyDatabaseByOid(Oid dbid, char *dbname, Oid *db_tablespace);
static void CheckMyDatabase(const char *name, bool am_superuser);
static void InitCommunication(void);
static void ShutdownPostgres(int code, Datum arg);
static bool ThereIsAtLeastOneRole(void);

#ifdef USE_ORCA
extern void InitGPOPT();
extern void TerminateGPOPT();
#endif

/*** InitPostgres support ***/

/*
 * FindMyDatabase -- get the critical info needed to locate my database
 *
 * Find the named database in pg_database, return its database OID and the
 * OID of its default tablespace.  Return TRUE if found, FALSE if not.
 *
 * Since we are not yet up and running as a backend, we cannot look directly
 * at pg_database (we can't obtain locks nor participate in transactions).
 * So to get the info we need before starting up, we must look at the "flat
 * file" copy of pg_database that is helpfully maintained by flatfiles.c.
 * This is subject to various race conditions, so after we have the
 * transaction infrastructure started, we have to recheck the information;
 * see InitPostgres.
 */
bool
FindMyDatabase(const char *name, Oid *db_id, Oid *db_tablespace)
{
	bool		result = false;
	char	   *filename;
	FILE	   *db_file;
	char		thisname[NAMEDATALEN];
	TransactionId db_frozenxid;

	filename = database_getflatfilename();
	db_file = AllocateFile(filename, "r");
	if (db_file == NULL)
		ereport(FATAL,
				(errcode_for_file_access(),
				 errmsg("could not open file \"%s\": %m", filename)));

	while (read_pg_database_line(db_file, thisname, db_id,
								 db_tablespace, &db_frozenxid))
	{
		if (strcmp(thisname, name) == 0)
		{
			result = true;
			break;
		}
	}

	FreeFile(db_file);
	pfree(filename);

	return result;
}

/*
 * FindMyDatabaseByOid
 *
 * As above, but the actual database Id is known.  Return its name and the
 * tablespace OID.	Return TRUE if found, FALSE if not.  The same restrictions
 * as FindMyDatabase apply.
 */
static bool
FindMyDatabaseByOid(Oid dbid, char *dbname, Oid *db_tablespace)
{
	bool		result = false;
	char	   *filename;
	FILE	   *db_file;
	Oid			db_id;
	char		thisname[NAMEDATALEN];
	TransactionId db_frozenxid;

	filename = database_getflatfilename();
	db_file = AllocateFile(filename, "r");
	if (db_file == NULL)
		ereport(FATAL,
				(errcode_for_file_access(),
				 errmsg("could not open file \"%s\": %m", filename)));

	while (read_pg_database_line(db_file, thisname, &db_id,
								 db_tablespace, &db_frozenxid))
	{
		if (dbid == db_id)
		{
			result = true;
			strlcpy(dbname, thisname, NAMEDATALEN);
			break;
		}
	}

	FreeFile(db_file);
	pfree(filename);

	return result;
}

/*
 * CheckMyDatabase -- fetch information from the pg_database entry for our DB
 */
static void
CheckMyDatabase(const char *name, bool am_superuser)
{
	HeapTuple	tup;
	Form_pg_database dbform;

	/* Fetch our real pg_database row */
	tup = SearchSysCache(DATABASEOID,
						 ObjectIdGetDatum(MyDatabaseId),
						 0, 0, 0);
	if (!HeapTupleIsValid(tup))
		elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
	dbform = (Form_pg_database) GETSTRUCT(tup);

	/* This recheck is strictly paranoia */
	if (strcmp(name, NameStr(dbform->datname)) != 0)
		ereport(FATAL,
				(errcode(ERRCODE_UNDEFINED_DATABASE),
				 errmsg("database \"%s\" has disappeared from pg_database",
						name),
				 errdetail("Database OID %u now seems to belong to \"%s\".",
						   MyDatabaseId, NameStr(dbform->datname))));

	/*
	 * Check permissions to connect to the database.
	 *
	 * These checks are not enforced when in standalone mode, so that there is
	 * a way to recover from disabling all access to all databases, for
	 * example "UPDATE pg_database SET datallowconn = false;".
	 *
	 * We do not enforce them for the autovacuum worker processes either.
	 */
	if (IsUnderPostmaster && !IsAutoVacuumProcess())
	{
		/*
		 * Check that the database is currently allowing connections.
		 * (exception during upgrade_mode)
		 */
		if (gp_upgrade_mode && !dbform->datallowconn)
	        elog(INFO, "Connecting to no-connection db in upgrade mode.");

		if (!dbform->datallowconn && !gp_upgrade_mode)
			ereport(FATAL,
					(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
			 errmsg("database \"%s\" is not currently accepting connections",
					name),
			 errOmitLocation(true),
			 errSendAlert(false)));

		/*
		 * Check privilege to connect to the database.	(The am_superuser test
		 * is redundant, but since we have the flag, might as well check it
		 * and save a few cycles.)
		 */
		if (!am_superuser &&
			pg_database_aclcheck(MyDatabaseId, GetUserId(),
								 ACL_CONNECT) != ACLCHECK_OK)
			ereport(FATAL,
					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
					 errmsg("permission denied for database \"%s\"", name),
					 errdetail("User does not have CONNECT privilege."),
					 errOmitLocation(true),
					 errSendAlert(false)));

		/*
		 * Check connection limit for this database.
		 *
		 * There is a race condition here --- we create our PGPROC before
		 * checking for other PGPROCs.	If two backends did this at about the
		 * same time, they might both think they were over the limit, while
		 * ideally one should succeed and one fail.  Getting that to work
		 * exactly seems more trouble than it is worth, however; instead we
		 * just document that the connection limit is approximate.
		 */
		if (dbform->datconnlimit >= 0 &&
			!am_superuser &&
			CountDBBackends(MyDatabaseId) > dbform->datconnlimit)
			ereport(FATAL,
					(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
					 errmsg("too many connections for database \"%s\"",
							name),
					 errOmitLocation(true),
					 errSendAlert(false)));
	}

	/*
	 * OK, we're golden.  Next to-do item is to save the encoding info out of
	 * the pg_database tuple.
	 */
	if (GpIdentity.segindex == UNINITIALIZED_GP_IDENTITY_VALUE ||
		GpIdentity.segindex == MASTER_CONTENT_ID)
		SetDatabaseEncoding(dbform->encoding);
	else
		SetDatabaseEncoding(MyProcPort->encoding);

	/* Record it as a GUC internal option, too */
	SetConfigOption("server_encoding", GetDatabaseEncodingName(),
					PGC_INTERNAL, PGC_S_OVERRIDE);
	/* If we have no other source of client_encoding, use server encoding */
	SetConfigOption("client_encoding", GetDatabaseEncodingName(),
					PGC_BACKEND, PGC_S_DEFAULT);

	/* Use the right encoding in translated messages */
#ifdef ENABLE_NLS
	pg_bind_textdomain_codeset(textdomain(NULL));
#endif

	/*
	 * Lastly, set up any database-specific configuration variables.
	 */
	if (IsUnderPostmaster)
	{
		Datum		datum;
		bool		isnull;

		datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_datconfig,
								&isnull);
		if (!isnull)
		{
			ArrayType  *a = DatumGetArrayTypeP(datum);

			ProcessGUCArray(a, PGC_S_DATABASE);
		}
	}

	ReleaseSysCache(tup);
}

/* --------------------------------
 *		InitCommunication
 *
 *		This routine initializes stuff needed for ipc, locking, etc.
 *		it should be called something more informative.
 * --------------------------------
 */
static void
InitCommunication(void)
{
	/*
	 * initialize shared memory and semaphores appropriately.
	 */
	if (!IsUnderPostmaster)		/* postmaster already did this */
	{
		/*
		 * We're running a postgres bootstrap process or a standalone backend.
		 * Create private "shmem" and semaphores.
		 */
		CreateSharedMemoryAndSemaphores(true, 0);
	}
}

/*
 * Early initialization of a backend (either standalone or under postmaster).
 * This happens even before InitPostgres.
 *
 * If you're wondering why this is separate from InitPostgres at all:
 * the critical distinction is that this stuff has to happen before we can
 * run XLOG-related initialization, which is done before InitPostgres --- in
 * fact, for cases such as the background writer process, InitPostgres may
 * never be done at all.
 */
void
BaseInit(void)
{
	/*
	 * Attach to shared memory and semaphores, and initialize our
	 * input/output/debugging file descriptors.
	 */
	InitCommunication();
	DebugFileOpen();

	/* Do local initialization of file, storage and buffer managers */
	InitFileAccess();
	smgrinit();
	InitBufferPoolAccess();
}

/* --------------------------------
 * InitPostgres
 *		Initialize POSTGRES.
 *
 * The database can be specified by name, using the in_dbname parameter, or by
 * OID, using the dboid parameter.	In the latter case, the computed database
 * name is passed out to the caller as a palloc'ed string in out_dbname.
 *
 * In bootstrap mode no parameters are used.
 *
 * The return value indicates whether the userID is a superuser.  (That
 * can only be tested inside a transaction, so we want to do it during
 * the startup transaction rather than doing a separate one in postgres.c.)
 *
 * As of PostgreSQL 8.2, we expect InitProcess() was already called, so we
 * already have a PGPROC struct ... but it's not filled in yet.
 *
 * Note:
 *		Be very careful with the order of calls in the InitPostgres function.
 * --------------------------------
 */
bool
InitPostgres(const char *in_dbname, Oid dboid, const char *username,
			 char **out_dbname)
{
	bool		bootstrap = IsBootstrapProcessingMode();
	bool		autovacuum = IsAutoVacuumProcess();
	bool		am_superuser;
	char	   *fullpath;
	char		dbname[NAMEDATALEN];

	/*
	 * User is not supposed to connect to hcatalog database,
	 * because it's reserved for HCatalog integration feature
	 */
	if (!bootstrap)
	{
		if (strcmp(in_dbname, HcatalogDbName) == 0)
		{
			ereport(ERROR,
					(errcode(ERRCODE_UNDEFINED_DATABASE),
					errmsg("\"%s\" database is only for system use",
					HcatalogDbName)));
		}
	}

	/*
	 * Set up the global variables holding database id and path.  But note we
	 * won't actually try to touch the database just yet.
	 *
	 * We take a shortcut in the bootstrap case, otherwise we have to look up
	 * the db name in pg_database.
	 */
	if (bootstrap)
	{
		MyDatabaseId = TemplateDbOid;
		MyDatabaseTableSpace = DEFAULTTABLESPACE_OID;
	}
	else
	{
		/*
		 * Find tablespace of the database we're about to open. Since we're
		 * not yet up and running we have to use one of the hackish
		 * FindMyDatabase variants, which look in the flat-file copy of
		 * pg_database.
		 *
		 * If the in_dbname param is NULL, lookup database by OID.
		 */
		if (in_dbname == NULL)
		{
			if (!FindMyDatabaseByOid(dboid, dbname, &MyDatabaseTableSpace))
				ereport(FATAL,
						(errcode(ERRCODE_UNDEFINED_DATABASE),
						 errmsg("database %u does not exist", dboid),
						 errSendAlert(false)));
			MyDatabaseId = dboid;
			/* pass the database name to the caller */
			*out_dbname = pstrdup(dbname);
		}
		else
		{
			if (!FindMyDatabase(in_dbname, &MyDatabaseId, &MyDatabaseTableSpace))
				ereport(FATAL,
						(errcode(ERRCODE_UNDEFINED_DATABASE),
						 errmsg("database \"%s\" does not exist",
								in_dbname),
						 errOmitLocation(true),
						 errSendAlert(false)));
			/* our database name is gotten from the caller */
			strlcpy(dbname, in_dbname, NAMEDATALEN);
		}
	}

	fullpath = GetDatabasePath(MyDatabaseId, MyDatabaseTableSpace);

	SetDatabasePath(fullpath);

	/*
	 * Finish filling in the PGPROC struct, and add it to the ProcArray. (We
	 * need to know MyDatabaseId before we can do this, since it's entered
	 * into the PGPROC struct.)
	 *
	 * Once I have done this, I am visible to other backends!
	 */
	InitProcessPhase2();

    // get temporary directory for QD
    if (!bootstrap && Gp_role == GP_ROLE_DISPATCH)
    {
        if (get_tmpdir_from_rm)
        {
            elog(ERROR, "The GUC value get_tmpdir_from_rm to be true hasn't been supported yet. "
			"Please set get_tmpdir_from_rm=false ");
            /*
        	char errorbuf[ERRORMESSAGE_SIZE] = "";
            int res = getLocalTmpDirFromMasterRM(errorbuf, sizeof(errorbuf));
            if ( res != FUNC_RETURN_OK )
            {
            	elog(ERROR, "%s", errorbuf);
            }
            */
        }
        else
        {
            getLocalTmpDirFromMasterConfig(gp_session_id);
            elog(LOG, "getLocalTmpDirFromMasterConfig session_id:%d tmpdir:%s", gp_session_id, LocalTempPath);
        }

    }
	/* Initialize SessionState entry */
	SessionState_Init();
	/* Initialize memory protection */
	GPMemoryProtect_Init();

#ifdef USE_ORCA
	/* Initialize GPOPT */
	InitGPOPT();
#endif

	/*
	 * Initialize my entry in the shared-invalidation manager's array of
	 * per-backend data.
	 *
	 * Sets up MyBackendId, a unique backend identifier.
	 */
	MyBackendId = InvalidBackendId;

	SharedInvalBackendInit(false);

	if (MyBackendId > MaxBackends || MyBackendId <= 0)
		elog(FATAL, "bad backend id: %d", MyBackendId);

	/* Now that we have a BackendId, we can participate in ProcSignal */
	ProcSignalInit(MyBackendId);

	/*
	 * bufmgr needs another initialization call too
	 */
	InitBufferPoolBackend();

	/*
	 * Initialize local process's access to XLOG.  In bootstrap case we may
	 * skip this since StartupXLOG() was run instead.
	 */
	if (!bootstrap)
		InitXLOGAccess();

	/*
	 * Initialize the relation cache and the system catalog caches.  Note that
	 * no catalog access happens here; we only set up the hashtable structure.
	 * We must do this before starting a transaction because transaction abort
	 * would try to touch these hashtables.
	 */
	RelationCacheInitialize();
	InitCatalogCache();

	/* Initialize portal manager */
	EnablePortalManager();

	/* Initialize stats collection --- must happen before first xact */
	if (!bootstrap)
		pgstat_initialize();

	/*
	 * Set up process-exit callback to do pre-shutdown cleanup.  This has to
	 * be after we've initialized all the low-level modules like the buffer
	 * manager, because during shutdown this has to run before the low-level
	 * modules start to close down.  On the other hand, we want it in place
	 * before we begin our first transaction --- if we fail during the
	 * initialization transaction, as is entirely possible, we need the
	 * AbortTransaction call to clean up.
	 */
	on_shmem_exit(ShutdownPostgres, 0);

	/*
	 * Start a new transaction here before first access to db, and get a
	 * snapshot.  We don't have a use for the snapshot itself, but we're
	 * interested in the secondary effect that it sets RecentGlobalXmin.
	 */
	if (!bootstrap)
	{
		StartTransactionCommand();
		(void) GetTransactionSnapshot();
	}

	/*
	 * Now that we have a transaction, we can take locks.  Take a writer's
	 * lock on the database we are trying to connect to.  If there is a
	 * concurrently running DROP DATABASE on that database, this will block us
	 * until it finishes (and has updated the flat file copy of pg_database).
	 *
	 * Note that the lock is not held long, only until the end of this startup
	 * transaction.  This is OK since we are already advertising our use of
	 * the database in the PGPROC array; anyone trying a DROP DATABASE after
	 * this point will see us there.
	 *
	 * Note: use of RowExclusiveLock here is reasonable because we envision
	 * our session as being a concurrent writer of the database.  If we had a
	 * way of declaring a session as being guaranteed-read-only, we could use
	 * AccessShareLock for such sessions and thereby not conflict against
	 * CREATE DATABASE.
	 */
	if (!bootstrap)
	{
		if (MyDatabaseId == TemplateDbOid)
			LockSharedObject(DatabaseRelationId, MyDatabaseId, 0,
							 AccessShareLock);
		else
			LockSharedObject(DatabaseRelationId, MyDatabaseId, 0,
							 RowExclusiveLock);
	}

	/*
	 * Recheck the flat file copy of pg_database to make sure the target
	 * database hasn't gone away.  If there was a concurrent DROP DATABASE,
	 * this ensures we will die cleanly without creating a mess.
	 */
	if (!bootstrap)
	{
		Oid			dbid2;
		Oid			tsid2;

		if (!FindMyDatabase(dbname, &dbid2, &tsid2) ||
			dbid2 != MyDatabaseId || tsid2 != MyDatabaseTableSpace)
			ereport(FATAL,
					(errcode(ERRCODE_UNDEFINED_DATABASE),
					 errmsg("database \"%s\" does not exist",
							dbname),
					 errdetail("It seems to have just been dropped or renamed.")));
	}

	/*
	 * Now we should be able to access the database directory safely. Verify
	 * it's there and looks reasonable.
	 */
	if (!bootstrap)
	{
		if (access(fullpath, F_OK) == -1)
		{
			if (errno == ENOENT)
				ereport(FATAL,
						(errcode(ERRCODE_UNDEFINED_DATABASE),
						 errmsg("database \"%s\" does not exist",
								dbname),
						 errdetail("The database subdirectory \"%s\" is missing.",
								   fullpath)));
			else
				ereport(FATAL,
						(errcode_for_file_access(),
						 errmsg("could not access directory \"%s\": %m",
								fullpath)));
		}

		ValidatePgVersion(fullpath);
	}

	/*
	 * It's now possible to do real access to the system catalogs.
	 *
	 * Load relcache entries for the system catalogs.  This must create at
	 * least the minimum set of "nailed-in" cache entries.
	 */
	RelationCacheInitializePhase2();

	/*
	 * Figure out our postgres user id, and see if we are a superuser.
	 *
	 * In standalone mode and in the autovacuum process, we use a fixed id,
	 * otherwise we figure it out from the authenticated user name.
	 */
	if (bootstrap || autovacuum)
	{
		InitializeSessionUserIdStandalone();
		am_superuser = true;
	}
	else if (!IsUnderPostmaster)
	{
		InitializeSessionUserIdStandalone();
		am_superuser = true;
		if (!ThereIsAtLeastOneRole())
			ereport(WARNING,
					(errcode(ERRCODE_UNDEFINED_OBJECT),
					 errmsg("no roles are defined in this database system"),
					 errhint("You should immediately run CREATE USER \"%s\" CREATEUSER;.",
							 username)));
	}
	else
	{
		/* normal multiuser case */
		InitializeSessionUserId(username);
		am_superuser = superuser();
	}

	/* set up ACL framework (so CheckMyDatabase can check permissions) */
	initialize_acl();

	/*
	 * Read the real pg_database row for our database, check permissions and
	 * set up database-specific GUC settings.  We can't do this until all the
	 * database-access infrastructure is up.  (Also, it wants to know if the
	 * user is a superuser, so the above stuff has to happen first.)
	 */
	if (!bootstrap)
		CheckMyDatabase(dbname, am_superuser);

	/*
	 * Check a normal user hasn't connected to a superuser reserved slot.
	 */
	if (!am_superuser &&
		ReservedBackends > 0 &&
		!HaveNFreeProcs(ReservedBackends))
		ereport(FATAL,
				(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
				 errmsg("connection limit exceeded for non-superusers"),
				 errOmitLocation(true),
				 errSendAlert(true)));

	/*
	 * Initialize various default states that can't be set up until we've
	 * selected the active user and gotten the right GUC settings.
	 */

	/* set default namespace search path */
	InitializeSearchPath();

	/* initialize client encoding */
	InitializeClientEncoding();

	/* report this backend in the PgBackendStatus array */
	if (!bootstrap)
		pgstat_bestart();

	/*
   * MPP package setup
   *
   * Primary function is to establish connctions to the qExecs.
   * This is SKIPPED when the database is in bootstrap mode or
   * Is not UnderPostmaster.
   */
  if (!bootstrap && IsUnderPostmaster)
  {
  cdb_setup();
  on_proc_exit( cdb_cleanup, 0 );
  }

	/* close the transaction we started above */
	if (!bootstrap)
		CommitTransactionCommand();

	return am_superuser;
}

/*
 * Backend-shutdown callback.  Do cleanup that we want to be sure happens
 * before all the supporting modules begin to nail their doors shut via
 * their own callbacks.
 *
 * User-level cleanup, such as temp-relation removal and UNLISTEN, happens
 * via separate callbacks that execute before this one.  We don't combine the
 * callbacks because we still want this one to happen if the user-level
 * cleanup fails.
 */
static void
ShutdownPostgres(int code, Datum arg)
{
	/* Make sure we've killed any active transaction */
	AbortOutOfAnyTransaction();

	/*
	 * If there was a segment OOM for which we haven't already reported
	 * our usage, report now.
	 */
	ReportOOMConsumption();

#ifdef USE_ORCA
	TerminateGPOPT();
#endif

	/* Disable memory protection */
	GPMemoryProtect_Shutdown();
	/* Release SessionState entry */
	SessionState_Shutdown();

	/*
	 * User locks are not released by transaction end, so be sure to release
	 * them explicitly.
	 */
	LockReleaseAll(USER_LOCKMETHOD, true);
}

/*
 * Returns true if at least one role is defined in this database cluster.
 */
static bool
ThereIsAtLeastOneRole(void)
{
	Relation	pg_authid_rel;
	HeapScanDesc scan;
	bool		result;

	pg_authid_rel = heap_open(AuthIdRelationId, AccessShareLock);

	scan = heap_beginscan(pg_authid_rel, SnapshotNow, 0, NULL);
	result = (heap_getnext(scan, ForwardScanDirection) != NULL);

	heap_endscan(scan);
	heap_close(pg_authid_rel, AccessShareLock);

	return result;
}


text:
//-----------------------------------------------------------------------
// <copyright file="OptionalProperties.cs" company="Patrick 'Ricky' Smith">
//  This file is part of the Twitterizer library (http://www.twitterizer.net/)
//
//  Copyright (c) 2010, Patrick "Ricky" Smith (ricky@digitally-born.com)
//  All rights reserved.
//
//  Redistribution and use in source and binary forms, with or without modification, are
//  permitted provided that the following conditions are met:
//
//  - Redistributions of source code must retain the above copyright notice, this list
//    of conditions and the following disclaimer.
//  - Redistributions in binary form must reproduce the above copyright notice, this list
//    of conditions and the following disclaimer in the documentation and/or other
//    materials provided with the distribution.
//  - Neither the name of the Twitterizer nor the names of its contributors may be
//    used to endorse or promote products derived from this software without specific
//    prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
//  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
//  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
//  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
//  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
//  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
//  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//  POSSIBILITY OF SUCH DAMAGE.
// </copyright>
// <author>Ricky Smith</author>
// <summary>The base class for optional property classes</summary>
//-----------------------------------------------------------------------

namespace Twitterizer
{
    using System;
    using System.Configuration;
    using System.Net;

    /// <include file='OptionalProperties.xml' path='OptionalProperties/OptionalProperties/*'/>
#if !SILVERLIGHT
    [Serializable]
#endif
    public class OptionalProperties
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="OptionalProperties"/> class.
        /// </summary>
        public OptionalProperties()
        {
            // Set the default values for the properties
            this.UseSSL = false;
            this.APIBaseAddress = "http://api.twitter.com/1/";
        }

        /// <include file='OptionalProperties.xml' path='OptionalProperties/Property[@name="UseSSL"]/*'/>
        /// <summary>
        ///  Allows configuration of the base address for API method requests for support for 3rd party 'twitter-like' APIs.
        /// </summary>
        public bool UseSSL { get; set; }

        /// <include file='OptionalProperties.xml' path='OptionalProperties/Property[@name="APIBaseAddress"]/*'/>
        public string APIBaseAddress { get; set; }

#if !SILVERLIGHT
        /// <include file='OptionalProperties.xml' path='OptionalProperties/Property[@name="Proxy"]/*'/>
        public WebProxy Proxy { get; set; }
#endif
    }
}


text:
module Sequel
  module Plugins
    # The PgTypecastOnLoad plugin exists because when you connect to PostgreSQL
    # using the do and swift adapters, Sequel doesn't have complete
    # control over typecasting, and may return columns as strings instead of how
    # the native postgres adapter would typecast them.  This is mostly needed for
    # the additional support that the pg_* extensions add for advanced PostgreSQL
    # types such as arrays. This plugin is not needed if using the postgres or
    # jdbc/postgresql adapters.
    #
    # This plugin makes model loading to do the same conversion that the
    # native postgres adapter would do for all columns given.  You can either
    # specify the columns to typecast on load in the plugin call itself, or
    # afterwards using add_pg_typecast_on_load_columns:
    #
    #   # aliases => text[] column
    #   # config => hstore column
    #
    #   Album.plugin :pg_typecast_on_load, :aliases, :config
    #   # or:
    #   Album.plugin :pg_typecast_on_load
    #   Album.add_pg_typecast_on_load_columns :aliases, :config
    #
    # This plugin only handles values that the adapter returns as strings.  If
    # the adapter returns a value other than a string, this plugin will have no
    # effect.  You may be able to use the regular typecast_on_load plugin to
    # handle those cases.
    module PgTypecastOnLoad
      # Call add_pg_typecast_on_load_columns on the passed column arguments.
      def self.configure(model, *columns)
        model.instance_eval do
          @pg_typecast_on_load_columns ||= []
          add_pg_typecast_on_load_columns(*columns)
        end
      end

      module ClassMethods
        # The columns to typecast on load for this model.
        attr_reader :pg_typecast_on_load_columns

        # Add additional columns to typecast on load for this model.
        def add_pg_typecast_on_load_columns(*columns)
          @pg_typecast_on_load_columns.concat(columns)
        end

        def call(values)
          super(load_typecast_pg(values))
        end

        # Lookup the conversion proc for the column's oid in the Database
        # object, and use it to convert the value.
        def load_typecast_pg(values)
          pg_typecast_on_load_columns.each do |c|
            if (v = values[c]).is_a?(String) && (oid = db_schema[c][:oid]) && (pr = db.conversion_procs[oid])
              values[c] = pr.call(v)
            end
          end
          values
        end

        Plugins.inherited_instance_variables(self, :@pg_typecast_on_load_columns=>:dup)
      end

      module InstanceMethods
        private

        # Typecast specific columns using the conversion procs when manually refreshing.
        def _refresh_set_values(values)
          super(model.load_typecast_pg(values))
        end

        # Typecast specific columns using the conversion procs when refreshing after save.
        def _save_set_values(values)
          super(model.load_typecast_pg(values))
        end
      end
    end
  end
end


text:
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version. See also the license.txt file for *
 *   additional informations.                                              *
 *                                                                         *
 ***************************************************************************/

//*************************************************************************//
// History of changes:
//
// 2002/05/15 - Pete
// - generic cleanup for the Peops release
//
//*************************************************************************//

void InterruptDMA4(void);
void InterruptDMA7(void);


text:
Class = require 'hump.class'

-- Entity is the basic building block of all objects in fysplane. All other
-- objects in game that are not part of the background derive from this class.

entity_id = 1

Entity = Class{
    init = function(self, x, y, level)
        self.x = x
        self.y = y
        self.level = level
        self.owner = nil

        self.id = entity_id
        entity_id = entity_id + 1

        self.level.entity_list[self.id] = self
    end;

    setOwner = function(self, owner)
        self.owner = owner
    end;

    getOwner = function(self)
        return self.owner
    end;

    draw = function(self)
    end;

    update = function(self, dt)
    end;

    delete = function(self)
        self.level.entity_list[self.id] = nil
    end;
}


text:
using VitruviusTest.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using WindowsPreview.Kinect;
using LightBuzz.Vitruvius;
using Windows.Storage.Pickers;
using Windows.Storage;
using Windows.UI.Xaml.Media.Imaging;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace VitruviusTest
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class CameraPage : Page
    {
        NavigationHelper _navigationHelper;

        KinectSensor _sensor;
        MultiSourceFrameReader _reader;
        UsersController _userReporter;

        bool _displaySkeleton;

        public NavigationHelper NavigationHelper
        {
            get { return _navigationHelper; }
        }

        public CameraPage()
        {
            InitializeComponent();

            _navigationHelper = new NavigationHelper(this);

            _sensor = KinectSensor.GetDefault();

            if (_sensor != null)
            {
                _sensor.Open();

                _reader = _sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color | FrameSourceTypes.Depth | FrameSourceTypes.Infrared | FrameSourceTypes.Body);
                _reader.MultiSourceFrameArrived += Reader_MultiSourceFrameArrived;

                _userReporter = new UsersController();
                _userReporter.BodyEntered += UserReporter_BodyEntered;
                _userReporter.BodyLeft += UserReporter_BodyLeft;
                _userReporter.Start();
            }
        }

        private void PageRoot_Unloaded(object sender, RoutedEventArgs e)
        {
            if (_userReporter != null)
            {
                _userReporter.Stop();
            }

            if (_reader != null)
            {
                _reader.Dispose();
            }

            if (_sensor != null)
            {
                _sensor.Close();
            }
        }

        private void Color_Click(object sender, RoutedEventArgs e)
        {
            viewer.Visualization = Visualization.Color;
        }

        private void Depth_Click(object sender, RoutedEventArgs e)
        {
            viewer.Visualization = Visualization.Depth;
        }

        private void Infrared_Click(object sender, RoutedEventArgs e)
        {
            viewer.Visualization = Visualization.Infrared;
        }

        private void Skeleton_Checked(object sender, RoutedEventArgs e)
        {
            _displaySkeleton = true;
        }

        private void Skeleton_Unchecked(object sender, RoutedEventArgs e)
        {
            _displaySkeleton = false;
        }

        void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            var reference = e.FrameReference.AcquireFrame();

            // Color
            using (var frame = reference.ColorFrameReference.AcquireFrame())
            {
                if (frame != null)
                {
                    if (viewer.Visualization == Visualization.Color)
                    {
                        viewer.Image = frame.ToBitmap();
                    }
                }
            }

            // Depth
            using (var frame = reference.DepthFrameReference.AcquireFrame())
            {
                if (frame != null)
                {
                    if (viewer.Visualization == Visualization.Depth)
                    {
                        viewer.Image = frame.ToBitmap();
                    }
                }
            }

            // Infrared
            using (var frame = reference.InfraredFrameReference.AcquireFrame())
            {
                if (frame != null)
                {
                    if (viewer.Visualization == Visualization.Infrared)
                    {
                        viewer.Image = frame.ToBitmap();
                    }
                }
            }

            // Body
            using (var frame = reference.BodyFrameReference.AcquireFrame())
            {
                if (frame != null)
                {
                    var bodies = frame.Bodies();

                    _userReporter.Update(bodies);

                    foreach (Body body in bodies)
                    {
                        if (_displaySkeleton)
                        {
                            viewer.DrawBody(body);
                        }
                    }
                }
            }
        }

        void UserReporter_BodyEntered(object sender, UsersControllerEventArgs e)
        {
            // A new user has entered the scene.
        }

        void UserReporter_BodyLeft(object sender, UsersControllerEventArgs e)
        {
            // A user has left the scene.
            viewer.Clear();
        }

        private async void Save_Click(object sender, RoutedEventArgs e)
        {
            // You need to have the "Pictures Library" capability enabled.
            StorageFile file = await KnownFolders.PicturesLibrary.CreateFileAsync("vitruvius-capture.jpg");
            await (viewer.Image as WriteableBitmap).Save(file);
        }
    }
}


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using SIL.Progress;

namespace Bloom.web
{
	/// <summary>
	/// Class that allows code expecting an SIL.Progress.IProgress object to use a WebSocketProgress instead.
	/// </summary>
	public class WebProgressAdapter : IProgress
	{
		private class NullProgressIndicator : IProgressIndicator
		{
			int _percent;
			public int PercentCompleted { get { return _percent; } set { _percent = value; } }
			public SynchronizationContext SyncContext { get { return null ; } set { return; } }

			public void Finish()
			{
			}

			public void IndicateUnknownProgress()
			{
			}

			public void Initialize()
			{
			}
		}

		private readonly WebSocketProgress _webProgress;

		public WebProgressAdapter(WebSocketProgress progress)
		{
			_webProgress = progress;
		}

		private bool _showVerbose;
		public bool ShowVerbose { set { _showVerbose = value; } }

		public bool CancelRequested { get { return false; } set { return; } }
		public bool ErrorEncountered { get { return false; } set { return; } }

		private IProgressIndicator _indicator = new NullProgressIndicator();
		public IProgressIndicator ProgressIndicator { get { return _indicator; } set { _indicator = value; } }

		public SynchronizationContext SyncContext { get { return null; } set { return; } }

		private List<string> _filters = new List<string>();
		public void AddFilter(string filter)
		{
			_filters.Add(filter);
		}

		private bool ShowMessage(string message)
		{
			return _filters.Count == 0 || _filters.Any(x => message.StartsWith(x));
		}

		public void WriteError(string message, params object[] args)
		{
			var msg = string.Format(message, args);
			if (ShowMessage(msg))
				_webProgress?.MessageWithoutLocalizing(msg, ProgressKind.Error);
		}

		public void WriteException(Exception error)
		{
			_webProgress?.MessageWithoutLocalizing(error.Message, ProgressKind.Error);
		}

		public void WriteMessage(string message, params object[] args)
		{
			var msg = string.Format(message, args);
			if (ShowMessage(msg))
				_webProgress?.MessageWithoutLocalizing(msg, ProgressKind.Note);
		}

		public void WriteMessageWithColor(string colorName, string message, params object[] args)
		{
			var msg = string.Format(message, args);
			if (ShowMessage(msg))
				_webProgress?.MessageWithoutLocalizing(msg, ProgressKind.Note);
		}

		public void WriteStatus(string message, params object[] args)
		{
			var msg = string.Format(message, args);
			if (ShowMessage(msg))
				_webProgress?.MessageWithoutLocalizing(msg, ProgressKind.Progress);
		}

		public void WriteVerbose(string message, params object[] args)
		{
			if (!_showVerbose)
				return;
			var msg = string.Format(message, args);
			if (ShowMessage(msg))
				_webProgress?.MessageWithoutLocalizing(msg, ProgressKind.Instruction);
		}

		public void WriteWarning(string message, params object[] args)
		{
			var msg = string.Format(message, args);
			if (ShowMessage(msg))
				_webProgress?.MessageWithoutLocalizing(msg, ProgressKind.Warning);
		}
	}
}


text:
package com.bt.pi.api.service;

import java.util.Locale;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.bt.pi.app.common.entities.Instance;
import com.bt.pi.app.common.entities.InstanceState;
import com.bt.pi.app.common.entities.PublicIpAddress;
import com.bt.pi.app.common.entities.SecurityGroup;
import com.bt.pi.app.common.entities.User;
import com.bt.pi.app.common.resource.PiQueue;
import com.bt.pi.app.networkmanager.NetworkManagerApplication;
import com.bt.pi.app.networkmanager.handlers.QueueTaskUriHelper;
import com.bt.pi.core.application.MessageContext;
import com.bt.pi.core.application.MessageContextFactory;
import com.bt.pi.core.application.watcher.task.TaskProcessingQueueContinuation;
import com.bt.pi.core.continuation.UpdateResolver;
import com.bt.pi.core.dht.BlockingDhtWriter;
import com.bt.pi.core.entity.EntityMethod;
import com.bt.pi.core.id.PId;
import com.bt.pi.core.util.MutableString;

@Component
public class ElasticIpAddressOperationHelper extends ServiceHelperBase {
    private static final Log LOG = LogFactory.getLog(ElasticIpAddressOperationHelper.class);
    private static final int MAX_QUEUE_RETRIES = 5;
    private MessageContextFactory messageContextFactory;

    public ElasticIpAddressOperationHelper() {
        messageContextFactory = null;
    }

    @Resource(type = ApiApplicationManager.class)
    public void setMessageContextFactory(MessageContextFactory aMessageContextFactory) {
        this.messageContextFactory = aMessageContextFactory;
    }

    protected String writePublicIpToInstanceRecord(final String publicIpAddress, final String instanceId) {
        LOG.debug(String.format("writePublicIpToInstanceRecord(%s, %s)", publicIpAddress, instanceId));
        PId instanceRecordId = getPiIdBuilder().getPIdForEc2AvailabilityZone(Instance.getUrl(instanceId));
        BlockingDhtWriter dhtWriterForInstance = getDhtClientFactory().createBlockingWriter();
        final MutableString securityGroupName = new MutableString();
        dhtWriterForInstance.update(instanceRecordId, null, new UpdateResolver<Instance>() {
            @Override
            public Instance update(Instance existingEntity, Instance requestedEntity) {
                LOG.debug(String.format("Provisionally setting public ip addr of instance %s to %s", instanceId, publicIpAddress));
                securityGroupName.set(existingEntity.getSecurityGroupName());

                existingEntity.setPublicIpAddress(publicIpAddress);
                return existingEntity;
            }
        });

        if (dhtWriterForInstance.getValueWritten() == null)
            throw new NotFoundException(String.format("Failed to update instance %s", instanceId));

        return securityGroupName.get();
    }

    protected void enqueueTask(PublicIpAddress publicIpAddressEntity, PiQueue queue, TaskProcessingQueueContinuation continuation) {
        LOG.debug(String.format("enqueueTask(%s, %s)", publicIpAddressEntity, queue));
        String taskUri = QueueTaskUriHelper.getUriForAssociateAddress(publicIpAddressEntity);
        LOG.debug(String.format("Adding elastic ip address task to queue for addr %s to instance %s, with uri %s", publicIpAddressEntity.getIpAddress(), publicIpAddressEntity.getInstanceId(), taskUri));
        int globalAvzCode = getPiIdBuilder().getGlobalAvailabilityZoneCodeFromEc2Id(publicIpAddressEntity.getInstanceId());
        int regionCode = PId.getRegionCodeFromGlobalAvailabilityZoneCode(globalAvzCode);
        PId queueId = getPiIdBuilder().getPId(queue.getUrl()).forRegion(regionCode);
        getTaskProcessingQueueHelper().addUrlToQueue(queueId, taskUri, MAX_QUEUE_RETRIES, continuation);
    }

    protected void sendElasticIpAddressRequestToNetworkManagerNode(PublicIpAddress publicIpAddressEntity, EntityMethod entityMethod) {
        LOG.debug(String.format("sendElasticIpAddressRequestToNetworkManagerNode(%s, %s)", publicIpAddressEntity, entityMethod));

        String secGroupUri = SecurityGroup.getUrl(publicIpAddressEntity.getOwnerId(), publicIpAddressEntity.getSecurityGroupName());
        int publicAddressInstanceGlobalAvzCode = getPiIdBuilder().getGlobalAvailabilityZoneCodeFromEc2Id(publicIpAddressEntity.getInstanceId());
        PId networkManagerAppId = getPiIdBuilder().getPId(secGroupUri).forGlobalAvailablityZoneCode(publicAddressInstanceGlobalAvzCode);
        MessageContext messageContext = messageContextFactory.newMessageContext();
        messageContext.routePiMessageToApplication(networkManagerAppId, entityMethod, publicIpAddressEntity, NetworkManagerApplication.APPLICATION_NAME);
    }

    protected void sendAssociateElasticIpAddressRequestToSecurityGroupNode(PublicIpAddress publicIpAddressEntity) {
        sendElasticIpAddressRequestToNetworkManagerNode(publicIpAddressEntity, EntityMethod.CREATE);
    }

    protected void sendDisassociateElasticIpAddressRequestToSecurityGroupNode(PublicIpAddress publicIpAddressEntity) {
        sendElasticIpAddressRequestToNetworkManagerNode(publicIpAddressEntity, EntityMethod.DELETE);
    }

    protected void validateInstanceForUser(String ownerId, String instanceId) {
        PId userRecordId = getPiIdBuilder().getPId(User.getUrl(ownerId));
        User userRecord = getBlockingDhtCache().getReadThrough(userRecordId);
        PId instancePastryId = getPiIdBuilder().getPIdForEc2AvailabilityZone(Instance.getUrl(instanceId));

        Instance instance = getBlockingDhtCache().getReadThrough(instancePastryId);
        if (userRecord == null)
            throw new UserNotFoundException(String.format("User %s not found", ownerId));

        if (instance == null)
            throw new NotFoundException(String.format("Instance %s not found", instanceId));

        if (!userRecord.hasInstance(instanceId))
            throw new NotFoundException(String.format("Instance %s not found for user %s", instanceId, ownerId));

        if (!instance.getState().equals(InstanceState.RUNNING))
            throw new IllegalArgumentException(String.format("Invalid instance state %s for instance %s, should be running", instance.getState().toString().toLowerCase(Locale.getDefault()), instanceId));
    }
}