<commit_before>#!/bin/env python

import discord
from discord.ext import commands

class Admin:
    def __init__(self, bot):
        self.bot = bot

    async def on_member_join(self, ctx):
        await ctx.send('Welcome to the server! For a complete list of commands, type `.help`.')

    @commands.command(hidden=True)
    @commands.is_owner()
    async def load(self, ctx, extension_name: str):
        """ Loads an extension """
        try:
            self.bot.load_extension(extension_name)
        except (AttributeError, ImportError) as e:
            return await ctx.send(f'```py\n{type(e).__name__}: {str(e)}\n```')
        await ctx.send('Cog `{}` loaded successfully.'.format(extension_name))

    @commands.command(hidden=True)
    @commands.is_owner()
    async def unload(self, ctx, extension_name: str):
        """ Unloads an extension. """
        self.bot.unload_extension(extension_name)
        await ctx.send('Cog `{}` has been unloaded.'.format(extension_name))

    @commands.command(name='reload', aliases='r', hidden=True)
    @commands.is_owner()
    async def _reload(self, ctx, extension_name: str):
        """ Reloads an extension """
        try:
            self.bot.unload_extension(extension_name)
            self.bot.load_extension(extension_name)
        except Exception as e:
            return await ctx.send(f'```py\n{type(e).__name__}: {str(e)}\n```')
        await ctx.send(f'Cog `{extension_name}` has been reloaded.')

    @commands.command(name='reload_all', aliases=['ra'], hidden=True)
    @commands.is_owner()
    async def reload_all(self, ctx):
        """ Reloads all extensions """

        # Gets cog list and pops admin cog (can't reload without it)
        ext_list = []
        for extension in self.bot.startup_extensions:
            ext_list.append(extension)
        ext_list.pop(ext_list.index('cogs.admin'))

        # Reloads all cogs
        for extension in ext_list:
            try:
                self.bot.unload_extension(extension)
                self.bot.load_extension(extension)
            except Exception as e:
                return await ctx.send(f'```py\n{type(e).__name__}: {str(e)}\n```')

        await ctx.send('All cogs have been reloaded.')

    @commands.command(name='clean', aliases=['purge'])
    @commands.has_permissions(manage_messages=True)
    async def clean_bot_messages(self, ctx, num_msg: int):
        """ Remove a given number of bot messages """

        if num_msg > 100:
            await ctx.send('Sorry, number of messages to be deleted must not exceed 100.')

        # Check so that only bot msgs are removed
        def check(message):
            return message.author.id == self.bot.user.id

        try:
            await ctx.channel.purge(check=check, limit=num_msg)
        except Exception as e:
            await ctx.send(f'Failed to delete messages.\n ```py\n{e}```')

    @commands.command(aliases=['up'])
    async def uptime(self, ctx):
        """Get current bot uptime."""
        current_time = datetime.now()
        current_time_str = current_time.strftime('%B %d %H:%M:%S')
        await ctx.send(f'Initialized `{self.start_time_str}`\nCurrent Time: `{current_time_str}`\nUptime: '\
                       f'`{str(current_time - self.start_time).split(".")[0]}`')

def setup(bot):
    bot.add_cog(Admin(bot))
<commit_msg>fix spacing and return properly in clean<commit_after>#!/bin/env python

import discord
from discord.ext import commands

class Admin:
    def __init__(self, bot):
        self.bot = bot

    async def on_member_join(self, ctx):
        await ctx.send('Welcome to the server! For a complete list of commands, type `.help`.')

    @commands.command(hidden=True)
    @commands.is_owner()
    async def load(self, ctx, extension_name: str):
        """ Loads an extension """
        try:
            self.bot.load_extension(extension_name)
        except (AttributeError, ImportError) as e:
            return await ctx.send(f'```py\n{type(e).__name__}: {str(e)}\n```')
        await ctx.send('Cog `{}` loaded successfully.'.format(extension_name))

    @commands.command(hidden=True)
    @commands.is_owner()
    async def unload(self, ctx, extension_name: str):
        """ Unloads an extension. """
        self.bot.unload_extension(extension_name)
        await ctx.send('Cog `{}` has been unloaded.'.format(extension_name))

    @commands.command(name='reload', aliases='r', hidden=True)
    @commands.is_owner()
    async def _reload(self, ctx, extension_name: str):
        """ Reloads an extension """
        try:
            self.bot.unload_extension(extension_name)
            self.bot.load_extension(extension_name)
        except Exception as e:
            return await ctx.send(f'```py\n{type(e).__name__}: {str(e)}\n```')
        await ctx.send(f'Cog `{extension_name}` has been reloaded.')

    @commands.command(name='reload_all', aliases=['ra'], hidden=True)
    @commands.is_owner()
    async def reload_all(self, ctx):
        """ Reloads all extensions """

        # Gets cog list and pops admin cog (can't reload without it)
        ext_list = []
        for extension in self.bot.startup_extensions:
            ext_list.append(extension)
        ext_list.pop(ext_list.index('cogs.admin'))

        # Reloads all cogs
        for extension in ext_list:
            try:
                self.bot.unload_extension(extension)
                self.bot.load_extension(extension)
            except Exception as e:
                return await ctx.send(f'```py\n{type(e).__name__}: {str(e)}\n```')

        await ctx.send('All cogs have been reloaded.')

    @commands.command(name='clean', aliases=['purge'])
    @commands.has_permissions(manage_messages=True)
    async def clean_bot_messages(self, ctx, num_msg: int):
        """ Remove a given number of bot messages """

        if num_msg > 100:
            return await ctx.send('Sorry, number of messages to be deleted must not exceed 100.')

        # Check so that only bot msgs are removed
        def check(message):
            return message.author.id == self.bot.user.id

        try:
            await ctx.channel.purge(check=check, limit=num_msg)
        except Exception as e:
            await ctx.send(f'Failed to delete messages.\n ```py\n{e}```')

    @commands.command(aliases=['up'])
    async def uptime(self, ctx):
        """Get current bot uptime."""
        current_time = datetime.now()
        current_time_str = current_time.strftime('%B %d %H:%M:%S')
        await ctx.send(f'Initialized `{self.start_time_str}`\nCurrent Time: `{current_time_str}`\nUptime: '\
                       f'`{str(current_time - self.start_time).split(".")[0]}`')

def setup(bot):
    bot.add_cog(Admin(bot))
<|endoftext|>


<commit_before>from Display.Shape import Shape, UnitShape
import osg
from math import atan2, pi, sqrt

class Ball(UnitShape):

    @classmethod
    def name(cls):
        return gettext('Ball')

    # Cached values indexed by tesselation level
    _vertices = []
    _vertexArray = []
    _normalArray = []
    _texCoordArray = []
    _faces = []
    _faceSet = []

    @classmethod
    def _tessellate(cls):
        # Code inspired by Chapter 2 of the OpenGL Red Book.

        if not any(Ball._vertices):
            # Start with an icosahedron
            X = .525731112119133606 * 0.5
            Z = .850650808352039932 * 0.5
            newVertices = [(-X, 0.0, Z), (X, 0.0, Z), (-X, 0.0, -Z), (X, 0.0, -Z), (0.0, Z, X), (0.0, Z, -X), (0.0, -Z, X), (0.0, -Z, -X), (Z, X, 0.0), (-Z, X, 0.0), (Z, -X, 0.0), (-Z, -X, 0.0)]
            newFaces = [(0,4,1), (0,9,4), (9,5,4), (4,5,8), (4,8,1), (8,10,1), (8,3,10), (5,3,8), (5,2,3), (2,7,3), (7,10,3), (7,6,10), (7,11,6), (11,0,6), (0,1,6), (6,1,10), (9,0,11), (9,11,2), (9,2,5), (7,2,11)]
        else:
            # Subdivide each triangular face into four triangular sub-faces by finding the midpoint of each side.
            # New vertices will be added to a copy of the existing list and a completely new set of faces will be produced.
            newVertices = list(Ball._vertices[-1])
            newFaces = []
            for face in Ball._faces[-1]:
                index0 = face[0]
                vertex0 = newVertices[index0]
                index1 = face[1]
                vertex1 = newVertices[index1]
                index2 = face[2]
                vertex2 = newVertices[index2]

                # Add a new vertex between vertex 0 and vertex 1.
                vertex3 = (vertex0[0] + vertex1[0], vertex0[1] + vertex1[1], vertex0[2] + vertex1[2])
                mag = sqrt(vertex3[0] ** 2 + vertex3[1] ** 2 + vertex3[2] ** 2) * 2.0
                vertex3 = (vertex3[0] / mag, vertex3[1] / mag, vertex3[2] / mag)
                try:
                    index3 = newVertices.index(vertex3)
                except:
                    newVertices += [vertex3]
                    index3 = len(newVertices) - 1

                # Add a new vertex between vertex 1 and vertex 2.
                vertex4 = (vertex1[0] + vertex2[0], vertex1[1] + vertex2[1], vertex1[2] + vertex2[2])
                mag = sqrt(vertex4[0] ** 2 + vertex4[1] ** 2 + vertex4[2] ** 2) * 2.0
                vertex4 = (vertex4[0] / mag, vertex4[1] / mag, vertex4[2] / mag)
                try:
                    index4 = newVertices.index(vertex4)
                except:
                    newVertices += [vertex4]
                    index4 = len(newVertices) - 1

                # Add a new vertex between vertex 0 and vertex 2.
                vertex5 = (vertex0[0] + vertex2[0], vertex0[1] + vertex2[1], vertex0[2] + vertex2[2])
                mag = sqrt(vertex5[0] ** 2 + vertex5[1] ** 2 + vertex5[2] ** 2) * 2.0
                vertex5 = (vertex5[0] / mag, vertex5[1] / mag, vertex5[2] / mag)
                try:
                    index5 = newVertices.index(vertex5)
                except:
                    newVertices += [vertex5]
                    index5 = len(newVertices) - 1

                # Add the four new faces.
                newFaces += [(index0, index3, index5), (index3, index1, index4), (index3, index4, index5), (index5, index4, index2)]

        Ball._vertices += [newVertices]
        Ball._vertexArray += [Shape.vectorArrayFromList(newVertices)]

        normals = []
        texCoords = []
        for vertex in newVertices:
            normals += [(vertex[0] * 2.0, vertex[1] * 2.0, vertex[2] * 2.0)]
            texCoords += [(vertex[0] + 0.5, vertex[2] + 0.5)] #[(atan2(vertex[2], vertex[0]) / pi / 2.0 + 0.5, atan2(vertex[2], vertex[1]) / pi / 2.0 + 0.5)]
        Ball._normalArray += [Shape.vectorArrayFromList(normals)]
        Ball._texCoordArray += [Shape.vectorArrayFromList(texCoords)]

        Ball._faces += [newFaces]
        faceVertices = []
        for face in newFaces:
            faceVertices += face
        Ball._faceSet += [Shape.primitiveSetFromList(osg.PrimitiveSet.TRIANGLES, faceVertices)]

    @classmethod
    def _geometryAtTessellation(cls, tessellation):
        while len(Ball._vertices) <= tessellation:
            Ball._tessellate()
        return (Ball._vertexArray[tessellation], Ball._normalArray[tessellation], Ball._texCoordArray[tessellation], Ball._faceSet[tessellation])

    def __init__(self, tessellation = 1, *args, **keywordArgs):
        Shape.__init__(self, *args, **keywordArgs)

        self.tessellation = tessellation
        vertices, normals, texCoords, faceSet = Ball._geometryAtTessellation(tessellation)
        self.geometry().setVertexArray(vertices)
        self.geometry().setNormalArray(normals)
        self.geometry().setNormalBinding(osg.Geometry.BIND_PER_VERTEX)
        self.geometry().setTexCoordArray(0, texCoords)
        self.geometry().addPrimitiveSet(faceSet)

    def interiorBounds(self):
        halfSize = 0.5 / sqrt(3)
        return ((-halfSize, -halfSize, -halfSize), (halfSize, halfSize, halfSize))

    def intersectionPoint(self, rayOrigin, rayDirection):
        # TODO
        return None
