<commit_before>from ChatExchange.chatexchange import events
from ChatExchange.chatexchange.client import Client
from ChatExchange.chatexchange.messages import Message
import collections
import itertools
import os.path
import pickle
import regex
import sys
import threading
import time
import yaml

import datahandling
from deletionwatcher import DeletionWatcher
from excepthook import log_exception
from globalvars import GlobalVars
from parsing import fetch_post_url_from_msg_content, fetch_owner_url_from_msg_content

LastMessages = collections.namedtuple("LastMessages", ["messages", "reports"])

class RoomData:
    def __init__(self, room, lock, block_time, last_report_data, deletion_watcher):
        self.room = room
        self.lock = lock
        self.block_time = block_time
        self.last_report_data = last_report_data
        self.deletion_watcher = deletion_watcher

class CmdException(Exception):
    pass

_commands = {"reply": {}, "prefix": {}}

_clients = {
    "stackexchange.com": None,
    "stackoverflow.com": None,
    "meta.stackexchange.com": None
}

_command_rooms = set()
_watcher_rooms = set()
_room_roles = {}
_privileges = {}

_global_block = -1
_rooms = {}
_last_messages = LastMessages({}, collections.OrderedDict())

_pickle_run = threading.Event()

def init(username, password):
    global _clients
    global _rooms
    global _room_data
    global _last_messages

    for site in _clients.keys():
        client = Client(site)

        for _ in range(10):
            try:
                client.login(username, password)
                break
            except:
                pass
        else:
            raise Exception("Failed to log into " + site)

        _clients[site] = client

    parse_room_config("rooms.yml")

    if not GlobalVars.standby_mode:
        for site, roomid in _command_rooms:
            room = _clients[site].get_room(roomid)
            deletion_watcher = (site, roomid) in _watcher_rooms

            room.join()
            room.watch_socket(lambda msg, client: on_msg(msg, client, room))
            _rooms[(site, roomid)] = RoomData(room, threading.Event(), -1, (), deletion_watcher)
            _rooms[(site, roomid)].lock.set()

    if os.path.isfile("messageData.p"):
        _last_messages = pickle.load(open("messageData.p", "rb"))

    threading.Thread(name="pickle ---rick--- runner", target=pickle_last_messages).start()

def parse_room_config(path):
    with open(path, "r") as room_config:
        room_dict = yaml.load(room_config.read())

        for site, site_rooms in room_dict.items():
            for roomid, room in site_rooms.items():
                room_identifier = (site, roomid)
                _privileges[room_identifier] = set(room["privileges"]) if "privileges" in room else set()

                if "commands" in room and room["commands"]:
                    _command_rooms.add(room_identifier)

                if "watcher" in room and room["watcher"]:
                    _watcher_rooms.add(room_identifier)

                if "msg_types" in room:
                    add_room(room_identifier, room["msg_types"])

def add_room(room, roles):
    for role in roles:
        if role not in _room_roles:
            _room_roles[role] = set()

        _room_roles[role].add(room)

def pickle_last_messages():
    while True:
        _pickle_run.wait()
        _pickle_run.clear()

        with open("messageData.p", "wb") as pickle_file:
            pickle.dump(_last_messages, pickle_file)

def on_msg(msg, client, room):
    if isinstance(msg, events.MessagePosted) or isinstance(msg, events.MessageEdited):
        message = msg.message

        if message.owner.id == client._br.user_id:
            identifier = (client.host, room.id)
            room_data = _rooms[identifier]

            if identifier not in _last_messages.messages:
                _last_messages.messages[identifier] = collections.deque((message.id,))
            else:
                last = _last_messages.messages[identifier]

                if len(last) > 100:
                    last.popleft()

                last.append(message.id)

            if room_data.last_report_data != ():
                _last_messages.reports[message.id] = room_data.last_report_data

                if len(_last_messages.reports) > 50:
                    _last_messages.reports.popitem(last=False)

                threading.Thread(name="deletion watcher",
                                 target=DeletionWatcher.check_if_report_was_deleted,
                                 args=(room_data.last_report_data[0], message))

                room_data.last_report_data = ()

            room_data.lock.set()
            _pickle_run.set()
        elif message.parent and message.parent.owner.id == client._br.user_id:
            command = message.content.split(" ", 1)[1]
            result = dispatch_reply_command(message.parent, message, command)

            if result:
                message.reply(result)
        elif message.content.startswith("sd "):
            result = dispatch_shorthand_command(message, room)

            if result:
                message.reply(result)
        elif message.content.startswith("!!/"):
            result = dispatch_command(message)

            if result:
                message.reply(result)

def send_to_room(room, msg, report_data=()):
    timestamp = time.time()

    if room.block_time < timestamp and _global_block < timestamp:
        msg = msg.rstrip()

        if report_data:
            room.last_report_data = report_data

            if "delay" in _room_roles and room in _room_roles["delay"]:
                threading.Thread(name="delayed post",
                                 target=DeletionWatcher.post_message_if_not_deleted,
                                 args=(report_data[0], msg, room))
                return

        room.lock.wait()
        room.lock.clear()
        room.room.send_message(msg)

def tell_rooms_with(prop, msg, notify_site="", report_data=()):
    tell_rooms(msg, (prop,), (), notify_site=notify_site, report_data=report_data)

def tell_rooms_without(prop, msg, notify_site="", report_data=()):
    tell_rooms(msg, (), (prop,), notify_site=notify_site, report_data=report_data)

def tell_rooms(msg, has, hasnt, notify_site="", report_data=()):
    global _rooms

    target_rooms = set()

    for prop_has in has:
        if prop_has not in _room_roles:
            continue

        for room in _room_roles[prop_has]:
            if all(map(lambda prop: prop not in _room_roles or room not in _room_roles[prop], hasnt)):
                if room not in _rooms:
                    site, roomid = room
                    deletion_watcher = room in _watcher_rooms

                    new_room = _clients[site].get_room(roomid)
                    new_room.join()

                    _rooms[room] = RoomData(new_room, threading.Event(), -1, (), deletion_watcher)
                    _rooms[room].lock.set()

                target_rooms.add(_rooms[room])

    for room in target_rooms:
        if notify_site:
            pings = datahandling.get_user_names_on_notification_list(room.room._client.host,
                                                                     room.room.id,
                                                                     notify_site,
                                                                     room.room._client)

            msg = datahandling.append_pings(msg, pings)

        send_to_room(room, msg, report_data=report_data)

def get_last_messages(room, count):
    for msg_id in itertools.islice(reversed(_last_messages.messages[(room._client.host, room.id)]), count):
        yield room._client.get_message(msg_id)

def get_report_data(message):
    if message.id in _last_messages.reports:
        return _last_messages.reports[message.id]
    else:
        return (fetch_post_url_from_msg_content(message.content), fetch_owner_url_from_msg_content(message.content))

def is_privileged(user, room):
    return user.id in _privileges[(room._client.host, room.id)] or user.is_moderator

def block_room(room_id, site, time):
    global _global_block

    if room_id is None:
        _global_block = time
    else:
        _rooms[(site, room_id)].block_time = time

def command(*type_signature, reply=False, whole_msg=False, privileged=False, arity=None, aliases=None, give_name=False):
    if aliases is None:
        aliases = []

    def decorator(func):
        def f(*args, original_msg=None, alias_used=None, quiet_action=False):
            if privileged and not is_privileged(original_msg.owner, original_msg.room):
                return GlobalVars.not_privileged_warning

            if whole_msg:
                processed_args = [original_msg]
            else:
                processed_args = []

            try:
                processed_args.extend([get_type(arg) if arg else arg for get_type, arg in zip(type_signature, args)])

                result = func(*processed_args, **({"alias_used": alias_used} if give_name else {}))
                return result if not quiet_action else ""
            except CmdException as e:
                return str(e)
            except:
                log_exception(*sys.exc_info())
                return "I hit an error while trying to run that command; run `!!/errorlogs` for details."

        cmd = (f, arity if arity else (len(type_signature), len(type_signature)))

        if reply:
            _commands["reply"][func.__name__] = cmd

            for alias in aliases:
                _commands["reply"][alias] = cmd
        else:
            _commands["prefix"][func.__name__] = cmd

            for alias in aliases:
                _commands["prefix"][alias] = cmd

        return f

    return decorator

def message(msg):
    assert isinstance(msg, Message)
    return msg

def dispatch_command(msg):
    command_parts = GlobalVars.parser.unescape(msg.content).split(" ", 1)

    if len(command_parts) == 2:
        cmd, args = command_parts
    else:
        cmd, = command_parts
        args = ""

    command_name = cmd[3:].lower()

    quiet_action = command_name[-1] == "-"
    command_name = regex.sub(r"\W*$", "", command_name)

    if command_name not in _commands["prefix"]:
        return "No such command '{}'.".format(command_name)
    else:
        func, (min_arity, max_arity) = _commands["prefix"][command_name]

        if max_arity == 0:
            return func(original_msg=msg, alias_used=command_name, quiet_action=quiet_action)
        elif max_arity == 1:
            return func(args or None, original_msg=msg, alias_used=command_name, quiet_action=quiet_action)
        else:
            args = args.split()
            args.extend([None] * (max_arity - len(args)))

            if len(args) < min_arity:
                return "Too few arguments."
            elif len(args) > max_arity:
                return "Too many arguments."
            else:
                return func(*args, original_msg=msg, alias_used=command_name, quiet_action=quiet_action)

def dispatch_reply_command(msg, reply, command_name):
    cmd = GlobalVars.parser.unescape(command_name).lower()

    quiet_action = cmd[-1] == "-"
    cmd = regex.sub(r"\W*$", "", cmd)

    if cmd not in _commands["reply"]:
        return "No such command {}.".format(cmd)
    else:
        func, arity = _commands["reply"][cmd]

        assert arity == (1, 1)

        return func(msg, original_msg=reply, alias_used=cmd, quiet_action=quiet_action)

def dispatch_shorthand_command(msg, room):
    commands = GlobalVars.parser.unescape(msg.content[3:]).split()

    output = []
    processed_commands = []

    for cmd in commands:
        count, cmd = regex.match(r"^(\d*)(.*)", cmd).groups()

        for _ in range(int(count) if count else 1):
            processed_commands.append(cmd)

    should_return_output = False

    for current_command, message in zip(processed_commands, get_last_messages(room, len(processed_commands))):
        if current_command == "-":
            output.append("[:{}] <skipped>".format(message.id))
        else:
            result = dispatch_reply_command(message, msg, current_command)

            if result:
                should_return_output = True
                output.append("[:{}] {}".format(message.id, result))
            else:
                output.append("[:{}] <processed without return value>".format(message.id))

    return "\n".join(output) if should_return_output else ""
