<commit_before># ======================================================================
# matscipy - Python materials science tools
# https://github.com/libAtoms/matscipy
#
# Copyright (2014) James Kermode, King's College London
#                  Lars Pastewka, Karlsruhe Institute of Technology
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# 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 this program.  If not, see <http://www.gnu.org/licenses/>.
# ======================================================================

import re

import numpy as np

###

def savetbl(fn, **kwargs):
    """
    Save tabulated data and write column header strings.

    Example:
        savetbl('file.dat', time=time, strain=strain, energy=energy)

    Parameters
    ----------
    fn : str
        Name of file to write to.
    kwargs : dict
        Keyword argument pass data and column header names.
    """
    header = reduce(lambda s, (i, x): '{0} {1}:{2}'.format(s, i+1, x),
                    enumerate(kwargs.iterkeys()), '')
    data = np.transpose([x for x in kwargs.itervalues()])
    np.savetxt(fn, data, header=header)

def loadtbl(fn, usecols=None):
    """
    Load tabulated data from column header strings.

    Example data file:
        # time strain energy
        1.0 0.01 283
        2.0 0.02 398
        ...

        strain, energy = loadtbl('file.dat', usecols=['strain', 'energy'])

    Parameters
    ----------
    fn : str
        Name of file to load.
    usecols : list of strings
        List of column names.

    Returns
    -------
    data : tuple of arrays
        Return tuple of array with data for each colume in usecols if
        usecols specified. For usecols=None, return dictionary with header
        keys and arrays as data entries.
    """
    f = open(fn)
    line = f.readline()
    while line.startswith('#'):
        column_labels = [ s.strip() for s in re.split('[\s,]+', line)[1:] ]
        line = f.readline()
    f.close()

    sep_i = [ x.find(':') for x in column_labels ]
    column_labels = map(lambda s,i: s[i+1:] if i >= 0 else s, column_labels,
                        sep_i)
    if usecols is None:
        data = np.loadtxt(fn, unpack=True)
        return { s: d for s, d in zip(column_labels, data) }
    else:
        column_i = [ column_labels.index(s) for s in usecols ]
        return np.loadtxt(fn, usecols=column_i, unpack=True)
<commit_msg>Sort by name in savetbl.<commit_after># ======================================================================
# matscipy - Python materials science tools
# https://github.com/libAtoms/matscipy
#
# Copyright (2014) James Kermode, King's College London
#                  Lars Pastewka, Karlsruhe Institute of Technology
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# 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 this program.  If not, see <http://www.gnu.org/licenses/>.
# ======================================================================

import re

import numpy as np

###

def savetbl(fn, **kwargs):
    """
    Save tabulated data and write column header strings.

    Example:
        savetbl('file.dat', time=time, strain=strain, energy=energy)

    Parameters
    ----------
    fn : str
        Name of file to write to.
    kwargs : dict
        Keyword argument pass data and column header names.
    """
    sorted_kwargs = sorted(kwargs.items(), key=lambda t: t[0])
    header = reduce(lambda s, (i, (x, y)): '{0} {1}:{2}'.format(s, i+1, x),
                    enumerate(sorted_kwargs), '')
    data = np.transpose([y for x, y in sorted_kwargs])
    np.savetxt(fn, data, header=header)

def loadtbl(fn, usecols=None):
    """
    Load tabulated data from column header strings.

    Example data file:
        # time strain energy
        1.0 0.01 283
        2.0 0.02 398
        ...

        strain, energy = loadtbl('file.dat', usecols=['strain', 'energy'])

    Parameters
    ----------
    fn : str
        Name of file to load.
    usecols : list of strings
        List of column names.

    Returns
    -------
    data : tuple of arrays
        Return tuple of array with data for each colume in usecols if
        usecols specified. For usecols=None, return dictionary with header
        keys and arrays as data entries.
    """
    f = open(fn)
    line = f.readline()
    while line.startswith('#'):
        column_labels = [ s.strip() for s in re.split('[\s,]+', line)[1:] ]
        line = f.readline()
    f.close()

    sep_i = [ x.find(':') for x in column_labels ]
    column_labels = map(lambda s,i: s[i+1:] if i >= 0 else s, column_labels,
                        sep_i)
    if usecols is None:
        data = np.loadtxt(fn, unpack=True)
        return { s: d for s, d in zip(column_labels, data) }
    else:
        column_i = [ column_labels.index(s) for s in usecols ]
        return np.loadtxt(fn, usecols=column_i, unpack=True)
<|endoftext|>


<commit_before># Copyright (c) 2016 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.

from UM.Application import Application
from UM.Settings.Models.InstanceContainersModel import InstanceContainersModel
from UM.Settings.ContainerRegistry import ContainerRegistry

from cura.Settings.ExtruderManager import ExtruderManager

##  QML Model for listing the current list of valid quality profiles.
#
class ProfilesModel(InstanceContainersModel):
    def __init__(self, parent = None):
        super().__init__(parent)

    ##  Fetch the list of containers to display.
    #
    #   See UM.Settings.Models.InstanceContainersModel._fetchInstanceContainers().
    def _fetchInstanceContainers(self):
        global_container_stack = Application.getInstance().getGlobalContainerStack()
        global_machine_definition = global_container_stack.getBottom()

        extruder_stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
        if extruder_stacks:
            # Multi-extruder machine detected.

            # Determine the common set of quality types which can be
            # applied to all of the materials for this machine.
            quality_type_dict = self.__fetchQualityTypeDictForStack(global_container_stack, global_machine_definition)
            common_quality_types = set(quality_type_dict.keys())
            for stack in extruder_stacks[1:]:
                next_quality_type_dict = self.__fetchQualityTypeDictForStack(stack, global_machine_definition)
                common_quality_types.intersection_update(set(next_quality_type_dict.keys()))

            return [quality_type_dict[quality_type] for quality_type in common_quality_types]

        else:
            # Machine with one extruder.
            quality_type_dict = self.__fetchQualityTypeDictForStack(global_container_stack, global_machine_definition)
            return list(quality_type_dict.values())
        return []

    def __fetchQualityTypeDictForStack(self, stack, global_machine_definition):
        criteria = {"type": "quality" }
        if global_machine_definition.getMetaDataEntry("has_machine_quality", False):
            criteria["definition"] = global_machine_definition.getId()
            if global_machine_definition.getMetaDataEntry("has_materials", False):
                material = stack.findContainer(type="material")
                criteria["material"] = material.getId()
        else:
            criteria["definition"] = "fdmprinter"

        qualities = ContainerRegistry.getInstance().findInstanceContainers(**criteria)

        quality_type_dict = {}
        for quality in qualities:
            quality_type_dict[quality.getMetaDataEntry("quality_type")] = quality
        return quality_type_dict
<commit_msg>Handle the startup situation where there is no global stack. Fixed a bug where the wrong stack was used instead of the first extruder.<commit_after># Copyright (c) 2016 Ultimaker B.V.
# Uranium is released under the terms of the AGPLv3 or higher.

from UM.Application import Application
from UM.Settings.Models.InstanceContainersModel import InstanceContainersModel
from UM.Settings.ContainerRegistry import ContainerRegistry

from cura.Settings.ExtruderManager import ExtruderManager

##  QML Model for listing the current list of valid quality profiles.
#
class ProfilesModel(InstanceContainersModel):
    def __init__(self, parent = None):
        super().__init__(parent)

    ##  Fetch the list of containers to display.
    #
    #   See UM.Settings.Models.InstanceContainersModel._fetchInstanceContainers().
    def _fetchInstanceContainers(self):
        global_container_stack = Application.getInstance().getGlobalContainerStack()
        if global_container_stack is None:
            return []

        global_machine_definition = global_container_stack.getBottom()

        extruder_stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
        if extruder_stacks:
            # Multi-extruder machine detected.

            # Determine the common set of quality types which can be
            # applied to all of the materials for this machine.
            quality_type_dict = self.__fetchQualityTypeDictForStack(extruder_stacks[0], global_machine_definition)
            common_quality_types = set(quality_type_dict.keys())
            for stack in extruder_stacks[1:]:
                next_quality_type_dict = self.__fetchQualityTypeDictForStack(stack, global_machine_definition)
                common_quality_types.intersection_update(set(next_quality_type_dict.keys()))

            return [quality_type_dict[quality_type] for quality_type in common_quality_types]

        else:
            # Machine with one extruder.
            quality_type_dict = self.__fetchQualityTypeDictForStack(global_container_stack, global_machine_definition)
            return list(quality_type_dict.values())
        return []

    def __fetchQualityTypeDictForStack(self, stack, global_machine_definition):
        criteria = {"type": "quality" }
        if global_machine_definition.getMetaDataEntry("has_machine_quality", False):
            criteria["definition"] = global_machine_definition.getId()
            if global_machine_definition.getMetaDataEntry("has_materials", False):
                material = stack.findContainer(type="material")
                criteria["material"] = material.getId()
        else:
            criteria["definition"] = "fdmprinter"

        qualities = ContainerRegistry.getInstance().findInstanceContainers(**criteria)

        quality_type_dict = {}
        for quality in qualities:
            quality_type_dict[quality.getMetaDataEntry("quality_type")] = quality
        return quality_type_dict
<|endoftext|>


<commit_before>from collections import namedtuple

from sqlagg.filters import ORFilter, EQFilter

from dimagi.utils.decorators.memoized import memoized

from corehq.apps.es import filters
from corehq.apps.locations.models import SQLLocation
from corehq.apps.reports_core.filters import DynamicChoiceListFilter
from corehq.apps.userreports.reports.filters.choice_providers import LocationChoiceProvider
from corehq.apps.userreports.reports.filters.specs import FilterSpec
from corehq.apps.userreports.reports.filters.values import FilterValue, dynamic_choice_list_url, SHOW_ALL_CHOICE

_LocationFilter = namedtuple("_LocationFilter", "column parameter_slug filter_value")