<commit_msg>Implemented intersection method.<commit_after>from Display.Shape import Shape, UnitShape
import osg
from math import atan2, pi, sqrt

class Ball(UnitShape):

    @classmethod
    def name(cls):
        return gettext('Ball')

    # Cached values indexed by tesselation level
    _vertices = []
    _vertexArray = []
    _normalArray = []
    _texCoordArray = []
    _faces = []
    _faceSet = []

    @classmethod
    def _tessellate(cls):
        # Code inspired by Chapter 2 of the OpenGL Red Book.

        if not any(Ball._vertices):
            # Start with an icosahedron
            X = .525731112119133606 * 0.5
            Z = .850650808352039932 * 0.5
            newVertices = [(-X, 0.0, Z), (X, 0.0, Z), (-X, 0.0, -Z), (X, 0.0, -Z), (0.0, Z, X), (0.0, Z, -X), (0.0, -Z, X), (0.0, -Z, -X), (Z, X, 0.0), (-Z, X, 0.0), (Z, -X, 0.0), (-Z, -X, 0.0)]
            newFaces = [(0,4,1), (0,9,4), (9,5,4), (4,5,8), (4,8,1), (8,10,1), (8,3,10), (5,3,8), (5,2,3), (2,7,3), (7,10,3), (7,6,10), (7,11,6), (11,0,6), (0,1,6), (6,1,10), (9,0,11), (9,11,2), (9,2,5), (7,2,11)]
        else:
            # Subdivide each triangular face into four triangular sub-faces by finding the midpoint of each side.
            # New vertices will be added to a copy of the existing list and a completely new set of faces will be produced.
            newVertices = list(Ball._vertices[-1])
            newFaces = []
            for face in Ball._faces[-1]:
                index0 = face[0]
                vertex0 = newVertices[index0]
                index1 = face[1]
                vertex1 = newVertices[index1]
                index2 = face[2]
                vertex2 = newVertices[index2]

                # Add a new vertex between vertex 0 and vertex 1.
                vertex3 = (vertex0[0] + vertex1[0], vertex0[1] + vertex1[1], vertex0[2] + vertex1[2])
                mag = sqrt(vertex3[0] ** 2 + vertex3[1] ** 2 + vertex3[2] ** 2) * 2.0
                vertex3 = (vertex3[0] / mag, vertex3[1] / mag, vertex3[2] / mag)
                try:
                    index3 = newVertices.index(vertex3)
                except:
                    newVertices += [vertex3]
                    index3 = len(newVertices) - 1

                # Add a new vertex between vertex 1 and vertex 2.
                vertex4 = (vertex1[0] + vertex2[0], vertex1[1] + vertex2[1], vertex1[2] + vertex2[2])
                mag = sqrt(vertex4[0] ** 2 + vertex4[1] ** 2 + vertex4[2] ** 2) * 2.0
                vertex4 = (vertex4[0] / mag, vertex4[1] / mag, vertex4[2] / mag)
                try:
                    index4 = newVertices.index(vertex4)
                except:
                    newVertices += [vertex4]
                    index4 = len(newVertices) - 1

                # Add a new vertex between vertex 0 and vertex 2.
                vertex5 = (vertex0[0] + vertex2[0], vertex0[1] + vertex2[1], vertex0[2] + vertex2[2])
                mag = sqrt(vertex5[0] ** 2 + vertex5[1] ** 2 + vertex5[2] ** 2) * 2.0
                vertex5 = (vertex5[0] / mag, vertex5[1] / mag, vertex5[2] / mag)
                try:
                    index5 = newVertices.index(vertex5)
                except:
                    newVertices += [vertex5]
                    index5 = len(newVertices) - 1

                # Add the four new faces.
                newFaces += [(index0, index3, index5), (index3, index1, index4), (index3, index4, index5), (index5, index4, index2)]

        Ball._vertices += [newVertices]
        Ball._vertexArray += [Shape.vectorArrayFromList(newVertices)]

        normals = []
        texCoords = []
        for vertex in newVertices:
            normals += [(vertex[0] * 2.0, vertex[1] * 2.0, vertex[2] * 2.0)]
            texCoords += [(vertex[0] + 0.5, vertex[2] + 0.5)] #[(atan2(vertex[2], vertex[0]) / pi / 2.0 + 0.5, atan2(vertex[2], vertex[1]) / pi / 2.0 + 0.5)]
        Ball._normalArray += [Shape.vectorArrayFromList(normals)]
        Ball._texCoordArray += [Shape.vectorArrayFromList(texCoords)]

        Ball._faces += [newFaces]
        faceVertices = []
        for face in newFaces:
            faceVertices += face
        Ball._faceSet += [Shape.primitiveSetFromList(osg.PrimitiveSet.TRIANGLES, faceVertices)]

    @classmethod
    def _geometryAtTessellation(cls, tessellation):
        while len(Ball._vertices) <= tessellation:
            Ball._tessellate()
        return (Ball._vertexArray[tessellation], Ball._normalArray[tessellation], Ball._texCoordArray[tessellation], Ball._faceSet[tessellation])

    def __init__(self, tessellation = 1, *args, **keywordArgs):
        Shape.__init__(self, *args, **keywordArgs)

        self.tessellation = tessellation
        vertices, normals, texCoords, faceSet = Ball._geometryAtTessellation(tessellation)
        self.geometry().setVertexArray(vertices)
        self.geometry().setNormalArray(normals)
        self.geometry().setNormalBinding(osg.Geometry.BIND_PER_VERTEX)
        self.geometry().setTexCoordArray(0, texCoords)
        self.geometry().addPrimitiveSet(faceSet)

    def interiorBounds(self):
        halfSize = 0.5 / sqrt(3)
        return ((-halfSize, -halfSize, -halfSize), (halfSize, halfSize, halfSize))

    def intersectionPoint(self, rayOrigin, rayDirection):
        # Logic from <http://local.wasp.uwa.edu.au/~pbourke/geometry/sphereline/>
        a = rayDirection[0] ** 2 + rayDirection[1] ** 2 + rayDirection[2] ** 2
        b = 2 * (rayDirection[0] * rayOrigin[0] + rayDirection[1] * rayOrigin[1] + rayDirection[2] * rayOrigin[2])
        c = rayOrigin[0] ** 2 + rayOrigin[1] ** 2 + rayOrigin[2] ** 2 - 0.5 ** 2
        core = b ** 2 - 4 * a * c
        if core < 0.0:
            return None
        elif core == 0.0:
            u = -b / (2 * a)
        else:
            u1 = (-b + sqrt(core)) / (2 * a)
            u2 = (-b - sqrt(core)) / (2 * a)
            u = min(u1, u2)
        return (rayOrigin[0] + rayDirection[0] * u, rayOrigin[1] + rayDirection[1] * u, rayOrigin[2] + rayDirection[2] * u)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

__name__ = 'FixIndents'
__desc__ = 'A simple yet flexible utility to convert indentation.'
__version__ = '0.1'
__author__ = 'Mathieu D. (MatToufoutu)'

import os
import sys
from argparse import ArgumentParser

NOTOUCH_DIRS = ('.git', '.hg', '.svn')

def convert_indents(in_filename, out_filename, in_tabs, in_size, out_tabs, out_size, debug):
    in_indent_char = '\t' if in_tabs else ' '
    out_indent = '\t' if out_tabs else (' ' * out_size)
    out_file = None if debug else open(out_filename, 'w')
    if not debug:
        print "[*] Converting %s" % in_filename
    with open(in_filename) as in_file:
        for line in in_file:
            char_count = 0
            for char in line:
                if char == in_indent_char:
                    char_count += 1
                else:
                    break
            indent_count = char_count if in_tabs else (char_count / in_size)
            line = (out_indent * indent_count) + line[char_count:]
            if debug:
                print line.rstrip()
            else:
                out_file.write(line)
    if not debug:
        out_file.close()

parser = ArgumentParser(description=__desc__)
parser.add_argument('--version', action='version', version=__version__)