<commit_msg>Fix get_report_data<commit_after>from ChatExchange.chatexchange import events
from ChatExchange.chatexchange.client import Client
from ChatExchange.chatexchange.messages import Message
import collections
import itertools
import os.path
import pickle
import regex
import sys
import threading
import time
import yaml

import datahandling
from deletionwatcher import DeletionWatcher
from excepthook import log_exception
from globalvars import GlobalVars
from parsing import fetch_post_url_from_msg_content, fetch_owner_url_from_msg_content

LastMessages = collections.namedtuple("LastMessages", ["messages", "reports"])

class RoomData:
    def __init__(self, room, lock, block_time, last_report_data, deletion_watcher):
        self.room = room
        self.lock = lock
        self.block_time = block_time
        self.last_report_data = last_report_data
        self.deletion_watcher = deletion_watcher

class CmdException(Exception):
    pass

_commands = {"reply": {}, "prefix": {}}

_clients = {
    "stackexchange.com": None,
    "stackoverflow.com": None,
    "meta.stackexchange.com": None
}

_command_rooms = set()
_watcher_rooms = set()
_room_roles = {}
_privileges = {}

_global_block = -1
_rooms = {}
_last_messages = LastMessages({}, collections.OrderedDict())

_pickle_run = threading.Event()

def init(username, password):
    global _clients
    global _rooms
    global _room_data
    global _last_messages

    for site in _clients.keys():
        client = Client(site)

        for _ in range(10):
            try:
                client.login(username, password)
                break
            except:
                pass
        else:
            raise Exception("Failed to log into " + site)

        _clients[site] = client

    parse_room_config("rooms.yml")

    if not GlobalVars.standby_mode:
        for site, roomid in _command_rooms:
            room = _clients[site].get_room(roomid)
            deletion_watcher = (site, roomid) in _watcher_rooms

            room.join()
            room.watch_socket(lambda msg, client: on_msg(msg, client, room))
            _rooms[(site, roomid)] = RoomData(room, threading.Event(), -1, (), deletion_watcher)
            _rooms[(site, roomid)].lock.set()

    if os.path.isfile("messageData.p"):
        _last_messages = pickle.load(open("messageData.p", "rb"))

    threading.Thread(name="pickle ---rick--- runner", target=pickle_last_messages).start()

def parse_room_config(path):
    with open(path, "r") as room_config:
        room_dict = yaml.load(room_config.read())

        for site, site_rooms in room_dict.items():
            for roomid, room in site_rooms.items():
                room_identifier = (site, roomid)
                _privileges[room_identifier] = set(room["privileges"]) if "privileges" in room else set()

                if "commands" in room and room["commands"]:
                    _command_rooms.add(room_identifier)

                if "watcher" in room and room["watcher"]:
                    _watcher_rooms.add(room_identifier)

                if "msg_types" in room:
                    add_room(room_identifier, room["msg_types"])

def add_room(room, roles):
    for role in roles:
        if role not in _room_roles:
            _room_roles[role] = set()

        _room_roles[role].add(room)

def pickle_last_messages():
    while True:
        _pickle_run.wait()
        _pickle_run.clear()

        with open("messageData.p", "wb") as pickle_file:
            pickle.dump(_last_messages, pickle_file)

def on_msg(msg, client, room):
    if isinstance(msg, events.MessagePosted) or isinstance(msg, events.MessageEdited):
        message = msg.message

        if message.owner.id == client._br.user_id:
            identifier = (client.host, room.id)
            room_data = _rooms[identifier]

            if identifier not in _last_messages.messages:
                _last_messages.messages[identifier] = collections.deque((message.id,))
            else:
                last = _last_messages.messages[identifier]

                if len(last) > 100:
                    last.popleft()

                last.append(message.id)

            if room_data.last_report_data != ():
                _last_messages.reports[message.id] = room_data.last_report_data

                if len(_last_messages.reports) > 50:
                    _last_messages.reports.popitem(last=False)

                threading.Thread(name="deletion watcher",
                                 target=DeletionWatcher.check_if_report_was_deleted,
                                 args=(room_data.last_report_data[0], message))

                room_data.last_report_data = ()

            room_data.lock.set()
            _pickle_run.set()
        elif message.parent and message.parent.owner.id == client._br.user_id:
            command = message.content.split(" ", 1)[1]
            result = dispatch_reply_command(message.parent, message, command)

            if result:
                message.reply(result)
        elif message.content.startswith("sd "):
            result = dispatch_shorthand_command(message, room)

            if result:
                message.reply(result)
        elif message.content.startswith("!!/"):
            result = dispatch_command(message)

            if result:
                message.reply(result)

def send_to_room(room, msg, report_data=()):
    timestamp = time.time()

    if room.block_time < timestamp and _global_block < timestamp:
        msg = msg.rstrip()

        if report_data:
            room.last_report_data = report_data

            if "delay" in _room_roles and room in _room_roles["delay"]:
                threading.Thread(name="delayed post",
                                 target=DeletionWatcher.post_message_if_not_deleted,
                                 args=(report_data[0], msg, room))
                return

        room.lock.wait()
        room.lock.clear()
        room.room.send_message(msg)

def tell_rooms_with(prop, msg, notify_site="", report_data=()):
    tell_rooms(msg, (prop,), (), notify_site=notify_site, report_data=report_data)

def tell_rooms_without(prop, msg, notify_site="", report_data=()):
    tell_rooms(msg, (), (prop,), notify_site=notify_site, report_data=report_data)

def tell_rooms(msg, has, hasnt, notify_site="", report_data=()):
    global _rooms

    target_rooms = set()

    for prop_has in has:
        if prop_has not in _room_roles:
            continue

        for room in _room_roles[prop_has]:
            if all(map(lambda prop: prop not in _room_roles or room not in _room_roles[prop], hasnt)):
                if room not in _rooms:
                    site, roomid = room
                    deletion_watcher = room in _watcher_rooms

                    new_room = _clients[site].get_room(roomid)
                    new_room.join()

                    _rooms[room] = RoomData(new_room, threading.Event(), -1, (), deletion_watcher)
                    _rooms[room].lock.set()

                target_rooms.add(_rooms[room])

    for room in target_rooms:
        if notify_site:
            pings = datahandling.get_user_names_on_notification_list(room.room._client.host,
                                                                     room.room.id,
                                                                     notify_site,
                                                                     room.room._client)

            msg = datahandling.append_pings(msg, pings)

        send_to_room(room, msg, report_data=report_data)

def get_last_messages(room, count):
    for msg_id in itertools.islice(reversed(_last_messages.messages[(room._client.host, room.id)]), count):
        yield room._client.get_message(msg_id)

def get_report_data(message):
    if message.id in _last_messages.reports:
        return _last_messages.reports[message.id]
    else:
        post_url = fetch_post_url_from_msg_content(message.content)

        if post_url:
            return (post_url, fetch_owner_url_from_msg_content(message.content))

def is_privileged(user, room):
    return user.id in _privileges[(room._client.host, room.id)] or user.is_moderator

def block_room(room_id, site, time):
    global _global_block

    if room_id is None:
        _global_block = time
    else:
        _rooms[(site, room_id)].block_time = time

def command(*type_signature, reply=False, whole_msg=False, privileged=False, arity=None, aliases=None, give_name=False):
    if aliases is None:
        aliases = []

    def decorator(func):
        def f(*args, original_msg=None, alias_used=None, quiet_action=False):
            if privileged and not is_privileged(original_msg.owner, original_msg.room):
                return GlobalVars.not_privileged_warning

            if whole_msg:
                processed_args = [original_msg]
            else:
                processed_args = []

            try:
                processed_args.extend([get_type(arg) if arg else arg for get_type, arg in zip(type_signature, args)])

                result = func(*processed_args, **({"alias_used": alias_used} if give_name else {}))
                return result if not quiet_action else ""
            except CmdException as e:
                return str(e)
            except:
                log_exception(*sys.exc_info())
                return "I hit an error while trying to run that command; run `!!/errorlogs` for details."

        cmd = (f, arity if arity else (len(type_signature), len(type_signature)))

        if reply:
            _commands["reply"][func.__name__] = cmd

            for alias in aliases:
                _commands["reply"][alias] = cmd
        else:
            _commands["prefix"][func.__name__] = cmd

            for alias in aliases:
                _commands["prefix"][alias] = cmd

        return f

    return decorator

def message(msg):
    assert isinstance(msg, Message)
    return msg

def dispatch_command(msg):
    command_parts = GlobalVars.parser.unescape(msg.content).split(" ", 1)

    if len(command_parts) == 2:
        cmd, args = command_parts
    else:
        cmd, = command_parts
        args = ""

    command_name = cmd[3:].lower()

    quiet_action = command_name[-1] == "-"
    command_name = regex.sub(r"\W*$", "", command_name)

    if command_name not in _commands["prefix"]:
        return "No such command '{}'.".format(command_name)
    else:
        func, (min_arity, max_arity) = _commands["prefix"][command_name]

        if max_arity == 0:
            return func(original_msg=msg, alias_used=command_name, quiet_action=quiet_action)
        elif max_arity == 1:
            return func(args or None, original_msg=msg, alias_used=command_name, quiet_action=quiet_action)
        else:
            args = args.split()
            args.extend([None] * (max_arity - len(args)))

            if len(args) < min_arity:
                return "Too few arguments."
            elif len(args) > max_arity:
                return "Too many arguments."
            else:
                return func(*args, original_msg=msg, alias_used=command_name, quiet_action=quiet_action)

def dispatch_reply_command(msg, reply, command_name):
    cmd = GlobalVars.parser.unescape(command_name).lower()

    quiet_action = cmd[-1] == "-"
    cmd = regex.sub(r"\W*$", "", cmd)

    if cmd not in _commands["reply"]:
        return "No such command {}.".format(cmd)
    else:
        func, arity = _commands["reply"][cmd]

        assert arity == (1, 1)

        return func(msg, original_msg=reply, alias_used=cmd, quiet_action=quiet_action)

def dispatch_shorthand_command(msg, room):
    commands = GlobalVars.parser.unescape(msg.content[3:]).split()

    output = []
    processed_commands = []

    for cmd in commands:
        count, cmd = regex.match(r"^(\d*)(.*)", cmd).groups()

        for _ in range(int(count) if count else 1):
            processed_commands.append(cmd)

    should_return_output = False

    for current_command, message in zip(processed_commands, get_last_messages(room, len(processed_commands))):
        if current_command == "-":
            output.append("[:{}] <skipped>".format(message.id))
        else:
            result = dispatch_reply_command(message, msg, current_command)

            if result:
                should_return_output = True
                output.append("[:{}] {}".format(message.id, result))
            else:
                output.append("[:{}] <processed without return value>".format(message.id))

    return "\n".join(output) if should_return_output else ""
