<commit_before>#!/usr/bin/python3
# General libraries
import asyncio
import configparser
import discord
import os
import logging
from discord.ext import commands

# Our specific stuff
import utils

logging.basicConfig(level=logging.DEBUG)

global config
# Parse the config and stick in global "config" var
config = configparser.ConfigParser()
for inifile in [os.path.expanduser('~')+'/.bayohwoolph.ini','bayohwoolph.local.ini','bayohwoolph.ini']:
    if os.path.isfile(inifile):
        config.read(inifile)
        break # First config file wins
global MAIN
MAIN = config['MAIN']

description = '''Dark Echo's barkeep'''
global bot
bot = commands.Bot(command_prefix=commands.when_mentioned_or('$'), description=description)

@bot.event
@asyncio.coroutine
def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

newcadetmsg = """
**Welcome to Dark Echo, {0.mention}!**

**<:echoBlue:230423421983522816> Here are the basic steps to get started with Dark Echo: <:echoBlue:230423421983522816>**

1. Sign up for a forum account at <http://darkecho.org/forums/ucp.php?mode=register>
2. If you use Inara, join us at http://inara.cz/wing/300
3. In the game, under "Friends and Private Groups", request membership in the "Dark Echo" private group and send "Dark Echo" a friend request.
4. Send in-game friend requests to the Echoes you see currently active on Discord and/or via in the in-game private group.
5. Move your primary base of operations (any additional ships, etc) to Snodgrass Orbital in Disci.
6. Once your forum account is activated, look for "Getting Started" instructions there.

Note: You cannot get to Disci in a starter sidewinder.  You need a ship with at least a 9.5LY jump range.  Upgrade most components from "E" to "D" rated in a Sidewinder or Eagle or use a Hauler to make it. If you're still having trouble, talk to us and somebody can help.

Make sure you get that forum account set up, since that's what we use to track how long you've been a Cadet.

Please set an avatar image in Discord, as it greatly helps with telling people apart when using the in-game overlay.

If you stay active with us for a couple of weeks and haven't heard about a promotion to Officer, please remind the @Leadership.
"""

@bot.command()
@commands.has_role('Leadership')
@asyncio.coroutine
def newcadet(member : discord.Member):
    """Give intro message to new cadet and assign them cadet role."""
    global bot

    yield from bot.type()

    cadetrole = discord.Object(id=146725461727117314)
    yield from bot.add_roles(member,cadetrole)

    yield from bot.say(newcadetmsg.format(member))

bot.run(MAIN.get('login_token'))
<commit_msg>Make newcadet command smart enough to handle up to 20 members at once.<commit_after>#!/usr/bin/python3
# General libraries
import asyncio
import configparser
import discord
import os
import logging
from discord.ext import commands

# Our specific stuff
import utils

logging.basicConfig(level=logging.DEBUG)

global config
# Parse the config and stick in global "config" var
config = configparser.ConfigParser()
for inifile in [os.path.expanduser('~')+'/.bayohwoolph.ini','bayohwoolph.local.ini','bayohwoolph.ini']:
    if os.path.isfile(inifile):
        config.read(inifile)
        break # First config file wins
global MAIN
MAIN = config['MAIN']

description = '''Dark Echo's barkeep'''
global bot
bot = commands.Bot(command_prefix=commands.when_mentioned_or('$'), description=description)

@bot.event
@asyncio.coroutine
def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

newcadetmsg = """
**Welcome to Dark Echo, {0}!**

**<:echoBlue:230423421983522816> Here are the basic steps to get started with Dark Echo: <:echoBlue:230423421983522816>**

1. Sign up for a forum account at <http://darkecho.org/forums/ucp.php?mode=register>
2. If you use Inara, join us at http://inara.cz/wing/300
3. In the game, under "Friends and Private Groups", request membership in the "Dark Echo" private group and send "Dark Echo" a friend request.
4. Send in-game friend requests to the Echoes you see currently active on Discord and/or via in the in-game private group.
5. Move your primary base of operations (any additional ships, etc) to Snodgrass Orbital in Disci.
6. Once your forum account is activated, look for "Getting Started" instructions there.

Note: You cannot get to Disci in a starter sidewinder.  You need a ship with at least a 9.5LY jump range.  Upgrade most components from "E" to "D" rated in a Sidewinder or Eagle or use a Hauler to make it. If you're still having trouble, talk to us and somebody can help.

Make sure you get that forum account set up, since that's what we use to track how long you've been a Cadet.

Please set an avatar image in Discord, as it greatly helps with telling people apart when using the in-game overlay.

If you stay active with us for a couple of weeks and haven't heard about a promotion to Officer, please remind the @Leadership.
"""

@bot.command()
@commands.has_role('Leadership')
@asyncio.coroutine
def newcadet(
    member1  : discord.Member = None,
    member2  : discord.Member = None,
    member3  : discord.Member = None,
    member4  : discord.Member = None,
    member5  : discord.Member = None,
    member6  : discord.Member = None,
    member7  : discord.Member = None,
    member8  : discord.Member = None,
    member9  : discord.Member = None,
    member10 : discord.Member = None,
    member11 : discord.Member = None,
    member12 : discord.Member = None,
    member13 : discord.Member = None,
    member14 : discord.Member = None,
    member15 : discord.Member = None,
    member16 : discord.Member = None,
    member17 : discord.Member = None,
    member18 : discord.Member = None,
    member19 : discord.Member = None,
    member20 : discord.Member = None ):
    """Give intro message to new cadet and assign them cadet role."""
    global bot

    yield from bot.type()

    # pull all the arguments into an array
    argmembers = [member1, member2, member3, member4, member5, member6, member7, member8, member9, member10, member11, member12, member13, member14, member15, member16, member17, member18, member19, member20 ]

    # and then filter out the None/empty items, so that we have only an array of things actually mentioned
    filter(None,argmembers)
    members = [i for i in argmembers if i is not None]

    # FIXME: make this a utilfunction.
    # Turns an array of members into a nicely formatted list of mentions.
    mentiontext = ''
    if len(members) == 0:
        mentiontext = 'Nobody'
    elif len(members) == 1:
        mentiontext = members[0].mention
    elif len(members) > 1:
        mentions = [i.mention for i in members]
        mentiontext = ', '.join(mentions[:-1])
        mentiontext = mentiontext + ' and ' + mentions[-1]

    yield from bot.say(newcadetmsg.format(mentiontext))

    cadetrole = discord.Object(id=146725461727117314)

    for member in members:
        yield from bot.add_roles(member,cadetrole)

bot.run(MAIN.get('login_token'))
<|endoftext|>


<commit_before># Copyright 2017 Google 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.

import snippets

def test_create_get_delete_metric_descriptor(capsys):
    snippets.create_metric_descriptor()
    snippets.get_metric_descriptor('custom.googleapis.com/my_metric')
    out, _ = capsys.readouterr()
    assert 'DOUBLE' in out
    snippets.delete_metric_descriptor('custom.googleapis.com/my_metric')
    out, _ = capsys.readouterr()
    assert 'Deleted metric' in out

def test_list_metric_descriptors(capsys):
    snippets.list_metric_descriptors()
    out, _ = capsys.readouterr()
    assert 'logging.googleapis.com/byte_count' in out

def test_list_resources(capsys):
    snippets.list_monitored_resources()
    out, _ = capsys.readouterr()
    assert 'pubsub_topic' in out

def test_get_resources(capsys):
    snippets.get_monitored_resource_descriptor('pubsub_topic')
    out, _ = capsys.readouterr()
    assert 'A topic in Google Cloud Pub/Sub' in out

def test_time_series(capsys):
    snippets.write_time_series()

    snippets.list_time_series()
    out, _ = capsys.readouterr()
    assert 'TimeSeries with' in out

    snippets.list_time_series_header()
    out, _ = capsys.readouterr()
    assert 'TimeSeries with' in out

    snippets.list_time_series_aggregate()
    out, _ = capsys.readouterr()
    assert 'TimeSeries with' in out

    snippets.list_time_series_reduce()
    out, _ = capsys.readouterr()
    assert 'TimeSeries with' in out
<commit_msg>De-flake metric test<commit_after># Copyright 2017 Google 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.

import snippets
from gcp.testing import eventually_consistent

def test_create_get_delete_metric_descriptor(capsys):
    snippets.create_metric_descriptor()

    @eventually_consistent.call
    def __():
        snippets.get_metric_descriptor('custom.googleapis.com/my_metric')

    out, _ = capsys.readouterr()
    assert 'DOUBLE' in out
    snippets.delete_metric_descriptor('custom.googleapis.com/my_metric')
    out, _ = capsys.readouterr()
    assert 'Deleted metric' in out

def test_list_metric_descriptors(capsys):
    snippets.list_metric_descriptors()
    out, _ = capsys.readouterr()
    assert 'logging.googleapis.com/byte_count' in out

def test_list_resources(capsys):
    snippets.list_monitored_resources()
    out, _ = capsys.readouterr()
    assert 'pubsub_topic' in out

def test_get_resources(capsys):
    snippets.get_monitored_resource_descriptor('pubsub_topic')
    out, _ = capsys.readouterr()
    assert 'A topic in Google Cloud Pub/Sub' in out

def test_time_series(capsys):
    snippets.write_time_series()

    snippets.list_time_series()
    out, _ = capsys.readouterr()
    assert 'TimeSeries with' in out

    snippets.list_time_series_header()
    out, _ = capsys.readouterr()
    assert 'TimeSeries with' in out

    snippets.list_time_series_aggregate()
    out, _ = capsys.readouterr()
    assert 'TimeSeries with' in out

    snippets.list_time_series_reduce()
    out, _ = capsys.readouterr()
    assert 'TimeSeries with' in out
<|endoftext|>


<commit_before>import logging
log = logging.getLogger(__name__)

from django.shortcuts import (
    render,
    redirect,
)

from apps.api.models import (
    Group,
    Song,
    Person,
    Collection,
    Duplicate,
)

def home(request):
    return render(
        request,
        'home.html',
    )

def collections(request):
    collections = Collection.objects.exclude(is_flag=True).order_by('id')
    return render(
        request,
        'collections.html',
        {'collections': collections},
    )

