text:
u"""
Script to help a human solve disambiguations by presenting a set of options.

Specify the disambiguation page on the command line.

The program will pick up the page, and look for all alternative links,
and show them with a number adjacent to them. It will then automatically
loop over all pages referring to the disambiguation page,
and show 30 characters of context on each side of the reference to help you
make the decision between the alternatives. It will ask you to type the
number of the appropriate replacement, and perform the change.

It is possible to choose to replace only the link (just type the number) or
replace both link and link-text (type 'r' followed by the number).

Multiple references in one page will be scanned in order, but typing 'n'
(next) on any one of them will leave the complete page unchanged. To leave
only some reference unchanged, use the 's' (skip) option.

Command line options:

   -pos:XXXX   adds XXXX as an alternative disambiguation

   -just       only use the alternatives given on the command line, do not
               read the page for other possibilities

   -dnskip     Skip links already marked with a disambiguation-needed
               template (e.g., {{dn}})

   -primary    "primary topic" disambiguation (Begriffsklärung nach Modell 2).
               That's titles where one topic is much more important, the
               disambiguation page is saved somewhere else, and the important
               topic gets the nice name.

   -primary:XY like the above, but use XY as the only alternative, instead of
               searching for alternatives in [[Keyword (disambiguation)]].
               Note: this is the same as -primary -just -pos:XY

   -file:XYZ   reads a list of pages from a text file. XYZ is the name of the
               file from which the list is taken. If XYZ is not given, the
               user is asked for a filename. Page titles should be inside
               [[double brackets]]. The -pos parameter won't work if -file
               is used.

   -always:XY  instead of asking the user what to do, always perform the same
               action. For example, XY can be "r0", "u" or "2". Be careful with
               this option, and check the changes made by the bot. Note that
               some choices for XY don't make sense and will result in a loop,
               e.g. "l" or "m".

   -main       only check pages in the main namespace, not in the talk,
               wikipedia, user, etc. namespaces.

   -start:XY   goes through all disambiguation pages in the category on your
               wiki that is defined (to the bot) as the category containing
               disambiguation pages, starting at XY. If only '-start' or
               '-start:' is given, it starts at the beginning.

   -min:XX     (XX being a number) only work on disambiguation pages for which
               at least XX are to be worked on.

To complete a move of a page, one can use:

    python pwb.py solve_disambiguation -just -pos:New_Name Old_Name

"""
#
# (C) Rob W.W. Hooft, 2003
# (C) Daniel Herding, 2004
# (C) Andre Engels, 2003-2004
# (C) WikiWichtel, 2004
# (C) Pywikibot team, 2003-2017
#
# Distributed under the terms of the MIT license.
#
from __future__ import absolute_import, unicode_literals

import codecs
import os
import re

import pywikibot
from pywikibot import editor as editarticle
from pywikibot.tools import first_lower, first_upper as firstcap
from pywikibot import pagegenerators, config, i18n
from pywikibot.bot import (
    Bot, QuitKeyboardInterrupt,
    StandardOption, HighlightContextOption, ListOption, OutputProxyOption,
)
from pywikibot.tools.formatter import SequenceOutputter

# Disambiguation Needed template
dn_template = {
    'en': u'{{dn}}',
    'fr': u'{{Lien vers un homonyme}}',
}

# disambiguation page name format for "primary topic" disambiguations
# (Begriffsklärungen nach Modell 2)
primary_topic_format = {
    'ar': u'%s_(توضيح)',
    'ca': u'%s_(desambiguació)',
    'cs': u'%s_(rozcestník)',
    'de': u'%s_(Begriffsklärung)',
    'en': u'%s_(disambiguation)',
    'fa': u'%s_(ابهام‌زدایی)',
    'fi': u'%s_(täsmennyssivu)',
    'hu': u'%s_(egyértelműsítő lap)',
    'ia': u'%s_(disambiguation)',
    'it': u'%s_(disambigua)',
    'lt': u'%s_(reikšmės)',
    'kk': u'%s_(айрық)',
    'ko': u'%s_(동음이의)',
    'nl': u'%s_(doorverwijspagina)',
    'no': u'%s_(peker)',
    'pl': u'%s_(ujednoznacznienie)',
    'pt': u'%s_(desambiguação)',
    'pfl': u'%s_BKL',
    'he': u'%s_(פירושונים)',
    'ru': u'%s_(значения)',
    'sr': u'%s_(вишезначна одредница)',
    'sv': u'%s_(olika betydelser)',
    'uk': u'%s_(значення)',
}

# List pages that will be ignored if they got a link to a disambiguation
# page. An example is a page listing disambiguations articles.
# Special chars should be encoded with unicode (\x##) and space used
# instead of _

ignore_title = {
    'wikipedia': {
        'ar': [
            u'تصنيف:صفحات توضيح',
        ],
        'ca': [
            u'Viquipèdia:Enllaços incorrectes a pàgines de desambiguació',
            u'Viquipèdia:Registre de pàgines de desambiguació òrfenes',
            u'.*Discussió:.+',
            u'.*Usuari:.+',
            u'.+/[aA]rxiu.*',
        ],
        'cs': [
            u'Wikipedie:Chybějící interwiki/.+',
            u'Wikipedie:Rozcestníky',
            u'Wikipedie diskuse:Rozcestníky',
            u'Wikipedie:Seznam nejvíce odkazovaných rozcestníků',
            u'Wikipedie:Seznam rozcestníků/první typ',
            u'Wikipedie:Seznam rozcestníků/druhý typ',
            u'Wikipedista:Zirland/okres',
        ],
        'da': [
            u'Wikipedia:Links til sider med flertydige titler'
        ],
        'de': [
            u'.+/[aA]rchiv.*',
            u'.+/Baustelle.*',
            u'.+/Index',
            u'.+/Spielwiese',
            u'.+/[tT]est.*',
            u'.*Diskussion:.+',
            u'Benutzer:.+/[Ll]og.*',
            u'Benutzer:C.Löser/.+',
            u'Benutzer:Katharina/Begriffsklärungen',
            u'Benutzer:Kirschblut/.+buchstabenkürzel',
            u'Benutzer:Mathias Schindler/.+',
            u'Benutzer:Noisper/Dingliste/[A-Z]',
            u'Benutzer:Professor Einstein.*',
            u'Benutzer:Sebbot/.+',
            u'Benutzer:SirJective/.+',
            u'Benutzer:Srbauer.*',
            u'Benutzer:SteEis.',
            u'Benutzer:Steindy.*',
            u'Benutzer:SrbBot.*',
            u'Benutzer:PortalBot/.+',
            u'Benutzer:Xqbot/.+',
            u'Lehnwort',
            u'Liste griechischer Wortstämme in deutschen Fremdwörtern',
            u'Liste von Gräzismen',
            u'Portal:Abkürzungen/.+',
            u'Portal:Astronomie/Moves',
            u'Portal:Astronomie/Index/.+',
            u'Portal:Hund',
            u'Portal:Hund/Beobachtungsliste',
            u'Portal:Marxismus',
            u'Portal:Täuferbewegung/Seitenindex',
            u'Wikipedia:Administratoren/Anfragen',
            u'Wikipedia:Archiv/.+',
            u'Wikipedia:Artikelwünsche/Ding-Liste/[A-Z]',
            u'Wikipedia:Begriffsklärung.*',
            u'Wikipedia:Bots/.+',
            u'Wikipedia:Interwiki-Konflikte',
            u'Wikipedia:ISBN-Suche',
            u'Wikipedia:Liste mathematischer Themen/BKS',
            u'Wikipedia:Liste mathematischer Themen/Redirects',
            u'Wikipedia:Meinungsbilder/.+',
            u'Wikipedia:Löschkandidaten/.+',
            u'Wikipedia:WikiProjekt Altertumswissenschaft/.+',
            u'Wikipedia:WikiProjekt Verwaiste Seiten/Begriffsklärungen',
            u'Wikipedia:Qualitätssicherung/.+',
            u'Vorlage:Infobox Weltraum',
            u'Vorlage:Navigationsleiste Raumfahrt',
        ],
        'en': [
            u'Wikipedia:Links to disambiguating pages',
            u'Wikipedia:Disambiguation pages with links',
            u'Wikipedia:Multiple-place names \\([A-Z]\\)',
            u'Wikipedia:Non-unique personal name',
            u"User:Jerzy/Disambiguation Pages i've Editted",
            u'User:Gareth Owen/inprogress',
            u'TLAs from [A-Z][A-Z][A-Z] to [A-Z][A-Z][A-Z]',
            u'List of all two-letter combinations',
            u'User:Daniel Quinlan/redirects.+',
            u'User:Oliver Pereira/stuff',
            u'Wikipedia:French Wikipedia language links',
            u'Wikipedia:Polish language links',
            u'Wikipedia:Undisambiguated abbreviations/.+',
            u'List of acronyms and initialisms',
            u'Wikipedia:Usemod article histories',
            u'User:Pizza Puzzle/stuff',
            u'List of generic names of political parties',
            u'Talk:List of initialisms/marked',
            u'Talk:List of initialisms/sorted',
            u'Talk:Programming language',
            u'Talk:SAMPA/To do',
            u"Wikipedia:Outline of Roget's Thesaurus",
            u'User:Wik/Articles',
            u'User:Egil/Sandbox',
            u'Wikipedia talk:Make only links relevant to the context',
            u'Wikipedia:Common words, searching for which is not possible',
        ],
        'fa': [
            u'ویکی‌پدیا:فهرست صفحات ابهام‌زدایی',
        ],
        'fi': [
            u'Wikipedia:Luettelo täsmennyssivuista',
            u'Wikipedia:Luettelo (täsmennyssivuista)',
            u'Wikipedia:Täsmennyssivu',
        ],
        'fr': [
            u'Wikipédia:Liens aux pages d'homonymie',
            u'Wikipédia:Homonymie',
            u'Wikipédia:Homonymie/Homonymes dynastiques',
            'Wikipédia:Prise de décision, noms des membres '
            'de dynasties/liste des dynastiens',
            u'Liste de toutes les combinaisons de deux lettres',
            u'Wikipédia:Log d'upload/.*',
            u'Sigles de trois lettres de [A-Z]AA à [A-Z]ZZ',
            u'Wikipédia:Pages sans interwiki,.'
        ],
        'fy': [
            u'Wikipedy:Fangnet',
        ],
        'hu': [
            # hu:Wikipédia:Kocsmafal (egyéb)#Hol nem kell egyértelműsíteni?
            # 2012-02-08
            u'Wikipédia:(?!Sportműhely/Eddigi cikkeink).*',
            u'.*\\(egyértelműsítő lap\\)$',
            u'.*[Vv]ita:.*',
            u'Szerkesztő:[^/]+$',
        ],
        'ia': [
            u'Categoria:Disambiguation',
            u'Wikipedia:.+',
            u'Usator:.+',
            u'Discussion Usator:.+',
        ],
        'it': [
            u'Aiuto:Disambigua/Disorfanamento',
            u'Discussioni utente:.+',
            u'Utente:Civvì/disorfanamento',
        ],
        'kk': [
            u'Санат:Айрықты бет',
        ],
        'ko': [
            u'위키백과:(동음이의) 문서의 목록',
            u'위키백과:동음이의어 문서의 목록',
        ],
        'lt': [
            u'Wikipedia:Rodomi nukreipiamieji straipsniai',
        ],
        'nl': [
            u"Gebruiker:.*",
            u"Overleg gebruiker:.+[aA]rchief.*",
            u"Overleg gebruiker:Pven",
            u"Portaal:.+[aA]rchief.*",
            u"Wikipedia:Humor en onzin.*",
            u"Wikipedia:Links naar doorverwijspagina's/Winkeldochters.*",
            u"Wikipedia:Project aanmelding bij startpagina's",
            u"Wikipedia:Wikiproject Roemeense gemeenten/Doorverwijspagina's",
            u'Categorie:Doorverwijspagina',
            u'Lijst van Nederlandse namen van pausen',
            u'Overleg Wikipedia:Discussie spelling 2005',
            u'Overleg Wikipedia:Doorverwijspagina',
            u'Overleg Wikipedia:Logboek.*',
            u'Wikipedia:Logboek.*',
            u'Overleg gebruiker:Sybren/test.*',
            u'Overleg gebruiker:([0-9][0-9]?[0-9]?\\.){3}[0-9][0-9]?[0-9]?',
            u'Overleg:Lage Landen (staatkunde)',
            u'Wikipedia:.*[aA]rchief.*',
            u'Wikipedia:Doorverwijspagina',
            u'Wikipedia:Lijst van alle tweeletter-combinaties',
            u'Wikipedia:Onderhoudspagina',
            u'Wikipedia:Ongelijke redirects',
            u'Wikipedia:Protection log',
            u'Wikipedia:Te verwijderen.*',
            u'Wikipedia:Top 1000 van meest bekeken artikelen',
            u'Wikipedia:Wikipedianen met een encyclopedisch artikel',
            u'Wikipedia:Woorden die niet als zoekterm gebruikt kunnen worden',
            u'Overleg gebruiker:Taka(/.*)?',
            u"Wikipedia:Links naar doorverwijspagina's/Artikelen",
            u"Wikipedia:Wikiproject/Redirects/.*",
            u"Wikipedia:Wikiproject/Muziek/Overzicht/.*",
            u"Wikipedia:Wikiproject/Roemeense gemeenten/Doorverwijspagina's",
            u"Overleg Wikipedia:Wikiproject/Redirects.*",
            u"Wikipedia:Links naar doorverwijspagina's/Amsterdamconstructie",
        ],
        'pl': [
            u'Wikipedysta:.+',
            u'Dyskusja.+:.+',
        ],
        'pt': [
            u'Usuário:.+',
            u'Usuário Discussão:.+',
            u'Discussão:.+',
            u'Lista de combinações de duas letras',
            u'Wikipedia:Lista de páginas de desambiguação.+',
            u'Wikipedia:Páginas para eliminar/.+',
        ],
        'ru': [
            u'Категория:Disambig',
            u'Википедия:Страницы разрешения неоднозначностей',
            u'Википедия:Вики-уборка/Статьи без языковых ссылок',
            u'Википедия:Страницы с пометкой «(значения)»',
            u'Список общерусских фамилий',
        ],
    },
    'memoryalpha': {
        'en': [
            u'Memory Alpha:Links to disambiguating pages'
        ],
        'de': [
            u'Memory Alpha:Liste der Wortklärungsseiten'
        ],
    },
}