# Source file indentation specs
source_specs = parser.add_mutually_exclusive_group(required=True)
source_specs.add_argument('--source-tabs',
                          help='Source file uses tabs for indentation.',
                          action='store_true', )
source_specs.add_argument('--source-size',
                          help='Indents size in source file(s).',
                          type=int,
                          metavar='SIZE')

# Destination file indentation specs
target_specs = parser.add_mutually_exclusive_group(required=True)
target_specs.add_argument('--dest-tabs',
                          help='Use tabs for indentation in destination file.',
                          action='store_true')
target_specs.add_argument('--dest-size',
                          help='Indents size in destination file.',
                          type=int,
                          default=4,
                          metavar='SIZE')
# Source and target paths
parser.add_argument('--source',
                    help='File of folder from which code should be read.',
                    required=True)
output_group = parser.add_mutually_exclusive_group(required=True)
output_group.add_argument('--dest',
                          help='File or folder to which code should be written.')
output_group.add_argument('--debug',
                          help="Output converted data to console, don't write anything.",
                          action='store_true',
                          default=False)
parser.add_argument('--exclude',
                    help=(
                        'Comma-separated list of directory names to ignore. '
                        'Only useful when SOURCE is a directory.'
                    ))
parser.add_argument('--extensions',
                    help=(
                        'Comma-separated list of file extensions to convert. '
                        'Only useful when SOURCE is a directory.'
                    ))
args = parser.parse_args()

# Arguments checks
if not os.path.exists(args.source):
    print 'File or directory not found: %s' % args.source
    sys.exit(2)
# TODO: check indent size (should not be 0)

# File input
if os.path.isfile(args.source):
    convert_indents(
        args.source, args.dest,
        args.source_tabs, args.source_size,
        args.dest_tabs, args.dest_size,
        args.debug
    )

# Directory input
else:
    if args.source == args.dest:
        parser.error("SOURCE and DEST can not be the same.")
    NOTOUCH_DIRS += () if args.exclude is None else tuple(args.exclude.split(','))
    if args.debug:
        args.dest = '.'  # Prevents errors
    else:
        if not args.dest.endswith(os.path.sep):
            args.dest += os.path.sep
    for dirpath, dirnames, filenames in os.walk(args.source, followlinks=True):
        for d in NOTOUCH_DIRS:
            if d in dirnames:
                dirnames.remove(d)
        handled_files = set()
        for ext in args.extensions.split(','):
            for f in filenames:
                if f[-(len(ext)):] == ext:
                    handled_files.add(f)
        for filename in handled_files:
            in_path = os.path.abspath(os.path.join(dirpath, filename))
            out_path = os.path.abspath(os.path.join(args.dest, dirpath, filename))
            out_dir = os.path.dirname(out_path)
            if (not args.debug) and (not os.path.exists(out_dir)):
                # TODO: prompt to overwrite directory if exists
                os.mkdir(out_dir)
                # TODO: prompt to overwrite file if exists
            convert_indents(
                in_path, out_path,
                args.source_tabs, args.source_size,
                args.dest_tabs, args.dest_size,
                args.debug
            )

<commit_msg>fix bug when no extensions are provided<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

__name__ = 'FixIndents'
__desc__ = 'A simple yet flexible utility to convert indentation.'
__version__ = '0.1'
__author__ = 'Mathieu D. (MatToufoutu)'

import os
import sys
from argparse import ArgumentParser

NOTOUCH_DIRS = ('.git', '.hg', '.svn')

def convert_indents(in_filename, out_filename, in_tabs, in_size, out_tabs, out_size, debug):
    in_indent_char = '\t' if in_tabs else ' '
    out_indent = '\t' if out_tabs else (' ' * out_size)
    out_file = None if debug else open(out_filename, 'w')
    if not debug:
        print "[*] Converting %s" % in_filename
    with open(in_filename) as in_file:
        for line in in_file:
            char_count = 0
            for char in line:
                if char == in_indent_char:
                    char_count += 1
                else:
                    break
            indent_count = char_count if in_tabs else (char_count / in_size)
            line = (out_indent * indent_count) + line[char_count:]
            if debug:
                print line.rstrip()
            else:
                out_file.write(line)
    if not debug:
        out_file.close()

parser = ArgumentParser(description=__desc__)
parser.add_argument('--version', action='version', version=__version__)

# Source file indentation specs
source_specs = parser.add_mutually_exclusive_group(required=True)
source_specs.add_argument('--source-tabs',
                          help='Source file uses tabs for indentation.',
                          action='store_true', )
source_specs.add_argument('--source-size',
                          help='Indents size in source file(s).',
                          type=int,
                          metavar='SIZE')

# Destination file indentation specs
target_specs = parser.add_mutually_exclusive_group(required=True)
target_specs.add_argument('--dest-tabs',
                          help='Use tabs for indentation in destination file.',
                          action='store_true')
target_specs.add_argument('--dest-size',
                          help='Indents size in destination file.',
                          type=int,
                          default=4,
                          metavar='SIZE')
# Source and target paths
parser.add_argument('--source',
                    help='File of folder from which code should be read.',
                    required=True)
output_group = parser.add_mutually_exclusive_group(required=True)
output_group.add_argument('--dest',
                          help='File or folder to which code should be written.')
output_group.add_argument('--debug',
                          help="Output converted data to console, don't write anything.",
                          action='store_true',
                          default=False)
parser.add_argument('--exclude',
                    help=(
                        'Comma-separated list of directory names to ignore. '
                        'Only useful when SOURCE is a directory.'
                    ))
parser.add_argument('--extensions',
                    help=(
                        'Comma-separated list of specific file extensions to convert. '
                        'Only useful when SOURCE is a directory.'
                    ))
args = parser.parse_args()

# Arguments checks
if not os.path.exists(args.source):
    print 'File or directory not found: %s' % args.source
    sys.exit(2)
# TODO: check indent size (should not be 0)

# File input
if os.path.isfile(args.source):
    convert_indents(
        args.source, args.dest,
        args.source_tabs, args.source_size,
        args.dest_tabs, args.dest_size,
        args.debug
    )

# Directory input
else:
    if args.source == args.dest:
        parser.error("SOURCE and DEST can not be the same.")
    NOTOUCH_DIRS += () if args.exclude is None else tuple(args.exclude.split(','))
    if args.debug:
        args.dest = '.'  # Prevents errors
    else:
        if not args.dest.endswith(os.path.sep):
            args.dest += os.path.sep
    for dirpath, dirnames, filenames in os.walk(args.source, followlinks=True):
        for d in NOTOUCH_DIRS:
            if d in dirnames:
                dirnames.remove(d)
        if args.extensions is not None:
            handled_files = set()
            for ext in args.extensions.split(','):
                for f in filenames:
                    if f[-(len(ext)):] == ext:
                        handled_files.add(f)
        else:
            handled_files = filenames
        for filename in handled_files:
            in_path = os.path.abspath(os.path.join(dirpath, filename))
            out_path = os.path.abspath(os.path.join(args.dest, dirpath, filename))
            out_dir = os.path.dirname(out_path)
            if (not args.debug) and (not os.path.exists(out_dir)):
                # TODO: prompt to overwrite directory if exists
                os.makedirs(out_dir)
                # TODO: prompt to overwrite file if exists
            convert_indents(
                in_path, out_path,
                args.source_tabs, args.source_size,
                args.dest_tabs, args.dest_size,
                args.debug
            )

<|endoftext|>


<commit_before>#!/bin/bash -

# If you have PyPy 1.6+ in a directory called pypy alongside pox.py, we
# use it.
# Otherwise, we try to use a Python interpreter called python2.7, which
# is a good idea if you're using Python from MacPorts, for example.
# We fall back to just "python" and hope that works.

''''echo -n
export OPT="-O"
export FLG=""
if [[ "$(basename $0)" == "debug-pox.py" ]]; then
  export OPT=""
  export FLG="--debug"
fi

if [ -x pypy/bin/pypy ]; then
  exec pypy/bin/pypy $OPT "$0" $FLG "$@"
fi

if [ "$(type -P python2.7)" != "" ]; then
  exec python2.7 $OPT "$0" $FLG "$@"
fi
exec python $OPT "$0" $FLG "$@"
'''

from sts.procutils import kill_procs

from sts.topology import FatTree, BufferedPatchPanel
from sts.control_flow import Fuzzer
from sts.simulation import Simulation
from pox.lib.recoco.recoco import Scheduler

import signal
import sys
import string
import subprocess
import time
import argparse
import logging
logging.basicConfig(level=logging.DEBUG)

log = logging.getLogger("sts")

description = """
Run a simulation.
Example usage:

$ %s -c config.fat_tree
""" % (sys.argv[0])

parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
                                 description=description)

parser.add_argument('-c', '--config',
                    default='config.fat_tree',
                    help='''experiment config module in the config/ '''
                         '''subdirectory, e.g. config.fat_tree''')

args = parser.parse_args()
config = __import__(args.config, globals(), locals(), ["*"])

# For instrumenting the controller
if hasattr(config, 'controllers'):
  controller_configs = config.controllers
else:
  raise RuntimeError("Must specify controllers in config file")

# For forwarding packets
if hasattr(config, 'patch_panel_class'):
  patch_panel_class = config.patch_panel_class
else:
  # We default to a BufferedPatchPanel
  patch_panel_class = BufferedPatchPanel

# For tracking the edges and vertices in our network
if hasattr(config, 'topology_class'):
  topology_class = config.topology_class
else:
  # We default to a FatTree
  topology_class = FatTree

# For constructing the topology object
if hasattr(config, 'topology_params'):
  topology_params = config.topology_params
else:
  # We default to no parameters
  topology_params = ""

# For controlling the simulation
if hasattr(config, 'control_flow'):
  simulator = config.control_flow
else:
  # We default to a Fuzzer
  simulator = Fuzzer()

# For injecting dataplane packets into the simulated network
if hasattr(config, 'dataplane_trace') and config.dataplane_trace:
  dataplane_trace_path = config.dataplane_trace
else:
  # We default to no dataplane trace
  dataplane_trace_path = None