class ENikshayLocationHierarchyFilterValue(FilterValue):

    @memoized
    def get_hierarchy(self, location_id):
        """
        Return a list of _LocationFilter(column, parameter_slug, filter_value) objects to be used in constructing
        the filter.
        """
        relevant_location_types = ["sto", "cto", "dto", "tu", "phi"]
        location = SQLLocation.objects.get(location_id=location_id)

        if location.location_type.name not in relevant_location_types:
            # This report could be filtered by a user who is not assigned to one of the pertinent location types.
            # In that case, the report should show nothing, so return a filter that is guaranteed not to match
            # any records.
            return [_LocationFilter("phi", "{}_phi".format(self.filter.slug), location_id)]

        filters = []
        for ancestor in location.get_ancestors(include_self=True):
            if ancestor.location_type.name in relevant_location_types:
                column = ancestor.location_type.name
                parameter = "{slug}_{column}".format(slug=self.filter.slug, column=column)
                value = ancestor.location_id
                filters.append(
                    _LocationFilter(column, parameter, value)
                )

        below_column = "below_{}".format(location.location_type.name)
        below_parameter = "{slug}_{column}".format(slug=self.filter.slug, column=below_column)
        filters.append(
            _LocationFilter(below_column, below_parameter, location.location_id)
        )
        return filters

    @property
    def show_all(self):
        return SHOW_ALL_CHOICE in [choice.value for choice in self.value]

    def to_sql_filter(self):
        if self.show_all:
            return None
        location_id = self.value[0].value
        hierarchy = self.get_hierarchy(location_id)
        if len(hierarchy) == 1:
            f = self.get_hierarchy(location_id)[0]
            return EQFilter(f.column, f.parameter_slug)
        else:
            return ORFilter([
                EQFilter(x.column, x.parameter_slug) for x in self.get_hierarchy(location_id)
            ])

    def to_sql_values(self):
        if self.show_all:
            return {}
        location_id = self.value[0].value
        return {
            x.parameter_slug: x.filter_value for x in self.get_hierarchy(location_id)
        }

    def to_es_filter(self):
        if self.show_all:
            return None
        location_id = self.value[0].value
        fs = [
            filters.term(x.column, x.filter_value) for x in self.get_hierarchy(location_id)
        ]
        return filters.OR(fs)

class EnikshayLocationHiearachyFilter(DynamicChoiceListFilter):
    """
    A custom ucr report filter for the enikshay Referral Report.
    This filter will use the selected filter to filter against multiple fields.
    https://docs.google.com/document/d/1ejESqYoqSff0u4X72Fq0TYi7ll3IvH5KX3VWXzLyMQs/edit

    Each referral in the data source has fields like:
        cto
        dto
        sto
        ...
        below_cto
        below_dto
        below_sto
        ...
    corresponding to the location hieararchy levels.
    If a person was referred to a location below a given level, then the below_<location_level> column will
    contain the id of the location at <location_level>. The <location_level> column corresponding to the location
    that the person was referred to will contain the location id, and the other location level columns will be
    blank.
    """

    def __init__(self, name, label, choice_provider, url_generator, css_id=None):

        super(EnikshayLocationHiearachyFilter, self).__init__(
            name=name,
            field=None,
            datatype="string",
            label=label,
            show_all=None,
            choice_provider=choice_provider,
            url_generator=url_generator,
            css_id=css_id,
        )

def _build_enikshay_location_hierarchy(spec, report):
    wrapped = FilterSpec.wrap(spec)
    choice_provider = LocationChoiceProvider(report, wrapped.slug)
    choice_provider.configure({})
    return EnikshayLocationHiearachyFilter(
        name=wrapped.slug,
        label=wrapped.get_display(),
        choice_provider=choice_provider,
        url_generator=dynamic_choice_list_url,
    )
<commit_msg>check location type code, not name<commit_after>from collections import namedtuple

from sqlagg.filters import ORFilter, EQFilter

from dimagi.utils.decorators.memoized import memoized

from corehq.apps.es import filters
from corehq.apps.locations.models import SQLLocation
from corehq.apps.reports_core.filters import DynamicChoiceListFilter
from corehq.apps.userreports.reports.filters.choice_providers import LocationChoiceProvider
from corehq.apps.userreports.reports.filters.specs import FilterSpec
from corehq.apps.userreports.reports.filters.values import FilterValue, dynamic_choice_list_url, SHOW_ALL_CHOICE

_LocationFilter = namedtuple("_LocationFilter", "column parameter_slug filter_value")

class ENikshayLocationHierarchyFilterValue(FilterValue):

    @memoized
    def get_hierarchy(self, location_id):
        """
        Return a list of _LocationFilter(column, parameter_slug, filter_value) objects to be used in constructing
        the filter.
        """
        relevant_location_types = ["sto", "cto", "dto", "tu", "phi"]
        location = SQLLocation.objects.get(location_id=location_id)

        if location.location_type.code not in relevant_location_types:
            # This report could be filtered by a user who is not assigned to one of the pertinent location types.
            # In that case, the report should show nothing, so return a filter that is guaranteed not to match
            # any records.
            return [_LocationFilter("phi", "{}_phi".format(self.filter.slug), location_id)]

        filters = []
        for ancestor in location.get_ancestors(include_self=True):
            if ancestor.location_type.code in relevant_location_types:
                column = ancestor.location_type.code
                parameter = "{slug}_{column}".format(slug=self.filter.slug, column=column)
                value = ancestor.location_id
                filters.append(
                    _LocationFilter(column, parameter, value)
                )

        below_column = "below_{}".format(location.location_type.code)
        below_parameter = "{slug}_{column}".format(slug=self.filter.slug, column=below_column)
        filters.append(
            _LocationFilter(below_column, below_parameter, location.location_id)
        )
        return filters

    @property
    def show_all(self):
        return SHOW_ALL_CHOICE in [choice.value for choice in self.value]

    def to_sql_filter(self):
        if self.show_all:
            return None
        location_id = self.value[0].value
        hierarchy = self.get_hierarchy(location_id)
        if len(hierarchy) == 1:
            f = self.get_hierarchy(location_id)[0]
            return EQFilter(f.column, f.parameter_slug)
        else:
            return ORFilter([
                EQFilter(x.column, x.parameter_slug) for x in self.get_hierarchy(location_id)
            ])

    def to_sql_values(self):
        if self.show_all:
            return {}
        location_id = self.value[0].value
        return {
            x.parameter_slug: x.filter_value for x in self.get_hierarchy(location_id)
        }

    def to_es_filter(self):
        if self.show_all:
            return None
        location_id = self.value[0].value
        fs = [
            filters.term(x.column, x.filter_value) for x in self.get_hierarchy(location_id)
        ]
        return filters.OR(fs)

class EnikshayLocationHiearachyFilter(DynamicChoiceListFilter):
    """
    A custom ucr report filter for the enikshay Referral Report.
    This filter will use the selected filter to filter against multiple fields.
    https://docs.google.com/document/d/1ejESqYoqSff0u4X72Fq0TYi7ll3IvH5KX3VWXzLyMQs/edit

    Each referral in the data source has fields like:
        cto
        dto
        sto
        ...
        below_cto
        below_dto
        below_sto
        ...
    corresponding to the location hieararchy levels.
    If a person was referred to a location below a given level, then the below_<location_level> column will
    contain the id of the location at <location_level>. The <location_level> column corresponding to the location
    that the person was referred to will contain the location id, and the other location level columns will be
    blank.
    """

    def __init__(self, name, label, choice_provider, url_generator, css_id=None):

        super(EnikshayLocationHiearachyFilter, self).__init__(
            name=name,
            field=None,
            datatype="string",
            label=label,
            show_all=None,
            choice_provider=choice_provider,
            url_generator=url_generator,
            css_id=css_id,
        )

def _build_enikshay_location_hierarchy(spec, report):
    wrapped = FilterSpec.wrap(spec)
    choice_provider = LocationChoiceProvider(report, wrapped.slug)
    choice_provider.configure({})
    return EnikshayLocationHiearachyFilter(
        name=wrapped.slug,
        label=wrapped.get_display(),
        choice_provider=choice_provider,
        url_generator=dynamic_choice_list_url,
    )
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

# Copyright (c) 2015 Ericsson AB
#
# 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.

import sys
import traceback
import time
import Queue
import os
import OpenSSL.crypto

from twisted.internet import reactor, defer, threads

from calvin.runtime.south.plugins.storage.twistedimpl.securedht.append_server import AppendServer
from calvin.runtime.south.plugins.storage.twistedimpl.securedht.service_discovery_ssdp import SSDPServiceDiscovery
# FIXME refactor to calvin.runtime.north.certificate
from calvin.runtime.south.plugins.storage.twistedimpl.securedht import certificate
from calvin.runtime.north.plugins.storage.storage_base import StorageBase
from calvin.utilities import calvinlogger
from calvin.utilities import calvinconfig

_conf = calvinconfig.get()
_log = calvinlogger.get_logger(__name__)

def logger(message):
    _log.debug(message)
    #print message

class ServerApp(object):

    def __init__(self, server_type, identifier):
        self.kserver = None
        self.port = None
        self.server_type = server_type
        self.id = identifier

    def start(self, port=0, iface='', bootstrap=None):
        if bootstrap is None:
            bootstrap = []

        self.kserver = self.server_type(id=self.id)
        self.kserver.bootstrap(bootstrap)

        self.port = reactor.listenUDP(port,
                                        self.kserver.protocol,
                                        interface=iface)

        return self.port.getHost().host, self.port.getHost().port

    def __getattr__(self, name):
        if hasattr(self.kserver, name) and callable(getattr(self.kserver, name)):
            return getattr(self.kserver, name)
        else:
            # Default behaviour
            raise AttributeError

    def get_port(self):
        return self.port

    def stop(self):
        if self.port:
            return self.port.stopListening()

class ThreadWrapper(object):
    def __init__(self, obj, *args, **kwargs):
        self._obj = threads.blockingCallFromThread(reactor, obj, *args, **kwargs)

    def _call(self, func, *args, **kwargs):
        return threads.blockingCallFromThread(reactor, func, *args, **kwargs)

    def __getattr__(self, name):
        class Caller(object):
            def __init__(self, f, func):
                self.f = f
                self.func = func

            def __call__(self, *args, **kwargs):
                # _log.debug("Calling %s(%s, %s, %s)" %(self.f, self.func, args,  kwargs))
                return self.func(*args, **kwargs)

        if hasattr(self._obj, name):
            if callable(getattr(self._obj, name)):
                return Caller(self._call, getattr(self._obj, name))
            else:
                return getattr(self._obj, name)

        else:
            # Default behaviour
            raise AttributeError