<|endoftext|>


<commit_before>import unittest
import simplecpreprocessor
import os.path
import os
import tempfile

class FakeFile(object):

    def __init__(self, name, contents):
        self.name = name
        self.contents = contents

    def __iter__(self):
        for line in self.contents:
            yield line

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        pass

class FakeHandler(simplecpreprocessor.HeaderHandler):

    def __init__(self, header_mapping, include_paths=()):
        self.header_mapping = header_mapping
        super(FakeHandler, self).__init__(list(include_paths))

    def _open(self, header_path):
        contents = self.header_mapping.get(header_path)
        if contents is not None:
            return FakeFile(header_path, contents)
        else:
            return None

    def parent_open(self, header_path):
        return super(FakeHandler, self)._open(header_path)

class TestSimpleCPreprocessor(unittest.TestCase):

    def run_case(self, input_list, expected_list):
        output_list = list(simplecpreprocessor.preprocess(input_list))
        self.assertEqual(output_list, expected_list)

    def test_define(self):
        f_obj = FakeFile("header.h", ["#define FOO 1\n",
                                      "FOO"])
        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_multiline_define(self):
        f_obj = FakeFile("header.h", ["#define FOO \\\n",
                                      "\t1\n",
                                      "FOO\n"])
        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_define_simple_self_referential(self):
        f_obj = FakeFile("header.h", ["#define FOO FOO\n",
                                      "FOO\n"])
        expected_list = ["FOO\n"]
        self.run_case(f_obj, expected_list)

    def test_define_indirect_self_reference(self):
        f_obj = FakeFile("header.h", ["#define x (4 + y)\n",
                                      "#define y (2 * x)\n",
                                      "x\n", "y\n"])
        expected_list = ["(4 + (2 * x))\n", "(2 * (4 + y))\n"]
        self.run_case(f_obj, expected_list)

    def test_partial_match(self):
        f_obj = FakeFile("header.h", [
            "#define FOO\n",
            "FOOBAR\n"
        ])
        expected_list = ["FOOBAR\n"]
        self.run_case(f_obj, expected_list)

    def test_blank_define(self):
        f_obj = FakeFile("header.h", ["#define FOO\n",
                                      "FOO\n"])
        expected_list = ["\n"]
        self.run_case(f_obj, expected_list)

    def test_define_parens(self):
        f_obj = FakeFile("header.h", ["#define FOO (x)\n",
                                      "FOO\n"])
        expected_list = ["(x)\n"]
        self.run_case(f_obj, expected_list)

    def test_define_undefine(self):
        f_obj = FakeFile("header.h", ["#define FOO 1\n",
                                      "#undef FOO\n",
                                      "FOO"])
        expected_list = ["FOO\n"]
        self.run_case(f_obj, expected_list)

    def test_complex_ignore(self):
        f_obj = FakeFile("header.h",
                         [
                            "#ifdef X\n",
                            "#define X 1\n",
                            "#ifdef X\n",
                            "#define X 2\n",
                            "#else\n",
                            "#define X 3\n",
                            "#endif\n",
                            "#define X 4\n",
                            "#endif\n",
                            "X\n"])
        expected_list = ["X\n"]
        self.run_case(f_obj, expected_list)

    def test_extra_endif_causes_error(self):
        input_list = ["#endif\n"]
        with self.assertRaises(simplecpreprocessor.ParseError):
            list(simplecpreprocessor.preprocess(input_list))

    def test_ifdef_left_open_causes_error(self):
        f_obj = FakeFile("header.h", ["#ifdef FOO\n"])
        with self.assertRaises(simplecpreprocessor.ParseError):
            list(simplecpreprocessor.preprocess(f_obj))

    def test_ifndef_left_open_causes_error(self):
        f_obj = FakeFile("header.h", ["#ifndef FOO\n"])
        with self.assertRaises(simplecpreprocessor.ParseError):
            list(simplecpreprocessor.preprocess(f_obj))

    def test_unexpected_macro_gives_parse_error(self):
        f_obj = FakeFile("header.h", ["#something_unsupported foo bar\n"])
        with self.assertRaises(simplecpreprocessor.ParseError):
            list(simplecpreprocessor.preprocess(f_obj))

    def test_ifndef_unfulfilled_define_ignored(self):
        f_obj = FakeFile("header.h", ["#define FOO\n", "#ifndef FOO\n",
                                      "#define BAR 1\n",
                                      "#endif\n", "BAR\n"])
        expected_list = ["BAR\n"]
        self.run_case(f_obj, expected_list)

    def test_ifdef_unfulfilled_define_ignored(self):
        f_obj = FakeFile("header.h", ["#ifdef FOO\n", "#define BAR 1\n",
                                      "#endif\n", "BAR\n"])
        expected_list = ["BAR\n"]
        self.run_case(f_obj, expected_list)

    def test_ifndef_fulfilled_define_allowed(self):
        f_obj = FakeFile("header.h", ["#ifndef FOO\n", "#define BAR 1\n",
                                      "#endif\n", "BAR\n"])
        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_fulfilled_ifdef_define_allowed(self):
        f_obj = FakeFile("header.h", ["#define FOO", "#ifdef FOO\n",
                                      "#define BAR 1\n",
                                      "#endif\n", "BAR\n"])
        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_define_inside_ifndef(self):
        f_obj = FakeFile("header.h", ["#ifndef MODULE\n",
                                      "#define MODULE\n",
                                      "#ifdef BAR\n",
                                      "5\n",
                                      "#endif\n",
                                      "1\n",
                                      "#endif\n"])

        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_ifdef_else_undefined(self):
        f_obj = FakeFile("header.h", [
            "#ifdef A\n",
            "#define X 1\n",
            "#else\n",
            "#define X 0\n",
            "#endif\n",
            "X\n"])
        expected_list = ["0\n"]
        self.run_case(f_obj, expected_list)

    def test_ifdef_else_defined(self):
        f_obj = FakeFile("header.h", [
            "#define A\n",
            "#ifdef A\n",
            "#define X 1\n",
            "#else\n",
            "#define X 0\n",
            "#endif\n",
            "X\n"])
        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_ifndef_else_undefined(self):
        f_obj = FakeFile("header.h", [
            "#ifndef A\n",
            "#define X 1\n",
            "#else\n",
            "#define X 0\n",
            "#endif\n",
            "X\n"])
        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_ifndef_else_defined(self):
        f_obj = FakeFile("header.h", [
            "#define A\n",
            "#ifndef A\n",
            "#define X 1\n",
            "#else\n",
            "#define X 0\n",
            "#endif\n",
            "X\n"])
        expected_list = ["0\n"]
        self.run_case(f_obj, expected_list)

    def test_lines_normalized(self):
        f_obj = FakeFile("header.h", ["foo\r\n", "bar\r\n"])
        expected_list = ["foo\n", "bar\n"]
        self.run_case(f_obj, expected_list)

    def test_lines_normalize_custom(self):
        f_obj = FakeFile("header.h", ["foo\n", "bar\n"])
        expected_list = ["foo\r\n", "bar\r\n"]
        ret = simplecpreprocessor.preprocess(f_obj,
                                             line_ending="\r\n")
        self.assertEqual(list(ret), expected_list)

    def test_include_local_file_with_subdirectory(self):
        other_header = "somedirectory/other.h"
        f_obj = FakeFile("header.h", ['#include "%s"\n' % other_header])
        handler = FakeHandler({other_header: ["1\n"]})
        ret = simplecpreprocessor.preprocess(f_obj,
                                             header_handler=handler)
        self.assertEqual(list(ret), ["1\n"])

    def test_define_with_comment(self):
        f_obj = FakeFile("header.h", [
            "#define FOO 1 // comment\n",
            "FOO\n"])
        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_ifdef_with_comment(self):
        f_obj = FakeFile("header.h", [
            "#define FOO",
            "#ifdef FOO // comment\n",
            "1\n",
            "#endif"])
        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_include_with_path_list(self):
        f_obj = FakeFile("header.h", ['#include <other.h>\n'])
        handler = FakeHandler({os.path.join("subdirectory",
                                            "other.h"): ["1\n"]})
        include_paths = ["subdirectory"]
        ret = simplecpreprocessor.preprocess(f_obj,
                                             include_paths=include_paths,
                                             header_handler=handler)
        self.assertEqual(list(ret), ["1\n"])

    def test_tab_macro_indentation(self):
        f_obj = FakeFile("header.h", [
            "\t#define FOO 1\n",
            "\tFOO\n"])
        expected_list = ["\t1\n"]
        self.run_case(f_obj, expected_list)

    def test_space_macro_indentation(self):
        f_obj = FakeFile("header.h", [
            "    #define FOO 1\n",
            "    FOO\n"])
        expected_list = ["    1\n"]
        self.run_case(f_obj, expected_list)

    def test_include_with_path_list_with_subdirectory(self):
        header_file = os.path.join("nested", "other.h")
        include_path = "somedir"
        f_obj = FakeFile("header.h", ['#include <%s>\n' % header_file])
        handler = FakeHandler({os.path.join(include_path,
                                            header_file): ["1\n"]})
        include_paths = [include_path]
        ret = simplecpreprocessor.preprocess(f_obj,
                                             include_paths=include_paths,
                                             header_handler=handler)
        self.assertEqual(list(ret), ["1\n"])

    def test_include_missing_local_file(self):
        other_header = os.path.join("somedirectory", "other.h")
        f_obj = FakeFile("header.h", ['#include "%s"\n' % other_header])
        handler = FakeHandler({})
        with self.assertRaises(simplecpreprocessor.ParseError):
            ret = simplecpreprocessor.preprocess(f_obj,
                                                 header_handler=handler)
            list(ret)

    def test_ignore_include_path(self):
        f_obj = FakeFile("header.h", ['#include <other.h>\n'])
        handler = FakeHandler({os.path.join("subdirectory",
                                            "other.h"): ["1\n"]})
        paths = ["subdirectory"]
        ignored = ["other.h"]
        ret = simplecpreprocessor.preprocess(f_obj,
                                             include_paths=paths,
                                             header_handler=handler,
                                             ignore_headers=ignored)
        self.assertEqual(list(ret), [])

    def test_pragma_once(self):
        f_obj = FakeFile("header.h", ["""#include "other.h"\n""",
                                      """#include "other.h"\n""",
                                      "X\n"])
        handler = FakeHandler({"other.h": [
            "#pragma once\n",
            "#ifdef X\n",
            "#define X 2\n",
            "#else\n",
            "#define X 1\n",
            "#endif\n"]})
        ret = simplecpreprocessor.preprocess(f_obj, header_handler=handler)
        self.assertEqual(list(ret), ["1\n"])

    def test_platform_constants(self):
        f_obj = FakeFile("header.h", ['#ifdef ODDPLATFORM\n',
                                      'ODDPLATFORM\n', '#endif'])
        const = {"ODDPLATFORM": "ODDPLATFORM"}
        ret = simplecpreprocessor.preprocess(f_obj,
                                             platform_constants=const)
        self.assertEqual(list(ret), ["ODDPLATFORM\n"])

    def test_handler_missing_file(self):
        handler = FakeHandler([])
        self.assertIs(handler.parent_open("does_not_exist"), None)

    def test_handler_existing_file(self):
        handler = FakeHandler([])
        with tempfile.NamedTemporaryFile(dir="") as f_obj_a:
            with handler.parent_open(f_obj_a.name) as f_obj_b:
                self.assertEqual(os.fstat(f_obj_a.fileno()).st_ino,
                                 os.fstat(f_obj_b.fileno()).st_ino)
                self.assertEqual(f_obj_a.name, f_obj_b.name)
