#!/usr/bin/env python

import requests, datetime, re, argparse, zipfile, os, sys, json
try:
    from MaltegoTransform import *
    transform = 1
except:
    transform = 0

__author__  = "Jeff White [karttoon]"
__email__   = "jwhite@paloaltonetworks.com"
__version__ = "1.0.3"
__date__    = "15DEC2016"

#################### [ API KEY ] ####################
pt_apikey   = ""
pt_user     = ""
#################### [ API KEY ] ####################

if pt_apikey == "":
    print "[=] API key not specified in", sys.argv[0]
    sys.exit(1)

graph_header = """<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:y="http://www.yworks.com/xml/graphml" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd">
<VersionInfo createdBy="Maltego Chlorine" subtitle="" version="3.6.0.6526"/>
<key for="port" id="d0" yfiles.type="portgraphics"/>
<key for="port" id="d1" yfiles.type="portgeometry"/>
<key for="port" id="d2" yfiles.type="portuserdata"/>
<key attr.name="MaltegoEntity" for="node" id="d3"/>
<key for="node" id="d4" yfiles.type="nodegraphics"/>
<key for="graphml" id="d5" yfiles.type="resources"/>
<key attr.name="MaltegoLink" for="edge" id="d6"/>
<key for="edge" id="d7" yfiles.type="edgegraphics"/>
<graph edgedefault="directed" id="G">"""

graph_footer = """</graph>
<data key="d5">
  <y:Resources/>
</data>
</graphml>"""

def pt_query(value, transform):
    url = 'https://api.passivetotal.org/v2/dns/passive'
    auth = (pt_user, pt_apikey)
    params = {'query': value}
    try:
        # Timeout can also act as a quasi break on hosting sites/large return values - remove the timeout if you really want the nodes
        pt_response = requests.get(url, params=params, auth=auth, timeout=60)
	if pt_response.status_code == 504: # Gateway Timeout error
		api_result = {'error': 'Gateway Timeout Error - 504'}
	else:
	        api_result = pt_response.json()
    except requests.exceptions.RequestException as error:
        api_result = {"error": error}
        if transform == 1:
            maltrans.addUIMessage("API Error. Too many resolves or connectivity issues - validate manually.", messageType="PartialError")
    return api_result

def date_convert(date, type): # Normalize the dates coming in from the various sources
    date_list = []
    if type == "PT": # PassiveTotal date format
        if date == "None":
            date = ['1970', '1', '1']
        else:
            date = (date.split(" ")[0]).split("-")
    if type == "user": # Dates supplied via CLI or Maltego
        date = date.split("-")
    for i in date:
        date_list.append(int(i))
    return datetime.date(date_list[0], date_list[1], date_list[2])

def build_ptlist(api_result): # Written for just PT at the moment, will need to rework for other APIs
    record_list = {}
    for record in api_result['results']:
        record_list[record['recordHash']] = [record['resolve'], record['firstSeen'], record['lastSeen']]
    return record_list

def build_node(value, type, number):
    node_body = '<node id="n' + str(number) + '">'
    node_body += """
<data key="d3">
<mtg:MaltegoEntity xmlns:mtg="http://maltego.paterva.com/xml/mtgx" id="""
    id_value = "malnode" + str(number)
    if type == "ip":
        node_body += '"' + id_value + '" type="maltego.IPv4Address">'
        node_body += """
<mtg:Properties>
<mtg:Property displayName="IP Address" hidden="false" name="ipv4-address" nullable="true" readonly="false" type="string">"""
    if type == "domain":
        node_body += '"' + id_value + '" type="maltego.Domain">'
        node_body += """
<mtg:Properties>
<mtg:Property displayName="Domain Name" hidden="false" name="fqdn" nullable="true" readonly="false" type="string">"""
    node_body += """
  <mtg:Value>""" + value + """</mtg:Value>"""
    node_body += """</mtg:Property>
</mtg:Properties>
</mtg:MaltegoEntity>
</data>
<data key="d4">
  <mtg:EntityRenderer xmlns:mtg="http://maltego.paterva.com/xml/mtgx">
  <mtg:Position x=""" + '"' + str(5 * number) + '"' + ' y="' + str(5 * number) + '"' + """/>
</mtg:EntityRenderer>
</data>
</node>"""
    return node_body

def build_edge(src_node, dst_node, number):
    edge_body = '<edge id="e' + str(number) + '" source="' + src_node + '" target="' + dst_node + '">'
    edge_body += """
<data key="d6">
<mtg:MaltegoLink xmlns:mtg="http://maltego.paterva.com/xml/mtgx" id="maledge""" + str(number) + '"' + """ type="maltego.link.manual-link">
</mtg:MaltegoLink>
  </data>
  <data key="d7">
    <mtg:LinkRenderer xmlns:mtg="http://maltego.paterva.com/xml/mtgx"/>
  </data>
</edge>"""
    return edge_body

def api_query(value, target_start, target_end, recursive, api, transform):
    # Initialize some values, lists, and dicts
    type = "user"
    count = 0
    final_list = {}
    record_list = {}
    value_stage = []
    processed_list = []
    while count < int(recursive): # Main iteration loop
        if transform == False: # Only print if transform is false (to STDOUT) otherwise Maltego will have an error
            print "    [@] Beginning recurse search number", str(count + 1) + "."
        if count == 0: # Set initial value first run
            value_list = [value]
        else:
            value_stage = []
        if value_list == []: # If all values popped out, don't let it keep iterating
            print "\t[=] *** No further values to lookup. ***"
            break
        val_len = len(value_list)
        val_count = 0
        while val_count < val_len:
            for value in value_list: # Iterate through each value
                if api == "PT": # PassiveTotal API Query
                    api_result = pt_query(value, transform)
                if "error" in api_result: # Print error if it timesout (generally due to a large value of resolutions being returned - seems 1K+ or # of queries
                    if transform == False:
                        print "\t[=] *** ERROR processing", value + ", too many resolves or connection issues. ***\n\t\t", api_result['error']
                    processed_list.append(value) # Once finished, add the value to the completed list
                    val_count += 1 # Increase the value count so we know where we're at
                    value_list = list(set(value_list)) # Unique the remaining value list
                    value_list.remove(value) # Remove just queried value
                    continue
                result_count = api_result['totalRecords']
                if result_count == 0: # If a domain has no resolutions or vice versa, remove from value list and break out of this value loop
                    if transform == False:
                        print "\t[=] *** No results found for", value, ". ***"
                    processed_list.append(value) # Once finished, add the value to the completed list
                    val_count += 1 # Increase the value count so we know where we're at
                    value_list = list(set(value_list)) # Unique the remaining value list
                    value_list.remove(value) # Remove just queried value
                    break
                if result_count >= 50: # If it returns a large number of results, most likely not wanted in the graph - prompt the user
                    if transform == False:
                        large_continue = raw_input("[%] " + str(result_count) + " results for " + value + " continue? [y/n] = ")
                        if large_continue.lower() == "n":
                            processed_list.append(value) # Once finished, add the value to the completed list
                            val_count += 1 # Increase the value count so we know where we're at
                            value_list = list(set(value_list)) # Unique the remaining value list
                            value_list.remove(value) # Remove just queried value
                            break
                if transform == False:
                    print "    [$]", str(result_count), "results for", value + "."
                # Try to match the type so it can do the correct lookup
                if re.match("^[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}$", api_result['queryValue']):
                    type = "ip"
                if re.match(".*\.[a-zA-Z]{2,63}$", api_result['queryValue']): # 63 for the new TLDs - just a general catch all
                    type = "domain"
                if type == "ip":
                    record_list = build_ptlist(api_result) # Query data - only setup for PT currently
                    for record in record_list: # For each domain that resolved to IP
                        ip_address = api_result['queryValue'] # IP record
                        domain = record_list[record][0] # Domain record
                        try: # Pull the PT Tag value for the domain
                            tag_value = api_result['results']['enrichment_map'][domain]['tags'][0]['value']
                        except:
                            tag_value = "Untagged"
                        try: # Pull the PT Dynamic value for the domain
                            dynamic_value = api_result['results']['enrichment_map'][domain]['dynamic']
                        except:
                            dynamic_value = "Unknown"
                        try: # Pull the PT Classification value for the domain
                            class_value = api_result['results']['enrichment_map'][domain]['classification']
                            if class_value == "":
                                class_value = "Unknown"
                        except:
                            class_value = "Unknown"
                        start_date = date_convert(record_list[record][1], api) # First seen
                        end_date = date_convert(record_list[record][2], api) # Last seen
                        date_overlap = min(target_end - start_date, end_date - target_start).days + 1
                        if date_overlap >= 1: # Validate target range falls within seen range
                            if ip_address not in final_list.keys(): # Check if the IP is already set, if not then add it
                                final_list[ip_address] = []
                            if domain not in final_list[ip_address]: # Check to make sure domain isn't tied to IP already
                                final_list[ip_address].append(domain) # Add IP:domain
                                if transform == False:
                                    if verbose == True:
                                        print "\t[=] MATCH: [start]", start_date, "[end]", end_date, "[value]", domain, "[class]", class_value, "[tag]", tag_value, "[dynamic]", dynamic_value
                                    else:
                                        print "\t[=] MATCH:", domain
                            if domain not in processed_list and domain not in value_stage: # Make sure the domain hasn't been processed yet
                                value_stage.append(domain) # Add domain to temp list for processing in next iteration
                if type == "domain":
                    record_list = build_ptlist(api_result) # Query data - only setup for PT currently
                    for record in record_list: # For each IP that resolved to domain
                        ip_address = record_list[record][0] # IP record
                        domain = api_result['queryValue']
                        try: # Pull the PT Tag value for the IP
                            tag_value = api_result['results']['enrichment_map'][ip_address]['tags'][0]['value']
                            if tag_value == "":
                                tag_value = "Untagged"
                        except:
                            tag_value = "Untagged"
                        try: # Pull the PT Sinkhole value for the IP
                            sinkhole_value = api_result['results']['enrichment_map'][ip_address]['sinkhole']
                        except:
                            sinkhole_value = "Unknown"
                        try: # Pull the PT Classification value for the IP
                            class_value = api_result['results']['enrichment_map'][ip_address]['classification']
                            if class_value == "":
                                class_value = "Unknown"
                        except:
                            class_value = "Unknown"
                        start_date = date_convert(record_list[record][1], api) # First seen
                        end_date = date_convert(record_list[record][2], api) # Last seen
                        date_overlap = min(target_end - start_date, end_date - target_start).days + 1
                        if date_overlap >= 1: # Validate target range falls within seen range
                            if ip_address not in final_list.keys(): # Validate that this IP entry doesnt exist
                                final_list[ip_address] = [] # Build blank IP entry
                            if ip_address not in processed_list and ip_address not in value_stage: # Validate IP hasn't been processed yet or set to run next iteration
                                final_list[ip_address].append(domain) # Add IP:domain
                                value_stage.append(ip_address) # Add IP to temp list for processing in next iteration
                                if transform == False:
                                    if verbose == True:
                                        print "\t[=] MATCH: [start]",start_date, "[end]", end_date, "[value]", ip_address, "[class]", class_value, "[tag]", tag_value, "[sinkhole]", sinkhole_value
                                    else:
                                        print "\t[=] MATCH:", ip_address
                processed_list.append(value) # Once finished, add the value to the completed list
                val_count += 1 # Increase the value count so we know where we're at
                value_list = list(set(value_list)) # Unique the remaining value list
                value_list.remove(value) # Remove just queried value
        count += 1 # Increase overall count by 1 to check againt recusrive level
        value_stage = list(set(value_stage)) # Unique the temp list (future value list) - returned results usually have a lot of duplicates due to temporal aspect
        for entry in value_stage: # Build next iteration loops value list
            value_list.append(entry)
    return final_list, type

