<commit_before>
import pausable_unittest
import os
import os.path
import sys
import subprocess
import tempfile
import ctypes

TASK_NAME = "pausable_unittest"
BASE_DIR = os.path.abspath(os.getcwd())
BAT_PATH = os.path.join(BASE_DIR, "startup.bat")
PYTHON_PATH = os.path.abspath(sys.executable)
SCRIPT_PATH = os.path.relpath(sys.argv[0])
BAT_CONTENT_CMD_OPEN = "cd /d \"%~dp0\"\n" + \
    ('start "pausable_unittest" cmd /k ""%s" "%s""\n' % (PYTHON_PATH, SCRIPT_PATH))
BAT_CONTENT_CMD_CLOSE = "cd /d \"%~dp0\"\n" + \
    ('start "pausable_unittest" cmd /c ""%s" "%s""\n' % (PYTHON_PATH, SCRIPT_PATH))

class Pauser(pausable_unittest.BasePauser):
    def __init__(self, close_cmd=False):
        super(Pauser, self).__init__()
        self._close_cmd = close_cmd

    def check_call(self, command):
        subprocess.check_output(command, stderr=subprocess.STDOUT)

    def check_output(self, command):
        return subprocess.check_output(command, stderr=subprocess.STDOUT)

    def is_admin(self):
        return (ctypes.windll.shell32.IsUserAnAdmin() != 0)

    def system_reboot(self):
        self.check_call([ "shutdown.exe", "/r", "/t", "5" ])

    def register_admin_startup(self):
        try:
            user = os.environ["USERNAME"]
            command = [ "schtasks.exe", "/Create", "/RU", user, "/SC", "ONLOGON", "/TN", TASK_NAME, "/TR", '"' + BAT_PATH + '"', "/F", "/RL", "HIGHEST" ]
            self.check_call(command)

            command = [ "schtasks.exe", "/Query", "/TN", TASK_NAME, "/XML", "ONE" ]
            xml = self.check_output(command)
            xml = xml.replace("<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>",
                              "<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>")
            xml = xml.replace("<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>",
                              "<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>")

            with tempfile.NamedTemporaryFile(dir=BASE_DIR, delete=False) as xml_file:
                xml_file.write(xml)
                xml_file.close()
                xml_filename = xml_file.name

            try:
                command = [ "schtasks.exe", "/Create", "/TN", TASK_NAME, "/F", "/XML", xml_filename ]
                self.check_call(command)
            finally:
                os.remove(xml_filename)
        except:
            self.unregister_startup()

    def nonadmin_startup_filepath(self):
        startup_folder = os.path.join(os.environ["APPDATA"], r'Microsoft\Windows\Start Menu\Programs\Startup')
        return os.path.join(startup_folder, "pausable_unittest.bat")

    def register_nonadmin_startup(self):
        path = self.nonadmin_startup_filepath()
        try:
            with open(path, "w") as f:
                f.write('"%s"' % BAT_PATH)
        except:
            if os.path.exists(path):
                os.remove(path)

    @property
    def bat_path(self):
        return BAT_PATH

    def create_bat(self):
        with open(BAT_PATH, "w") as f:
            if self._close_cmd:
                f.write(BAT_CONTENT_CMD_CLOSE)
            else:
                f.write(BAT_CONTENT_CMD_OPEN)

    def remove_bat(self):
        if os.path.exists(BAT_PATH):
            os.remove(BAT_PATH)

    def register_startup(self):
        self.create_bat()
        if self.is_admin():
            self.register_admin_startup()
        else:
            self.register_nonadmin_startup()

    def unregister_startup(self):
        try:
            if self.is_admin():
                self.check_call([ "schtasks.exe", "/Delete", "/TN", TASK_NAME, "/F" ])
            else:
                path = self.nonadmin_startup_filepath()
                if os.path.exists(path):
                    os.remove(path)
            self.remove_bat()
        except:
            pass

    def add_actions(self):
        def reboot(self):
            self.pause(("reboot",))
        self.add_action("reboot", reboot)

        def exec_for_reboot(self, command, expected_exitcode=0, register_startup=True):
            self.pause(("exec_for_reboot", command, expected_exitcode, register_startup))
        self.add_action("exec_for_reboot", exec_for_reboot)

        def bat_path(self):
            return self.call_pauser_callback("bat_path")
        self.add_action("bat_path", bat_path)

        def create_bat(self):
            self.call_pauser_callback("create_bat")
        self.add_action("create_bat", create_bat)

    def do_pause(self, info):
        if info[0] == "reboot":
            self.register_startup()
            self.system_reboot()

        elif info[0] == "exec_for_reboot":
            cmd = info[1]
            expected_exitcode = info[2]
            register_startup = info[3]

            if register_startup:
                self.register_startup()
            ret = subprocess.call(cmd)
            if type(expected_exitcode) == list or type(expected_exitcode) == tuple:
                if ret in expected_exitcode:
                    raise subprocess.CalledProcessError(ret, str(cmd))
            elif expected_exitcode is not None:
                if ret != expected_exitcode:
                    raise subprocess.CalledProcessError(ret, str(cmd))

    def after_pause(self):
        self.unregister_startup()

    def exec_callback(self, action, info):
        if action == "create_bat":
            return self.create_bat()
        elif action == "bat_path":
            return self.bat_path()
        else:
            return super(Pauser, self).exec_callback(action, info)
<commit_msg>Remove @property decorator.<commit_after>
import pausable_unittest
import os
import os.path
import sys
import subprocess
import tempfile
import ctypes

TASK_NAME = "pausable_unittest"
BASE_DIR = os.path.abspath(os.getcwd())
BAT_PATH = os.path.join(BASE_DIR, "startup.bat")
PYTHON_PATH = os.path.abspath(sys.executable)
SCRIPT_PATH = os.path.relpath(sys.argv[0])
BAT_CONTENT_CMD_OPEN = "cd /d \"%~dp0\"\n" + \
    ('start "pausable_unittest" cmd /k ""%s" "%s""\n' % (PYTHON_PATH, SCRIPT_PATH))
BAT_CONTENT_CMD_CLOSE = "cd /d \"%~dp0\"\n" + \
    ('start "pausable_unittest" cmd /c ""%s" "%s""\n' % (PYTHON_PATH, SCRIPT_PATH))

class Pauser(pausable_unittest.BasePauser):
    def __init__(self, close_cmd=False):
        super(Pauser, self).__init__()
        self._close_cmd = close_cmd

    def check_call(self, command):
        subprocess.check_output(command, stderr=subprocess.STDOUT)

    def check_output(self, command):
        return subprocess.check_output(command, stderr=subprocess.STDOUT)

    def is_admin(self):
        return (ctypes.windll.shell32.IsUserAnAdmin() != 0)

    def system_reboot(self):
        self.check_call([ "shutdown.exe", "/r", "/t", "5" ])

    def register_admin_startup(self):
        try:
            user = os.environ["USERNAME"]
            command = [ "schtasks.exe", "/Create", "/RU", user, "/SC", "ONLOGON", "/TN", TASK_NAME, "/TR", '"' + BAT_PATH + '"', "/F", "/RL", "HIGHEST" ]
            self.check_call(command)

            command = [ "schtasks.exe", "/Query", "/TN", TASK_NAME, "/XML", "ONE" ]
            xml = self.check_output(command)
            xml = xml.replace("<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>",
                              "<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>")
            xml = xml.replace("<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>",
                              "<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>")

            with tempfile.NamedTemporaryFile(dir=BASE_DIR, delete=False) as xml_file:
                xml_file.write(xml)
                xml_file.close()
                xml_filename = xml_file.name

            try:
                command = [ "schtasks.exe", "/Create", "/TN", TASK_NAME, "/F", "/XML", xml_filename ]
                self.check_call(command)
            finally:
                os.remove(xml_filename)
        except:
            self.unregister_startup()

    def nonadmin_startup_filepath(self):
        startup_folder = os.path.join(os.environ["APPDATA"], r'Microsoft\Windows\Start Menu\Programs\Startup')
        return os.path.join(startup_folder, "pausable_unittest.bat")

    def register_nonadmin_startup(self):
        path = self.nonadmin_startup_filepath()
        try:
            with open(path, "w") as f:
                f.write('"%s"' % BAT_PATH)
        except:
            if os.path.exists(path):
                os.remove(path)

    def bat_path(self):
        return BAT_PATH

    def create_bat(self):
        with open(BAT_PATH, "w") as f:
            if self._close_cmd:
                f.write(BAT_CONTENT_CMD_CLOSE)
            else:
                f.write(BAT_CONTENT_CMD_OPEN)

    def remove_bat(self):
        if os.path.exists(BAT_PATH):
            os.remove(BAT_PATH)

    def register_startup(self):
        self.create_bat()
        if self.is_admin():
            self.register_admin_startup()
        else:
            self.register_nonadmin_startup()

    def unregister_startup(self):
        try:
            if self.is_admin():
                self.check_call([ "schtasks.exe", "/Delete", "/TN", TASK_NAME, "/F" ])
            else:
                path = self.nonadmin_startup_filepath()
                if os.path.exists(path):
                    os.remove(path)
            self.remove_bat()
        except:
            pass

    def add_actions(self):
        def reboot(self):
            self.pause(("reboot",))
        self.add_action("reboot", reboot)

        def exec_for_reboot(self, command, expected_exitcode=0, register_startup=True):
            self.pause(("exec_for_reboot", command, expected_exitcode, register_startup))
        self.add_action("exec_for_reboot", exec_for_reboot)

        def bat_path(self):
            return self.call_pauser_callback("bat_path")
        self.add_action("bat_path", bat_path)

        def create_bat(self):
            self.call_pauser_callback("create_bat")
        self.add_action("create_bat", create_bat)

    def do_pause(self, info):
        if info[0] == "reboot":
            self.register_startup()
            self.system_reboot()

        elif info[0] == "exec_for_reboot":
            cmd = info[1]
            expected_exitcode = info[2]
            register_startup = info[3]

            if register_startup:
                self.register_startup()
            ret = subprocess.call(cmd)
            if type(expected_exitcode) == list or type(expected_exitcode) == tuple:
                if ret in expected_exitcode:
                    raise subprocess.CalledProcessError(ret, str(cmd))
            elif expected_exitcode is not None:
                if ret != expected_exitcode:
                    raise subprocess.CalledProcessError(ret, str(cmd))

    def after_pause(self):
        self.unregister_startup()

    def exec_callback(self, action, info):
        if action == "create_bat":
            return self.create_bat()
        elif action == "bat_path":
            return self.bat_path()
        else:
            return super(Pauser, self).exec_callback(action, info)