def collection(request, id):
    collection = Collection.objects.get(id=id)
    dups = []
    if collection.primitive.name == 'Group':
        for d in collection.duplicates.all():
            g = Group.objects.get(id=d.source_id)
            dups.append(g)
    elif collection.primitive.name == 'Song':
        for d in collection.duplicates.all():
            s = Song.objects.get(id=d.source_id)
            dups.append(s)
    elif collection.primitive.name == 'Person':
        for d in collection.duplicates.all():
            p = Person.objects.get(id=d.source_id)
            dups.append(p)
    return render(
        request,
        'collection.html',
        {'collection': collection, 'dups': dups},
    )

def merge(request, id):
    keep = Duplicate.objects.get(source_id=id)
    collection = keep.collection
    if collection.primitive.name == 'Group':
        parent = Group.objects.get(id=keep.source_id)
    elif collection.primitive.name == 'Song':
        parent = Song.objects.get(id=keep.source_id)
    elif collection.primitive.name == 'Person':
        parent = Person.objects.get(id=keep.source_id)
    else:
        raise RuntimeError("No primitive selected.")
    orphans = collection.duplicates.exclude(source_id=id)
    for orphan in orphans:
        if collection.primitive.name == 'Group':
            drop = Group.objects.get(id=orphan.source_id)
            for contestant in drop.contestants.all():
                contestant.group = parent
                contestant.save()
            drop.delete()
        elif collection.primitive.name == 'Song':
            drop = Song.objects.get(id=orphan.source_id)
            for chart in drop.charts.all():
                chart.songs.add(parent)
                chart.songs.remove(drop)
            drop.delete()
        elif collection.primitive.name == 'Person':
            drop = Person.objects.get(id=orphan.source_id)
            for director in drop.choruses.all():
                director.person = parent
                director.save()
            for singer in drop.quartets.all():
                singer.person = parent
                singer.save()
            for arrangement in drop.arrangements.all():
                arrangement.person = parent
                arrangement.save()
            drop.delete()
        else:
            raise RuntimeError("How did i get here?")
    try:
        r = redirect('website:collection', collection.get_next().id)
    except AttributeError:
        r = redirect('website:collections')
    collection.delete()
    return r

def skip(request, id):
    collection = Collection.objects.get(id=id)
    try:
        r = redirect('website:collection', collection.get_next().id)
    except AttributeError:
        r = redirect('website:collections')
    collection.delete()
    return r

def flag(request, id):
    collection = Collection.objects.get(id=id)
    try:
        r = redirect('website:collection', collection.get_next().id)
    except AttributeError:
        r = redirect('website:collections')
    collection.is_flag = True
    collection.save()
    return r

def choruses(request):
    groups = Group.objects.filter(
        group_duplicates__isnull=False,
    ).filter(kind=2).order_by('name')
    return render(
        request,
        'groups.html',
        {'groups': groups},
    )

def quartets(request):
    groups = Group.objects.filter(
        group_duplicates__isnull=False,
    ).filter(kind=1).order_by('name')
    return render(
        request,
        'groups.html',
        {'groups': groups},
    )

def songs(request):
    songs = Song.objects.filter(
        song_duplicates__isnull=False,
    ).order_by('name')
    return render(
        request,
        'songs.html',
        {'songs': songs},
    )

def persons(request):
    persons = Person.objects.filter(
        person_duplicates__isnull=False,
    ).order_by('name')
    return render(
        request,
        'persons.html',
        {'persons': persons},
    )
<commit_msg>Slice (for testing)<commit_after>import logging
log = logging.getLogger(__name__)

from django.shortcuts import (
    render,
    redirect,
)

from apps.api.models import (
    Group,
    Song,
    Person,
    Collection,
    Duplicate,
)

def home(request):
    return render(
        request,
        'home.html',
    )

def collections(request):
    collections = Collection.objects.exclude(is_flag=True).order_by('id')
    return render(
        request,
        'collections.html',
        {'collections': collections},
    )

def collection(request, id):
    collection = Collection.objects.get(id=id)
    dups = []
    if collection.primitive.name == 'Group':
        for d in collection.duplicates.all():
            g = Group.objects.get(id=d.source_id)
            dups.append(g)
    elif collection.primitive.name == 'Song':
        for d in collection.duplicates.all():
            s = Song.objects.get(id=d.source_id)
            dups.append(s)
    elif collection.primitive.name == 'Person':
        for d in collection.duplicates.all():
            p = Person.objects.get(id=d.source_id)
            dups.append(p)
    return render(
        request,
        'collection.html',
        {'collection': collection, 'dups': dups},
    )

def merge(request, id):
    keep = Duplicate.objects.get(source_id=id)
    collection = keep.collection
    if collection.primitive.name == 'Group':
        parent = Group.objects.get(id=keep.source_id)
    elif collection.primitive.name == 'Song':
        parent = Song.objects.get(id=keep.source_id)
    elif collection.primitive.name == 'Person':
        parent = Person.objects.get(id=keep.source_id)
    else:
        raise RuntimeError("No primitive selected.")
    orphans = collection.duplicates.exclude(source_id=id)
    for orphan in orphans:
        if collection.primitive.name == 'Group':
            drop = Group.objects.get(id=orphan.source_id)
            for contestant in drop.contestants.all():
                contestant.group = parent
                contestant.save()
            drop.delete()
        elif collection.primitive.name == 'Song':
            drop = Song.objects.get(id=orphan.source_id)
            for chart in drop.charts.all():
                chart.songs.add(parent)
                chart.songs.remove(drop)
            drop.delete()
        elif collection.primitive.name == 'Person':
            drop = Person.objects.get(id=orphan.source_id)
            for director in drop.choruses.all():
                director.person = parent
                director.save()
            for singer in drop.quartets.all():
                singer.person = parent
                singer.save()
            for arrangement in drop.arrangements.all():
                arrangement.person = parent
                arrangement.save()
            drop.delete()
        else:
            raise RuntimeError("How did i get here?")
    try:
        r = redirect('website:collection', collection.get_next().id)
    except AttributeError:
        r = redirect('website:collections')
    collection.delete()
    return r

def skip(request, id):
    collection = Collection.objects.get(id=id)
    try:
        r = redirect('website:collection', collection.get_next().id)
    except AttributeError:
        r = redirect('website:collections')
    collection.delete()
    return r

def flag(request, id):
    collection = Collection.objects.get(id=id)
    try:
        r = redirect('website:collection', collection.get_next().id)
    except AttributeError:
        r = redirect('website:collections')
    collection.is_flag = True
    collection.save()
    return r

def choruses(request):
    groups = Group.objects.filter(
        group_duplicates__isnull=False,
    ).filter(kind=2).order_by('name')
    return render(
        request,
        'groups.html',
        {'groups': groups},
    )

def quartets(request):
    groups = Group.objects.filter(
        group_duplicates__isnull=False,
    ).filter(kind=1).order_by('name')
    return render(
        request,
        'groups.html',
        {'groups': groups},
    )

def songs(request):
    songs = Song.objects.filter(
        song_duplicates__isnull=False,
    ).order_by('name')
    return render(
        request,
        'songs.html',
        {'songs': songs},
    )

def persons(request):
    persons = Person.objects.filter(
        person_duplicates__isnull=False,
    ).order_by('name')[:100]
    return render(
        request,
        'persons.html',
        {'persons': persons},
    )
<|endoftext|>


<commit_before>import gtk
from gettext import gettext as _

import com
import config

from lib.gui_control import OutputWindow

class WelcomeWindow(OutputWindow):

	def __init__(self, *args, **kwargs):
		OutputWindow.__init__(self, *args, **kwargs)

		self.set_properties( hscrollbar_policy=gtk.POLICY_AUTOMATIC,
			vscrollbar_policy = gtk.POLICY_NEVER )

		self.remove(self.textview)

		self.table = gtk.Table(rows = 2, columns = 2)

		self.image = gtk.image_new_from_file(
			config.get("tekka","status_icon"))

		# scale image down to 128x128
		self.image.set_property("pixbuf",
			self.image.get_property("pixbuf").scale_simple(
			128,128,gtk.gdk.INTERP_HYPER))

		self.label = gtk.Label()
		self.label.set_markup(_("Welcome to tekka!"))

		self.table.attach(self.image, 0, 1, 0, 1, xoptions=0)
		self.table.attach(self.label, 1, 2, 0, 1, xoptions=0)

		self.descr = gtk.Label()

		self.table.attach(self.descr, 0, 2, 1, 2, xoptions=0)

		self.add_with_viewport(self.table)

		if com.sushi.connected:
			self.sushi_connected_cb(com.sushi)
		else:
			self.sushi_disconnected_cb(com.sushi)

		com.sushi.g_connect("maki-connected", self.sushi_connected_cb)
		com.sushi.g_connect("maki-disconnected", self.sushi_disconnected_cb)

	def sushi_connected_cb(self, sushi):
		s = _("You're connected to maki so the next step"
  				"is, that you connect to a server over the server"
				" dialog in the tekka menu.")
		self.descr.set_markup(s)

	def sushi_disconnected_cb(self, sushi):
		s = _("You're not connected to maki, the central "
		  		"IRC daemon which interacts with the IRC server. "
	  			"Without maki you can't connect to a "
  				"server or write messages.\n\n"
				"If you're having problems running maki, try to "
				"visit http://sushi.ikkoku.de/ and see if there's "
				"a solution for your problem. Otherwise, feel free "
				"to ask for support.")
		self.descr.set_markup(s)

<commit_msg>Fix some style issues in welcome dialog<commit_after>import gtk
from gettext import gettext as _

import com
import config

from lib.gui_control import OutputWindow