def build_graph(final_list, filename):
    out_file = open('Graphs/Graph1.graphml', 'w')
    # Start building graph
    node_count = 0
    node_track = {}
    # Build necessary graph_header
    out_file.write(graph_header)
    # Build IP nodes
    for ip_entry in final_list:
        out_file.write(build_node(ip_entry, "ip", node_count))
        node_track["n" + str(node_count)] = ip_entry
        node_count += 1
    # Build Domain nodes
    domain_list = []
    for ip_entry in final_list:
        for domain in final_list[ip_entry]:
            domain_list.append(domain)
    domain_list = list(set(domain_list))
    for domain_entry in domain_list:
        out_file.write(build_node(domain_entry, "domain", node_count))
        node_track["n" + str(node_count)] = domain_entry
        node_count += 1
    # Build Edge links
    edge_count = 0
    for ip_entry in final_list: # Iterate through each IP
        for list_key, list_value in node_track.iteritems(): # Find node value for IP
            if list_value == ip_entry:
                src_node = list_key # Assign it to src_node for edge link
        for domain_entry in final_list[ip_entry]: # Find node value for domain
            for list_key, list_value in node_track.iteritems():
                if list_value == domain_entry:
                    dst_node = list_key # Assign it to dst_node for edge link
                    out_file.write(build_edge(src_node, dst_node, edge_count))
                    edge_count += 1
    # Build necessary graph footer
    out_file.write(graph_footer)
    out_file.close()

def zip_file(filename): # Builds the MTGX file by zipping the files in the directory up with the new Graph xml file
    zip_out = zipfile.ZipFile(filename, 'w')
    dir_include = ["Icons", "Graphs", "Entities"]
    for directory in dir_include:
        for root, dirs, files in os.walk(directory):
            for file in files:
                zip_out.write(os.path.join(root, file))
    zip_out.write('./', 'version_properties')
    zip_out.close()

def build_maltego(final_list, type, start_date, end_date): # Sends XML data to STDOUT that Maltego will catch, also ensures dates are returned for machines to keep the filter
    if type == "ip":
        for ip_entry in final_list:
            for domain_entry in final_list[ip_entry]:
                node = maltrans.addEntity("maltego.Domain", domain_entry)
                node.setType("maltego.Domain")
                node.addAdditionalFields("After", "Display Value", True, start_date)
                node.addAdditionalFields("Before", "Display Value", True, end_date)
    if type == "domain":
        for ip_entry in final_list:
            node = maltrans.addEntity("maltego.IPv4Address", ip_entry)
            node.setType("maltego.IPv4Address")
            node.addAdditionalFields("After", "Display Value", True, start_date)
            node.addAdditionalFields("Before", "Display Value", True, end_date)

def main():
    parser = argparse.ArgumentParser(description="Jumpstart Maltego graph of C2 infrastructure off domain or IP.", epilog="spidermal.py -l paloaltonetworks.com -s 2014-09-12 -e 2015-12-1 -r 2 -o pan.mgtx -a PT")
    parser.add_argument("-s", "--start", help="Start date for range; \"YYYY-MM-DD\".", metavar="YYYY-MM-DD")
    parser.add_argument("-e", "--end", help="End date for range; \"YYYY-MM-DD\".", metavar="YYYY-MM-DD")
    parser.add_argument("-l", "--lookup", help="Value you start search with.", required=True, metavar="IP|DOMAIN")
    parser.add_argument("-o", "--out", help="Output file name (will append \"mtgx\" if not present.", default="malgraph.mtgx", metavar="filename.mtgx")
    parser.add_argument("-r", "--recurse", help="Number of levels to recurse. Default is 1; be careful with hosting sites.", default=1, metavar="LEVEL")
    parser.add_argument("-a", "--api", help="Choose API to use. Default is PassiveTotal.", default="PT", choices=["PT"])
    parser.add_argument("-t", "--transform", help="Run in Maltego Transform mode (run from inside Maltego client).", action="store_true")
    parser.add_argument("-v", "--verbose", help="Print additional data (tags/class/dynamic fields).", action="store_true")
    args, unknown = parser.parse_known_args() # Make sure to collect the unknown arguments since Maltego will pass them in "#" format
    global verbose
    verbose = args.verbose
    target_start = datetime.date(1970, 1, 1) # Default start date for range
    target_end = datetime.date.today() # Default end date for range
    if args.transform == True:
        if transform == 0: # Check to make sure the MaltegoTransform.py file is there, otherwise notify user within Maltego
            print """<MaltegoMessage><MaltegoTransformResponseMessage><Entities></Entities><UIMessages><UIMessage MessageType="FatalError">MaltegoTransform.py Module Not Found!</UIMessage></UIMessages></MaltegoTransformResponseMessage></MaltegoMessage>"""
            sys.exit()
        unknownargs = unknown[0].split("#") # Peel off any dates sent by Maltego in the "Before" or "After" fields
        for argument in unknownargs:
            if argument.startswith("After"):
                target_start = date_convert(argument.split("=")[1], "user")
            elif argument.startswith("Before"):
                target_end = date_convert(argument.split("=")[1], "user")
            else:
                pass
        global maltrans # Build maltego transform to pipe data back if transform is selected
        maltrans = MaltegoTransform()
        final_list, type = api_query(args.lookup, target_start, target_end, "1", "PT", args.transform)
        build_maltego(final_list, type, str(target_start), str(target_end))
        maltrans.returnOutput()
    else:
        if args.start:
            target_start = date_convert(args.start, "user")
        if args.end:
            target_end = date_convert(args.end, "user")
        print "[+] Begining search for", args.lookup, "using", args.api, "API between", str(target_start), "and", str(target_end) + "."
        final_list, type = api_query(args.lookup, target_start, target_end, args.recurse, args.api, args.transform)
        print "[+] Finished API queries."
        print "[+] Building graph (nodes/edges)."
        build_graph(final_list, args.out)
        print "[+] Building Maltego file named", args.out + "."
        zip_file(args.out)

if __name__ == '__main__':
    main()

{'repo_name': 'pan-unit42/public_tools', 'stars': '510', 'repo_language': 'Python', 'file_name': 'README.MD', 'mime_type': 'text/plain', 'hash': 7005717624036162736, 'source_dataset': 'data'}


# Copyright 2020 The StackStorm Authors.
# Copyright 2019 Extreme Networks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
import datetime

import bson
import mock

import st2tests.config as tests_config
tests_config.parse_args()

from st2actions.notifier.notifier import Notifier
from st2common.constants.action import LIVEACTION_COMPLETED_STATES
from st2common.constants.action import LIVEACTION_STATUSES
from st2common.constants.triggers import INTERNAL_TRIGGER_TYPES
from st2common.models.api.action import LiveActionAPI
from st2common.models.db.action import ActionDB
from st2common.models.db.execution import ActionExecutionDB
from st2common.models.db.liveaction import LiveActionDB
from st2common.models.db.notification import NotificationSchema
from st2common.models.db.notification import NotificationSubSchema
from st2common.models.db.runner import RunnerTypeDB
from st2common.persistence.action import Action
from st2common.persistence.liveaction import LiveAction
from st2common.persistence.policy import Policy
from st2common.models.system.common import ResourceReference
from st2common.util import date as date_utils
from st2common.util import isotime
from st2tests.base import CleanDbTestCase

ACTION_TRIGGER_TYPE = INTERNAL_TRIGGER_TYPES['action'][0]
NOTIFY_TRIGGER_TYPE = INTERNAL_TRIGGER_TYPES['action'][1]
MOCK_EXECUTION = ActionExecutionDB(id=bson.ObjectId(), result={'stdout': 'stuff happens'})