simulation = None
scheduler = None
def kill_scheduler():
  if scheduler and not scheduler._hasQuit:
    sys.stderr.write("Stopping Recoco Scheduler...")
    scheduler.quit()
    sys.stderr.write(" OK\n")

def handle_int(signal, frame):
  print >> sys.stderr, "Caught signal %d, stopping sdndebug" % signal
  kill_scheduler()
  if simulation is not None:
    simulation.clean_up()
  sys.exit(0)

signal.signal(signal.SIGINT, handle_int)
signal.signal(signal.SIGTERM, handle_int)

try:
  scheduler = Scheduler(daemon=True, useEpoll=False)
  simulation = Simulation(scheduler, controller_configs, topology_class,
                          topology_params, patch_panel_class,
                          dataplane_trace_path=dataplane_trace_path)
  simulator.simulate(simulation)
finally:
  if simulation is not None:
    simulation.clean_up()
  kill_scheduler()
<commit_msg>Initiate the right SnapshotService from the config (WIP)<commit_after>#!/bin/bash -

# If you have PyPy 1.6+ in a directory called pypy alongside pox.py, we
# use it.
# Otherwise, we try to use a Python interpreter called python2.7, which
# is a good idea if you're using Python from MacPorts, for example.
# We fall back to just "python" and hope that works.

''''echo -n
export OPT="-O"
export FLG=""
if [[ "$(basename $0)" == "debug-pox.py" ]]; then
  export OPT=""
  export FLG="--debug"
fi

if [ -x pypy/bin/pypy ]; then
  exec pypy/bin/pypy $OPT "$0" $FLG "$@"
fi

if [ "$(type -P python2.7)" != "" ]; then
  exec python2.7 $OPT "$0" $FLG "$@"
fi
exec python $OPT "$0" $FLG "$@"
'''

from sts.procutils import kill_procs

from sts.topology import FatTree, BufferedPatchPanel
from sts.control_flow import Fuzzer
from sts.invariant_checker import InvariantChecker
from sts.simulation import Simulation
from pox.lib.recoco.recoco import Scheduler

import signal
import sys
import string
import subprocess
import time
import argparse
import logging
logging.basicConfig(level=logging.DEBUG)

log = logging.getLogger("sts")

description = """
Run a simulation.
Example usage:

$ %s -c config.fat_tree
""" % (sys.argv[0])

parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
                                 description=description)

parser.add_argument('-c', '--config',
                    default='config.fat_tree',
                    help='''experiment config module in the config/ '''
                         '''subdirectory, e.g. config.fat_tree''')

args = parser.parse_args()
config = __import__(args.config, globals(), locals(), ["*"])

# For instrumenting the controller
if hasattr(config, 'controllers'):
  controller_configs = config.controllers
else:
  raise RuntimeError("Must specify controllers in config file")

# For forwarding packets
if hasattr(config, 'patch_panel_class'):
  patch_panel_class = config.patch_panel_class
else:
  # We default to a BufferedPatchPanel
  patch_panel_class = BufferedPatchPanel

# For tracking the edges and vertices in our network
if hasattr(config, 'topology_class'):
  topology_class = config.topology_class
else:
  # We default to a FatTree
  topology_class = FatTree

# For constructing the topology object
if hasattr(config, 'topology_params'):
  topology_params = config.topology_params
else:
  # We default to no parameters
  topology_params = ""

# For controlling the simulation
if hasattr(config, 'control_flow'):
  simulator = config.control_flow
else:
  # We default to a Fuzzer
  simulator = Fuzzer()

# For snapshot service
from sts.snapshot import *
# Read from config what controller we are using
if controller_configs[0].name == "pox":
  snapshotService = PoxSnapshotService()
if controller_configs[0].name == "floodlight":
  snapshotService = FloodlightSnapshotService()
# We default snapshotService to POX
snapshotService = PoxSnapshotService()
# Hacky way to pass the right snapshot service to the invariantchecker
simulator.invariant_checker.snapshotService = snapshotService

# For injecting dataplane packets into the simulated network
if hasattr(config, 'dataplane_trace') and config.dataplane_trace:
  dataplane_trace_path = config.dataplane_trace
else:
  # We default to no dataplane trace
  dataplane_trace_path = None

simulation = None
scheduler = None
def kill_scheduler():
  if scheduler and not scheduler._hasQuit:
    sys.stderr.write("Stopping Recoco Scheduler...")
    scheduler.quit()
    sys.stderr.write(" OK\n")

def handle_int(signal, frame):
  print >> sys.stderr, "Caught signal %d, stopping sdndebug" % signal
  kill_scheduler()
  if simulation is not None:
    simulation.clean_up()
  sys.exit(0)

signal.signal(signal.SIGINT, handle_int)
signal.signal(signal.SIGTERM, handle_int)

try:
  scheduler = Scheduler(daemon=True, useEpoll=False)
  simulation = Simulation(scheduler, controller_configs, topology_class,
                          topology_params, patch_panel_class,
                          dataplane_trace_path=dataplane_trace_path)
  simulator.simulate(simulation)
finally:
  if simulation is not None:
    simulation.clean_up()
  kill_scheduler()
<|endoftext|>


<commit_before>from django.db import migrations

from extras.choices import CustomFieldTypeChoices

def deserialize_value(field, value):
    """
    Convert serialized values to JSON equivalents.
    """
    if field.type in (CustomFieldTypeChoices.TYPE_INTEGER):
        return int(value)
    if field.type == CustomFieldTypeChoices.TYPE_BOOLEAN:
        return bool(int(value))
    if field.type == CustomFieldTypeChoices.TYPE_SELECT:
        return field._choices.get(pk=int(value)).value
    return value

def migrate_customfield_defaults(apps, schema_editor):
    """
    Copy old serialized defaults to native JSON types.
    """
    CustomField = apps.get_model('extras', 'CustomField')

    for customfield in CustomField.objects.exclude(default=''):
        try:
            if customfield.type == CustomFieldTypeChoices.TYPE_INTEGER:
                value = int(customfield.default)
            elif customfield.type == CustomFieldTypeChoices.TYPE_BOOLEAN:
                value = customfield.default in ['true', 'yes', '1']
            else:
                value = customfield.default
        except ValueError:
            raise ValueError(
                f'Invalid default value "{customfield.default}" found for {customfield.type} '
                f'custom field {customfield.name}'
            )
        CustomField.objects.filter(pk=customfield.pk).update(default2=value)

def migrate_customfieldchoices(apps, schema_editor):
    """
    Collect all CustomFieldChoices for each applicable CustomField, and save them locally as an array on
    the CustomField instance.
    """
    CustomField = apps.get_model('extras', 'CustomField')
    CustomFieldChoice = apps.get_model('extras', 'CustomFieldChoice')

    for cf in CustomField.objects.filter(type='select'):
        cf.choices = [
            cfc.value for cfc in CustomFieldChoice.objects.filter(field=cf).order_by('weight', 'value')
        ]
        cf.save()

def migrate_customfieldvalues(apps, schema_editor):
    """
    Copy data from CustomFieldValues into the custom_field_data JSON field on each model instance.
    """
    CustomFieldValue = apps.get_model('extras', 'CustomFieldValue')

    for cfv in CustomFieldValue.objects.prefetch_related('field').exclude(serialized_value=''):
        model = apps.get_model(cfv.obj_type.app_label, cfv.obj_type.model)

        # Read and update custom field value for each instance
        # TODO: This can be done more efficiently once .update() is supported for JSON fields
        cf_data = model.objects.filter(pk=cfv.obj_id).values('custom_field_data').first()
        try:
            cf_data['custom_field_data'][cfv.field.name] = deserialize_value(cfv.field, cfv.serialized_value)
        except ValueError as e:
            print(f'{cfv.field.name} ({cfv.field.type}): {cfv.serialized_value} ({cfv.pk})')
            raise e
        model.objects.filter(pk=cfv.obj_id).update(**cf_data)

def fix_filter_logic_values(apps, schema_editor):
    """
    Fix invalid values for CustomField.filter_logic (see #5376)
    """
    CustomField = apps.get_model('extras', 'CustomField')

    CustomField.objects.filter(filter_logic="integer").update(filter_logic="loose")

class Migration(migrations.Migration):

    dependencies = [
        ('circuits', '0020_custom_field_data'),
        ('dcim', '0117_custom_field_data'),
        ('extras', '0050_customfield_changes'),
        ('ipam', '0038_custom_field_data'),
        ('secrets', '0010_custom_field_data'),
        ('tenancy', '0010_custom_field_data'),
        ('virtualization', '0018_custom_field_data'),
    ]

    operations = [
        migrations.RunPython(
            code=migrate_customfield_defaults
        ),
        migrations.RunPython(
            code=migrate_customfieldchoices
        ),
        migrations.RunPython(
            code=migrate_customfieldvalues
        ),
        migrations.RunPython(
            code=fix_filter_logic_values
        ),
    ]
<commit_msg>Raise exceptions for other inconsistencies when migrating custom field data<commit_after>from django.db import migrations

from extras.choices import CustomFieldTypeChoices

def deserialize_value(field, value):
    """
    Convert serialized values to JSON equivalents.
    """
    if field.type in (CustomFieldTypeChoices.TYPE_INTEGER):
        return int(value)
    if field.type == CustomFieldTypeChoices.TYPE_BOOLEAN:
        return bool(int(value))
    if field.type == CustomFieldTypeChoices.TYPE_SELECT:
        return field._choices.get(pk=int(value)).value
    return value

def migrate_customfield_defaults(apps, schema_editor):
    """
    Copy old serialized defaults to native JSON types.
    """
    CustomField = apps.get_model('extras', 'CustomField')

    for customfield in CustomField.objects.exclude(default=''):
        try:
            if customfield.type == CustomFieldTypeChoices.TYPE_INTEGER:
                value = int(customfield.default)
            elif customfield.type == CustomFieldTypeChoices.TYPE_BOOLEAN:
                value = customfield.default in ['true', 'yes', '1']
            else:
                value = customfield.default
        except ValueError:
            raise ValueError(
                f'Invalid default value "{customfield.default}" found for {customfield.type} '
                f'custom field {customfield.name}'
            )
        CustomField.objects.filter(pk=customfield.pk).update(default2=value)