<|endoftext|>


<commit_before>import asyncio
import discord
import random
from   discord.ext import commands
from   Cogs import Settings
from   Cogs import DisplayName
from   Cogs import Nullify
import requests
import urllib

class Ascii:

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

	@commands.command(pass_context=True, no_pm=True)
	async def ascii(self, ctx, *, text : str = None):
		"""Beautify some text (font list at http://artii.herokuapp.com/fonts_list)."""

		if text == None:
			await self.bot.send_message(ctx.message.channel, 'Usage: `$ascii [font (optional)] [text]`\n(font list at http://artii.herokuapp.com/fonts_list)')
			return

		# Get list of fonts
		fonturl = "http://artii.herokuapp.com/fonts_list"
		get_request = self.bot.loop.run_in_executor(None, requests.get, fonturl)
		response = await get_request
		fonts = response.text.split()

		font = None
		# Split text by space - and see if the first word is a font
		parts = text.split()
		if len(parts) > 1:
			# We have enough entries for a font
			if parts[0] in fonts:
				# We got a font!
				font = parts[0]
				text = ' '.join(parts[1:])

		url = "http://artii.herokuapp.com/make?{}".format(urllib.parse.urlencode({'text':text}))
		if font:
			url += '&font={}'.format(font)
		get_request = self.bot.loop.run_in_executor(None, requests.get, url)
		response = await get_request
		await self.bot.say("```Markup\n {} ```".format(response.text))<commit_msg>Fixed an errant space or two<commit_after>import asyncio
import discord
import random
from   discord.ext import commands
from   Cogs import Settings
from   Cogs import DisplayName
from   Cogs import Nullify
import requests
import urllib

class Ascii:

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

	@commands.command(pass_context=True, no_pm=True)
	async def ascii(self, ctx, *, text : str = None):
		"""Beautify some text (font list at http://artii.herokuapp.com/fonts_list)."""

		if text == None:
			await self.bot.send_message(ctx.message.channel, 'Usage: `$ascii [font (optional)] [text]`\n(font list at http://artii.herokuapp.com/fonts_list)')
			return

		# Get list of fonts
		fonturl = "http://artii.herokuapp.com/fonts_list"
		get_request = self.bot.loop.run_in_executor(None, requests.get, fonturl)
		response = await get_request
		fonts = response.text.split()

		font = None
		# Split text by space - and see if the first word is a font
		parts = text.split()
		if len(parts) > 1:
			# We have enough entries for a font
			if parts[0] in fonts:
				# We got a font!
				font = parts[0]
				text = ' '.join(parts[1:])

		url = "http://artii.herokuapp.com/make?{}".format(urllib.parse.urlencode({'text':text}))
		if font:
			url += '&font={}'.format(font)
		get_request = self.bot.loop.run_in_executor(None, requests.get, url)
		response = await get_request
		await self.bot.say("```Markup\n{}```".format(response.text))
<|endoftext|>


<commit_before>
import pausable_unittest
import os
import os.path
import sys
import subprocess
import tempfile
import ctypes
from .utils import winutils

TASK_NAME = "pausable_unittest"
BASE_DIR = os.path.abspath(os.getcwd())
BAT_PATH = os.path.join(BASE_DIR, "startup.bat")
PYTHON_PATH = os.path.abspath(sys.executable)
SCRIPT_PATH = os.path.relpath(sys.argv[0])
BAT_CONTENT_CMD_OPEN = "cd /d \"%~dp0\"\n" + \
    ('start "pausable_unittest" cmd /k ""%s" "%s""\n' % (PYTHON_PATH, SCRIPT_PATH))
BAT_CONTENT_CMD_CLOSE = "cd /d \"%~dp0\"\n" + \
    ('start "pausable_unittest" cmd /c ""%s" "%s""\n' % (PYTHON_PATH, SCRIPT_PATH))

class Pauser(pausable_unittest.BasePauser):
    def __init__(self, close_cmd=False):
        super(Pauser, self).__init__()
        self._close_cmd = close_cmd

    def check_call(self, command):
        subprocess.check_output(command, stderr=subprocess.STDOUT)

    def check_output(self, command):
        return subprocess.check_output(command, stderr=subprocess.STDOUT)

    def is_admin(self):
        return (ctypes.windll.shell32.IsUserAnAdmin() != 0)

    def system_reboot(self):
        self.check_call([ "shutdown.exe", "/r", "/t", "5" ])

    def register_admin_startup(self):
        try:
            winutils.register_schtasks(TASK_NAME, BAT_PATH, os.environ["USERNAME"], None, True)
       except:
            winutils.unregister_schtasks(TASK_NAME)

    def nonadmin_startup_filepath(self):
        startup_folder = os.path.join(os.environ["APPDATA"], r'Microsoft\Windows\Start Menu\Programs\Startup')
        return os.path.join(startup_folder, "pausable_unittest.bat")

    def register_nonadmin_startup(self):
        path = self.nonadmin_startup_filepath()
        try:
            with open(path, "w") as f:
                f.write('"%s"' % BAT_PATH)
        except:
            if os.path.exists(path):
                os.remove(path)

    def bat_path(self):
        return BAT_PATH

    def create_bat(self):
        with open(BAT_PATH, "w") as f:
            if self._close_cmd:
                f.write(BAT_CONTENT_CMD_CLOSE)
            else:
                f.write(BAT_CONTENT_CMD_OPEN)

    def remove_bat(self):
        if os.path.exists(BAT_PATH):
            os.remove(BAT_PATH)

    def register_startup(self):
        self.create_bat()
        if self.is_admin():
            self.register_admin_startup()
        else:
            self.register_nonadmin_startup()

    def unregister_startup(self):
        try:
            if self.is_admin():
                self.check_call([ "schtasks.exe", "/Delete", "/TN", TASK_NAME, "/F" ])
            else:
                path = self.nonadmin_startup_filepath()
                if os.path.exists(path):
                    os.remove(path)
            self.remove_bat()
        except:
            pass

    def add_actions(self):
        def reboot(self):
            self.pause(("reboot",))
        self.add_action("reboot", reboot)

        def exec_for_reboot(self, command, expected_exitcode=0, register_startup=True):
            self.pause(("exec_for_reboot", command, expected_exitcode, register_startup))
        self.add_action("exec_for_reboot", exec_for_reboot)

        def bat_path(self):
            return self.call_pauser_callback("bat_path")
        self.add_action("bat_path", bat_path)

        def create_bat(self):
            self.call_pauser_callback("create_bat")
        self.add_action("create_bat", create_bat)

    def do_pause(self, info):
        if info[0] == "reboot":
            self.register_startup()
            self.system_reboot()

        elif info[0] == "exec_for_reboot":
            cmd = info[1]
            expected_exitcode = info[2]
            register_startup = info[3]

            if register_startup:
                self.register_startup()
            ret = subprocess.call(cmd)
            if type(expected_exitcode) == list or type(expected_exitcode) == tuple:
                if ret in expected_exitcode:
                    raise subprocess.CalledProcessError(ret, str(cmd))
            elif expected_exitcode is not None:
                if ret != expected_exitcode:
                    raise subprocess.CalledProcessError(ret, str(cmd))

    def after_pause(self):
        self.unregister_startup()

    def exec_callback(self, action, info):
        if action == "create_bat":
            return self.create_bat()
        elif action == "bat_path":
            return self.bat_path()
        else:
            return super(Pauser, self).exec_callback(action, info)
<commit_msg>Fix indent in windowspauser.py.<commit_after>
import pausable_unittest
import os
import os.path
import sys
import subprocess
import ctypes
from .utils import winutils

TASK_NAME = "pausable_unittest"
BASE_DIR = os.path.abspath(os.getcwd())
BAT_PATH = os.path.join(BASE_DIR, "startup.bat")
PYTHON_PATH = os.path.abspath(sys.executable)
SCRIPT_PATH = os.path.relpath(sys.argv[0])
BAT_CONTENT_CMD_OPEN = "cd /d \"%~dp0\"\n" + \
    ('start "pausable_unittest" cmd /k ""%s" "%s""\n' % (PYTHON_PATH, SCRIPT_PATH))
BAT_CONTENT_CMD_CLOSE = "cd /d \"%~dp0\"\n" + \
    ('start "pausable_unittest" cmd /c ""%s" "%s""\n' % (PYTHON_PATH, SCRIPT_PATH))

