text:
import datetime
import logging
import re
import warnings
from typing import Dict, Iterable

from great_expectations.datasource.batch_kwargs_generator.batch_kwargs_generator import (
    BatchKwargsGenerator,
)
from great_expectations.datasource.types import S3BatchKwargs
from great_expectations.exceptions import BatchKwargsError, GreatExpectationsError

try:
    import boto3
except ImportError:
    boto3 = None

logger = logging.getLogger(__name__)

class S3GlobReaderBatchKwargsGenerator(BatchKwargsGenerator):
    r"""
    S3 BatchKwargGenerator provides support for generating batches of data from an S3 bucket. For the S3 batch kwargs generator, assets must
    be individually defined using a prefix and glob, although several additional configuration parameters are available
    for assets (see below).

    Example configuration::

        datasources:
          my_datasource:
            ...
            batch_kwargs_generator:
              my_s3_generator:
                class_name: S3GlobReaderBatchKwargsGenerator
                bucket: my_bucket.my_organization.priv
                reader_method: parquet  # This will be automatically inferred from suffix where possible, but can be explicitly specified as well
                reader_options:  # Note that reader options can be specified globally or per-asset
                  sep: ","
                delimiter: "/"  # Note that this is the delimiter for the BUCKET KEYS. By default it is "/"
                boto3_options:
                  endpoint_url: $S3_ENDPOINT  # Use the S3_ENDPOINT environment variable to determine which endpoint to use
                max_keys: 100  # The maximum number of keys to fetch in a single list_objects request to s3. When accessing batch_kwargs through an iterator, the iterator will silently refetch if more keys were available
                assets:
                  my_first_asset:
                    prefix: my_first_asset/
                    regex_filter: .*  # The regex filter will filter the results returned by S3 for the key and prefix to only those matching the regex
                    directory_assets: True # if True, the contents of the directory will be treated as one batch. Notice that this option does not work with Pandas, since Pandas does not support loading multiple files from and S3 bucket into a data frame.
                  access_logs:
                    prefix: access_logs
                    regex_filter: access_logs/2019.*\.csv.gz
                    sep: "~"
                    max_keys: 100
    """

    recognized_batch_parameters = {
        "data_asset_name",
        "partition_id",
        "reader_method",
        "reader_options",
        "limit",
    }

    # FIXME add tests for new partitioner functionality
    def __init__(
        self,
        name="default",
        datasource=None,
        bucket=None,
        reader_options=None,
        assets=None,
        delimiter="/",
        reader_method=None,
        boto3_options=None,
        max_keys=1000,
    ) -> None:
        """Initialize a new S3GlobReaderBatchKwargsGenerator

        Args:
            name: the name of the batch kwargs generator
            datasource: the datasource to which it is attached
            bucket: the name of the s3 bucket from which it generates batch_kwargs
            reader_options: options passed to the datasource reader method
            assets: asset configuration (see class docstring for more information)
            delimiter: the BUCKET KEY delimiter
            reader_method: the reader_method to include in generated batch_kwargs
            boto3_options: dictionary of key-value pairs to use when creating boto3 client or resource objects
            max_keys: the maximum number of keys to fetch in a single list_objects request to s3
        """
        super().__init__(name, datasource=datasource)
        if reader_options is None:
            reader_options = {}

        if assets is None:
            assets = {"default": {"prefix": "", "regex_filter": ".*"}}

        self._bucket = bucket
        self._reader_method = reader_method
        self._reader_options = reader_options
        self._assets = assets
        self._delimiter = delimiter
        if boto3_options is None:
            boto3_options = {}
        self._max_keys = max_keys
        self._iterators: Dict = {}
        try:
            self._s3 = boto3.client("s3", **boto3_options)
        except TypeError:
            raise (
                ImportError(
                    "Unable to load boto3, which is required for S3 batch kwargs generator"
                )
            )

    @property
    def reader_options(self):
        return self._reader_options

    @property
    def assets(self):
        return self._assets

    @property
    def bucket(self):
        return self._bucket

    def get_available_data_asset_names(self):
        return {"names": [(key, "file") for key in self._assets.keys()]}

    def _get_iterator(
        self, data_asset_name, reader_method=None, reader_options=None, limit=None
    ):
        logger.debug(
            f"Beginning S3GlobReaderBatchKwargsGenerator _get_iterator for data_asset_name: {data_asset_name}"
        )

        if data_asset_name not in self._assets:
            batch_kwargs = {
                "data_asset_name": data_asset_name,
                "reader_method": reader_method,
                "reader_options": reader_options,
                "limit": limit,
            }
            raise BatchKwargsError(
                f"Unknown asset_name {data_asset_name}", batch_kwargs
            )

        if data_asset_name not in self._iterators:
            self._iterators[data_asset_name] = {}

        asset_config = self._assets[data_asset_name]

        return self._build_asset_iterator(
            asset_config=asset_config,
            iterator_dict=self._iterators[data_asset_name],
            reader_method=reader_method,
            reader_options=reader_options,
            limit=limit,
        )

    def _build_batch_kwargs_path_iter(
        self, path_list, reader_options=None, limit=None
    ) -> Iterable[S3BatchKwargs]:
        for path in path_list:
            yield self._build_batch_kwargs(  # type: ignore[call-arg] # no parameters `reader_options` or `limit`
                path, reader_options=reader_options, limit=limit
            )

    def _build_batch_kwargs(self, batch_parameters):
        try:
            data_asset_name = batch_parameters.pop("data_asset_name")
        except KeyError:
            raise BatchKwargsError(
                "Unable to build BatchKwargs: no name provided in batch_parameters.",
                batch_kwargs=batch_parameters,
            )

        partition_id = batch_parameters.pop("partition_id", None)
        batch_kwargs = self._datasource.process_batch_parameters(batch_parameters)

        if partition_id:
            try:
                asset_config = self._assets[data_asset_name]
            except KeyError:
                raise GreatExpectationsError(
                    f"No asset config found for asset {data_asset_name}"
                )
            if data_asset_name not in self._iterators:
                self._iterators[data_asset_name] = {}

            iterator_dict = self._iterators[data_asset_name]
            for key in self._get_asset_options(asset_config, iterator_dict):
                if (
                    self._partitioner(key=key, asset_config=asset_config)
                    == partition_id
                ):
                    batch_kwargs = self._build_batch_kwargs_from_key(
                        key=key,
                        asset_config=asset_config,
                        reader_options=batch_parameters.get(
                            "reader_options"
                        ),  # handled in generator
                        limit=batch_kwargs.get(
                            "limit"
                        ),  # may have been processed from datasource
                    )

            if batch_kwargs is None:
                raise BatchKwargsError(
                    f"Unable to identify partition {partition_id} for asset {data_asset_name}",
                    {data_asset_name: data_asset_name, partition_id: partition_id},
                )

            return batch_kwargs

        else:
            return self.yield_batch_kwargs(
                data_asset_name=data_asset_name, **batch_parameters, **batch_kwargs
            )

    def _build_batch_kwargs_from_key(
        self,
        key,
        asset_config=None,
        reader_method=None,
        reader_options=None,
        limit=None,
    ) -> S3BatchKwargs:
        batch_kwargs = {
            "s3": f"s3a://{self.bucket}/{key}",
            "reader_options": self.reader_options,
        }
        if asset_config.get("reader_options"):
            batch_kwargs["reader_options"].update(asset_config.get("reader_options"))
        if reader_options is not None:
            batch_kwargs["reader_options"].update(reader_options)

        if self._reader_method is not None:
            batch_kwargs["reader_method"] = self._reader_method
        if asset_config.get("reader_method"):
            batch_kwargs["reader_method"] = asset_config.get("reader_method")
        if reader_method is not None:
            batch_kwargs["reader_method"] = reader_method

        if limit:
            batch_kwargs["limit"] = limit

        return S3BatchKwargs(batch_kwargs)

    def _get_asset_options(self, asset_config, iterator_dict) -> Iterable[str]:
        query_options = {
            "Bucket": self.bucket,
            "Delimiter": asset_config.get("delimiter", self._delimiter),
            "Prefix": asset_config.get("prefix", None),
            "MaxKeys": asset_config.get("max_keys", self._max_keys),
        }
        directory_assets = asset_config.get("directory_assets", False)

        if "continuation_token" in iterator_dict:
            query_options.update(
                {"ContinuationToken": iterator_dict["continuation_token"]}
            )

        logger.debug(
            f"Fetching objects from S3 with query options: {str(query_options)}"
        )
        asset_options = self._s3.list_objects_v2(**query_options)
        if directory_assets:
            if "CommonPrefixes" not in asset_options:
                raise BatchKwargsError(
                    "Unable to build batch_kwargs. The asset may not be configured correctly. If directory assets "
                    "are requested, then common prefixes must be returned.",
                    {
                        "asset_configuration": asset_config,
                        "contents": asset_options["Contents"]
                        if "Contents" in asset_options
                        else None,
                    },
                )
            keys = [item["Prefix"] for item in asset_options["CommonPrefixes"]]
        else:
            if "Contents" not in asset_options:
                raise BatchKwargsError(
                    "Unable to build batch_kwargs. The asset may not be configured correctly. If s3 returned common "
                    "prefixes it may not have been able to identify desired keys, and they are included in the "
                    "incomplete batch_kwargs object returned with this error.",
                    {
                        "asset_configuration": asset_config,
                        "common_prefixes": asset_options["CommonPrefixes"]
                        if "CommonPrefixes" in asset_options
                        else None,
                    },
                )
            keys = [
                item["Key"] for item in asset_options["Contents"] if item["Size"] > 0
            ]

        keys = [
            key
            for key in filter(
                lambda x: re.match(asset_config.get("regex_filter", ".*"), x)
                is not None,
                keys,
            )
        ]
        yield from keys

        if asset_options["IsTruncated"]:
            iterator_dict["continuation_token"] = asset_options["NextContinuationToken"]
            # Recursively fetch more
            yield from self._get_asset_options(asset_config, iterator_dict)
        elif "continuation_token" in iterator_dict:
            # Make sure we clear the token once we've gotten fully through
            del iterator_dict["continuation_token"]

    def _build_asset_iterator(
        self,
        asset_config,
        iterator_dict,
        reader_method=None,
        reader_options=None,
        limit=None,
    ) -> Iterable[S3BatchKwargs]:
        for key in self._get_asset_options(asset_config, iterator_dict):
            yield self._build_batch_kwargs_from_key(
                key,
                asset_config,
                reader_method=None,
                reader_options=reader_options,
                limit=limit,
            )

    # TODO: deprecate generator_asset argument
    def get_available_partition_ids(self, generator_asset=None, data_asset_name=None):
        assert (generator_asset and not data_asset_name) or (
            not generator_asset and data_asset_name
        ), "Please provide either generator_asset or data_asset_name."
        if generator_asset:
            # deprecated-v0.11.0
            warnings.warn(
                "The 'generator_asset' argument is deprecated as of v0.11.0 and will be removed in v0.16. "
                "Please use 'data_asset_name' instead.",
                DeprecationWarning,
            )
            data_asset_name = generator_asset

        if data_asset_name not in self._iterators:
            self._iterators[data_asset_name] = {}
        iterator_dict = self._iterators[data_asset_name]
        asset_config = self._assets[data_asset_name]
        available_ids = [
            self._partitioner(key=key, asset_config=asset_config)
            for key in self._get_asset_options(asset_config, iterator_dict)
        ]
        return available_ids

    def _partitioner(self, key, asset_config):
        if "partition_regex" in asset_config:
            match_group_id = asset_config.get("match_group_id", 1)
            matches = re.match(asset_config["partition_regex"], key)
            # In the case that there is a defined regex, the user *wanted* a partition. But it didn't match.
            # So, we'll add a *sortable* id
            if matches is None:
                logger.warning(f"No match found for key: {key}")
                return (
                    datetime.datetime.now(datetime.timezone.utc).strftime(
                        "%Y%m%dT%H%M%S.%fZ"
                    )
                    + "__unmatched"
                )
            else:
                try:
                    return matches.group(match_group_id)
                except IndexError:
                    logger.warning(f"No match group {match_group_id} in key {key}")
                    return (
                        datetime.datetime.now(datetime.timezone.utc).strftime(
                            "%Y%m%dT%H%M%S.%fZ"
                        )
                        + "__no_match_group"
                    )

        # If there is no partitioner defined, fall back on using the path as a partition_id
        else:
            prefix = asset_config.get("prefix", "")
            if key.startswith(prefix):
                key = key[len(prefix) :]
            return key