def migrate_customfieldchoices(apps, schema_editor):
    """
    Collect all CustomFieldChoices for each applicable CustomField, and save them locally as an array on
    the CustomField instance.
    """
    CustomField = apps.get_model('extras', 'CustomField')
    CustomFieldChoice = apps.get_model('extras', 'CustomFieldChoice')

    for cf in CustomField.objects.filter(type='select'):
        cf.choices = [
            cfc.value for cfc in CustomFieldChoice.objects.filter(field=cf).order_by('weight', 'value')
        ]
        cf.save()

def migrate_customfieldvalues(apps, schema_editor):
    """
    Copy data from CustomFieldValues into the custom_field_data JSON field on each model instance.
    """
    CustomFieldValue = apps.get_model('extras', 'CustomFieldValue')

    for cfv in CustomFieldValue.objects.prefetch_related('field').exclude(serialized_value=''):
        model = apps.get_model(cfv.obj_type.app_label, cfv.obj_type.model)

        # Read and update custom field value for each instance
        # TODO: This can be done more efficiently once .update() is supported for JSON fields
        cf_data = model.objects.filter(pk=cfv.obj_id).values('custom_field_data').first()
        try:
            cf_data['custom_field_data'][cfv.field.name] = deserialize_value(cfv.field, cfv.serialized_value)
        except Exception as e:
            print(f'{cfv.field.name} ({cfv.field.type}): {cfv.serialized_value} ({cfv.pk})')
            raise e
        model.objects.filter(pk=cfv.obj_id).update(**cf_data)

def fix_filter_logic_values(apps, schema_editor):
    """
    Fix invalid values for CustomField.filter_logic (see #5376)
    """
    CustomField = apps.get_model('extras', 'CustomField')

    CustomField.objects.filter(filter_logic="integer").update(filter_logic="loose")

class Migration(migrations.Migration):

    dependencies = [
        ('circuits', '0020_custom_field_data'),
        ('dcim', '0117_custom_field_data'),
        ('extras', '0050_customfield_changes'),
        ('ipam', '0038_custom_field_data'),
        ('secrets', '0010_custom_field_data'),
        ('tenancy', '0010_custom_field_data'),
        ('virtualization', '0018_custom_field_data'),
    ]

    operations = [
        migrations.RunPython(
            code=migrate_customfield_defaults
        ),
        migrations.RunPython(
            code=migrate_customfieldchoices
        ),
        migrations.RunPython(
            code=migrate_customfieldvalues
        ),
        migrations.RunPython(
            code=fix_filter_logic_values
        ),
    ]
<|endoftext|>


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

import  asyncio
import  discord
from    discord.ext         import commands

from    libraries.perms     import *
from    libraries.library   import *

class Admin:
    """Commandes d'administration et de gestion"""

    def __init__(self, bot):
        self.bot = bot
        self.voice_states = {}

    @commands.command(pass_context=True, no_pm=True)
    async def setgame(self, ctx, *, game : str):
        """Définit le jeu du bot"""

        member = ctx.message.author
        await self.bot.delete_message(ctx.message)
        if member.server_permissions.administrator == True:
            await self.bot.change_presence(game=discord.Game(name=game))
        else:
            await self.bot.say("Vous n'êtes pas administrateur")

    @commands.command(pass_context=True, no_pm=True)
    async def perms(self, ctx, *, user=None):
        """Donne les permissions du joueur choisi"""

        if user == None:
            member = ctx.message.author
        else:
            user = str(user)
            member = ctx.message.server.get_member_named(user)

        if member == None:
            await self.bot.say("{1} L'utilisateur {0} est inconnu".format(user, ctx.message.author.mention))
        else:
            tmp = await self.bot.say("Récupération des permissions...")

            perms = [
                get_perm_admin(member),
                get_perm_create_instant_invite(member),
                get_perm_kick_members(member),
                get_perm_ban_members(member),
                get_perm_manage_channels(member),
                get_perm_manage_server(member),
                get_perm_add_reactions(member),
                get_perm_send_tts_messages(member),
                get_perm_manage_messages(member),
                get_perm_mute(member),
                get_perm_deafen(member),
                get_perm_send_embed_links(member),
                get_perm_attach_files(member),
                get_perm_mention_everyone(member),
                get_perm_external_emojis(member),
                get_perm_change_nickname(member),
                get_perm_manage_nicknames(member),
                get_perm_manage_roles(member),
                get_perm_manage_webhooks(member),
                get_perm_manage_emojis(member),
                get_perm_view_audit_logs(member)
            ]

            titles = [
                "Permissions administrateur",
                "Créer invitations",
                "Éjecter les membres",
                "Bannir les membres",
                "Gérer les channels",
                "Gérer le serveur",
                "Ajouter des réactions",
                "Envoyer des messages tts",
                "Gérer les messages",
                "Rendre muet",
                "Rendre sourd",
                "Envoyer des messages Embed",
                "Envoyer des pièces jointes",
                "Mentionner @everyone",
                "Utiliser des emojis externes au serveur",
                "Changer son pseudo",
                "Gérer les pseudos",
                "Gérer les roles",
                "Gérer les WebHooks",
                "Gérer les Emojis",
                "Voir les logs"
            ]

            MsgBase = ctx.message.author.mention + " Voici les permissions de " + member.mention + " : ```scheme\n"
            Msg = MsgBase

            for perm, title in zip(perms, titles):
                fmt = "[>] {0:45} {1:12}\n".format(title, perm)
                Msg += fmt

            Msg += "```"

            await self.bot.delete_message(ctx.message)
            await self.bot.delete_message(tmp)
            await self.bot.say(Msg)

    @commands.command(pass_context=True, no_pm=True)
    async def userinfo(self, ctx, *, user=None):
        """Donne les informations concernant le joueur cité"""

        if user == None:
            member = ctx.message.author
        else:
            user = str(user)
            member = ctx.message.server.get_member_named(user)

        if member == None:
            await self.bot.delete_message(ctx.message)
            await self.bot.say("{1} L'utilisateur {0} est inconnu".format(user, ctx.message.author.mention))
        else:
            await self.bot.delete_message(ctx.message)
            tmp = await self.bot.say("Chargement des informations...")

            try:
                RolesList = get_user_roles(member)
                createdAt = dateConverter(member.created_at)
                joinedAt = dateConverter(member.joined_at)
                Statut = str(member.status)
                StatutFinal = Statut.capitalize()

                UserEmbed = discord.Embed()
                UserEmbed.title = "Userinfo for {0}#{1} [{2}]:".format(member.name, member.discriminator, member.top_role)
                UserEmbed.colour = 0x3498db
                UserEmbed.set_thumbnail(url=member.avatar_url)
                UserEmbed.add_field(name = "Surnom", value = member.nick)
                UserEmbed.add_field(name = "ID", value = member.id)
                UserEmbed.add_field(name = "Discriminateur", value = member.discriminator)
                UserEmbed.add_field(name = 'Statut', value = StatutFinal)
                UserEmbed.add_field(name = 'Joue à', value = member.game)
                UserEmbed.add_field(name = 'Compte créé le', value = createdAt)
                UserEmbed.add_field(name = 'A rejoint le serveur le', value = joinedAt)
                UserEmbed.add_field(name = "Roles", value = RolesList)
                UserEmbed.set_footer(text = "Requested by {0}".format(ctx.message.author.name), icon_url = ctx.message.author.avatar_url)

                await self.bot.delete_message(tmp)
                await self.bot.say(embed=UserEmbed)

            except Exception as e:
                fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
                await self.bot.send_message(ctx.message.channel, fmt.format(type(e).__name__, e))

    @commands.command(pass_context=True, no_pm=True)
    async def serverinfo(self, ctx):
        """Donne les informations du serveur"""

        server = ctx.message.server
        await self.bot.delete_message(ctx.message)
        tmp = await self.bot.say('Processing request...')
        VerifLevel = str(server.verification_level)

        ServerEmbed = discord.Embed()
        ServerEmbed.colour = 0x3498db
        ServerEmbed.set_thumbnail(url=server.icon_url)
        ServerEmbed.add_field(name = "Server Name", value = server.name)
        ServerEmbed.add_field(name = "Server ID", value = server.id)
        ServerEmbed.add_field(name = "Owner's Name", value = server.owner.name)
        ServerEmbed.add_field(name = "Owner's ID", value = server.owner.id)
        ServerEmbed.add_field(name = "Text Channels", value = str(len(getTextChannels(server))))
        ServerEmbed.add_field(name = "Voice Channels", value = str(len(getVoiceChannels(server))))
        ServerEmbed.add_field(name = "Users", value = server.member_count)
        ServerEmbed.add_field(name = "Verification level", value = VerifLevel.upper())
        ServerEmbed.add_field(name = "Roles Count", value = str(len(server.role_hierarchy)))
        ServerEmbed.add_field(name = "Region", value = formatServerRegion(server.region))
        ServerEmbed.add_field(name = "Creation Date", value = dateConverter(server.created_at))
        ServerEmbed.add_field(name = "Emotes Count", value = str(len(server.emojis)))
        ServerEmbed.add_field(name = "Roles", value = formatServerRoles(server.role_hierarchy), inline=False)
        ServerEmbed.add_field(name = "Emojis", value = formatEmojis(server.emojis), inline = False)
        ServerEmbed.set_footer(text = "Requested by {0}".format(ctx.message.author.name), icon_url = ctx.message.author.avatar_url)

        await self.bot.delete_message(tmp)
        await self.bot.say(embed=ServerEmbed)

    @commands.command(pass_context=True, no_pm=False)
    async def switchoff(self, ctx):
        requester = ctx.message.author
        await self.bot.delete_message(ctx.message)
        if requester.id == '187565415512276993':
            print('[FTS] Déconnexion...')
            await self.bot.logout()
            print('[FTS] Logged out')
            await self.bot.close()
            print('[FTS] Connexions closed')
            exit()
        else:
            tmp = await self.bot.say("Vous n'êtes pas le Bot Master")
            await asyncio.sleep(5)
            await self.bot.delete_message(tmp)

    @commands.command(pass_context=True, no_pm=False)
    async def test(self, ctx):
        """Teste le bot [10 secondes]"""
        await self.bot.delete_message(ctx.message)
        tmp = await self.bot.say("Test en cours :\n```\n|..........|\n```")
        i = 0
        bar = ""
        pt = ".........."
        while i < 10:
            i += 1
            bar += "█"
            pt = pt[:-1]
            await self.bot.edit_message(tmp, "Test en cours :\n```\n|"+ bar + pt +"|\n```")
            await asyncio.sleep(1)
        await self.bot.edit_message(tmp, "```Test terminé```")
        await asyncio.sleep(5)
        await self.bot.delete_message(tmp)

    @commands.command(pass_context=True, no_pm=True)
    async def randomplayer(self, ctx):
        """Retourne un membre du serveur au hasard"""
        await self.bot.delete_message(ctx.message)
        server = ctx.message.server
        members = getServerMembers(server)

        length = len(members) - 1
        rand = random.randint(0, length)

        member = members[rand]

        MbrEmbed = discord.Embed()
        MbrEmbed.colour = 0x3498db
        MbrEmbed.title = "Random Player :"
        MbrEmbed.description = member
        MbrEmbed.set_footer(text = "Requested by {0}".format(ctx.message.author.name), icon_url = ctx.message.author.avatar_url)
        await self.bot.say(embed=MbrEmbed)<commit_msg>Bug fix<commit_after># -*- coding: utf-8 -*-