<commit_msg>Change test so it runs on Windows too Opening a file with write through Python API's give exclusive access to it so nothing else can open it for reading anymore<commit_after>import unittest
import simplecpreprocessor
import os.path
import os
import tempfile

class FakeFile(object):

    def __init__(self, name, contents):
        self.name = name
        self.contents = contents

    def __iter__(self):
        for line in self.contents:
            yield line

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        pass

class FakeHandler(simplecpreprocessor.HeaderHandler):

    def __init__(self, header_mapping, include_paths=()):
        self.header_mapping = header_mapping
        super(FakeHandler, self).__init__(list(include_paths))

    def _open(self, header_path):
        contents = self.header_mapping.get(header_path)
        if contents is not None:
            return FakeFile(header_path, contents)
        else:
            return None

    def parent_open(self, header_path):
        return super(FakeHandler, self)._open(header_path)

class TestSimpleCPreprocessor(unittest.TestCase):

    def run_case(self, input_list, expected_list):
        output_list = list(simplecpreprocessor.preprocess(input_list))
        self.assertEqual(output_list, expected_list)

    def test_define(self):
        f_obj = FakeFile("header.h", ["#define FOO 1\n",
                                      "FOO"])
        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_multiline_define(self):
        f_obj = FakeFile("header.h", ["#define FOO \\\n",
                                      "\t1\n",
                                      "FOO\n"])
        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_define_simple_self_referential(self):
        f_obj = FakeFile("header.h", ["#define FOO FOO\n",
                                      "FOO\n"])
        expected_list = ["FOO\n"]
        self.run_case(f_obj, expected_list)

    def test_define_indirect_self_reference(self):
        f_obj = FakeFile("header.h", ["#define x (4 + y)\n",
                                      "#define y (2 * x)\n",
                                      "x\n", "y\n"])
        expected_list = ["(4 + (2 * x))\n", "(2 * (4 + y))\n"]
        self.run_case(f_obj, expected_list)

    def test_partial_match(self):
        f_obj = FakeFile("header.h", [
            "#define FOO\n",
            "FOOBAR\n"
        ])
        expected_list = ["FOOBAR\n"]
        self.run_case(f_obj, expected_list)

    def test_blank_define(self):
        f_obj = FakeFile("header.h", ["#define FOO\n",
                                      "FOO\n"])
        expected_list = ["\n"]
        self.run_case(f_obj, expected_list)

    def test_define_parens(self):
        f_obj = FakeFile("header.h", ["#define FOO (x)\n",
                                      "FOO\n"])
        expected_list = ["(x)\n"]
        self.run_case(f_obj, expected_list)

    def test_define_undefine(self):
        f_obj = FakeFile("header.h", ["#define FOO 1\n",
                                      "#undef FOO\n",
                                      "FOO"])
        expected_list = ["FOO\n"]
        self.run_case(f_obj, expected_list)

    def test_complex_ignore(self):
        f_obj = FakeFile("header.h",
                         [
                            "#ifdef X\n",
                            "#define X 1\n",
                            "#ifdef X\n",
                            "#define X 2\n",
                            "#else\n",
                            "#define X 3\n",
                            "#endif\n",
                            "#define X 4\n",
                            "#endif\n",
                            "X\n"])
        expected_list = ["X\n"]
        self.run_case(f_obj, expected_list)

    def test_extra_endif_causes_error(self):
        input_list = ["#endif\n"]
        with self.assertRaises(simplecpreprocessor.ParseError):
            list(simplecpreprocessor.preprocess(input_list))

    def test_ifdef_left_open_causes_error(self):
        f_obj = FakeFile("header.h", ["#ifdef FOO\n"])
        with self.assertRaises(simplecpreprocessor.ParseError):
            list(simplecpreprocessor.preprocess(f_obj))

    def test_ifndef_left_open_causes_error(self):
        f_obj = FakeFile("header.h", ["#ifndef FOO\n"])
        with self.assertRaises(simplecpreprocessor.ParseError):
            list(simplecpreprocessor.preprocess(f_obj))

    def test_unexpected_macro_gives_parse_error(self):
        f_obj = FakeFile("header.h", ["#something_unsupported foo bar\n"])
        with self.assertRaises(simplecpreprocessor.ParseError):
            list(simplecpreprocessor.preprocess(f_obj))

    def test_ifndef_unfulfilled_define_ignored(self):
        f_obj = FakeFile("header.h", ["#define FOO\n", "#ifndef FOO\n",
                                      "#define BAR 1\n",
                                      "#endif\n", "BAR\n"])
        expected_list = ["BAR\n"]
        self.run_case(f_obj, expected_list)

    def test_ifdef_unfulfilled_define_ignored(self):
        f_obj = FakeFile("header.h", ["#ifdef FOO\n", "#define BAR 1\n",
                                      "#endif\n", "BAR\n"])
        expected_list = ["BAR\n"]
        self.run_case(f_obj, expected_list)

    def test_ifndef_fulfilled_define_allowed(self):
        f_obj = FakeFile("header.h", ["#ifndef FOO\n", "#define BAR 1\n",
                                      "#endif\n", "BAR\n"])
        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_fulfilled_ifdef_define_allowed(self):
        f_obj = FakeFile("header.h", ["#define FOO", "#ifdef FOO\n",
                                      "#define BAR 1\n",
                                      "#endif\n", "BAR\n"])
        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_define_inside_ifndef(self):
        f_obj = FakeFile("header.h", ["#ifndef MODULE\n",
                                      "#define MODULE\n",
                                      "#ifdef BAR\n",
                                      "5\n",
                                      "#endif\n",
                                      "1\n",
                                      "#endif\n"])

        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_ifdef_else_undefined(self):
        f_obj = FakeFile("header.h", [
            "#ifdef A\n",
            "#define X 1\n",
            "#else\n",
            "#define X 0\n",
            "#endif\n",
            "X\n"])
        expected_list = ["0\n"]
        self.run_case(f_obj, expected_list)

    def test_ifdef_else_defined(self):
        f_obj = FakeFile("header.h", [
            "#define A\n",
            "#ifdef A\n",
            "#define X 1\n",
            "#else\n",
            "#define X 0\n",
            "#endif\n",
            "X\n"])
        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_ifndef_else_undefined(self):
        f_obj = FakeFile("header.h", [
            "#ifndef A\n",
            "#define X 1\n",
            "#else\n",
            "#define X 0\n",
            "#endif\n",
            "X\n"])
        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_ifndef_else_defined(self):
        f_obj = FakeFile("header.h", [
            "#define A\n",
            "#ifndef A\n",
            "#define X 1\n",
            "#else\n",
            "#define X 0\n",
            "#endif\n",
            "X\n"])
        expected_list = ["0\n"]
        self.run_case(f_obj, expected_list)

    def test_lines_normalized(self):
        f_obj = FakeFile("header.h", ["foo\r\n", "bar\r\n"])
        expected_list = ["foo\n", "bar\n"]
        self.run_case(f_obj, expected_list)

    def test_lines_normalize_custom(self):
        f_obj = FakeFile("header.h", ["foo\n", "bar\n"])
        expected_list = ["foo\r\n", "bar\r\n"]
        ret = simplecpreprocessor.preprocess(f_obj,
                                             line_ending="\r\n")
        self.assertEqual(list(ret), expected_list)

    def test_include_local_file_with_subdirectory(self):
        other_header = "somedirectory/other.h"
        f_obj = FakeFile("header.h", ['#include "%s"\n' % other_header])
        handler = FakeHandler({other_header: ["1\n"]})
        ret = simplecpreprocessor.preprocess(f_obj,
                                             header_handler=handler)
        self.assertEqual(list(ret), ["1\n"])

    def test_define_with_comment(self):
        f_obj = FakeFile("header.h", [
            "#define FOO 1 // comment\n",
            "FOO\n"])
        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_ifdef_with_comment(self):
        f_obj = FakeFile("header.h", [
            "#define FOO",
            "#ifdef FOO // comment\n",
            "1\n",
            "#endif"])
        expected_list = ["1\n"]
        self.run_case(f_obj, expected_list)

    def test_include_with_path_list(self):
        f_obj = FakeFile("header.h", ['#include <other.h>\n'])
        handler = FakeHandler({os.path.join("subdirectory",
                                            "other.h"): ["1\n"]})
        include_paths = ["subdirectory"]
        ret = simplecpreprocessor.preprocess(f_obj,
                                             include_paths=include_paths,
                                             header_handler=handler)
        self.assertEqual(list(ret), ["1\n"])

    def test_tab_macro_indentation(self):
        f_obj = FakeFile("header.h", [
            "\t#define FOO 1\n",
            "\tFOO\n"])
        expected_list = ["\t1\n"]
        self.run_case(f_obj, expected_list)

    def test_space_macro_indentation(self):
        f_obj = FakeFile("header.h", [
            "    #define FOO 1\n",
            "    FOO\n"])
        expected_list = ["    1\n"]
        self.run_case(f_obj, expected_list)

    def test_include_with_path_list_with_subdirectory(self):
        header_file = os.path.join("nested", "other.h")
        include_path = "somedir"
        f_obj = FakeFile("header.h", ['#include <%s>\n' % header_file])
        handler = FakeHandler({os.path.join(include_path,
                                            header_file): ["1\n"]})
        include_paths = [include_path]
        ret = simplecpreprocessor.preprocess(f_obj,
                                             include_paths=include_paths,
                                             header_handler=handler)
        self.assertEqual(list(ret), ["1\n"])

    def test_include_missing_local_file(self):
        other_header = os.path.join("somedirectory", "other.h")
        f_obj = FakeFile("header.h", ['#include "%s"\n' % other_header])
        handler = FakeHandler({})
        with self.assertRaises(simplecpreprocessor.ParseError):
            ret = simplecpreprocessor.preprocess(f_obj,
                                                 header_handler=handler)
            list(ret)

    def test_ignore_include_path(self):
        f_obj = FakeFile("header.h", ['#include <other.h>\n'])
        handler = FakeHandler({os.path.join("subdirectory",
                                            "other.h"): ["1\n"]})
        paths = ["subdirectory"]
        ignored = ["other.h"]
        ret = simplecpreprocessor.preprocess(f_obj,
                                             include_paths=paths,
                                             header_handler=handler,
                                             ignore_headers=ignored)
        self.assertEqual(list(ret), [])

    def test_pragma_once(self):
        f_obj = FakeFile("header.h", ["""#include "other.h"\n""",
                                      """#include "other.h"\n""",
                                      "X\n"])
        handler = FakeHandler({"other.h": [
            "#pragma once\n",
            "#ifdef X\n",
            "#define X 2\n",
            "#else\n",
            "#define X 1\n",
            "#endif\n"]})
        ret = simplecpreprocessor.preprocess(f_obj, header_handler=handler)
        self.assertEqual(list(ret), ["1\n"])

    def test_platform_constants(self):
        f_obj = FakeFile("header.h", ['#ifdef ODDPLATFORM\n',
                                      'ODDPLATFORM\n', '#endif'])
        const = {"ODDPLATFORM": "ODDPLATFORM"}
        ret = simplecpreprocessor.preprocess(f_obj,
                                             platform_constants=const)
        self.assertEqual(list(ret), ["ODDPLATFORM\n"])

    def test_handler_missing_file(self):
        handler = FakeHandler([])
        self.assertIs(handler.parent_open("does_not_exist"), None)

    def test_handler_existing_file(self):
        handler = FakeHandler([])
        file_info = os.stat(__file__)
        with handler.parent_open(__file__) as f_obj:
            self.assertEqual(os.fstat(f_obj.fileno()).st_ino,
                file_info.st_ino)
            self.assertEqual(f_obj.name, __file__)