def correctcap(link, text):
    """Return the link capitalized/uncapitalized according to the text.

    @param link: link page
    @type link: pywikibot.Page
    @param text: the wikitext that is supposed to refer to the link
    @type text: str
    @return: uncapitalized title of the link if the text links to the link
        with an uncapitalized title, else capitalized
    @rtype: str

    """
    linkupper = link.title()
    linklower = first_lower(linkupper)
    if "[[%s]]" % linklower in text or "[[%s|" % linklower in text:
        return linklower
    else:
        return linkupper

class ReferringPageGeneratorWithIgnore(object):

    """Referring Page generator, with an ignore manager."""

    def __init__(self, disambPage, primary=False, minimum=0, main_only=False):
        """Constructor.

        @type disambPage: pywikibot.Page
        @type primary: bool
        @type minimum: int
        @type main_only: bool
        @rtype: None

        """
        self.disambPage = disambPage
        # if run with the -primary argument, enable the ignore manager
        self.primaryIgnoreManager = PrimaryIgnoreManager(disambPage,
                                                         enabled=primary)
        self.minimum = minimum
        self.main_only = main_only

    def __iter__(self):
        """Yield pages."""
        # TODO: start yielding before all referring pages have been found
        refs = [
            page for page in self.disambPage.getReferences(
                follow_redirects=False,
                withTemplateInclusion=False,
                namespaces=0 if self.main_only else None
            )
        ]
        pywikibot.output(u"Found %d references." % len(refs))
        # Remove ignorables
        if self.disambPage.site.family.name in ignore_title and \
           self.disambPage.site.lang in ignore_title[
               self.disambPage.site.family.name]:
            for ig in ignore_title[self.disambPage.site.family.name
                                   ][self.disambPage.site.lang]:
                for i in range(len(refs) - 1, -1, -1):
                    if re.match(ig, refs[i].title()):
                        pywikibot.log(u'Ignoring page %s'
                                      % refs[i].title())
                        del refs[i]
                    elif self.primaryIgnoreManager.isIgnored(refs[i]):
                        del refs[i]
        if len(refs) < self.minimum:
            pywikibot.output(u"Found only %d pages to work on; skipping."
                             % len(refs))
            return
        pywikibot.output(u"Will work on %d pages." % len(refs))
        for ref in refs:
            yield ref

class PrimaryIgnoreManager(object):

    """
    Primary ignore manager.

    If run with the -primary argument, reads from a file which pages should
    not be worked on; these are the ones where the user pressed n last time.
    If run without the -primary argument, doesn't ignore any pages.

    """

    def __init__(self, disambPage, enabled=False):
        """Constructor.

        @type disambPage: pywikibot.Page
        @type enabled: bool
        @rtype: None

        """
        self.disambPage = disambPage
        self.enabled = enabled
        self.ignorelist = []

        folder = config.datafilepath('disambiguations')
        if os.path.exists(folder):
            self._read_ignorelist(folder)

    def _read_ignorelist(self, folder):
        """Read pages to be ignored from file.

        @type folder: str
        @rtype: None

        """
        filename = os.path.join(
            folder, self.disambPage.title(as_filename=True) + '.txt')
        try:
            # The file is stored in the disambiguation/ subdir.
            # Create if necessary.
            f = codecs.open(filename, 'r', 'utf-8')
            for line in f.readlines():
                # remove trailing newlines and carriage returns
                while line[-1] in ['\n', '\r']:
                    line = line[:-1]
                # skip empty lines
                if line != '':
                    self.ignorelist.append(line)
            f.close()
        except IOError:
            pass

    def isIgnored(self, refPage):
        """Return if refPage is to be ignored.

        @type refPage: pywikibot.Page
        @rtype: bool

        """
        return self.enabled and refPage.title(asUrl=True) in self.ignorelist

    def ignore(self, refPage):
        """Write page to ignorelist.

        @type refPage: pywikibot.Page
        @rtype: None

        """
        if self.enabled:
            # Skip this occurrence next time.
            filename = config.datafilepath(
                'disambiguations',
                self.disambPage.title(asUrl=True) + '.txt')
            try:
                # Open file for appending. If none exists, create a new one.
                f = codecs.open(filename, 'a', 'utf-8')
                f.write(refPage.title(asUrl=True) + '\n')
                f.close()
            except IOError:
                pass

class AddAlternativeOption(OutputProxyOption):

    """Add a new alternative."""

    def result(self, value):
        """Add the alternative and then list them."""
        newAlternative = pywikibot.input(u'New alternative:')
        self._outputter.sequence.append(newAlternative)
        super(AddAlternativeOption, self).result(value)

class EditOption(StandardOption):

    """Edit the text."""

    def __init__(self, option, shortcut, text, start, title):
        """Constructor.

        @type option: str
        @type shortcut: str
        @type text: str
        @type start: int
        @type title: str
        @rtype: None

        """
        super(EditOption, self).__init__(option, shortcut)
        self._text = text
        self._start = start
        self._title = title

    @property
    def stop(self):
        """Return whether if user didn't press cancel and changed it.

        @rtype: bool

        """
        return self.new_text and self.new_text != self._text

    def result(self, value):
        """Open a text editor and let the user change it."""
        editor = editarticle.TextEditor()
        self.new_text = editor.edit(self._text, jumpIndex=self._start,
                                    highlight=self._title)
        return super(EditOption, self).result(value)

class ShowPageOption(StandardOption):

    """Show the page's contents in an editor."""

    def __init__(self, option, shortcut, start, page):
        """Constructor."""
        super(ShowPageOption, self).__init__(option, shortcut, False)
        self._start = start
        if page.isRedirectPage():
            page = page.getRedirectTarget()
        self._page = page

    def result(self, value):
        """Open a text editor and show the text."""
        editor = editarticle.TextEditor()
        editor.edit(self._page.text,
                    jumpIndex=self._start,
                    highlight=self._page.title())