class Pauser(pausable_unittest.BasePauser):
    def __init__(self, close_cmd=False):
        super(Pauser, self).__init__()
        self._close_cmd = close_cmd

    def check_call(self, command):
        subprocess.check_output(command, stderr=subprocess.STDOUT)

    def check_output(self, command):
        return subprocess.check_output(command, stderr=subprocess.STDOUT)

    def is_admin(self):
        return (ctypes.windll.shell32.IsUserAnAdmin() != 0)

    def system_reboot(self):
        self.check_call(["shutdown.exe", "/r", "/t", "5"])

    def register_admin_startup(self):
        try:
            winutils.register_schtasks(TASK_NAME, BAT_PATH, os.environ["USERNAME"], None, True)
        except:
            winutils.unregister_schtasks(TASK_NAME)

    def nonadmin_startup_filepath(self):
        startup_folder = os.path.join(os.environ["APPDATA"], r'Microsoft\Windows\Start Menu\Programs\Startup')
        return os.path.join(startup_folder, "pausable_unittest.bat")

    def register_nonadmin_startup(self):
        path = self.nonadmin_startup_filepath()
        try:
            with open(path, "w") as f:
                f.write('"%s"' % BAT_PATH)
        except:
            if os.path.exists(path):
                os.remove(path)

    def bat_path(self):
        return BAT_PATH

    def create_bat(self):
        with open(BAT_PATH, "w") as f:
            if self._close_cmd:
                f.write(BAT_CONTENT_CMD_CLOSE)
            else:
                f.write(BAT_CONTENT_CMD_OPEN)

    def remove_bat(self):
        if os.path.exists(BAT_PATH):
            os.remove(BAT_PATH)

    def register_startup(self):
        self.create_bat()
        if self.is_admin():
            self.register_admin_startup()
        else:
            self.register_nonadmin_startup()

    def unregister_startup(self):
        try:
            if self.is_admin():
                self.check_call([ "schtasks.exe", "/Delete", "/TN", TASK_NAME, "/F" ])
            else:
                path = self.nonadmin_startup_filepath()
                if os.path.exists(path):
                    os.remove(path)
            self.remove_bat()
        except:
            pass

    def add_actions(self):
        def reboot(self):
            self.pause(("reboot",))
        self.add_action("reboot", reboot)

        def exec_for_reboot(self, command, expected_exitcode=0, register_startup=True):
            self.pause(("exec_for_reboot", command, expected_exitcode, register_startup))
        self.add_action("exec_for_reboot", exec_for_reboot)

        def bat_path(self):
            return self.call_pauser_callback("bat_path")
        self.add_action("bat_path", bat_path)

        def create_bat(self):
            self.call_pauser_callback("create_bat")
        self.add_action("create_bat", create_bat)

    def do_pause(self, info):
        if info[0] == "reboot":
            self.register_startup()
            self.system_reboot()

        elif info[0] == "exec_for_reboot":
            cmd = info[1]
            expected_exitcode = info[2]
            register_startup = info[3]

            if register_startup:
                self.register_startup()
            ret = subprocess.call(cmd)
            if type(expected_exitcode) == list or type(expected_exitcode) == tuple:
                if ret in expected_exitcode:
                    raise subprocess.CalledProcessError(ret, str(cmd))
            elif expected_exitcode is not None:
                if ret != expected_exitcode:
                    raise subprocess.CalledProcessError(ret, str(cmd))

    def after_pause(self):
        self.unregister_startup()

    def exec_callback(self, action, info):
        if action == "create_bat":
            return self.create_bat()
        elif action == "bat_path":
            return self.bat_path()
        else:
            return super(Pauser, self).exec_callback(action, info)
<|endoftext|>


<commit_before>from discord.ext import commands
import discord
import os
import asyncio
import inspect
import textwrap
import tokage

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

    @commands.command(hidden=True, enabled=False)
    async def setavatar(self, ctx, picture):
        path = os.path.join("Bot Pics", picture)
        try:
            with open('%s' % path, 'rb') as f:
                await ctx.bot.user.edit(avatar=f.read())
            await ctx.send(":ok_hand: Avatar changed to %s" % picture.split(".")[0])
        except Exception:
            await ctx.send(":exclamation: File not found!")

    @commands.command(hidden=True, aliases=["eval", "evaluate"])
    @commands.is_owner()
    async def debug(self, ctx, *, code: str):
        """Evaluates code."""
        code = code.strip('`')
        python = '```py\n{}\n```'
        result = None

        env = {
            'bot': self.bot,
            'ctx': ctx,
            'message': ctx.message,
            'server': ctx.guild,
            'guild': ctx.guild,
            'channel': ctx.channel,
            'author': ctx.author,
            'history': await ctx.channel.history().flatten(),
            't_client': tokage.Client()
        }

        env.update(globals())

        try:
            result = eval(code, env)
            if inspect.isawaitable(result):
                result = await result
        except Exception as e:
            await ctx.send(python.format(type(e).__name__ + ': ' + str(e)))
            return

        await ctx.send(python.format(result))

    @commands.command(name="exec", hidden=True)
    @commands.is_owner()
    async def execute(self, ctx, *, code):
        if code.startswith("```") and code.endswith("```"):
            code = code.strip("```")
            if code.startswith("py\n"):
                code = code[3:]
        env = {
            'bot': self.bot,
            'ctx': ctx,
            'message': ctx.message,
            'server': ctx.guild,
            'guild': ctx.guild,
            'channel': ctx.channel,
            'author': ctx.author,
            'history': await ctx.channel.history().flatten(),
            't_client': tokage.Client()
        }
        env.update(globals())
        wrapped = 'async def func():\n%s' % textwrap.indent(code, '  ')
        try:
            result = exec(wrapped, env)
            func = env['func']
            await func()
            if result:
                await ctx.send(f"```{result}```")
        except Exception as e:
            await ctx.send(f"```{type(e).__name__ + ': ' + str(e)}```")

    @commands.command(aliased=["join", "inv"])
    async def invite(self, ctx):
        """Sends an invite link for the bot"""
        await ctx.send("https://discordapp.com/oauth2/authorize/?permissions=64&scope=bot&client_id=236176083861372928")

def setup(bot):
    bot.add_cog(Utilities(bot))
<commit_msg>add base64 to img cmd<commit_after>import os
import asyncio
import inspect
import textwrap
import base64
from io import BytesIO

import tokage
import discord
from discord.ext import commands

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

    @commands.command(hidden=True, enabled=False)
    async def setavatar(self, ctx, picture):
        path = os.path.join("Bot Pics", picture)
        try:
            with open('%s' % path, 'rb') as f:
                await ctx.bot.user.edit(avatar=f.read())
            await ctx.send(":ok_hand: Avatar changed to %s" % picture.split(".")[0])
        except Exception:
            await ctx.send(":exclamation: File not found!")

    @commands.command(hidden=True, aliases=["eval", "evaluate"])
    @commands.is_owner()
    async def debug(self, ctx, *, code: str):
        """Evaluates code."""
        code = code.strip('`')
        python = '```py\n{}\n```'
        result = None

        env = {
            'bot': self.bot,
            'ctx': ctx,
            'message': ctx.message,
            'server': ctx.guild,
            'guild': ctx.guild,
            'channel': ctx.channel,
            'author': ctx.author,
            'history': await ctx.channel.history().flatten(),
            't_client': tokage.Client()
        }

        env.update(globals())

        try:
            result = eval(code, env)
            if inspect.isawaitable(result):
                result = await result
        except Exception as e:
            await ctx.send(python.format(type(e).__name__ + ': ' + str(e)))
            return

        await ctx.send(python.format(result))

    @commands.command(name="exec", hidden=True)
    @commands.is_owner()
    async def execute(self, ctx, *, code):
        if code.startswith("```") and code.endswith("```"):
            code = code.strip("```")
            if code.startswith("py\n"):
                code = code[3:]
        env = {
            'bot': self.bot,
            'ctx': ctx,
            'message': ctx.message,
            'server': ctx.guild,
            'guild': ctx.guild,
            'channel': ctx.channel,
            'author': ctx.author,
            'history': await ctx.channel.history().flatten(),
            't_client': tokage.Client()
        }
        env.update(globals())
        wrapped = 'async def func():\n%s' % textwrap.indent(code, '  ')
        try:
            result = exec(wrapped, env)
            func = env['func']
            await func()
            if result:
                await ctx.send(f"```{result}```")
        except Exception as e:
            await ctx.send(f"```{type(e).__name__ + ': ' + str(e)}```")

    @commands.command(aliased=["join", "inv"])
    async def invite(self, ctx):
        """Sends an invite link for the bot"""
        await ctx.send("https://discordapp.com/oauth2/authorize/?permissions=64&scope=bot&client_id=236176083861372928")

    class ByteString(commands.Converter):
        async def convert(self, ctx, arg):
            return bytes(arg)

    @commands.command()
    async def img64(self, ctx, *, b64: ByteString):
        f = BytesIO(base64.decodebytes(b64))
        await ctx.send(file=discord.File(f, "img.png"))

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


<commit_before>from __future__ import absolute_import, print_function
__author__ = "katharine"

import collections
import os
import re
import requests

from ..base import BaseCommand
from pebble_tool.exceptions import MissingSDK
from pebble_tool.sdk import sdk_manager
from pebble_tool.util.versions import version_to_key

class SDKManager(BaseCommand):
    """Manages available SDKs"""
    command = 'sdk'
    has_subcommands = True

    def __call__(self, args):
        super(SDKManager, self).__call__(args)
        args.sub_func(args)

    @classmethod
    def add_parser(cls, parser):
        parser = super(SDKManager, cls).add_parser(parser)
        subparsers = parser.add_subparsers(title="subcommand")

        list_parser = subparsers.add_parser("list", help="Lists available SDKs.")
        list_parser.set_defaults(sub_func=cls.do_list)

        install_parser = subparsers.add_parser("install", help="Installs the given SDK.")
        group = install_parser.add_mutually_exclusive_group(required=True)
        group.add_argument('version', nargs='?', help="Version to install, or 'latest' for the latest.")
        group.add_argument('--tintin', help="Path to a copy of the tintin source (internal only).")
        install_parser.set_defaults(sub_func=cls.do_install)

        activate_parser = subparsers.add_parser("activate", help="Makes the given, installed SDK active.")
        activate_parser.add_argument('version', help="Version to make active.")
        activate_parser.set_defaults(sub_func=cls.do_activate)

        uninstall_parser = subparsers.add_parser("uninstall", help="Uninstalls the given SDK.")
        uninstall_parser.add_argument('version', help="Version to uninstall.")
        uninstall_parser.set_defaults(sub_func=cls.do_uninstall)

        set_channel_parser = subparsers.add_parser("set-channel", help="Sets the SDK channel.")
        set_channel_parser.add_argument('channel', help="The channel to use.")
        set_channel_parser.set_defaults(sub_func=cls.do_set_channel)

        include_path_parser = subparsers.add_parser("include-path", help="Prints out the SDK include path.")
        include_path_parser.add_argument("platform", help="The platform to give includes for.")
        include_path_parser.add_argument("--sdk", help="Optional SDK version override.")
        include_path_parser.set_defaults(sub_func=cls.do_include_path)
        return parser

    @classmethod
    def do_list(cls, args):
        current_sdk = sdk_manager.get_current_sdk()
        local_sdks = sdk_manager.list_local_sdks()
        local_sdk_versions = sdk_manager.list_local_sdk_versions()
        sorted_local_sdks = sorted(local_sdks, key=lambda x: version_to_key(x['version']), reverse=True)
        if len(local_sdks) > 0:
            print("Installed SDKs:")
            for sdk in sorted_local_sdks:
                line = sdk['version']
                if sdk['channel']:
                    line += " ({})".format(sdk['channel'])
                if sdk['version'] == current_sdk:
                    line += " (active)"
                print(line)
            print()
        else:
            print("No SDKs installed yet.")
        if sdk_manager.get_channel() != '':
            channel_text = ' ({} channel)'.format(sdk_manager.get_channel())
        else:
            channel_text = ''
        print("Available SDKs{}:".format(channel_text))
        try:
            for sdk in sdk_manager.list_remote_sdks():
                if sdk['version'] in local_sdk_versions:
                    continue
                line = sdk['version']
                if sdk['channel']:
                    line += " ({})".format(sdk['channel'])
                if sdk['version'] == current_sdk:
                    line += " (active)"
                print(line)
        except requests.RequestException:
            print("Could not fetch list of available SDKs.")

    @classmethod
    def do_install(cls, args):
        print("Installing SDK...")
        if args.tintin:
            sdk_manager.make_tintin_sdk(args.tintin)
        else:
            if re.match("https?://", args.version):
                sdk_manager.install_from_url(args.version)
            elif os.path.exists(args.version):
                sdk_manager.install_from_path(args.version)
            else:
                sdk_manager.install_remote_sdk(args.version)
        print("Installed.")

    @classmethod
    def do_uninstall(cls, args):
        print("Uninstalling SDK {}...".format(args.version))
        sdk_manager.uninstall_sdk(args.version)
        print("Done.")

    @classmethod
    def do_activate(cls, args):
        sdk_manager.set_current_sdk(args.version)
        print("Set active SDK to {}.".format(sdk_manager.get_current_sdk()))

    @classmethod
    def do_set_channel(cls, args):
        sdk_manager.set_channel(args.channel)
        print("Set channel to {}.".format(sdk_manager.get_channel()))

    @classmethod
    def do_include_path(cls, args):
        sdk = args.sdk or sdk_manager.get_current_sdk()
        path = sdk_manager.path_for_sdk(sdk)
        path = os.path.join(path, "pebble", args.platform, "include")
        if not os.path.exists(path):
            raise MissingSDK("No platform '{}' available for SDK {}".format(args.platform, sdk))
        print(path)