class TwistedWaitObject(object):
    def __init__(self, func, **kwargs):
        self._value = None
        self._q = Queue.Queue()
        self._done = False
        self._func = func
        self._kwargs = kwargs
        self._callback_class = kwargs.pop("cb")
        d = func(**kwargs)
        d.addCallback(self._callback)

    def _callback(self, value):
        self._value = value
        if self._callback_class:
            self._callback_class(self._kwargs['key'], value)
            # reactor.callFromThread(self._callback_class, self._kwargs['key'], value)
        self._q.put(self._value)
        self._done = True

    def done(self):
        return self._done

    def wait(self, timeout=5):
        if self.done():
            return self._value
        try:
            value = self._q.get(timeout=timeout)
        except Queue.Empty:
            logger("Timeout in %s(%s)" % (self._func, self._kwargs))
            raise
        return value

    def get(self):
        return self._value

class AutoDHTServer(StorageBase):
    def __init__(self):
        super(AutoDHTServer, self).__init__()
        self.dht_server = None
        self._ssdps = None
        self._started = False
        self.cert_conf = certificate.Config(_conf.get("security", "certificate_conf"),
                                            _conf.get("security", "certificate_domain")).configuration

    def start(self, iface='', network=None, bootstrap=None, cb=None, name=None):
        if bootstrap is None:
            bootstrap = []
        name_dir = os.path.join(self.cert_conf["CA_default"]["runtimes_dir"], name)
        filename = os.listdir(os.path.join(name_dir, "mine"))
        st_cert = open(os.path.join(name_dir, "mine", filename[0]), 'rt').read()
        cert_part = st_cert.split(certificate.BEGIN_LINE)
        certstr = "{}{}".format(certificate.BEGIN_LINE, cert_part[1])

        try:
            cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
                                                  certstr)
        except:
            raise
        key = cert.digest("sha256")
        newkey = key.replace(":", "")
        bytekey = newkey.decode("hex")

        if network is None:
            network = _conf.get_in_order("dht_network_filter", "ALL")

        self.dht_server = ServerApp(AppendServer, bytekey[-20:])
        ip, port = self.dht_server.start(iface=iface)

        dlist = []
        dlist.append(self.dht_server.bootstrap(bootstrap))

        self._ssdps = SSDPServiceDiscovery(iface, cert=certstr)
        dlist += self._ssdps.start()

        logger("Register service %s %s:%s" % (network, ip, port))
        self._ssdps.register_service(network, ip, port)

        logger("Set client filter %s" % (network))
        self._ssdps.set_client_filter(network)

        start_cb = defer.Deferred()

        def bootstrap_proxy(addrs):
            def started(args):
                logger("DHT Started %s" % (args))
                if not self._started:
                    reactor.callLater(.2, start_cb.callback, True)
                if cb:
                    reactor.callLater(.2, cb, True)
                self._started = True

            def failed(args):
                logger("DHT failed to bootstrap %s" % (args))
                #reactor.callLater(.5, bootstrap_proxy, addrs)

            logger("Trying to bootstrap with %s" % (repr(addrs)))
            d = self.dht_server.bootstrap(addrs)
            d.addCallback(started)
            d.addErrback(failed)

        def start_msearch(args):
            logger("** msearch %s args: %s" % (self, repr(args)))
            reactor.callLater(0,
                                self._ssdps.start_search,
                                bootstrap_proxy,
                                stop=False)

        # Wait until servers all listen
        dl = defer.DeferredList(dlist)
        dl.addBoth(start_msearch)
        #FIXME handle inside IDServerApp
        self.dht_server.kserver.protocol.sourceNode.port = port
        self.dht_server.kserver.protocol.sourceNode.ip = "0.0.0.0"
        self.dht_server.kserver.name = name
        self.dht_server.kserver.protocol.name = name
        self.dht_server.kserver.protocol.storeOwnCert(certstr)
        self.dht_server.kserver.protocol.setPrivateKey()

        return start_cb

    def set(self, key, value, cb=None):
        return TwistedWaitObject(self.dht_server.set, key=key, value=value, cb=cb)

    def get(self, key, cb=None):
        return TwistedWaitObject(self.dht_server.get, key=key, cb=cb)

    def get_concat(self, key, cb=None):
        return TwistedWaitObject(self.dht_server.get_concat, key=key, cb=cb)

    def append(self, key, value, cb=None):
        return TwistedWaitObject(self.dht_server.append, key=key, value=value, cb=cb)

    def remove(self, key, value, cb=None):
        return TwistedWaitObject(self.dht_server.remove, key=key, value=value, cb=cb)

    def bootstrap(self, addrs, cb=None):
        return TwistedWaitObject(self.dht_server.bootstrap, addr=addrs, cb=cb)

    def stop_search(self):
        return self._ssdps.stop_search()

    def stop(self, cb=None):
        d1 = self.dht_server.stop()
        d2 = self._ssdps.stop()

        dl = defer.DeferredList((d1, d2))
        if cb:
            dl.addBoth(cb)

        return dl

def main(iface):
    ret = 0
    try:
        a = AutoDHTServer()
        a.start(iface)

        b = AutoDHTServer()
        b.start(iface)

        time.sleep(4)

        print a.set(key="APA", value="banan")

        print a.get(key="APA")
        print b.get(key="APA")

        a.stop()
        b.stop()

    except:
        traceback.print_exc()
        ret = 1

    finally:
        if reactor.running:
            threads.blockingCallFromThread(reactor, reactor.stop)

    return ret

if __name__ == "__main__":
    print sys.argv
    if len(sys.argv) != 2:
        print "Usage: %s <server|client> <IP of interface>" % (sys.argv[0], )
        # sys.exit(1)
        interface = ''
    else:
        interface = sys.argv[1]
    sys.exit(main(interface))
<commit_msg>Clarifying comments<commit_after># -*- coding: utf-8 -*-

# Copyright (c) 2015 Ericsson AB
#
# 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.

import sys
import traceback
import time
import Queue
import os
import OpenSSL.crypto

from twisted.internet import reactor, defer, threads

from calvin.runtime.south.plugins.storage.twistedimpl.securedht.append_server import AppendServer
from calvin.runtime.south.plugins.storage.twistedimpl.securedht.service_discovery_ssdp import SSDPServiceDiscovery
# FIXME refactor to calvin.runtime.north.certificate
from calvin.runtime.south.plugins.storage.twistedimpl.securedht import certificate
from calvin.runtime.north.plugins.storage.storage_base import StorageBase
from calvin.utilities import calvinlogger
from calvin.utilities import calvinconfig

_conf = calvinconfig.get()
_log = calvinlogger.get_logger(__name__)

def logger(message):
    _log.debug(message)
    #print message

class ServerApp(object):

    def __init__(self, server_type, identifier):
        self.kserver = None
        self.port = None
        self.server_type = server_type
        self.id = identifier

    def start(self, port=0, iface='', bootstrap=None):
        if bootstrap is None:
            bootstrap = []

        self.kserver = self.server_type(id=self.id)
        self.kserver.bootstrap(bootstrap)

        self.port = reactor.listenUDP(port,
                                        self.kserver.protocol,
                                        interface=iface)

        return self.port.getHost().host, self.port.getHost().port

    def __getattr__(self, name):
        if hasattr(self.kserver, name) and callable(getattr(self.kserver, name)):
            return getattr(self.kserver, name)
        else:
            # Default behaviour
            raise AttributeError

    def get_port(self):
        return self.port

    def stop(self):
        if self.port:
            return self.port.stopListening()

class ThreadWrapper(object):
    def __init__(self, obj, *args, **kwargs):
        self._obj = threads.blockingCallFromThread(reactor, obj, *args, **kwargs)

    def _call(self, func, *args, **kwargs):
        return threads.blockingCallFromThread(reactor, func, *args, **kwargs)

    def __getattr__(self, name):
        class Caller(object):
            def __init__(self, f, func):
                self.f = f
                self.func = func

            def __call__(self, *args, **kwargs):
                # _log.debug("Calling %s(%s, %s, %s)" %(self.f, self.func, args,  kwargs))
                return self.func(*args, **kwargs)

        if hasattr(self._obj, name):
            if callable(getattr(self._obj, name)):
                return Caller(self._call, getattr(self._obj, name))
            else:
                return getattr(self._obj, name)

        else:
            # Default behaviour
            raise AttributeError

class TwistedWaitObject(object):
    def __init__(self, func, **kwargs):
        self._value = None
        self._q = Queue.Queue()
        self._done = False
        self._func = func
        self._kwargs = kwargs
        self._callback_class = kwargs.pop("cb")
        d = func(**kwargs)
        d.addCallback(self._callback)

    def _callback(self, value):
        self._value = value
        if self._callback_class:
            self._callback_class(self._kwargs['key'], value)
            # reactor.callFromThread(self._callback_class, self._kwargs['key'], value)
        self._q.put(self._value)
        self._done = True

    def done(self):
        return self._done

    def wait(self, timeout=5):
        if self.done():
            return self._value
        try:
            value = self._q.get(timeout=timeout)
        except Queue.Empty:
            logger("Timeout in %s(%s)" % (self._func, self._kwargs))
            raise
        return value

    def get(self):
        return self._value