class NotifierTestCase(CleanDbTestCase):

    class MockDispatcher(object):
        def __init__(self, tester):
            self.tester = tester
            self.notify_trigger = ResourceReference.to_string_reference(
                pack=NOTIFY_TRIGGER_TYPE['pack'],
                name=NOTIFY_TRIGGER_TYPE['name'])
            self.action_trigger = ResourceReference.to_string_reference(
                pack=ACTION_TRIGGER_TYPE['pack'],
                name=ACTION_TRIGGER_TYPE['name'])

        def dispatch(self, *args, **kwargs):
            try:
                self.tester.assertEqual(len(args), 1)
                self.tester.assertTrue('payload' in kwargs)
                payload = kwargs['payload']

                if args[0] == self.notify_trigger:
                    self.tester.assertEqual(payload['status'], 'succeeded')
                    self.tester.assertTrue('execution_id' in payload)
                    self.tester.assertEqual(payload['execution_id'], str(MOCK_EXECUTION.id))
                    self.tester.assertTrue('start_timestamp' in payload)
                    self.tester.assertTrue('end_timestamp' in payload)
                    self.tester.assertEqual('core.local', payload['action_ref'])
                    self.tester.assertEqual('Action succeeded.', payload['message'])
                    self.tester.assertTrue('data' in payload)
                    self.tester.assertTrue('local-shell-cmd', payload['runner_ref'])

                if args[0] == self.action_trigger:
                    self.tester.assertEqual(payload['status'], 'succeeded')
                    self.tester.assertTrue('execution_id' in payload)
                    self.tester.assertEqual(payload['execution_id'], str(MOCK_EXECUTION.id))
                    self.tester.assertTrue('start_timestamp' in payload)
                    self.tester.assertEqual('core.local', payload['action_name'])
                    self.tester.assertEqual('core.local', payload['action_ref'])
                    self.tester.assertTrue('result' in payload)
                    self.tester.assertTrue('parameters' in payload)
                    self.tester.assertTrue('local-shell-cmd', payload['runner_ref'])

            except Exception:
                self.tester.fail('Test failed')

    @mock.patch('st2common.util.action_db.get_action_by_ref', mock.MagicMock(
        return_value=ActionDB(pack='core', name='local', runner_type={'name': 'local-shell-cmd'},
                              parameters={})))
    @mock.patch('st2common.util.action_db.get_runnertype_by_name', mock.MagicMock(
        return_value=RunnerTypeDB(name='foo', runner_parameters={})))
    @mock.patch.object(Action, 'get_by_ref', mock.MagicMock(
        return_value={'runner_type': {'name': 'local-shell-cmd'}}))
    @mock.patch.object(Policy, 'query', mock.MagicMock(
        return_value=[]))
    @mock.patch.object(Notifier, '_get_trace_context', mock.MagicMock(return_value={}))
    def test_notify_triggers(self):
        liveaction_db = LiveActionDB(action='core.local')
        liveaction_db.id = bson.ObjectId()
        liveaction_db.description = ''
        liveaction_db.status = 'succeeded'
        liveaction_db.parameters = {}
        on_success = NotificationSubSchema(message='Action succeeded.')
        on_failure = NotificationSubSchema(message='Action failed.')
        liveaction_db.notify = NotificationSchema(on_success=on_success,
                                                  on_failure=on_failure)
        liveaction_db.start_timestamp = date_utils.get_datetime_utc_now()
        liveaction_db.end_timestamp = \
            (liveaction_db.start_timestamp + datetime.timedelta(seconds=50))
        LiveAction.add_or_update(liveaction_db)

        execution = MOCK_EXECUTION
        execution.liveaction = vars(LiveActionAPI.from_model(liveaction_db))
        execution.status = liveaction_db.status

        dispatcher = NotifierTestCase.MockDispatcher(self)
        notifier = Notifier(connection=None, queues=[], trigger_dispatcher=dispatcher)
        notifier.process(execution)

    @mock.patch('st2common.util.action_db.get_action_by_ref', mock.MagicMock(
        return_value=ActionDB(pack='core', name='local', runner_type={'name': 'local-shell-cmd'},
                              parameters={})))
    @mock.patch('st2common.util.action_db.get_runnertype_by_name', mock.MagicMock(
        return_value=RunnerTypeDB(name='foo', runner_parameters={})))
    @mock.patch.object(Action, 'get_by_ref', mock.MagicMock(
        return_value={'runner_type': {'name': 'local-shell-cmd'}}))
    @mock.patch.object(Policy, 'query', mock.MagicMock(
        return_value=[]))
    @mock.patch.object(Notifier, '_get_trace_context', mock.MagicMock(return_value={}))
    def test_notify_triggers_end_timestamp_none(self):
        liveaction_db = LiveActionDB(action='core.local')
        liveaction_db.id = bson.ObjectId()
        liveaction_db.description = ''
        liveaction_db.status = 'succeeded'
        liveaction_db.parameters = {}
        on_success = NotificationSubSchema(message='Action succeeded.')
        on_failure = NotificationSubSchema(message='Action failed.')
        liveaction_db.notify = NotificationSchema(on_success=on_success,
                                                  on_failure=on_failure)
        liveaction_db.start_timestamp = date_utils.get_datetime_utc_now()

        # This tests for end_timestamp being set to None, which can happen when a policy cancels
        # a request.
        # The assertions within "MockDispatcher.dispatch" will validate that the underlying code
        # handles this properly, so all we need to do is keep the call to "notifier.process" below
        liveaction_db.end_timestamp = None
        LiveAction.add_or_update(liveaction_db)

        execution = MOCK_EXECUTION
        execution.liveaction = vars(LiveActionAPI.from_model(liveaction_db))
        execution.status = liveaction_db.status

        dispatcher = NotifierTestCase.MockDispatcher(self)
        notifier = Notifier(connection=None, queues=[], trigger_dispatcher=dispatcher)
        notifier.process(execution)

    @mock.patch('st2common.util.action_db.get_action_by_ref', mock.MagicMock(
        return_value=ActionDB(pack='core', name='local', runner_type={'name': 'local-shell-cmd'})))
    @mock.patch('st2common.util.action_db.get_runnertype_by_name', mock.MagicMock(
        return_value=RunnerTypeDB(name='foo', runner_parameters={'runner_foo': 'foo'})))
    @mock.patch.object(Action, 'get_by_ref', mock.MagicMock(
        return_value={'runner_type': {'name': 'local-shell-cmd'}}))
    @mock.patch.object(Policy, 'query', mock.MagicMock(
        return_value=[]))
    @mock.patch.object(Notifier, '_post_generic_trigger', mock.MagicMock(
        return_value=True))
    @mock.patch.object(Notifier, '_get_trace_context', mock.MagicMock(return_value={}))
    @mock.patch('st2common.transport.reactor.TriggerDispatcher.dispatch')
    def test_notify_triggers_jinja_patterns(self, dispatch):
        liveaction_db = LiveActionDB(action='core.local')
        liveaction_db.id = bson.ObjectId()
        liveaction_db.description = ''
        liveaction_db.status = 'succeeded'
        liveaction_db.parameters = {'cmd': 'mamma mia', 'runner_foo': 'foo'}
        on_success = NotificationSubSchema(message='Command {{action_parameters.cmd}} succeeded.',
                                           data={'stdout': '{{action_results.stdout}}'})
        liveaction_db.notify = NotificationSchema(on_success=on_success)
        liveaction_db.start_timestamp = date_utils.get_datetime_utc_now()
        liveaction_db.end_timestamp = \
            (liveaction_db.start_timestamp + datetime.timedelta(seconds=50))

        LiveAction.add_or_update(liveaction_db)

        execution = MOCK_EXECUTION
        execution.liveaction = vars(LiveActionAPI.from_model(liveaction_db))
        execution.status = liveaction_db.status

        notifier = Notifier(connection=None, queues=[])
        notifier.process(execution)
        exp = {'status': 'succeeded',
               'start_timestamp': isotime.format(liveaction_db.start_timestamp),
               'route': 'notify.default', 'runner_ref': 'local-shell-cmd',
               'channel': 'notify.default', 'message': u'Command mamma mia succeeded.',
               'data': {'result': '{}', 'stdout': 'stuff happens'},
               'action_ref': u'core.local',
               'execution_id': str(MOCK_EXECUTION.id),
               'end_timestamp': isotime.format(liveaction_db.end_timestamp)}
        dispatch.assert_called_once_with('core.st2.generic.notifytrigger', payload=exp,
                                         trace_context={})
        notifier.process(execution)

    @mock.patch.object(Notifier, '_get_runner_ref', mock.MagicMock(
        return_value='local-shell-cmd'))
    @mock.patch.object(Notifier, '_get_trace_context', mock.MagicMock(
        return_value={}))
    @mock.patch('st2common.transport.reactor.TriggerDispatcher.dispatch')
    def test_post_generic_trigger_emit_when_default_value_is_used(self, dispatch):
        for status in LIVEACTION_STATUSES:
            liveaction_db = LiveActionDB(action='core.local')
            liveaction_db.status = status
            execution = MOCK_EXECUTION
            execution.liveaction = vars(LiveActionAPI.from_model(liveaction_db))
            execution.status = liveaction_db.status

            notifier = Notifier(connection=None, queues=[])
            notifier._post_generic_trigger(liveaction_db, execution)

            if status in LIVEACTION_COMPLETED_STATES:
                exp = {'status': status,
                       'start_timestamp': str(liveaction_db.start_timestamp),
                       'result': {}, 'parameters': {},
                       'action_ref': u'core.local',
                       'runner_ref': 'local-shell-cmd',
                       'execution_id': str(MOCK_EXECUTION.id),
                       'action_name': u'core.local'}
                dispatch.assert_called_with('core.st2.generic.actiontrigger',
                                            payload=exp, trace_context={})

        self.assertEqual(dispatch.call_count, len(LIVEACTION_COMPLETED_STATES))

    @mock.patch('oslo_config.cfg.CONF.action_sensor', mock.MagicMock(
        emit_when=['scheduled', 'pending', 'abandoned']))
    @mock.patch.object(Notifier, '_get_runner_ref', mock.MagicMock(
        return_value='local-shell-cmd'))
    @mock.patch.object(Notifier, '_get_trace_context', mock.MagicMock(
        return_value={}))
    @mock.patch('st2common.transport.reactor.TriggerDispatcher.dispatch')
    def test_post_generic_trigger_with_emit_condition(self, dispatch):
        for status in LIVEACTION_STATUSES:
            liveaction_db = LiveActionDB(action='core.local')
            liveaction_db.status = status
            execution = MOCK_EXECUTION
            execution.liveaction = vars(LiveActionAPI.from_model(liveaction_db))
            execution.status = liveaction_db.status

            notifier = Notifier(connection=None, queues=[])
            notifier._post_generic_trigger(liveaction_db, execution)

            if status in ['scheduled', 'pending', 'abandoned']:
                exp = {'status': status,
                       'start_timestamp': str(liveaction_db.start_timestamp),
                       'result': {}, 'parameters': {},
                       'action_ref': u'core.local',
                       'runner_ref': 'local-shell-cmd',
                       'execution_id': str(MOCK_EXECUTION.id),
                       'action_name': u'core.local'}
                dispatch.assert_called_with('core.st2.generic.actiontrigger',
                                            payload=exp, trace_context={})

        self.assertEqual(dispatch.call_count, 3)

    @mock.patch('oslo_config.cfg.CONF.action_sensor.enable', mock.MagicMock(
        return_value=True))
    @mock.patch.object(Notifier, '_get_runner_ref', mock.MagicMock(
        return_value='local-shell-cmd'))
    @mock.patch.object(Notifier, '_get_trace_context', mock.MagicMock(
        return_value={}))
    @mock.patch('st2common.transport.reactor.TriggerDispatcher.dispatch')
    @mock.patch('st2actions.notifier.notifier.LiveAction')
    @mock.patch('st2actions.notifier.notifier.policy_service.apply_post_run_policies', mock.Mock())
    def test_process_post_generic_notify_trigger_on_completed_state_default(self,
            mock_LiveAction, mock_dispatch):
        # Verify that generic action trigger is posted on all completed states when action sensor
        # is enabled
        for status in LIVEACTION_STATUSES:
            notifier = Notifier(connection=None, queues=[])

            liveaction_db = LiveActionDB(id=bson.ObjectId(), action='core.local')
            liveaction_db.status = status
            execution = MOCK_EXECUTION
            execution.liveaction = vars(LiveActionAPI.from_model(liveaction_db))
            execution.status = liveaction_db.status

            mock_LiveAction.get_by_id.return_value = liveaction_db

            notifier = Notifier(connection=None, queues=[])
            notifier.process(execution)

            if status in LIVEACTION_COMPLETED_STATES:
                exp = {'status': status,
                       'start_timestamp': str(liveaction_db.start_timestamp),
                       'result': {}, 'parameters': {},
                       'action_ref': u'core.local',
                       'runner_ref': 'local-shell-cmd',
                       'execution_id': str(MOCK_EXECUTION.id),
                       'action_name': u'core.local'}
                mock_dispatch.assert_called_with('core.st2.generic.actiontrigger',
                                                 payload=exp, trace_context={})

        self.assertEqual(mock_dispatch.call_count, len(LIVEACTION_COMPLETED_STATES))

    @mock.patch('oslo_config.cfg.CONF.action_sensor', mock.MagicMock(
        enable=True, emit_when=['scheduled', 'pending', 'abandoned']))
    @mock.patch.object(Notifier, '_get_runner_ref', mock.MagicMock(
        return_value='local-shell-cmd'))
    @mock.patch.object(Notifier, '_get_trace_context', mock.MagicMock(
        return_value={}))
    @mock.patch('st2common.transport.reactor.TriggerDispatcher.dispatch')
    @mock.patch('st2actions.notifier.notifier.LiveAction')
    @mock.patch('st2actions.notifier.notifier.policy_service.apply_post_run_policies', mock.Mock())
    def test_process_post_generic_notify_trigger_on_custom_emit_when_states(self,
            mock_LiveAction, mock_dispatch):
        # Verify that generic action trigger is posted on all completed states when action sensor
        # is enabled
        for status in LIVEACTION_STATUSES:
            notifier = Notifier(connection=None, queues=[])

            liveaction_db = LiveActionDB(id=bson.ObjectId(), action='core.local')
            liveaction_db.status = status
            execution = MOCK_EXECUTION
            execution.liveaction = vars(LiveActionAPI.from_model(liveaction_db))
            execution.status = liveaction_db.status

            mock_LiveAction.get_by_id.return_value = liveaction_db

            notifier = Notifier(connection=None, queues=[])
            notifier.process(execution)

            if status in ['scheduled', 'pending', 'abandoned']:
                exp = {'status': status,
                       'start_timestamp': str(liveaction_db.start_timestamp),
                       'result': {}, 'parameters': {},
                       'action_ref': u'core.local',
                       'runner_ref': 'local-shell-cmd',
                       'execution_id': str(MOCK_EXECUTION.id),
                       'action_name': u'core.local'}
                mock_dispatch.assert_called_with('core.st2.generic.actiontrigger',
                                                 payload=exp, trace_context={})

        self.assertEqual(mock_dispatch.call_count, 3)