class WelcomeWindow(OutputWindow):

	def __init__(self, *args, **kwargs):
		OutputWindow.__init__(self, *args, **kwargs)

		self.set_properties(
			hscrollbar_policy=gtk.POLICY_AUTOMATIC,
			vscrollbar_policy = gtk.POLICY_NEVER )

		self.remove(self.textview)

		self.table = gtk.Table(rows = 2, columns = 2)
		self.table.set_homogeneous(False)
		self.table.set_property("border-width", 12)

		self.image = gtk.image_new_from_file(
			config.get("tekka","status_icon"))

		# scale image down to 128x128
		self.image.set_property("pixbuf",
			self.image.get_property("pixbuf").scale_simple(
			128,128, gtk.gdk.INTERP_HYPER))

		# Create Header label
		self.label = gtk.Label()
		self.label.set_property("yalign", 1)
		self.label.set_markup(
			_("<big>Welcome to tekka!</big>"))

		# Add Image to table
		self.ibox = gtk.EventBox()
		self.ibox.add(self.image)

		self.table.attach(self.ibox, 0, 1, 0, 2,
			xoptions=gtk.FILL|gtk.SHRINK,
			yoptions = gtk.FILL|gtk.EXPAND)

		# Add Label to table
		self.lbox = gtk.EventBox()
		self.lbox.add(self.label)

		self.table.attach(self.lbox, 1, 2, 0, 1,
			xoptions=gtk.FILL|gtk.EXPAND,
			yoptions = gtk.FILL|gtk.EXPAND)

		# Create Description label
		self.descr = gtk.Label()
		self.descr.set_properties(
			selectable = True,
			use_markup = True,
			width_chars = 30,
			wrap = True)

		# Add Description to table
		self.dbox = gtk.EventBox()
		self.dbox.add(self.descr)

		self.table.attach(self.dbox, 1, 2, 1, 2,
			xoptions=gtk.FILL|gtk.EXPAND,
			yoptions=gtk.FILL|gtk.EXPAND)

		def mod_bg(w, c):
			# for debugging purposes
			if False:
				s = w.get_style().copy()
				s.bg[gtk.STATE_NORMAL] = c
				w.set_style(s)

		mod_bg(self.lbox, gtk.gdk.Color("#FF0000"))
		mod_bg(self.dbox, gtk.gdk.Color("#00FF00"))
		mod_bg(self.ibox, gtk.gdk.Color("#0000FF"))

		self.add_with_viewport(self.table)

		if com.sushi.connected:
			self.sushi_connected_cb(com.sushi)
		else:
			self.sushi_disconnected_cb(com.sushi)

		com.sushi.g_connect("maki-connected", self.sushi_connected_cb)
		com.sushi.g_connect("maki-disconnected", self.sushi_disconnected_cb)

	def sushi_connected_cb(self, sushi):
		s = _("You're connected to maki so the next step "
  				"is, that you connect to a server over the server "
				"dialog in the tekka menu.")
		self.descr.set_markup(s)

	def sushi_disconnected_cb(self, sushi):
		s = _("You're not connected to maki, the central "
		  		"IRC daemon which interacts with the IRC server. "
	  			"Without maki you can't connect to a "
  				"server or write messages.\n\n"
				"If you're having problems running maki, try to "
				"visit http://sushi.ikkoku.de/ and see if there's "
				"a solution for your problem. Otherwise, feel free "
				"to ask for support.")
		self.descr.set_markup(s)

<|endoftext|>


<commit_before># Copyright (C) 2019 The Android Open Source Project
#
# 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.

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")

# This file must be kept in sync with tools/install-build-deps.

def perfetto_deps():
    # Note: this is more recent than the version of protobuf we use in the
    # GN and Android builds. This is because older versions of protobuf don't
    # support Bazel.
    _add_repo_if_not_existing(
        http_archive,
        name = "com_google_protobuf",
        strip_prefix = "protobuf-3.9.0",
        url = "https://github.com/protocolbuffers/protobuf/archive/v3.9.0.tar.gz",
        sha256 = "2ee9dcec820352671eb83e081295ba43f7a4157181dad549024d7070d079cf65",
    )

    _add_repo_if_not_existing(
        http_archive,
        name = "perfetto_dep_sqlite",
        url = "https://storage.googleapis.com/perfetto/sqlite-amalgamation-3250300.zip",
        sha256 = "2ad5379f3b665b60599492cc8a13ac480ea6d819f91b1ef32ed0e1ad152fafef",
        strip_prefix = "sqlite-amalgamation-3250300",
        build_file = "//bazel:sqlite.BUILD",
    )

    _add_repo_if_not_existing(
        http_archive,
        name = "perfetto_dep_sqlite_src",
        url = "https://storage.googleapis.com/perfetto/sqlite-src-3250300.zip",
        sha256 = "c7922bc840a799481050ee9a76e679462da131adba1814687f05aa5c93766421",
        strip_prefix = "sqlite-src-3250300",
        build_file = "//bazel:sqlite.BUILD",
    )

    _add_repo_if_not_existing(
        new_git_repository,
        name = "perfetto_dep_linenoise",
        remote = "https://fuchsia.googlesource.com/third_party/linenoise.git",
        commit = "c894b9e59f02203dbe4e2be657572cf88c4230c3",
        build_file = "//bazel:linenoise.BUILD",
        shallow_since = "1469784335 +0200",
    )

    _add_repo_if_not_existing(
        new_git_repository,
        name = "perfetto_dep_jsoncpp",
        remote = "https://github.com/open-source-parsers/jsoncpp",
        commit = "6aba23f4a8628d599a9ef7fa4811c4ff6e4070e2",  # v1.9.3
        build_file = "//bazel:jsoncpp.BUILD",
        shallow_since = "1590760226 +0800",
    )

    _add_repo_if_not_existing(
        new_git_repository,
        name = "perfetto_dep_zlib",
        remote = "https://android.googlesource.com/platform/external/zlib.git",
        commit = "dfa0646a03b4e1707469e04dc931b09774968fe6",
        build_file = "//bazel:zlib.BUILD",
        shallow_since = "1557160162 -0700",
    )

    # Without this protobuf.bzl fails. This seems a bug in protobuf_deps().
    _add_repo_if_not_existing(
        http_archive,
        name = "bazel_skylib",
        sha256 = "bbccf674aa441c266df9894182d80de104cabd19be98be002f6d478aaa31574d",
        strip_prefix = "bazel-skylib-2169ae1c374aab4a09aa90e65efe1a3aad4e279b",
        url = "https://github.com/bazelbuild/bazel-skylib/archive/2169ae1c374aab4a09aa90e65efe1a3aad4e279b.tar.gz",
    )

def _add_repo_if_not_existing(repo_rule, name, **kwargs):
    if name not in native.existing_rules():
        repo_rule(name = name, **kwargs)
<commit_msg>bazel: Bump bazel protobuf version<commit_after># Copyright (C) 2019 The Android Open Source Project
#
# 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.

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")

# This file must be kept in sync with tools/install-build-deps.

def perfetto_deps():
    # Note: this is more recent than the version of protobuf we use in the
    # GN and Android builds. This is because older versions of protobuf don't
    # support Bazel.
    _add_repo_if_not_existing(
        http_archive,
        name = "com_google_protobuf",
        strip_prefix = "protobuf-3.10.1",
        url = "https://github.com/protocolbuffers/protobuf/archive/v3.10.1.tar.gz",
        sha256 = "6adf73fd7f90409e479d6ac86529ade2d45f50494c5c10f539226693cb8fe4f7",
    )

    _add_repo_if_not_existing(
        http_archive,
        name = "perfetto_dep_sqlite",
        url = "https://storage.googleapis.com/perfetto/sqlite-amalgamation-3250300.zip",
        sha256 = "2ad5379f3b665b60599492cc8a13ac480ea6d819f91b1ef32ed0e1ad152fafef",
        strip_prefix = "sqlite-amalgamation-3250300",
        build_file = "//bazel:sqlite.BUILD",
    )

    _add_repo_if_not_existing(
        http_archive,
        name = "perfetto_dep_sqlite_src",
        url = "https://storage.googleapis.com/perfetto/sqlite-src-3250300.zip",
        sha256 = "c7922bc840a799481050ee9a76e679462da131adba1814687f05aa5c93766421",
        strip_prefix = "sqlite-src-3250300",
        build_file = "//bazel:sqlite.BUILD",
    )

    _add_repo_if_not_existing(
        new_git_repository,
        name = "perfetto_dep_linenoise",
        remote = "https://fuchsia.googlesource.com/third_party/linenoise.git",
        commit = "c894b9e59f02203dbe4e2be657572cf88c4230c3",
        build_file = "//bazel:linenoise.BUILD",
        shallow_since = "1469784335 +0200",
    )

    _add_repo_if_not_existing(
        new_git_repository,
        name = "perfetto_dep_jsoncpp",
        remote = "https://github.com/open-source-parsers/jsoncpp",
        commit = "6aba23f4a8628d599a9ef7fa4811c4ff6e4070e2",  # v1.9.3
        build_file = "//bazel:jsoncpp.BUILD",
        shallow_since = "1590760226 +0800",
    )

    _add_repo_if_not_existing(
        new_git_repository,
        name = "perfetto_dep_zlib",
        remote = "https://android.googlesource.com/platform/external/zlib.git",
        commit = "dfa0646a03b4e1707469e04dc931b09774968fe6",
        build_file = "//bazel:zlib.BUILD",
        shallow_since = "1557160162 -0700",
    )

    # Without this protobuf.bzl fails. This seems a bug in protobuf_deps().
    _add_repo_if_not_existing(
        http_archive,
        name = "bazel_skylib",
        sha256 = "bbccf674aa441c266df9894182d80de104cabd19be98be002f6d478aaa31574d",
        strip_prefix = "bazel-skylib-2169ae1c374aab4a09aa90e65efe1a3aad4e279b",
        url = "https://github.com/bazelbuild/bazel-skylib/archive/2169ae1c374aab4a09aa90e65efe1a3aad4e279b.tar.gz",
    )

def _add_repo_if_not_existing(repo_rule, name, **kwargs):
    if name not in native.existing_rules():
        repo_rule(name = name, **kwargs)
<|endoftext|>


<commit_before>import os
import shutil
import pandas as pd
from tools.path_maker import PathMaker
from tools.logger import Explog
from tools.emailer import Emailer