class AliasOption(StandardOption):

    """An option allowing multiple aliases which also select it."""

    def __init__(self, option, shortcuts, stop=True):
        """Constructor."""
        super(AliasOption, self).__init__(option, shortcuts[0], stop)
        self._aliases = frozenset(s.lower() for s in shortcuts[1:])

    def test(self, value):
        """Test aliases and combine it with the original test."""
        return value.lower() in self._aliases or super(AliasOption,
                                                       self).test(value)

class DisambiguationRobot(Bot):

    """Disambiguation bot."""

    ignore_contents = {
        'de': (u'{{[Ii]nuse}}',
               u'{{[Ll]öschen}}',
               ),
        'fi': (u'{{[Tt]yöstetään}}',
               ),
        'kk': (u'{{[Ii]nuse}}',
               u'{{[Pp]rocessing}}',
               ),
        'nl': (u'{{wiu2}}',
               u'{{nuweg}}',
               ),
        'ru': (u'{{[Ii]nuse}}',
               u'{{[Pp]rocessing}}',
               ),
    }

    primary_redir_template = {
        # Page.templates() format, first letter uppercase
        'hu': u'Egyért-redir',
    }

    def __init__(self, always, alternatives, getAlternatives, dnSkip,
                 generator, primary, main_only, minimum=0):
        """Constructor."""
        super(DisambiguationRobot, self).__init__()
        self.always = always
        self.alternatives = alternatives
        self.getAlternatives = getAlternatives
        self.dnSkip = dnSkip
        self.generator = generator
        self.primary = primary
        self.main_only = main_only
        self.minimum = minimum

        self.site = self.mysite = pywikibot.Site()
        self.mylang = self.mysite.lang
        self.comment = None

        self.dn_template_str = i18n.translate(self.mysite, dn_template)

        self.setupRegexes()

    def checkContents(self, text):
        """
        Check if the text matches any of the ignore regexes.

        For a given text, returns False if none of the regular
        expressions given in the dictionary at the top of this class
        matches a substring of the text.
        Otherwise returns the substring which is matched by one of
        the regular expressions.
        """
        for ig in self.ignore_contents_regexes:
            match = ig.search(text)
            if match:
                return match.group()
        return None

    def makeAlternativesUnique(self):
        """Remove duplicate items from self.alternatives.

        Preserve the order of alternatives.
        @rtype: None

        """
        seen = set()
        self.alternatives = [
            i for i in self.alternatives if i not in seen and not seen.add(i)
        ]

    def listAlternatives(self):
        """Show a list of alternatives."""
        list = u'\n'
        for i in range(len(self.alternatives)):
            list += (u"%3i - %s\n" % (i, self.alternatives[i]))
        pywikibot.output(list)

    def setupRegexes(self):
        """Compile regular expressions."""
        self.ignore_contents_regexes = []
        if self.mylang in self.ignore_contents:
            for ig in self.ignore_contents[self.mylang]:
                self.ignore_contents_regexes.append(re.compile(ig))

        linktrail = self.mysite.linktrail()
        self.trailR = re.compile(linktrail)
        # The regular expression which finds links. Results consist of four
        # groups:
        # group title is the target page title, that is, everything before
        # | or ].
        # group section is the page section. It'll include the # to make life
        # easier for us.
        # group label is the alternative link title, that's everything
        # between | and ].
        # group linktrail is the link trail, that's letters after ]] which
        # are part of the word.
        # note: the definition of 'letter' varies from language to language.
        self.linkR = re.compile(r'''
            \[\[  (?P<title>     [^\[\]\|#]*)
                  (?P<section> \#[^\]\|]*)?
               (\|(?P<label>     [^\]]*))?  \]\]
            (?P<linktrail>%s)''' % linktrail,
                                flags=re.X)

    def treat(self, refPage, disambPage):
        """Treat a page.

        @param disambPage: the disambiguation page or redirect we don't want
            anything to link to
        @type disambPage: pywikibot.Page
        @param refPage: a page linking to disambPage
        @type refPage: pywikibot.Page
        @return: False if the user pressed q to completely quit the program,
            True otherwise
        @rtype: bool

        """
        # TODO: break this function up into subroutines!

        self.current_page = refPage
        include = False
        unlink_counter = 0
        new_targets = []
        try:
            text = refPage.get()
            ignoreReason = self.checkContents(text)
            if ignoreReason:
                pywikibot.output('\n\nSkipping %s because it contains %s.\n\n'
                                 % (refPage.title(), ignoreReason))
            else:
                include = True
        except pywikibot.IsRedirectPage:
            pywikibot.output(u'%s is a redirect to %s'
                             % (refPage.title(), disambPage.title()))
            if disambPage.isRedirectPage():
                target = self.alternatives[0]
                if pywikibot.input_yn(u'Do you want to make redirect %s point '
                                      'to %s?' % (refPage.title(), target),
                                      default=False, automatic_quit=False):
                    redir_text = '#%s [[%s]]' \
                                 % (self.mysite.redirect(), target)
                    try:
                        refPage.put_async(redir_text, summary=self.comment)
                    except pywikibot.PageNotSaved as error:
                        pywikibot.output(u'Page not saved: %s' % error.args)
            else:
                choice = pywikibot.input_choice(
                    u'Do you want to work on pages linking to %s?'
                    % refPage.title(),
                    [('yes', 'y'), ('no', 'n'), ('change redirect', 'c')], 'n',
                    automatic_quit=False)
                if choice == 'y':
                    gen = ReferringPageGeneratorWithIgnore(
                        refPage, self.primary, main_only=self.main_only
                    )
                    preloadingGen = pagegenerators.PreloadingGenerator(gen)
                    for refPage2 in preloadingGen:
                        # run until the user selected 'quit'
                        if not self.treat(refPage2, refPage):
                            break
                elif choice == 'c':
                    text = refPage.get(get_redirect=True)
                    include = "redirect"
        except pywikibot.NoPage:
            pywikibot.output(
                u'Page [[%s]] does not seem to exist?! Skipping.'
                % refPage.title())
            include = False
        if include in (True, "redirect"):
            # save the original text so we can show the changes later
            original_text = text
            n = 0
            curpos = 0
            dn = False
            edited = False
            # This loop will run until we have finished the current page
            while True:
                m = self.linkR.search(text, pos=curpos)
                if not m:
                    if n == 0:
                        pywikibot.output(u"No changes necessary in %s"
                                         % refPage.title())
                        return True
                    else:
                        # stop loop and save page
                        break
                # Ensure that next time around we will not find this same hit.
                curpos = m.start() + 1
                try:
                    foundlink = pywikibot.Link(m.group('title'),
                                               disambPage.site)
                    foundlink.parse()
                except pywikibot.Error:
                    continue
                # ignore interwiki links
                if foundlink.site != disambPage.site:
                    continue
                # Check whether the link found is to disambPage.
                try:
                    if foundlink.canonical_title() != disambPage.title():
                        continue
                except pywikibot.Error:
                    # must be a broken link
                    pywikibot.log(u"Invalid link [[%s]] in page [[%s]]"
                                  % (m.group('title'), refPage.title()))
                    continue
                n += 1
                # how many bytes should be displayed around the current link
                context = 60
                # check if there's a dn-template here already
                if (self.dnSkip and self.dn_template_str and
                        self.dn_template_str[:-2] in text[
                            m.end():m.end() + len(self.dn_template_str) + 8]):
                    continue

                edit = EditOption('edit page', 'e', text, m.start(),
                                  disambPage.title())
                context_option = HighlightContextOption(
                    'more context', 'm', text, 60, start=m.start(),
                    end=m.end())
                context_option.before_question = True

                options = [ListOption(self.alternatives, ''),
                           ListOption(self.alternatives, 'r'),
                           StandardOption('skip link', 's'),
                           edit,
                           StandardOption('next page', 'n'),
                           StandardOption('unlink', 'u')]
                if self.dn_template_str:
                    # '?', '/' for old choice
                    options += [AliasOption('tag template %s' %
                                            self.dn_template_str,
                                            ['t', '?', '/'])]
                options += [context_option]
                if not edited:
                    options += [ShowPageOption('show disambiguation page', 'd',
                                               m.start(), disambPage)]
                options += [
                    OutputProxyOption('list', 'l',
                                      SequenceOutputter(self.alternatives)),
                    AddAlternativeOption('add new', 'a',
                                         SequenceOutputter(self.alternatives))]
                if edited:
                    options += [StandardOption('save in this form', 'x')]

                # TODO: Output context on each question
                answer = pywikibot.input_choice('Option', options,
                                                default=self.always,
                                                force=bool(self.always))
                if answer == 'x':
                    assert edited, 'invalid option before editing'
                    break
                elif answer == 's':
                    n -= 1  # TODO what's this for?
                    continue
                elif answer == 'e':
                    text = edit.new_text
                    edited = True
                    curpos = 0
                    continue
                elif answer == 'n':
                    # skip this page
                    if self.primary:
                        # If run with the -primary argument, skip this
                        # occurrence next time.
                        self.primaryIgnoreManager.ignore(refPage)
                    return True

                # The link looks like this:
                # [[page_title|link_text]]trailing_chars
                page_title = m.group('title')
                link_text = m.group('label')

                if not link_text:
                    # or like this: [[page_title]]trailing_chars
                    link_text = page_title
                if m.group('section') is None:
                    section = ''
                else:
                    section = m.group('section')
                trailing_chars = m.group('linktrail')
                if trailing_chars:
                    link_text += trailing_chars
                if answer == 't':
                    assert self.dn_template_str
                    # small chunk of text to search
                    search_text = text[m.end():m.end() + context]
                    # figure out where the link (and sentance) ends, put note
                    # there
                    end_of_word_match = re.search(r'\s', search_text)
                    if end_of_word_match:
                        position_split = end_of_word_match.start(0)
                    else:
                        position_split = 0
                    # insert dab needed template
                    text = (text[:m.end() + position_split] +
                            self.dn_template_str +
                            text[m.end() + position_split:])
                    dn = True
                    continue
                elif answer == 'u':
                    # unlink - we remove the section if there's any
                    text = text[:m.start()] + link_text + text[m.end():]
                    unlink_counter += 1
                    continue
                else:
                    # Check that no option from above was missed
                    assert isinstance(answer, tuple), 'only tuple answer left.'
                    assert answer[0] in ['r', ''], 'only valid tuple answers.'
                    if answer[0] == 'r':
                        # we want to throw away the original link text
                        replaceit = link_text == page_title
                    elif include == "redirect":
                        replaceit = True
                    else:
                        replaceit = False

                    new_page_title = answer[1]
                    repPl = pywikibot.Page(pywikibot.Link(new_page_title,
                                                          disambPage.site))
                    if (new_page_title[0].isupper() or
                            link_text[0].isupper()):
                        new_page_title = repPl.title()
                    else:
                        new_page_title = repPl.title()
                        new_page_title = first_lower(new_page_title)
                    if new_page_title not in new_targets:
                        new_targets.append(new_page_title)
                    if replaceit and trailing_chars:
                        newlink = "[[%s%s]]%s" % (new_page_title,
                                                  section,
                                                  trailing_chars)
                    elif replaceit or (new_page_title == link_text and
                                       not section):
                        newlink = "[[%s]]" % new_page_title
                    # check if we can create a link with trailing characters
                    # instead of a pipelink
                    elif (
                        (len(new_page_title) <= len(link_text)) and
                        (firstcap(link_text[:len(new_page_title)]) ==
                         firstcap(new_page_title)) and
                        (re.sub(self.trailR, '',
                                link_text[len(new_page_title):]) == '') and
                        (not section)
                    ):
                        newlink = "[[%s]]%s" \
                                  % (link_text[:len(new_page_title)],
                                     link_text[len(new_page_title):])
                    else:
                        newlink = "[[%s%s|%s]]" \
                                  % (new_page_title, section, link_text)
                    text = text[:m.start()] + newlink + text[m.end():]
                    continue

                pywikibot.output(text[max(0, m.start() - 30):m.end() + 30])
            if text == original_text:
                pywikibot.output(u'\nNo changes have been made:\n')
            else:
                pywikibot.output(u'\nThe following changes have been made:\n')
                pywikibot.showDiff(original_text, text)
                pywikibot.output(u'')
                # save the page
                self.setSummaryMessage(disambPage, new_targets, unlink_counter,
                                       dn)
                try:
                    refPage.put_async(text, summary=self.comment)
                except pywikibot.LockedPage:
                    pywikibot.output(u'Page not saved: page is locked')
                except pywikibot.PageNotSaved as error:
                    pywikibot.output(u'Page not saved: %s' % error.args)
        return True

    def findAlternatives(self, disambPage):
        """Extend self.alternatives using correctcap of disambPage.linkedPages.

        @param disambPage: the disambiguation page
        @type disambPage: pywikibot.Page
        @return: True if everything goes fine, False otherwise
        @rtype: bool

        """
        if disambPage.isRedirectPage() and not self.primary:
            if (disambPage.site.lang in self.primary_redir_template and
                    self.primary_redir_template[disambPage.site.lang]
                    in disambPage.templates(get_redirect=True)):
                baseTerm = disambPage.title()
                for template in disambPage.templatesWithParams(
                        get_redirect=True):
                    if template[0] == self.primary_redir_template[
                        disambPage.site.lang] \
                            and len(template[1]) > 0:
                        baseTerm = template[1][1]
                disambTitle = primary_topic_format[self.mylang] % baseTerm
                try:
                    disambPage2 = pywikibot.Page(
                        pywikibot.Link(disambTitle, self.mysite))
                    links = disambPage2.linkedPages()
                    links = [correctcap(l, disambPage2.get()) for l in links]
                except pywikibot.NoPage:
                    pywikibot.output(u"No page at %s, using redirect target."
                                     % disambTitle)
                    links = disambPage.linkedPages()[:1]
                    links = [correctcap(l, disambPage.get(get_redirect=True))
                             for l in links]
                self.alternatives += links
            else:
                try:
                    target = disambPage.getRedirectTarget().title()
                    self.alternatives.append(target)
                except pywikibot.NoPage:
                    pywikibot.output(u"The specified page was not found.")
                    user_input = pywikibot.input(u"""\
Please enter the name of the page where the redirect should have pointed at,
or press enter to quit:""")
                    if user_input == "":
                        self.quit()
                    else:
                        self.alternatives.append(user_input)
                except pywikibot.IsNotRedirectPage:
                    pywikibot.output(
                        u"The specified page is not a redirect. Skipping.")
                    return False
        elif self.getAlternatives:
            # not disambPage.isRedirectPage() or self.primary
            try:
                if self.primary:
                    try:
                        disambPage2 = pywikibot.Page(
                            pywikibot.Link(
                                primary_topic_format[self.mylang]
                                % disambPage.title(),
                                self.mysite))
                        links = disambPage2.linkedPages()
                        links = [correctcap(l, disambPage2.get())
                                 for l in links]
                    except pywikibot.NoPage:
                        pywikibot.output(
                            'Page does not exist; using first link in page %s.'
                            % disambPage.title())
                        links = disambPage.linkedPages()[:1]
                        links = [correctcap(l, disambPage.get())
                                 for l in links]
                else:
                    try:
                        links = disambPage.linkedPages()
                        links = [correctcap(l, disambPage.get())
                                 for l in links]
                    except pywikibot.NoPage:
                        pywikibot.output(u"Page does not exist, skipping.")
                        return False
            except pywikibot.IsRedirectPage:
                pywikibot.output(u"Page is a redirect, skipping.")
                return False
            self.alternatives += links
        return True

    def setSummaryMessage(self, disambPage, new_targets=[], unlink_counter=0,
                          dn=False):
        """Setup i18n summary message."""
        # make list of new targets
        comma = self.mysite.mediawiki_message(u"comma-separator")
        targets = comma.join(u'[[%s]]' % page_title
                             for page_title in new_targets)

        if not targets:
            targets = i18n.twtranslate(self.mysite,
                                       'solve_disambiguation-unknown-page')

        # first check whether user has customized the edit comment
        if (self.mysite.family.name in config.disambiguation_comment and
                self.mylang in config.disambiguation_comment[
                    self.mysite.family.name]):
            try:
                self.comment = i18n.translate(
                    self.mysite,
                    config.disambiguation_comment[self.mysite.family.name],
                    fallback=True) % (disambPage.title(), targets)

            # Backwards compatibility, type error probably caused by too
            # many arguments for format string
            except TypeError:
                self.comment = i18n.translate(
                    self.mysite,
                    config.disambiguation_comment[self.mysite.family.name],
                    fallback=True) % disambPage.title()
        elif disambPage.isRedirectPage():
            # when working on redirects, there's another summary message
            if unlink_counter and not new_targets:
                self.comment = i18n.twtranslate(
                    self.mysite,
                    'solve_disambiguation-redirect-removed',
                    {'from': disambPage.title(),
                     'count': unlink_counter})
            elif dn and not new_targets:
                self.comment = i18n.twtranslate(
                    self.mysite,
                    'solve_disambiguation-redirect-adding-dn-template',
                    {'from': disambPage.title()})
            else:
                self.comment = i18n.twtranslate(
                    self.mysite, 'solve_disambiguation-redirect-resolved',
                    {'from': disambPage.title(),
                     'to': targets,
                     'count': len(new_targets)})
        else:
            if unlink_counter and not new_targets:
                self.comment = i18n.twtranslate(
                    self.mysite, 'solve_disambiguation-links-removed',
                    {'from': disambPage.title(),
                     'count': unlink_counter})
            elif dn and not new_targets:
                self.comment = i18n.twtranslate(
                    self.mysite, 'solve_disambiguation-adding-dn-template',
                    {'from': disambPage.title()})
            else:
                self.comment = i18n.twtranslate(
                    self.mysite, 'solve_disambiguation-links-resolved',
                    {'from': disambPage.title(),
                     'to': targets,
                     'count': len(new_targets)})

    def run(self):
        """Run the bot."""
        for disambPage in self.generator:
            self.primaryIgnoreManager = PrimaryIgnoreManager(
                disambPage, enabled=self.primary)

            if not self.findAlternatives(disambPage):
                continue

            pywikibot.output('\nAlternatives for %s' % disambPage)
            self.makeAlternativesUnique()
            # sort possible choices
            if config.sort_ignore_case:
                self.alternatives.sort(key=lambda x: x.lower())
            else:
                self.alternatives.sort()
            SequenceOutputter(self.alternatives).output()

            gen = ReferringPageGeneratorWithIgnore(
                disambPage,
                self.primary,
                minimum=self.minimum,
                main_only=self.main_only
            )
            preloadingGen = pagegenerators.PreloadingGenerator(gen)
            for refPage in preloadingGen:
                if not self.primaryIgnoreManager.isIgnored(refPage):
                    try:
                        self.treat(refPage, disambPage)
                    except QuitKeyboardInterrupt:
                        pywikibot.output('\nUser quit %s bot run...' %
                                         self.__class__.__name__)
                        return

            # clear alternatives before working on next disambiguation page
            self.alternatives = []