{'repo_name': 'StackStorm/st2', 'stars': '3884', 'repo_language': 'Python', 'file_name': 'config.yml', 'mime_type': 'text/plain', 'hash': -863940236814003933, 'source_dataset': 'data'}


from __future__ import with_statement
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig
import logging

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
logger = logging.getLogger('alembic.env')

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from flask import current_app
config.set_main_option('sqlalchemy.url',
                       current_app.config.get('SQLALCHEMY_DATABASE_URI'))
target_metadata = current_app.extensions['migrate'].db.metadata

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.

def run_migrations_offline():
    """Run migrations in 'offline' mode.

    This configures the context with just a URL
    and not an Engine, though an Engine is acceptable
    here as well.  By skipping the Engine creation
    we don't even need a DBAPI to be available.

    Calls to context.execute() here emit the given string to the
    script output.

    """
    url = config.get_main_option("sqlalchemy.url")
    context.configure(url=url)

    with context.begin_transaction():
        context.run_migrations()

def run_migrations_online():
    """Run migrations in 'online' mode.

    In this scenario we need to create an Engine
    and associate a connection with the context.

    """

    # this callback is used to prevent an auto-migration from being generated
    # when there are no changes to the schema
    # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
    def process_revision_directives(context, revision, directives):
        if getattr(config.cmd_opts, 'autogenerate', False):
            script = directives[0]
            if script.upgrade_ops.is_empty():
                directives[:] = []
                logger.info('No changes in schema detected.')

    engine = engine_from_config(config.get_section(config.config_ini_section),
                                prefix='sqlalchemy.',
                                poolclass=pool.NullPool)

    connection = engine.connect()
    context.configure(connection=connection,
                      target_metadata=target_metadata,
                      process_revision_directives=process_revision_directives,
                      **current_app.extensions['migrate'].configure_args)

    try:
        with context.begin_transaction():
            context.run_migrations()
    except Exception as exception:
        logger.error(exception)
        raise exception
    finally:
        connection.close()

if context.is_offline_mode():
    run_migrations_offline()
else:
    run_migrations_online()

{'repo_name': 'okomarov/dash_on_flask', 'stars': '153', 'repo_language': 'Python', 'file_name': '8e9c46887a13_init.py', 'mime_type': 'text/x-python', 'hash': -1107039489958967875, 'source_dataset': 'data'}


# %% [markdown]
# # Crowdsourcing Tutorial

# %% [markdown]
# In this tutorial, we'll provide a simple walkthrough of how to use Snorkel in conjunction with crowdsourcing to create a training set for a sentiment analysis task.
# We already have crowdsourced labels for about half of the training dataset.
# The crowdsourced labels are fairly accurate, but do not cover the entire training dataset, nor are they available for the test set or during inference.
# To make up for their lack of training set coverage, we combine crowdsourced labels with heuristic labeling functions to increase the number of training labels we have.
# Like most Snorkel labeling pipelines, we'll use the denoised labels to train a deep learning
# model which can be applied to new, unseen data to automatically make predictions.

# %% [markdown]
# ## Dataset Details

# %% [markdown]
# In this tutorial, we'll use the [Weather Sentiment](https://data.world/crowdflower/weather-sentiment) dataset from Figure Eight.
# Our goal is to train a classifier that can label new tweets as expressing either a positive or negative sentiment.
#
# Crowdworkers were asked to label the sentiment of a particular tweet relating to the weather.
# The catch is that 20 crowdworkers graded each tweet, and in many cases crowdworkers assigned conflicting sentiment labels to the same tweet.
# This is a common issue when dealing with crowdsourced labeling workloads.
#
# Label options were positive, negative, or one of three other options saying they weren't sure if it was positive or negative; we use only the positive/negative labels.
# We've also altered the dataset to reflect a realistic crowdsourcing pipeline where only a subset of our available training set has received crowd labels.
#
# We will treat each crowdworker's labels as coming from a single labeling function (LF).
# This will allow us to learn a weight for how much to trust the labels from each crowdworker.
# We will also write a few heuristic labeling functions to cover the data points without crowd labels.
# Snorkel's ability to build high-quality datasets from multiple noisy labeling signals makes it an ideal framework to approach this problem.

# %% [markdown]
# ## Loading Crowdsourcing Dataset

# %% [markdown]
# We start by loading our data which has 287 data points in total.
# We take 50 for our development set and 50 for our test set.
# The remaining 187 data points form our training set.
# Since the dataset is already small, we skip using a validation set.
# Note that this very small dataset is primarily used for demonstration purposes here.
# In a real setting, we would expect to have access to many more unlabeled tweets, which could help us to train a higher quality model.

# %% {"tags": ["md-exclude"]}
import os

# Make sure we're in the right directory
if os.path.basename(os.getcwd()) == "snorkel-tutorials":
    os.chdir("crowdsourcing")

# %%
from data import load_data

crowd_labels, df_train, df_dev, df_test = load_data()
Y_dev = df_dev.sentiment.values
Y_test = df_test.sentiment.values

# %% [markdown] {"tags": ["md-exclude"]}
# First, let's take a look at our development set to get a sense of what the tweets look like.
# We use the following label convention: 0 = Negative, 1 = Positive.

# %% {"tags": ["md-exclude"]}
import pandas as pd

# Don't truncate text fields in the display
pd.set_option("display.max_colwidth", 0)

df_dev.head()

# %% [markdown] {"tags": ["md-exclude"]}
# Now let's take a look at the crowd labels.
# We'll convert these into labeling functions.

# %% {"tags": ["md-exclude"]}
crowd_labels.head()

# %% [markdown]
# ## Writing Labeling Functions
# Each crowdworker can be thought of as a single labeling function,
# as each worker labels a subset of data points,
# and may have errors or conflicting labels with other workers / labeling functions.
# So we create one labeling function per worker.
# We'll simply return the label the worker submitted for a given tweet, and abstain
# if they didn't submit a label for it.

# %% [markdown]
# ### Crowdworker labeling functions

# %%
labels_by_annotator = crowd_labels.groupby("worker_id")
worker_dicts = {}
for worker_id in labels_by_annotator.groups:
    worker_df = labels_by_annotator.get_group(worker_id)[["label"]]
    worker_dicts[worker_id] = dict(zip(worker_df.index, worker_df.label))

print("Number of workers:", len(worker_dicts))

# %%
from snorkel.labeling import LabelingFunction

ABSTAIN = -1

def worker_lf(x, worker_dict):
    return worker_dict.get(x.tweet_id, ABSTAIN)

def make_worker_lf(worker_id):
    worker_dict = worker_dicts[worker_id]
    name = f"worker_{worker_id}"
    return LabelingFunction(name, f=worker_lf, resources={"worker_dict": worker_dict})

worker_lfs = [make_worker_lf(worker_id) for worker_id in worker_dicts]

# %% [markdown]
# Let's take a quick look at how well they do on the development set.

# %% {"tags": ["md-exclude-output"]}
from snorkel.labeling import PandasLFApplier

applier = PandasLFApplier(worker_lfs)
L_train = applier.apply(df_train)
L_dev = applier.apply(df_dev)

# %% [markdown]
# Note that because our dev set is so small and our LFs are relatively sparse, many LFs will appear to have zero coverage.
# Fortunately, our label model learns weights for LFs based on their outputs on the training set, which is generally much larger.

# %%
from snorkel.labeling import LFAnalysis

LFAnalysis(L_dev, worker_lfs).lf_summary(Y_dev).sample(5)

# %% [markdown]
# So the crowd labels in general are quite good! But how much of our dev and training
# sets do they cover?

# %%
print(f"Training set coverage: {100 * LFAnalysis(L_train).label_coverage(): 0.1f}%")
print(f"Dev set coverage: {100 * LFAnalysis(L_dev).label_coverage(): 0.1f}%")

# %% [markdown]
# ### Additional labeling functions
#
# To improve coverage of the training set, we can mix the crowdworker labeling functions with labeling
# functions of other types.
# For example, we can use [TextBlob](https://textblob.readthedocs.io/en/dev/index.html), a tool that provides a pretrained sentiment analyzer. We run TextBlob on our tweets and create some simple LFs that threshold its polarity score, similar to what we did in the spam_tutorial.

# %%
from snorkel.labeling import labeling_function
from snorkel.preprocess import preprocessor
from textblob import TextBlob

@preprocessor(memoize=True)
def textblob_polarity(x):
    scores = TextBlob(x.tweet_text)
    x.polarity = scores.polarity
    return x

# Label high polarity tweets as positive.
@labeling_function(pre=[textblob_polarity])
def polarity_positive(x):
    return 1 if x.polarity > 0.3 else -1

# Label low polarity tweets as negative.
@labeling_function(pre=[textblob_polarity])
def polarity_negative(x):
    return 0 if x.polarity < -0.25 else -1

# Similar to polarity_negative, but with higher coverage and lower precision.
@labeling_function(pre=[textblob_polarity])
def polarity_negative_2(x):
    return 0 if x.polarity <= 0.3 else -1

# %% [markdown]
# ### Applying labeling functions to the training set

# %% {"tags": ["md-exclude-output"]}
text_lfs = [polarity_positive, polarity_negative, polarity_negative_2]
lfs = text_lfs + worker_lfs

applier = PandasLFApplier(lfs)
L_train = applier.apply(df_train)
L_dev = applier.apply(df_dev)

# %%
LFAnalysis(L_dev, lfs).lf_summary(Y_dev).head()

# %% [markdown]
# Using the text-based LFs, we've expanded coverage on both our training set
# and dev set to 100%.
# We'll now take these noisy and conflicting labels, and use the LabelModel
# to denoise and combine them.

# %%
print(f"Training set coverage: {100 * LFAnalysis(L_train).label_coverage(): 0.1f}%")
print(f"Dev set coverage: {100 * LFAnalysis(L_dev).label_coverage(): 0.1f}%")

# %% [markdown]
# ## Train LabelModel And Generate Probabilistic Labels

# %% {"tags": ["md-exclude-output"]}
from snorkel.labeling.model import LabelModel

# Train LabelModel.
label_model = LabelModel(cardinality=2, verbose=True)
label_model.fit(L_train, n_epochs=100, seed=123, log_freq=20, l2=0.1, lr=0.01)

# %% [markdown]
# As a spot-check for the quality of our LabelModel, we'll score it on the dev set.

# %%
from snorkel.analysis import metric_score

preds_dev = label_model.predict(L_dev)

acc = metric_score(Y_dev, preds_dev, probs=None, metric="accuracy")
print(f"LabelModel Accuracy: {acc:.3f}")

# %% [markdown]
# We see that we get very high accuracy on the development set.
# This is due to the abundance of high quality crowdworker labels.
# **Since we don't have these high quality crowdsourcing labels for the
# test set or new incoming data points, we can't use the LabelModel reliably
# at inference time.**
# In order to run inference on new incoming data points, we need to train a
# discriminative model over the tweets themselves.
# Let's generate a set of labels for that training set.