text:
'use strict';

/**
 * @ngdoc directive
 * @name izzyposWebApp.directive:adminPosHeader
 * @description
 * # adminPosHeader
 */
angular.module('CarreEntrySystem')
	.directive('carreFooter',function(){
		return {
        templateUrl:'app/components/footer/footer.html',
        restrict: 'E',
        replace: true
    	}
	});

redpajama_set_name: RedPajamaGithub


text:
import defaultStates from '../../consts/default_states';
import { MODAL } from '../../consts/types';
import reduce from '../modal';

const defaultState = defaultStates.modal;

describe('[Reducer] modal', () => {
  it('should return the default state', () => {
    const newState = reduce(defaultState, {});
    expect(newState).toEqual(defaultState);
  });
  it('should handle OPEN', () => {
    const action = {
      type: MODAL.OPEN,
      item: {
        type: 'rant',
        id: 1,
        data: {
          test: 'test',
        },
      },
    };

    const newState = reduce(defaultState, action);
    const expected = {
      item: {
        type: 'rant',
        id: 1,
        data: {
          test: 'test',
        },
      },
    };

    expect(newState).toEqual(expected);
  });
  it('should handle CLOSE', () => {
    const action = {
      type: MODAL.CLOSE,
    };

    const newState = reduce(defaultState, action);
    const expected = {
      item: null,
    };

    expect(newState).toEqual(expected);
  });
});


text:
All Notable changes to Laravel-Analytics will be documented in this file

## 2.2.0 - 2015-07-23
- added config setting to specify cache path

## 2.1.0 - 2015-06-22
- added `pageTitle` to `fetchVisitorsAndPageViews` and `fetchMostVisitedPages`
- fixed `credentialsJsonDoesNotExist` exception

## 2.0.0 - 2015-06-20

- refactored all methods
- introduced `Spatie\Analytics\Period` to specify date ranges
- the package now uses json credentials instead of .p12 file
- the `Spatie\Analytics\Analtyics` class is much easier to extend

## 1.4.1 - 2015-05-12

- fixes a bug introduced in 1.4.0 where the check whether a p12 certificate is present was broken

## 1.4.0 - 2015-05-11

**This version does not work, please upgrade to 1.4.0**

- removing the use of facades

## 1.3.1
- make `calculateNumberOfDays`-function protected

## 1.3.0
- Added method getSiteId()

## 1.2.3
- Fix PHP 5.4 compatibility

## 1.2.2
- An injected `Spatie\LaravelAnalytics\LaravelAnalytics`-object will now be properly configured

## 1.2.1
- Removed a var_dump-call that was not supposed to be there

## 1.2.0
###Do not use this version as it contains a var_dump that will mess up your output
- Added a method to set the siteId at runtime

## 1.1.5
- Store Google Api's cache in Laravel's storage directory

## 1.1.4
- Handled a breaking change caused by Laravel 5.1

## 1.1.3
- Moved the repo

## 1.1.2
- Corrected a bug that caused an error when using the realtime-cache

## 1.1.1
- Corrected a bug that caused the realtime-cache to be stored for too long