<commit_msg>Delete emulator files by default when uninstalling an SDK.<commit_after>from __future__ import absolute_import, print_function
__author__ = "katharine"

import collections
import os
import re
import requests
import shutil

from ..base import BaseCommand
from pebble_tool.exceptions import MissingSDK
from pebble_tool.sdk import get_sdk_persist_dir, sdk_manager, pebble_platforms
from pebble_tool.util.versions import version_to_key

class SDKManager(BaseCommand):
    """Manages available SDKs"""
    command = 'sdk'
    has_subcommands = True

    def __call__(self, args):
        super(SDKManager, self).__call__(args)
        args.sub_func(args)

    @classmethod
    def add_parser(cls, parser):
        parser = super(SDKManager, cls).add_parser(parser)
        subparsers = parser.add_subparsers(title="subcommand")

        list_parser = subparsers.add_parser("list", help="Lists available SDKs.")
        list_parser.set_defaults(sub_func=cls.do_list)

        install_parser = subparsers.add_parser("install", help="Installs the given SDK.")
        group = install_parser.add_mutually_exclusive_group(required=True)
        group.add_argument('version', nargs='?', help="Version to install, or 'latest' for the latest.")
        group.add_argument('--tintin', help="Path to a copy of the tintin source (internal only).")
        install_parser.set_defaults(sub_func=cls.do_install)

        activate_parser = subparsers.add_parser("activate", help="Makes the given, installed SDK active.")
        activate_parser.add_argument('version', help="Version to make active.")
        activate_parser.set_defaults(sub_func=cls.do_activate)

        uninstall_parser = subparsers.add_parser("uninstall", help="Uninstalls the given SDK.")
        uninstall_parser.add_argument('--keep-emu', action="store_true", help="Skip deleting emulator data such as persistent data.")
        uninstall_parser.add_argument('version', help="Version to uninstall.")
        uninstall_parser.set_defaults(sub_func=cls.do_uninstall)

        set_channel_parser = subparsers.add_parser("set-channel", help="Sets the SDK channel.")
        set_channel_parser.add_argument('channel', help="The channel to use.")
        set_channel_parser.set_defaults(sub_func=cls.do_set_channel)

        include_path_parser = subparsers.add_parser("include-path", help="Prints out the SDK include path.")
        include_path_parser.add_argument("platform", help="The platform to give includes for.")
        include_path_parser.add_argument("--sdk", help="Optional SDK version override.")
        include_path_parser.set_defaults(sub_func=cls.do_include_path)
        return parser

    @classmethod
    def do_list(cls, args):
        current_sdk = sdk_manager.get_current_sdk()
        local_sdks = sdk_manager.list_local_sdks()
        local_sdk_versions = sdk_manager.list_local_sdk_versions()
        sorted_local_sdks = sorted(local_sdks, key=lambda x: version_to_key(x['version']), reverse=True)
        if len(local_sdks) > 0:
            print("Installed SDKs:")
            for sdk in sorted_local_sdks:
                line = sdk['version']
                if sdk['channel']:
                    line += " ({})".format(sdk['channel'])
                if sdk['version'] == current_sdk:
                    line += " (active)"
                print(line)
            print()
        else:
            print("No SDKs installed yet.")
        if sdk_manager.get_channel() != '':
            channel_text = ' ({} channel)'.format(sdk_manager.get_channel())
        else:
            channel_text = ''
        print("Available SDKs{}:".format(channel_text))
        try:
            for sdk in sdk_manager.list_remote_sdks():
                if sdk['version'] in local_sdk_versions:
                    continue
                line = sdk['version']
                if sdk['channel']:
                    line += " ({})".format(sdk['channel'])
                if sdk['version'] == current_sdk:
                    line += " (active)"
                print(line)
        except requests.RequestException:
            print("Could not fetch list of available SDKs.")

    @classmethod
    def do_install(cls, args):
        print("Installing SDK...")
        if args.tintin:
            sdk_manager.make_tintin_sdk(args.tintin)
        else:
            if re.match("https?://", args.version):
                sdk_manager.install_from_url(args.version)
            elif os.path.exists(args.version):
                sdk_manager.install_from_path(args.version)
            else:
                sdk_manager.install_remote_sdk(args.version)
        print("Installed.")

    @classmethod
    def do_uninstall(cls, args):
        print("Uninstalling SDK {}...".format(args.version))
        sdk_manager.uninstall_sdk(args.version)
        if not args.keep_emu:
            print("Deleting emulator files.")
            for platform in pebble_platforms:
                shutil.rmtree(os.path.join(get_sdk_persist_dir(platform, args.version)))
        print("Done.")

    @classmethod
    def do_activate(cls, args):
        sdk_manager.set_current_sdk(args.version)
        print("Set active SDK to {}.".format(sdk_manager.get_current_sdk()))

    @classmethod
    def do_set_channel(cls, args):
        sdk_manager.set_channel(args.channel)
        print("Set channel to {}.".format(sdk_manager.get_channel()))

    @classmethod
    def do_include_path(cls, args):
        sdk = args.sdk or sdk_manager.get_current_sdk()
        path = sdk_manager.path_for_sdk(sdk)
        path = os.path.join(path, "pebble", args.platform, "include")
        if not os.path.exists(path):
            raise MissingSDK("No platform '{}' available for SDK {}".format(args.platform, sdk))
        print(path)
<|endoftext|>


<commit_before># Copyright (c) 2017 Sony Corporation. All Rights Reserved.
#
# 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 google.protobuf.text_format as text_format
import os
import shutil
import tempfile
import zipfile

from . import expander
from nnabla.utils import nnabla_pb2

class NnpImporter:

    def _shrink_with_executor(self, executor):
        print(' Try to leave only executor[{}].'.format(executor.name))
        network = None
        for n in self._nnp.network:
            if n.name == executor.network_name:
                network = n
        if network is None:
            return None

        nnp = nnabla_pb2.NNablaProtoBuf()
        nnp.CopyFrom(self._nnp)

        nnp.ClearField('optimizer')
        nnp.ClearField('monitor')

        nnp.ClearField('network')
        net = nnp.network.add()
        net.CopyFrom(network)

        nnp.ClearField('executor')
        exe = nnp.executor.add()
        exe.CopyFrom(executor)

        return nnp

    def __init__(self, *args, **kwargs):
        self._args = args

        self._expand_network = False
        if 'expand_network' in kwargs:
            self._expand_network = kwargs['expand_network']

        self._executor_index = None
        if 'executor_index' in kwargs:
            self._executor_index = kwargs['executor_index']

    def load_parameters(self, filename):
        e = os.path.splitext(filename)[1].lower()
        if e == '.h5':
            import h5py
            with h5py.File(filename, 'r') as hd:
                keys = []

                def _get_keys(name):
                    ds = hd[name]
                    if not isinstance(ds, h5py.Dataset):
                        # Group
                        return
                    # To preserve order of parameters
                    keys.append((ds.attrs.get('index', None), name))
                hd.visit(_get_keys)
                for _, key in sorted(keys):
                    ds = hd[key]
                    parameter = self._nnp.parameter.add()
                    parameter.variable_name = key
                    parameter.shape.dim.extend(ds.shape)
                    parameter.data.extend(ds[...].flatten())
                    if ds.attrs['need_grad']:
                        parameter.need_grad = True
                    else:
                        parameter.need_grad = False

        elif e == '.protobuf':
            with open(filename, 'rb') as f:
                self._nnp.MergeFromString(f.read())

    def find_network(self, executor_name):
        net = None
        for network in self._nnp.network:
            if network.name == executor_name:
                net = network
        return net

    def find_parameter_variable(self, network):
        var_list = []
        for var in network.variable:
            if var.type == "Parameter" and var.initializer:
                var_list.append(var)
        return var_list

    def generate_parameters_data(self, var_list, batch_size):
        from nnabla.utils.load import _create_variable
        import numpy as np

        rng = np.random.RandomState(0)
        for var in var_list:
            shape = tuple(
                [d if d >= 1 else batch_size for d in var.shape.dim])
            variable = _create_variable(var, var.name, shape, rng)
            p = self._nnp.parameter.add()
            p.variable_name = variable.name
            p.shape.dim.extend(variable.shape)
            p.data.extend(variable.variable_instance.d.flatten())

    def execute(self):
        self._nnp = nnabla_pb2.NNablaProtoBuf()
        other_files = []
        for ifile in self._args:
            print('Importing {}'.format(ifile))
            ext = os.path.splitext(ifile)[1].lower()
            if ext == '.nnp':
                try:
                    tmpdir = tempfile.mkdtemp()
                    with zipfile.ZipFile(ifile, 'r') as nnpzip:
                        for name in nnpzip.namelist():
                            if os.path.splitext(name)[1].lower() in ['.nntxt', '.prototxt']:
                                nnpzip.extract(name, tmpdir)
                                with open(os.path.join(tmpdir, name), 'rt') as f:
                                    text_format.Merge(f.read(), self._nnp)
                        for name in nnpzip.namelist():  # Param
                            if os.path.splitext(name)[1].lower() in ['.protobuf', '.h5']:
                                nnpzip.extract(name, tmpdir)
                                self.load_parameters(
                                    os.path.join(tmpdir, name))
                finally:
                    shutil.rmtree(tmpdir)
            elif ext in ['.nntxt', '.prototxt']:
                with open(ifile, 'rt') as f:
                    text_format.Merge(f.read(), self._nnp)
            elif ext in ['.protobuf', '.h5']:
                self.load_parameters(ifile)
            else:
                other_files.append(ifile)

        executor_name = self._nnp.executor[0].network_name
        network = self.find_network(executor_name)
        parameter_variable_list = self.find_parameter_variable(network)
        if parameter_variable_list and not self._nnp.parameter:
            self.generate_parameters_data(
                parameter_variable_list, network.batch_size)

        if self._executor_index is not None:
            if self._executor_index < len(self._nnp.executor):
                self._nnp = self._shrink_with_executor(
                    self._nnp.executor[self._executor_index])

        if self._expand_network:
            self._nnp = expander.NnpExpander(self._nnp).execute()

        class nnp:
            pass
        nnp.protobuf = self._nnp
        nnp.other_files = other_files
        return nnp