class AutoDHTServer(StorageBase):
    def __init__(self):
        super(AutoDHTServer, self).__init__()
        self.dht_server = None
        self._ssdps = None
        self._started = False
        self.cert_conf = certificate.Config(_conf.get("security", "certificate_conf"),
                                            _conf.get("security", "certificate_domain")).configuration

    def start(self, iface='', network=None, bootstrap=None, cb=None, name=None):
        if bootstrap is None:
            bootstrap = []
        name_dir = os.path.join(self.cert_conf["CA_default"]["runtimes_dir"], name)
        filename = os.listdir(os.path.join(name_dir, "mine"))
        st_cert = open(os.path.join(name_dir, "mine", filename[0]), 'rt').read()
        cert_part = st_cert.split(certificate.BEGIN_LINE)
        certstr = "{}{}".format(certificate.BEGIN_LINE, cert_part[1])

        try:
            cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
                                                  certstr)
        except:
            raise
        # Derive DHT node id
        key = cert.digest("sha256")
        newkey = key.replace(":", "")
        bytekey = newkey.decode("hex")

        if network is None:
            network = _conf.get_in_order("dht_network_filter", "ALL")

        self.dht_server = ServerApp(AppendServer, bytekey[-20:])
        ip, port = self.dht_server.start(iface=iface)

        dlist = []
        dlist.append(self.dht_server.bootstrap(bootstrap))

        self._ssdps = SSDPServiceDiscovery(iface, cert=certstr)
        dlist += self._ssdps.start()

        logger("Register service %s %s:%s" % (network, ip, port))
        self._ssdps.register_service(network, ip, port)

        logger("Set client filter %s" % (network))
        self._ssdps.set_client_filter(network)

        start_cb = defer.Deferred()

        def bootstrap_proxy(addrs):
            def started(args):
                logger("DHT Started %s" % (args))
                if not self._started:
                    reactor.callLater(.2, start_cb.callback, True)
                if cb:
                    reactor.callLater(.2, cb, True)
                self._started = True

            def failed(args):
                logger("DHT failed to bootstrap %s" % (args))
                #reactor.callLater(.5, bootstrap_proxy, addrs)

            logger("Trying to bootstrap with %s" % (repr(addrs)))
            d = self.dht_server.bootstrap(addrs)
            d.addCallback(started)
            d.addErrback(failed)

        def start_msearch(args):
            logger("** msearch %s args: %s" % (self, repr(args)))
            reactor.callLater(0,
                                self._ssdps.start_search,
                                bootstrap_proxy,
                                stop=False)

        # Wait until servers all listen
        dl = defer.DeferredList(dlist)
        dl.addBoth(start_msearch)
        # Only for logging
        self.dht_server.kserver.protocol.sourceNode.port = port
        self.dht_server.kserver.protocol.sourceNode.ip = "0.0.0.0"
        #FIXME handle inside ServerApp
        self.dht_server.kserver.name = name
        self.dht_server.kserver.protocol.name = name
        self.dht_server.kserver.protocol.storeOwnCert(certstr)
        self.dht_server.kserver.protocol.setPrivateKey()

        return start_cb

    def set(self, key, value, cb=None):
        return TwistedWaitObject(self.dht_server.set, key=key, value=value, cb=cb)

    def get(self, key, cb=None):
        return TwistedWaitObject(self.dht_server.get, key=key, cb=cb)

    def get_concat(self, key, cb=None):
        return TwistedWaitObject(self.dht_server.get_concat, key=key, cb=cb)

    def append(self, key, value, cb=None):
        return TwistedWaitObject(self.dht_server.append, key=key, value=value, cb=cb)

    def remove(self, key, value, cb=None):
        return TwistedWaitObject(self.dht_server.remove, key=key, value=value, cb=cb)

    def bootstrap(self, addrs, cb=None):
        return TwistedWaitObject(self.dht_server.bootstrap, addr=addrs, cb=cb)

    def stop_search(self):
        return self._ssdps.stop_search()

    def stop(self, cb=None):
        d1 = self.dht_server.stop()
        d2 = self._ssdps.stop()

        dl = defer.DeferredList((d1, d2))
        if cb:
            dl.addBoth(cb)

        return dl

def main(iface):
    ret = 0
    try:
        a = AutoDHTServer()
        a.start(iface)

        b = AutoDHTServer()
        b.start(iface)

        time.sleep(4)

        print a.set(key="APA", value="banan")

        print a.get(key="APA")
        print b.get(key="APA")

        a.stop()
        b.stop()

    except:
        traceback.print_exc()
        ret = 1

    finally:
        if reactor.running:
            threads.blockingCallFromThread(reactor, reactor.stop)

    return ret

if __name__ == "__main__":
    print sys.argv
    if len(sys.argv) != 2:
        print "Usage: %s <server|client> <IP of interface>" % (sys.argv[0], )
        # sys.exit(1)
        interface = ''
    else:
        interface = sys.argv[1]
    sys.exit(main(interface))
<|endoftext|>


<commit_before>import re, socket, popen2, time

class TBReport:
    def __init__(self, project, experiment, nsfile):
        cmd = "ssh users.emulab.net /usr/testbed/bin/expinfo -l -e %s,%s" % (project, experiment)
        child = popen2.Popen3(cmd)
        cstdout = child.fromchild
        self.tblines = cstdout.readlines()
        self.pid = project
        self.eid = experiment
        self.nsfile = nsfile
        self.nodes = self.read_nsnodes(nsfile)
        self.nodes.sort()
        self.eips = self.lookup_external_ips()
        #for i in range(10000):
        #    print "TRY BLA"
        #    cmd = "ssh users.emulab.net /usr/testbed/bin/expinfo -l -e %s,%s" % (project, experiment)
        #    child = popen2.Popen3(cmd)
        #    cstdout = child.fromchild
        #    self.tblines = cstdout.readlines()
        self.iips = self.read_internal_ips()

    def read_nsnodes(self, nsfile):
        nodes = []
        for l in open(self.nsfile).readlines():
            m = re.search("set\s+(node\d+)\s+\[\s*\$ns\s+node\s*\]", l)
            if m:
                nodes.append(m.group(1))
        return nodes

    def lookup_external_ips(self):
        ips = {}
        for node in self.nodes:
            hostname = "%s.%s.%s.emulab.net" % (node, self.eid, self.pid)
            for retrynum in range(100000):
                try:
                    info = socket.gethostbyname_ex(hostname)
                    break
                except:
                    print "Failed looking up", hostname, "waiting a second"
                    time.sleep(1)
            ip = info[2][0] # Pick first external IP address
            ips[node] = ip
        return ips

    def read_internal_ips(self):
        ips = {}
        for l in self.tblines:
            m = re.search("^(node\d+)\s+node\d+:0\s+(.*?)\s+", l)
            if m:
                node, ip = m.group(1), m.group(2)
                ips[node] = ip
        return ips
<commit_msg>*** empty log message ***<commit_after>import re, socket, popen2, time

class TBReport:
    def __init__(self, project, experiment, nsfile):
        cmd = "ssh users.emulab.net /usr/testbed/bin/expinfo -l -e %s,%s" % (project, experiment)
        child = popen2.Popen3(cmd)
        cstdout = child.fromchild
        self.tblines = cstdout.readlines()
        self.pid = project
        self.eid = experiment
        self.nsfile = nsfile
        self.nodes = self.read_nsnodes(nsfile)
        self.nodes.sort()
        self.eips = self.lookup_external_ips()
        #for i in range(10000):
        #    print "TRY BLA"
        #    cmd = "ssh users.emulab.net /usr/testbed/bin/expinfo -l -e %s,%s" % (project, experiment)
        #    child = popen2.Popen3(cmd)
        #    cstdout = child.fromchild
        #    self.tblines = cstdout.readlines()
        self.iips = self.read_internal_ips()

    def read_nsnodes(self, nsfile):
        nodes = []
        for l in open(self.nsfile).readlines():
            m = re.search("set\s+(node\d+)\s+\[\s*\$ns\s+node\s*\]", l)
            if m:
                nodes.append(m.group(1))
        return nodes

    def lookup_external_ips(self):
        ips = {}
        for node in self.nodes:
            hostname = "%s.%s.%s.emulab.net" % (node, self.eid, self.pid)
            for retrynum in range(100000):
                try:
                    info = socket.gethostbyname_ex(hostname)
                    break
                except:
                    print "Failed looking up", hostname, "waiting a second"
                    time.sleep(1)
            ip = info[2][0] # Pick first external IP address
            ips[node] = ip
        return ips

    def read_internal_ips(self):
        ips = {}
        for l in self.tblines:
            m = re.search("^.*(node\d+):0\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*", l)
            if m and not ips.has_key(m.group(1)):
                node, ip = m.group(1), m.group(2)
                ips[node] = ip
        return ips
<|endoftext|>


<commit_before>from django.contrib.sitemaps import Sitemap

from .models import Document

class DocsSitemap(Sitemap):

    def items(self):
        return (Document.objects
                .order_by('release__lang', '-release__release', 'path')
                .select_related('release'))

    def changefreq(self, obj):
        if obj.release.is_dev:
            return 'daily'
        elif obj.release.is_default:
            return 'monthly'
        else:
            return 'yearly'

    def priority(self, obj):
        if obj.release.is_dev:
            return 0.5
        elif obj.release.is_default:
            return 1
        else:
            return 0.1

    def _urls(self, page, site, protocol):
        # XXX: To workaround bad interaction between contrib.sitemaps and
        # django-hosts (scheme/domain would be repeated twice in URLs)
        urls = []
        for item in self.paginator.page(page).object_list:
            loc = item.get_absolute_url()
            priority = self.priority(item)
            url_info = {
                'item': item,
                'location': loc,
                'changefreq': self.changefreq(item),
                'priority': str(priority if priority is not None else ''),
            }
            urls.append(url_info)
        return urls
<commit_msg>Increased docs sitemap crawl frequency to daily to be able to test SEO changes easier.<commit_after>from django.contrib.sitemaps import Sitemap

from .models import Document

class DocsSitemap(Sitemap):

    def items(self):
        return (Document.objects
                .order_by('release__lang', '-release__release', 'path')
                .select_related('release'))

    def changefreq(self, obj):
        return 'daily'
#        if obj.release.is_dev:
#            return 'daily'
#        elif obj.release.is_default:
#            return 'monthly'
#        else:
#            return 'yearly'

    def priority(self, obj):
        if obj.release.is_dev:
            return 0.5
        elif obj.release.is_default:
            return 1
        else:
            return 0.1

    def _urls(self, page, site, protocol):
        # XXX: To workaround bad interaction between contrib.sitemaps and
        # django-hosts (scheme/domain would be repeated twice in URLs)
        urls = []
        for item in self.paginator.page(page).object_list:
            loc = item.get_absolute_url()
            priority = self.priority(item)
            url_info = {
                'item': item,
                'location': loc,
                'changefreq': self.changefreq(item),
                'priority': str(priority if priority is not None else ''),
            }
            urls.append(url_info)
        return urls
<|endoftext|>


<commit_before>import csv
import os
import re
import pytz
from datetime import datetime, timedelta
import requests
import lxml.html
from lxml.etree import tostring

from django.conf import settings
from django.utils import timezone
from django.utils.text import slugify

app_timezone = pytz.timezone(settings.TIME_ZONE)