def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: list of unicode
    """
    # the option that's always selected when the bot wonders what to do with
    # a link. If it's None, the user is prompted (default behaviour).
    always = None
    alternatives = []
    getAlternatives = True
    dnSkip = False
    generator = None
    primary = False
    main_only = False

    # For sorting the linked pages, case can be ignored
    minimum = 0

    local_args = pywikibot.handle_args(args)
    generator_factory = pagegenerators.GeneratorFactory(
        positional_arg_name='page')

    for arg in local_args:
        if arg.startswith('-primary:'):
            primary = True
            getAlternatives = False
            alternatives.append(arg[9:])
        elif arg == '-primary':
            primary = True
        elif arg.startswith('-always:'):
            always = arg[8:]
        elif arg.startswith('-pos:'):
            if arg[5] != ':':
                mysite = pywikibot.Site()
                page = pywikibot.Page(pywikibot.Link(arg[5:], mysite))
                if page.exists():
                    alternatives.append(page.title())
                else:
                    if pywikibot.input_yn(
                            u'Possibility %s does not actually exist. Use it '
                            'anyway?' % page.title(),
                            default=False, automatic_quit=False):
                        alternatives.append(page.title())
            else:
                alternatives.append(arg[5:])
        elif arg == '-just':
            getAlternatives = False
        elif arg == '-dnskip':
            dnSkip = True
        elif arg == '-main':
            main_only = True
        elif arg.startswith('-min:'):
            minimum = int(arg[5:])
        elif arg.startswith('-start'):
            try:
                generator = pagegenerators.CategorizedPageGenerator(
                    pywikibot.Site().disambcategory(),
                    start=arg[7:], namespaces=[0])
            except pywikibot.NoPage:
                pywikibot.output(
                    'Disambiguation category for your wiki is not known.')
                raise
        else:
            generator_factory.handleArg(arg)

    site = pywikibot.Site()

    generator = generator_factory.getCombinedGenerator(generator)

    if not generator:
        pywikibot.bot.suggest_help(missing_generator=True)
        return False

    site.login()

    bot = DisambiguationRobot(always, alternatives, getAlternatives, dnSkip,
                              generator, primary, main_only,
                              minimum=minimum)
    bot.run()

if __name__ == "__main__":
    main()


text:
namespace System.Activities.Debugger
{
    using System;
    using System.Activities.Debugger.Symbol;
    using System.Diagnostics;
    using System.Linq;
    using System.Runtime;

    // Identifies a specific location in the target source code.
    //
    // This source information is used in creating PDBs, which will be passed to the debugger,
    // which will resolve the source file based off its own source paths.
    // Source ranges can:
    // * refer to just an entire single line.
    // * can be a subset within a single line (when StartLine == EndLine)
    // * can also span multiple lines.
    // When column info is provided, the debugger will highlight the characters starting at the start line and start column,
    // and going up to but not including the character specified by the end line and end column.
    [Fx.Tag.SecurityNote(Miscellaneous = "RequiresReview - Our partial trust mechanisms require that this class remain Immutable. Do not add code that allows an instance of this class to change after creation without strict review.")]
    [DebuggerNonUserCode]
    [Serializable]
    [Fx.Tag.XamlVisible(false)]
    public class SourceLocation
    {
        string fileName;
        int startLine;
        int endLine;
        int startColumn;
        int endColumn;
        byte[] checksum;

        // Define a source location from a filename and line-number (1-based).
        // This is a convenience constructor to specify the entire line.
        // This does not load the source file to determine column ranges.
        public SourceLocation(string fileName, int line)
            : this(fileName, line, 1, line, int.MaxValue)
        {
        }

        public SourceLocation(
           string fileName,
           int startLine,
           int startColumn,
           int endLine,
           int endColumn)
            : this(fileName, null, startLine, startColumn, endLine, endColumn)
        {
        }

        // Define a source location in a file.
        // Line/Column are 1-based.
        internal SourceLocation(
            string fileName,
            byte[] checksum,
            int startLine,
            int startColumn,
            int endLine,
            int endColumn)
        {
            if (startLine <= 0)
            {
                throw FxTrace.Exception.Argument("startLine", SR.InvalidSourceLocationLineNumber("startLine", startLine));
            }

            if (startColumn <= 0)
            {
                throw FxTrace.Exception.Argument("startColumn", SR.InvalidSourceLocationColumn("startColumn", startColumn));
            }

            if (endLine <= 0)
            {
                throw FxTrace.Exception.Argument("endLine", SR.InvalidSourceLocationLineNumber("endLine", endLine));
            }

            if (endColumn <= 0)
            {
                throw FxTrace.Exception.Argument("endColumn", SR.InvalidSourceLocationColumn("endColumn", endColumn));
            }

            if (startLine > endLine)
            {
                throw FxTrace.Exception.ArgumentOutOfRange("endLine", endLine, SR.OutOfRangeSourceLocationEndLine(startLine));
            }

            if ((startLine == endLine) && (startColumn > endColumn))
            {
                throw FxTrace.Exception.ArgumentOutOfRange("endColumn", endColumn, SR.OutOfRangeSourceLocationEndColumn(startColumn));
            }

            this.fileName = (fileName != null) ? fileName.ToUpperInvariant() : null;
            this.startLine = startLine;
            this.endLine = endLine;
            this.startColumn = startColumn;
            this.endColumn = endColumn;
            this.checksum = checksum;
        }

        public string FileName
        {
            get { return this.fileName; }
        }

        // Get the 1-based start line.
        public int StartLine
        {
            get { return this.startLine; }
        }

        // Get the 1-based starting column.
        public int StartColumn
        {
            get { return this.startColumn; }
        }

        // Get the 1-based end line. This should be greater or equal to StartLine.
        public int EndLine
        {
            get { return this.endLine; }
        }

        // Get the 1-based ending column.
        public int EndColumn
        {
            get { return this.endColumn; }
        }

        // get the checksum of the source file
        internal byte[] Checksum
        {
            get { return this.checksum; }
        }

        public bool IsSingleWholeLine
        {
            get
            {
                return this.endColumn == int.MaxValue && this.startLine == this.endLine && this.startColumn == 1;
            }
        }

        // Equality comparison function. This checks for strict equality and
        // not for superset or subset relationships.
        public override bool Equals(object obj)
        {
            SourceLocation rsl = obj as SourceLocation;
            if (rsl == null)
            {
                return false;
            }

            if (this.FileName != rsl.FileName)
            {
                return false;
            }

            if (this.StartLine != rsl.StartLine ||
                this.StartColumn != rsl.StartColumn ||
                this.EndLine != rsl.EndLine ||
                this.EndColumn != rsl.EndColumn)
            {
                return false;
            }

            if (this.Checksum == null ^ rsl.Checksum == null)
            {
                return false;
            }
            else if ((this.Checksum != null && rsl.Checksum != null) && !this.Checksum.SequenceEqual(rsl.Checksum))
            {
                return false;
            }

            // everything matches
            return true;
        }

        // Get a hash code.
        public override int GetHashCode()
        {
            return (string.IsNullOrEmpty(this.FileName) ? 0 : this.FileName.GetHashCode()) ^
                    this.StartLine.GetHashCode() ^
                    this.StartColumn.GetHashCode() ^
                    ((this.Checksum == null) ? 0 : SymbolHelper.GetHexStringFromChecksum(this.Checksum).GetHashCode());
        }

        internal static bool IsValidRange(int startLine, int startColumn, int endLine, int endColumn)
        {
            return
                (startLine > 0) && (startColumn > 0) && (endLine > 0) && (endColumn > 0) &&
                ((startLine < endLine) || (startLine == endLine) && (startColumn < endColumn));

        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
import * as React from 'react';
import {
  A11yTitleType,
  AlignSelfType,
  GridAreaType,
  MarginType,
} from '../../utils';

export interface TableProps {
  a11yTitle?: A11yTitleType;
  alignSelf?: AlignSelfType;
  caption?: string;
  gridArea?: GridAreaType;
  margin?: MarginType;
}

type htmlTableProps = JSX.IntrinsicElements['table'];

export interface TableExtendedProps extends TableProps, htmlTableProps {}

declare const Table: React.FC<TableExtendedProps>;

export { Table };


text:
typedef struct p4_ctrl_msg {
	uint8_t type;
	uint32_t xid;
	char* table_name;
	uint8_t action_type;
	char* action_name;
	int num_action_params;
	struct p4_action_parameter* action_params[P4_MAX_NUMBER_OF_ACTION_PARAMETERS];
	int num_field_matches;
	struct p4_field_match_header* field_matches[P4_MAX_NUMBER_OF_FIELD_MATCHES];
} p4_ctrl_msg_t;

typedef uint32_t* (*p4_cnt_read)(char*, int*, bool);
typedef void (*p4_msg_callback)(struct p4_ctrl_msg*);

int handle_p4_msg(char* buffer, int length, p4_msg_callback cb);
int handle_p4_ctrl_initialized(struct p4_header* header, struct p4_ctrl_msg* ctrl_m);
int handle_p4_set_default_action(struct p4_set_default_action* m, struct p4_ctrl_msg* ctrl_m);
int handle_p4_add_table_entry(struct p4_add_table_entry* m, struct p4_ctrl_msg* ctrl_m);

#endif


text:
package dpla.ingestion3.reports

import org.apache.spark.sql.{DataFrame, Dataset, SparkSession}
import dpla.ingestion3.model._
import org.apache.spark.sql.functions.{col, explode}

case class PropertyDistinctValueRpt(value: Seq[String])
/**
  * Property Distinct Value QA report.  Takes one field and gives a count of unique
  * values.
  *
  * Fields currently supported:
  *   - sourceResource.format
  *   - sourceResource.rights
  *   - sourceResource.type
  *
  * @example Example invocation:
  *
  *   $ sbt "run-main dpla.ingestion3.ReporterMain \
  *     /path/to/enriched-data.avro /path/to/propvalreport local[2] \
  *     propertyValue sourceResource.type"
  *
  *     ... This produces a directory named 'propvalreport' with a file in it
  *     named 'part-< ... >.csv', with contents like:
  *
  *     value,count
  *     physicalobject,1
  *     dataset,13
  *     [...]
  *
  * @param inputURI         Input URI or file path (Avro file / directory)
  * @param outputURI        Output URI or file path (directory containing CSV
  *                         file)
  * @param params           Additional parameters, currently:
  *                         params(0): The DPLA MAP field to analyze
  */
class PropertyDistinctValueReport(
                                   val input: Dataset[OreAggregation],
                                   val spark: SparkSession,
                                   val params: Array[String]
                                 ) extends Report with Serializable {

  /*
   * We set instance fields from constructor arguments, and override accessor
   * methods in order to make the class more amenable to unit testing.
   * However ...
   * FIXME: It's not clear how to write unit tests that involve Dataframes
   * and Datasets, so there are no such tests for this class.
   *
   */
  override val sparkAppName: String = "PropertyDistinctValueReport"
  override def getInput: Dataset[OreAggregation] = input
  override def getSparkSession: SparkSession = spark
  override def getParams: Option[Array[String]] = {
    if (params.nonEmpty) Some(params) else None
  }

  /**
    * Process the incoming data set.
    *
    * @see          Report.process()
    *
    * @param ds     Dataset of DplaMapData (mapped or enriched records)
    * @param spark  The Spark session, which contains encoding / parsing info.
    * @return       DataFrame, typically of Row[value: String, count: Int]
    */
  override def process(ds: Dataset[OreAggregation],
                       spark: SparkSession
                      ): DataFrame = {

    import spark.implicits._

    val token: String = getParams match {
      case Some(p) => p.headOption.getOrElse(throw new RuntimeException(s"No field specified"))
      case _ => throw new RuntimeException(s"No field specified")
    }

    val rptDs = token match {
      case "dataProvider" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(Seq(dplaMapData.dataProvider))))
      case "edmRights" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.edmRights.toSeq)))
      case "intermediateProvider" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.intermediateProvider.toSeq)))
      case "isShownAt" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(Seq(dplaMapData.isShownAt.uri))))
      case "sourceResource.alternateTitle" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.alternateTitle)))
      case "sourceResource.collection.title" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.collection)))
      case "sourceResource.contributor.name" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.contributor)))
      case "sourceResource.creator.name" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.creator)))
      case "sourceResource.date.originalSourceDate" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.date)))
      case "sourceResource.description" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.description)))
      case "sourceResource.extent" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.extent)))
      case "sourceResource.format" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(value = extractValue(dplaMapData.sourceResource.format)))
      case "sourceResource.genre" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.genre)))
      case "sourceResource.identifier" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.identifier)))
      case "sourceResource.language.providedLabel" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.language)))
      // Special mapping for concept
      case "sourceResource.language.concept" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(dplaMapData.sourceResource.language.map(l =>
          l.concept.getOrElse("__MISSING SkosConcept.concept__"))))
      case "sourceResource.place.name" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.place)))
      case "sourceResource.publisher.name" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.publisher)))
      case "sourceResource.relation" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.relation)))
      case "sourceResource.replacedBy" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.replacedBy)))
      case "sourceResource.replaces" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.replaces)))
      case "sourceResource.rights" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.rights)))
      case "sourceResource.rightsHolder.name" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.rightsHolder)))
      case "sourceResource.subject.providedLabel" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.subject)))
      case "sourceResource.temporal.originalSourceDate" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.temporal)))
      case "sourceResource.title" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.title)))
      case "sourceResource.type" =>
        ds.map(dplaMapData => PropertyDistinctValueRpt(extractValue(dplaMapData.sourceResource.`type`)))
      case x =>
        throw new RuntimeException(s"Unrecognized field name '$x'")
    }

    makeTable(rptDs, spark, token)
  }

  /**
    * Explodes the value column so that multiple values
    * occur on separate rows. Then the groupBy and count
    * operations are performed on the data
    *
    * @param rptDataset Dataset to report against
    * @param spark Spark session
    * @param token Column name
    * @return
    */
  def makeTable(rptDataset: Dataset[PropertyDistinctValueRpt],
                spark: SparkSession,
                token: String): DataFrame = {
    val sqlContext = spark.sqlContext
    // Periods are not allowed in column names...
    val colName = token.replace(".", "_")
    rptDataset.createOrReplaceTempView("tmpPropValRpt")

    sqlContext.sql("""SELECT * FROM tmpPropValRpt""")
      .withColumn(colName, explode(col("value")))
      .drop(col("value"))
      .groupBy(colName)
      .count()
      .orderBy(colName, "count")
  }
}


text:
_Author_        : Prakhar Kumar
_Date Created_  : 02/10/2016

**What is tested?**
_Importing external contacts & Export contacts_

Import External Contacts via CSV
-----

1. Download [sample test data](https://github.com/takbok/shared-contacts-admin/blob/master/testcases/test-data/sample2.csv).
2. Open [GAE Test](http://www.gae-test1.com/) in your browser.
3. Click on 'Choose File' or 'Browse' button depending on your browser.
4. Select the downloaded file from step # 1.
5. Enter the domain name, that is hosted on Google Apps, to which the contacts need to be imported to.
6. Click on 'Import CSV' button.
7. On the OAuth consent screen, enter the credentials that have the necessary permissions on the domain from #5 above.
8. Results should be displayed on screen as below.

![Import Results](https://raw.githubusercontent.com/takbok/shared-contacts-admin/master/testcases/images/test-case-04-01.jpeg)

Export Contacts to CSV
-----

1. Open [GAE Test](http://www.gae-test1.com) in your browser.
2. Enter the domain name, that is hosted on Google Apps, and for which Domain Shared Contacts need to be exported.
3. Click on 'Set Domain & Export CSV'
4. On the OAuth consent screen, enter the credentials that have the necessary permissions on the domain from #2 above.
5. A CSV file containing the contacts will be downloaded

Delete (All) Contacts
-----

1. Open [GAE Test](http://www.gae-test1.com) in your browser.
2. Enter the domain name, that is hosted on Google Apps, and for which Domain Shared Contacts need to be deleted.
3. Click on 'Delete All Contacts'
4. On the OAuth consent screen, enter the credentials that have the necessary permissions on the domain from #2 above.
5. A maximum of 100 contacts will be deleted (current limit in place by Google). The screen should say : **200 OK**


text:
using Microsoft.Azure.Management.Compute.Models;

namespace Microsoft.Azure.Commands.Compute.Automation.Models
{
    public partial class PSUpgradeOperationHistoricalStatusInfo : UpgradeOperationHistoricalStatusInfo
    {
    }
}


text:
using System.Collections.Generic;
using Microsoft.PythonTools.Parsing.Ast;
using Microsoft.VisualStudioTools.Navigation;
using Microsoft.VisualStudioTools.Parsing;

namespace Microsoft.PythonTools.Navigation {
    class FunctionScopeNode : IScopeNode {
        private readonly FunctionDefinition _func;

        public FunctionScopeNode(FunctionDefinition func) {
            _func = func;
        }

        public FunctionDefinition Definition {
            get {
                return _func;
            }
        }

        #region IScopeNode Members

        public LibraryNodeType NodeType {
            get {
                return LibraryNodeType.Members;
            }
        }

        public string Name {
            get { return _func.Name; }
        }

        public string Description {
            get { return _func.Body.Documentation; }
        }

        public SourceLocation Start {
            get { return AstScopeNode.FromPythonSourceLocation(_func.GetStart(_func.GlobalParent)); }
        }

        public SourceLocation End {
            get { return AstScopeNode.FromPythonSourceLocation(_func.GetEnd(_func.GlobalParent)); }
        }

        public IEnumerable<IScopeNode> NestedScopes {
            get { return AstScopeNode.EnumerateBody(_func.GlobalParent, _func.Body, false); }
        }

        #endregion
    }
}


text:
from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))

# Get the long description from the relevant file
with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='rocker',

    # Versions should comply with PEP440.  For a discussion on single-sourcing
    # the version across setup.py and the project code, see
    # https://packaging.python.org/en/latest/single_source_version.html
    version='0.1.0.dev7',

    description='Docker management tool',
    long_description=long_description,

    # The project's main homepage.
    url='https://github.com/mreithub/rocker',

    # Author details
    author='Manuel Reithuber',
    author_email='rocker@coding.zone',

    # Choose your license
    license='Apache',

    # See https://pypi.python.org/pypi?%3Aaction=list_classifiers
    classifiers=[
        # How mature is this project? Common values are
        #   3 - Alpha
        #   4 - Beta
        #   5 - Production/Stable
        'Development Status :: 3 - Alpha',

        # Indicate who your project is intended for
        'Intended Audience :: Developers',
        'Topic :: Software Development :: Build Tools',

        # Pick your license as you wish (should match "license" above)
        'License :: OSI Approved :: Apache Software License',

        # Specify the Python versions you support here. In particular, ensure
        # that you indicate whether you support Python 2, Python 3 or both.
#        'Programming Language :: Python :: 2',
#        'Programming Language :: Python :: 2.6',
#        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.2',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
    ],

    # What does your project relate to?
    keywords='development docker',

    # You can just specify the packages manually here if your project is
    # simple. Or you can use find_packages().
    packages=find_packages(exclude=['examples', 'docs', 'tests*']),

    # List run-time dependencies here.  These will be installed by pip when
    # your project is installed. For an analysis of "install_requires" vs pip's
    # requirements files see:
    # https://packaging.python.org/en/latest/requirements.html
    install_requires=[],

    # List additional groups of dependencies here (e.g. development
    # dependencies). You can install these using the following syntax,
    # for example:
    # $ pip install -e .[dev,test]
    extras_require={
        'dev': [],
        'test': [],
    },

    # If there are data files included in your packages that need to be
    # installed, specify them here.  If using Python 2.6 or less, then these
    # have to be included in MANIFEST.in as well.
#    package_data={
#        'sample': ['package_data.dat'],
#    },

    # To provide executable scripts, use entry points in preference to the
    # "scripts" keyword. Entry points provide cross-platform support and allow
    # pip to create the appropriate form of executable for the target platform.
    entry_points={
        'console_scripts': [
            'rocker=rocker:main',
        ],
    },

	test_suite="tests"
)


text:
layout: post
title:  "[react tip] create-react-app "
date:   2017-8-23 00:01:30 +0530
categories: science
author: "Jin Woo Park"
---
# This post is based on my personal experiences.

I was wondering that, whether there is simple way to create react project.
and finally found out there is a thing called "create-react-app".
it is simple but powerful for react project creation.

How to install?
  * npm install -g create-react-app

after, installation type "create-react-app <nameOfProject>"

it will generate react project.


text:
#ifndef CLICK_FROMDEVICE_USERLEVEL_HH
#define CLICK_FROMDEVICE_USERLEVEL_HH
#include <click/element.hh>
#include "elements/userlevel/kernelfilter.hh"

#ifdef __linux__
# define FROMDEVICE_ALLOW_LINUX 1
#endif

#if HAVE_PCAP
# define FROMDEVICE_ALLOW_PCAP 1
extern "C" {
# include <pcap.h>
/* Prototype pcap_setnonblock if we have it, but not the prototype. */
# if HAVE_PCAP_SETNONBLOCK && !HAVE_DECL_PCAP_SETNONBLOCK
int pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf);
# endif
}
#endif

#if HAVE_NET_NETMAP_H
# define FROMDEVICE_ALLOW_NETMAP 1
# include "elements/userlevel/netmapinfo.hh"
#endif

#if FROMDEVICE_ALLOW_NETMAP || FROMDEVICE_ALLOW_PCAP
# include <click/task.hh>
extern "C" {
void FromDevice_get_packet(u_char*, const struct pcap_pkthdr*, const u_char*);
}
#endif

CLICK_DECLS

/*
=title FromDevice.u

=c

FromDevice(DEVNAME [, I<keywords> SNIFFER, PROMISC, FORCE_IP, etc.])

=s netdevices

reads packets from network device (user-level)

=d

This manual page describes the user-level version of the FromDevice
element. For the Linux kernel module element, read the FromDevice(n) manual
page.

Reads packets from the kernel that were received on the network controller
named DEVNAME.

User-level FromDevice behaves like a packet sniffer by default.  Packets
emitted by FromDevice are also received and processed by the kernel.  Thus, it
doesn't usually make sense to run a router with user-level Click, since each
packet will get processed twice (once by Click, once by the kernel).  Install
firewalling rules in your kernel if you want to prevent this, for instance
using the KernelFilter element or FromDevice's SNIFFER false argument.

Under Linux, a FromDevice element will not receive packets sent by a
ToDevice element for the same device. Under other operating systems, your
mileage may vary.

Sets the packet type annotation appropriately. Also sets the timestamp
annotation to the time the kernel reports that the packet was received.

Keyword arguments are:

=over 8

=item SNIFFER

Boolean.  Specifies whether FromDevice should run in sniffer mode.  In
non-sniffer mode, FromDevice installs KernelFilter filtering rules to block
the kernel from handling any packets arriving on device DEVNAME.  Default is
true (sniffer mode).

=item PROMISC

Boolean.  FromDevice puts the device in promiscuous mode if PROMISC is true.
The default is false.

=item SNAPLEN

Unsigned.  On some systems, packets larger than SNAPLEN will be truncated.
Defaults to 2046.

=item FORCE_IP

Boolean. If true, then output only IP packets. (Any link-level header remains,
but the IP header annotation has been set appropriately.) Default is false.

=item METHOD

Word.  Defines the capture method FromDevice will use to read packets from the
device.  Linux targets generally support PCAP and LINUX; other targets support
only PCAP.  Defaults to PCAP.

=item BPF_FILTER

String.  A BPF filter expression used to select the interesting packets.
Default is the empty string, which means all packets.  If METHOD is not PCAP,
then any filter expression is ignored with a warning.

=item ENCAP

Word.  The encapsulation type the interface should use; see FromDump for
choices.  Ignored if METHOD is not PCAP.

=item OUTBOUND

Boolean. If true, then emit packets that the kernel sends to the given
interface, as well as packets that the kernel receives from it. Default is
false.

=item PROTOCOL

Integer. If set and nonzero, then only emit packets with this link-level
protocol. Only affects METHOD LINUX. Default is 0.

=item HEADROOM

Integer. Amount of bytes of headroom to leave before the packet data. Defaults
to roughly 28.

=item BURST

Integer. Maximum number of packets to read per scheduling. Defaults to 1.

=item TIMESTAMP

Boolean. If false, then do not timestamp packets. Defaults to true.

=back

=e

  FromDevice(eth0) -> ...

=n

FromDevice sets packets' extra length annotations as appropriate.

=h count read-only

Returns the number of packets read by the device.

=h reset_counts write-only

Resets "count" to zero.

=h kernel_drops read-only

Returns the number of packets dropped by the kernel, probably due to memory
constraints, before FromDevice could get them. This may be an integer; the
notation C<"<I<d>">, meaning at most C<I<d>> drops; or C<"??">, meaning the
number of drops is not known.

=h encap read-only

Returns a string indicating the encapsulation type on this link. Can be
`C<IP>', `C<ETHER>', or `C<FDDI>', for example.

=a ToDevice.u, FromDump, ToDump, KernelFilter, FromDevice(n) */

class FromDevice : public Element { public:

    FromDevice() CLICK_COLD;
    ~FromDevice() CLICK_COLD;

    const char *class_name() const	{ return "FromDevice"; }
    const char *port_count() const	{ return "0/1-2"; }
    const char *processing() const	{ return PUSH; }

    enum { default_snaplen = 2046 };
    int configure_phase() const		{ return KernelFilter::CONFIGURE_PHASE_FROMDEVICE; }
    int configure(Vector<String> &, ErrorHandler *) CLICK_COLD;
    int initialize(ErrorHandler *) CLICK_COLD;
    void cleanup(CleanupStage) CLICK_COLD;
    void add_handlers() CLICK_COLD;

    inline String ifname() const	{ return _ifname; }
#if FROMDEVICE_ALLOW_LINUX || FROMDEVICE_ALLOW_PCAP || FROMDEVICE_ALLOW_NETMAP
    inline int fd() const		{ return _fd; }
#else
    inline int fd() const		{ return -1; }
#endif

    void selected(int fd, int mask);

#if FROMDEVICE_ALLOW_PCAP
    pcap_t *pcap() const		{ return _pcap; }
    static const char* fetch_pcap_error(pcap_t* pcap, const char* ebuf);
    static pcap_t *open_pcap(String ifname, int snaplen, bool promisc, ErrorHandler *errh);
#endif

#if FROMDEVICE_ALLOW_LINUX
    int linux_fd() const		{ return _method == method_linux ? _fd : -1; }
    static int open_packet_socket(String, ErrorHandler *);
    static int set_promiscuous(int, String, bool);
#endif

#if FROMDEVICE_ALLOW_NETMAP
    const NetmapInfo *netmap() const { return _method == method_netmap ? &_netmap : 0; }
#endif

#if FROMDEVICE_ALLOW_NETMAP || FROMDEVICE_ALLOW_PCAP
    bool run_task(Task *task);
#endif

    void kernel_drops(bool& known, int& max_drops) const;

  private:

#if FROMDEVICE_ALLOW_LINUX || FROMDEVICE_ALLOW_PCAP || FROMDEVICE_ALLOW_NETMAP
    int _fd;
#endif
#if FROMDEVICE_ALLOW_NETMAP || FROMDEVICE_ALLOW_PCAP
    Task _task;
#endif
#if FROMDEVICE_ALLOW_PCAP || FROMDEVICE_ALLOW_NETMAP
    void emit_packet(WritablePacket *p, int extra_len, const Timestamp &ts);
#endif
#if FROMDEVICE_ALLOW_PCAP
    pcap_t *_pcap;
    int _pcap_complaints;
#endif
#if FROMDEVICE_ALLOW_NETMAP
    NetmapInfo _netmap;
    int netmap_dispatch();
#endif
#if FROMDEVICE_ALLOW_PCAP || FROMDEVICE_ALLOW_NETMAP
    friend void FromDevice_get_packet(u_char*, const struct pcap_pkthdr*,
                                      const u_char*);
#endif

    bool _force_ip;
#if FROMDEVICE_ALLOW_PCAP && TIMESTAMP_NANOSEC && defined(PCAP_TSTAMP_PRECISION_NANO)
    bool _pcap_nanosec;
#endif
    int _burst;
    int _datalink;

#if HAVE_INT64_TYPES
    typedef uint64_t counter_t;
#else
    typedef uint32_t counter_t;
#endif
    counter_t _count;

    String _ifname;
    bool _sniffer : 1;
    bool _promisc : 1;
    bool _outbound : 1;
    bool _timestamp : 1;
    int _was_promisc : 2;
    int _snaplen;
    uint16_t _protocol;
    unsigned _headroom;
    enum { method_default, method_netmap, method_pcap, method_linux };
    int _method;
#if FROMDEVICE_ALLOW_PCAP
    String _bpf_filter;
#endif

    static String read_handler(Element*, void*) CLICK_COLD;
    static int write_handler(const String&, Element*, void*, ErrorHandler*) CLICK_COLD;

};

CLICK_ENDDECLS
#endif


text:
/**
    Supply Static JS Code
*/

S3.supply = Object();

/* Globals called by S3OptionsFilter */

// Filter Item Packs based on Items
S3.supply.fncPrepItem = function(data) {
    for (var i = 0; i < data.length; i++) {
        if (data[i].quantity == 1) {
            return data[i].name;
        }
    }
    return '';
}

S3.supply.fncRepresentItem = function(record, PrepResult) {
    if (record.quantity == 1) {
        return record.name;
    } else {
        return record.name + ' (' + record.quantity + ' x ' + PrepResult + ')';
    }
}

$(document).ready(function() {
    // Displays the number of items available in an inventory
    var InvItemPackIDChange = function() {
        // Cancel previous request
        try {S3.JSONRequest[$(this).attr('id')].abort();} catch(err) {}

        $('#TotalQuantity').remove();
        if ($('[name="inv_item_id"]').length > 0) {
            id = $('[name="inv_item_id"]').val();
        }
        else if  ($('[name="send_inv_item_id"]').length > 0) {
            id = $('[name="send_inv_item_id"]').val();
        }
        // Following condition removed since it doesn't appear to be correct
        // the ajax call is looking for the number of items in stock, but
        // this is the supply catalogue id - not an id related to an inventory
        //else if  ($('[name = "item_id"]').length > 0) {
        //    id = $('[name = "item_id"]').val();
        //}
        else
            return;

        var url = S3.Ap.concat('/inv/inv_item_quantity/' + id);
        if ($('#inv_quantity_ajax_throbber').length === 0) {
            $('[name="quantity"]').after('
');
        }

        // Save JSON Request by element id
        S3.JSONRequest[$(this).attr('id')] = $.getJSON(url, function(data) {
            // @ToDo: Error Checking
            var InvQuantity = data.iquantity;
            var InvPackQuantity = data.pquantity;

            var PackName = $('[name="item_pack_id"] option:selected').text();
            var re = /\(([0-9]*)\sx/;
            var RegExpResult = re.exec(PackName);
            var PackQuantity;
            if (RegExpResult === null) {
                PackQuantity = 1;
            } else {
                PackQuantity = RegExpResult[1];
            }

            var Quantity = (InvQuantity * InvPackQuantity) / PackQuantity;

            TotalQuantity = ' / ' + Quantity.toFixed(2) + ' ' + PackName + ' (' + i18n.in_inv + ')';
            $('#inv_quantity_ajax_throbber').remove();
            $('[name="quantity"]').after(TotalQuantity);
        });
    }

    // Displays the number of items available in an inventory
    var InvRecvTypeChange = function() {
        var RecvType = $("#inv_recv_type").val();
        if (RecvType != undefined) {
            if ( RecvType == 11) { // @ToDo: pass this value instead of hardcoding it base on s3cfg.py
                // Internal Shipment
                $('[id^="inv_recv_from_site_id__row"]').show();
                $('[id^="inv_recv_organisation_id__row"]').hide();
            } else if ( RecvType >= 32) { // @ToDo: pass this value instead of hardcoding it base on s3cfg.py
                // External Shipment
                $('[id^="inv_recv_from_site_id__row"]').hide();
                $('[id^="inv_recv_organisation_id__row"]').show();
            } else { // @ToDo: pass this value instead of hardcoding it base on s3cfg.py
                // External Shipment
                $('[id^="inv_recv_from_site_id__row"]').hide();
                $('[id^="inv_recv_organisation_id__row"]').hide();
            }

        }
    }

    $('#inv_track_item_item_pack_id').change(InvItemPackIDChange);
    InvRecvTypeChange();
    $('#inv_recv_type').change(InvRecvTypeChange);
/**
    // Item ID Field
    // Assets don't use Packs, so skip
    if ($("#asset_asset_item_id").val() == undefined &
    	  "#supply_catalog_item_item_id").val() == undefined) {
        S3FilterFieldChange({
            'FilterField':	'item_id',
            'Field':		'item_pack_id',
            'FieldResource':'item_pack',
            'FieldPrefix':	'supply',
            'msgNoRecords':	i18n.no_packs,
            'fncPrep':		S3.supply.fncPrepItem,
            'fncRepresent':	S3.supply.fncRepresentItem
        });
    }

    // Inv Item Field
    S3FilterFieldChange({
		'FilterField':	'inv_item_id',
		'Field':		'item_pack_id',
		'FieldResource':'item_pack',
		'FieldPrefix':	'supply',
	    'url':		 	S3.Ap.concat('/inv/inv_item_packs/'),
		'msgNoRecords':	i18n.no_packs,
		'fncPrep':		S3.supply.fncPrepItem,
		'fncRepresent':	S3.supply.fncRepresentItem
	});

    // Req Item Field
    S3FilterFieldChange({
		'FilterField':	'req_item_id',
		'Field':		'item_pack_id',
		'FieldResource':'item_pack',
		'FieldPrefix':	'supply',
	    'url':		 	S3.Ap.concat('/req/req_item_packs/'),
		'msgNoRecords':	i18n.no_packs,
		'fncPrep':		S3.supply.fncPrepItem,
		'fncRepresent':	S3.supply.fncRepresentItem
	});
*/
    // ========================================================================
    /**
     * Function to show the transactions related to request commit, transit &
     * fulfil quantities
     */
	$(document).on('click', '.quantity.ajax_more', function(e) {
		e.preventDefault();
		var DIV = $(this);
        var ShipmentType;
		var App;
		if (DIV.hasClass('collapsed')) {
			if (DIV.hasClass('fulfil')) {
				App = 'inv';
				ShipmentType = 'recv';
			} else if (DIV.hasClass('transit')) {
				ShipmentType = 'send';
				App = 'inv';
			} else if (DIV.hasClass('commit')) {
				ShipmentType = 'commit';
				App = 'req';
			}
			DIV.after('
')
			   .removeClass('collapsed')
			   .addClass('expanded');

			// Get the req_item_id
			var UpdateURL = $('.action-btn', DIV.parent().parent().parent()).attr('href');
			var re = /req_item\/(\d*).*/i;
			var req_item_id = re.exec(UpdateURL)[1];
			var url = S3.Ap.concat('/', App, '/', ShipmentType, '_item_json/', req_item_id);
			//var url = S3.Ap.concat('/', App, '/', ShipmentType, '_item.s3json?/',
			//		   ShipmentType, '_item.req_item_id=', req_item_id);
			$.ajax( {
				url: url,
				dataType: 'json',
				context: DIV
			}).done(function(data) {
                RecvTable = '
';
                for (i=0; i<data.length; i++) {
                    RecvTable += '
';
                    if (i===0) {
                        // Header Row
                        RecvTable += data[0].id;
                    } else {
                        RecvURL = S3.Ap.concat('/', App, '/', ShipmentType, '/',  data[i].id, '/track_item');
                        RecvTable += "";
                        if (data[i].date !== null) {
                            RecvTable += data[i].date.substring(0, 10) + ' - ';
                            RecvTable += data[i].name + '';
                        } else {
                            RecvTable +=  ' - ';
                        }
                    }
                    RecvTable += '	' + data[i].quantity + '
';
                }
                RecvTable += '
';
                $('.quantity_req_ajax_throbber', this.parent()).remove();
                this.parent().after(RecvTable);
            });
		} else {
			DIV.removeClass('expanded')
			   .addClass('collapsed');
			$('.recv_table', DIV.parent().parent() ).remove();
		}
	});

	// ========================================================================
	/**
     * ASSET APPLICATION JS
	 * @ToDo: - Populate Location based on site(?)
	 */
	// ------------------------------------------------------------------------
	/**
     * Code to switch between location & site widgets
	 * @ToDo: Find a better way to show/hide location widget
	 */
    /*
	$('[name="site_or_location"]').change(function() {
		if ($('#asset_log_site_or_location').length == 1) {
			$('[id^="asset_log_site_id__row"]').hide();
			// $('[id^="asset_log_location_id__row"]').hide();
			$('tr[id^="gis_location"]').hide();
			$('span[id^="gis_loc"]').hide();
			$('#gis_location_map-btn').hide();
			$('td.subheading').hide()
		}
		if ($('[name="site_or_location"]:checked').val() == '1') {
			// Enable Site Input
			$('[id^="asset_log_site_id__row"]').show();
			$('[id^="asset_log_room_id__row"]').show();
			$('tr[id^="gis_location"]').hide();
			$('span[id^="gis_loc"]').hide();
			$('#gis_location_map-btn').hide();
			$('td.subheading').hide();
		} else if ($('[name="site_or_location"]:checked').val() == '2'){
			// Enable Location Input
			$('[id^="asset_log_site_id__row"]').hide();
			$('[id^="asset_log_room_id__row"]').hide();
			// $('[id^="asset_log_location_id__row"]').hide();
			$('tr[id^="gis_location"]').show();
			$('span[id^="gis_loc"]').show();
			$('#gis_location_map-btn').show();
			$('td.subheading').show();
		}
	});
	$('[name="site_or_location"]').change();
	*/
	/* Populate Organisation based on Site  */
	/*
	$('#asset_log_site_id').change( function() {
		// Cancel previous request

		if ($('#asset_log_person_id').val() != '' || $('#asset_log_organisation_id').val() != '') {
			// Don't populate if the person & org are already set

			try {S3.JSONRequest[$(this).attr('id')].abort()} catch(err) {};

			var site_id = $('#asset_log_site_id').val()
			if (site_id != "") {
				$('#dummy_asset_log_organisation_id').after('
')
													 .hide()
				var url = S3.Ap.concat('/org/site_org_json/', site_id);

				// Save JSON Request by element id
				S3.JSONRequest[$(this).attr('id')] = $.ajax( {
					url: url,
					dataType: 'json'
				}).done(function(data) {
                    $('#organisation_ajax_throbber').remove();
                    if (data.length > 0) {
                        $('#dummy_asset_log_organisation_id').show()
                                                             .val(data[0].name)
                                                             .prop('disabled', true);
                        $('#asset_log_organisation_id').val(data[0].id);
                    } else {
                        $('#dummy_asset_log_organisation_id').show()
                                                             .val('')
                                                             .prop('disabled', false);
                        $('#asset_log_organisation_id').val('');
                    }

                });
			} else {
				$('#dummy_asset_log_organisation_id').val('')
													 .prop('disabled', false);
				$('#asset_log_organisation_id').val('');
			}
		}
	});
	*/
	/** Populate Site & Org Based on Person
	/* @ToDo: have this only select the correct site - and not disable the field
     */
	/*$('#asset_log_person_id').change( function() {
		// Cancel previous request
		try {S3.JSONRequest[$(this).attr('id')].abort()} catch(err) {};

		// Do NOT Populate Organisation based on Site
		$('#asset_log_site_id').change( function() {});

		var person_id = $('#asset_log_person_id').val()
		if (person_id != '') {
			$('#asset_log_site_id').after('
')
								   .hide()
			$('#dummy_asset_log_organisation_id').after('
')
												 .hide()
			var url = S3.Ap.concat('/hrm/staff_org_site_json/', person_id);

			// Save JSON Request by element id
			S3.JSONRequest[$(this).attr('id')] = $.ajax({
				url: url,
				dataType: 'json'
			}).done(function(data) {
                $('#site_ajax_throbber').remove();
                $('#organisation_ajax_throbber').remove();
                if (data.length > 0) {
                    $('#dummy_asset_log_organisation_id').show()
                     .val(data[0].name)
                     .prop('disabled', true);
                    $('#asset_log_organisation_id').val(data[0].id);
                    $('#asset_log_site_id').show()
                                           .val(data[0].site_id)
                                           .prop('disabled', true)
                                           .change();
                } else {
                    $('#dummy_asset_log_organisation_id').show()
                     .val('')
                     .prop('disabled', false);
                    $('#asset_log_organisation_id').val('');
                    $('#asset_log_site_id').show()
                                           .val('')
                                           .prop('disabled', false)
                                           .change();
                }

            });
		} else {
			$('#asset_log_site_id').show()
								   .val('')
								   .prop('disabled', false)
								   .change();
			$('#asset_log_organisation_id').val('');
			$('#dummy_asset_log_organisation_id').val('')
												 .prop('disabled', false);
		}
	});*/
});

// END ========================================================================