<commit_msg>fix issue that call the removed code.<commit_after># Copyright (c) 2017 Sony Corporation. All Rights Reserved.
#
# 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 google.protobuf.text_format as text_format
import os
import shutil
import tempfile
import zipfile

from . import expander
from nnabla.utils import nnabla_pb2

class NnpImporter:

    def _shrink_with_executor(self, executor):
        print(' Try to leave only executor[{}].'.format(executor.name))
        network = None
        for n in self._nnp.network:
            if n.name == executor.network_name:
                network = n
        if network is None:
            return None

        nnp = nnabla_pb2.NNablaProtoBuf()
        nnp.CopyFrom(self._nnp)

        nnp.ClearField('optimizer')
        nnp.ClearField('monitor')

        nnp.ClearField('network')
        net = nnp.network.add()
        net.CopyFrom(network)

        nnp.ClearField('executor')
        exe = nnp.executor.add()
        exe.CopyFrom(executor)

        return nnp

    def __init__(self, *args, **kwargs):
        self._args = args

        self._expand_network = False
        if 'expand_network' in kwargs:
            self._expand_network = kwargs['expand_network']

        self._executor_index = None
        if 'executor_index' in kwargs:
            self._executor_index = kwargs['executor_index']

    def load_parameters(self, filename):
        e = os.path.splitext(filename)[1].lower()
        if e == '.h5':
            import h5py
            with h5py.File(filename, 'r') as hd:
                keys = []

                def _get_keys(name):
                    ds = hd[name]
                    if not isinstance(ds, h5py.Dataset):
                        # Group
                        return
                    # To preserve order of parameters
                    keys.append((ds.attrs.get('index', None), name))
                hd.visit(_get_keys)
                for _, key in sorted(keys):
                    ds = hd[key]
                    parameter = self._nnp.parameter.add()
                    parameter.variable_name = key
                    parameter.shape.dim.extend(ds.shape)
                    parameter.data.extend(ds[...].flatten())
                    if ds.attrs['need_grad']:
                        parameter.need_grad = True
                    else:
                        parameter.need_grad = False

        elif e == '.protobuf':
            with open(filename, 'rb') as f:
                self._nnp.MergeFromString(f.read())

    def find_network(self, executor_name):
        net = None
        for network in self._nnp.network:
            if network.name == executor_name:
                net = network
        return net

    def find_parameter_variable(self, network):
        var_list = []
        for var in network.variable:
            if var.type == "Parameter" and var.initializer:
                var_list.append(var)
        return var_list

    def generate_parameters_data(self, var_list, batch_size):
        from nnabla.core.graph_def import _create_initializer
        from nnabla.parameter import get_parameter_or_create
        import numpy as np

        rng = np.random.RandomState(0)
        for var in var_list:
            shape = tuple(
                [d if d >= 1 else batch_size for d in var.shape.dim])
            initializer = _create_initializer(var, rng)
            variable_instance = get_parameter_or_create(
                var.name, shape, initializer)
            p = self._nnp.parameter.add()
            p.variable_name = var.name
            p.shape.dim.extend(shape)
            p.data.extend(variable_instance.d.flatten())

    def execute(self):
        self._nnp = nnabla_pb2.NNablaProtoBuf()
        other_files = []
        for ifile in self._args:
            print('Importing {}'.format(ifile))
            ext = os.path.splitext(ifile)[1].lower()
            if ext == '.nnp':
                try:
                    tmpdir = tempfile.mkdtemp()
                    with zipfile.ZipFile(ifile, 'r') as nnpzip:
                        for name in nnpzip.namelist():
                            if os.path.splitext(name)[1].lower() in ['.nntxt', '.prototxt']:
                                nnpzip.extract(name, tmpdir)
                                with open(os.path.join(tmpdir, name), 'rt') as f:
                                    text_format.Merge(f.read(), self._nnp)
                        for name in nnpzip.namelist():  # Param
                            if os.path.splitext(name)[1].lower() in ['.protobuf', '.h5']:
                                nnpzip.extract(name, tmpdir)
                                self.load_parameters(
                                    os.path.join(tmpdir, name))
                finally:
                    shutil.rmtree(tmpdir)
            elif ext in ['.nntxt', '.prototxt']:
                with open(ifile, 'rt') as f:
                    text_format.Merge(f.read(), self._nnp)
            elif ext in ['.protobuf', '.h5']:
                self.load_parameters(ifile)
            else:
                other_files.append(ifile)

        executor_name = self._nnp.executor[0].network_name
        network = self.find_network(executor_name)
        parameter_variable_list = self.find_parameter_variable(network)
        if parameter_variable_list and not self._nnp.parameter:
            self.generate_parameters_data(
                parameter_variable_list, network.batch_size)

        if self._executor_index is not None:
            if self._executor_index < len(self._nnp.executor):
                self._nnp = self._shrink_with_executor(
                    self._nnp.executor[self._executor_index])

        if self._expand_network:
            self._nnp = expander.NnpExpander(self._nnp).execute()

        class nnp:
            pass
        nnp.protobuf = self._nnp
        nnp.other_files = other_files
        return nnp
<|endoftext|>


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

The podcast plugin generates plain-text or XML RSS podcasts.
'''

from __future__ import unicode_literals

import re
import collections
import os.path

from datetime import datetime
from logging import warning, info
from codecs import open
from pytz import timezone

from pelican import signals, contents
from pelican.utils import get_date

XML_HEADER = """<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<channel>
<title>{0}</title>
<link>{1}<link>
<language>{2}</language>
<itunes:subtitle>{3}</itunes:subtitle>
<itunes:author>{4}</itunes:author>
<itunes:summary>{5}</itunes:summary>
<description>{6}</description>
<itunes:owner>
    <itunes:name>{7}</itunes:name>
    <itunes:email>{8}</itunes:email>
</itunes:owner>
<itunes:explicit>no</itunes:explicit>
<itunes:image href="{9}" />
<itunes:category text="{10}"/></itunes:category>
"""

XML_ITEM = """
<item>
    <title>{0}</title>
    <description>{1}</description>
    <link>{2}</link>
    <enclosure url="{3}" type="audio/mpeg" length="1024"></enclosure>
    <pubDate>{4}</pubDate>
    <itunes:author>{5}</itunes:author>
    <itunes:duration>{6}</itunes:duration>
    <itunes:explicit>no</itunes:explicit>
    <itunes:image href="{7}"/>
    <itunes:summary>{8}</itunes:summary>
    <guid>{9}</guid>
</item>
"""

XML_FOOTER = """
</channel>
</rss>
"""

def format_date(date):
    if date.tzinfo:
        tz = date.strftime('%z')
        tz = tz[:-2] + ':' + tz[-2:]
    else:
        tz = "-00:00"
    return date.strftime("%Y-%m-%dT%H:%M:%S") + tz

class PodcastGenerator(object):

    def __init__(self, context, settings, path, theme, output_path, *null):

        self.output_path = output_path
        self.context = context
        self.now = datetime.now()
        self.siteurl = settings.get('SITEURL')

        self.default_timezone = settings.get('TIMEZONE', 'UTC')
        self.timezone = getattr(self, 'timezone', self.default_timezone)
        self.timezone = timezone(self.timezone)

        config = settings.get('PODCAST', {})

        if not isinstance(config, dict):
            warning("podcast plugin: the PODCAST setting must be a dict")
        else:
            self.config = config

    def write_item(self, article, fd):

        if not hasattr(article, 'tags'):
            return

        if not 'Podcast' in getattr(article, 'tags'):
            return

        if getattr(article, 'status', 'published') != 'published':
            return

        if getattr(article, 'private', 'False') == 'True':
            return

        # We can disable categories/authors/etc by using False instead of ''
        if not article.save_as:
            return

        page_path = os.path.join(self.output_path, article.save_as)
        if not os.path.exists(page_path):
            return

        #Exclude URLs from the sitemap:
        fd.write(XML_ITEM.format(
            article.title,
            article.description,
            self.config['item']['linkTemplate'].format(article.slug),
            self.config['item']['audioTemplate'].format(article.slug),
            article.date,
            article.author,
            article.duration,
            article.image,
            article.description,
            self.config['item']['linkTemplate'].format(article.slug)
        ))

    def generate_output(self, writer):
        path = os.path.join(self.output_path, 'podcast.xml')

        info('writing {0}'.format(path))

        with open(path, 'w', encoding='utf-8') as fd:

            fd.write(XML_HEADER.format(
                self.config['title'],
                self.config['link'],
                self.config['language'],
                self.config['itunes']['subtitle'],
                self.config['itunes']['author'],
                self.config['itunes']['summary'],
                self.config['description'],
                self.config['itunes']['owner']['name'],
                self.config['itunes']['owner']['email'],
                self.config['itunes']['image'],
                self.config['itunes']['category']))

            for article in self.context['articles']:
                self.write_item(article, fd)

            fd.write(XML_FOOTER)

def get_generators(generators):
    return PodcastGenerator

def register():
    signals.get_generators.connect(get_generators)
<commit_msg>fix itunes:category tag closing.<commit_after># -*- coding: utf-8 -*-
'''
Podcast
-------