def format_full_text(full_text):
    '''
    The search results and board report titles (on the BillDetail) should show the "SUBJECT:" header from the board report when present.
    The ocr_full_text contains this information. Some example snippets:

    # Subject header followed by two linebreaks.
    ..Subject\nSUBJECT:\tFOOD SERVICE OPERATOR\n\n..Action\nACTION:\tAWARD SERVICES CONTRACT\n\n..

    # Subject header followed by a return carriage and linebreak.
    ..Subject/Action\r\nSUBJECT: MONTHLY REPORT ON CRENSHAW/LAX SAFETY\r\nACTION: RECEIVE AND FILE\r\n

    # Subject header with a linebreak in the middle and without an ACTION header.
    ..Subject\nSUBJECT:    REVISED MOTION BY DIRECTORS HAHN, SOLIS, \nGARCIA, AND DUPONT-WALKER\n..Title\n
    '''
    results = ''

    if full_text:
        clean_full_text = full_text.replace('\n\n', 'NEWLINE').replace('\r\n', 'NEWLINE').replace('\n..', 'NEWLINE').replace('\n', ' ')
        match = re.search('(SUBJECT:)(.*?)(NEWLINE|ACTION:)', clean_full_text)
        if match:
            results = match.group(2)

    return results

def parse_subject(text):
    if ('[PROJECT OR SERVICE NAME]' not in text) and ('[DESCRIPTION]' not in text) and ('[CONTRACT NUMBER]' not in text):
        return text.strip()

def get_identifier(obj_or_string):
    if isinstance(obj_or_string, str):
        return obj_or_string
    return obj_or_string.id

def get_list_from_csv(filename):
    file_directory = os.path.dirname(__file__)
    absolute_file_directory = os.path.abspath(file_directory)
    my_file = os.path.join(absolute_file_directory, '..', 'data', filename)

    with open(my_file) as f:
        reader = csv.DictReader(f)

        new_fieldnames = []
        for field in reader.fieldnames:
            new_fieldname = field.lower().replace(' ', '_')
            new_fieldnames.append(new_fieldname)
        reader.fieldnames = new_fieldnames

        my_list = [row for row in reader]

    return my_list
<commit_msg>remove slugify from imports<commit_after>import csv
import os
import re
import pytz
from datetime import datetime, timedelta
import requests
import lxml.html
from lxml.etree import tostring

from django.conf import settings
from django.utils import timezone

app_timezone = pytz.timezone(settings.TIME_ZONE)

def format_full_text(full_text):
    '''
    The search results and board report titles (on the BillDetail) should show the "SUBJECT:" header from the board report when present.
    The ocr_full_text contains this information. Some example snippets:

    # Subject header followed by two linebreaks.
    ..Subject\nSUBJECT:\tFOOD SERVICE OPERATOR\n\n..Action\nACTION:\tAWARD SERVICES CONTRACT\n\n..

    # Subject header followed by a return carriage and linebreak.
    ..Subject/Action\r\nSUBJECT: MONTHLY REPORT ON CRENSHAW/LAX SAFETY\r\nACTION: RECEIVE AND FILE\r\n

    # Subject header with a linebreak in the middle and without an ACTION header.
    ..Subject\nSUBJECT:    REVISED MOTION BY DIRECTORS HAHN, SOLIS, \nGARCIA, AND DUPONT-WALKER\n..Title\n
    '''
    results = ''

    if full_text:
        clean_full_text = full_text.replace('\n\n', 'NEWLINE').replace('\r\n', 'NEWLINE').replace('\n..', 'NEWLINE').replace('\n', ' ')
        match = re.search('(SUBJECT:)(.*?)(NEWLINE|ACTION:)', clean_full_text)
        if match:
            results = match.group(2)

    return results

def parse_subject(text):
    if ('[PROJECT OR SERVICE NAME]' not in text) and ('[DESCRIPTION]' not in text) and ('[CONTRACT NUMBER]' not in text):
        return text.strip()

def get_identifier(obj_or_string):
    if isinstance(obj_or_string, str):
        return obj_or_string
    return obj_or_string.id

def get_list_from_csv(filename):
    file_directory = os.path.dirname(__file__)
    absolute_file_directory = os.path.abspath(file_directory)
    my_file = os.path.join(absolute_file_directory, '..', 'data', filename)

    with open(my_file) as f:
        reader = csv.DictReader(f)

        new_fieldnames = []
        for field in reader.fieldnames:
            new_fieldname = field.lower().replace(' ', '_')
            new_fieldnames.append(new_fieldname)
        reader.fieldnames = new_fieldnames

        my_list = [row for row in reader]

    return my_list
<|endoftext|>


<commit_before>"""Provides the repository macro to import TFRT."""

load("//third_party:repo.bzl", "tf_http_archive")

def repo():
    """Imports TFRT."""

    # Attention: tools parse and update these lines.
    TFRT_COMMIT = "51fd8c14300e5ec7e66a0952e0b8c9da06116853"
    TFRT_SHA256 = "87a6f27753f21cada4c0d5c899028acab34911c3dbc047bb98e3279281cf4001"

    tf_http_archive(
        name = "tf_runtime",
        sha256 = TFRT_SHA256,
        strip_prefix = "runtime-{commit}".format(commit = TFRT_COMMIT),
        urls = [
            "http://mirror.tensorflow.org/github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT),
            "https://github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT),
        ],
        # A patch file can be provided for atomic commits to both TF and TFRT.
        # The job that bumps the TFRT_COMMIT also resets patch_file to 'None'.
        patch_file = None,
    )
<commit_msg>Update TFRT dependency to use revision http://github.com/tensorflow/runtime/commit/9718e43f94cb2a6753989c27c2fde077209fff72.<commit_after>"""Provides the repository macro to import TFRT."""

load("//third_party:repo.bzl", "tf_http_archive")

def repo():
    """Imports TFRT."""

    # Attention: tools parse and update these lines.
    TFRT_COMMIT = "9718e43f94cb2a6753989c27c2fde077209fff72"
    TFRT_SHA256 = "fb552e70f225d87e4ff7ba1e3ed94a64f4e26e85f681c15d669f94fb9f63c410"

    tf_http_archive(
        name = "tf_runtime",
        sha256 = TFRT_SHA256,
        strip_prefix = "runtime-{commit}".format(commit = TFRT_COMMIT),
        urls = [
            "http://mirror.tensorflow.org/github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT),
            "https://github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT),
        ],
        # A patch file can be provided for atomic commits to both TF and TFRT.
        # The job that bumps the TFRT_COMMIT also resets patch_file to 'None'.
        patch_file = None,
    )
<|endoftext|>


<commit_before>"""Provides the repository macro to import TFRT."""

load("//third_party:repo.bzl", "tf_http_archive")

def repo():
    """Imports TFRT."""

    # Attention: tools parse and update these lines.
    TFRT_COMMIT = "47a82b8999c5a1787f61814bd79bc237bd3d3dc1"
    TFRT_SHA256 = "b1280f54f3d379bc7caa6b53da3cb70084780b0d4329e83dcdac3e6fed2f64e3"

    tf_http_archive(
        name = "tf_runtime",
        sha256 = TFRT_SHA256,
        strip_prefix = "runtime-{commit}".format(commit = TFRT_COMMIT),
        urls = [
            "http://mirror.tensorflow.org/github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT),
            "https://github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT),
        ],
    )
<commit_msg>Update TFRT dependency to use revision http://github.com/tensorflow/runtime/commit/36ee0338418a57c49b7fb9bb0cf1d61279134142.<commit_after>"""Provides the repository macro to import TFRT."""

load("//third_party:repo.bzl", "tf_http_archive")

def repo():
    """Imports TFRT."""

    # Attention: tools parse and update these lines.
    TFRT_COMMIT = "36ee0338418a57c49b7fb9bb0cf1d61279134142"
    TFRT_SHA256 = "671d027625fc22c3eb1287b13071b17a000160a0d63b724f1a426fd3219570d4"

    tf_http_archive(
        name = "tf_runtime",
        sha256 = TFRT_SHA256,
        strip_prefix = "runtime-{commit}".format(commit = TFRT_COMMIT),
        urls = [
            "http://mirror.tensorflow.org/github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT),
            "https://github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT),
        ],
    )
<|endoftext|>


<commit_before>"""Provides the repository macro to import TFRT."""

load("//third_party:repo.bzl", "tf_http_archive", "tf_mirror_urls")