<|endoftext|>


<commit_before>from flask import Flask, request, redirect, render_template
import base64, hashlib, hmac
import simplejson as json
import urllib, urllib2
import os, os.path

FBAPI_APP_ID = os.environ.get('FACEBOOK_APP_ID')

def oauth_login_url(preserve_path=True, next_url=None):
    fb_login_uri = ("https://www.facebook.com/dialog/oauth"
                    "?client_id=%s&redirect_uri=%s" %
                    (app.config['FBAPI_APP_ID'], get_home()))

    if app.config['FBAPI_SCOPE']:
        fb_login_uri += "&scope=%s" % ",".join(app.config['FBAPI_SCOPE'])
    return fb_login_uri

def simple_dict_serialisation(params):
    return "&".join(map(lambda k: "%s=%s" % (k, params[k]), params.keys()))

def base64_url_encode(data):
    return base64.urlsafe_b64encode(data).rstrip('=')

def fbapi_get_string(path, domain=u'graph', params=None, access_token=None,
                     encode_func=urllib.urlencode):
    """Make an API call"""
    if not params:
        params = {}
    params[u'method'] = u'GET'
    if access_token:
        params[u'access_token'] = access_token

    for k, v in params.iteritems():
        if hasattr(v, 'encode'):
            params[k] = v.encode('utf-8')

    url = u'https://' + domain + u'.facebook.com' + path
    params_encoded = encode_func(params)
    url = url + params_encoded
    result = urllib2.urlopen(url).read()

    return result

def fbapi_auth(code):
    params = {'client_id':app.config['FBAPI_APP_ID'],
              'redirect_uri':get_home(),
              'client_secret':app.config['FBAPI_APP_SECRET'],
              'code':code}

    result = fbapi_get_string(path=u"/oauth/access_token?", params=params,
                              encode_func=simple_dict_serialisation)
    pairs = result.split("&", 1)
    result_dict = {}
    for pair in pairs:
        (key, value) = pair.split("=")
        result_dict[key] = value
    return (result_dict["access_token"], result_dict["expires"])

def fbapi_get_application_access_token(id):
    token = fbapi_get_string(
        path=u"/oauth/access_token",
        params=dict(grant_type=u'client_credentials', client_id=id,
                    client_secret=app.config['FB_APP_SECRET']),
        domain=u'graph')

    token = token.split('=')[-1]
    if not str(id) in token:
        current_app.logger.error('Token mismatch: %s not in %s', id, token)
    return token

def fql(fql, token, args=None):
    if not args: args = {}
    args["query"], args["format"], args["access_token"] = fql, "json", token
    return json.loads(
        urllib2.urlopen("https://api.facebook.com/method/fql.query?" +
                        urllib.urlencode(args)).read())

def fb_call(call, args=None):
    return json.loads(urllib2.urlopen("https://graph.facebook.com/" + call +
                                      '?' + urllib.urlencode(args)).read())

app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_object('conf.Config')

def get_home():
    return 'https://' + request.host + '/'

@app.route('/')
def index():
    print get_home()
    if request.args.get('code', None):
        access_token = fbapi_auth(request.args.get('code'))[0]

        me = fb_call('me', args={'access_token': access_token})
        app = fb_call(FBAPI_APP_ID, args={'access_token': access_token})
        likes = fb_call('me/likes',
                        args={'access_token': access_token, 'limit': 11})
        friends = fb_call('me/friends',
                          args={'access_token': access_token, 'limit': 3})
        photos = fb_call('me/photos',
                         args={'access_token': access_token, 'limit': 11})

        redir = get_home() + 'close/'
        POST_TO_WALL = ("https://www.facebook.com/dialog/feed?redirect_uri=%s&"
                        "display=popup&app_id=%s" % (redir, FBAPI_APP_ID))

        app_friends = fql(
            "SELECT uid, name, is_app_user, pic_square "
            "FROM user "
            "WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND "
            "  is_app_user = 1", access_token)

        SEND_TO = ('https://www.facebook.com/dialog/send?'
                   'redirect_uri=%s&display=popup&app_id=%s&link=%s'
                   % (redir, FBAPI_APP_ID, get_home()))

        return render_template(
            'index.html', appId=FBAPI_APP_ID, token=access_token, likes=likes,
            friends=friends, photos=photos, app_friends=app_friends, app=app,
            me=me, POST_TO_WALL=POST_TO_WALL, SEND_TO=SEND_TO)
    else:
        print oauth_login_url(next_url=get_home())
        return redirect(oauth_login_url(next_url=get_home()))

@app.route('/close/')
def close():
    return render_template('close.html')

if __name__ == '__main__':
    port = int(os.environ.get("PORT", 5000))
    if app.config.get('FBAPI_APP_ID') and app.config.get('FBAPI_APP_SECRET'):
        app.run(host='0.0.0.0', port=port)
    else:
        print 'Cannot start application without Facebook App Id and Secret set'
<commit_msg>Remove unused libraries<commit_after>from flask import Flask, request, redirect, render_template
import base64
import simplejson as json
import urllib, urllib2
import os, os.path

FBAPI_APP_ID = os.environ.get('FACEBOOK_APP_ID')

def oauth_login_url(preserve_path=True, next_url=None):
    fb_login_uri = ("https://www.facebook.com/dialog/oauth"
                    "?client_id=%s&redirect_uri=%s" %
                    (app.config['FBAPI_APP_ID'], get_home()))

    if app.config['FBAPI_SCOPE']:
        fb_login_uri += "&scope=%s" % ",".join(app.config['FBAPI_SCOPE'])
    return fb_login_uri

def simple_dict_serialisation(params):
    return "&".join(map(lambda k: "%s=%s" % (k, params[k]), params.keys()))

def base64_url_encode(data):
    return base64.urlsafe_b64encode(data).rstrip('=')

def fbapi_get_string(path, domain=u'graph', params=None, access_token=None,
                     encode_func=urllib.urlencode):
    """Make an API call"""
    if not params:
        params = {}
    params[u'method'] = u'GET'
    if access_token:
        params[u'access_token'] = access_token

    for k, v in params.iteritems():
        if hasattr(v, 'encode'):
            params[k] = v.encode('utf-8')

    url = u'https://' + domain + u'.facebook.com' + path
    params_encoded = encode_func(params)
    url = url + params_encoded
    result = urllib2.urlopen(url).read()

    return result

def fbapi_auth(code):
    params = {'client_id':app.config['FBAPI_APP_ID'],
              'redirect_uri':get_home(),
              'client_secret':app.config['FBAPI_APP_SECRET'],
              'code':code}

    result = fbapi_get_string(path=u"/oauth/access_token?", params=params,
                              encode_func=simple_dict_serialisation)
    pairs = result.split("&", 1)
    result_dict = {}
    for pair in pairs:
        (key, value) = pair.split("=")
        result_dict[key] = value
    return (result_dict["access_token"], result_dict["expires"])

def fbapi_get_application_access_token(id):
    token = fbapi_get_string(
        path=u"/oauth/access_token",
        params=dict(grant_type=u'client_credentials', client_id=id,
                    client_secret=app.config['FB_APP_SECRET']),
        domain=u'graph')

    token = token.split('=')[-1]
    if not str(id) in token:
        current_app.logger.error('Token mismatch: %s not in %s', id, token)
    return token

def fql(fql, token, args=None):
    if not args: args = {}
    args["query"], args["format"], args["access_token"] = fql, "json", token
    return json.loads(
        urllib2.urlopen("https://api.facebook.com/method/fql.query?" +
                        urllib.urlencode(args)).read())

def fb_call(call, args=None):
    return json.loads(urllib2.urlopen("https://graph.facebook.com/" + call +
                                      '?' + urllib.urlencode(args)).read())

app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_object('conf.Config')

def get_home():
    return 'https://' + request.host + '/'