# %%
preds_train = label_model.predict(L_train)

# %% [markdown]
# ## Use Soft Labels to Train End Model

# %% [markdown]
# ### Getting features from BERT
# Since we have very limited training data, we cannot train a complex model like an LSTM with a lot of parameters.
# Instead, we use a pre-trained model, [BERT](https://github.com/google-research/bert), to generate embeddings for each our tweets, and treat the embedding values as features.
# This may take 5-10 minutes on a CPU, as the BERT model is very large.

# %% {"tags": ["md-exclude-output"]}
import numpy as np
import torch
from pytorch_transformers import BertModel, BertTokenizer

model = BertModel.from_pretrained("bert-base-uncased")
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")

def encode_text(text):
    input_ids = torch.tensor([tokenizer.encode(text)])
    return model(input_ids)[0].mean(1)[0].detach().numpy()

X_train = np.array(list(df_train.tweet_text.apply(encode_text).values))
X_test = np.array(list(df_test.tweet_text.apply(encode_text).values))

# %% [markdown]
# ### Model on labels
# Now, we train a simple logistic regression model on the BERT features, using labels
# obtained from our LabelModel.

# %% {"tags": ["md-exclude-output"]}
from sklearn.linear_model import LogisticRegression

sklearn_model = LogisticRegression(solver="liblinear")
sklearn_model.fit(X_train, preds_train)

# %%
print(f"Accuracy of trained model: {sklearn_model.score(X_test, Y_test)}")

# %% [markdown]
# We now have a trained model that can be applied to future data points without requiring crowdsourced labels, and with accuracy not much lower than the `LabelModel` that _does_ have access to crowdsourced labels!

# %% [markdown]
# ## Summary
#
# In this tutorial, we accomplished the following:
# * We demonstrated how to combine crowdsourced labels with other programmatic LFs to improve coverage.
# * We used the `LabelModel` to combine inputs from crowdworkers and other LFs to generate high quality probabilistic labels.
# * We used our labels to train a classifier for making predictions on new, unseen data points.

{'repo_name': 'snorkel-team/snorkel-tutorials', 'stars': '143', 'repo_language': 'Python', 'file_name': 'spouse_demo.py', 'mime_type': 'text/x-python', 'hash': -5850045433052576231, 'source_dataset': 'data'}


# RUN: not llc -o - -march=arm64 -global-isel -run-pass=none -verify-machineinstrs %s 2>&1 | FileCheck %s
# REQUIRES: global-isel, aarch64-registered-target

---
name:            test_trunc
legalized:       true
regBankSelected: false
selected:        false
tracksRegLiveness: true
liveins:
body:             |
  bb.0:

    ; CHECK: Bad machine code: Too few operands
    %0:_(s32) = G_TRUNC

    %1:_(s64) = G_IMPLICIT_DEF

    ; CHECK: Bad machine code: Too few operands
    ; CHECK: Bad machine code: Explicit definition marked as use
    G_TRUNC %1

...

{'repo_name': 'earl/llvm-mirror', 'stars': '122', 'repo_language': 'LLVM', 'file_name': 'registers_readonly.s', 'mime_type': 'text/plain', 'hash': 877995628859694178, 'source_dataset': 'data'}


# -*- coding: binary -*-

require 'rc4'

module Rex
  module Crypto

    # Returns a decrypted or encrypted RC4 string.
    #
    # @param key [String] Secret key.
    # @param [String]
    def self.rc4(key, value)
      rc4 = RC4.new(key)

      # This can also be used to decrypt
      rc4.encrypt(value)
    end

  end
end

{'repo_name': 'rapid7/metasploit-framework', 'stars': '20997', 'repo_language': 'Ruby', 'file_name': 'token_adduser.rb', 'mime_type': 'text/x-ruby', 'hash': -473109219708902685, 'source_dataset': 'data'}


// license:BSD-3-Clause
// copyright-holders:Ryan Holtz
/***************************************************************************

    tmc208k.h
    TRW TMC208K/TMC28KU 8x8-bit Parallel Multiplier

    Known Equivalents:
    - Logic Devices Inc. LMU08/8U

***************************************************************************/

#ifndef MAME_MACHINE_TMC208K_TMC208K_H
#define MAME_MACHINE_TMC208K_TMC208K_H

#pragma once

//**************************************************************************
//  TYPE DEFINITIONS
//**************************************************************************

// ======================> tmc208_base_device

template <typename RegType, typename OutType>
class tmc208_base_device : public device_t
{
public:
	// construction/destruction
	tmc208_base_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock = 0);

	void a_w(uint8_t data);
	void b_w(uint8_t data);
	void trim_w(int state);
	void tril_w(int state);
	void clk_a_w(int state);
	void clk_b_w(int state);
	void clk_r_w(int state);
	void rnd_w(int state);

	// Outputs by group
	auto msp() { return m_msp.bind(); }
	auto lsp() { return m_lsp.bind(); }

	// Full output
	auto r() { return m_r.bind(); }

protected:
	// device-level overrides
	virtual void device_start() override;
	virtual void device_reset() override;

	void clock_a();
	virtual void clock_b();
	void multiply();

	RegType m_a_in;
	RegType m_a;
	RegType m_b_in;
	RegType m_b;
	OutType m_r_out;

	bool m_trim;
	bool m_tril;
	bool m_clk_a;
	bool m_clk_b;
	bool m_clk_r;
	bool m_rnd_in;
	bool m_rnd;

	devcb_write8 m_msp;
	devcb_write8 m_lsp;
	devcb_write16 m_r;
};

// ======================> tmc208k_device

class tmc208k_device : public tmc208_base_device<int8_t, int16_t>
{
public:
	// construction/destruction
	tmc208k_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock = 0);
};

// ======================> tmc28ku_device

class tmc28ku_device : public tmc208_base_device<uint8_t, uint16_t>
{
public:
	// construction/destruction
	tmc28ku_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock = 0);

protected:
	virtual void clock_b() override;
};

// device type definition
DECLARE_DEVICE_TYPE(TMC208K, tmc208k_device)
DECLARE_DEVICE_TYPE(TMC28KU, tmc28ku_device)

#endif // MAME_MACHINE_TMC208K_TMC208K_H

{'repo_name': 'mamedev/mame', 'stars': '4604', 'repo_language': 'C++', 'file_name': 'z-alphabumpspec-inES3.txt', 'mime_type': 'text/x-c', 'hash': 5009703044366907553, 'source_dataset': 'data'}


/**
 * Copyright (c) 2016-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the license found in the LICENSE file in
 * the root directory of this source tree.
 */

#import "FBMemoryProfilerSegmentedControl.h"

@implementation FBMemoryProfilerSegmentedControl

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
  NSInteger currentSegment = self.selectedSegmentIndex;

  [super touchesEnded:touches withEvent:event];

  if (currentSegment == self.selectedSegmentIndex) {
    [self sendActionsForControlEvents:UIControlEventValueChanged];
  }
}

@end

{'repo_name': 'facebook/FBMemoryProfiler', 'stars': '3320', 'repo_language': 'Objective-C', 'file_name': 'FBMemoryProfiler.xcscheme', 'mime_type': 'text/xml', 'hash': 6535958889212042597, 'source_dataset': 'data'}


#ifndef PYTHONIC_INCLUDE_OMP_GET_THREAD_NUM_HPP
#define PYTHONIC_INCLUDE_OMP_GET_THREAD_NUM_HPP

#include <omp.h>
#include "pythonic/include/utils/functor.hpp"

PYTHONIC_NS_BEGIN

namespace omp
{

  long get_thread_num();

  DEFINE_FUNCTOR(pythonic::omp, get_thread_num);
}
PYTHONIC_NS_END

#endif

{'repo_name': 'serge-sans-paille/pythran', 'stars': '1428', 'repo_language': 'Python', 'file_name': 'typeof_impl.hpp', 'mime_type': 'text/x-c++', 'hash': 6869561491843169225, 'source_dataset': 'data'}


<?php

namespace Exceedone\Exment\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Grid\Linker;
use Illuminate\Http\Request;
use Exceedone\Exment\Model\CustomTable;
use Exceedone\Exment\Model\Notify;
use Exceedone\Exment\Model\Define;
use Exceedone\Exment\Model\Menu;
use Exceedone\Exment\Form\Tools;
use Exceedone\Exment\Enums\MailKeyName;
use Exceedone\Exment\Enums\FilterType;
use Exceedone\Exment\Enums\FilterOption;
use Exceedone\Exment\Enums\SystemTableName;
use Exceedone\Exment\Enums\NotifyTrigger;
use Exceedone\Exment\Enums\NotifyAction;
use Exceedone\Exment\Enums\NotifyActionTarget;
use Exceedone\Exment\Enums\NotifySavedType;
use Exceedone\Exment\Enums\MenuType;
use Exceedone\Exment\Enums\Permission;
use Exceedone\Exment\Enums\FormActionType;
use Exceedone\Exment\Enums\MultisettingType;
use Exceedone\Exment\Enums\ShareTrigger;
use Exceedone\Exment\Enums\SharePermission;

class CustomTableController extends AdminControllerBase
{
    use HasResourceActions;

    protected $exists = false;

    public function __construct()
    {
        $this->setPageInfo(exmtrans("custom_table.header"), exmtrans("custom_table.header"), exmtrans("custom_table.description"), 'fa-table');
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Request $request, Content $content)
    {
        $content = $this->AdminContent($content);

        $row = new Row($this->grid());
        $row->class(['block_custom_table']);

        return $content->row($row);
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CustomTable);
        $grid->column('table_name', exmtrans("custom_table.table_name"))->sortable();
        $grid->column('table_view_name', exmtrans("custom_table.table_view_name"))->sortable();
        $grid->column('order', exmtrans("custom_table.order"))->editable('number')->sortable();

        $grid->tools(function (Grid\Tools $tools) {
            $tools->disableBatchActions();
            $tools->append(new Tools\CustomTableMenuAjaxButton());
        });

        $grid->disableExport();
        if (!\Exment::user()->hasPermission(Permission::CUSTOM_TABLE)) {
            $grid->disableCreateButton();
        }

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableDelete();

            // add new multiple columns
            $linker = (new Linker)
                ->url(admin_urls('table', $actions->getKey(), 'edit').'?columnmulti=1')
                ->icon('fa-cogs')
                ->tooltip(exmtrans('custom_table.expand_setting'));
            $actions->append($linker);

            $custom_table = $actions->row;

            // add custom column
            if ($custom_table->hasPermission(Permission::CUSTOM_TABLE)) {
                $linker = (new Linker)
                ->url(admin_urls('column', $custom_table->table_name))
                ->icon('fa-list')
                ->tooltip(exmtrans('change_page_menu.custom_column'));
                $actions->append($linker);
            }