import  asyncio
import  discord
from    discord.ext         import commands

from    libraries.perms     import *
from    libraries.library   import *

class Admin:
    """Commandes d'administration et de gestion"""

    def __init__(self, bot):
        self.bot = bot
        self.voice_states = {}

    @commands.command(pass_context=True, no_pm=True)
    async def setgame(self, ctx, *, game : str):
        """Définit le jeu du bot"""

        member = ctx.message.author
        await self.bot.delete_message(ctx.message)
        if member.server_permissions.administrator == True:
            await self.bot.change_presence(game=discord.Game(name=game))
        else:
            await self.bot.say("Vous n'êtes pas administrateur")

    @commands.command(pass_context=True, no_pm=True)
    async def perms(self, ctx, *, user=None):
        """Donne les permissions du joueur choisi"""

        if user == None:
            member = ctx.message.author
        else:
            user = str(user)
            member = ctx.message.server.get_member_named(user)

        if member == None:
            await self.bot.say("{1} L'utilisateur {0} est inconnu".format(user, ctx.message.author.mention))
        else:
            tmp = await self.bot.say("Récupération des permissions...")

            perms = [
                get_perm_admin(member),
                get_perm_create_instant_invite(member),
                get_perm_kick_members(member),
                get_perm_ban_members(member),
                get_perm_manage_channels(member),
                get_perm_manage_server(member),
                get_perm_add_reactions(member),
                get_perm_send_tts_messages(member),
                get_perm_manage_messages(member),
                get_perm_mute(member),
                get_perm_deafen(member),
                get_perm_send_embed_links(member),
                get_perm_attach_files(member),
                get_perm_mention_everyone(member),
                get_perm_external_emojis(member),
                get_perm_change_nickname(member),
                get_perm_manage_nicknames(member),
                get_perm_manage_roles(member),
                get_perm_manage_webhooks(member),
                get_perm_manage_emojis(member),
                get_perm_view_audit_logs(member)
            ]

            titles = [
                "Permissions administrateur",
                "Créer invitations",
                "Éjecter les membres",
                "Bannir les membres",
                "Gérer les channels",
                "Gérer le serveur",
                "Ajouter des réactions",
                "Envoyer des messages tts",
                "Gérer les messages",
                "Rendre muet",
                "Rendre sourd",
                "Envoyer des messages Embed",
                "Envoyer des pièces jointes",
                "Mentionner @everyone",
                "Utiliser des emojis externes au serveur",
                "Changer son pseudo",
                "Gérer les pseudos",
                "Gérer les roles",
                "Gérer les WebHooks",
                "Gérer les Emojis",
                "Voir les logs"
            ]

            MsgBase = ctx.message.author.mention + " Voici les permissions de " + member.mention + " : ```scheme\n"
            Msg = MsgBase

            for perm, title in zip(perms, titles):
                fmt = "[>] {0:45} {1:12}\n".format(title, perm)
                Msg += fmt

            Msg += "```"

            await self.bot.delete_message(ctx.message)
            await self.bot.delete_message(tmp)
            await self.bot.say(Msg)

    @commands.command(pass_context=True, no_pm=True)
    async def userinfo(self, ctx, *, user=None):
        """Donne les informations concernant le joueur cité"""

        if user == None:
            member = ctx.message.author
        else:
            user = str(user)
            member = ctx.message.server.get_member_named(user)

        if member == None:
            await self.bot.delete_message(ctx.message)
            await self.bot.say("{1} L'utilisateur {0} est inconnu".format(user, ctx.message.author.mention))
        else:
            await self.bot.delete_message(ctx.message)
            tmp = await self.bot.say("Chargement des informations...")

            try:
                RolesList = get_user_roles(member)
                createdAt = dateConverter(member.created_at)
                joinedAt = dateConverter(member.joined_at)
                Statut = str(member.status)
                StatutFinal = Statut.capitalize()

                UserEmbed = discord.Embed()
                UserEmbed.title = "Userinfo for {0}#{1} [{2}]:".format(member.name, member.discriminator, member.top_role)
                UserEmbed.colour = 0x3498db
                UserEmbed.set_thumbnail(url=member.avatar_url)
                UserEmbed.add_field(name = "Surnom", value = member.nick)
                UserEmbed.add_field(name = "ID", value = member.id)
                UserEmbed.add_field(name = "Discriminateur", value = member.discriminator)
                UserEmbed.add_field(name = 'Statut', value = StatutFinal)
                UserEmbed.add_field(name = 'Joue à', value = member.game)
                UserEmbed.add_field(name = 'Compte créé le', value = createdAt)
                UserEmbed.add_field(name = 'A rejoint le serveur le', value = joinedAt)
                UserEmbed.add_field(name = "Roles", value = RolesList)
                UserEmbed.set_footer(text = "Requested by {0}".format(ctx.message.author.name), icon_url = ctx.message.author.avatar_url)

                await self.bot.delete_message(tmp)
                await self.bot.say(embed=UserEmbed)

            except Exception as e:
                fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
                await self.bot.send_message(ctx.message.channel, fmt.format(type(e).__name__, e))

    @commands.command(pass_context=True, no_pm=True)
    async def serverinfo(self, ctx):
        """Donne les informations du serveur"""

        server = ctx.message.server
        await self.bot.delete_message(ctx.message)
        tmp = await self.bot.say('Processing request...')
        VerifLevel = str(server.verification_level)

        ServerEmbed = discord.Embed()
        ServerEmbed.colour = 0x3498db
        ServerEmbed.set_thumbnail(url=server.icon_url)
        ServerEmbed.add_field(name = "Server Name", value = server.name)
        ServerEmbed.add_field(name = "Server ID", value = server.id)
        ServerEmbed.add_field(name = "Owner's Name", value = server.owner.name)
        ServerEmbed.add_field(name = "Owner's ID", value = server.owner.id)
        ServerEmbed.add_field(name = "Text Channels", value = str(len(getTextChannels(server))))
        ServerEmbed.add_field(name = "Voice Channels", value = str(len(getVoiceChannels(server))))
        ServerEmbed.add_field(name = "Users", value = server.member_count)
        ServerEmbed.add_field(name = "Verification level", value = VerifLevel.upper())
        ServerEmbed.add_field(name = "Roles Count", value = str(len(server.role_hierarchy)))
        ServerEmbed.add_field(name = "Region", value = formatServerRegion(server.region))
        ServerEmbed.add_field(name = "Creation Date", value = dateConverter(server.created_at))
        ServerEmbed.add_field(name = "Emotes Count", value = str(len(server.emojis)))
        ServerEmbed.add_field(name = "Roles", value = formatServerRoles(server.role_hierarchy), inline=False)
        ServerEmbed.add_field(name = "Emojis", value = formatEmojis(server.emojis), inline = False)
        ServerEmbed.set_footer(text = "Requested by {0}".format(ctx.message.author.name), icon_url = ctx.message.author.avatar_url)

        await self.bot.delete_message(tmp)
        await self.bot.say(embed=ServerEmbed)

    @commands.command(pass_context=True, no_pm=False)
    async def disconnect(self, ctx):
        """Déconnecte le bot - Bot Master uniquement"""
        requester = ctx.message.author
        await self.bot.delete_message(ctx.message)
        if requester.id == '187565415512276993':
            print('[FTS] Déconnexion...')
            await self.bot.logout()
            print('[FTS] Logged out')
            await self.bot.close()
            print('[FTS] Connexions closed')
            exit()
        else:
            tmp = await self.bot.say("Vous n'êtes pas le Bot Master")
            await asyncio.sleep(5)
            await self.bot.delete_message(tmp)

    @commands.command(pass_context=True, no_pm=False)
    async def test(self, ctx):
        """Teste le bot [10 secondes]"""
        await self.bot.delete_message(ctx.message)
        tmp = await self.bot.say("Test en cours :\n```\n|..........|\n```")
        i = 0
        bar = ""
        pt = ".........."
        while i < 10:
            i += 1
            bar += "█"
            pt = pt[:-1]
            await self.bot.edit_message(tmp, "Test en cours :\n```\n|"+ bar + pt +"|\n```")
            await asyncio.sleep(1)
        await self.bot.edit_message(tmp, "```Test terminé```")
        await asyncio.sleep(5)
        await self.bot.delete_message(tmp)

    @commands.command(pass_context=True, no_pm=True)
    async def randomplayer(self, ctx):
        """Retourne un membre du serveur au hasard"""
        await self.bot.delete_message(ctx.message)
        server = ctx.message.server
        members = getServerMembers(server)

        length = len(members) - 1
        rand = random.randint(0, length)

        member = members[rand]

        MbrEmbed = discord.Embed()
        MbrEmbed.colour = 0x3498db
        MbrEmbed.title = "Random Player :"
        MbrEmbed.description = member
        MbrEmbed.set_footer(text = "Requested by {0}".format(ctx.message.author.name), icon_url = ctx.message.author.avatar_url)
        await self.bot.say(embed=MbrEmbed)<|endoftext|>