@app.route('/')
def index():
    print get_home()
    if request.args.get('code', None):
        access_token = fbapi_auth(request.args.get('code'))[0]

        me = fb_call('me', args={'access_token': access_token})
        app = fb_call(FBAPI_APP_ID, args={'access_token': access_token})
        likes = fb_call('me/likes',
                        args={'access_token': access_token, 'limit': 11})
        friends = fb_call('me/friends',
                          args={'access_token': access_token, 'limit': 3})
        photos = fb_call('me/photos',
                         args={'access_token': access_token, 'limit': 11})

        redir = get_home() + 'close/'
        POST_TO_WALL = ("https://www.facebook.com/dialog/feed?redirect_uri=%s&"
                        "display=popup&app_id=%s" % (redir, FBAPI_APP_ID))

        app_friends = fql(
            "SELECT uid, name, is_app_user, pic_square "
            "FROM user "
            "WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND "
            "  is_app_user = 1", access_token)

        SEND_TO = ('https://www.facebook.com/dialog/send?'
                   'redirect_uri=%s&display=popup&app_id=%s&link=%s'
                   % (redir, FBAPI_APP_ID, get_home()))

        return render_template(
            'index.html', appId=FBAPI_APP_ID, token=access_token, likes=likes,
            friends=friends, photos=photos, app_friends=app_friends, app=app,
            me=me, POST_TO_WALL=POST_TO_WALL, SEND_TO=SEND_TO)
    else:
        print oauth_login_url(next_url=get_home())
        return redirect(oauth_login_url(next_url=get_home()))

@app.route('/close/')
def close():
    return render_template('close.html')

if __name__ == '__main__':
    port = int(os.environ.get("PORT", 5000))
    if app.config.get('FBAPI_APP_ID') and app.config.get('FBAPI_APP_SECRET'):
        app.run(host='0.0.0.0', port=port)
    else:
        print 'Cannot start application without Facebook App Id and Secret set'
<|endoftext|>


<commit_before>#! /usr/bin/env python
# Copyright (C) 2011 OpenStack, LLC.
# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
# Copyright (c) 2013 OpenStack Foundation
#
# 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 os
import pkg_resources
import shlex
import shutil
import subprocess
import sys
import tempfile

def run_command(cmd):
    print(cmd)
    cmd_list = shlex.split(str(cmd))
    p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
    (out, nothing) = p.communicate()
    return out.strip()

def increase_version(version_string):
    """
    Returns simple increased version string
    """
    items = version_string.split('.')
    for i in range(len(items) - 1, 0, -1):
        current_item = items[i]
        if current_item.isdigit():
            current_item = int(current_item) + 1
            items[i] = str(current_item)
            break
    final = '.'.join(items)
    return final

def decrease_version(version_string):
    """
    Returns simple decreased version string
    """
    items = version_string.split('.')
    for i in range(len(items) - 1, 0, -1):
        current_item = items[i]
        if current_item.isdigit():
            current_item = int(current_item) - 1
            if current_item >= 0:
                items[i] = str(current_item)
                break
            else:
                # continue to parent
                items[i] = '9'

    final = '.'.join(items)
    return final

def get_version_required(req):
    """
    Returns required version string depending on reqs
    """
    operator = req[0]
    version = req[1]
    if operator == '>':
        version = increase_version(version)
    elif operator == '<':
        version = decrease_version(version)
    return version

class RequirementsList(object):
    def __init__(self, name):
        self.name = name
        self.reqs = {}
        self.failed = False

    def read_requirements(self, fn, ignore_dups=False, strict=False):
        """ Read a requirements file and optionally enforce style."""
        if not os.path.exists(fn):
            return
        for line in open(fn):
            if strict and '\n' not in line:
                raise Exception("Requirements file %s does not "
                                "end with a newline." % fn)
            if '#' in line:
                line = line[:line.find('#')]
            line = line.strip()
            if (not line or
                line.startswith('http://tarballs.openstack.org/') or
                line.startswith('-e') or
                line.startswith('-f')):
                continue
            req = pkg_resources.Requirement.parse(line)
            if (not ignore_dups and strict and req.project_name.lower()
                in self.reqs):
                print("Duplicate requirement in %s: %s" %
                      (self.name, str(req)))
                self.failed = True
            self.reqs[req.project_name.lower()] = req

    def read_all_requirements(self, global_req=False, include_dev=False,
                              strict=False):
        """ Read all the requirements into a list.

        Build ourselves a consolidated list of requirements. If global_req is
        True then we are parsing the global requirements file only, and
        ensure that we don't parse it's test-requirements.txt erroneously.

        If include_dev is true allow for development requirements, which
        may be prereleased versions of libraries that would otherwise be
        listed. This is most often used for olso prereleases.

        If strict is True then style checks should be performed while reading
        the file.
        """
        if global_req:
            self.read_requirements('global-requirements.txt', strict=strict)
        else:
            for fn in ['tools/pip-requires',
                       'tools/test-requires',
                       'requirements.txt',
                       'test-requirements.txt'
                       ]:
                self.read_requirements(fn, strict=strict)
        if include_dev:
            self.read_requirements('dev-requirements.txt',
                                   ignore_dups=True, strict=strict)

def main():
    branch = sys.argv[1]

    # build a list of requirements in the proposed change,
    # and check them for style violations while doing so
    head = run_command("git rev-parse HEAD").strip()
    head_reqs = RequirementsList('HEAD')
    head_reqs.read_all_requirements(strict=True)

    # build a list of requirements already in the target branch,
    # so that we can create a diff and identify what's being changed
    run_command("git remote update")
    run_command("git checkout remotes/origin/%s" % branch)
    branch_reqs = RequirementsList(branch)
    branch_reqs.read_all_requirements()

    # switch back to the proposed change now
    run_command("git checkout %s" % head)

    # build a list of requirements from the global list in the
    # openstack/requirements project so we can match them to the changes
    reqroot = tempfile.mkdtemp()
    reqdir = os.path.join(reqroot, "requirements")
    run_command("git clone https://review.openstack.org/p/openstack/"
                "requirements --depth 1 %s" % reqdir)
    os.chdir(reqdir)
    run_command("git checkout remotes/origin/%s" % branch)
    print "requirements git sha: %s" % run_command(
        "git rev-parse HEAD").strip()
    os_reqs = RequirementsList('openstack/requirements')
    os_reqs.read_all_requirements(include_dev=(branch == 'master'),
                                  global_req=True)

    # iterate through the changing entries and see if they match the global
    # equivalents we want enforced
    failed = False
    for req in head_reqs.reqs.values():
        name = req.project_name.lower()
        if name in branch_reqs.reqs:
            if req == branch_reqs.reqs[name]:
                continue
            else:
                # check if overlaps
                for spec in req.specs:
                    version = get_version_required(spec)
                    branch_req = branch_reqs.reqs[name]
                    if not branch_req.__contains__(version):
                        failed = True
                        print("Requirement %s does not overlap with branch " %
                              str(req))
                        continue

        if name not in os_reqs.reqs:
            print("Requirement %s not in openstack/requirements" % str(req))
            failed = True
            continue
        # pkg_resources.Requirement implements __eq__() but not __ne__().
        # There is no implied relationship between __eq__() and __ne__()
        # so we must negate the result of == here instead of using !=.
        if not (req == os_reqs.reqs[name]):
            print("Requirement %s does not match openstack/requirements "
                  "value %s" % (str(req), str(os_reqs.reqs[name])))
            failed = True

    # clean up and report the results
    shutil.rmtree(reqroot)
    if failed or os_reqs.failed or head_reqs.failed or branch_reqs.failed:
        sys.exit(1)
    print("Updated requirements match openstack/requirements.")

if __name__ == '__main__':
    main()
<commit_msg>Revert "Checks if requirements overlap between head and branch reqs"<commit_after>#! /usr/bin/env python
# Copyright (C) 2011 OpenStack, LLC.
# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
# Copyright (c) 2013 OpenStack Foundation
#
# 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 os
import pkg_resources
import shlex
import shutil
import subprocess
import sys
import tempfile

def run_command(cmd):
    print(cmd)
    cmd_list = shlex.split(str(cmd))
    p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
    (out, nothing) = p.communicate()
    return out.strip()

class RequirementsList(object):
    def __init__(self, name):
        self.name = name
        self.reqs = {}
        self.failed = False

    def read_requirements(self, fn, ignore_dups=False, strict=False):
        """ Read a requirements file and optionally enforce style."""
        if not os.path.exists(fn):
            return
        for line in open(fn):
            if strict and '\n' not in line:
                raise Exception("Requirements file %s does not "
                                "end with a newline." % fn)
            if '#' in line:
                line = line[:line.find('#')]
            line = line.strip()
            if (not line or
                line.startswith('http://tarballs.openstack.org/') or
                line.startswith('-e') or
                line.startswith('-f')):
                continue
            req = pkg_resources.Requirement.parse(line)
            if (not ignore_dups and strict and req.project_name.lower()
                in self.reqs):
                print("Duplicate requirement in %s: %s" %
                      (self.name, str(req)))
                self.failed = True
            self.reqs[req.project_name.lower()] = req

    def read_all_requirements(self, global_req=False, include_dev=False,
                              strict=False):
        """ Read all the requirements into a list.

        Build ourselves a consolidated list of requirements. If global_req is
        True then we are parsing the global requirements file only, and
        ensure that we don't parse it's test-requirements.txt erroneously.

        If include_dev is true allow for development requirements, which
        may be prereleased versions of libraries that would otherwise be
        listed. This is most often used for olso prereleases.

        If strict is True then style checks should be performed while reading
        the file.
        """
        if global_req:
            self.read_requirements('global-requirements.txt', strict=strict)
        else:
            for fn in ['tools/pip-requires',
                       'tools/test-requires',
                       'requirements.txt',
                       'test-requirements.txt'
                       ]:
                self.read_requirements(fn, strict=strict)
        if include_dev:
            self.read_requirements('dev-requirements.txt',
                                   ignore_dups=True, strict=strict)