            // add data
            if ($custom_table->hasPermission(Permission::AVAILABLE_VIEW_CUSTOM_VALUE)) {
                $linker = (new Linker)
                ->url($actions->row->getGridUrl())
                ->icon('fa-database')
                ->tooltip(exmtrans('change_page_menu.custom_value'));
                $actions->append($linker);
            }
        });

        // filter table --------------------------------------------------
        CustomTable::filterList($grid->model(), ['getModel' => false]);

        $grid->filter(function ($filter) {
            $filter->like('table_name', exmtrans("custom_table.table_name"));
            $filter->like('table_view_name', exmtrans("custom_table.table_view_name"));
        });

        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = null)
    {
        $form = new Form(new CustomTable);
        if (!isset($id)) {
            $form->text('table_name', exmtrans("custom_table.table_name"))
                ->required()
                ->rules("max:30|unique:".CustomTable::getTableName()."|regex:/".Define::RULES_REGEX_SYSTEM_NAME."/")
                ->help(sprintf(exmtrans('common.help.max_length'), 30) . exmtrans('common.help_code'));
        } else {
            $form->display('table_name', exmtrans("custom_table.table_name"));
        }
        $form->text('table_view_name', exmtrans("custom_table.table_view_name"))
            ->required()
            ->rules("max:40")
            ->help(exmtrans('common.help.view_name'));
        $form->textarea('description', exmtrans("custom_table.field_description"))->rows(3);

        $form->number('order', exmtrans("custom_table.order"))->rules("integer")
            ->help(sprintf(exmtrans("common.help.order"), exmtrans('common.custom_table')));

        $form->exmheader(exmtrans('common.detail_setting'))->hr();

        $form->embeds('options', exmtrans("custom_column.options.header"), function ($form) {
            $form->color('color', exmtrans("custom_table.color"))->help(exmtrans("custom_table.help.color"));
            $form->icon('icon', exmtrans("custom_table.icon"))->help(exmtrans("custom_table.help.icon"));
            $form->switchbool('search_enabled', exmtrans("custom_table.search_enabled"))->help(exmtrans("custom_table.help.search_enabled"))->default("1")
            ;
            $form->switchbool('use_label_id_flg', exmtrans("custom_table.use_label_id_flg"))
                ->help(sprintf(exmtrans("custom_table.help.use_label_id_flg"), getManualUrl('column?id='.exmtrans('custom_column.options.use_label_flg'))))
                ->default("0")
                ;
            $form->switchbool('one_record_flg', exmtrans("custom_table.one_record_flg"))
                ->help(exmtrans("custom_table.help.one_record_flg"))
                ->default("0")
                ;
            $form->switchbool('attachment_flg', exmtrans("custom_table.attachment_flg"))->help(exmtrans("custom_table.help.attachment_flg"))
                ->default("1")
                ;
            $form->switchbool('comment_flg', exmtrans("custom_table.comment_flg"))
                ->help(exmtrans("custom_table.help.comment_flg"))
                ->default("1")
                ;
            $form->switchbool('revision_flg', exmtrans("custom_table.revision_flg"))->help(exmtrans("custom_table.help.revision_flg"))
                ->default("1")
                ->attribute(['data-filtertrigger' =>true])
                ;
            $form->number('revision_count', exmtrans("custom_table.revision_count"))->help(exmtrans("custom_table.help.revision_count"))
                ->min(0)
                ->max(500)
                ->default(config('exment.revision_count', 100))
                ->attribute(['data-filter' => json_encode(['key' => 'options_revision_flg', 'value' => "1"])])
                ;

            $form->exmheader(exmtrans('role_group.permission_setting'))->hr();

            $form->switchbool('all_user_editable_flg', exmtrans("custom_table.all_user_editable_flg"))->help(exmtrans("custom_table.help.all_user_editable_flg"))
                ->default("0");

            $form->switchbool('all_user_viewable_flg', exmtrans("custom_table.all_user_viewable_flg"))->help(exmtrans("custom_table.help.all_user_viewable_flg"))
                ->default("0");

            $form->switchbool('all_user_accessable_flg', exmtrans("custom_table.all_user_accessable_flg"))->help(exmtrans("custom_table.help.all_user_accessable_flg"))
                ->default("0");
        })->disableHeader();

        // if create table, show menulist
        if (!isset($id)) {
            $form->exmheader(exmtrans('common.create_only_setting'))->hr();

            $form->switchbool('add_parent_menu_flg', exmtrans("custom_table.add_parent_menu_flg"))->help(exmtrans("custom_table.help.add_parent_menu_flg"))
                ->default("0")
                ->attribute(['data-filtertrigger' =>true])
            ;
            $form->select('add_parent_menu', exmtrans("custom_table.add_parent_menu"))->help(exmtrans("custom_table.help.add_parent_menu"))
            ->options(function ($value) {
                $options = Menu::selectOptions();
                return $options;
            })
            ->attribute(['data-filter' => json_encode(['key' => 'add_parent_menu_flg', 'value' => '1'])]);
            ;
            $form->ignore('add_parent_menu');
            $form->ignore('add_parent_menu_flg');

            $form->switchbool('add_notify_flg', exmtrans("custom_table.add_notify_flg"))->help(exmtrans("custom_table.help.add_notify_flg"))
                ->default("0")
            ;
            $form->ignore('add_notify_flg');
        }

        // Role setting --------------------------------------------------
        $deleteButton = $this->confirmDeleteButton($id);

        $form->tools(function (Form\Tools $tools) use ($id, $deleteButton) {
            $custom_table = CustomTable::getEloquent($id);
            if (isset($custom_table) && $custom_table->disabled_delete) {
                $tools->disableDelete();
            } elseif (isset($deleteButton)) {
                $tools->disableDelete();
                $tools->prepend($deleteButton);
            }
            // if edit mode
            if ($id != null) {
                $model = CustomTable::getEloquent($id);
                $tools->append((new Tools\CustomTableMenuButton('table', $model, 'default_setting')));
            }
        });

        $form->saving(function (Form $form) {
            $this->exists = $form->model()->exists;
        });

        $form->savedInTransaction(function (Form $form) {
            // create or drop index --------------------------------------------------
            $model = $form->model();
            // if has value 'add_parent_menu', add menu
            $this->addMenuAfterSaved($model);

            // if has value 'add_notify_flg', add notify
            $this->addNotifyAfterSaved($model);
        });

        if ($id != null) {
            $form->disableEditingCheck(false);
        }
        $form->saved(function (Form $form) {
            // create or drop index --------------------------------------------------
            $model = $form->model();
            $model->createTable();

            // redirect custom column page
            if (!$this->exists) {
                $table_name = CustomTable::getEloquent($model->id)->table_name;
                $custom_column_url = admin_urls('column', $table_name);

                admin_toastr(exmtrans('custom_table.help.saved_redirect_column'));
                return redirect($custom_column_url);
            }
        });

        return $form;
    }

    /**
     * Render `delete` button.
     *
     * @return ?string
     */
    protected function confirmDeleteButton($id = null)
    {
        if (is_null($id)) {
            return null;
        }

        $url = url(admin_urls('table', $id));
        $listUrl = url(admin_urls('table'));
        $keyword = Define::DELETE_CONFIRM_KEYWORD;
        $trans = [
            'delete_confirm' => trans('admin.delete_confirm'),
            'confirm'        => trans('admin.confirm'),
            'cancel'         => trans('admin.cancel'),
            'delete'         => trans('admin.delete'),
            'delete_guide'   => sprintf(exmtrans('custom_table.help.delete_confirm_message'), $keyword),
            'delete_keyword' => exmtrans('custom_table.help.delete_confirm_error'),
        ];

        $class = uniqid();

        $script = <<

{'repo_name': 'exceedone/exment', 'stars': '121', 'repo_language': 'PHP', 'file_name': 'bootstrap-number-input.js', 'mime_type': 'text/plain', 'hash': -2572998686918177950, 'source_dataset': 'data'}


(*
 * Copyright (C) 2006-2010 Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *)

open Db_rpc_common_v1
open Db_exn

module Make =
functor
  (RPC : Db_interface.RPC)
  ->
  struct
    exception Remote_db_server_returned_unknown_exception

    (* Process an exception returned from server, throwing local exception *)
    let process_exception_xml xml =
      match XMLRPC.From.array (fun x -> x) xml with
      | [exn_name_xml; exn_params_xml] -> (
          let exn_name = XMLRPC.From.string exn_name_xml in
          match exn_name with
          | "dbcache_notfound" ->
              let x, y, z = unmarshall_3strings exn_params_xml in
              raise (DBCache_NotFound (x, y, z))
          | "duplicate_key_of" ->
              let w, x, y, z = unmarshall_4strings exn_params_xml in
              raise (Duplicate_key (w, x, y, z))
          | "uniqueness_constraint_violation" ->
              let x, y, z = unmarshall_3strings exn_params_xml in
              raise (Uniqueness_constraint_violation (x, y, z))
          | "read_missing_uuid" ->
              let x, y, z = unmarshall_3strings exn_params_xml in
              raise (Read_missing_uuid (x, y, z))
          | "too_many_values" ->
              let x, y, z = unmarshall_3strings exn_params_xml in
              raise (Too_many_values (x, y, z))
          | _ ->
              raise DB_remote_marshall_error
        )
      | _ ->
          raise Remote_db_server_returned_unknown_exception

    exception Remote_db_server_returned_bad_message

    let do_remote_call marshall_args unmarshall_resp fn_name args =
      let xml = marshall_args args in
      let xml =
        XMLRPC.To.array
          [XMLRPC.To.string fn_name; XMLRPC.To.string "" (* unused *); xml]
      in
      let resp = RPC.rpc (Xml.to_string xml) |> Xml.parse_string in
      match XMLRPC.From.array (fun x -> x) resp with
      | [status_xml; resp_xml] ->
          let status = XMLRPC.From.string status_xml in
          if status = "success" then
            unmarshall_resp resp_xml
          else
            process_exception_xml resp_xml
      | _ ->
          raise Remote_db_server_returned_bad_message

    let get_table_from_ref _ x =
      do_remote_call marshall_get_table_from_ref_args
        unmarshall_get_table_from_ref_response "get_table_from_ref" x

    let is_valid_ref _ x =
      do_remote_call marshall_is_valid_ref_args unmarshall_is_valid_ref_response
        "is_valid_ref" x

    let read_refs _ x =
      do_remote_call marshall_read_refs_args unmarshall_read_refs_response
        "read_refs" x

    let read_field_where _ x =
      do_remote_call marshall_read_field_where_args
        unmarshall_read_field_where_response "read_field_where" x

    let db_get_by_uuid _ t u =
      do_remote_call marshall_db_get_by_uuid_args
        unmarshall_db_get_by_uuid_response "db_get_by_uuid" (t, u)

    let db_get_by_name_label _ t l =
      do_remote_call marshall_db_get_by_name_label_args
        unmarshall_db_get_by_name_label_response "db_get_by_name_label" (t, l)

    let create_row _ x y z =
      do_remote_call marshall_create_row_args unmarshall_create_row_response
        "create_row" (x, y, z)

    let delete_row _ x y =
      do_remote_call marshall_delete_row_args unmarshall_delete_row_response
        "delete_row" (x, y)

    let write_field _ a b c d =
      do_remote_call marshall_write_field_args unmarshall_write_field_response
        "write_field" (a, b, c, d)

    let read_field _ x y z =
      do_remote_call marshall_read_field_args unmarshall_read_field_response
        "read_field" (x, y, z)

    let find_refs_with_filter _ s e =
      do_remote_call marshall_find_refs_with_filter_args
        unmarshall_find_refs_with_filter_response "find_refs_with_filter" (s, e)

    let read_record _ x y =
      do_remote_call marshall_read_record_args unmarshall_read_record_response
        "read_record" (x, y)

    let read_records_where _ x e =
      do_remote_call marshall_read_records_where_args
        unmarshall_read_records_where_response "read_records_where" (x, e)

    let process_structured_field _ a b c d e =
      do_remote_call marshall_process_structured_field_args
        unmarshall_process_structured_field_response "process_structured_field"
        (a, b, c, d, e)

    let initialise = RPC.initialise
  end

{'repo_name': 'xapi-project/xen-api', 'stars': '295', 'repo_language': 'OCaml', 'file_name': 'unixpwd.c', 'mime_type': 'text/x-c', 'hash': -4556919479898794355, 'source_dataset': 'data'}


import * as React from "react"
import { GenericPillComponent, PillRenderingContext } from "../generic-pill.component"
import { Box, PillLogoProps } from "@dfuse/explorer"
import { FormattedText } from "../../../formatted-text/formatted-text"
import { ExternalTextLink } from "../../../../atoms/text/text.component"

import { getInfiniverseMakeOfferLevel1Fields } from "../pill-template.helpers"

export class MakeOfferPillComponent extends GenericPillComponent {
  get logoParams(): PillLogoProps | undefined {
    return {
      path: "/images/pill-logos/logo-contract-infiniverse-01.svg",
      website: "https://infiniverse.net"
    }
  }

  static requireFields: string[] = ["buyer", "land_id", "price"]

  static contextForRendering = (): PillRenderingContext => {
    return {
      networks: ["eos-mainnet"],

      validActions: [{ contract: "infiniverse1", action: "makeoffer" }]
    }
  }

  static i18n() {
    return {
      en: {
        infiniversemakeoffer: {
          summary: "<0>{{buyer}}</0> offers <1>{{quantity}}</1> for Land <2>{{land_id}}</2>"
        }
      },
      zh: {
        infiniversemakeoffer: {
          summary: "<0>{{buyer}}</0> 为 Land <2>{{land_id}}</2> 报价 <1>{{quantity}}</1>"
        }
      }
    }
  }

  renderContent = () => {
    const { action } = this.props

    return (
      <Box fontSize={[1]} mx={[2]} minWidth="10px" minHeight="26px" alignItems="center">
        <FormattedText
          fields={getInfiniverseMakeOfferLevel1Fields(action)}
          i18nKey="pillTemplates.infiniversemakeoffer.summary"
          fontSize={[1]}
        />
      </Box>
    )
  }

  renderLevel2Template = () => {
    return (
      <Box fontSize={[1]} mx={[2]} minWidth="10px" minHeight="26px" alignItems="center">
        <ExternalTextLink to="https://infiniverse.net">https://infiniverse.net</ExternalTextLink>
      </Box>
    )
  }
}

{'repo_name': 'dfuse-io/dfuse-eosio', 'stars': '147', 'repo_language': 'Go', 'file_name': 'logging.go', 'mime_type': 'text/plain', 'hash': -2565457336370924016, 'source_dataset': 'data'}


/*
 *      Copyright (C) 2005-2013 Team XBMC
 *      http://xbmc.org
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with XBMC; see the file COPYING.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

#include "PeripheralBusUSBLibUdev.h"
#include "peripherals/Peripherals.h"
extern "C" {
#include <libudev.h>
}
#include <poll.h>
#include "utils/log.h"

#ifndef USB_CLASS_PER_INTERFACE
#define USB_CLASS_PER_INTERFACE         0
#endif

#ifndef USB_CLASS_AUDIO
#define USB_CLASS_AUDIO                 1
#endif

#ifndef USB_CLASS_COMM
#define USB_CLASS_COMM                  2
#endif

#ifndef USB_CLASS_HID
#define USB_CLASS_HID                   3
#endif

#ifndef USB_CLASS_PHYSICAL
#define USB_CLASS_PHYSICAL              5
#endif

#ifndef USB_CLASS_PTP
#define USB_CLASS_PTP                   6
#endif

#ifndef USB_CLASS_PRINTER
#define USB_CLASS_PRINTER               7
#endif

#ifndef USB_CLASS_MASS_STORAGE
#define USB_CLASS_MASS_STORAGE          8
#endif

#ifndef USB_CLASS_HUB
#define USB_CLASS_HUB                   9
#endif

#ifndef USB_CLASS_DATA
#define USB_CLASS_DATA                  10
#endif

#ifndef USB_CLASS_APP_SPEC
#define USB_CLASS_APP_SPEC              0xfe
#endif

#ifndef USB_CLASS_VENDOR_SPEC
#define USB_CLASS_VENDOR_SPEC           0xff
#endif

using namespace PERIPHERALS;

CPeripheralBusUSB::CPeripheralBusUSB(CPeripherals *manager) :
    CPeripheralBus("PeripBusUSBUdev", manager, PERIPHERAL_BUS_USB)
{
  /* the Process() method in this class overrides the one in CPeripheralBus, so leave this set to true */
  m_bNeedsPolling = true;

  m_udev          = NULL;
  m_udevMon       = NULL;

  if (!(m_udev = udev_new()))
  {
    CLog::Log(LOGERROR, "%s - failed to allocate udev context", __FUNCTION__);
    return;
  }

  /* set up a devices monitor that listen for any device change */
  m_udevMon = udev_monitor_new_from_netlink(m_udev, "udev");
  udev_monitor_enable_receiving(m_udevMon);

  CLog::Log(LOGDEBUG, "%s - initialised udev monitor", __FUNCTION__);
}