## 1.1.0
- Added support for the [Real Time Reporting API](https://developers.google.com/analytics/devguides/reporting/realtime/v3/)

## 1.0.4
- Lowered minimum required Carbon version from 1.17 to ~1.0

## 1.0.3
- Fix the breaking error in the Api Helper.

## 1.0.2 (do not use this version)
- Lowered minimum required PHP version to 5.4
- This version is not working due to a bug in the Api Helper.

## 1.0.1
- Use Laraval 5 contracts to typehint cache
- Add DateTime type hints

## 1.0.0
- Stable first release

## 0.0.1
- Experimental initial release


text:
package com.utility.generator.clazz.part;

public class GeneratedImport {

	private String qualifiedName;
	private boolean staticImport = false;

	public GeneratedImport(Class<?> clazz) {
		this.qualifiedName = clazz.getPackage().getName() + "." + clazz.getSimpleName();
	}

	public GeneratedImport(Class<?> clazz, boolean staticImport) {
		this.qualifiedName = clazz.getPackage().getName() + "." + clazz.getSimpleName();
		this.staticImport = staticImport;
	}

	public GeneratedImport(String qualifiedName, boolean staticImport) {
		this.qualifiedName = qualifiedName;
		this.staticImport = staticImport;
	}

	public String qualifiedName() {
		return qualifiedName;
	}

	public boolean isStatic() {
		return staticImport;
	}

}


text:
package nz.co.doltech.gwtls.client.options;

import com.google.gwt.core.client.JsArray;

public class Responsive extends JsArray<Breakpoint> {

    protected Responsive() {}

    public static Responsive create() {
        return JsArray.createArray().cast();
    }
}


text:
package com.artemis.managers;

import com.artemis.Entity;
import com.artemis.Manager;
import com.artemis.utils.Bag;
import com.artemis.utils.ImmutableBag;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.ObjectSet;

/**
 * If you need to group your entities together, e.g. tanks going into "units" group or explosions into "effects",
 * then use this manager. You must retrieve it using world instance.
 *
 * A entity can be assigned to more than one group.
 *
 * @author Arni Arent
 *
 */
public class GroupManager extends Manager {
	private ObjectMap<String, Bag<Entity>> entitiesByGroup;
	private ObjectMap<Entity, Bag<String>> groupsByEntity;

	public GroupManager() {
		entitiesByGroup = new ObjectMap<String, Bag<Entity>>();
		groupsByEntity = new ObjectMap<Entity, Bag<String>>();
	}

	@Override
	protected void initialize() {
	}

	/**
	 * Set the group of the entity.
	 *
	 * @param group group to add the entity into.
	 * @param e entity to add into the group.
	 */
	public void add(Entity e, String group) {
		Bag<Entity> entities;
		if (!entitiesByGroup.containsKey(group))
		{
			entities = new Bag<Entity>();
			entitiesByGroup.put(group, entities);
		}
		else
		{
			entities = entitiesByGroup.get(group);
		}
		entities.add(e);

		Bag<String> groups;
		if (!groupsByEntity.containsKey(e))
		{
			groups = new Bag<String>();
			groupsByEntity.put(e, groups);
		}
		else
		{
			groups = groupsByEntity.get(e);
		}
		groups.add(group);
	}

	/**
	 * Remove the entity from the specified group.
	 * @param e
	 * @param group
	 */
	public void remove(Entity e, String group) {
		if (entitiesByGroup.containsKey(group)){
			Bag<Entity> entities = entitiesByGroup.get(group);
			entities.remove(e);
		}

		if (groupsByEntity.containsKey(e)){
			Bag<String> groups = groupsByEntity.get(e);
			groups.remove(group);
		}
	}

	public void removeFromAllGroups(Entity e) {
		if (groupsByEntity.containsKey(e)){
			Bag<String> groups = groupsByEntity.get(e);
			for(int i = 0; groups.size() > i; i++) {
				Bag<Entity> entities = entitiesByGroup.get(groups.get(i));
				if(entities != null) {
					entities.remove(e);
				}
			}
			groupsByEntity.remove(e);
		}
	}

	/**
	 * Get all entities that belong to the provided group.
	 * @param group name of the group.
	 * @return read-only bag of entities belonging to the group.
	 */
	public ImmutableBag<Entity> getEntities(String group) {
		if (!entitiesByGroup.containsKey(group)){
			entitiesByGroup.put(group, new Bag<Entity>());
		}
		return entitiesByGroup.get(group);
	}

	/**
	 * Get all entities that only belong to the provided group
	 * @param group
	 * @return
	 */
	public ImmutableBag<Entity> getExclusiveEntities(String group) {
		ImmutableBag<Entity> entities = getEntities(group);

		Bag<Entity> bag = new Bag<Entity>();
		//check for collisions
		for (int i = 0; i < entities.size(); i++)
		{
			Entity e = entities.get(i);
			if (getGroups(e).size() == 1){
				bag.add(e);
			}
		}

		return bag;

	}

	/**
	 * Get all entities that belong to all the provided group.
	 * @param group name of the group.
	 * @return read-only bag of entities belonging to the group.
	 */
	public ImmutableBag<Entity> getEntities(String... groups) {
		ObjectSet<Entity> collisions = new ObjectSet<Entity>();

		//get initial set
		{
			String s = groups[0];
			ImmutableBag<Entity> entities = getEntities(s);

			for (int i = 0; i < entities.size(); i++)
			{
				Entity e = entities.get(i);
				collisions.add(e);
			}
		}

		for (int i = 1; i < groups.length; i++)
		{
			ObjectSet<Entity> c = new ObjectSet<Entity>();
			String s = groups[i];
			ImmutableBag<Entity> entities = getEntities(s);

			for (int n = 0; n < entities.size(); n++)
			{
				Entity e = entities.get(n);
				if (collisions.contains(e))
					c.add(e);
			}

			collisions = c;
		}

		Bag<Entity> e = new Bag<Entity>();
		e.addAll(collisions.iterator().toArray());

		return e;
	}

	/**
	 * @param e entity
	 * @return the groups the entity belongs to, null if none.
	 */
	public ImmutableBag<String> getGroups(Entity e) {
		return groupsByEntity.get(e);
	}

	/**
	 * Checks if the entity belongs to any group.
	 * @param e the entity to check.
	 * @return true if it is in any group, false if none.
	 */
	public boolean isInAnyGroup(Entity e) {
		return getGroups(e) != null;
	}

	/**
	 * Check if the entity is in the supplied group.
	 * @param group the group to check in.
	 * @param e the entity to check for.
	 * @return true if the entity is in the supplied group, false if not.
	 */
	public boolean isInGroup(Entity e, String group) {
		Bag<String> groups = groupsByEntity.get(e);
		return groups.contains(group);
	}

	@Override
	public void deleted(Entity e) {
		removeFromAllGroups(e);
	}

}


text:
<?php
  require_once 'assets/php/initialize.php';
  $full_name = $_SESSION['user_data']['first_name'] . ' ' . $_SESSION['user_data']['last_name'];
?>

<?php if (isset($full_name)) { echo $full_name; } ?>

         Online

MAIN NAVIGATION

 Startpagina

           Profiel

 Profiel

 Instellingen


text:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.microrisc.simply</groupId>
        <artifactId>simply-iqrf-dpa30x</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <artifactId>simply-iqrf-dpa-v30x-devices</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0-SNAPSHOT</version>

    <modules>
        <module>simply-devices-protronix-dpa30x</module>
    </modules>
</project>


text:
document.write('<script type="text/javascript" src="/janitor/admin/js/lib/seg_desktop_include.js"></script>');

document.write('<script type="text/javascript" src="/janitor/js/lib/desktop/m-form.js"></script>');
document.write('<script type="text/javascript" src="/janitor/js/lib/desktop/m-device.js"></script>');
document.write('<script type="text/javascript" src="/janitor/js/lib/desktop/m-mainetance.js"></script>');
document.write('<script type="text/javascript" src="/janitor/js/lib/desktop/m-unidentified.js"></script>');


text:
package commands

import (
	"bufio"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"github.com/github/hub/github"
	"github.com/github/hub/utils"
)

type listFlag []string

func (l *listFlag) String() string {
	return strings.Join([]string(*l), ",")
}

func (l *listFlag) Set(value string) error {
	for _, flag := range strings.Split(value, ",") {
		*l = append(*l, flag)
	}
	return nil
}

func isDir(file string) bool {
	f, err := os.Open(file)
	if err != nil {
		return false
	}
	defer f.Close()

	fi, err := f.Stat()
	if err != nil {
		return false
	}

	return fi.IsDir()
}

func gitRemoteForProject(project *github.Project) (foundRemote *github.Remote) {
	remotes, err := github.Remotes()
	utils.Check(err)
	for _, remote := range remotes {
		remoteProject, pErr := remote.Project()
		if pErr == nil && remoteProject.SameAs(project) {
			foundRemote = &remote
			return
		}
	}

	return nil
}

func isEmptyDir(path string) bool {
	fullPath := filepath.Join(path, "*")
	match, _ := filepath.Glob(fullPath)
	return match == nil
}

func getTitleAndBodyFromFlags(messageFlag, fileFlag string) (title, body string, err error) {
	if messageFlag != "" {
		title, body = readMsg(messageFlag)
	} else if fileFlag != "" {
		var (
			content []byte
			err     error
		)

		if fileFlag == "-" {
			content, err = ioutil.ReadAll(os.Stdin)
		} else {
			content, err = ioutil.ReadFile(fileFlag)
		}
		utils.Check(err)

		title, body = readMsg(string(content))
	}

	return
}

func readMsg(msg string) (title, body string) {
	s := bufio.NewScanner(strings.NewReader(msg))
	if s.Scan() {
		title = s.Text()
		body = strings.TrimLeft(msg, title)

		title = strings.TrimSpace(title)
		body = strings.TrimSpace(body)
	}

	return
}

func runInLocalRepo(fn func(localRepo *github.GitHubRepo, project *github.Project, client *github.Client)) {
	localRepo, err := github.LocalRepo()
	utils.Check(err)

	project, err := localRepo.CurrentProject()
	utils.Check(err)

	client := github.NewClient(project.Host)
	fn(localRepo, project, client)

	os.Exit(0)
}


text:
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import './component/minimap.css';

class Minimap extends Component {
    constructor(props) {
        super(props);
        this.state = {
            mousedown: false
        };
    }
    componentWillMount() {
        let minimapElement = document.getElementsByClassName('minimap-treeview')[0];
        let hasRegionBody = document.getElementsByClassName('miniregion')[0];
        if (typeof (minimapElement) !== 'undefined' && minimapElement !== null) {
            minimapElement.parentNode.removeChild(minimapElement);
        }
        if (typeof (hasRegionBody) !== 'undefined' && hasRegionBody !== null) {
            hasRegionBody.parentNode.removeChild(hasRegionBody);
        }
    }
    componentDidMount() {
        const miniElement = document.getElementById(this.props.rootId);
        if (typeof (miniElement) === 'undefined' && miniElement === null)
            return false;
        miniElement.className = "react-minimap-component";
        let minimapElement = document.createElement('div');
        minimapElement.className = 'minimap-treeview';
        document.body.appendChild(minimapElement);
        let cloneDiv = miniElement.cloneNode(true);
        cloneDiv.className = "minimap noselect " + this.props.rootId;
        let miniregion = document.createElement('div');
        miniregion.className = 'miniregion';
        document.body.appendChild(miniregion);
        minimapElement.appendChild(cloneDiv);
        let mElement = document.getElementsByClassName('minimap')[0];
        let minimapOveraly = document.createElement('div');
        minimapOveraly.className = 'minimap-overlay';
        mElement.appendChild(minimapOveraly);
        this.onResizeHandler(mElement);
        window.addEventListener('resize', this.onResizeHandler.bind(this, miniElement), false);
        window.addEventListener('scroll', this.onScrollHandler.bind(this), false);
        miniregion.addEventListener('mousedown', this.onMouseDownHandler.bind(this), false);
        miniregion.addEventListener('mouseup', this.onMouseUpHandler.bind(this), false);
        miniregion.addEventListener('mousemove', this.onMouseMoveHandler.bind(this), false);
        miniregion.addEventListener('click', this.onClickHandler.bind(this), false);
        mElement.addEventListener('mousedown', this.onMouseDownHandler.bind(this), false);
        mElement.addEventListener('mouseup', this.onMouseUpHandler.bind(this), false);
        mElement.addEventListener('mousemove', this.onMouseMoveHandler.bind(this), false);
        mElement.addEventListener('click', this.onClickHandler.bind(this), false);
        minimapElement.addEventListener('click', this.onClickHandler.bind(this), false);
    }
    componentDidUpdate() {
        let hasClassBody = document.getElementsByClassName('minimap')[0];
        let hasRegionBody = document.getElementsByClassName('miniregion')[0];
        hasRegionBody.parentNode.removeChild(hasRegionBody);
        hasClassBody.parentNode.removeChild(hasClassBody);
        const miniElement = document.getElementById(this.props.rootId);
        let minimapElement = document.getElementsByClassName('minimap-treeview')[0];
        let cloneDiv = miniElement.cloneNode(miniElement);
        cloneDiv.className = 'minimap noselect ' + this.props.rootId;
        let miniregion = document.createElement('div');
        miniregion.className = 'miniregion';
        document.body.appendChild(miniregion);
        minimapElement.appendChild(cloneDiv);
        let mElement = document.getElementsByClassName('minimap')[0];
        let minimapOveraly = document.createElement('div');
        minimapOveraly.className = 'minimap-overlay';
        mElement.appendChild(minimapOveraly);
        this.onResizeHandler(this);
        mElement.addEventListener('mousemove', this.onMouseMoveHandler.bind(this));
        mElement.addEventListener('click', this.onClickHandler.bind(this), false);
        miniregion.addEventListener('mousedown', this.onMouseDownHandler.bind(this), false);
        miniregion.addEventListener('mouseup', this.onMouseUpHandler.bind(this), false);
        miniregion.addEventListener('mousemove', this.onMouseMoveHandler.bind(this), false);
        miniregion.addEventListener('click', this.onClickHandler.bind(this), false);
    }
    onResizeHandler(e) {
        const {offsetHeightRatio, offsetWidthRatio, widthRatio, heightRatio, style, position, toggle} = this.props;
        if (!toggle)
            return false;
        const minimap = document.getElementsByClassName('minimap')[0];
        const minimapOverlay = document.getElementsByClassName('minimap-overlay')[0];
        const minimapTree = document.getElementsByClassName('minimap-treeview')[0];
        let scale = this.scale();
        console.log(scale);
        let animation, width, height, top, leftRight, styles,
                offsetHeight = document.documentElement.clientHeight * offsetHeightRatio,
                offsetWidth = document.documentElement.clientWidth * offsetWidthRatio;
        width = document.documentElement.clientWidth * (1 / scale.x) * widthRatio;
        height = document.documentElement * (1 / scale.y) * heightRatio;
        top = minimap.clientHeight * (scale.y - 1) / 2 + offsetHeight;
        leftRight = minimap.clientWidth * (scale.x - 1) / 2 + offsetWidth;
        animation = 'scale3d(' + scale.x + ',' + scale.y + ',1)';
        styles = {
            '-webkit-transform': animation,
            '-moz-transform': animation,
            '-ms-transform': animation,
            '-o-transform': animation,
            transforn: animation,
            width: width + 'px',
            height: height + 'px',
            margin: '0px',
            padding: '0px',
        };
        Object.assign(minimap.style, this._mergeStyles(styles, style));
        Object.assign(minimapOverlay.style, {height: height + 'px', width: '475px'});
        let treeStyle = {height: document.documentElement.clientHeight+ 'px'};
        treeStyle[position] = '5px';
        Object.assign(minimapTree.style, this._mergeStyles(treeStyle, style));
        this._miniRegion(e);
    }
    onScrollHandler() {
        let minimap = document.getElementsByClassName('minimap')[0];
        let minregion = document.getElementsByClassName('miniregion')[0];
        const {offsetHeightRatio} = this.props;
        minimap = minimap.getBoundingClientRect();
        let scale = this.scale();
        let offsetHeight = window.innerHeight * offsetHeightRatio;
        let offsetTop = minimap.top * scale.y;
        let offsetPosition = (window.pageYOffset) * scale.y;
        let regionHeight = minregion.offsetHeight;
        let regionBottom = minimap.offsetHeight * scale.y + offsetTop;

        if (offsetPosition + regionHeight + offsetHeight < offsetTop || offsetPosition > regionBottom) {
            Object.assign(minregion.style, {display: 'none'});
        } else {
            Object.assign(minregion.style, {top: offsetPosition + offsetHeight - offsetTop + 'px', display: 'block'});
        }
    }
    onClickHandler(e) {
        this._scrollTop(e);
        this.setState({mousedown: false});
    }
    onMouseMoveHandler(e) {
        const mouseDown = this.state.mousedown;
        if (!mouseDown) {
            this.setState({mousedown: false});
            return false;
        }
        this._scrollTop(e);
    }
    onMouseDownHandler(e) {
        this.setState({mousedown: true});
        let region = document.getElementsByClassName('miniregion')[0];
        let minimap = document.getElementsByClassName('minimap')[0];
        this.addClass(region, 'dragging');
        this.addClass(minimap, 'noselect');
    }
    onMouseUpHandler(e) {
        let region = document.getElementsByClassName('miniregion')[0];
        let minimap = document.getElementsByClassName('minimap')[0];
        this.removeClass(region, 'dragging');
        this.removeClass(minimap, 'noselect');
        this.setState({mousedown: false});
    }
    scale() {
        const {widthRatio, heightRatio} = this.props;
        let minimap = document.getElementsByClassName('minimap')[0];
        if(minimap.offsetHeight <= 7000){
            let heightRatio = 0.1;
            let widthRatio = 0.04;
        return {
            x: (document.getElementById(this.props.rootId).clientWidth / minimap.clientWidth) * widthRatio,
            y: (document.getElementById(this.props.rootId).clientHeight / minimap.clientHeight) * heightRatio
        };
    }else{
        let heightRatio = 0.95;
        let widthRatio = 0.05;
        return {
            x: (document.documentElement.clientWidth / minimap.clientWidth) * widthRatio ,
            y: (document.documentElement.clientHeight / minimap.clientHeight) * heightRatio
        };
    }
    }
    onScrollChecker(element) {
        return document.getElementById(element).scrollHeight > document.getElementById(element).clientHeight;
    }
    _mergeStyles(...args) {
        return Object.assign({}, ...args);
    }
    _miniRegion() {
        const {offsetHeightRatio, offsetWidthRatio, position, style} = this.props;
        const minimap = document.getElementsByClassName('minimap')[0].getBoundingClientRect();
        let scale = this.scale();
        let regionTop = minimap.top * scale.y;
        let offsetWidth = document.documentElement.clientWidth * offsetWidthRatio;
        let width = document.getElementsByClassName('minimap-treeview')[0].clientWidth + 5;

        let styles = {
            width: width + 'px',
            height: window.innerHeight * scale.y + 'px',
            margin: '0px',
            top: window.pageYOffset * scale.y + document.documentElement.clientHeight * offsetHeightRatio - regionTop + 'px',
            display: 'block'
        };
        styles[position] = offsetWidth + 'px';
        Object.assign(document.getElementsByClassName('miniregion')[0].style, this._mergeStyles(styles, style));
    }
    _scrollTop(e) {
        const {offsetHeightRatio, toggle, smoothScrollDelay, smoothScroll} = this.props;
        if (!toggle)
            return false;
        const minimap = document.getElementsByClassName('minimap')[0].getBoundingClientRect();
        let scale = this.scale();
        let offsetHeight = document.documentElement.clientHeight * offsetHeightRatio;
        let offsetTop = minimap.top * scale.y;
        let regionHeight = document.getElementsByClassName('miniregion')[0].offsetHeight;
        let target = (e.clientY - regionHeight / 2 - offsetHeight + offsetTop) / scale.y;
        if (e.type === 'click' && smoothScroll) {
            let current = window.pageYOffset;
            let maxTarget = document.getElementsByClassName('minimap')[0].offsetHeight;
            target = Math.max(target, Math.min(target, maxTarget));
            let direction = target > current;
            let delay = smoothScrollDelay;
            let distance = Math.abs(current - target);
            let r = delay / distance;
            let unitScroll = 1;
            let unitDelay = 4;

            if (r >= 4) {
                unitDelay = parseInt(unitScroll, 15);
            } else if (r >= 1) {
                unitScroll = parseInt(r, 15) * 4;
            } else {
                unitScroll = parseInt((4 / r), 15);
            }
            let next = current;
            let count = parseInt(distance / unitScroll, 8);
            var smoothScrolling = () => {
                next = next + (direction ? unitScroll : -unitScroll);
                if (--count <= 0) {
                    clearInterval(timer);
                    next = target;
                }
                console.log(next);
                window.scrollTo(0, next);
            };
            let timer = window.setInterval(smoothScrolling, unitDelay);
        } else {
            window.scrollTo(0, target);
        }
        e.stopPropagation();
    }

    addClass(el, className) {
        if (el.className)
            el.classList.add(className);
        else if (!this.hasClass(el, className))
            el.className += " " + className;

    }
    hasClass(el, className) {
        if (el.classList)
            return el.classList.contains(className);
        else
            return !!el.className.match(new RegExp('(\\b' + className + '(\\b)'));
    }
    removeClass(el, className) {
        if (el.classList)
            el.classList.remove(className);
        else if (this.hasClass(el, className)) {
            let reg = new RegExp('(\\b' + className + '(\\b)');
            el.className = el.className.replace(reg, ' ');
        }
    }
    render() {
        return

;
    }
}
Minimap.defaultProps = {
    heightRatio: 0.95,
    widthRatio: 0.05,
    offsetHeightRatio : 0.01,
    offsetWidthRatio : 0.002,
    position: 'right',
    smoothScroll: true,
    smoothScrollDelay: 200,
    toggle: true
};
Minimap.propTypes = {
    heightRatio: PropTypes.number,
    widthRatio: PropTypes.number,
    offsetHeightRatio: PropTypes.number,
    offsetWidthRatio: PropTypes.number,
    position: PropTypes.string,
    smoothScroll: PropTypes.bool,
    smoothScrollDelay: PropTypes.number,
    toggle: PropTypes.bool,
    rootId: PropTypes.string.isRequired
};
export default Minimap;


text:
package gov.sandia.cognition.statistics.method;

import gov.sandia.cognition.annotation.PublicationReference;
import gov.sandia.cognition.statistics.ClosedFormDistribution;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Describes the assumptions and other information of a statistical confidence
 * test.
 * @author Kevin R. Dixon
 * @since 3.0
 */
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target(ElementType.TYPE)
public @interface ConfidenceTestAssumptions
{

    /**
     * Most-common name of the confidence test.
     * @return
     * Most-common name of the confidence test.
     */
    String name();

    /**
     * High-level description of the confidence test.
     * @return
     * High-level description of the confidence test.
     */
    String[] description();

    /**
     * Other names that the test may be known as.
     * @return
     * Other names that the test may be known as.
     */
    String[] alsoKnownAs() default "";

    /**
     * Description of the null hypothesis.
     * @return
     * Description of the null hypothesis.
     */
    String nullHypothesis();

    /**
     * Distribution used to evaluate the null hypothesis probability.
     * @return
     * Distribution used to evaluate the null hypothesis probability.
     */
    Class<? extends ClosedFormDistribution<?>> distribution();

    /**
     * List of assumptions of the confidence test.
     * @return
     * List of assumptions of the confidence test.
     */
    String[] assumptions();

    /**
     * Flag if the data must be the same size.
     * @return
     * Flag if the data must be the same size.
     */
    boolean dataSameSize();

    /**
     * Flag is the data are paired together during the test.
     * @return
     * Flag if the data are paired together during the test.
     */
    boolean dataPaired();

    /**
     * A reference to a publication providing a basis for the confidence test.
     *
     * @return
     * A reference to a publication providing a basis for the confidence test.
     */
    PublicationReference[] reference();

}


text:
using System;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Steward.Ai.Salesforce.Model;
using Steward.Ai.ThomsonReuters.Model;
using Steward.Ai.Watson.Conversation;
using Steward.Configuration;
using Steward.Dialogs;
using Steward.Helper;
using Steward.Logging;
using Steward.Service;

namespace Steward.Actor
{
    internal class OwnerServiceActor : BotDialogActor, IActor
    {
        private readonly IConversationData conversationData;
        private const string RequestDataName = "requestData";
        private const string AppOwnerServiceEndPointName = "AppOwner.Endpoint";

        private readonly ISettings settings;
        private readonly ILog log;

        internal OwnerServiceActor(IStewardDialogContext context, IConversationData conversationData, ISettings settings, ILog log) : base(context)
        {
            this.conversationData = conversationData;
            this.settings = settings;
            this.log = log;
        }

        async Task IActor.Execute()
        {
            using (var client = ServiceResolver.Get<IWebClient>())
            {
                dynamic data = conversationData[RequestDataName];

                string appName = data.appName;

                await PostAsync("Finding owners of " + appName);

                var endpoint = GetEndPoint(data);

                client.Headers.Add("Content-Type", "application/json");
                client.Headers.Add("Accept", "application/json");

                try
                {
                    string responseMessage = await client.DownloadStringTaskAsync(endpoint);

                    log.Debug($"responseMessage : {responseMessage}");

                    if (string.IsNullOrWhiteSpace(responseMessage))
                    {
                        await PostAsync($"{appName} is not available!");

                        return;
                    }

                    var owners = JsonConvert.DeserializeObject<AppOwner[]>(responseMessage);

                    await PostAsync($"Owners for {appName}");

                    foreach (var owner in owners)
                    {
                        var replyMessage = string.Empty;
                        replyMessage += $"**Name : **\n\n{owner.Name}\n\n\n\n";
                        replyMessage += $"** Email :**\n\n{owner.Email}\n\n\n\n";
                        await PostAsync(replyMessage);
                    }

                }
                catch (WebException webExc)
                {
                    log.Error(webExc.StackTrace);

                    await PostAsync("Sorry! There is something wrong at the moment. Please try again later.");
                }
            }
        }

        private Uri GetEndPoint(dynamic data)
        {
            return new UriBuilder(settings[AppOwnerServiceEndPointName] + "/" + data.appName).Uri;
        }
    }
}


text:
(function ($) {
    $.fn.CaptionValueEditor = function (options) {

        var settings = {
            editLinkText: 'edit',
            editImageUrl: '~/images/edit.png',
            captionHeaderText: 'Caption',
            valueHeaderText: 'Value',
            addButtonText: 'add',
            addImageUrl: '~/images/add.gif',
            removeButtonText: 'remove',
            removeImageUrl: '~/images/delete.gif',
            saveButtonText: 'save',
            cancelButtonText: 'cancel',
            title: 'Edit',
            dialogClass: 'dnnFormPopup dnnClear',
            modal: true,
            resizable: true,
            width: 420,
            height: 360
        };

        if (options) {
            $.extend(settings, options);
        }

        var input = this;
        var dialog;

        input.parent().append('');
        input.parent().append('

');

        // in tabelle wandeln
        $("#editCaptions").click(function () {
            dialog = $("#cveditor").dialog(settings);
            $("#popupCaptions").text("");
            $("#popupCaptions").append('

'
                + '

'
                + '

' + settings.saveButtonText + '
'
                + '
' + settings.cancelButtonText + '

');

            $("#inputTbl > thead").append('
	' + settings.captionHeaderText + '	' + settings.valueHeaderText + '
');
            $("#inputTbl > tbody").remove();
            $("#inputTbl").append("

");

            if (input.val().indexOf(';') != -1 || $.trim(input.val()).length > 0) {
                var splits = input.val().split(";");

                for (var i = 0; i < splits.length; i++) {
                    var newLine = "";
                    if (splits[i].indexOf('|') != -1) {
                        var kvp = splits[i].split('|');
                        newLine = getRow(kvp[0], kvp[1], false);
                    }
                    else {
                        if ($.trim(splits[i]).length > 0) {
                            newLine = getRow("", splits[i], false);
                        }
                    }

                    $("#inputTbl > tbody:last").append(newLine);
                }
            }
            $("#inputTbl > tbody:last").append(getRow("", "", true));
//            $("#inputTbl tr:odd").addClass("dnnGridAltItem");
//            $("#inputTbl tr:even").addClass("dnnGridItem");
            $("#popupCaptions").show();
        });

        // tabelle auslesen
        $("#cveditor").on('click', '#parse', function () {
            var array = new Array();

            $("#inputTbl > tbody tr").each(function () {
                var value = $(this).find(':text');

                var first = $.trim(value.first().val());
                var last = $.trim(value.last().val());

                if (first.length > 0 && last.length > 0) {
                    if (first == last) {
                        array.push(first);
                    } else {
                        array.push(first + "|" + last);
                    }
                } else if (last.length > 0) {
                    array.push(last);
                }
            });
            input.val(array.join(';'));
            $("#popupCaptions").hide();
            dialog.dialog("close");
        });
        $("#cveditor").on('click', '#cancelEdit', function () {
            $("#popupCaptions").hide();
            dialog.dialog("close");
        });

        // zeile entfernen
        $("#cveditor").on('click', 'a.rem', function () {
            $(this).parent().parent().remove();
        });

        // neue zeile
        $("#cveditor").on('click', 'a.add', function () {
            var values = $(this).parent().siblings().find(':text');
            var first = $.trim(values.first().val());
            var last = $.trim(values.last().val());

            if (first.length > 0 || last.length > 0) {
                $(this).hide();
                $(this).siblings(".rem").show();
                $("#inputTbl > tbody:last").append(getRow("", "", true));
            }
        });

        function getRow(key, value, isAdd) {
            var row = "
";

            if (isAdd) {
                row += ""
				+ "	";
            } else {
                row += "	";
            }

            row += '<input type="text" value="' + key + '" style="width:100%;margin:-3px;border:2px inset #eee" />	'
				+ '<input type="text" value="' + value + '" style="width:100%;margin:-3px;border:2px inset #eee" />
';

            return row;
        }
    };
})(jQuery);


text:
require 'spec_helper'

describe BitBucket::Repos::Commits do
  let(:commits) { BitBucket::Repos::Commits.new }

  describe '.list' do
    before do
      expect(commits).to receive(:request).with(
        :get,
        '/2.0/repositories/mock_username/mock_repo/commits',
        {},
        {}
      )
    end

    it 'should send a GET request for the commits belonging to the given repo' do
      commits.list('mock_username', 'mock_repo')
    end
  end
end


text:
<?php

namespace Respect\Validation\Rules\CountrySubdivision;

use Respect\Validation\Rules\AbstractSearcher;

/**
 * Validator for Suriname country subdivision.
 *
 * ISO 3166-1 alpha-2: SR
 *
 * @link http://www.geonames.org/SR/administrative-division-suriname.html
 */
class SrCountrySubdivision extends AbstractSearcher
{
    public $haystack = array(
        'BR', // Brokopondo
        'CM', // Commewijne
        'CR', // Coronie
        'MA', // Marowijne
        'NI', // Nickerie
        'PM', // Paramaribo
        'PR', // Para
        'SA', // Saramacca
        'SI', // Sipaliwini
        'WA', // Wanica
    );

    public $compareIdentical = true;
}


text:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Global_model extends CI_Model {

	function __construct()
	{
		parent::__construct();
	}

	function consultacliente($stringCriterio, $stringImei) {

		$query = "CALL spandroidbcontratos( ? , ?);";

		$resultado = $this->db->query($query, array($stringCriterio, $stringImei));

		$row = new stdClass();
		$row->result = $resultado->result();
		$row->num_rows = $resultado->num_rows();

		$resultado->next_result(); //la libreria fue modificada para tener este parametro dentro de mysqli_result
		$resultado->free_result();

		return $row;

	}

}


text:
'''
Parse the Versa Literate (Markdown) serialization of Versa

Proper entry point of use is versa.serial.literate

see: doc/literate_format.md

'''

import re
import itertools

import markdown

from amara3 import iri # for absolutize & matches_uri_syntax
from amara3.uxml import html5
from amara3.uxml.tree import treebuilder, element, text
from amara3.uxml.treeutil import *

from versa.contrib import mkdcomments
from versa import I, VERSA_BASEIRI
from versa.contrib.datachefids import idgen

# Temp until amara3-xml fix to add comment.xmnl_name
# from amara3.uxml.tree import comment

TEXT_VAL, RES_VAL, UNKNOWN_VAL = 1, 2, 3

TYPE_REL = VERSA_BASEIRI('type')

# IRI ref candidate
IRIREF_CAND_PAT = re.compile('<(.+)?>')

# Does not support the empty URL <> as a property name
# REL_PAT = re.compile('((<(.+)>)|([@\\-_\\w#/]+)):\s*((<(.+)>)|("(.*?)")|(\'(.*?)\')|(.*))', re.DOTALL)
REL_PAT = re.compile('((<(.+)>)|([@\\-_\\w#/]+)):\s*((<(.+)>)|("(.*)")|(\'(.*)\')|(.*))', re.DOTALL)

#
URI_ABBR_PAT = re.compile('@([\\-_\\w]+)([#/@])(.+)', re.DOTALL)
URI_EXPLICIT_PAT = re.compile('<(.+)>', re.DOTALL)

# Does not support the empty URL <> as a property name
RESOURCE_STR = '([^\s\\[\\]]+)?\s?(\\[([^\s\\[\\]]*?)\\])?'
RESOURCE_PAT = re.compile(RESOURCE_STR)
AB_RESOURCE_PAT = re.compile('<\s*' + RESOURCE_STR + '\s*>')

HEADER_PAT = re.compile('h\\d')

'''
>>> import re
>>> RESOURCE_PAT = re.compile('([^\s\\[\\]]+)?\s?(\\[([^\s\\[\\]]*?)\\])?')
>>> m = RESOURCE_PAT.match("ResourceID")
>>> m.groups()
('ResourceID', None, None)
>>> m = RESOURCE_PAT.match("[ResourceType]")
>>> m.groups()
(None, '[ResourceType]', 'ResourceType')
>>> m = RESOURCE_PAT.match("ResourceID [ResourceType]")
>>> m.groups()
('ResourceID', '[ResourceType]', 'ResourceType')
>>> m = RESOURCE_PAT.match("[]")
>>> m.groups()
(None, '[]', '')
'''

def handle_resourcelist(ltext, **kwargs):
    '''
    A helper that converts lists of resources from a textual format such as Markdown, including absolutizing relative IRIs
    '''
    base = kwargs.get('base', VERSA_BASEIRI)
    model = kwargs.get('model')
    iris = ltext.strip().split()
    newlist = model.generate_resource()
    for i in iris:
        model.add(newlist, VERSA_BASEIRI + 'item', I(iri.absolutize(i, base)))
    return newlist

def handle_resourceset(ltext, **kwargs):
    '''
    A helper that converts sets of resources from a textual format such as Markdown, including absolutizing relative IRIs
    '''
    fullprop=kwargs.get('fullprop')
    rid=kwargs.get('rid')
    base=kwargs.get('base', VERSA_BASEIRI)
    model=kwargs.get('model')
    iris = ltext.strip().split()
    for i in iris:
        model.add(rid, fullprop, I(iri.absolutize(i, base)))
    return None

PREP_METHODS = {
    VERSA_BASEIRI + 'text': lambda x, **kwargs: x,
    VERSA_BASEIRI + 'resource': lambda x, base=VERSA_BASEIRI, **kwargs: I(iri.absolutize(x, base)),
    VERSA_BASEIRI + 'resourceset': handle_resourceset,
}

def parse(md, model, encoding='utf-8', config=None):
    """
    Translate the Versa Markdown syntax into Versa model relationships

    md -- markdown source text
    model -- Versa model to take the output relationship
    encoding -- character encoding (defaults to UTF-8)

    Returns: The overall base URI (`@base`) specified in the Markdown file, or None

    >>> from versa.driver.memory import newmodel
    >>> from versa.serial.literate import parse
    >>> m = newmodel()
    >>> parse(open('test/resource/poetry.md').read(), m)
    'http://uche.ogbuji.net/poems/'
    >>> m.size()
    40
    >>> next(m.match(None, 'http://uche.ogbuji.net/poems/updated', '2013-10-15'))
    (I(http://uche.ogbuji.net/poems/1), I(http://uche.ogbuji.net/poems/updated), '2013-10-15', {})
    """
    #Set up configuration to interpret the conventions for the Markdown
    config = config or {}
    #This mapping takes syntactical elements such as the various header levels in Markdown and associates a resource type with the specified resources
    syntaxtypemap = {}
    if config.get('autotype-h1'): syntaxtypemap['h1'] = config.get('autotype-h1')
    if config.get('autotype-h2'): syntaxtypemap['h2'] = config.get('autotype-h2')
    if config.get('autotype-h3'): syntaxtypemap['h3'] = config.get('autotype-h3')
    interp_stanza = config.get('interpretations', {})
    interpretations = {}

    def setup_interpretations(interp):
        #Map the interpretation IRIs to functions to do the data prep
        for prop, interp_key in interp.items():
            if interp_key.startswith('@'):
                interp_key = iri.absolutize(interp_key[1:], VERSA_BASEIRI)
            if interp_key in PREP_METHODS:
                interpretations[prop] = PREP_METHODS[interp_key]
            else:
                #just use the identity, i.e. no-op
                interpretations[prop] = lambda x, **kwargs: x

    setup_interpretations(interp_stanza)

    #Prep ID generator, in case needed
    idg = idgen(None)

    #Preprocess the Markdown to deal with IRI-valued property values
    def iri_ref_tool(m):
        body = m.group(1)
        lchar = '<' if iri.matches_uri_ref_syntax(body ) else '<'
        return lchar + m.group(1) + '>'
    md = IRIREF_CAND_PAT.sub(iri_ref_tool, md)

    # Parse the Markdown
    # Alternately:
    # from xml.sax.saxutils import escape, unescape
    # h = markdown.markdown(escape(md.decode(encoding)), output_format='html5')
    # Note: even using safe_mode this should not be presumed safe from tainted input
    # h = markdown.markdown(md.decode(encoding), safe_mode='escape', output_format='html5')
    comments = mkdcomments.CommentsExtension()
    h = markdown.markdown(md, safe_mode='escape', output_format='html5', extensions=[comments])

    #doc = html.markup_fragment(inputsource.text(h.encode('utf-8')))
    tb = treebuilder()
    h = '<html>' + h + '</html>'
    root = html5.parse(h)
    #root = tb.parse(h)
    #Each section contains one resource description, but the special one named @docheader contains info to help interpret the rest
    first_h1 = next(select_name(descendants(root), 'h1'))
    #top_section_fields = itertools.takewhile(lambda x: x.xml_name != 'h1', select_name(following_siblings(first_h1), 'h2'))

    # Extract header elements. Notice I use an empty element with an empty parent as the default result
    docheader = next(select_value(select_name(descendants(root), 'h1'), '@docheader'),
                    element('empty', parent=root)) # //h1[.="@docheader"]
    sections = filter(lambda x: x.xml_value != '@docheader',
                    select_name_pattern(descendants(root), HEADER_PAT)) # //h1[not(.="@docheader")]|h2[not(.="@docheader")]|h3[not(.="@docheader")]

    def fields(sect):
        '''
        Each section represents a resource and contains a list with its properties
        This generator parses the list and yields the key value pairs representing the properties
        Some properties have attributes, expressed in markdown as a nested list. If present these attributes
        Are yielded as well, else None is yielded
        '''
        #import logging; logging.debug(repr(sect))
        #Pull all the list elements until the next header. This accommodates multiple lists in a section
        try:
            sect_body_items = itertools.takewhile(lambda x: HEADER_PAT.match(x.xml_name) is None, select_elements(following_siblings(sect)))
        except StopIteration:
            return
        #results_until(sect.xml_select('following-sibling::*'), 'self::h1|self::h2|self::h3')
        #field_list = [ U(li) for ul in sect.xml_select('following-sibling::ul') for li in ul.xml_select('./li') ]
        field_list = [ li for elem in select_name(sect_body_items, 'ul') for li in select_name(elem, 'li') ]

        def parse_li(pair):
            '''
            Parse each list item into a property pair
            '''
            if pair.strip():
                matched = REL_PAT.match(pair)
                if not matched:
                    raise ValueError(_('Syntax error in relationship expression: {0}'.format(pair)))
                if matched.group(3): prop = matched.group(3).strip()
                if matched.group(4): prop = matched.group(4).strip()
                if matched.group(7):
                    val = matched.group(7).strip()
                    typeindic = RES_VAL
                elif matched.group(9):
                    val = matched.group(9).strip()
                    typeindic = TEXT_VAL
                elif matched.group(11):
                    val = matched.group(11).strip()
                    typeindic = TEXT_VAL
                elif matched.group(12):
                    val = matched.group(12).strip()
                    typeindic = UNKNOWN_VAL
                else:
                    val = ''
                    typeindic = UNKNOWN_VAL
                #prop, val = [ part.strip() for part in U(li.xml_select('string(.)')).split(':', 1) ]
                #import logging; logging.debug(repr((prop, val)))
                return prop, val, typeindic
            return None, None, None

        def prep_li(li):
            '''
            Take care of Markdown parsing minutiae. Also, Exclude child uls

            * a/href embedded in the li means it was specified as <link_text>.
            Restore the angle brackets as expected by the li parser
            * Similar for cases where e.g. prop: <abc> gets turned into prop: <abc></abc>
            '''
            prepped = ''
            for ch in itertools.takewhile(
                lambda x: not (isinstance(x, element) and x.xml_name == 'ul'), li.xml_children
            ):
                if isinstance(ch, text):
                    prepped += ch
                elif isinstance(ch, element):
                    if ch.xml_name == 'a':
                        prepped += '<' + ch.xml_value + '>'
                    else:
                        prepped += '<' + ch.xml_name + '>'
            return prepped

        #Go through each list item
        for li in field_list:
            #Is there a nested list, which expresses attributes on a property
            if list(select_name(li, 'ul')):
                #main = ''.join([ node.xml_value
                #        for node in itertools.takewhile(
                #            lambda x: x.xml_name != 'ul', select_elements(li)
                #            )
                #    ])
                main = prep_li(li)
                prop, val, typeindic = parse_li(main)
                subfield_list = [ parse_li(prep_li(sli)) for e in select_name(li, 'ul') for sli in (
                                select_name(e, 'li')
                                ) ]
                subfield_list = [ (p, v, t) for (p, v, t) in subfield_list if p is not None ]
                #Support a special case for syntax such as in the @iri and @interpretations: stanza of @docheader
                if val is None: val = ''
                yield prop, val, typeindic, subfield_list
            #Just a regular, unadorned property
            else:
                prop, val, typeindic = parse_li(prep_li(li))
                if prop: yield prop, val, typeindic, None

    iris = {}

    # Gather the document-level metadata from the @docheader section
    base = schemabase = rtbase = document_iri = default_lang = None
    for prop, val, typeindic, subfield_list in fields(docheader):
        #The @iri section is where key IRI prefixes can be set
        if prop == '@iri':
            for (k, uri, typeindic) in subfield_list:
                if k == '@base':
                    base = schemabase = rtbase = uri
                # @property is legacy
                elif k == '@schema' or k == '@property':
                    schemabase = uri
                elif k == '@resource-type':
                    rtbase = uri
                else:
                    iris[k] = uri
        #The @interpretations section is where defaults can be set as to the primitive types of values from the Markdown, based on the relevant property/relationship
        elif prop == '@interpretations':
            #Iterate over items from the @docheader/@interpretations section to set up for further parsing
            interp = {}
            for k, v, x in subfield_list:
                interp[I(iri.absolutize(k, schemabase))] = v
            setup_interpretations(interp)
        #Setting an IRI for this very document being parsed
        elif prop == '@document':
            document_iri = val
        elif prop == '@language':
            default_lang = val
        #If we have a resource to which to attach them, just attach all other properties
        elif document_iri or base:
            rid = document_iri or base
            fullprop = I(iri.absolutize(prop, schemabase or base))
            if fullprop in interpretations:
                val = interpretations[fullprop](val, rid=rid, fullprop=fullprop, base=base, model=model)
                if val is not None: model.add(rid, fullprop, val)
            else:
                model.add(rid, fullprop, val)

    #Default IRI prefixes if @iri/@base is set
    if not schemabase: schemabase = base
    if not rtbase: rtbase = base
    if not document_iri: document_iri = base

    #Go through the resources expressed in remaining sections
    for sect in sections:
        #if U(sect) == '@docheader': continue #Not needed because excluded by ss
        #The header can take one of 4 forms: "ResourceID" "ResourceID [ResourceType]" "[ResourceType]" or "[]"
        #The 3rd form is for an anonymous resource with specified type and the 4th an anonymous resource with unspecified type
        matched = RESOURCE_PAT.match(sect.xml_value)
        if not matched:
            raise ValueError(_('Syntax error in resource header: {0}'.format(sect.xml_value)))
        rid = matched.group(1)
        rtype = matched.group(3)
        if rtype:
            rtype = I(iri.absolutize(rtype, schemabase))

        if rid:
            rid = I(iri.absolutize(rid, base))
        if not rid:
            rid = next(idg)

        #Resource type might be set by syntax config
        if not rtype:
            rtype = syntaxtypemap.get(sect.xml_name)
        if rtype:
            model.add(rid, TYPE_REL, rtype)

        def expand_iri(iri_in, base):
            if iri_in.startswith('@'):
                return I(iri.absolutize(iri_in[1:], VERSA_BASEIRI))
            iri_match = URI_EXPLICIT_PAT.match(iri_in)
            if iri_match:
                return I(iri.absolutize(iri_match.group(1), base))
            iri_match = URI_ABBR_PAT.match(iri_in)
            if iri_match:
                uri = iris[iri_match.group(1)]
                fulliri = URI_ABBR_PAT.sub(uri + '\\2\\3', iri_in)
            else:
                fulliri = I(iri.absolutize(iri_in, base))
            return fulliri

        #Add the property
        for prop, val, typeindic, subfield_list in fields(sect):
            attrs = {}
            for (aprop, aval, atype) in subfield_list or ():
                fullaprop = expand_iri(aprop, schemabase)
                if atype == RES_VAL:
                    val = expand_iri(aval, rtbase)
                    valmatch = URI_ABBR_PAT.match(aval)
                    if valmatch:
                        uri = iris[valmatch.group(1)]
                        attrs[fullaprop] = URI_ABBR_PAT.sub(uri + '\\2\\3', aval)
                    else:
                        attrs[fullaprop] = I(iri.absolutize(aval, rtbase))
                elif atype == TEXT_VAL:
                    attrs[fullaprop] = aval
                elif atype == UNKNOWN_VAL:
                    val_iri_match = URI_EXPLICIT_PAT.match(aval)
                    if val_iri_match:
                        aval = expand_iri(aval, rtbase)
                    elif fullaprop in interpretations:
                        aval = interpretations[fullaprop](aval, rid=rid, fullprop=fullaprop, base=base, model=model)
                    if aval is not None:
                        attrs[fullaprop] = aval

            fullprop = expand_iri(prop, schemabase)
            if typeindic == RES_VAL:
                val = expand_iri(val, rtbase)
                model.add(rid, fullprop, val, attrs)
            elif typeindic == TEXT_VAL:
                if '@lang' not in attrs: attrs['@lang'] = default_lang
                model.add(rid, fullprop, val, attrs)
            elif typeindic == UNKNOWN_VAL:
                val_iri_match = URI_EXPLICIT_PAT.match(val)
                if val_iri_match:
                    val = expand_iri(val, rtbase)
                elif fullprop in interpretations:
                    val = interpretations[fullprop](val, rid=rid, fullprop=fullprop, base=base, model=model)
                if val is not None:
                    model.add(rid, fullprop, val, attrs)

            #resinfo = AB_RESOURCE_PAT.match(val)
            #if resinfo:
            #    val = resinfo.group(1)
            #    valtype = resinfo.group(3)
            #    if not val: val = model.generate_resource()
            #    if valtype: attrs[TYPE_REL] = valtype

    return document_iri

# XXX Add support for long Versa literate docs fed incrementally
def parse_iter():
    raise NotImplementedError


text:
<?php

/**
 * Run pull commands on local working copies to keep them up to date. This
 * daemon handles all repository types.
 *
 * By default, the daemon pulls **every** repository. If you want it to be
 * responsible for only some repositories, you can launch it with a list of
 * repositories:
 *
 *   ./phd launch repositorypulllocal -- X Q Z
 *
 * You can also launch a daemon which is responsible for all //but// one or
 * more repositories:
 *
 *   ./phd launch repositorypulllocal -- --not A --not B
 *
 * If you have a very large number of repositories and some aren't being pulled
 * as frequently as you'd like, you can either change the pull frequency of
 * the less-important repositories to a larger number (so the daemon will skip
 * them more often) or launch one daemon for all the less-important repositories
 * and one for the more important repositories (or one for each more important
 * repository).
 *
 * @task pull   Pulling Repositories
 */
final class PhabricatorRepositoryPullLocalDaemon
  extends PhabricatorDaemon {

  private $statusMessageCursor = 0;

/* -(  Pulling Repositories  )----------------------------------------------- */

  /**
   * @task pull
   */
  protected function run() {
    $argv = $this->getArgv();
    array_unshift($argv, __CLASS__);
    $args = new PhutilArgumentParser($argv);
    $args->parse(
      array(
        array(
          'name'      => 'no-discovery',
          'help'      => pht('Pull only, without discovering commits.'),
        ),
        array(
          'name'      => 'not',
          'param'     => 'repository',
          'repeat'    => true,
          'help'      => pht('Do not pull __repository__.'),
        ),
        array(
          'name'      => 'repositories',
          'wildcard'  => true,
          'help'      => pht('Pull specific __repositories__ instead of all.'),
        ),
      ));

    $no_discovery   = $args->getArg('no-discovery');
    $include = $args->getArg('repositories');
    $exclude = $args->getArg('not');

    // Each repository has an individual pull frequency; after we pull it,
    // wait that long to pull it again. When we start up, try to pull everything
    // serially.
    $retry_after = array();

    $min_sleep = 15;
    $max_futures = 4;
    $futures = array();
    $queue = array();

    while (!$this->shouldExit()) {
      PhabricatorCaches::destroyRequestCache();
      $device = AlmanacKeys::getLiveDevice();

      $pullable = $this->loadPullableRepositories($include, $exclude, $device);

      // If any repositories have the NEEDS_UPDATE flag set, pull them
      // as soon as possible.
      $need_update_messages = $this->loadRepositoryUpdateMessages(true);
      foreach ($need_update_messages as $message) {
        $repo = idx($pullable, $message->getRepositoryID());
        if (!$repo) {
          continue;
        }

        $this->log(
          pht(
            'Got an update message for repository "%s"!',
            $repo->getMonogram()));

        $retry_after[$message->getRepositoryID()] = time();
      }

      // If any repositories were deleted, remove them from the retry timer map
      // so we don't end up with a retry timer that never gets updated and
      // causes us to sleep for the minimum amount of time.
      $retry_after = array_select_keys(
        $retry_after,
        array_keys($pullable));

      // Figure out which repositories we need to queue for an update.
      foreach ($pullable as $id => $repository) {
        $now = PhabricatorTime::getNow();
        $display_name = $repository->getDisplayName();

        if (isset($futures[$id])) {
          $this->log(
            pht(
              'Repository "%s" is currently updating.',
              $display_name));
          continue;
        }

        if (isset($queue[$id])) {
          $this->log(
            pht(
              'Repository "%s" is already queued.',
              $display_name));
          continue;
        }

        $after = idx($retry_after, $id);
        if (!$after) {
          $smart_wait = $repository->loadUpdateInterval($min_sleep);
          $last_update = $this->loadLastUpdate($repository);

          $after = $last_update + $smart_wait;
          $retry_after[$id] = $after;

          $this->log(
            pht(
              'Scheduling repository "%s" with an update window of %s '.
              'second(s). Last update was %s second(s) ago.',
              $display_name,
              new PhutilNumber($smart_wait),
              new PhutilNumber($now - $last_update)));
        }

        if ($after > time()) {
          $this->log(
            pht(
              'Repository "%s" is not due for an update for %s second(s).',
              $display_name,
              new PhutilNumber($after - $now)));
          continue;
        }

        $this->log(
          pht(
            'Scheduling repository "%s" for an update (%s seconds overdue).',
            $display_name,
            new PhutilNumber($now - $after)));

        $queue[$id] = $after;
      }

      // Process repositories in the order they became candidates for updates.
      asort($queue);

      // Dequeue repositories until we hit maximum parallelism.
      while ($queue && (count($futures) < $max_futures)) {
        foreach ($queue as $id => $time) {
          $repository = idx($pullable, $id);
          if (!$repository) {
            $this->log(
              pht('Repository %s is no longer pullable; skipping.', $id));
            unset($queue[$id]);
            continue;
          }

          $display_name = $repository->getDisplayName();
          $this->log(
            pht(
              'Starting update for repository "%s".',
              $display_name));

          unset($queue[$id]);
          $futures[$id] = $this->buildUpdateFuture(
            $repository,
            $no_discovery);

          break;
        }
      }

      if ($queue) {
        $this->log(
          pht(
            'Not enough process slots to schedule the other %s '.
            'repository(s) for updates yet.',
            phutil_count($queue)));
      }

      if ($futures) {
        $iterator = id(new FutureIterator($futures))
          ->setUpdateInterval($min_sleep);

        foreach ($iterator as $id => $future) {
          $this->stillWorking();

          if ($future === null) {
            $this->log(pht('Waiting for updates to complete...'));
            $this->stillWorking();

            if ($this->loadRepositoryUpdateMessages()) {
              $this->log(pht('Interrupted by pending updates!'));
              break;
            }

            continue;
          }

          unset($futures[$id]);
          $retry_after[$id] = $this->resolveUpdateFuture(
            $pullable[$id],
            $future,
            $min_sleep);

          // We have a free slot now, so go try to fill it.
          break;
        }

        // Jump back into prioritization if we had any futures to deal with.
        continue;
      }

      $this->waitForUpdates($min_sleep, $retry_after);
    }

  }

  /**
   * @task pull
   */
  private function buildUpdateFuture(
    PhabricatorRepository $repository,
    $no_discovery) {

    $bin = dirname(phutil_get_library_root('phabricator')).'/bin/repository';

    $flags = array();
    if ($no_discovery) {
      $flags[] = '--no-discovery';
    }

    $monogram = $repository->getMonogram();
    $future = new ExecFuture('%s update %Ls -- %s', $bin, $flags, $monogram);

    // Sometimes, the underlying VCS commands will hang indefinitely. We've
    // observed this occasionally with GitHub, and other users have observed
    // it with other VCS servers.

    // To limit the damage this can cause, kill the update out after a
    // reasonable amount of time, under the assumption that it has hung.

    // Since it's hard to know what a "reasonable" amount of time is given that
    // users may be downloading a repository full of pirated movies over a
    // potato, these limits are fairly generous. Repositories exceeding these
    // limits can be manually pulled with `bin/repository update X`, which can
    // just run for as long as it wants.

    if ($repository->isImporting()) {
      $timeout = phutil_units('4 hours in seconds');
    } else {
      $timeout = phutil_units('15 minutes in seconds');
    }

    $future->setTimeout($timeout);

    // The default TERM inherited by this process is "unknown", which causes PHP
    // to produce a warning upon startup.  Override it to squash this output to
    // STDERR.
    $future->updateEnv('TERM', 'dumb');

    return $future;
  }

  /**
   * Check for repositories that should be updated immediately.
   *
   * With the `$consume` flag, an internal cursor will also be incremented so
   * that these messages are not returned by subsequent calls.
   *
   * @param bool Pass `true` to consume these messages, so the process will
   *   not see them again.
   * @return list<wild> Pending update messages.
   *
   * @task pull
   */
  private function loadRepositoryUpdateMessages($consume = false) {
    $type_need_update = PhabricatorRepositoryStatusMessage::TYPE_NEEDS_UPDATE;
    $messages = id(new PhabricatorRepositoryStatusMessage())->loadAllWhere(
      'statusType = %s AND id > %d',
      $type_need_update,
      $this->statusMessageCursor);

    // Keep track of messages we've seen so that we don't load them again.
    // If we reload messages, we can get stuck a loop if we have a failing
    // repository: we update immediately in response to the message, but do
    // not clear the message because the update does not succeed. We then
    // immediately retry. Instead, messages are only permitted to trigger
    // an immediate update once.

    if ($consume) {
      foreach ($messages as $message) {
        $this->statusMessageCursor = max(
          $this->statusMessageCursor,
          $message->getID());
      }
    }

    return $messages;
  }

  /**
   * @task pull
   */
  private function loadLastUpdate(PhabricatorRepository $repository) {
    $table = new PhabricatorRepositoryStatusMessage();
    $conn = $table->establishConnection('r');

    $epoch = queryfx_one(
      $conn,
      'SELECT MAX(epoch) last_update FROM %T
        WHERE repositoryID = %d
          AND statusType IN (%Ls)',
      $table->getTableName(),
      $repository->getID(),
      array(
        PhabricatorRepositoryStatusMessage::TYPE_INIT,
        PhabricatorRepositoryStatusMessage::TYPE_FETCH,
      ));

    if ($epoch) {
      return (int)$epoch['last_update'];
    }

    return PhabricatorTime::getNow();
  }

  /**
   * @task pull
   */
  private function loadPullableRepositories(
    array $include,
    array $exclude,
    AlmanacDevice $device = null) {

    $query = id(new PhabricatorRepositoryQuery())
      ->setViewer($this->getViewer());

    if ($include) {
      $query->withIdentifiers($include);
    }

    $repositories = $query->execute();
    $repositories = mpull($repositories, null, 'getPHID');

    if ($include) {
      $map = $query->getIdentifierMap();
      foreach ($include as $identifier) {
        if (empty($map[$identifier])) {
          throw new Exception(
            pht(
              'No repository "%s" exists!',
              $identifier));
        }
      }
    }

    if ($exclude) {
      $xquery = id(new PhabricatorRepositoryQuery())
        ->setViewer($this->getViewer())
        ->withIdentifiers($exclude);

      $excluded_repos = $xquery->execute();
      $xmap = $xquery->getIdentifierMap();

      foreach ($exclude as $identifier) {
        if (empty($xmap[$identifier])) {
          throw new Exception(
            pht(
              'No repository "%s" exists!',
              $identifier));
        }
      }

      foreach ($excluded_repos as $excluded_repo) {
        unset($repositories[$excluded_repo->getPHID()]);
      }
    }

    foreach ($repositories as $key => $repository) {
      if (!$repository->isTracked()) {
        unset($repositories[$key]);
      }
    }

    $viewer = $this->getViewer();

    $filter = id(new DiffusionLocalRepositoryFilter())
      ->setViewer($viewer)
      ->setDevice($device)
      ->setRepositories($repositories);

    $repositories = $filter->execute();

    foreach ($filter->getRejectionReasons() as $reason) {
      $this->log($reason);
    }

    // Shuffle the repositories, then re-key the array since shuffle()
    // discards keys. This is mostly for startup, we'll use soft priorities
    // later.
    shuffle($repositories);
    $repositories = mpull($repositories, null, 'getID');

    return $repositories;
  }

  /**
   * @task pull
   */
  private function resolveUpdateFuture(
    PhabricatorRepository $repository,
    ExecFuture $future,
    $min_sleep) {

    $display_name = $repository->getDisplayName();

    $this->log(pht('Resolving update for "%s".', $display_name));

    try {
      list($stdout, $stderr) = $future->resolvex();
    } catch (Exception $ex) {
      $proxy = new PhutilProxyException(
        pht(
          'Error while updating the "%s" repository.',
          $display_name),
        $ex);
      phlog($proxy);

      $smart_wait = $repository->loadUpdateInterval($min_sleep);
      return PhabricatorTime::getNow() + $smart_wait;
    }

    if (strlen($stderr)) {
      $stderr_msg = pht(
        'Unexpected output while updating repository "%s": %s',
        $display_name,
        $stderr);
      phlog($stderr_msg);
    }

    $smart_wait = $repository->loadUpdateInterval($min_sleep);

    $this->log(
      pht(
        'Based on activity in repository "%s", considering a wait of %s '.
        'seconds before update.',
        $display_name,
        new PhutilNumber($smart_wait)));

    return PhabricatorTime::getNow() + $smart_wait;
  }

  /**
   * Sleep for a short period of time, waiting for update messages from the
   *
   *
   * @task pull
   */
  private function waitForUpdates($min_sleep, array $retry_after) {
    $this->log(
      pht('No repositories need updates right now, sleeping...'));

    $sleep_until = time() + $min_sleep;
    if ($retry_after) {
      $sleep_until = min($sleep_until, min($retry_after));
    }

    while (($sleep_until - time()) > 0) {
      $sleep_duration = ($sleep_until - time());

      $this->log(
        pht(
          'Sleeping for %s more second(s)...',
          new PhutilNumber($sleep_duration)));

      $this->sleep(1);

      if ($this->shouldExit()) {
        $this->log(pht('Awakened from sleep by graceful shutdown!'));
        return;
      }

      if ($this->loadRepositoryUpdateMessages()) {
        $this->log(pht('Awakened from sleep by pending updates!'));
        break;
      }
    }
  }

}


text:
package configure

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"os"

	"github.com/hashicorp/hcl"
)

// NewHCL returns an instance of the HCL checker. It takes a function
// which returns an io.Reader which will be called when the first value
// is recalled. The contents of the io.Reader MUST be decodable into HCL or JSON.
func NewHCL(gen func() (io.Reader, error)) *HCL {
	return &HCL{
		gen: gen,
	}
}

// NewHCLFromFile returns an instance of the HCL checker. It reads its
// data from a file (file content can be HCL or JSON) which its location has been specified through the path
// parameter
func NewHCLFromFile(path string) *HCL {
	return NewHCL(func() (io.Reader, error) {
		return os.Open(path)
	})
}

// HCL represents the HCL Checker. It reads an io.Reader and then pulls a value out of a map[string]interface{}.
type HCL struct {
	values map[string]interface{}
	gen    func() (io.Reader, error)
}

// Setup initializes the HCL Checker
func (h *HCL) Setup() error {
	r, err := h.gen()
	if err != nil {
		return err
	}

	buf := new(bytes.Buffer)
	buf.ReadFrom(r)
	s := buf.String()

	//first parse the hcl file
	obj, err := hcl.Parse(s)
	if err != nil {
		return err
	}

	h.values = make(map[string]interface{})

	// then decode the object
	if err = hcl.DecodeObject(&h.values, obj); err != nil {
		return err
	}

	return nil
}

func (h *HCL) value(name string) (interface{}, error) {
	val, ok := h.values[name]
	if !ok {
		return nil, errors.New("variable does not exist")
	}

	return val, nil
}

// Int returns an int if it exists within the HCL io.Reader
func (h *HCL) Int(name string) (int, error) {
	v, err := h.value(name)
	if err != nil {
		return 0, err
	}

	f, ok := v.(float64)
	if !ok {
		i, ok := v.(int)
		if !ok {
			return v.(int), errors.New(fmt.Sprintf("%T unable", v))
		}

		return i, nil
	}

	return int(f), nil
}

// Bool returns a bool if it exists within the HCL io.Reader.
func (h *HCL) Bool(name string) (bool, error) {
	v, err := h.value(name)
	if err != nil {
		return false, err
	}

	return v.(bool), nil
}

// String returns a string if it exists within the HCL io.Reader.
func (h *HCL) String(name string) (string, error) {
	v, err := h.value(name)
	if err != nil {
		return "", err
	}

	return v.(string), nil
}


text:
package net.mikaboshi.gui;

import java.awt.Component;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JViewport;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

/**
 * ビジュアルクラスに関するユーティリティ
 * @author Takuma Umezawa
 * @since 1.1.5
 */
public final class GuiUtils {

	private GuiUtils() {}

	/**
	 * ESCキーでウィンドウを閉じる。
	 * @param window
	 * @param panel
	 */
	public static void closeByESC(final Window window, JPanel panel) {
		panel.getActionMap().put(
				"close",
				new AbstractAction() {
					private static final long serialVersionUID = 1L;
					public void actionPerformed(ActionEvent e) {
						window.dispose();
					}
				}
		);

		panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");

	}

	/**
	 * テキストエリアの一番上にスクロールを合わせる。
	 * @param textArea
	 */
	public static void setCeil(final JTextArea textArea) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JViewport v = (JViewport) textArea.getParent();
				v.setViewPosition(new Point(0, 0));
			}
		});
	}

	/**
	 * テーブルの列の幅をヘッダサイズに合わせる。
	 * @param table
	 */
	public static void adjustHeader(JTable table) {

		TableCellRenderer headerRenderer =
				table.getTableHeader().getDefaultRenderer();

		for (int i = 0; i < table.getColumnCount(); i++) {

			TableColumn column = table.getColumnModel().getColumn(i);

			Component header =
				headerRenderer.getTableCellRendererComponent(
					table,
					column.getHeaderValue(), false,
					false,
					0,
					i);

			column.setPreferredWidth(header.getPreferredSize().width + 8);
		}
	}

}