def main():
    branch = sys.argv[1]

    # build a list of requirements in the proposed change,
    # and check them for style violations while doing so
    head = run_command("git rev-parse HEAD").strip()
    head_reqs = RequirementsList('HEAD')
    head_reqs.read_all_requirements(strict=True)

    # build a list of requirements already in the target branch,
    # so that we can create a diff and identify what's being changed
    run_command("git remote update")
    run_command("git checkout remotes/origin/%s" % branch)
    branch_reqs = RequirementsList(branch)
    branch_reqs.read_all_requirements()

    # switch back to the proposed change now
    run_command("git checkout %s" % head)

    # build a list of requirements from the global list in the
    # openstack/requirements project so we can match them to the changes
    reqroot = tempfile.mkdtemp()
    reqdir = os.path.join(reqroot, "requirements")
    run_command("git clone https://review.openstack.org/p/openstack/"
                "requirements --depth 1 %s" % reqdir)
    os.chdir(reqdir)
    run_command("git checkout remotes/origin/%s" % branch)
    print "requirements git sha: %s" % run_command(
        "git rev-parse HEAD").strip()
    os_reqs = RequirementsList('openstack/requirements')
    os_reqs.read_all_requirements(include_dev=(branch == 'master'),
                                  global_req=True)

    # iterate through the changing entries and see if they match the global
    # equivalents we want enforced
    failed = False
    for req in head_reqs.reqs.values():
        name = req.project_name.lower()
        if name in branch_reqs.reqs and req == branch_reqs.reqs[name]:
            continue
        if name not in os_reqs.reqs:
            print("Requirement %s not in openstack/requirements" % str(req))
            failed = True
            continue
        # pkg_resources.Requirement implements __eq__() but not __ne__().
        # There is no implied relationship between __eq__() and __ne__()
        # so we must negate the result of == here instead of using !=.
        if not (req == os_reqs.reqs[name]):
            print("Requirement %s does not match openstack/requirements "
                  "value %s" % (str(req), str(os_reqs.reqs[name])))
            failed = True

    # clean up and report the results
    shutil.rmtree(reqroot)
    if failed or os_reqs.failed or head_reqs.failed or branch_reqs.failed:
        sys.exit(1)
    print("Updated requirements match openstack/requirements.")

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


<commit_before>"""Leetcode 73. Set Matrix Zeroes
Medium

URL: https://leetcode.com/problems/set-matrix-zeroes/

Given a m x n matrix, if an element is 0, set its entire row and column to 0.
Do it in-place.

Example 1:
Input:
[
  [1,1,1],
  [1,0,1],
  [1,1,1]
]
Output:
[
  [1,0,1],
  [0,0,0],
  [1,0,1]
]

Example 2:
Input:
[
  [0,1,2,0],
  [3,4,5,2],
  [1,3,1,5]
]
Output:
[
  [0,0,0,0],
  [0,4,5,0],
  [0,3,1,0]
]

Follow up:
- A straight forward solution using O(mn) space is probably a bad idea.
- A simple improvement uses O(m + n) space, but still not the best solution.
- Could you devise a constant space solution?
"""