CPeripheralBusUSB::~CPeripheralBusUSB(void)
{
  StopThread(true);
  udev_monitor_unref(m_udevMon);
  udev_unref(m_udev);
}

bool CPeripheralBusUSB::PerformDeviceScan(PeripheralScanResults &results)
{
  struct udev_enumerate *enumerate;
  struct udev_list_entry *devices, *dev_list_entry;
  struct udev_device *dev(NULL), *parent(NULL);
  enumerate = udev_enumerate_new(m_udev);
  udev_enumerate_scan_devices(enumerate);
  devices = udev_enumerate_get_list_entry(enumerate);

  bool bContinue(true);
  std::string strPath, strClass;
  udev_list_entry_foreach(dev_list_entry, devices)
  {
    strPath = udev_list_entry_get_name(dev_list_entry);
    if (strPath.empty())
      bContinue = false;

    if (bContinue)
    {
      if (!(parent = udev_device_new_from_syspath(m_udev, strPath.c_str())))
        bContinue = false;
    }

    if (bContinue)
    {
      dev = udev_device_get_parent(udev_device_get_parent(parent));
      if (!dev || !udev_device_get_sysattr_value(dev,"idVendor") || !udev_device_get_sysattr_value(dev, "idProduct"))
        bContinue = false;
    }

    if (bContinue)
    {
      strClass = udev_device_get_sysattr_value(dev, "bDeviceClass");
      if (strClass.empty())
        bContinue = false;
    }

    if (bContinue)
    {
      int iClass = PeripheralTypeTranslator::HexStringToInt(strClass.c_str());
      if (iClass == USB_CLASS_PER_INTERFACE)
      {
        //TODO just assume this is a HID device for now, since the only devices that we're currently
        //     interested in are HID devices
        iClass = USB_CLASS_HID;
      }

      PeripheralScanResult result(m_type);
      result.m_iVendorId   = PeripheralTypeTranslator::HexStringToInt(udev_device_get_sysattr_value(dev, "idVendor"));
      result.m_iProductId  = PeripheralTypeTranslator::HexStringToInt(udev_device_get_sysattr_value(dev, "idProduct"));
      result.m_type        = GetType(iClass);
      result.m_strLocation = udev_device_get_syspath(dev);
      result.m_iSequence   = GetNumberOfPeripheralsWithId(result.m_iVendorId, result.m_iProductId);
      if (!results.ContainsResult(result))
        results.m_results.push_back(result);
    }

    bContinue = true;
    if (parent)
    {
      /* unref the _parent_ device */
      udev_device_unref(parent);
      parent = NULL;
    }
  }
  /* Free the enumerator object */
  udev_enumerate_unref(enumerate);

  return true;
}

const PeripheralType CPeripheralBusUSB::GetType(int iDeviceClass)
{
  switch (iDeviceClass)
  {
  case USB_CLASS_HID:
    return PERIPHERAL_HID;
  case USB_CLASS_COMM:
    return PERIPHERAL_NIC;
  case USB_CLASS_MASS_STORAGE:
    return PERIPHERAL_DISK;
  case USB_CLASS_PER_INTERFACE:
  case USB_CLASS_AUDIO:
  case USB_CLASS_PRINTER:
  case USB_CLASS_PTP:
  case USB_CLASS_HUB:
  case USB_CLASS_DATA:
  case USB_CLASS_VENDOR_SPEC:
  default:
    return PERIPHERAL_UNKNOWN;
  }
}

void CPeripheralBusUSB::Process(void)
{
  bool bUpdated(false);
  ScanForDevices();
  while (!m_bStop)
  {
    bUpdated = WaitForUpdate();
    if (bUpdated && !m_bStop)
      ScanForDevices();
  }

  m_bIsStarted = false;
}

void CPeripheralBusUSB::Clear(void)
{
  StopThread(false);

  CPeripheralBus::Clear();
}

bool CPeripheralBusUSB::WaitForUpdate()
{
  int udevFd = udev_monitor_get_fd(m_udevMon);

  if (udevFd < 0)
  {
    CLog::Log(LOGERROR, "%s - get udev monitor", __FUNCTION__);
    return false;
  }

  /* poll for udev changes */
  struct pollfd pollFd;
  pollFd.fd = udevFd;
  pollFd.events = POLLIN;
  int iPollResult;
  while (!m_bStop && ((iPollResult = poll(&pollFd, 1, 100)) <= 0))
    if (errno != EINTR && iPollResult != 0)
      break;

  /* the thread is being stopped, so just return false */
  if (m_bStop)
    return false;

  /* we have to read the message from the queue, even though we're not actually using it */
  struct udev_device *dev = udev_monitor_receive_device(m_udevMon);
  if (dev)
    udev_device_unref(dev);
  else
  {
    CLog::Log(LOGERROR, "%s - failed to get device from udev_monitor_receive_device()", __FUNCTION__);
    Clear();
    return false;
  }

  return true;
}

{'repo_name': 'MrMC/mrmc', 'stars': '144', 'repo_language': 'C++', 'file_name': 'project.pbxproj', 'mime_type': 'text/plain', 'hash': -1538965424297957722, 'source_dataset': 'data'}


/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.bookkeeper.tests.containers;

/**
 * An abstract class for metadata store container.
 */
public abstract class MetadataStoreContainer<SelfT extends MetadataStoreContainer<SelfT>>
        extends ChaosContainer<SelfT> {
    protected MetadataStoreContainer(String clusterName, String image) {
        super(clusterName, image);
    }

    public abstract String getExternalServiceUri();

    public abstract String getInternalServiceUri();

}

{'repo_name': 'apache/bookkeeper', 'stars': '982', 'repo_language': 'Java', 'file_name': 'ListLedgerServiceTest.java', 'mime_type': 'text/x-java', 'hash': -2497517175065447327, 'source_dataset': 'data'}