class ExpCommon(Explog, PathMaker):
    def __init__(self, root, name):
        """
        `root`: file root
        `name`: project name
        """
        PathMaker.__init__(self, root, name)
        Explog.__init__(self, root, name)
        self.root = root
        self.name = name
        self.comp = 6

    def __save_key(self, fpath, key):
        kf = self.load('keys')
        if kf is None:
            kf = pd.DataFrame([key], columns=['key'])
        else:
            kf = kf.append(pd.DataFrame([key], columns=['key']))
            kf = kf.reset_index()
            for rc in ['index', 'level']:
                if rc in kf.columns:
                    del kf[rc]
        kf.to_hdf(fpath, 'keys', mode='a', data_columns=True,
                  format='t', complib='blosc', complevel=self.comp)

    def _ustr_dict(self, di):
        """
        Make dict keys underscore for saving to hdfs
        """
        s = ""
        ks = di.keys()
        ks.sort()
        for k in ks:
            s += "/{}_{}".format(k, di[k])
        return s

    def store(self):
        sp = self.make()
        return pd.HDFStore(sp, format='t', data_columns=True,
                           complib='blosc', complevel=self.comp)

    def delete_file(self, tar=[('store', 'h5', False)]):
        for res, ext, root in tar:
            ph = self.make(res, ext, False, root)
            print ph
            if ext is None or root:  # for whole directory
                shutil.rmtree(ph)
            else:  # for a single file
                if os.path.isfile(ph):
                    os.remove(ph)

    def save(self, key, data):
        """
        Save key to hstore
        """
        sp = self.make('stores', 'h5', asure=True, root=False)
        data.to_hdf(sp, key, mode='a', data_columns=True, format='t',
                    complib='blosc', complevel=self.comp)
        self.elog.info('Key [{}] saved to path: {}'.format(key, sp))
        self.__save_key(sp, key)

    def load(self, key):
        sp = self.make(asure=False)
        try:
            df = pd.read_hdf(sp, key, format='t')
        except KeyError, e:
            print e
            self.elog.error('load key [{}] from path: {}'.format(key, sp))
            self.elog.error('  >> Error: {}'.format(e))
            return None
        return df

    def notify(self, summary):
        if self.upass is None:
            return
        ps = self.upass
        cn = self.underscore(self.__class__.__name__)
        title = "Pyslider Job: {} <{}-{}> Finished". \
            format(cn, self.root, self.name)
        me = "speed.of.lightt@gmail.com"
        with Emailer(config=dict(uname=me, upass=ps)) as mailer:
            mailer.send(title, summary, ['speed.of.lightt@gmail.com'])
<commit_msg>Make saving data more informative.<commit_after>import os
import shutil
import pandas as pd
from tools.path_maker import PathMaker
from tools.logger import Explog
from tools.emailer import Emailer

class ExpCommon(Explog, PathMaker):
    def __init__(self, root, name):
        """
        `root`: file root
        `name`: project name
        """
        PathMaker.__init__(self, root, name)
        Explog.__init__(self, root, name)
        self.root = root
        self.name = name
        self.comp = 6

    def __save_key(self, fpath, key):
        kf = self.load('keys')
        kf = kf.append(pd.DataFrame([key], columns=['key']))
        kf = kf.reset_index()
        for rc in ['index', 'level']:
            if rc in kf.columns:
                del kf[rc]
        kf.to_hdf(fpath, 'keys', mode='a', data_columns=True,
                  format='t', complib='blosc', complevel=self.comp)

    def _ustr_dict(self, di):
        """
        Make dict keys underscore for saving to hdfs
        """
        s = ""
        ks = di.keys()
        ks.sort()
        for k in ks:
            s += "/{}_{}".format(k, di[k])
        return s

    def store(self):
        sp = self.make()
        return pd.HDFStore(sp, format='t', data_columns=True,
                           complib='blosc', complevel=self.comp)

    def delete_file(self, tar=[('store', 'h5', False)]):
        for res, ext, root in tar:
            ph = self.make(res, ext, False, root)
            print ph
            if ext is None or root:  # for whole directory
                shutil.rmtree(ph)
            else:  # for a single file
                if os.path.isfile(ph):
                    os.remove(ph)

    def save(self, key, data):
        """
        Save key to hstore
        """
        sp = self.make('stores', 'h5', asure=True, root=False)
        data.to_hdf(sp, key, mode='a', data_columns=True, format='t',
                    complib='blosc', complevel=self.comp)
        self.elog.info('Key [{}] saved to path: {}'.format(key, sp))
        self.__save_key(sp, key)

    def __make_keyfile(self, key):
        kf = pd.DataFrame([key], columns=['key'])
        sp = self.make('stores', 'h5', asure=True, root=False)
        kf.to_hdf(sp, key, mode='a', data_columns=True, format='t',
                  complib='blosc', complevel=self.comp)
        return kf

    def load(self, key):
        sp = self.make(asure=False)
        try:
            df = pd.read_hdf(sp, key, format='t')
        except KeyError, e:
            print e
            self.elog.error('load key [{}] from path: {}'.format(key, sp))
            if "No object named" in str(e):
                print "auto create one"
                return self.__make_keyfile(key)
            return None
        return df

    def notify(self, summary):
        if self.upass is None:
            return
        ps = self.upass
        cn = self.underscore(self.__class__.__name__)
        title = "Pyslider Job: {} <{}-{}> Finished". \
            format(cn, self.root, self.name)
        me = "speed.of.lightt@gmail.com"
        with Emailer(config=dict(uname=me, upass=ps)) as mailer:
            mailer.send(title, summary, ['speed.of.lightt@gmail.com'])
<|endoftext|>


<commit_before># Created by adlh on 2012-10-24
#
# Templates are needed in order render the navigations:
#
# <template_folder>/multinavigation/tabnavigation.html
# <template_folder>/multinavigation/breadcrumbs.html

from django.conf import settings
from django import template
from django.template import RequestContext
from collections import namedtuple
from django.core.urlresolvers import reverse, resolve, Resolver404
from pprint import pformat
import logging

logger = logging.getLogger(__name__)
register = template.Library()

""" A tree node represents each item on a tree-navigation """
TNode = namedtuple('TNode', 'url label active children context')
def build_tnode(n, children, url, active):
    """ Takes a multinavigation.conf.Node namedtuple and builds a tree node. """
    return TNode(url, n.label, active, children, n.context)

@register.inclusion_tag('multinavigation/tabnavigation.html',
        takes_context=True)
def tabnavigation(context, request, nodes):
    """ Returns nodes for the complete navigation tree. """
    url_match = get_url_match(request)
    parents = [n for n in nodes if not n.parent]
    tree_nodes = add_nodes(parents, nodes, request, url_match)
    context['nodes'] = tree_nodes
    return RequestContext(request, {'nodes': tree_nodes, 'context': context})

@register.inclusion_tag('multinavigation/flatnavigation.html',
        takes_context=True)
def flatnavigation(context, request, nodes):
    """ Returns nodes only for the root level. This can be used in combination
    with the subnavigation. """
    tree_nodes = []
    url_match = get_url_match(request)
    for n in nodes:
        url = reverse_url(n, url_match)
        if not n.parent:
            tree_nodes.append(build_tnode(n, [], url, is_active(request, url)))
    return RequestContext(request, {'nodes': tree_nodes, 'context': context})

@register.inclusion_tag('multinavigation/subnavigation.html',
        takes_context=True)
def subnavigation(context, request, nodes):
    """ Returns only a submenu (tree), if any, for the current parent. """
    url_match = get_url_match(request)
    urlname = url_match.url_name
    if not urlname:
        return []
    subnodes = []
    children = []
    for n in nodes:
        if n.url_name == urlname:
            # this is the active node, get top-most parent first
            parent = get_root(n, nodes)
            children = [c for c in nodes if c.parent == parent.url_name]
    tree_nodes = add_nodes(children, nodes, request, url_match)
    return RequestContext(request, {'nodes': tree_nodes, 'context': context})

@register.inclusion_tag('multinavigation/breadcrumbs.html', takes_context=True)
def breadcrumbs(context, request, nodes):
    """ Returns the bredcrumbs nodes """
    url_match = get_url_match(request)
    urlname = url_match.url_name
    if not urlname:
        return []
    b_nodes = []
    # if any breadcrumbs are found the first one is the active one
    find_breadcrumbs(urlname, nodes, b_nodes, True, url_match)
    # now reverse to get parent > child > grandchild > ...
    b_nodes.reverse()
    return RequestContext(request, {'nodes': b_nodes, 'context': context })

def get_root(n, nodes):
    """ returns the top-most parent in nodes for the given node """
    if not n.parent:
        return n
    for node in nodes:
        if n.parent == node.url_name:
            if not node.parent:
                return node
            else:
                return get_root(node, nodes)

def get_url_match(request):
    """ Get the name of the matching urlpattern """
    if not hasattr(request, 'path'):
        return ""
    # first get the name of the matching urlpattern
    try:
        return resolve(request.path)
    except Resolver404:
        return None

def find_breadcrumbs(url_name, nodes, b_nodes, active, url_match):
    for n in nodes:
        if n.url_name == url_name:
            url = reverse_url(n, url_match)
            b_nodes.append(build_tnode(n, [], url, active))
            if n.parent:
                find_breadcrumbs(n.parent, nodes, b_nodes, False, url_match)

def add_nodes(parents, nodes, request, url_match):
    tn_list = []
    for n in parents:
        children = [c for c in nodes if c.parent == n.url_name]
        tn_children = add_nodes(children, nodes, request, url_match)
        url = reverse_url(n, url_match)
        active = is_active(request, url)
        tn_list.append(build_tnode(n, tn_children, url, active))
    return tn_list

def get_url_kwargs(url_kwargs_str):
    # url_kwargs should be a str in form 'kwd_1:val_1, kwd_2:val_2, ...'...
    url_kwargs_str = str(url_kwargs_str) # just make sure it's a string
    url_kwargs = {}
    if url_kwargs_str:
        for pair in url_kwargs_str.split(','):
            k, v = pair.strip().split(':')
            k = k.strip()
            v = v.strip()
        if k and v:
            url_kwargs[k] = v
    return url_kwargs

def reverse_url(n, url_match):
    url_kwargs = n.context.get('url_kwargs', '')
    kwargs_dict = {}
    if url_kwargs:
        kwargs_dict = get_url_kwargs(url_kwargs)
    # if there is a required kwarg defined in the node-context but it's empty
    # and it's in the url_match present, update dict
    for k in kwargs_dict.keys():
        v = url_match.kwargs.get(k, None)
        if v:
            kwargs_dict[k] = v
    url = reverse(n.url_name, args=url_match.args, kwargs=kwargs_dict)
    return url

def is_active(request, link_url):
    """ check if the corresponding parts of the given link and the request.path
    match (active).
    For example, if requeest.path is '/bla/bli/blu/' and link_url is '/bla/',
    '/bla/bli' or /bla/bli/blu/' is_active should return True
    """
    if not hasattr(request, 'path'):
        return False
    link_parts = link_url.strip('/').split('/')
    request_parts = (request.path).strip('/').split('/')
    if len(request_parts) < len(link_parts):
        return False
    return '/'.join(link_parts) == '/'.join(request_parts[0:len(link_parts)])

<commit_msg>Handle case of url_match being None<commit_after># Created by adlh on 2012-10-24
#
# Templates are needed in order render the navigations:
#
# <template_folder>/multinavigation/tabnavigation.html
# <template_folder>/multinavigation/breadcrumbs.html