class SolutionCopy(object):
    def setZeroes(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: None Do not return anything, modify matrix in-place instead.

        Time complexity: O(m * n * max(m, n)).
        Space complexity: O(m * n).
        """
        m, n = len(matrix), len(matrix[0])

        # Copy matrix to a new matrix.
        copy = [[matrix[i][j] for j in range(n)] for i in range(m)]

        # Update its row and col by checking matrix[i][j] == 0.
        for i in range(m):
            for j in range(n):
                if copy[i][j] == 0:
                    for r in range(m):
                        matrix[r][j] = 0
                    for c in range(n):
                        matrix[i][c] = 0

class SolutionRowCol(object):
    def setZeroes(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: None Do not return anything, modify matrix in-place instead.

        Time complexity: O(m * n).
        Space complexity: O(m + n).
        """
        m, n = len(matrix), len(matrix[0])

        # Create rows and cols for 0's positions.
        rows = [0] * m
        cols = [0] * n

        # Update its row and col by checking matrix[i][j] == 0.
        for i in range(m):
            for j in range(n):
                if matrix[i][j] == 0:
                    rows[i] = 1
                    cols[j] = 1

        for r in range(len(rows)):
            if rows[r] == 1:
                for j in range(n):
                    matrix[r][j] = 0

        for c in range(len(cols)):
            if cols[c] == 1:
                for i in range(m):
                    matrix[i][c] = 0

def main():
    matrix = [
               [1,1,1],
               [1,0,1],
               [1,1,1]
             ]
    # SolutionCopy().setZeroes(matrix)
    SolutionRowCol().setZeroes(matrix)
    print matrix

    matrix = [
               [0,1,2,0],
               [3,4,5,2],
               [1,3,1,5]
             ]
    # SolutionCopy().setZeroes(matrix)
    SolutionRowCol().setZeroes(matrix)
    print matrix

if __name__ == '__main__':
    main()
<commit_msg>Complete optim space sol<commit_after>"""Leetcode 73. Set Matrix Zeroes
Medium

URL: https://leetcode.com/problems/set-matrix-zeroes/

Given a m x n matrix, if an element is 0, set its entire row and column to 0.
Do it in-place.

Example 1:
Input:
[
  [1,1,1],
  [1,0,1],
  [1,1,1]
]
Output:
[
  [1,0,1],
  [0,0,0],
  [1,0,1]
]

Example 2:
Input:
[
  [0,1,2,0],
  [3,4,5,2],
  [1,3,1,5]
]
Output:
[
  [0,0,0,0],
  [0,4,5,0],
  [0,3,1,0]
]

Follow up:
- A straight forward solution using O(mn) space is probably a bad idea.
- A simple improvement uses O(m + n) space, but still not the best solution.
- Could you devise a constant space solution?
"""

class SolutionCopy(object):
    def setZeroes(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: None Do not return anything, modify matrix in-place instead.

        Time complexity: O(m * n * max(m, n)).
        Space complexity: O(m * n).
        """
        m, n = len(matrix), len(matrix[0])

        # Copy matrix to a new matrix.
        copy = [[matrix[i][j] for j in range(n)] for i in range(m)]

        # Update its row and col by checking matrix[i][j] == 0.
        for i in range(m):
            for j in range(n):
                if copy[i][j] == 0:
                    for r in range(m):
                        matrix[r][j] = 0
                    for c in range(n):
                        matrix[i][c] = 0

class SolutionRowCol(object):
    def setZeroes(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: None Do not return anything, modify matrix in-place instead.

        Time complexity: O(m * n).
        Space complexity: O(m + n).
        """
        m, n = len(matrix), len(matrix[0])

        # Create rows and cols for 0's positions.
        rows = [0] * m
        cols = [0] * n

        # Update its row and col by checking matrix[i][j] == 0.
        for i in range(m):
            for j in range(n):
                if matrix[i][j] == 0:
                    rows[i] = 1
                    cols[j] = 1

        for r in range(len(rows)):
            if rows[r] == 1:
                for j in range(n):
                    matrix[r][j] = 0

        for c in range(len(cols)):
            if cols[c] == 1:
                for i in range(m):
                    matrix[i][c] = 0

class SolutionOptimSpace(object):
    def setZeroes(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: None Do not return anything, modify matrix in-place instead.

        Time complexity: O().
        Space complexity: O(1).
        """
        m, n = len(matrix), len(matrix[0])

        # Record any zeros are in the first row/col.
        is_zero_row0, is_zero_col0 = False, False

        for i in range(m):
            for j in range(n):
                if matrix[i][j] == 0:
                    if i == 0:
                        is_zero_row0 = True
                    if j == 0:
                        is_zero_col0 = True

                    # Use 1st element in the row/col to record zero.
                    matrix[i][0] = 0
                    matrix[0][j] = 0

        # For each element, update it to zero by checking its 1st row/col elements.
        for i in range(1, m):
            for j in range(1, n):
                if matrix[i][0] == 0 or matrix[0][j] == 0:
                        matrix[i][j] = 0

        # For 1st row/col, update to zero if needed.
        if is_zero_row0:
            for j in range(n):
                matrix[0][j] = 0
        if is_zero_col0:
            for i in range(m):
                matrix[i][0] = 0

def main():
    matrix = [
               [1,1,1],
               [1,0,1],
               [1,1,1]
             ]
    # SolutionCopy().setZeroes(matrix)
    SolutionRowCol().setZeroes(matrix)
    # SolutionOptimSpace().setZeroes(matrix)
    print matrix

    matrix = [
               [0,1,2,0],
               [3,4,5,2],
               [1,3,1,5]
             ]
    # SolutionCopy().setZeroes(matrix)
    SolutionRowCol().setZeroes(matrix)
    # SolutionOptimSpace().setZeroes(matrix)
    print matrix

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


<commit_before>import pytest
import sys

import py

mypath = py.path.local(__file__).new(ext=".py")

@pytest.fixture(autouse=True)
def skip_under_pytest_3_1():
    from distutils.version import LooseVersion
    if sys.platform.startswith('win') and LooseVersion(pytest.__version__) >= LooseVersion('3.1'):
        pytest.skip('apiwarn is not compatible with pytest >= 3.1 (#162)')

@pytest.mark.xfail
def test_forwarding_to_warnings_module():
    pytest.deprecated_call(py.log._apiwarn, "1.3", "..")

def test_apiwarn_functional(recwarn):
    capture = py.io.StdCapture()
    py.log._apiwarn("x.y.z", "something", stacklevel=1)
    out, err = capture.reset()
    py.builtin.print_("out", out)
    py.builtin.print_("err", err)
    assert err.find("x.y.z") != -1
    lno = py.code.getrawcode(test_apiwarn_functional).co_firstlineno + 2
    exp = "%s:%s" % (mypath, lno)
    assert err.find(exp) != -1

def test_stacklevel(recwarn):
    def f():
        py.log._apiwarn("x", "some", stacklevel=2)
    # 3
    # 4
    capture = py.io.StdCapture()
    f()
    out, err = capture.reset()
    lno = py.code.getrawcode(test_stacklevel).co_firstlineno + 6
    warning = str(err)
    assert warning.find(":%s" % lno) != -1

def test_stacklevel_initpkg_with_resolve(testdir, recwarn):
    testdir.makepyfile(modabc="""
        import py
        def f():
            py.log._apiwarn("x", "some", stacklevel="apipkg123")
    """)
    testdir.makepyfile(apipkg123="""
        def __getattr__():
            import modabc
            modabc.f()
    """)
    p = testdir.makepyfile("""
        import apipkg123
        apipkg123.__getattr__()
    """)
    capture = py.io.StdCapture()
    p.pyimport()
    out, err = capture.reset()
    warning = str(err)
    loc = 'test_stacklevel_initpkg_with_resolve.py:2'
    assert warning.find(loc) != -1

def test_stacklevel_initpkg_no_resolve(recwarn):
    def f():
        py.log._apiwarn("x", "some", stacklevel="apipkg")
    capture = py.io.StdCapture()
    f()
    out, err = capture.reset()
    lno = py.code.getrawcode(test_stacklevel_initpkg_no_resolve).co_firstlineno + 2
    warning = str(err)
    assert warning.find(":%s" % lno) != -1

def test_function(recwarn):
    capture = py.io.StdCapture()
    py.log._apiwarn("x.y.z", "something", function=test_function)
    out, err = capture.reset()
    py.builtin.print_("out", out)
    py.builtin.print_("err", err)
    assert err.find("x.y.z") != -1
    lno = py.code.getrawcode(test_function).co_firstlineno
    exp = "%s:%s" % (mypath, lno)
    assert err.find(exp) != -1

<commit_msg>Change fixture to a mark in test_warning<commit_after>import sys
from distutils.version import LooseVersion

import pytest

import py

mypath = py.path.local(__file__).new(ext=".py")

win = sys.platform.startswith('win')
pytestmark = pytest.mark.skipif(win and LooseVersion(pytest.__version__) >= LooseVersion('3.1'),
                                reason='apiwarn is not compatible with pytest >= 3.1 (#162)')

@pytest.mark.xfail
def test_forwarding_to_warnings_module():
    pytest.deprecated_call(py.log._apiwarn, "1.3", "..")

def test_apiwarn_functional(recwarn):
    capture = py.io.StdCapture()
    py.log._apiwarn("x.y.z", "something", stacklevel=1)
    out, err = capture.reset()
    py.builtin.print_("out", out)
    py.builtin.print_("err", err)
    assert err.find("x.y.z") != -1
    lno = py.code.getrawcode(test_apiwarn_functional).co_firstlineno + 2
    exp = "%s:%s" % (mypath, lno)
    assert err.find(exp) != -1

def test_stacklevel(recwarn):
    def f():
        py.log._apiwarn("x", "some", stacklevel=2)
    # 3
    # 4
    capture = py.io.StdCapture()
    f()
    out, err = capture.reset()
    lno = py.code.getrawcode(test_stacklevel).co_firstlineno + 6
    warning = str(err)
    assert warning.find(":%s" % lno) != -1

def test_stacklevel_initpkg_with_resolve(testdir, recwarn):
    testdir.makepyfile(modabc="""
        import py
        def f():
            py.log._apiwarn("x", "some", stacklevel="apipkg123")
    """)
    testdir.makepyfile(apipkg123="""
        def __getattr__():
            import modabc
            modabc.f()
    """)
    p = testdir.makepyfile("""
        import apipkg123
        apipkg123.__getattr__()
    """)
    capture = py.io.StdCapture()
    p.pyimport()
    out, err = capture.reset()
    warning = str(err)
    loc = 'test_stacklevel_initpkg_with_resolve.py:2'
    assert warning.find(loc) != -1

def test_stacklevel_initpkg_no_resolve(recwarn):
    def f():
        py.log._apiwarn("x", "some", stacklevel="apipkg")
    capture = py.io.StdCapture()
    f()
    out, err = capture.reset()
    lno = py.code.getrawcode(test_stacklevel_initpkg_no_resolve).co_firstlineno + 2
    warning = str(err)
    assert warning.find(":%s" % lno) != -1

def test_function(recwarn):
    capture = py.io.StdCapture()
    py.log._apiwarn("x.y.z", "something", function=test_function)
    out, err = capture.reset()
    py.builtin.print_("out", out)
    py.builtin.print_("err", err)
    assert err.find("x.y.z") != -1
    lno = py.code.getrawcode(test_function).co_firstlineno
    exp = "%s:%s" % (mypath, lno)
    assert err.find(exp) != -1

<|endoftext|>


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

"""Test plugin system."""

#------------------------------------------------------------------------------
# Imports
#------------------------------------------------------------------------------

import os.path as op

from ..core import save_text
from ..plugin import (IPluginRegistry, IPlugin, discover_plugins,
                      iter_plugins_dirs)

#------------------------------------------------------------------------------
# Fixtures
#------------------------------------------------------------------------------

def setup():
    IPluginRegistry.plugins = []

def teardown():
    IPluginRegistry.plugins = []

#------------------------------------------------------------------------------
# Tests
#------------------------------------------------------------------------------

def test_plugin_registration():
    class MyPlugin(IPlugin):
        pass

    assert IPluginRegistry.plugins == [MyPlugin]

def test_discover_plugins(tempdir):
    path = op.join(tempdir, 'my_plugin.py')
    contents = '''from podoc import IPlugin\nclass MyPlugin(IPlugin): pass'''
    save_text(path, contents)
    plugins = discover_plugins([tempdir])
    assert plugins
    assert plugins[0].__name__ == 'MyPlugin'

def test_iter_plugins_dirs():
    assert 'json' in [op.basename(plugin_dir)
                      for plugin_dir in iter_plugins_dirs()]
<commit_msg>Add iter_plugin_test_files() test<commit_after># -*- coding: utf-8 -*-

"""Test plugin system."""

#------------------------------------------------------------------------------
# Imports
#------------------------------------------------------------------------------

import os.path as op

from ..core import save_text
from ..plugin import (IPluginRegistry, IPlugin, discover_plugins,
                      iter_plugins_dirs, iter_plugin_test_files)

#------------------------------------------------------------------------------
# Fixtures
#------------------------------------------------------------------------------

def setup():
    IPluginRegistry.plugins = []

def teardown():
    IPluginRegistry.plugins = []

#------------------------------------------------------------------------------
# Tests
#------------------------------------------------------------------------------

def test_plugin_registration():
    class MyPlugin(IPlugin):
        pass

    assert IPluginRegistry.plugins == [MyPlugin]

def test_discover_plugins(tempdir):
    path = op.join(tempdir, 'my_plugin.py')
    contents = '''from podoc import IPlugin\nclass MyPlugin(IPlugin): pass'''
    save_text(path, contents)
    plugins = discover_plugins([tempdir])
    assert plugins
    assert plugins[0].__name__ == 'MyPlugin'

def test_iter_plugins_dirs():
    assert 'json' in [op.basename(plugin_dir)
                      for plugin_dir in iter_plugins_dirs()]

def test_iter_plugin_test_files():
    for plugin_dir in iter_plugins_dirs():
        if 'json' in plugin_dir:
            assert 'hello.json' in map(op.basename,
                                       iter_plugin_test_files(plugin_dir))
<|endoftext|>


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

import csv

def make_paramstxt(train, txtfile, file_descrip):

    rxtr_param = ['reactor', 'cooling', 'enrichment', 'burnup']
    algs = ['knn', 'dtree']
    # all funcs:
    #func_type = ['--track_preds', '--err_n_scores', '--learn_curves',
    #             '--valid_curves', '--test_compare', '--random_error']
    # funcs ran for nuc conc scenario:
    #func_type = ['--test_compare', '--random_error']
    # funcs ran for gamma spec scenario:
    func_type = ['--random_error',]
    #tset_frac = [0.1, 0.3, 1.0]
    #tset_frac = [0.2, 0.6]
    tset_frac = [0.2,]
    cv = 5

    for param in rxtr_param:
        for func in func_type:
            # test_compare or blank args
            # only applicable in nuc conc scenario so leaving as-is
            if 'test_compare' in func:
                filename = 'sfco_compare' + txtfile
                ts_flag = '--testing_set'
                if '15' in train:
                    testset = 'sfcompo_nuc15.pkl'
                else:
                    testset = 'sfcompo_nuc29.pkl'
            else:
                filename = func[2:] + txtfile
                ts_flag = ' '
                testset = ' '
            for alg in algs:
                for frac in tset_frac:
                    # outfile naming
                    outfile = param + '_' + alg + '_tset' + str(frac) + file_descrip
                    with open(filename, 'a') as f:
                        w = csv.writer(f)
                        job = [outfile, param, alg, str(frac), str(cv),
                               train, func, ts_flag, testset]
                        w.writerow(job)
    return

def main():
    """
    Populates the necessary htc param.txt files

    """
    # nuclide concentration lists
    #dblist = ['sim_grams_nuc15.pkl', 'sim_grams_nuc29.pkl']
    #txtfile = ['_nuc15_param.txt', '_nuc29_param.txt']
    #outfile = ['_nuc15', '_nuc29']

    # activity lists
    dblist = ['d1_hpge_spectra_peaks_trainset.pkl',]
    txtfile = ['_d1_param.txt',]
    outfile = ['_d1_hpge',]

    for i, traindb in enumerate(dblist):
        make_paramstxt(traindb, txtfile[i], outfile[i])
    return

if __name__ == "__main__":
    main()
<commit_msg>making htc param files for d1 and d2<commit_after>#! /usr/bin/env python3

import csv

def make_paramstxt(train, txtfile, file_descrip):

    rxtr_param = ['reactor', 'cooling', 'enrichment', 'burnup']
    algs = ['knn', 'dtree']
    # all funcs:
    #func_type = ['--track_preds', '--err_n_scores', '--learn_curves',
    #             '--valid_curves', '--test_compare', '--random_error']
    # funcs ran for nuc conc scenario:
    #func_type = ['--test_compare', '--random_error']
    # funcs ran for gamma spec scenario:
    func_type = ['--err_n_scores',]
    #tset_frac = [0.1, 0.3, 1.0]
    #tset_frac = [0.2, 0.6]
    tset_frac = [0.1, 0.2, 0.3, 0.5, 1.0]
    cv = 5

    for param in rxtr_param:
        for func in func_type:
            # test_compare or blank args
            # only applicable in nuc conc scenario so leaving as-is
            if 'test_compare' in func:
                filename = 'sfco_compare' + txtfile
                ts_flag = '--testing_set'
                if '15' in train:
                    testset = 'sfcompo_nuc15.pkl'
                else:
                    testset = 'sfcompo_nuc29.pkl'
            else:
                filename = func[2:] + txtfile
                ts_flag = ' '
                testset = ' '
            for alg in algs:
                for frac in tset_frac:
                    # outfile naming
                    outfile = param + '_' + alg + '_tset' + str(frac) + file_descrip
                    with open(filename, 'a') as f:
                        w = csv.writer(f)
                        job = [outfile, param, alg, str(frac), str(cv),
                               train, func, ts_flag, testset]
                        w.writerow(job)
    return

def main():
    """
    Populates the necessary htc param.txt files

    """
    # nuclide concentration lists
    #dblist = ['sim_grams_nuc15.pkl', 'sim_grams_nuc29.pkl']
    #txtfile = ['_nuc15_param.txt', '_nuc29_param.txt']
    #outfile = ['_nuc15', '_nuc29']

    # activity lists
    dblist = ['d1_hpge_spectra_peaks_trainset.pkl',
              'd2_hpge_spectra_peaks_trainset.pkl',
              ]
    txtfile = ['_d1_param.txt',
               '_d2_param.txt',
               ]
    outfile = ['_d1_hpge',
               '_d2_hpge',
               ]

    for i, traindb in enumerate(dblist):
        make_paramstxt(traindb, txtfile[i], outfile[i])
    return

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