{
  "compilerOptions": {
    /* Basic Options */
    "target": "ES2018" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
    "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
    // "lib": [],                             /* Specify library files to be included in the compilation. */
    // "allowJs": true,                       /* Allow javascript files to be compiled. */
    // "checkJs": true,                       /* Report errors in .js files. */
    // "jsx": "preserve",                     /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
    // "declaration": true,                   /* Generates corresponding '.d.ts' file. */
    // "declarationMap": true,                /* Generates a sourcemap for each corresponding '.d.ts' file. */
    // "sourceMap": true,                     /* Generates corresponding '.map' file. */
    // "outFile": "./",                       /* Concatenate and emit output to single file. */
    "outDir": "./dist" /* Redirect output structure to the directory. */,
    "rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
    // "composite": true,                     /* Enable project compilation */
    // "removeComments": true,                /* Do not emit comments to output. */
    // "noEmit": true,                        /* Do not emit outputs. */
    // "importHelpers": true,                 /* Import emit helpers from 'tslib'. */
    // "downlevelIteration": true,            /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
    // "isolatedModules": true,               /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

    /* Strict Type-Checking Options */
    "strict": true /* Enable all strict type-checking options. */,
    "noImplicitAny": false /* Raise error on expressions and declarations with an implied 'any' type. */,
    // "strictNullChecks": true,              /* Enable strict null checks. */
    // "strictFunctionTypes": true,           /* Enable strict checking of function types. */
    // "strictBindCallApply": true,           /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
    // "strictPropertyInitialization": true,  /* Enable strict checking of property initialization in classes. */
    // "noImplicitThis": true,                /* Raise error on 'this' expressions with an implied 'any' type. */
    // "alwaysStrict": true,                  /* Parse in strict mode and emit "use strict" for each source file. */

    /* Additional Checks */
    // "noUnusedLocals": true,                /* Report errors on unused locals. */
    // "noUnusedParameters": true,            /* Report errors on unused parameters. */
    // "noImplicitReturns": true,             /* Report error when not all code paths in function return a value. */
    // "noFallthroughCasesInSwitch": true,    /* Report errors for fallthrough cases in switch statement. */

    /* Module Resolution Options */
    // "moduleResolution": "node",            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    // "baseUrl": "./",                       /* Base directory to resolve non-absolute module names. */
    // "paths": {},                           /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
    // "rootDirs": [],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
    "typeRoots": [
      "./node_modules/@types"
    ] /* List of folders to include type definitions from. */,
    // "types": [],                           /* Type declaration files to be included in compilation. */
    // "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    // "preserveSymlinks": true,              /* Do not resolve the real path of symlinks. */

    /* Source Map Options */
    // "sourceRoot": "",                      /* Specify the location where debugger should locate TypeScript files instead of source locations. */
    // "mapRoot": "",                         /* Specify the location where debugger should locate map files instead of generated locations. */
    // "inlineSourceMap": true,               /* Emit a single file with source maps instead of having a separate file. */
    // "inlineSources": true,                 /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

    /* Experimental Options */
    // "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
    // "emitDecoratorMetadata": true,         /* Enables experimental support for emitting type metadata for decorators. */
  },
  "exclude": ["node_modules", "typings"]
}

{'repo_name': 'stefanjudis/netlify-menubar', 'stars': '122', 'repo_language': 'TypeScript', 'file_name': 'menus.test.ts.snap', 'mime_type': 'text/plain', 'hash': -2516811797832741946, 'source_dataset': 'data'}


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
	<dict>
		<key>com.apple.security.cs.allow-jit</key>
		<true/>
		<key>com.apple.security.cs.allow-unsigned-executable-memory</key>
		<true/>
		<key>com.apple.security.cs.allow-dyld-environment-variables</key>
		<true/>
	</dict>
</plist>

{'repo_name': 'polkadot-js/apps', 'stars': '264', 'repo_language': 'TypeScript', 'file_name': 'settings.json', 'mime_type': 'text/plain', 'hash': 9043461144196575326, 'source_dataset': 'data'}


package mapstructure

import (
	"errors"
	"reflect"
	"strconv"
	"strings"
	"time"
)

// typedDecodeHook takes a raw DecodeHookFunc (an interface{}) and turns
// it into the proper DecodeHookFunc type, such as DecodeHookFuncType.
func typedDecodeHook(h DecodeHookFunc) DecodeHookFunc {
	// Create variables here so we can reference them with the reflect pkg
	var f1 DecodeHookFuncType
	var f2 DecodeHookFuncKind

	// Fill in the variables into this interface and the rest is done
	// automatically using the reflect package.
	potential := []interface{}{f1, f2}

	v := reflect.ValueOf(h)
	vt := v.Type()
	for _, raw := range potential {
		pt := reflect.ValueOf(raw).Type()
		if vt.ConvertibleTo(pt) {
			return v.Convert(pt).Interface()
		}
	}

	return nil
}

// DecodeHookExec executes the given decode hook. This should be used
// since it'll naturally degrade to the older backwards compatible DecodeHookFunc
// that took reflect.Kind instead of reflect.Type.
func DecodeHookExec(
	raw DecodeHookFunc,
	from reflect.Type, to reflect.Type,
	data interface{}) (interface{}, error) {
	// Build our arguments that reflect expects
	argVals := make([]reflect.Value, 3)
	argVals[0] = reflect.ValueOf(from)
	argVals[1] = reflect.ValueOf(to)
	argVals[2] = reflect.ValueOf(data)

	switch f := typedDecodeHook(raw).(type) {
	case DecodeHookFuncType:
		return f(from, to, data)
	case DecodeHookFuncKind:
		return f(from.Kind(), to.Kind(), data)
	default:
		return nil, errors.New("invalid decode hook signature")
	}
}

// ComposeDecodeHookFunc creates a single DecodeHookFunc that
// automatically composes multiple DecodeHookFuncs.
//
// The composed funcs are called in order, with the result of the
// previous transformation.
func ComposeDecodeHookFunc(fs ...DecodeHookFunc) DecodeHookFunc {
	return func(
		f reflect.Type,
		t reflect.Type,
		data interface{}) (interface{}, error) {
		var err error
		for _, f1 := range fs {
			data, err = DecodeHookExec(f1, f, t, data)
			if err != nil {
				return nil, err
			}

			// Modify the from kind to be correct with the new data
			f = nil
			if val := reflect.ValueOf(data); val.IsValid() {
				f = val.Type()
			}
		}

		return data, nil
	}
}

// StringToSliceHookFunc returns a DecodeHookFunc that converts
// string to []string by splitting on the given sep.
func StringToSliceHookFunc(sep string) DecodeHookFunc {
	return func(
		f reflect.Kind,
		t reflect.Kind,
		data interface{}) (interface{}, error) {
		if f != reflect.String || t != reflect.Slice {
			return data, nil
		}

		raw := data.(string)
		if raw == "" {
			return []string{}, nil
		}

		return strings.Split(raw, sep), nil
	}
}

// StringToTimeDurationHookFunc returns a DecodeHookFunc that converts
// strings to time.Duration.
func StringToTimeDurationHookFunc() DecodeHookFunc {
	return func(
		f reflect.Type,
		t reflect.Type,
		data interface{}) (interface{}, error) {
		if f.Kind() != reflect.String {
			return data, nil
		}
		if t != reflect.TypeOf(time.Duration(5)) {
			return data, nil
		}

		// Convert it by parsing
		return time.ParseDuration(data.(string))
	}
}

func WeaklyTypedHook(
	f reflect.Kind,
	t reflect.Kind,
	data interface{}) (interface{}, error) {
	dataVal := reflect.ValueOf(data)
	switch t {
	case reflect.String:
		switch f {
		case reflect.Bool:
			if dataVal.Bool() {
				return "1", nil
			} else {
				return "0", nil
			}
		case reflect.Float32:
			return strconv.FormatFloat(dataVal.Float(), 'f', -1, 64), nil
		case reflect.Int:
			return strconv.FormatInt(dataVal.Int(), 10), nil
		case reflect.Slice:
			dataType := dataVal.Type()
			elemKind := dataType.Elem().Kind()
			if elemKind == reflect.Uint8 {
				return string(dataVal.Interface().([]uint8)), nil
			}
		case reflect.Uint:
			return strconv.FormatUint(dataVal.Uint(), 10), nil
		}
	}

	return data, nil
}

{'repo_name': 'mkokho/kubemrr', 'stars': '125', 'repo_language': 'Go', 'file_name': 'key.pem', 'mime_type': 'text/plain', 'hash': -9095042888796949611, 'source_dataset': 'data'}


// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto2";

package sapi;

message ProtoArg {
  // Fully qualified name of the protobuf that is being passed.
  required string full_name = 1;
  // The serialized protobuf data.
  required bytes protobuf_data = 2;
}

{'repo_name': 'google/sandboxed-api', 'stars': '1162', 'repo_language': 'C++', 'file_name': 'presubmit.yml', 'mime_type': 'text/plain', 'hash': 2902723837174245425, 'source_dataset': 'data'}


.static {
  width: 100%;
  height: 100%;
  background-color: #bbbbbb;
  color: #7f7f7f;
  font-size: 50px;
}

.static span {
  top: 50%;
  transform: translate(-50%, -50%);
  -webkit-transform: translate(-50%, -50%);
  left: 50%;
  display: table;
}

.sk-cube-grid {
  width: 100%;
  height: 100%;
}

.sk-cube-grid .sk-cube {
  width: calc(100% / 3);
  height: calc(100% / 3);
  background-color: #bbbbbb;
  float: left;
}

.sk-cube-grid.animate .sk-cube {
  -webkit-animation: sk-cubeGridScaleDelay 4.6s infinite ease-in-out;
  animation: sk-cubeGridScaleDelay 4.6s infinite ease-in-out;
}

.sk-cube-grid.animate .sk-cube1 {
  -webkit-animation-delay: 0.4s;
  animation-delay: 0.4s;
}

.sk-cube-grid.animate .sk-cube2 {
  -webkit-animation-delay: 0.6s;
  animation-delay: 0.6s;
}

.sk-cube-grid.animate .sk-cube3 {
  -webkit-animation-delay: 0.8s;
  animation-delay: 0.8s;
}

.sk-cube-grid.animate .sk-cube4 {
  -webkit-animation-delay: 0.2s;
  animation-delay: 0.2s;
}

.sk-cube-grid.animate .sk-cube5 {
  -webkit-animation-delay: 0.4s;
  animation-delay: 0.4s;
}

.sk-cube-grid.animate .sk-cube6 {
  -webkit-animation-delay: 0.6s;
  animation-delay: 0.6s;
}

.sk-cube-grid.animate .sk-cube7 {
  -webkit-animation-delay: 0s;
  animation-delay: 0s;
}

.sk-cube-grid.animate .sk-cube8 {
  -webkit-animation-delay: 0.2s;
  animation-delay: 0.2s;
}

.sk-cube-grid.animate .sk-cube9 {
  -webkit-animation-delay: 0.4s;
  animation-delay: 0.4s;
}

@-webkit-keyframes sk-cubeGridScaleDelay {
  0%, 70%, 100% {
    -webkit-transform: scale3D(1, 1, 1);
    transform: scale3D(1, 1, 1);
  }
  35% {
    -webkit-transform: scale3D(0, 0, 1);
    transform: scale3D(0, 0, 1);
  }
}

@keyframes sk-cubeGridScaleDelay {
  0%, 70%, 100% {
    -webkit-transform: scale3D(1, 1, 1);
    transform: scale3D(1, 1, 1);
  }
  35% {
    -webkit-transform: scale3D(0, 0, 1);
    transform: scale3D(0, 0, 1);
  }
}

{'repo_name': 'bpatrik/pigallery2', 'stars': '283', 'repo_language': 'TypeScript', 'file_name': 'Utils.spec.ts', 'mime_type': 'text/x-java', 'hash': -2891526976962055665, 'source_dataset': 'data'}