The podcast plugin generates plain-text or XML RSS podcasts.
'''

from __future__ import unicode_literals

import re
import collections
import os.path

from datetime import datetime
from logging import warning, info
from codecs import open
from pytz import timezone

from pelican import signals, contents
from pelican.utils import get_date

XML_HEADER = """<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<channel>
<title>{0}</title>
<link>{1}<link>
<language>{2}</language>
<itunes:subtitle>{3}</itunes:subtitle>
<itunes:author>{4}</itunes:author>
<itunes:summary>{5}</itunes:summary>
<description>{6}</description>
<itunes:owner>
    <itunes:name>{7}</itunes:name>
    <itunes:email>{8}</itunes:email>
</itunes:owner>
<itunes:explicit>no</itunes:explicit>
<itunes:image href="{9}" />
<itunes:category text="{10}"/>
"""

XML_ITEM = """
<item>
    <title>{0}</title>
    <description>{1}</description>
    <link>{2}</link>
    <enclosure url="{3}" type="audio/mpeg" length="1024"></enclosure>
    <pubDate>{4}</pubDate>
    <itunes:author>{5}</itunes:author>
    <itunes:duration>{6}</itunes:duration>
    <itunes:explicit>no</itunes:explicit>
    <itunes:image href="{7}"/>
    <itunes:summary>{8}</itunes:summary>
    <guid>{9}</guid>
</item>
"""

XML_FOOTER = """
</channel>
</rss>
"""

def format_date(date):
    if date.tzinfo:
        tz = date.strftime('%z')
        tz = tz[:-2] + ':' + tz[-2:]
    else:
        tz = "-00:00"
    return date.strftime("%Y-%m-%dT%H:%M:%S") + tz

class PodcastGenerator(object):

    def __init__(self, context, settings, path, theme, output_path, *null):

        self.output_path = output_path
        self.context = context
        self.now = datetime.now()
        self.siteurl = settings.get('SITEURL')

        self.default_timezone = settings.get('TIMEZONE', 'UTC')
        self.timezone = getattr(self, 'timezone', self.default_timezone)
        self.timezone = timezone(self.timezone)

        config = settings.get('PODCAST', {})

        if not isinstance(config, dict):
            warning("podcast plugin: the PODCAST setting must be a dict")
        else:
            self.config = config

    def write_item(self, article, fd):

        if not hasattr(article, 'tags'):
            return

        if not 'Podcast' in getattr(article, 'tags'):
            return

        if getattr(article, 'status', 'published') != 'published':
            return

        if getattr(article, 'private', 'False') == 'True':
            return

        # We can disable categories/authors/etc by using False instead of ''
        if not article.save_as:
            return

        page_path = os.path.join(self.output_path, article.save_as)
        if not os.path.exists(page_path):
            return

        #Exclude URLs from the sitemap:
        fd.write(XML_ITEM.format(
            article.title,
            article.description,
            self.config['item']['linkTemplate'].format(article.slug),
            self.config['item']['audioTemplate'].format(article.slug),
            article.date,
            article.author,
            article.duration,
            article.image,
            article.description,
            self.config['item']['linkTemplate'].format(article.slug)
        ))

    def generate_output(self, writer):
        path = os.path.join(self.output_path, 'podcast.xml')

        info('writing {0}'.format(path))

        with open(path, 'w', encoding='utf-8') as fd:

            fd.write(XML_HEADER.format(
                self.config['title'],
                self.config['link'],
                self.config['language'],
                self.config['itunes']['subtitle'],
                self.config['itunes']['author'],
                self.config['itunes']['summary'],
                self.config['description'],
                self.config['itunes']['owner']['name'],
                self.config['itunes']['owner']['email'],
                self.config['itunes']['image'],
                self.config['itunes']['category']))

            for article in self.context['articles']:
                self.write_item(article, fd)

            fd.write(XML_FOOTER)

def get_generators(generators):
    return PodcastGenerator

def register():
    signals.get_generators.connect(get_generators)
<|endoftext|>


<commit_before>import math
import shutil
from pathlib import Path

import pytest
from fontTools.designspaceLib import DesignSpaceDocument
from fontTools.designspaceLib.split import (
    _conditionSetFrom,
    convert5to4,
    splitInterpolable,
    splitVariableFonts,
)
from fontTools.designspaceLib.types import ConditionSet, Range

from .fixtures import datadir

UPDATE_REFERENCE_OUT_FILES_INSTEAD_OF_TESTING = False

@pytest.mark.parametrize(
    "test_ds,expected_interpolable_spaces",
    [
        (
            "test_v5_aktiv.designspace",
            [
                (
                    {},
                    {
                        "AktivGroteskVF_Italics_Wght",
                        "AktivGroteskVF_Italics_WghtWdth",
                        "AktivGroteskVF_Wght",
                        "AktivGroteskVF_WghtWdth",
                        "AktivGroteskVF_WghtWdthItal",
                    },
                )
            ],
        ),
        (
            "test_v5_sourceserif.designspace",
            [
                (
                    {"italic": 0},
                    {"SourceSerif4Variable-Roman"},
                ),
                (
                    {"italic": 1},
                    {"SourceSerif4Variable-Italic"},
                ),
            ],
        ),
        (
            "test_v5_MutatorSans_and_Serif.designspace",
            [
                (
                    {"serif": 0},
                    {
                        "MutatorSansVariable_Weight_Width",
                        "MutatorSansVariable_Weight",
                        "MutatorSansVariable_Width",
                    },
                ),
                (
                    {"serif": 1},
                    {
                        "MutatorSerifVariable_Width",
                    },
                ),
            ],
        ),
    ],
)
def test_split(datadir, tmpdir, test_ds, expected_interpolable_spaces):
    data_in = datadir / test_ds
    temp_in = Path(tmpdir) / test_ds
    shutil.copy(data_in, temp_in)
    doc = DesignSpaceDocument.fromfile(temp_in)

    for i, (location, sub_doc) in enumerate(splitInterpolable(doc)):
        expected_location, expected_vf_names = expected_interpolable_spaces[i]
        assert location == expected_location
        vfs = list(splitVariableFonts(sub_doc))
        assert expected_vf_names == set(vf[0] for vf in vfs)

        loc_str = "_".join(f"{name}_{value}"for name, value in sorted(location.items()))
        data_out = datadir / "split_output" / f"{temp_in.stem}_{loc_str}.designspace"
        temp_out = Path(tmpdir) / "out" / f"{temp_in.stem}_{loc_str}.designspace"
        temp_out.parent.mkdir(exist_ok=True)
        sub_doc.write(temp_out)

        if UPDATE_REFERENCE_OUT_FILES_INSTEAD_OF_TESTING:
            data_out.write_text(temp_out.read_text(encoding="utf-8"), encoding="utf-8")
        else:
            assert data_out.read_text(encoding="utf-8") == temp_out.read_text(
                encoding="utf-8"
            )

        for vf_name, vf_doc in vfs:
            data_out = (datadir / "split_output" / vf_name).with_suffix(".designspace")
            temp_out = (Path(tmpdir) / "out" / vf_name).with_suffix(".designspace")
            temp_out.parent.mkdir(exist_ok=True)
            vf_doc.write(temp_out)

            if UPDATE_REFERENCE_OUT_FILES_INSTEAD_OF_TESTING:
                data_out.write_text(
                    temp_out.read_text(encoding="utf-8"), encoding="utf-8"
                )
            else:
                assert data_out.read_text(encoding="utf-8") == temp_out.read_text(
                    encoding="utf-8"
                )

@pytest.mark.parametrize(
    "test_ds,expected_vfs",
    [
        (
            "test_v5_aktiv.designspace",
            {
                "AktivGroteskVF_Italics_Wght",
                "AktivGroteskVF_Italics_WghtWdth",
                "AktivGroteskVF_Wght",
                "AktivGroteskVF_WghtWdth",
                "AktivGroteskVF_WghtWdthItal",
            },
        ),
        (
            "test_v5_sourceserif.designspace",
            {
                "SourceSerif4Variable-Italic",
                "SourceSerif4Variable-Roman",
            },
        ),
    ],
)
def test_convert5to4(datadir, tmpdir, test_ds, expected_vfs):
    data_in = datadir / test_ds
    temp_in = tmpdir / test_ds
    shutil.copy(data_in, temp_in)
    doc = DesignSpaceDocument.fromfile(temp_in)

    variable_fonts = convert5to4(doc)

    assert variable_fonts.keys() == expected_vfs
    for vf_name, vf in variable_fonts.items():
        data_out = (datadir / "convert5to4_output" / vf_name).with_suffix(".designspace")
        temp_out = (Path(tmpdir) / "out" / vf_name).with_suffix(".designspace")
        temp_out.parent.mkdir(exist_ok=True)
        vf.write(temp_out)

        if UPDATE_REFERENCE_OUT_FILES_INSTEAD_OF_TESTING:
            data_out.write_text(temp_out.read_text(encoding="utf-8"), encoding="utf-8")
        else:
            assert data_out.read_text(encoding="utf-8") == temp_out.read_text(
                encoding="utf-8"
            )

@pytest.mark.parametrize(
    ["unbounded_condition"],
    [
        ({"name": "Weight", "minimum": 500, "maximum": None},),
        ({"name": "Weight", "minimum": None, "maximum": 500},),
    ],
)
def test_optional_min_max(unbounded_condition):
    """Check that split functions can handle conditions that are partially
    unbounded without tripping over the None values."""
    doc = DesignSpaceDocument()

    doc.addAxisDescriptor(
        name="Weight", tag="wght", minimum=400, maximum=1000, default=400
    )

    doc.addRuleDescriptor(
        name="unbounded",
        conditionSets=[[unbounded_condition]],
    )

    assert len(list(splitInterpolable(doc))) == 1
    assert len(list(splitVariableFonts(doc))) == 1

@pytest.mark.parametrize(
    ["condition", "expected_set"],
    [
        (
            {"name": "axis", "minimum": 0.5, "maximum": None},
            {"axis": Range(minimum=0.5, maximum=math.inf)},
        ),
        (
            {"name": "axis", "minimum": None, "maximum": 0.5},
            {"axis": Range(minimum=-math.inf, maximum=0.5)},
        ),
    ],
)
def test_optional_min_max_internal(condition, expected_set: ConditionSet):
    """Check that split's internal helper functions produce the correct output
    for conditions that are partially unbounded."""
    assert _conditionSetFrom([condition]) == expected_set
<commit_msg>Confirm split functions can handle unbounded conditions with absent keys<commit_after>import math
import shutil
from pathlib import Path

import pytest
from fontTools.designspaceLib import DesignSpaceDocument
from fontTools.designspaceLib.split import (
    _conditionSetFrom,
    convert5to4,
    splitInterpolable,
    splitVariableFonts,
)
from fontTools.designspaceLib.types import ConditionSet, Range

from .fixtures import datadir

UPDATE_REFERENCE_OUT_FILES_INSTEAD_OF_TESTING = False

@pytest.mark.parametrize(
    "test_ds,expected_interpolable_spaces",
    [
        (
            "test_v5_aktiv.designspace",
            [
                (
                    {},
                    {
                        "AktivGroteskVF_Italics_Wght",
                        "AktivGroteskVF_Italics_WghtWdth",
                        "AktivGroteskVF_Wght",
                        "AktivGroteskVF_WghtWdth",
                        "AktivGroteskVF_WghtWdthItal",
                    },
                )
            ],
        ),
        (
            "test_v5_sourceserif.designspace",
            [
                (
                    {"italic": 0},
                    {"SourceSerif4Variable-Roman"},
                ),
                (
                    {"italic": 1},
                    {"SourceSerif4Variable-Italic"},
                ),
            ],
        ),
        (
            "test_v5_MutatorSans_and_Serif.designspace",
            [
                (
                    {"serif": 0},
                    {
                        "MutatorSansVariable_Weight_Width",
                        "MutatorSansVariable_Weight",
                        "MutatorSansVariable_Width",
                    },
                ),
                (
                    {"serif": 1},
                    {
                        "MutatorSerifVariable_Width",
                    },
                ),
            ],
        ),
    ],
)
def test_split(datadir, tmpdir, test_ds, expected_interpolable_spaces):
    data_in = datadir / test_ds
    temp_in = Path(tmpdir) / test_ds
    shutil.copy(data_in, temp_in)
    doc = DesignSpaceDocument.fromfile(temp_in)

    for i, (location, sub_doc) in enumerate(splitInterpolable(doc)):
        expected_location, expected_vf_names = expected_interpolable_spaces[i]
        assert location == expected_location
        vfs = list(splitVariableFonts(sub_doc))
        assert expected_vf_names == set(vf[0] for vf in vfs)

        loc_str = "_".join(f"{name}_{value}"for name, value in sorted(location.items()))
        data_out = datadir / "split_output" / f"{temp_in.stem}_{loc_str}.designspace"
        temp_out = Path(tmpdir) / "out" / f"{temp_in.stem}_{loc_str}.designspace"
        temp_out.parent.mkdir(exist_ok=True)
        sub_doc.write(temp_out)

        if UPDATE_REFERENCE_OUT_FILES_INSTEAD_OF_TESTING:
            data_out.write_text(temp_out.read_text(encoding="utf-8"), encoding="utf-8")
        else:
            assert data_out.read_text(encoding="utf-8") == temp_out.read_text(
                encoding="utf-8"
            )

        for vf_name, vf_doc in vfs:
            data_out = (datadir / "split_output" / vf_name).with_suffix(".designspace")
            temp_out = (Path(tmpdir) / "out" / vf_name).with_suffix(".designspace")
            temp_out.parent.mkdir(exist_ok=True)
            vf_doc.write(temp_out)

            if UPDATE_REFERENCE_OUT_FILES_INSTEAD_OF_TESTING:
                data_out.write_text(
                    temp_out.read_text(encoding="utf-8"), encoding="utf-8"
                )
            else:
                assert data_out.read_text(encoding="utf-8") == temp_out.read_text(
                    encoding="utf-8"
                )

@pytest.mark.parametrize(
    "test_ds,expected_vfs",
    [
        (
            "test_v5_aktiv.designspace",
            {
                "AktivGroteskVF_Italics_Wght",
                "AktivGroteskVF_Italics_WghtWdth",
                "AktivGroteskVF_Wght",
                "AktivGroteskVF_WghtWdth",
                "AktivGroteskVF_WghtWdthItal",
            },
        ),
        (
            "test_v5_sourceserif.designspace",
            {
                "SourceSerif4Variable-Italic",
                "SourceSerif4Variable-Roman",
            },
        ),
    ],
)
def test_convert5to4(datadir, tmpdir, test_ds, expected_vfs):
    data_in = datadir / test_ds
    temp_in = tmpdir / test_ds
    shutil.copy(data_in, temp_in)
    doc = DesignSpaceDocument.fromfile(temp_in)

    variable_fonts = convert5to4(doc)

    assert variable_fonts.keys() == expected_vfs
    for vf_name, vf in variable_fonts.items():
        data_out = (datadir / "convert5to4_output" / vf_name).with_suffix(".designspace")
        temp_out = (Path(tmpdir) / "out" / vf_name).with_suffix(".designspace")
        temp_out.parent.mkdir(exist_ok=True)
        vf.write(temp_out)

        if UPDATE_REFERENCE_OUT_FILES_INSTEAD_OF_TESTING:
            data_out.write_text(temp_out.read_text(encoding="utf-8"), encoding="utf-8")
        else:
            assert data_out.read_text(encoding="utf-8") == temp_out.read_text(
                encoding="utf-8"
            )

@pytest.mark.parametrize(
    ["unbounded_condition"],
    [
        ({"name": "Weight", "minimum": 500},),
        ({"name": "Weight", "maximum": 500},),
        ({"name": "Weight", "minimum": 500, "maximum": None},),
        ({"name": "Weight", "minimum": None, "maximum": 500},),
    ],
)
def test_optional_min_max(unbounded_condition):
    """Check that split functions can handle conditions that are partially
    unbounded without tripping over None values and missing keys."""
    doc = DesignSpaceDocument()

    doc.addAxisDescriptor(
        name="Weight", tag="wght", minimum=400, maximum=1000, default=400
    )

    doc.addRuleDescriptor(
        name="unbounded",
        conditionSets=[[unbounded_condition]],
    )

    assert len(list(splitInterpolable(doc))) == 1
    assert len(list(splitVariableFonts(doc))) == 1

@pytest.mark.parametrize(
    ["condition", "expected_set"],
    [
        (
            {"name": "axis", "minimum": 0.5},
            {"axis": Range(minimum=0.5, maximum=math.inf)},
        ),
        (
            {"name": "axis", "maximum": 0.5},
            {"axis": Range(minimum=-math.inf, maximum=0.5)},
        ),
        (
            {"name": "axis", "minimum": 0.5, "maximum": None},
            {"axis": Range(minimum=0.5, maximum=math.inf)},
        ),
        (
            {"name": "axis", "minimum": None, "maximum": 0.5},
            {"axis": Range(minimum=-math.inf, maximum=0.5)},
        ),
    ],
)
def test_optional_min_max_internal(condition, expected_set: ConditionSet):
    """Check that split's internal helper functions produce the correct output
    for conditions that are partially unbounded."""
    assert _conditionSetFrom([condition]) == expected_set
<|endoftext|>


<commit_before>import os
import argparse
import cppyy

def get_naoth_dir():
    script_path = os.path.abspath(__file__)
    return os.path.abspath(os.path.join(script_path, "../../../../../"))

def get_toolchain_dir():
    return os.path.abspath(os.environ["NAOTH_TOOLCHAIN_PATH"])

def get_files_of_dir(d):
    imageFiles = [os.path.join(d,f) for f in os.listdir(d) if os.path.isfile(os.path.join(d,f)) and f.endswith(".png")]

    # TODO: add camera matrix to result
    # TODO: parse XML and add ground truth to result

    return imageFiles

class Evaluator:

    def __init__(self):
        naoth_dir = get_naoth_dir()
        toolchain_dir = get_toolchain_dir()

        # load shared library: all depending libraries should be found automatically
        cppyy.load_library(os.path.join(
            naoth_dir, "NaoTHSoccer/dist/Native/libdummysimulator.so"))

        # add relevant include paths to allow mapping our code
        cppyy.add_include_path(os.path.join(naoth_dir, "Framework/Commons/Source"))
        cppyy.add_include_path(os.path.join(naoth_dir, "NaoTHSoccer/Source"))
        cppyy.add_include_path(os.path.join(toolchain_dir, "toolchain_native/extern/include"))

        # include platform
        cppyy.include(os.path.join(
            naoth_dir, "Framework/Commons/Source/PlatformInterface/Platform.h"))
        cppyy.include(os.path.join(
            naoth_dir, "Framework/Platforms/Source/DummySimulator/DummySimulator.h"))

        # change working directory so that the configuration is found
        os.chdir(os.path.join(naoth_dir, "NaoTHSoccer"))

        # start dummy simulator
        self.sim = cppyy.gbl.DummySimulator(False, False, 5401)
        cppyy.gbl.naoth.Platform.getInstance().init(self.sim)

        # create the cognition and motion objects
        cog = cppyy.gbl.createCognition()
        mo = cppyy.gbl.createMotion()

        # cast to callable
        callable_cog = cppyy.bind_object(
            cppyy.addressof(cog), cppyy.gbl.naoth.Callable)
        callable_mo = cppyy.bind_object(
            cppyy.addressof(mo), cppyy.gbl.naoth.Callable)

        self.sim.registerCognition(callable_cog)
        self.sim.registerMotion(callable_mo)

        # get access to the module manager and return it to the calling function
        cppyy.include(os.path.join(naoth_dir, "Framework/Commons/Source/ModuleFramework/ModuleManager.h"))
        cppyy.include(os.path.join(naoth_dir, "NaoTHSoccer/Source/Cognition/Cognition.h"))

        self.moduleManager = cppyy.gbl.getModuleManager(cog)

        # get the ball detector module
        self.ball_detector = self.moduleManager.getModule("CNNBallDetector").getModule()

    def execute(self, directories):
        for d in directories:
            imageFiles = get_files_of_dir(d)
            for f in imageFiles:
                self.set_images_from_file(f)
                self.sim.executeFrame()

    def set_images_from_file(self, file, bottom=True):
        # get reference to the image input representation
        if bottom:
            image = self.ball_detector.getRequire().at("Image")
        else:
            image = self.ball_detector.getRequire().at("ImageTop")

        print(file)
        # TODO: read  image from file with opencv
        # TODO: convert image
        # TODO: move image into representation

if __name__ == "__main__":

    parser = argparse.ArgumentParser(description='Evaluate ball detection on logfile images annotated with CVAT.')
    parser.add_argument('directory', nargs='+',
                        help='A list of directories containing the images files (as png with the integrated camera matrix)')

    args = parser.parse_args()

    evaluator = Evaluator()
    evaluator.execute(args.directory)
<commit_msg>Load image files and move them into representation<commit_after>import os
import argparse
import cppyy
import PIL.Image
from typing import NamedTuple
import numpy as np
import ctypes

def get_naoth_dir():
    script_path = os.path.abspath(__file__)
    return os.path.abspath(os.path.join(script_path, "../../../../../"))

def get_toolchain_dir():
    return os.path.abspath(os.environ["NAOTH_TOOLCHAIN_PATH"])

class Frame(NamedTuple):
    file: str

def get_frames_for_dir(d):
    file_names = os.listdir(d)
    file_names.sort()
    imageFiles = [os.path.join(d, f) for f in file_names if os.path.isfile(
        os.path.join(d, f)) and f.endswith(".png")]

    result = list()

    for file in imageFiles:
        frame = Frame(file)
        # TODO: parse camera matrix using PIL to result
        # TODO: parse XML and add ground truth to result
        result.append(frame)

    return result

def load_image(f: Frame):
    # don't import cv globally, because the dummy simulator shared library might need to load a non-system library
    # and we need to make sure loading the dummy simulator shared library happens first
    import cv2 as cv

    cv_img = cv.imread(f)

    # convert image for bottom to yuv422
    cv_img = cv.cvtColor(cv_img, cv.COLOR_BGR2YUV).tobytes()
    yuv422 = np.ndarray(480*640*2, np.uint8)
    for i in range(0, 480 * 640, 2):
            yuv422[i*2] = cv_img[i*3]
            yuv422[i*2 + 1] = (cv_img[i*3 + 1] + cv_img[i*3 + 4]) / 2.0
            yuv422[i*2 + 2] = cv_img[i*3 + 3]
            yuv422[i*2 + 3] = (cv_img[i*3 + 2] + cv_img[i*3 + 5]) / 2.0
    return yuv422

class Evaluator:

    def __init__(self):
        naoth_dir = get_naoth_dir()
        toolchain_dir = get_toolchain_dir()

        # load shared library: all depending libraries should be found automatically
        cppyy.load_library(os.path.join(
            naoth_dir, "NaoTHSoccer/dist/Native/libdummysimulator.so"))

        # add relevant include paths to allow mapping our code
        cppyy.add_include_path(os.path.join(
            naoth_dir, "Framework/Commons/Source"))
        cppyy.add_include_path(os.path.join(naoth_dir, "NaoTHSoccer/Source"))
        cppyy.add_include_path(os.path.join(
            toolchain_dir, "toolchain_native/extern/include"))

        # include platform
        cppyy.include(os.path.join(
            naoth_dir, "Framework/Commons/Source/PlatformInterface/Platform.h"))
        cppyy.include(os.path.join(
            naoth_dir, "Framework/Platforms/Source/DummySimulator/DummySimulator.h"))

        # change working directory so that the configuration is found
        os.chdir(os.path.join(naoth_dir, "NaoTHSoccer"))

        # start dummy simulator
        self.sim = cppyy.gbl.DummySimulator(False, False, 5401)
        cppyy.gbl.naoth.Platform.getInstance().init(self.sim)

        # create the cognition and motion objects
        cog = cppyy.gbl.createCognition()
        mo = cppyy.gbl.createMotion()

        # cast to callable
        callable_cog = cppyy.bind_object(
            cppyy.addressof(cog), cppyy.gbl.naoth.Callable)
        callable_mo = cppyy.bind_object(
            cppyy.addressof(mo), cppyy.gbl.naoth.Callable)

        self.sim.registerCognition(callable_cog)
        self.sim.registerMotion(callable_mo)

        # get access to the module manager and return it to the calling function
        cppyy.include(os.path.join(
            naoth_dir, "Framework/Commons/Source/ModuleFramework/ModuleManager.h"))
        cppyy.include(os.path.join(
            naoth_dir, "NaoTHSoccer/Source/Cognition/Cognition.h"))

        self.moduleManager = cppyy.gbl.getModuleManager(cog)

        # get the ball detector module
        self.ball_detector = self.moduleManager.getModule(
            "CNNBallDetector").getModule()

    def set_current_frame(self, frame, bottom=True):
        # get reference to the image input representation
        if bottom:
            imageRepresentation = self.ball_detector.getRequire().at("Image")
        else:
            imageRepresentation = self.ball_detector.getRequire().at("ImageTop")

        print(frame.file)

        # load image in YUV422 format
        yuv422 = load_image(frame.file)
        p_data = yuv422.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8))
        # move image into representation
        imageRepresentation.copyImageDataYUV422(p_data, yuv422.size)

    def execute(self, directories):
        for d in directories:
            frames = get_frames_for_dir(d)
            for f in frames:
                self.set_current_frame(f)
                self.sim.executeFrame()

if __name__ == "__main__":

    parser = argparse.ArgumentParser(
        description='Evaluate ball detection on logfile images annotated with CVAT.')
    parser.add_argument('directory', nargs='+',
                        help='A list of directories containing the images files (as png with the integrated camera matrix)')

    args = parser.parse_args()

    evaluator = Evaluator()
    evaluator.execute(args.directory)
<|endoftext|>


<commit_before>from django.core.management import BaseCommand

from corehq.apps.accounting.models import DefaultProductPlan
from corehq.apps.accounting.subscription_changes import DomainDowngradeStatusHandler
from corehq.apps.domain.models import Domain

class Command(BaseCommand):
    help = 'Print out a CSV containing a table of what features are in use by project space.'

    def handle(self, *args, **kwargs):
        domain_names = args
        privileges = sorted(DomainDowngradeStatusHandler.supported_privileges())
        print ','.join(['Project Space'] + privileges + ['Lowest Plan'])
        for domain in filter(lambda domain: domain, map(Domain.get_by_name, domain_names)):
            is_privilege_being_used = {
                priv: _is_domain_using_privilege(domain, priv)
                for priv in DomainDowngradeStatusHandler.supported_privileges()
            }
            using_privileges = [priv for (priv, is_in_use) in is_privilege_being_used.items() if is_in_use]
            minimum_plan = DefaultProductPlan.get_lowest_edition_by_domain(domain.name, using_privileges)
            print ','.join(
                [domain.name] +
                ['X' if is_privilege_being_used[priv] else '' for priv in privileges] +
                [minimum_plan]
            )

def _is_domain_using_privilege(domain, privilege):
    if domain.has_privilege(privilege):
        return bool(DomainDowngradeStatusHandler.privilege_to_response_function()[privilege](domain))
    return False
<commit_msg>check for other privileges that Ops is interested in<commit_after>from django.core.management import BaseCommand

from corehq import privileges
from corehq.apps.accounting.models import DefaultProductPlan
from corehq.apps.accounting.subscription_changes import DomainDowngradeStatusHandler
from corehq.apps.app_manager.dbaccessors import get_all_apps
from corehq.apps.app_manager.util import app_callout_templates
from corehq.apps.domain.models import Domain

class Command(BaseCommand):
    help = 'Print out a CSV containing a table of what features are in use by project space.'

    def handle(self, *args, **kwargs):
        domain_names = args
        privileges = sorted(_privilege_to_response_function().keys())
        print ','.join(['Project Space'] + privileges + ['Lowest Plan'])
        for domain in filter(lambda domain: domain, map(Domain.get_by_name, domain_names)):
            is_privilege_being_used = {
                priv: _is_domain_using_privilege(domain, priv)
                for priv in privileges
            }
            using_privileges = [priv for (priv, is_in_use) in is_privilege_being_used.items() if is_in_use]
            minimum_plan = DefaultProductPlan.get_lowest_edition_by_domain(domain.name, using_privileges)
            print ','.join(
                [domain.name] +
                ['X' if is_privilege_being_used[priv] else '' for priv in privileges] +
                [minimum_plan]
            )

def _privilege_to_response_function():
    privilege_to_response_function = DomainDowngradeStatusHandler.privilege_to_response_function()
    privilege_to_response_function.update({
        privileges.CUSTOM_REPORTS: _domain_has_custom_report,
        privileges.LOCATIONS: _domain_uses_locations,
        privileges.TEMPLATED_INTENTS: _domain_has_apps_using_templated_intents,
        privileges.CUSTOM_INTENTS: _domain_has_apps_using_custom_intents,
    })
    return privilege_to_response_function

def _is_domain_using_privilege(domain, privilege):
    if domain.has_privilege(privilege):
        return bool(_privilege_to_response_function()[privilege](domain))
    return False

def _domain_has_custom_report(domain):
    from corehq.apps.reports.dispatcher import CustomProjectReportDispatcher
    return bool(CustomProjectReportDispatcher().get_reports(domain.name))

def _domain_uses_locations(domain):
    return domain.uses_locations

def _domain_has_apps_using_templated_intents(domain):
    templates = next(app_callout_templates)
    template_ids = set([t['id'] for t in templates])
    return any(
        any(
            any(
                intent in template_ids
                for intent in form.wrapped_xform().odk_intents
            )
            for form in app.get_forms()
        )
        for app in get_all_apps(domain.name)
    )

def _domain_has_apps_using_custom_intents(domain):
    templates = next(app_callout_templates)
    template_ids = set([t['id'] for t in templates])
    return any(
        any(
            any(set(form.wrapped_xform().odk_intents) - template_ids)
            for form in app.get_forms()
        )
        for app in get_all_apps(domain.name)
    )
<|endoftext|>