<commit_before>"""Flask-RAML (REST API Markup Language) API server with parameter conversion, response encoding, and examples."""

__all__ = 'API Loader Converter MimeEncoders RequestError ParameterError'.split()
__version__ = '0.1.5'

from operator import itemgetter
from functools import wraps

from flask import abort, request, has_request_context, Response
from werkzeug.datastructures import MultiDict

import flask.ext.mime_encoders
import flask.ext.mime_encoders.json

import raml
from raml import ApiError, RequestError, ParameterError  # Export exceptions.

class MimeEncoders(flask.ext.mime_encoders.MimeEncoders):
    default = flask.ext.mime_encoders.MimeEncoders.json

class Converter(raml.Converter):
    log = True

    def convert_params(self, specification, params):
        if isinstance(params, MultiDict):
            params, multidict = {}, params
            for key, values in multidict.iteritems():
                params[key] = values[0] if len(values) == 1 else values

        return super(Converter, self).convert_params(specification, params)

class Loader(raml.Loader):
    log = True

    spec_param_template = '{{{name}}}'
    flask_param_template = '<{flask_type}:{name}>'

    flask_types = {
        'integer': 'int',
        'number': 'float',
        'string': 'string',
        'boolean': 'bool',
        'date': 'date',
    }

    def get_resource_uri(self, resource):
        uri = resource['relativeUri']
        if 'uriParameters' in resource:
            spec_format, flask_format = self.spec_param_template.format, self.flask_param_template.format
            for name, param in resource['uriParameters'].items():
                param['name'] = name
                param['flask_type'] = self.flask_types[param['type']]
                uri = uri.replace(spec_format(**param), flask_format(**param))
            resource['allUriParameters'].update(resource['uriParameters'])
        return uri

class API(raml.API):
    """Flask API.
    """
    plugins = dict(raml.API.plugins, loader=Loader, encoders=MimeEncoders, converter=Converter)

    decode_request = True
    encode_response = True
    convert_query_params = True
    convert_uri_params = True
    endpoint_template = '{api}{resource}_{methods}'
    requested_response_status_header = 'X-Test-Response-Status'

    config_exclude = raml.API.config_exclude.union('unhandled_uris unhandled_methods'.split())

    def __init__(self, app, path, uri=None, id=None, log=None, **options):
        self.app = app
        self.views = {}

        if log is None or isinstance(log, basestring):
            log = app.logger.manager.getLogger(log or 'raml')

        super(API, self).__init__(path, uri, id, log, **options)

        self.default_mimetype = self.encoders.default.mimetype

        if log:
            log.debug(repr(self))

    @property
    def unhandled_uris(self):
        return [uri for uri in self.api if uri not in self.views]

    @property
    def unhandled_methods(self):
        result = []
        for uri, resource in self.api.iteritems():
            methods = self.views.get(uri, ())
            result.extend((uri, method) for method in resource['methodsByName'] if method.upper() not in methods)
        return result

    def abort(self, status, error, encoder=True):
        return abort(self.encoders[encoder].make_response(dict(status=status, error=error), status=status))

    def add_route(self, resource, view, methods=None, endpoint=None, **options):
        return self.route(resource, methods, endpoint, **options)(view)

    def route(self, resource, methods=None, endpoint=None, **options):
        resource = self.get_resource(resource)
        uri = resource['uri']
        config = dict(self.config, **options) if options else self.config
        methods = self.get_resource_methods(resource, methods)

        if endpoint is None:
            endpoint = self.get_endpoint(resource, methods, self.endpoint_template)

        decode_request = self.encoders[config['decode_request']]
        encode_response = self.encoders[config['encode_response']]
        convert_uri_params = config['convert_uri_params']
        convert_query_params = config['convert_query_params']

        def decorator(view):
            self.log.debug('map %s %s %s', self.id, '/'.join(sorted(methods)), uri)

            @wraps(view)
            def decorated_view(**uri_params):
                try:
                    self.log.info('{} {} {}'.format(request.method, uri, uri_params))
                    method = self.get_method_spec(resource, request.method)

                    if convert_uri_params:
                        uri_params = self.converter.convert_params(resource['allUriParameters'], uri_params)

                    if convert_query_params:
                        if 'queryParameters' in method:
                            uri_params.update(self.converter.convert_params(method['queryParameters'], request.args))
                        elif request.args:
                            self.abort(400, 'resource does not accept query parameters')

                    if decode_request:
                        uri_params.update(decode_request.get_request_data())

                    self.log.info('{} {} {}'.format(request.method, resource['uri'], uri_params))
                    response = view(**uri_params)

                    if encode_response and not isinstance(response, (Response, basestring)):
                        response = encode_response.make_response(response)

                    return response

                except ApiError as error:
                    self.abort(error.status, error.message)

            self.app.add_url_rule(uri, endpoint, decorated_view, methods=methods)

            for method in methods:
                self.views.setdefault(uri, {})[method] = decorated_view

            return decorated_view

        return decorator

    def serve(self, view, *args, **kwargs):
        try:
            return view(*args, **kwargs)
        except ApiError as error:
            self.abort(error.status, error.message)

    def get_endpoint(self, resource, methods=None, template=None):
        return (template or self.endpoint_template).format(
            api=self.id,
            resource=resource['uniqueId'],
            methods='+'.join(methods) if methods else 'any',
            )

    def get_response_mimetype(self, response, accept=None, request=request):
        if accept is None:
            if request and has_request_context():
                accept = map(itemgetter(0), request.accept_mimetypes)
        return super(API, self).get_response_mimetype(response, accept)

    def get_default_status(self, status=None, request=request):
        try:
            return request.headers[self.requested_response_status_header]
        except (KeyError, RuntimeError):
            return super(API, self).get_default_status()

    def serve_examples(self, **options):
        for uri, method in self.unhandled_methods:
            self.serve_example(uri, method)

    def serve_example(self, resource, methods=None, **options):
        resource = self.get_resource(resource)

        for method in self.get_resource_methods(resource, methods):
            method_spec = self.get_method_spec(resource, method)
            self.route(resource, method, **options)(self.create_example_view(method_spec))

    def create_example_view(self, method_spec):
        def view(**params):
            return self.serve(self.get_example, method_spec)
        return view

    def get_example(self, method_spec, status=None, mimetype=None):
        response = self.get_response(method_spec, status)
        body = self.get_example_body(response, mimetype)
        headers = self.get_example_headers(response)

        self.log.info('%s %s: %d %s (%d bytes, %d headers)', method_spec['method'].upper(), method_spec['uri'],
            response['status'], body.mimetype, len(body), len(headers))

        return Response(body.content, status=response['status'], headers=headers, mimetype=body.mimetype)
<commit_msg>feat: allow abort without response body<commit_after>"""Flask-RAML (REST API Markup Language) API server with parameter conversion, response encoding, and examples."""

__all__ = 'API Loader Converter MimeEncoders RequestError ParameterError'.split()
__version__ = '0.1.5'

from operator import itemgetter
from functools import wraps

from flask import abort, request, has_request_context, Response
from werkzeug.datastructures import MultiDict

import flask.ext.mime_encoders
import flask.ext.mime_encoders.json

import raml
from raml import ApiError, RequestError, ParameterError  # Export exceptions.

class MimeEncoders(flask.ext.mime_encoders.MimeEncoders):
    default = flask.ext.mime_encoders.MimeEncoders.json

class Converter(raml.Converter):
    log = True

    def convert_params(self, specification, params):
        if isinstance(params, MultiDict):
            params, multidict = {}, params
            for key, values in multidict.iteritems():
                params[key] = values[0] if len(values) == 1 else values

        return super(Converter, self).convert_params(specification, params)

class Loader(raml.Loader):
    log = True

    spec_param_template = '{{{name}}}'
    flask_param_template = '<{flask_type}:{name}>'

    flask_types = {
        'integer': 'int',
        'number': 'float',
        'string': 'string',
        'boolean': 'bool',
        'date': 'date',
    }

    def get_resource_uri(self, resource):
        uri = resource['relativeUri']
        if 'uriParameters' in resource:
            spec_format, flask_format = self.spec_param_template.format, self.flask_param_template.format
            for name, param in resource['uriParameters'].items():
                param['name'] = name
                param['flask_type'] = self.flask_types[param['type']]
                uri = uri.replace(spec_format(**param), flask_format(**param))
            resource['allUriParameters'].update(resource['uriParameters'])
        return uri