from django.conf import settings
from django import template
from django.template import RequestContext
from collections import namedtuple
from django.core.urlresolvers import reverse, resolve, Resolver404
from pprint import pformat
import logging

logger = logging.getLogger(__name__)
register = template.Library()

""" A tree node represents each item on a tree-navigation """
TNode = namedtuple('TNode', 'url label active children context')
def build_tnode(n, children, url, active):
    """ Takes a multinavigation.conf.Node namedtuple and builds a tree node. """
    return TNode(url, n.label, active, children, n.context)

@register.inclusion_tag('multinavigation/tabnavigation.html',
        takes_context=True)
def tabnavigation(context, request, nodes):
    """ Returns nodes for the complete navigation tree. """
    url_match = get_url_match(request)
    parents = [n for n in nodes if not n.parent]
    tree_nodes = add_nodes(parents, nodes, request, url_match)
    context['nodes'] = tree_nodes
    return RequestContext(request, {'nodes': tree_nodes, 'context': context})

@register.inclusion_tag('multinavigation/flatnavigation.html',
        takes_context=True)
def flatnavigation(context, request, nodes):
    """ Returns nodes only for the root level. This can be used in combination
    with the subnavigation. """
    tree_nodes = []
    url_match = get_url_match(request)
    for n in nodes:
        url = reverse_url(n, url_match)
        if not n.parent:
            tree_nodes.append(build_tnode(n, [], url, is_active(request, url)))
    return RequestContext(request, {'nodes': tree_nodes, 'context': context})

@register.inclusion_tag('multinavigation/subnavigation.html',
        takes_context=True)
def subnavigation(context, request, nodes):
    """ Returns only a submenu (tree), if any, for the current parent. """
    url_match = get_url_match(request)
    urlname = url_match.url_name
    if not urlname:
        return []
    subnodes = []
    children = []
    for n in nodes:
        if n.url_name == urlname:
            # this is the active node, get top-most parent first
            parent = get_root(n, nodes)
            children = [c for c in nodes if c.parent == parent.url_name]
    tree_nodes = add_nodes(children, nodes, request, url_match)
    return RequestContext(request, {'nodes': tree_nodes, 'context': context})

@register.inclusion_tag('multinavigation/breadcrumbs.html', takes_context=True)
def breadcrumbs(context, request, nodes):
    """ Returns the bredcrumbs nodes """
    url_match = get_url_match(request)
    urlname = url_match.url_name
    if not urlname:
        return []
    b_nodes = []
    # if any breadcrumbs are found the first one is the active one
    find_breadcrumbs(urlname, nodes, b_nodes, True, url_match)
    # now reverse to get parent > child > grandchild > ...
    b_nodes.reverse()
    return RequestContext(request, {'nodes': b_nodes, 'context': context })

def get_root(n, nodes):
    """ returns the top-most parent in nodes for the given node """
    if not n.parent:
        return n
    for node in nodes:
        if n.parent == node.url_name:
            if not node.parent:
                return node
            else:
                return get_root(node, nodes)

def get_url_match(request):
    """ Get the name of the matching urlpattern """
    if not hasattr(request, 'path'):
        return ""
    # first get the name of the matching urlpattern
    try:
        return resolve(request.path)
    except Resolver404:
        return None

def find_breadcrumbs(url_name, nodes, b_nodes, active, url_match):
    for n in nodes:
        if n.url_name == url_name:
            url = reverse_url(n, url_match)
            b_nodes.append(build_tnode(n, [], url, active))
            if n.parent:
                find_breadcrumbs(n.parent, nodes, b_nodes, False, url_match)

def add_nodes(parents, nodes, request, url_match):
    tn_list = []
    for n in parents:
        children = [c for c in nodes if c.parent == n.url_name]
        tn_children = add_nodes(children, nodes, request, url_match)
        url = reverse_url(n, url_match)
        active = is_active(request, url)
        tn_list.append(build_tnode(n, tn_children, url, active))
    return tn_list

def get_url_kwargs(url_kwargs_str):
    # url_kwargs should be a str in form 'kwd_1:val_1, kwd_2:val_2, ...'...
    url_kwargs_str = str(url_kwargs_str) # just make sure it's a string
    url_kwargs = {}
    if url_kwargs_str:
        for pair in url_kwargs_str.split(','):
            k, v = pair.strip().split(':')
            k = k.strip()
            v = v.strip()
        if k and v:
            url_kwargs[k] = v
    return url_kwargs

def reverse_url(n, url_match):
    url_kwargs = n.context.get('url_kwargs', '')
    kwargs_dict = {}
    if url_kwargs:
        kwargs_dict = get_url_kwargs(url_kwargs)
    # if there is a required kwarg defined in the node-context but it's empty
    # and it's in the url_match present, update dict
    args_dict = {}
    if url_match:
        args_dict = url_match.args
        for k in kwargs_dict.keys():
            v = url_match.kwargs.get(k, None)
            if v:
                kwargs_dict[k] = v
    url = reverse(n.url_name, args=url_match.args, kwargs=kwargs_dict)
    return url

def is_active(request, link_url):
    """ check if the corresponding parts of the given link and the request.path
    match (active).
    For example, if requeest.path is '/bla/bli/blu/' and link_url is '/bla/',
    '/bla/bli' or /bla/bli/blu/' is_active should return True
    """
    if not hasattr(request, 'path'):
        return False
    link_parts = link_url.strip('/').split('/')
    request_parts = (request.path).strip('/').split('/')
    if len(request_parts) < len(link_parts):
        return False
    return '/'.join(link_parts) == '/'.join(request_parts[0:len(link_parts)])

<|endoftext|>


<commit_before>from bears.markdown.MarkdownBear import MarkdownBear
from coalib.testing.LocalBearTestHelper import verify_local_bear

test_file1 = """1. abc
1. def
"""

test_file2 = """1. abc
2. def
"""

test_file3 = """1. abcdefghijklm
2. nopqrstuvwxyz
"""

MarkdownBearTest = verify_local_bear(MarkdownBear,
                                     valid_files=(test_file2,),
                                     invalid_files=(test_file1,))

MarkdownBearConfigsTest = verify_local_bear(
    MarkdownBear,
    valid_files=(test_file1,),
    invalid_files=(test_file2,),
    settings={'list_increment': False})

MarkdownBearMaxLineLengthSettingTest = verify_local_bear(
    MarkdownBear,
    valid_files=(test_file2,),
    invalid_files=(test_file3,),
    settings={'max_line_length': 10})
<commit_msg>MarkdownBear: Add test to check message for error<commit_after>import unittest
from queue import Queue

from bears.markdown.MarkdownBear import MarkdownBear
from coalib.testing.BearTestHelper import generate_skip_decorator
from coalib.testing.LocalBearTestHelper import verify_local_bear, execute_bear
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
from coalib.settings.Section import Section
from coalib.settings.Setting import Setting
from coala_utils.ContextManagers import prepare_file

test_file1 = """1. abc
1. def
"""

test_file2 = """1. abc
2. def
"""

test_file3 = """1. abcdefghijklm
2. nopqrstuvwxyz
"""

MarkdownBearTest = verify_local_bear(MarkdownBear,
                                     valid_files=(test_file2,),
                                     invalid_files=(test_file1,))

MarkdownBearConfigsTest = verify_local_bear(
    MarkdownBear,
    valid_files=(test_file1,),
    invalid_files=(test_file2,),
    settings={'list_increment': False})

MarkdownBearMaxLineLengthSettingTest = verify_local_bear(
    MarkdownBear,
    valid_files=(test_file2,),
    invalid_files=(test_file3,),
    settings={'max_line_length': 10})

@generate_skip_decorator(MarkdownBear)
class MarkdownBearMaxLineLengthMessageTest(unittest.TestCase):

    def setUp(self):
        self.section = Section('name')
        self.uut = MarkdownBear(self.section, Queue())

    def test_invalid_message(self):
        content = test_file3.splitlines()
        self.section.append(Setting('max_line_length', '10'))
        with prepare_file(content, None) as (file, fname):
            with execute_bear(self.uut, fname, file) as results:
                self.assertEqual(results[0].message,
                                 'Line must be at most 10 characters'
                                 '  maximum-line-length  remark-lint')
                self.assertEqual(results[0].severity, RESULT_SEVERITY.NORMAL)
<|endoftext|>


<commit_before>import sys
import os
import time
import requests
import json
import threading
import signal
import logging
import argparse
import traceback
from flask import Flask, request, jsonify
from time import gmtime, strftime

log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
logging.basicConfig(stream=sys.stderr)

sys.path.append("/home/user/stinger/src/py/")
os.environ['STINGER_LIB_PATH'] = "/home/user/stinger/build/lib/"

import stinger.stinger_net as sn
import stinger.stinger_core as sc

# Value of -1 in either field disables its use as a threshold
TIMEOUT_SECS = 1
BATCH_THRESHOLD = 500

STINGER_HOST = 'localhost'
STINGER_PORT = '8088'

app = Flask(__name__)

@app.route('/insert', methods=['POST'])
def insert():
  global q
  global cur
  global con

  exec_time = time.time()
  if not request.json:
    abort(400)

  # grab the lock
  counter_lock.acquire()

  try:
    data = request.json
    if isinstance(data["edges"], list):
      edges = data["edges"]
      print "Received batch of size", len(edges), 'at', strftime("%Y%m%d%H%M%S", gmtime()),""
      for x in edges:
        try:
          source = str(x["src"])
          destination = str(x["dest"])
          edge_type = x["type"]
          timestamp = int(x["time"])
          s.add_insert(source, destination, edge_type, ts=timestamp)
        #   print "added edge", source, destination, edge_type, timestamp
        except Exception as e:
          print(traceback.format_exc())
          pass

    # send immediately if the batch is now large
    current_batch_size = s.insertions_count + s.deletions_count
    if current_batch_size > BATCH_THRESHOLD and BATCH_THRESHOLD > 0:
      s.send_batch()
      print "Sending  batch of size", current_batch_size, 'at', strftime("%Y%m%d%H%M%S", gmtime()),""

  except:
    print(traceback.format_exc())
    return jsonify({"error": "invalid input"})

  # end critical section
  finally:
    counter_lock.release()
  exec_time = time.time() - exec_time
  return jsonify({"status": "success", "time": exec_time, "size": current_batch_size}), 201

@app.route('/jsonrpc', methods=['POST'])
def stinger_proxy():
  return stingerRPC(request.json)

@app.route('/algorithms', methods=['GET'])
def get_algs():
  return stingerRPC({"jsonrpc": "2.0", "method": "get_algorithms", "id": 1})