def repo():
    """Imports TFRT."""

    # Attention: tools parse and update these lines.
    TFRT_COMMIT = "8684a430e7c23d0ed83781e2df656a2f21a0b263"
    TFRT_SHA256 = "d0f11ad14fdbf59eccfefdebcf39cad229097f528e3d3f1670256e4a5d7e62fe"

    tf_http_archive(
        name = "tf_runtime",
        sha256 = TFRT_SHA256,
        strip_prefix = "runtime-{commit}".format(commit = TFRT_COMMIT),
        urls = tf_mirror_urls("https://github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT)),
        # A patch file can be provided for atomic commits to both TF and TFRT.
        # The job that bumps the TFRT_COMMIT also resets patch_file to 'None'.
        patch_file = None,
    )
<commit_msg>Update TFRT dependency to use revision http://github.com/tensorflow/runtime/commit/b8b0a570c1762364d904e0e2fd82cafe8bee9726.<commit_after>"""Provides the repository macro to import TFRT."""

load("//third_party:repo.bzl", "tf_http_archive", "tf_mirror_urls")

def repo():
    """Imports TFRT."""

    # Attention: tools parse and update these lines.
    TFRT_COMMIT = "b8b0a570c1762364d904e0e2fd82cafe8bee9726"
    TFRT_SHA256 = "0bedd719ef93381ccbe53420539bccf5d64a8fbf3e5a345fb7f6aa329c0bfcd0"

    tf_http_archive(
        name = "tf_runtime",
        sha256 = TFRT_SHA256,
        strip_prefix = "runtime-{commit}".format(commit = TFRT_COMMIT),
        urls = tf_mirror_urls("https://github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT)),
        # A patch file can be provided for atomic commits to both TF and TFRT.
        # The job that bumps the TFRT_COMMIT also resets patch_file to 'None'.
        patch_file = None,
    )
<|endoftext|>


<commit_before>#
# Copyright (c) 2009 Brad Taylor <brad@getcoded.net>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, 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 Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from django.http import HttpResponseRedirect, HttpResponseForbidden, Http404
from django.shortcuts import render_to_response, get_object_or_404
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator
from django.contrib.auth.models import User
from django.template import RequestContext
from django.db.models import Q

from snowy.notes.templates import CONTENT_TEMPLATES, DEFAULT_CONTENT_TEMPLATE
from snowy.notes.forms import SearchForm
from snowy.notes.models import *
from snowy import settings

def note_index(request, username,
               template_name='notes/note_index.html'):
    author = get_object_or_404(User, username=username)

    # TODO: retrieve the last open note from the user
    last_modified = Note.objects.user_viewable(request.user, author) \
                                .order_by('-user_modified')
    if last_modified.count() > 0:
        return HttpResponseRedirect(last_modified[0].get_absolute_url())

    # TODO: Instruction page to tell user to either sync or create a new note
    return render_to_response(template_name,
                              {'author': author,
                               # Django 1.1 does not support == operator, so
                               # we need to do the check here and pass it along
                               'author_is_user': username==request.user.username},
                              context_instance=RequestContext(request))

def note_list(request, username,
              template_name='notes/note_list.html'):
    author = get_object_or_404(User, username=username)
    notes = Note.objects.user_viewable(request.user, author)
    return render_to_response(template_name,
                              {'notes': notes},
                              context_instance=RequestContext(request))

def note_detail(request, username, note_id, slug='',
                template_name='notes/note_detail.html'):
    def clean_content(xml, author):
        """
        Adds an id attribute to <link:internal> tags so that URLs can be
        constructed by the XSLT.
        """
        from xml.dom import minidom
        doc = minidom.parseString(xml)

        for link in doc.getElementsByTagName('link:internal'):
            if len(link.childNodes) < 1: continue

            title = link.childNodes[0].nodeValue
            try:
                note = Note.objects.get(author=author, title=title)
            except ObjectDoesNotExist:
                continue

            link.setAttribute("id", str(note.pk))

        return doc.toxml()

    author = get_object_or_404(User, username=username)
    note = get_object_or_404(Note, pk=note_id, author=author)

    if request.user != author and note.permissions == 0:
        return HttpResponseForbidden()

    if note.slug != slug:
        return HttpResponseRedirect(note.get_absolute_url())

    # break this out into a function
    import libxslt
    import libxml2

    style, doc, result = None, None, None

    try:
        styledoc = libxml2.parseFile('data/note2xhtml.xsl')
        style = libxslt.parseStylesheetDoc(styledoc)

        template = CONTENT_TEMPLATES.get(note.content_version, DEFAULT_CONTENT_TEMPLATE)
        complete_xml = template.replace('%%%CONTENT%%%', note.content.encode('UTF-8'))
        doc = libxml2.parseDoc(clean_content(complete_xml, author).encode('UTF-8'))

        result = style.applyStylesheet(doc,
            {'base-user-url': "'%s'" % reverse('note_index', kwargs={'username': author.username})}
        )

        # libxml2 doesn't munge encodings, so forcibly decode from UTF-8
        body = unicode(style.saveResultToString(result), 'UTF-8')
    finally:
        if style != None: style.freeStylesheet()
        if doc != None: doc.freeDoc()
        if result != None: result.freeDoc()

    return render_to_response(template_name,
                              {'title': note.title,
                               'note': note, 'body': body,
                               'request': request, 'author': author},
                              context_instance=RequestContext(request))
<commit_msg>Remove SearchForm input, should have been in last patch<commit_after>#
# Copyright (c) 2009 Brad Taylor <brad@getcoded.net>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, 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 Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from django.http import HttpResponseRedirect, HttpResponseForbidden, Http404
from django.shortcuts import render_to_response, get_object_or_404
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator
from django.contrib.auth.models import User
from django.template import RequestContext
from django.db.models import Q

from snowy.notes.templates import CONTENT_TEMPLATES, DEFAULT_CONTENT_TEMPLATE
from snowy.notes.models import *
from snowy import settings

def note_index(request, username,
               template_name='notes/note_index.html'):
    author = get_object_or_404(User, username=username)

    # TODO: retrieve the last open note from the user
    last_modified = Note.objects.user_viewable(request.user, author) \
                                .order_by('-user_modified')
    if last_modified.count() > 0:
        return HttpResponseRedirect(last_modified[0].get_absolute_url())

    # TODO: Instruction page to tell user to either sync or create a new note
    return render_to_response(template_name,
                              {'author': author,
                               # Django 1.1 does not support == operator, so
                               # we need to do the check here and pass it along
                               'author_is_user': username==request.user.username},
                              context_instance=RequestContext(request))

def note_list(request, username,
              template_name='notes/note_list.html'):
    author = get_object_or_404(User, username=username)
    notes = Note.objects.user_viewable(request.user, author)
    return render_to_response(template_name,
                              {'notes': notes},
                              context_instance=RequestContext(request))

def note_detail(request, username, note_id, slug='',
                template_name='notes/note_detail.html'):
    def clean_content(xml, author):
        """
        Adds an id attribute to <link:internal> tags so that URLs can be
        constructed by the XSLT.
        """
        from xml.dom import minidom
        doc = minidom.parseString(xml)

        for link in doc.getElementsByTagName('link:internal'):
            if len(link.childNodes) < 1: continue

            title = link.childNodes[0].nodeValue
            try:
                note = Note.objects.get(author=author, title=title)
            except ObjectDoesNotExist:
                continue

            link.setAttribute("id", str(note.pk))

        return doc.toxml()

    author = get_object_or_404(User, username=username)
    note = get_object_or_404(Note, pk=note_id, author=author)

    if request.user != author and note.permissions == 0:
        return HttpResponseForbidden()

    if note.slug != slug:
        return HttpResponseRedirect(note.get_absolute_url())

    # break this out into a function
    import libxslt
    import libxml2

    style, doc, result = None, None, None

    try:
        styledoc = libxml2.parseFile('data/note2xhtml.xsl')
        style = libxslt.parseStylesheetDoc(styledoc)

        template = CONTENT_TEMPLATES.get(note.content_version, DEFAULT_CONTENT_TEMPLATE)
        complete_xml = template.replace('%%%CONTENT%%%', note.content.encode('UTF-8'))
        doc = libxml2.parseDoc(clean_content(complete_xml, author).encode('UTF-8'))

        result = style.applyStylesheet(doc,
            {'base-user-url': "'%s'" % reverse('note_index', kwargs={'username': author.username})}
        )

        # libxml2 doesn't munge encodings, so forcibly decode from UTF-8
        body = unicode(style.saveResultToString(result), 'UTF-8')
    finally:
        if style != None: style.freeStylesheet()
        if doc != None: doc.freeDoc()
        if result != None: result.freeDoc()

    return render_to_response(template_name,
                              {'title': note.title,
                               'note': note, 'body': body,
                               'request': request, 'author': author},
                              context_instance=RequestContext(request))
<|endoftext|>


<commit_before>"""Provides the repository macro to import TFRT."""

load("//third_party:repo.bzl", "tf_http_archive", "tf_mirror_urls")

def repo():
    """Imports TFRT."""

    # Attention: tools parse and update these lines.
    TFRT_COMMIT = "af0c4603b9bfb06a8c09c1cacaf97ab2644420ec"
    TFRT_SHA256 = "1346355c964073d597264e040d16276376887dfefa6dd48d9918d920c18b1af2"

    tf_http_archive(
        name = "tf_runtime",
        sha256 = TFRT_SHA256,
        strip_prefix = "runtime-{commit}".format(commit = TFRT_COMMIT),
        urls = tf_mirror_urls("https://github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT)),
        # A patch file can be provided for atomic commits to both TF and TFRT.
        # The job that bumps the TFRT_COMMIT also resets patch_file to 'None'.
        patch_file = None,
    )
<commit_msg>Update TFRT dependency to use revision http://github.com/tensorflow/runtime/commit/8700849191dea2ca9c12a3d26ec41fac0814671b.<commit_after>"""Provides the repository macro to import TFRT."""

load("//third_party:repo.bzl", "tf_http_archive", "tf_mirror_urls")

def repo():
    """Imports TFRT."""

    # Attention: tools parse and update these lines.
    TFRT_COMMIT = "8700849191dea2ca9c12a3d26ec41fac0814671b"
    TFRT_SHA256 = "021ab69903c0189794107916a46136f08e8673b048d6dbdd0aa495ca5173afac"

    tf_http_archive(
        name = "tf_runtime",
        sha256 = TFRT_SHA256,
        strip_prefix = "runtime-{commit}".format(commit = TFRT_COMMIT),
        urls = tf_mirror_urls("https://github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT)),
        # A patch file can be provided for atomic commits to both TF and TFRT.
        # The job that bumps the TFRT_COMMIT also resets patch_file to 'None'.
        patch_file = None,
    )
<|endoftext|>


<commit_before>"""Provides the repository macro to import TFRT."""

load("//third_party:repo.bzl", "tf_http_archive", "tf_mirror_urls")

def repo():
    """Imports TFRT."""

    # Attention: tools parse and update these lines.
    TFRT_COMMIT = "336df1b2df8295d01609eacd9284702f8ead01ca"
    TFRT_SHA256 = "555350732ba07938bf613857d6600cc2ce0815563a923f5c68ee91aeb369f4eb"

    tf_http_archive(
        name = "tf_runtime",
        sha256 = TFRT_SHA256,
        strip_prefix = "runtime-{commit}".format(commit = TFRT_COMMIT),
        urls = tf_mirror_urls("https://github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT)),
        # A patch file can be provided for atomic commits to both TF and TFRT.
        # The job that bumps the TFRT_COMMIT also resets patch_file to 'None'.
        patch_file = None,
    )
<commit_msg>Update TFRT dependency to use revision http://github.com/tensorflow/runtime/commit/bd93c3a55677d6e376c172c82258ecc9079c7498.<commit_after>"""Provides the repository macro to import TFRT."""

load("//third_party:repo.bzl", "tf_http_archive", "tf_mirror_urls")

def repo():
    """Imports TFRT."""

    # Attention: tools parse and update these lines.
    TFRT_COMMIT = "bd93c3a55677d6e376c172c82258ecc9079c7498"
    TFRT_SHA256 = "0cb142b8f8f859199a89fe9a8f5f91a3303e2141fa36ec1f577ca2ed8d0fcfff"

    tf_http_archive(
        name = "tf_runtime",
        sha256 = TFRT_SHA256,
        strip_prefix = "runtime-{commit}".format(commit = TFRT_COMMIT),
        urls = tf_mirror_urls("https://github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT)),
        # A patch file can be provided for atomic commits to both TF and TFRT.
        # The job that bumps the TFRT_COMMIT also resets patch_file to 'None'.
        patch_file = None,
    )
<|endoftext|>


<commit_before>"""Provides the repository macro to import TFRT."""

load("//third_party:repo.bzl", "tf_http_archive", "tf_mirror_urls")

def repo():
    """Imports TFRT."""

    # Attention: tools parse and update these lines.
    TFRT_COMMIT = "5413484b14366295565be23c6c3ea7456a47a8fd"
    TFRT_SHA256 = "25ec7760c79862508bda8e8166b8bcd427c8e5a93682de5f250d244b77eda734"

    tf_http_archive(
        name = "tf_runtime",
        sha256 = TFRT_SHA256,
        strip_prefix = "runtime-{commit}".format(commit = TFRT_COMMIT),
        urls = tf_mirror_urls("https://github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT)),
        # A patch file can be provided for atomic commits to both TF and TFRT.
        # The job that bumps the TFRT_COMMIT also resets patch_file to 'None'.
        patch_file = None,
    )
<commit_msg>Update TFRT dependency to use revision http://github.com/tensorflow/runtime/commit/eb44fa3bd8949218b9da72ad8fd018a45d5f03ee.<commit_after>"""Provides the repository macro to import TFRT."""

load("//third_party:repo.bzl", "tf_http_archive", "tf_mirror_urls")

def repo():
    """Imports TFRT."""

    # Attention: tools parse and update these lines.
    TFRT_COMMIT = "eb44fa3bd8949218b9da72ad8fd018a45d5f03ee"
    TFRT_SHA256 = "e6bf43117b091dc8fcc0e2477cec97adda46ddc7b106d595623bd1fb777ede11"

    tf_http_archive(
        name = "tf_runtime",
        sha256 = TFRT_SHA256,
        strip_prefix = "runtime-{commit}".format(commit = TFRT_COMMIT),
        urls = tf_mirror_urls("https://github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT)),
        # A patch file can be provided for atomic commits to both TF and TFRT.
        # The job that bumps the TFRT_COMMIT also resets patch_file to 'None'.
        patch_file = None,
    )
<|endoftext|>


<commit_before>"""Provides the repository macro to import TFRT."""

load("//third_party:repo.bzl", "tf_http_archive", "tf_mirror_urls")

def repo():
    """Imports TFRT."""

    # Attention: tools parse and update these lines.
    TFRT_COMMIT = "3cc340356a8c78cf15d220b0b237b339e0038dfb"
    TFRT_SHA256 = "7e210b4ddfc06f579a24ab88cc8db25739b1f1e61aa1d673e81604f78e277b2f"

    tf_http_archive(
        name = "tf_runtime",
        sha256 = TFRT_SHA256,
        strip_prefix = "runtime-{commit}".format(commit = TFRT_COMMIT),
        urls = tf_mirror_urls("https://github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT)),
        # A patch file can be provided for atomic commits to both TF and TFRT.
        # The job that bumps the TFRT_COMMIT also resets patch_file to 'None'.
        patch_file = None,
    )
<commit_msg>Update TFRT dependency to use revision http://github.com/tensorflow/runtime/commit/0dd2922b29d640b9f7a79ef77508df415598618e.<commit_after>"""Provides the repository macro to import TFRT."""

load("//third_party:repo.bzl", "tf_http_archive", "tf_mirror_urls")

def repo():
    """Imports TFRT."""

    # Attention: tools parse and update these lines.
    TFRT_COMMIT = "0dd2922b29d640b9f7a79ef77508df415598618e"
    TFRT_SHA256 = "55dbd5d1d1abb802c3c82b294eef8be5d7a4d2b32be8af2987668959b3f917f9"

    tf_http_archive(
        name = "tf_runtime",
        sha256 = TFRT_SHA256,
        strip_prefix = "runtime-{commit}".format(commit = TFRT_COMMIT),
        urls = tf_mirror_urls("https://github.com/tensorflow/runtime/archive/{commit}.tar.gz".format(commit = TFRT_COMMIT)),
        # A patch file can be provided for atomic commits to both TF and TFRT.
        # The job that bumps the TFRT_COMMIT also resets patch_file to 'None'.
        patch_file = None,
    )
<|endoftext|>


<commit_before>#!/usr/bin/env python
#encoding: utf-8
#@author: Psycho_Coder <https://psychocoder.github.io/>

import re,sys

TITLE = '''
 _     _            _        _____    _                  _  ___ _
| |   | |          | |      (_____)  | |            _   (_)/ __|_)
| |__ | | ____  ___| | _       _   _ | | ____ ____ | |_  _| |__ _  ____  ____
|  __)| |/ _  |/___) || \     | | / || |/ _  )  _ \|  _)| |  __) |/ _  )/ ___)
| |   | ( ( | |___ | | | |   _| |( (_| ( (/ /| | | | |__| | |  | ( (/ /| |
|_|   |_|\_||_(___/|_| |_|  (_____)____|\____)_| |_|\___)_|_|  |_|\____)_|
_______________________________________________________________________________

						   Version: 2.0 by Psycho_Coder
						   at HackCommunity.com.
_______________________________________________________________________________
	'''

USAGE = '''
		Usage:

		In the terminal run : python HashIdentifier.py

		2.7.6 <= Python Versions Support >= 3.0

	'''

HASHES = (
		("Blowfish(Eggdrop)",	"^\+[a-z0-9\/\.]{12}$"),
		("Blowfish(OpenBSD)",	"^\$2a\$[0-9]{0,2}?\$[a-z0-9\/\.]{53}$"),
		("DES(Unix)",		"^.{0,2}[a-z0-9\/\.]{11}$"),
		("MD5(Unix)",		"^\$1\$.{0,8}\$[a-z0-9\/\.]{22}$"),
		("MD5(APR)",		"^\$apr1\$.{0,8}\$[a-z0-9\/\.]{22}$"),
		("MD5(MyBB)",		"^[a-f-0-9]{32}:[a-z0-9]{8}$"),
		("MD5(Joomla)",		"^[a-f0-9]{32}:[a-z0-9]{16,32}$"),
		("MD5(Wordpress)",	"^\$P\$[a-z0-9\/\.]{31}$"),
		("MD5(phpBB3)",		"^\$H\$[A-Fa-z0-9\/\.]{31}$"),
		("MD5(Cisco PIX)",	"^[a-z0-9\/\.]{16}$"),
		("MD5(osCommerce)",	"^[a-f0-9]{32}:[a-z0-9]{2}$"),
		("MD5(Palshop)",	"^[a-f0-9]{51}$"),
		("MD5(IP.Board)",	"^[a-f0-9]{32}:.{5}$"),
		("Lotus Domino",	"^\(?[a-z0-9\+\/]{20}\)?$"),
		("Lineage II C4",	"^0x[a-f0-9]{32}$"),
		("CRC-96(ZIP)",		"^[a-f0-9]{24}$"),
		("Skein-1024",		"^[a-f0-9]{256}$"),
		(("RIPEMD-320","RIPEMD-320(HMAC)"),		"^[a-f0-9]{80}$"),
		("SHA-1(Django)",	"^sha1\$.{0,32}\$[a-f0-9]{40}$"),
		("SHA-512(Drupal)",	"^\$S\$[a-z0-9\/\.]{52}$"),
		("SHA-256(Django)",	"^sha256\$.{0,32}\$[a-f0-9]{64}$"),
		("SHA-384(Django)",	"^sha384\$.{0,32}\$[a-f0-9]{96}$"),
		("SHA-256(Unix)",	"^\$5\$.{0,22}\$[a-z0-9\/\.]{43,69}$"),
		("SHA-512(Unix)",	"^\$6\$.{0,22}\$[a-z0-9\/\.]{86}$"),
		(("SHA-384","SHA3-384","Skein-512(384)","Skein-1024(384)"),		"^[a-f0-9]{96}$"),
		(("SHA-512","SHA3-512","Whirlpool","SALSA-10","SALSA-20","Skein-512","Skein-1024(512)"),	"^[a-f0-9]{128}$"),
		("SSHA-1",		"^({SSHA})?[a-z0-9\+\/]{32,38}?(==)?$"),
		("MySQL 5.x",		"^\*[a-f0-9]{40}$"),
		(("MySQL 3.x","DES(Oracle)","LM","VNC","FNV-164"), 	"^[a-f0-9]{16}$"),
		("SAM(LM_Hash:NT_Hash)","^[a-f-0-9]{32}:[a-f-0-9]{32}$"),
		("MSSQL(2000)",		"^0x0100[a-f0-9]{0,8}?[a-f0-9]{80}$"),
		(("MSSQL(2005)","MSSQL(2008)"),		"^0x0100[a-f0-9]{0,8}?[a-f0-9]{40}$"),
		(("MySQL 4.x","SHA-1","HAVAL-160","SHA-1(MaNGOS)","SHA-1(MaNGOS2)","TIGER-160","RIPEMD-160","RIPEMD-160(HMAC)","TIGER-160(HMAC)","Skein-256(160)","Skein-512(160)"),	"^[a-f0-9]{40}$"),
		(("SHA-256","GOST R 34.11-94","RIPEMD-256","HAVAL-256","Snefru-256","RIPEMD-256(HMAC)","Keccak-256","Skein-256","Skein-512(256)"),		"^[a-f0-9]{64}$"),
		(("HAVAL-192","Tiger-192","TIGER-192(HMAC)"),		"^[a-f0-9]{48}$"),
		(("SHA-224","SHA-224(HMAC)","HAVAL-224","Keccak-224", "Skein-256(224)", "Skein-512(224)"),		"^[a-f0-9]{56}$"),
		(("Adler32","FNV-32","Joaat","CRC-32","CRC-32B","GHash-32-3","GHash-32-5","XOR-32"),		"^[a-f0-9]{8}$"),
		(("CRC-16-CCITT","CRC-16","FCS-16"),		"^[a-f0-9]{4}$"),
		(("MD5(HMAC(Wordpress))","MD5(HMAC)","MD5","RIPEMD-128","RIPEMD-128(HMAC)","Tiger-128","Tiger-128(HMAC)","RAdmin v2.x","NTLM","Domain Cached Credentials(DCC)","Domain Cached Credentials 2(DCC2)","MD4","MD2","Snefru-128","Snefru-128(HMAC)","HAVAL-128","HAVAL-128(HMAC)","Skein-256(128)", "Skein-512(128)"),		"^[0-9a-f]{32}$"),
)

# Function to identify all the hashes and return the results as list.
def identifyHashes(inputHash):

	# List to store the names of the identified Hash Algorithms
	res = []

	# Loop through all the hashes in the HASHES tuple and find all the possible hashes.
	for items in HASHES:
		if (re.match(items[1],inputHash,re.IGNORECASE)):
			res += [items[0]] if (type(items[0]) is str) else items[0]
	return res

#function Get input from the user maintaining the python compatibility with earlier and newer versions.
def getInput(prompt):
	if sys.hexversion > 0x03000000:
		return input(prompt)
	else:
		return raw_input(prompt)

def startProcess():

	# Run infinite loop to ask for entering a hash everytime a hash if found.
	while(1):
		print ("_" * 80)
		print ('\n')

		# Take the Hash as Input from the User.
		inputHash = getInput("Enter the Hash : ");

		if (len(inputHash) < 1):
			print ("\nPlease enter the hash. No input hash found.")
		else:
			# trim the hash entered and remove the unwanted spaces
			inputHash = inputHash.strip()

			# Do the operation of Identifying the hashes.
			results = identifyHashes(inputHash)

			# If the length of the list returned by the hash identifying method is zero
			# that means no hashes algorithms have been found
			if (len(results) == 0):
				print ("\n\nNot a Hash or Hash Unknown")
			elif (len(results) > 2):

				# Show the results with most and less probable hash algorithms
				print ("\nMost Probable Hash Algorithms found:\n")
				print ("[+] " + results[0])
				print ("[+] " + results[1])
				print ("\nOther Possible Hash Algorithms found:\n")
				for item in range(int(len(results))-2):
					print ("[+] " + results[item+2])
			else:
				print ("\nMost Probable Hash Algorithms found:\n")
				for item in range(int(len(results))):
					print ("[+] " + results[item])

def main():

	#Print the TITLE and USAGE and then start the main loop.
	print (TITLE)
	print (USAGE)
	startProcess()

if __name__ == "__main__":
	main()
<commit_msg>Update HashIdentifier.py<commit_after>#!/usr/bin/env python
#encoding: utf-8
#@author: Psycho_Coder <https://psychocoder.github.io/>

import re,sys

TITLE = '''
 _     _            _        _____    _                  _  ___ _
| |   | |          | |      (_____)  | |            _   (_)/ __|_)
| |__ | | ____  ___| | _       _   _ | | ____ ____ | |_  _| |__ _  ____  ____
|  __)| |/ _  |/___) || \     | | / || |/ _  )  _ \|  _)| |  __) |/ _  )/ ___)
| |   | ( ( | |___ | | | |   _| |( (_| ( (/ /| | | | |__| | |  | ( (/ /| |
|_|   |_|\_||_(___/|_| |_|  (_____)____|\____)_| |_|\___)_|_|  |_|\____)_|
_______________________________________________________________________________

						   Version: 2.1 by Psycho_Coder
						   at HackCommunity.com.
_______________________________________________________________________________
	'''

USAGE = '''
		Usage:

		In the terminal run : python HashIdentifier.py

		2.7.6 <= Python Versions Support >= 3.0

	'''

HASHES = (
		("Blowfish(Eggdrop)",	"^\+[a-z0-9\/\.]{12}$"),
		("Blowfish(OpenBSD)",	"^\$2a\$[0-9]{0,2}?\$[a-z0-9\/\.]{53}$"),
		("DES(Unix)",		"^.{0,2}[a-z0-9\/\.]{11}$"),
		("MD5(Unix)",		"^\$1\$.{0,8}\$[a-z0-9\/\.]{22}$"),
		("MD5(APR)",		"^\$apr1\$.{0,8}\$[a-z0-9\/\.]{22}$"),
		("MD5(MyBB)",		"^[a-f-0-9]{32}:[a-z0-9]{8}$"),
		("MD5(Joomla)",		"^[a-f0-9]{32}:[a-z0-9]{16,32}$"),
		("MD5(Wordpress)",	"^\$P\$[a-z0-9\/\.]{31}$"),
		("MD5(phpBB3)",		"^\$H\$[A-Fa-z0-9\/\.]{31}$"),
		("MD5(Cisco PIX)",	"^[a-z0-9\/\.]{16}$"),
		("MD5(osCommerce)",	"^[a-f0-9]{32}:[a-z0-9]{2}$"),
		("MD5(Palshop)",	"^[a-f0-9]{51}$"),
		("MD5(IP.Board)",	"^[a-f0-9]{32}:.{5}$"),
		("Lotus Domino",	"^\(?[a-z0-9\+\/]{20}\)?$"),
		("Lineage II C4",	"^0x[a-f0-9]{32}$"),
		("CRC-96(ZIP)",		"^[a-f0-9]{24}$"),
		("Skein-1024",		"^[a-f0-9]{256}$"),
		(("RIPEMD-320","RIPEMD-320(HMAC)"),		"^[a-f0-9]{80}$"),
		("SHA-1(Django)",	"^sha1\$.{0,32}\$[a-f0-9]{40}$"),
		("SHA-512(Drupal)",	"^\$S\$[a-z0-9\/\.]{52}$"),
		("SHA-256(Django)",	"^sha256\$.{0,32}\$[a-f0-9]{64}$"),
		("SHA-384(Django)",	"^sha384\$.{0,32}\$[a-f0-9]{96}$"),
		("SHA-256(Unix)",	"^\$5\$.{0,22}\$[a-z0-9\/\.]{43,69}$"),
		("SHA-512(Unix)",	"^\$6\$.{0,22}\$[a-z0-9\/\.]{86}$"),
		(("SHA-384","SHA3-384","Skein-512(384)","Skein-1024(384)"),		"^[a-f0-9]{96}$"),
		(("SHA-512","SHA3-512","Whirlpool","SALSA-10","SALSA-20","Skein-512","Skein-1024(512)"),	"^[a-f0-9]{128}$"),
		("SSHA-1",		"^({SSHA})?[a-z0-9\+\/]{32,38}?(==)?$"),
		("MySQL 5.x",		"^\*[a-f0-9]{40}$"),
		(("MySQL 3.x","DES(Oracle)","LM","VNC","FNV-164"), 	"^[a-f0-9]{16}$"),
		("SAM(LM_Hash:NT_Hash)","^[a-f-0-9]{32}:[a-f-0-9]{32}$"),
		("MSSQL(2000)",		"^0x0100[a-f0-9]{0,8}?[a-f0-9]{80}$"),
		(("MSSQL(2005)","MSSQL(2008)"),		"^0x0100[a-f0-9]{0,8}?[a-f0-9]{40}$"),
		(("MySQL 4.x","SHA-1","HAVAL-160","SHA-1(MaNGOS)","SHA-1(MaNGOS2)","TIGER-160","RIPEMD-160","RIPEMD-160(HMAC)","TIGER-160(HMAC)","Skein-256(160)","Skein-512(160)"),	"^[a-f0-9]{40}$"),
		(("SHA-256","GOST R 34.11-94","RIPEMD-256","HAVAL-256","Snefru-256","RIPEMD-256(HMAC)","Keccak-256","Skein-256","Skein-512(256)"),		"^[a-f0-9]{64}$"),
		(("HAVAL-192","Tiger-192","TIGER-192(HMAC)"),		"^[a-f0-9]{48}$"),
		(("SHA-224","SHA-224(HMAC)","HAVAL-224","Keccak-224", "Skein-256(224)", "Skein-512(224)"),		"^[a-f0-9]{56}$"),
		(("Adler32","FNV-32","Joaat","CRC-32","CRC-32B","GHash-32-3","GHash-32-5","XOR-32"),		"^[a-f0-9]{8}$"),
		(("CRC-16-CCITT","CRC-16","FCS-16"),		"^[a-f0-9]{4}$"),
		(("MD5(HMAC(Wordpress))","MD5(HMAC)","MD5","RIPEMD-128","RIPEMD-128(HMAC)","Tiger-128","Tiger-128(HMAC)","RAdmin v2.x","NTLM","Domain Cached Credentials(DCC)","Domain Cached Credentials 2(DCC2)","MD4","MD2","Snefru-128","Snefru-128(HMAC)","HAVAL-128","HAVAL-128(HMAC)","Skein-256(128)", "Skein-512(128)"),		"^[0-9a-f]{32}$"),
)

# Function to identify all the hashes and return the results as list.
def identifyHashes(inputHash):

	# List to store the names of the identified Hash Algorithms
	res = []

	# Loop through all the hashes in the HASHES tuple and find all the possible hashes.
	for items in HASHES:
		if (re.match(items[1],inputHash,re.IGNORECASE)):
			res += [items[0]] if (type(items[0]) is str) else items[0]
	return res

#function Get input from the user maintaining the python compatibility with earlier and newer versions.
def getInput(prompt):
	if sys.hexversion > 0x03000000:
		return input(prompt)
	else:
		return raw_input(prompt)

def startProcess():

	# Run infinite loop to ask for entering a hash everytime a hash if found.
	while(1):
		print ("_" * 80)
		print ('\n')

		# Take the Hash as Input from the User.
		inputHash = getInput("Enter the Hash : ");

		if (len(inputHash) < 1):
			print ("\nPlease enter the hash. No input hash found.")
		else:
			# trim the hash entered and remove the unwanted spaces
			inputHash = inputHash.strip()

			# Do the operation of Identifying the hashes.
			results = identifyHashes(inputHash)

			# If the length of the list returned by the hash identifying method is zero
			# that means no hashes algorithms have been found
			if (len(results) == 0):
				print ("\n\nNot a Hash or Hash Unknown")
			elif (len(results) > 2):

				# Show the results with most and less probable hash algorithms
				print ("\nMost Probable Hash Algorithms found:\n")
				print ("[+] " + results[0])
				print ("[+] " + results[1])
				print ("\nOther Possible Hash Algorithms found:\n")
				for item in range(int(len(results))-2):
					print ("[+] " + results[item+2])
			else:
				print ("\nMost Probable Hash Algorithms found:\n")
				for item in range(int(len(results))):
					print ("[+] " + results[item])

def main():

	#Print the TITLE and USAGE and then start the main loop.
	print (TITLE)
	print (USAGE)
	startProcess()

if __name__ == "__main__":
	main()
<|endoftext|>