class API(raml.API):
    """Flask API.
    """
    plugins = dict(raml.API.plugins, loader=Loader, encoders=MimeEncoders, converter=Converter)

    decode_request = True
    encode_response = True
    convert_query_params = True
    convert_uri_params = True
    endpoint_template = '{api}{resource}_{methods}'
    requested_response_status_header = 'X-Test-Response-Status'

    config_exclude = raml.API.config_exclude.union('unhandled_uris unhandled_methods'.split())

    def __init__(self, app, path, uri=None, id=None, log=None, **options):
        self.app = app
        self.views = {}

        if log is None or isinstance(log, basestring):
            log = app.logger.manager.getLogger(log or 'raml')

        super(API, self).__init__(path, uri, id, log, **options)

        self.default_mimetype = self.encoders.default.mimetype

        if log:
            log.debug(repr(self))

    @property
    def unhandled_uris(self):
        return [uri for uri in self.api if uri not in self.views]

    @property
    def unhandled_methods(self):
        result = []
        for uri, resource in self.api.iteritems():
            methods = self.views.get(uri, ())
            result.extend((uri, method) for method in resource['methodsByName'] if method.upper() not in methods)
        return result

    def abort(self, status, error=None, encoder=True):
        return abort(
            self.encoders[encoder].make_response(dict(status=status, error=error), status=status) if error
            else Response(status=status))

    def add_route(self, resource, view, methods=None, endpoint=None, **options):
        return self.route(resource, methods, endpoint, **options)(view)

    def route(self, resource, methods=None, endpoint=None, **options):
        resource = self.get_resource(resource)
        uri = resource['uri']
        config = dict(self.config, **options) if options else self.config
        methods = self.get_resource_methods(resource, methods)

        if endpoint is None:
            endpoint = self.get_endpoint(resource, methods, self.endpoint_template)

        decode_request = self.encoders[config['decode_request']]
        encode_response = self.encoders[config['encode_response']]
        convert_uri_params = config['convert_uri_params']
        convert_query_params = config['convert_query_params']

        def decorator(view):
            self.log.debug('map %s %s %s', self.id, '/'.join(sorted(methods)), uri)

            @wraps(view)
            def decorated_view(**uri_params):
                try:
                    self.log.info('{} {} {}'.format(request.method, uri, uri_params))
                    method = self.get_method_spec(resource, request.method)

                    if convert_uri_params:
                        uri_params = self.converter.convert_params(resource['allUriParameters'], uri_params)

                    if convert_query_params:
                        if 'queryParameters' in method:
                            uri_params.update(self.converter.convert_params(method['queryParameters'], request.args))
                        elif request.args:
                            self.abort(400, 'resource does not accept query parameters')

                    if decode_request:
                        uri_params.update(decode_request.get_request_data())

                    self.log.info('{} {} {}'.format(request.method, resource['uri'], uri_params))
                    response = view(**uri_params)

                    if encode_response and not isinstance(response, (Response, basestring)):
                        response = encode_response.make_response(response)

                    return response

                except ApiError as error:
                    self.abort(error.status, error.message)

            self.app.add_url_rule(uri, endpoint, decorated_view, methods=methods)

            for method in methods:
                self.views.setdefault(uri, {})[method] = decorated_view

            return decorated_view

        return decorator

    def serve(self, view, *args, **kwargs):
        try:
            return view(*args, **kwargs)
        except ApiError as error:
            self.abort(error.status, error.message)

    def get_endpoint(self, resource, methods=None, template=None):
        return (template or self.endpoint_template).format(
            api=self.id,
            resource=resource['uniqueId'],
            methods='+'.join(methods) if methods else 'any',
            )

    def get_response_mimetype(self, response, accept=None, request=request):
        if accept is None:
            if request and has_request_context():
                accept = map(itemgetter(0), request.accept_mimetypes)
        return super(API, self).get_response_mimetype(response, accept)

    def get_default_status(self, status=None, request=request):
        try:
            return request.headers[self.requested_response_status_header]
        except (KeyError, RuntimeError):
            return super(API, self).get_default_status()

    def serve_examples(self, **options):
        for uri, method in self.unhandled_methods:
            self.serve_example(uri, method)

    def serve_example(self, resource, methods=None, **options):
        resource = self.get_resource(resource)

        for method in self.get_resource_methods(resource, methods):
            method_spec = self.get_method_spec(resource, method)
            self.route(resource, method, **options)(self.create_example_view(method_spec))

    def create_example_view(self, method_spec):
        def view(**params):
            return self.serve(self.get_example, method_spec)
        return view

    def get_example(self, method_spec, status=None, mimetype=None):
        response = self.get_response(method_spec, status)
        body = self.get_example_body(response, mimetype)
        headers = self.get_example_headers(response)

        self.log.info('%s %s: %d %s (%d bytes, %d headers)', method_spec['method'].upper(), method_spec['uri'],
            response['status'], body.mimetype, len(body), len(headers))

        return Response(body.content, status=response['status'], headers=headers, mimetype=body.mimetype)
<|endoftext|>


<commit_before>'''
Interface to interact on a database level
'''
# Import python libs
import os
import io
# Import sorbic libs
import sorbic.ind.hdht
# Import third party libs
import msgpack

DB_OPTS = (
        'key_delim',
        'hash_limit',
        'key_hash',
        'fmt',
        'fmt_map',
        'header_len')

class DB(object):
    '''
    Databaseing
    '''
    def __init__(
            self,
            root,
            key_delim='/',
            hash_limit=0xfffff,
            key_hash='blake',
            fmt='>KsQ',
            fmt_map=None,
            header_len=1024,
            serial='msgpack'):
        self.root = root
        self.key_delim = key_delim
        self.hash_limit = hash_limit
        self.key_hash = key_hash
        self.fmt = fmt
        self.fmt_map = fmt_map
        self.header_len = header_len
        self._get_db_meta()
        self.storage = sorbic.stor.Stor(self.root)
        self.index = sorbic.ind.hdht.HDHT(
                self.root,
                self.key_delim,
                self.hash_limit,
                self.key_hash,
                self.fmt,
                self.fmt_map,
                self.header_len)

    def _get_db_meta(self):
        '''
        Read in the database metadata to preserve the original behavior
        as to when the database are created
        '''
        db_meta = os.path.join(self.root, 'sorbic_db_meta.mp')
        meta = {}
        if os.path.isfile(db_meta):
            with io.open(db_meta, 'rb') as fp_:
                meta = msgpack.loads(fp_.read())
        for entry in DB_OPTS:
            meta[entry] = meta.get(entry, getattr(self, entry))
            setattr(self, entry, meta[entry])
        with io.open(db_meta, 'w+b') as fp_:
            fp_.write(msgpack.dumps(meta))

    def insert(self, key, data, id_=None, type_='doc', serial=None, **kwargs):
        '''
        Insert a key into the database
        '''
        c_key = self.index.raw_crypt_key(key)
        table_entry = self.index.get_table_entry(key, c_key)
        start, size = self.storage.write(
                table_entry,
                key,
                data,
                type_,
                serial)
        self.index.commit(
                table_entry,
                key,
                c_key,
                id_,
                start,
                size,
                type_,
                **kwargs)
<commit_msg>Add serial default entries to the db<commit_after>'''
Interface to interact on a database level
'''
# Import python libs
import os
import io
# Import sorbic libs
import sorbic.ind.hdht
# Import third party libs
import msgpack

DB_OPTS = (
        'key_delim',
        'hash_limit',
        'key_hash',
        'fmt',
        'fmt_map',
        'header_len',
        'serial')

class DB(object):
    '''
    Databaseing
    '''
    def __init__(
            self,
            root,
            key_delim='/',
            hash_limit=0xfffff,
            key_hash='blake',
            fmt='>KsQ',
            fmt_map=None,
            header_len=1024,
            serial='msgpack'):
        self.root = root
        self.key_delim = key_delim
        self.hash_limit = hash_limit
        self.key_hash = key_hash
        self.fmt = fmt
        self.fmt_map = fmt_map
        self.header_len = header_len
        self.serial = serial
        self._get_db_meta()
        self.storage = sorbic.stor.Stor(self.root)
        self.index = sorbic.ind.hdht.HDHT(
                self.root,
                self.key_delim,
                self.hash_limit,
                self.key_hash,
                self.fmt,
                self.fmt_map,
                self.header_len)

    def _get_db_meta(self):
        '''
        Read in the database metadata to preserve the original behavior
        as to when the database are created
        '''
        db_meta = os.path.join(self.root, 'sorbic_db_meta.mp')
        meta = {}
        if os.path.isfile(db_meta):
            with io.open(db_meta, 'rb') as fp_:
                meta = msgpack.loads(fp_.read())
        for entry in DB_OPTS:
            meta[entry] = meta.get(entry, getattr(self, entry))
            setattr(self, entry, meta[entry])
        with io.open(db_meta, 'w+b') as fp_:
            fp_.write(msgpack.dumps(meta))

    def insert(self, key, data, id_=None, type_='doc', serial=None, **kwargs):
        '''
        Insert a key into the database
        '''
        c_key = self.index.raw_crypt_key(key)
        table_entry = self.index.get_table_entry(key, c_key)
        start, size = self.storage.write(
                table_entry,
                key,
                data,
                type_,
                serial)
        self.index.commit(
                table_entry,
                key,
                c_key,
                id_,
                start,
                size,
                type_,
                **kwargs)
<|endoftext|>


<commit_before># -*- coding:utf8 -*-

'''
dsf core module for working with Debian / Ubuntu 'apt' package manager

@author: Stuart Herbert <stuart@stuartherbert.com>

https://devsetup.systems/dsf-framework/
'''

import os
import dsf

def install(pkg):
	"""
	Uses 'apt' to install the given package

	Params:

	* pkg: the name of the package to install. Can be a single string
	  or a list of packages
	"""

	# make sure we force non-interactive mode for apt (grrr)
	os.environ["DEBIAN_FRONTEND"] = "noninteractive"

	# build the command to run apt
	cmd = ["apt-get", "install", "-y" ]
	if type(cmd) in (tuple, list):
		cmd = cmd + pkg
	else:
		cmd = cmd + [ pkg ]

	# run the command, and make sure it worked
	retval = dsf.shell.run(cmd)
	if retval != 0:
		raise RuntimeError("apt-get install failed")<commit_msg>Fix: dsf.apt() unhappy since we started running commands with a real shell<commit_after># -*- coding:utf8 -*-

'''
dsf core module for working with Debian / Ubuntu 'apt' package manager

@author: Stuart Herbert <stuart@stuartherbert.com>

https://devsetup.systems/dsf-framework/
'''

import os
import dsf

def install(pkg):
	"""
	Uses 'apt' to install the given package

	Params:

	* pkg: the name of the package to install. Can be a single string
	  or a list of packages
	"""

	# make sure we force non-interactive mode for apt (grrr)
	os.environ["DEBIAN_FRONTEND"] = "noninteractive"

	# build the command to run apt
	cmd = ["apt-get", "install", "-y" ]
	if type(cmd) in (tuple, list):
		for pkg in cmd:
			cmd.append(pkg)
	else:
		cmd.append(pkg)

	# run the command, and make sure it worked
	retval = dsf.shell.run(cmd)
	if retval != 0:
		raise RuntimeError("apt-get install failed")<|endoftext|>