@app.route('/health', methods=['GET'])
def stinger_health():
  return stingerRPC({"jsonrpc": "2.0", "method": "get_server_health", "id": 1})

def stingerRPC(payload):
  try:
    r = requests.post('http://'+ STINGER_HOST +':'+ STINGER_PORT +'/jsonrpc', data=json.dumps(payload))
  except:
    print(traceback.format_exc())
    return jsonify({"error": "JSON-RPC down"})
  return r.text

#
# Initialize the connection to STINGER server
#
def connect(undirected=False,strings=True):
  global s
  s = sn.StingerStream(STINGER_HOST, 10102, strings, undirected)
  directedness = 'UNdirected' if undirected else 'directed'
  print "Inserting into",directedness,"graph"

  global counter_lock
  counter_lock = threading.Lock()

#
# attempt to send a batch every TIMEOUT_SECS seconds
#
def sendBatch():
  current_batch_size = s.insertions_count + s.deletions_count
  if current_batch_size > 0:
    counter_lock.acquire()
    try:
      s.send_batch()
      print "Sending  batch of size", current_batch_size, 'at', strftime("%Y%m%d%H%M%S", gmtime()),""
    finally:
      counter_lock.release()

  if TIMEOUT_SECS > -1:
      global t
      t = threading.Timer(TIMEOUT_SECS, sendBatch)
      t.start()

def signal_handler(signal, frame):
  try:
    t.cancel()
  except:
    pass
  sys.exit(0)

#
# main
#
if __name__ == '__main__':
    signal.signal(signal.SIGINT, signal_handler)
    parser = argparse.ArgumentParser(description="STINGER Flask Relay Server")
    parser.add_argument('--undirected', action="store_true")
    args = parser.parse_args()
    if not 's' in globals():
      try:
        connect(args.undirected)
        print 'STINGER connection successful'
      except e as Exception:
        print str(e)
    if not 't' in globals():
      try:
        sendBatch()
        print 'STINGER timer setup successful'
      except e as Exception:
        print str(e)
    app.run(debug=True)
<commit_msg>Allow Flask port and host to be configurable as command line arguments<commit_after>import sys
import os
import time
import requests
import json
import threading
import signal
import logging
import argparse
import traceback
from flask import Flask, request, jsonify
from time import gmtime, strftime

log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
logging.basicConfig(stream=sys.stderr)

sys.path.append("/home/tgoodyear/projects/stinger/src/py/")
os.environ['STINGER_LIB_PATH'] = "/home/tgoodyear/projects/stinger/build/lib/"

import stinger.stinger_net as sn
import stinger.stinger_core as sc

# Value of -1 in either field disables its use as a threshold
TIMEOUT_SECS = 1
BATCH_THRESHOLD = 500

STINGER_HOST = 'localhost'
STINGER_PORT = '8088'

app = Flask(__name__)

@app.route('/insert', methods=['POST'])
def insert():
  global q
  global cur
  global con

  exec_time = time.time()
  if not request.json:
    abort(400)

  # grab the lock
  counter_lock.acquire()

  try:
    data = request.json
    if isinstance(data["edges"], list):
      edges = data["edges"]
      print "Received batch of size", len(edges), 'at', strftime("%Y%m%d%H%M%S", gmtime()),""
      for x in edges:
        try:
          source = str(x["src"])
          destination = str(x["dest"])
          edge_type = x["type"]
          timestamp = int(x["time"])
          s.add_insert(source, destination, edge_type, ts=timestamp)
        #   print "added edge", source, destination, edge_type, timestamp
        except Exception as e:
          print(traceback.format_exc())
          pass

    # send immediately if the batch is now large
    current_batch_size = s.insertions_count + s.deletions_count
    if current_batch_size > BATCH_THRESHOLD and BATCH_THRESHOLD > 0:
      s.send_batch()
      print "Sending  batch of size", current_batch_size, 'at', strftime("%Y%m%d%H%M%S", gmtime()),""

  except:
    print(traceback.format_exc())
    return jsonify({"error": "invalid input"})

  # end critical section
  finally:
    counter_lock.release()
  exec_time = time.time() - exec_time
  return jsonify({"status": "success", "time": exec_time, "size": current_batch_size}), 201

@app.route('/jsonrpc', methods=['POST'])
def stinger_proxy():
  return stingerRPC(request.json)

@app.route('/algorithms', methods=['GET'])
def get_algs():
  return stingerRPC({"jsonrpc": "2.0", "method": "get_algorithms", "id": 1})

@app.route('/health', methods=['GET'])
def stinger_health():
  return stingerRPC({"jsonrpc": "2.0", "method": "get_server_health", "id": 1})

def stingerRPC(payload):
  try:
    r = requests.post('http://'+ STINGER_HOST +':'+ STINGER_PORT +'/jsonrpc', data=json.dumps(payload))
  except:
    print(traceback.format_exc())
    return jsonify({"error": "JSON-RPC down"})
  return r.text

#
# Initialize the connection to STINGER server
#
def connect(undirected=False,strings=True):
  global s
  s = sn.StingerStream(STINGER_HOST, 10102, strings, undirected)
  directedness = 'UNdirected' if undirected else 'directed'
  print "Inserting into",directedness,"graph"

  global counter_lock
  counter_lock = threading.Lock()

#
# attempt to send a batch every TIMEOUT_SECS seconds
#
def sendBatch():
  current_batch_size = s.insertions_count + s.deletions_count
  if current_batch_size > 0:
    counter_lock.acquire()
    try:
      s.send_batch()
      print "Sending  batch of size", current_batch_size, 'at', strftime("%Y%m%d%H%M%S", gmtime()),""
    finally:
      counter_lock.release()

  if TIMEOUT_SECS > -1:
      global t
      t = threading.Timer(TIMEOUT_SECS, sendBatch)
      t.start()

def signal_handler(signal, frame):
  try:
    t.cancel()
  except:
    pass
  sys.exit(0)

#
# main
#
if __name__ == '__main__':
    signal.signal(signal.SIGINT, signal_handler)
    parser = argparse.ArgumentParser(description="STINGER Flask Relay Server")
    parser.add_argument('--undirected', action="store_true")
    parser.add_argument('--flask_host', default="0.0.0.0")
    parser.add_argument('--flask_port', default=5000, type=int)
    args = parser.parse_args()
    if not 's' in globals():
      try:
        connect(args.undirected)
        print 'STINGER connection successful'
      except e as Exception:
        print str(e)
    if not 't' in globals():
      try:
        sendBatch()
        print 'STINGER timer setup successful'
      except e as Exception:
        print str(e)
    app.run(debug=True,host=args.flask_host,port=args.flask_port)
<|endoftext|>


<commit_before>"""Beaker utilities"""

try:
    import thread as _thread
    import threading as _threading
except ImportError:
    import dummy_thread as _thread
    import dummy_threading as _threading

from datetime import datetime, timedelta
import os
import string
import types
import weakref
import warnings
import sys

py3k = getattr(sys, 'py3kwarning', False) or sys.version_info >= (3, 0)
py24 = sys.version_info[0:2] <= (2,4)
jython = sys.platform.startswith('java')

if py3k or jython:
    import pickle
else:
    import cPickle as pickle

from beaker.converters import asbool
from threading import local as _tlocal

__all__  = ["ThreadLocal", "Registry", "WeakValuedRegistry", "SyncDict",
            "encoded_path", "verify_directory"]

def verify_directory(dir):
    """verifies and creates a directory.  tries to
    ignore collisions with other threads and processes."""

    tries = 0
    while not os.access(dir, os.F_OK):
        try:
            tries += 1
            os.makedirs(dir)
        except:
            if tries > 5:
                raise

def deprecated(message):
    def wrapper(fn):
        def deprecated_method(*args, **kargs):
            warnings.warn(message, DeprecationWarning, 2)
            return fn(*args, **kargs)
        # TODO: use decorator ?  functools.wrapper ?
        deprecated_method.__name__ = fn.__name__
        deprecated_method.__doc__ = "%s\n\n%s" % (message, fn.__doc__)
        return deprecated_method
    return wrapper

class ThreadLocal(object):
    """stores a value on a per-thread basis"""

    __slots__ = '_tlocal'

    def __init__(self):
        self._tlocal = _tlocal()

    def put(self, value):
        self._tlocal.value = value

    def has(self):
        return hasattr(self._tlocal, 'value')

    def get(self, default=None):
        return getattr(self._tlocal, 'value', default)

    def remove(self):
        del self._tlocal.value

class SyncDict(object):
    """
    An efficient/threadsafe singleton map algorithm, a.k.a.
    "get a value based on this key, and create if not found or not
    valid" paradigm:

        exists && isvalid ? get : create

    Designed to work with weakref dictionaries to expect items
    to asynchronously disappear from the dictionary.

    Use python 2.3.3 or greater !  a major bug was just fixed in Nov.
    2003 that was driving me nuts with garbage collection/weakrefs in
    this section.

    """
    def __init__(self):
        self.mutex = _thread.allocate_lock()
        self.dict = {}

    def get(self, key, createfunc, *args, **kwargs):
        try:
            if self.has_key(key):
                return self.dict[key]
            else:
                return self.sync_get(key, createfunc, *args, **kwargs)
        except KeyError:
            return self.sync_get(key, createfunc, *args, **kwargs)

    def sync_get(self, key, createfunc, *args, **kwargs):
        self.mutex.acquire()
        try:
            try:
                if self.has_key(key):
                    return self.dict[key]
                else:
                    return self._create(key, createfunc, *args, **kwargs)
            except KeyError:
                return self._create(key, createfunc, *args, **kwargs)
        finally:
            self.mutex.release()

    def _create(self, key, createfunc, *args, **kwargs):
        self[key] = obj = createfunc(*args, **kwargs)
        return obj

    def has_key(self, key):
        return self.dict.has_key(key)

    def __contains__(self, key):
        return self.dict.__contains__(key)
    def __getitem__(self, key):
        return self.dict.__getitem__(key)
    def __setitem__(self, key, value):
        self.dict.__setitem__(key, value)
    def __delitem__(self, key):
        return self.dict.__delitem__(key)
    def clear(self):
        self.dict.clear()

class WeakValuedRegistry(SyncDict):
    def __init__(self):
        self.mutex = _threading.RLock()
        self.dict = weakref.WeakValueDictionary()