text:
module ActiveRecordExtensions
  module Table
    module BaseMethods
      def self.included(base)
        base.extend ClassMethods
      end

      module ClassMethods
        def get_schema_name(table_name)
          if table_name =~ /\./ then
            return $`
          else
            return 'public'
          end
        end

        def get_table_name(table_name)
          if table_name =~ /\./ then
            return $'
          else
            return table_name
          end
        end

        def test_table_exists?(table_name=nil)
          table_name ||= self.table_name
          return test_by_sql "select 1 from #{table_name} limit 1;"
        end

        def filter_exist_tables(table_names)
          cond_list = table_names.map { |table_name|
            schema_name = self.get_schema_name table_name
            table_name  = self.get_table_name table_name
            "(table_schema = #{self.sanitize schema_name} and table_name = #{self.sanitize table_name})"
          }

          res = self.query_by_sql <<-EOF
            select table_schema || '.' || table_name as table_name
            from information_schema.tables
            #{SqlUtil.where_sql(SqlUtil.or_cond(cond_list))};
          EOF
          return res.map{|row| row[:table_name]}
        end

      end
    end
  end
end

class ActiveRecord::Base
  include ActiveRecordExtensions::Table::BaseMethods
end


text:
package org.testcontainers.jdbc.mariadb;

import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.testcontainers.jdbc.AbstractJDBCDriverTest;

import java.util.EnumSet;

import static java.util.Arrays.asList;

@RunWith(Parameterized.class)
public class MariaDBJDBCDriverTest extends AbstractJDBCDriverTest {

    @Parameterized.Parameters(name = "{index} - {0}")
    public static Iterable<Object[]> data() {
        return asList(
            new Object[][]{
                {"jdbc:tc:mariadb://hostname/databasename", EnumSet.noneOf(Options.class)},
                {"jdbc:tc:mariadb://hostname/databasename?user=someuser&TC_INITSCRIPT=somepath/init_mariadb.sql", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
                {"jdbc:tc:mariadb:10.2.14://hostname/databasename", EnumSet.noneOf(Options.class)},
                {"jdbc:tc:mariadb:10.2.14://hostname/databasename?TC_INITSCRIPT=somepath/init_unicode_mariadb.sql&useUnicode=yes&characterEncoding=utf8", EnumSet.of(Options.CharacterSet)},
                {"jdbc:tc:mariadb:10.2.14://hostname/databasename?user=someuser&TC_INITSCRIPT=somepath/init_mariadb.sql", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
                {"jdbc:tc:mariadb:10.2.14://hostname/databasename?user=someuser&TC_INITFUNCTION=org.testcontainers.jdbc.AbstractJDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
                {"jdbc:tc:mariadb:10.2.14://hostname/databasename?user=someuser&password=somepwd&TC_INITSCRIPT=somepath/init_mariadb.sql", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
                {"jdbc:tc:mariadb:10.2.14://hostname/databasename?user=someuser&password=somepwd&TC_INITFUNCTION=org.testcontainers.jdbc.AbstractJDBCDriverTest::sampleInitFunction", EnumSet.of(Options.ScriptedSchema, Options.JDBCParams)},
                {"jdbc:tc:mariadb:10.2.14://hostname/databasename?TC_MY_CNF=somepath/mariadb_conf_override", EnumSet.of(Options.CustomIniFile)},
            });
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<!--
 ! Excerpted from "Kotlin and Android Development featuring Jetpack",
 ! published by The Pragmatic Bookshelf.
 ! Copyrights apply to this code. It may not be used to create training material,
 ! courses, books, articles, and the like. Contact us if you are in doubt.
 ! We make no guarantees that this code is fit for any purpose.
 ! Visit http://www.pragmaticprogrammer.com/titles/mfjetpack for more book information.
-->
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="vm"
            type="dev.mfazio.pennydrop.viewmodels.PickPlayersViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="16dp"
        tools:context=".fragments.PickPlayersFragment">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <include
                android:id="@+id/mainPlayer"
                layout="@layout/player_list_item"
                app:player="@{vm.players[0]}" />

            <include
                android:id="@+id/player2"
                layout="@layout/player_list_item"
                app:player="@{vm.players[1]}" />

            <include
                android:id="@+id/player3"
                layout="@layout/player_list_item"
                app:player="@{vm.players[2]}" />

            <include
                android:id="@+id/player4"
                layout="@layout/player_list_item"
                app:player="@{vm.players[3]}" />

            <include
                android:id="@+id/player5"
                layout="@layout/player_list_item"
                app:player="@{vm.players[4]}" />

            <include
                android:id="@+id/player6"
                layout="@layout/player_list_item"
                app:player="@{vm.players[5]}" />
        </LinearLayout>

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/buttonPlayGame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:contentDescription="@string/play_game_button"
            android:src="@drawable/ic_baseline_play_arrow_24"
            app:tint="?android:attr/textColorPrimaryInverse"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>


text:
package com.navercorp.pinpoint.pluginit.jdbc.testcontainers;

import com.navercorp.pinpoint.pluginit.jdbc.DriverProperties;
import org.testcontainers.containers.JdbcDatabaseContainer;

import java.util.Objects;
import java.util.Properties;

public final class DatabaseContainers {

    private DatabaseContainers() {
    }

    public static Properties toProperties(JdbcDatabaseContainer container) {
        Objects.requireNonNull(container, "container");

        Properties properties = new Properties();
        properties.setProperty(DriverProperties.URL, container.getJdbcUrl());
        properties.setProperty(DriverProperties.USER, container.getUsername());
        properties.setProperty(DriverProperties.PASSWARD, container.getPassword());
        properties.setProperty(DriverProperties.HOST, container.getHost());
        properties.setProperty(DriverProperties.PORT, container.getFirstMappedPort().toString());

        try {
            properties.setProperty(DriverProperties.DATABASE, container.getDatabaseName());
        } catch (UnsupportedOperationException ignored) {
        }

        return properties;
    }

    public static Properties toProperties(String url, String user, String password) {
        Objects.requireNonNull(url, "url");
        Objects.requireNonNull(user, "user");
        Objects.requireNonNull(password, "password");

        Properties properties = new Properties();
        properties.setProperty(DriverProperties.URL, url);
        properties.setProperty(DriverProperties.USER, user);
        properties.setProperty(DriverProperties.PASSWARD, password);
        return properties;
    }

    public static DriverProperties readDriverProperties(Properties properties) {
        Objects.requireNonNull(properties, "properties");

        String url = properties.getProperty(DriverProperties.URL);
        String user = properties.getProperty(DriverProperties.USER);
        String password = properties.getProperty(DriverProperties.PASSWARD);
        return new DriverProperties(url, user, password, properties);
    }

}