sha1 = None
def encoded_path(root, identifiers, extension = ".enc", depth = 3,
                 digest_filenames=True):

    """Generate a unique file-accessible path from the given list of
    identifiers starting at the given root directory."""
    ident = "_".join(identifiers)

    global sha1
    if sha1 is None:
        from beaker.crypto import sha1

    if digest_filenames:
        if py3k:
            ident = sha1(ident.encode('utf-8')).hexdigest()
        else:
            ident = sha1(ident).hexdigest()

    ident = os.path.basename(ident)

    tokens = []
    for d in range(1, depth):
        tokens.append(ident[0:d])

    dir = os.path.join(root, *tokens)
    verify_directory(dir)

    return os.path.join(dir, ident + extension)

def verify_options(opt, types, error):
    if not isinstance(opt, types):
        if not isinstance(types, tuple):
            types = (types,)
        coerced = False
        for typ in types:
            try:
                if typ in (list, tuple):
                    opt = [x.strip() for x in opt.split(',')]
                else:
                    if typ == bool:
                        typ = asbool
                    opt = typ(opt)
                coerced = True
            except:
                pass
            if coerced:
                break
        if not coerced:
            raise Exception(error)
    elif isinstance(opt, str) and not opt.strip():
        raise Exception("Empty strings are invalid for: %s" % error)
    return opt

def verify_rules(params, ruleset):
    for key, types, message in ruleset:
        if key in params:
            params[key] = verify_options(params[key], types, message)
    return params

def coerce_session_params(params):
    rules = [
        ('data_dir', (str, types.NoneType), "data_dir must be a string "
         "referring to a directory."),
        ('lock_dir', (str, types.NoneType), "lock_dir must be a string referring to a "
         "directory."),
        ('type', (str, types.NoneType), "Session type must be a string."),
        ('cookie_expires', (bool, datetime, timedelta), "Cookie expires was "
         "not a boolean, datetime, or timedelta instance."),
        ('cookie_domain', (str, types.NoneType), "Cookie domain must be a "
         "string."),
        ('id', (str,), "Session id must be a string."),
        ('key', (str,), "Session key must be a string."),
        ('secret', (str, types.NoneType), "Session secret must be a string."),
        ('validate_key', (str, types.NoneType), "Session encrypt_key must be "
         "a string."),
        ('encrypt_key', (str, types.NoneType), "Session validate_key must be "
         "a string."),
        ('secure', (bool, types.NoneType), "Session secure must be a boolean."),
        ('timeout', (int, types.NoneType), "Session timeout must be an "
         "integer."),
        ('auto', (bool, types.NoneType), "Session is created if accessed."),
    ]
    return verify_rules(params, rules)

def coerce_cache_params(params):
    rules = [
        ('data_dir', (str, types.NoneType), "data_dir must be a string "
         "referring to a directory."),
        ('lock_dir', (str, types.NoneType), "lock_dir must be a string referring to a "
         "directory."),
        ('type', (str,), "Cache type must be a string."),
        ('enabled', (bool, types.NoneType), "enabled must be true/false "
         "if present."),
        ('expire', (int, types.NoneType), "expire must be an integer representing "
         "how many seconds the cache is valid for"),
        ('regions', (list, tuple, types.NoneType), "Regions must be a "
         "comma seperated list of valid regions")
    ]
    return verify_rules(params, rules)

def parse_cache_config_options(config, include_defaults=True):
    """Parse configuration options and validate for use with the
    CacheManager"""

    # Load default cache options
    if include_defaults:
        options= dict(type='memory', data_dir=None, expire=None,
                           log_file=None)
    else:
        options = {}
    for key, val in config.iteritems():
        if key.startswith('beaker.cache.'):
            options[key[13:]] = val
        if key.startswith('cache.'):
            options[key[6:]] = val
    coerce_cache_params(options)

    # Set cache to enabled if not turned off
    if 'enabled' not in options:
        options['enabled'] = True

    # Configure region dict if regions are available
    regions = options.pop('regions', None)
    if regions:
        region_configs = {}
        for region in regions:
            # Setup the default cache options
            region_options = dict(data_dir=options.get('data_dir'),
                                  lock_dir=options.get('lock_dir'),
                                  type=options.get('type'),
                                  enabled=options['enabled'],
                                  expire=options.get('expire'))
            region_len = len(region) + 1
            for key in options.keys():
                if key.startswith('%s.' % region):
                    region_options[key[region_len:]] = options.pop(key)
            coerce_cache_params(region_options)
            region_configs[region] = region_options
        options['cache_regions'] = region_configs
    return options

def func_namespace(func):
    """Generates a unique namespace for a function"""
    kls = None
    if hasattr(func, 'im_func'):
        kls = func.im_class
        func = func.im_func

    if kls:
        return '%s.%s' % (kls.__module__, kls.__name__)
    else:
        return '%s.%s' % (func.__module__, func.__name__)
<commit_msg>Cleaner python check<commit_after>"""Beaker utilities"""

try:
    import thread as _thread
    import threading as _threading
except ImportError:
    import dummy_thread as _thread
    import dummy_threading as _threading

from datetime import datetime, timedelta
import os
import string
import types
import weakref
import warnings
import sys

py3k = getattr(sys, 'py3kwarning', False) or sys.version_info >= (3, 0)
py24 = sys.version_info < (2,5)
jython = sys.platform.startswith('java')

if py3k or jython:
    import pickle
else:
    import cPickle as pickle

from beaker.converters import asbool
from threading import local as _tlocal

__all__  = ["ThreadLocal", "Registry", "WeakValuedRegistry", "SyncDict",
            "encoded_path", "verify_directory"]

def verify_directory(dir):
    """verifies and creates a directory.  tries to
    ignore collisions with other threads and processes."""

    tries = 0
    while not os.access(dir, os.F_OK):
        try:
            tries += 1
            os.makedirs(dir)
        except:
            if tries > 5:
                raise

def deprecated(message):
    def wrapper(fn):
        def deprecated_method(*args, **kargs):
            warnings.warn(message, DeprecationWarning, 2)
            return fn(*args, **kargs)
        # TODO: use decorator ?  functools.wrapper ?
        deprecated_method.__name__ = fn.__name__
        deprecated_method.__doc__ = "%s\n\n%s" % (message, fn.__doc__)
        return deprecated_method
    return wrapper

class ThreadLocal(object):
    """stores a value on a per-thread basis"""

    __slots__ = '_tlocal'

    def __init__(self):
        self._tlocal = _tlocal()

    def put(self, value):
        self._tlocal.value = value

    def has(self):
        return hasattr(self._tlocal, 'value')

    def get(self, default=None):
        return getattr(self._tlocal, 'value', default)

    def remove(self):
        del self._tlocal.value

class SyncDict(object):
    """
    An efficient/threadsafe singleton map algorithm, a.k.a.
    "get a value based on this key, and create if not found or not
    valid" paradigm:

        exists && isvalid ? get : create

    Designed to work with weakref dictionaries to expect items
    to asynchronously disappear from the dictionary.

    Use python 2.3.3 or greater !  a major bug was just fixed in Nov.
    2003 that was driving me nuts with garbage collection/weakrefs in
    this section.

    """
    def __init__(self):
        self.mutex = _thread.allocate_lock()
        self.dict = {}

    def get(self, key, createfunc, *args, **kwargs):
        try:
            if self.has_key(key):
                return self.dict[key]
            else:
                return self.sync_get(key, createfunc, *args, **kwargs)
        except KeyError:
            return self.sync_get(key, createfunc, *args, **kwargs)

    def sync_get(self, key, createfunc, *args, **kwargs):
        self.mutex.acquire()
        try:
            try:
                if self.has_key(key):
                    return self.dict[key]
                else:
                    return self._create(key, createfunc, *args, **kwargs)
            except KeyError:
                return self._create(key, createfunc, *args, **kwargs)
        finally:
            self.mutex.release()

    def _create(self, key, createfunc, *args, **kwargs):
        self[key] = obj = createfunc(*args, **kwargs)
        return obj

    def has_key(self, key):
        return self.dict.has_key(key)

    def __contains__(self, key):
        return self.dict.__contains__(key)
    def __getitem__(self, key):
        return self.dict.__getitem__(key)
    def __setitem__(self, key, value):
        self.dict.__setitem__(key, value)
    def __delitem__(self, key):
        return self.dict.__delitem__(key)
    def clear(self):
        self.dict.clear()

class WeakValuedRegistry(SyncDict):
    def __init__(self):
        self.mutex = _threading.RLock()
        self.dict = weakref.WeakValueDictionary()

sha1 = None
def encoded_path(root, identifiers, extension = ".enc", depth = 3,
                 digest_filenames=True):

    """Generate a unique file-accessible path from the given list of
    identifiers starting at the given root directory."""
    ident = "_".join(identifiers)

    global sha1
    if sha1 is None:
        from beaker.crypto import sha1

    if digest_filenames:
        if py3k:
            ident = sha1(ident.encode('utf-8')).hexdigest()
        else:
            ident = sha1(ident).hexdigest()

    ident = os.path.basename(ident)

    tokens = []
    for d in range(1, depth):
        tokens.append(ident[0:d])

    dir = os.path.join(root, *tokens)
    verify_directory(dir)

    return os.path.join(dir, ident + extension)

def verify_options(opt, types, error):
    if not isinstance(opt, types):
        if not isinstance(types, tuple):
            types = (types,)
        coerced = False
        for typ in types:
            try:
                if typ in (list, tuple):
                    opt = [x.strip() for x in opt.split(',')]
                else:
                    if typ == bool:
                        typ = asbool
                    opt = typ(opt)
                coerced = True
            except:
                pass
            if coerced:
                break
        if not coerced:
            raise Exception(error)
    elif isinstance(opt, str) and not opt.strip():
        raise Exception("Empty strings are invalid for: %s" % error)
    return opt

def verify_rules(params, ruleset):
    for key, types, message in ruleset:
        if key in params:
            params[key] = verify_options(params[key], types, message)
    return params

def coerce_session_params(params):
    rules = [
        ('data_dir', (str, types.NoneType), "data_dir must be a string "
         "referring to a directory."),
        ('lock_dir', (str, types.NoneType), "lock_dir must be a string referring to a "
         "directory."),
        ('type', (str, types.NoneType), "Session type must be a string."),
        ('cookie_expires', (bool, datetime, timedelta), "Cookie expires was "
         "not a boolean, datetime, or timedelta instance."),
        ('cookie_domain', (str, types.NoneType), "Cookie domain must be a "
         "string."),
        ('id', (str,), "Session id must be a string."),
        ('key', (str,), "Session key must be a string."),
        ('secret', (str, types.NoneType), "Session secret must be a string."),
        ('validate_key', (str, types.NoneType), "Session encrypt_key must be "
         "a string."),
        ('encrypt_key', (str, types.NoneType), "Session validate_key must be "
         "a string."),
        ('secure', (bool, types.NoneType), "Session secure must be a boolean."),
        ('timeout', (int, types.NoneType), "Session timeout must be an "
         "integer."),
        ('auto', (bool, types.NoneType), "Session is created if accessed."),
    ]
    return verify_rules(params, rules)

def coerce_cache_params(params):
    rules = [
        ('data_dir', (str, types.NoneType), "data_dir must be a string "
         "referring to a directory."),
        ('lock_dir', (str, types.NoneType), "lock_dir must be a string referring to a "
         "directory."),
        ('type', (str,), "Cache type must be a string."),
        ('enabled', (bool, types.NoneType), "enabled must be true/false "
         "if present."),
        ('expire', (int, types.NoneType), "expire must be an integer representing "
         "how many seconds the cache is valid for"),
        ('regions', (list, tuple, types.NoneType), "Regions must be a "
         "comma seperated list of valid regions")
    ]
    return verify_rules(params, rules)

def parse_cache_config_options(config, include_defaults=True):
    """Parse configuration options and validate for use with the
    CacheManager"""

    # Load default cache options
    if include_defaults:
        options= dict(type='memory', data_dir=None, expire=None,
                           log_file=None)
    else:
        options = {}
    for key, val in config.iteritems():
        if key.startswith('beaker.cache.'):
            options[key[13:]] = val
        if key.startswith('cache.'):
            options[key[6:]] = val
    coerce_cache_params(options)

    # Set cache to enabled if not turned off
    if 'enabled' not in options:
        options['enabled'] = True

    # Configure region dict if regions are available
    regions = options.pop('regions', None)
    if regions:
        region_configs = {}
        for region in regions:
            # Setup the default cache options
            region_options = dict(data_dir=options.get('data_dir'),
                                  lock_dir=options.get('lock_dir'),
                                  type=options.get('type'),
                                  enabled=options['enabled'],
                                  expire=options.get('expire'))
            region_len = len(region) + 1
            for key in options.keys():
                if key.startswith('%s.' % region):
                    region_options[key[region_len:]] = options.pop(key)
            coerce_cache_params(region_options)
            region_configs[region] = region_options
        options['cache_regions'] = region_configs
    return options

def func_namespace(func):
    """Generates a unique namespace for a function"""
    kls = None
    if hasattr(func, 'im_func'):
        kls = func.im_class
        func = func.im_func

    if kls:
        return '%s.%s' % (kls.__module__, kls.__name__)
    else:
        return '%s.%s' % (func.__module__, func.__name__)
<|endoftext|>


<commit_before>#! /usr/bin/env pytho

# ======================================================================
# 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/>.
# ======================================================================

from __future__ import print_function

import random
import unittest
import sys

import numpy as np
from numpy.linalg import norm

import ase.io as io
from ase.constraints import StrainFilter, UnitCellFilter
from ase.lattice.compounds import B1, B2, L1_0, L1_2
from ase.lattice.cubic import FaceCenteredCubic
from ase.lattice.hexagonal import HexagonalClosedPacked
from ase.optimize import FIRE
from ase.units import GPa
from ase.phonons import Phonons

import matscipytest
from matscipy.calculators.pair_potential import PairPotential, LennardJonesCut, LennardJonesQuadratic
import matscipy.calculators.pair_potential as calculator
from matscipy.elasticity import fit_elastic_constants, Voigt_6x6_to_cubic

###

class TestPairPotentialCalculator(matscipytest.MatSciPyTestCase):

    tol = 1e-4

    def test_forces(self):
        for calc in [PairPotential({(1, 1): LennardJonesQuadratic(1, 1, 3), (1, 2): LennardJonesQuadratic(1.5, 0.8, 2.4), (2, 2): LennardJonesQuadratic(0.5, 0.88, 2.64)})]:
            a = io.read('KA256.xyz')
            a.center(vacuum=5.0)
            a.set_calculator(calc)
            f = a.get_forces()
            fn = calc.calculate_numerical_forces(a, d=0.0001)
            self.assertArrayAlmostEqual(f, fn, tol=self.tol)

    def test_symmetry_dense(self):
        for calc in [{(1, 1): LennardJonesQuadratic(1, 1, 3), (1, 2): LennardJonesQuadratic(1.5, 0.8, 2.4), (2, 2): LennardJonesQuadratic(0.5, 0.88, 2.64)}]:
            a = io.read('KA256_Min.xyz')
            a.center(vacuum=5.0)
            b = calculator.PairPotential(calc)
            H = b.calculate_hessian_matrix(a, "dense")
            self.assertArrayAlmostEqual(np.sum(np.abs(H-H.T)), 0, tol=0)

    def test_hessian(self):
        for calc in [{(1, 1): LennardJonesQuadratic(1, 1, 3), (1, 2): LennardJonesQuadratic(1.5, 0.8, 2.4), (2, 2): LennardJonesQuadratic(0.5, 0.88, 2.64)}]:
            atoms = io.read("KA256_Min.xyz")
            atoms.center(vacuum=5.0)
            b = calculator.PairPotential(calc)
            H_analytical = b.calculate_hessian_matrix(atoms, "dense")
            # Numerical
            ph = Phonons(atoms, b, supercell=(1, 1, 1), delta=0.001)
            ph.run()
            ph.read(acoustic=False)
            ph.clean()
            H_numerical = ph.get_force_constant()[0, :, :]
            self.assertArrayAlmostEqual(H_analytical, H_numerical, tol=0.03)

    def test_hessian_split(self)
    for calc in [{(1, 1): LennardJonesQuadratic(1, 1, 3), (1, 2): LennardJonesQuadratic(1.5, 0.8, 2.4), (2, 2): LennardJonesQuadratic(0.5, 0.88, 2.64)}]:
            atoms = io.read("KA256_Min.xyz")
            atoms.center(vacuum=5.0)
            b = calculator.PairPotential(calc)
            H_full = b.calculate_hessian_matrix(atoms, "dense")
            H_0to128 = b.calculate_hessian_matrix(atoms, "dense", limits=[0,128])
            H_128to256 = b.calculate_hessian_matrix(atoms, "dense", limits=[0,256])
            self.assertArrayAlmostEqual(np.sum(np.sum(H_full-H_0to128-H_128to256,axis=1)), 0, tol=0)

###

if __name__ == '__main__':
    unittest.main()
<commit_msg>Cosmetics.<commit_after>#! /usr/bin/env pytho

# ======================================================================
# 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/>.
# ======================================================================

from __future__ import print_function

import random
import unittest
import sys

import numpy as np
from numpy.linalg import norm

import ase.io as io
from ase.constraints import StrainFilter, UnitCellFilter
from ase.lattice.compounds import B1, B2, L1_0, L1_2
from ase.lattice.cubic import FaceCenteredCubic
from ase.lattice.hexagonal import HexagonalClosedPacked
from ase.optimize import FIRE
from ase.units import GPa
from ase.phonons import Phonons

import matscipytest
from matscipy.calculators.pair_potential import PairPotential, LennardJonesCut, LennardJonesQuadratic
import matscipy.calculators.pair_potential as calculator
from matscipy.elasticity import fit_elastic_constants, Voigt_6x6_to_cubic

###

class TestPairPotentialCalculator(matscipytest.MatSciPyTestCase):

    tol = 1e-4

    def test_forces(self):
        for calc in [PairPotential({(1, 1): LennardJonesQuadratic(1, 1, 3), (1, 2): LennardJonesQuadratic(1.5, 0.8, 2.4), (2, 2): LennardJonesQuadratic(0.5, 0.88, 2.64)})]:
            a = io.read('KA256.xyz')
            a.center(vacuum=5.0)
            a.set_calculator(calc)
            f = a.get_forces()
            fn = calc.calculate_numerical_forces(a, d=0.0001)
            self.assertArrayAlmostEqual(f, fn, tol=self.tol)

    def test_symmetry_dense(self):
        for calc in [{(1, 1): LennardJonesQuadratic(1, 1, 3), (1, 2): LennardJonesQuadratic(1.5, 0.8, 2.4), (2, 2): LennardJonesQuadratic(0.5, 0.88, 2.64)}]:
            a = io.read('KA256_Min.xyz')
            a.center(vacuum=5.0)
            b = calculator.PairPotential(calc)
            H = b.calculate_hessian_matrix(a, "dense")
            self.assertArrayAlmostEqual(np.sum(np.abs(H-H.T)), 0, tol=0)

    def test_hessian(self):
        for calc in [{(1, 1): LennardJonesQuadratic(1, 1, 3), (1, 2): LennardJonesQuadratic(1.5, 0.8, 2.4), (2, 2): LennardJonesQuadratic(0.5, 0.88, 2.64)}]:
            atoms = io.read("KA256_Min.xyz")
            atoms.center(vacuum=5.0)
            b = calculator.PairPotential(calc)
            H_analytical = b.calculate_hessian_matrix(atoms, "dense")
            # Numerical
            ph = Phonons(atoms, b, supercell=(1, 1, 1), delta=0.001)
            ph.run()
            ph.read(acoustic=False)
            ph.clean()
            H_numerical = ph.get_force_constant()[0, :, :]
            self.assertArrayAlmostEqual(H_analytical, H_numerical, tol=0.03)

    def test_hessian_split(self)
    for calc in [{(1, 1): LennardJonesQuadratic(1, 1, 3), (1, 2): LennardJonesQuadratic(1.5, 0.8, 2.4), (2, 2): LennardJonesQuadratic(0.5, 0.88, 2.64)}]:
        atoms = io.read("KA256_Min.xyz")
        atoms.center(vacuum=5.0)
        b = calculator.PairPotential(calc)
        H_full = b.calculate_hessian_matrix(atoms, "dense")
        H_0to128 = b.calculate_hessian_matrix(atoms, "dense", limits=[0, 128])
        H_128to256 = b.calculate_hessian_matrix(
            atoms, "dense", limits=[0, 256])
        self.assertArrayAlmostEqual(
            np.sum(np.sum(H_full-H_0to128-H_128to256, axis=1)), 0, tol=0)

###

if __name__ == '__main__':
    unittest.main()
<|endoftext|>