<commit_before>"""Tests for the module that abstracts the OpenDaylight controller."""

import unittest

import cbenchf.controllers.opendaylight as odl_mod

class TestInit(unittest.TestCase):

    """Test building the ODL abstraction."""

    def test_basic(self):
        """Test a basic, all default construction."""
        odl_mod.OpenDaylight()

class TestStart(unittest.TestCase):

    """Test starting an OpenDaylight controller instance"""

    def setUp(self):
        """Build an ODL abstraction."""
        self.odl = odl_mod.OpenDaylight()

    def test_present(self):
        """Confirm that the start method is present. Required API method."""
        hasattr(self.odl, "start") and callable(getattr(self.odl, "start"))

class TestStop(unittest.TestCase):

    """Test stopping an OpenDaylight controller instance"""

    def setUp(self):
        """Build an ODL abstraction."""
        self.odl = odl_mod.OpenDaylight()

    def test_present(self):
        """Confirm that the stop method is present. Required API method."""
        hasattr(self.odl, "stop") and callable(getattr(self.odl, "stop"))
<commit_msg>Skip Docker-dependent tests for now, Travis doesn't have Docker<commit_after>"""Tests for the module that abstracts the OpenDaylight controller."""

import unittest

import cbenchf.controllers.opendaylight as odl_mod

class TestInit(unittest.TestCase):

    """Test building the ODL abstraction."""

    @unittest.skip("Travis doesn't have Docker")
    def test_basic(self):
        """Test a basic, all default construction."""
        odl_mod.OpenDaylight()

class TestStart(unittest.TestCase):

    """Test starting an OpenDaylight controller instance"""

    def setUp(self):
        """Build an ODL abstraction."""
        self.odl = odl_mod.OpenDaylight()

    @unittest.skip("Travis doesn't have Docker")
    def test_present(self):
        """Confirm that the start method is present. Required API method."""
        hasattr(self.odl, "start") and callable(getattr(self.odl, "start"))

class TestStop(unittest.TestCase):

    """Test stopping an OpenDaylight controller instance"""

    def setUp(self):
        """Build an ODL abstraction."""
        self.odl = odl_mod.OpenDaylight()

    @unittest.skip("Travis doesn't have Docker")
    def test_present(self):
        """Confirm that the stop method is present. Required API method."""
        hasattr(self.odl, "stop") and callable(getattr(self.odl, "stop"))
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""This module provides a query interface for media streams and captions."""
from typing import List, Optional, Dict

from pytube import Stream, Caption

class StreamQuery:
    """Interface for querying the available media streams."""

    def __init__(self, fmt_streams):
        """Construct a :class:`StreamQuery <StreamQuery>`.

        param list fmt_streams:
            list of :class:`Stream <Stream>` instances.
        """
        self.fmt_streams = fmt_streams
        self.itag_index = {int(s.itag): s for s in fmt_streams}

    def filter(
        self,
        fps=None,
        res=None,
        resolution=None,
        mime_type=None,
        type=None,
        subtype=None,
        file_extension=None,
        abr=None,
        bitrate=None,
        video_codec=None,
        audio_codec=None,
        only_audio=None,
        only_video=None,
        progressive=None,
        adaptive=None,
        is_dash=None,
        custom_filter_functions=None,
    ):
        """Apply the given filtering criterion.

        :param fps:
            (optional) The frames per second.
        :type fps:
            int or None

        :param resolution:
            (optional) Alias to ``res``.
        :type res:
            str or None

        :param res:
            (optional) The video resolution.
        :type resolution:
            str or None

        :param mime_type:
            (optional) Two-part identifier for file formats and format contents
            composed of a "type", a "subtype".
        :type mime_type:
            str or None

        :param type:
            (optional) Type part of the ``mime_type`` (e.g.: audio, video).
        :type type:
            str or None

        :param subtype:
            (optional) Sub-type part of the ``mime_type`` (e.g.: mp4, mov).
        :type subtype:
            str or None

        :param file_extension:
            (optional) Alias to ``sub_type``.
        :type file_extension:
            str or None

        :param abr:
            (optional) Average bitrate (ABR) refers to the average amount of
            data transferred per unit of time (e.g.: 64kbps, 192kbps).
        :type abr:
            str or None

        :param bitrate:
            (optional) Alias to ``abr``.
        :type bitrate:
            str or None

        :param video_codec:
            (optional) Video compression format.
        :type video_codec:
            str or None

        :param audio_codec:
            (optional) Audio compression format.
        :type audio_codec:
            str or None

        :param bool progressive:
            Excludes adaptive streams (one file contains both audio and video
            tracks).

        :param bool adaptive:
            Excludes progressive streams (audio and video are on separate
            tracks).

        :param bool is_dash:
            Include/exclude dash streams.

        :param bool only_audio:
            Excludes streams with video tracks.

        :param bool only_video:
            Excludes streams with audio tracks.

        :param custom_filter_functions:
            (optional) Interface for defining complex filters without
            subclassing.
        :type custom_filter_functions:
            list or None

        """
        filters = []
        if res or resolution:
            filters.append(lambda s: s.resolution == (res or resolution))

        if fps:
            filters.append(lambda s: s.fps == fps)

        if mime_type:
            filters.append(lambda s: s.mime_type == mime_type)

        if type:
            filters.append(lambda s: s.type == type)

        if subtype or file_extension:
            filters.append(lambda s: s.subtype == (subtype or file_extension))

        if abr or bitrate:
            filters.append(lambda s: s.abr == (abr or bitrate))

        if video_codec:
            filters.append(lambda s: s.video_codec == video_codec)

        if audio_codec:
            filters.append(lambda s: s.audio_codec == audio_codec)

        if only_audio:
            filters.append(
                lambda s: (s.includes_audio_track and not s.includes_video_track),
            )

        if only_video:
            filters.append(
                lambda s: (s.includes_video_track and not s.includes_audio_track),
            )

        if progressive:
            filters.append(lambda s: s.is_progressive)

        if adaptive:
            filters.append(lambda s: s.is_adaptive)

        if custom_filter_functions:
            for fn in custom_filter_functions:
                filters.append(fn)

        if is_dash is not None:
            filters.append(lambda s: s.is_dash == is_dash)

        fmt_streams = self.fmt_streams
        for fn in filters:
            fmt_streams = list(filter(fn, fmt_streams))
        return StreamQuery(fmt_streams)

    def order_by(self, attribute_name: str) -> "StreamQuery":
        """Apply a sort order. Filters out stream the do not have the attribute.

        :param str attribute_name:
            The name of the attribute to sort by.
        """
        has_attribute = [
            s for s in self.fmt_streams if getattr(s, attribute_name) is not None
        ]

        integer_attr_repr: Optional[Dict[str, int]] = None

        # check that the attribute value is a string
        if has_attribute and isinstance(
            getattr(has_attribute[0], attribute_name), str
        ):
            # attempt to extract numerical values from string
            try:
                integer_attr_repr = {
                    getattr(s, attribute_name): int(
                        "".join(list(filter(str.isdigit, getattr(s, attribute_name))))
                    )
                    for s in has_attribute
                }
            except ValueError:
                integer_attr_repr = None

        # lookup integer values if we have them
        if integer_attr_repr is not None:
            return StreamQuery(
                sorted(
                    has_attribute,
                    key=lambda s: integer_attr_repr[getattr(s, attribute_name)],  # type: ignore  # noqa: E501
                )
            )

        return StreamQuery(
            sorted(has_attribute, key=lambda s: getattr(s, attribute_name))
        )

    def desc(self) -> "StreamQuery":
        """Sort streams in descending order.

        :rtype: :class:`StreamQuery <StreamQuery>`

        """
        return StreamQuery(self.fmt_streams[::-1])

    def asc(self) -> "StreamQuery":
        """Sort streams in ascending order.

        :rtype: :class:`StreamQuery <StreamQuery>`

        """
        return self

    def get_by_itag(self, itag: int) -> Optional[Stream]:
        """Get the corresponding :class:`Stream <Stream>` for a given itag.

        :param int itag:
            YouTube format identifier code.
        :rtype: :class:`Stream <Stream>` or None
        :returns:
            The :class:`Stream <Stream>` matching the given itag or None if
            not found.

        """
        return self.itag_index.get(int(itag))

    def get_by_resolution(self, resolution: str) -> Optional[Stream]:
        """Get the corresponding :class:`Stream <Stream>` for a given resolution.
        Stream must be a progressive mp4.

        :param str resolution:
            Video resolution i.e. "720p", "480p", "360p", "240p", "144p"
        :rtype: :class:`Stream <Stream>` or None
        :returns:
            The :class:`Stream <Stream>` matching the given itag or None if
            not found.

        """
        return self.filter(
            progressive=True, subtype="mp4", resolution=resolution
        ).first()

    def get_lowest_resolution(self) -> Optional[Stream]:
        """Get lowest resolution stream that is a progressive mp4.

        :rtype: :class:`Stream <Stream>` or None
        :returns:
            The :class:`Stream <Stream>` matching the given itag or None if
            not found.

        """
        return (
            self.filter(progressive=True, subtype="mp4")
            .order_by("resolution")
            .first()
        )

    def get_highest_resolution(self) -> Optional[Stream]:
        """Get highest resolution stream that is a progressive video.

        :rtype: :class:`Stream <Stream>` or None
        :returns:
            The :class:`Stream <Stream>` matching the given itag or None if
            not found.

        """
        return self.filter(progressive=True).order_by("resolution").last()

    def get_audio_only(self, subtype: str = "mp4") -> Optional[Stream]:
        """Get highest bitrate audio stream for given codec (defaults to mp4)

        :param str subtype:
            Audio subtype, defaults to mp4
        :rtype: :class:`Stream <Stream>` or None
        :returns:
            The :class:`Stream <Stream>` matching the given itag or None if
            not found.

        """
        return (
            self.filter(only_audio=True, subtype=subtype).order_by("abr").last()
        )

    def first(self) -> Optional[Stream]:
        """Get the first :class:`Stream <Stream>` in the results.

        :rtype: :class:`Stream <Stream>` or None
        :returns:
            the first result of this query or None if the result doesn't
            contain any streams.

        """
        try:
            return self.fmt_streams[0]
        except IndexError:
            return None

    def last(self):
        """Get the last :class:`Stream <Stream>` in the results.

        :rtype: :class:`Stream <Stream>` or None
        :returns:
            Return the last result of this query or None if the result
            doesn't contain any streams.

        """
        try:
            return self.fmt_streams[-1]
        except IndexError:
            pass

    def count(self) -> int:
        """Get the count the query would return.

        :rtype: int

        """
        return len(self.fmt_streams)

    def all(self) -> List[Stream]:
        """Get all the results represented by this query as a list.

        :rtype: list

        """
        return self.fmt_streams

class CaptionQuery:
    """Interface for querying the available captions."""

    def __init__(self, captions: List[Caption]):
        """Construct a :class:`Caption <Caption>`.

        param list captions:
            list of :class:`Caption <Caption>` instances.

        """
        self.captions = captions
        self.lang_code_index = {c.code: c for c in captions}

    def get_by_language_code(self, lang_code: str) -> Optional[Caption]:
        """Get the :class:`Caption <Caption>` for a given ``lang_code``.

        :param str lang_code:
            The code that identifies the caption language.
        :rtype: :class:`Caption <Caption>` or None
        :returns:
            The :class:`Caption <Caption>` matching the given ``lang_code`` or
            None if it does not exist.
        """
        return self.lang_code_index.get(lang_code)

    def all(self) -> List[Caption]:
        """Get all the results represented by this query as a list.

        :rtype: list

        """
        return self.captions
<commit_msg>Run black, flake8<commit_after># -*- coding: utf-8 -*-
"""This module provides a query interface for media streams and captions."""
from typing import List, Optional, Dict

from pytube import Stream, Caption

class StreamQuery:
    """Interface for querying the available media streams."""

    def __init__(self, fmt_streams):
        """Construct a :class:`StreamQuery <StreamQuery>`.

        param list fmt_streams:
            list of :class:`Stream <Stream>` instances.
        """
        self.fmt_streams = fmt_streams
        self.itag_index = {int(s.itag): s for s in fmt_streams}

    def filter(
        self,
        fps=None,
        res=None,
        resolution=None,
        mime_type=None,
        type=None,
        subtype=None,
        file_extension=None,
        abr=None,
        bitrate=None,
        video_codec=None,
        audio_codec=None,
        only_audio=None,
        only_video=None,
        progressive=None,
        adaptive=None,
        is_dash=None,
        custom_filter_functions=None,
    ):
        """Apply the given filtering criterion.

        :param fps:
            (optional) The frames per second.
        :type fps:
            int or None

        :param resolution:
            (optional) Alias to ``res``.
        :type res:
            str or None

        :param res:
            (optional) The video resolution.
        :type resolution:
            str or None

        :param mime_type:
            (optional) Two-part identifier for file formats and format contents
            composed of a "type", a "subtype".
        :type mime_type:
            str or None

        :param type:
            (optional) Type part of the ``mime_type`` (e.g.: audio, video).
        :type type:
            str or None

        :param subtype:
            (optional) Sub-type part of the ``mime_type`` (e.g.: mp4, mov).
        :type subtype:
            str or None

        :param file_extension:
            (optional) Alias to ``sub_type``.
        :type file_extension:
            str or None

        :param abr:
            (optional) Average bitrate (ABR) refers to the average amount of
            data transferred per unit of time (e.g.: 64kbps, 192kbps).
        :type abr:
            str or None

        :param bitrate:
            (optional) Alias to ``abr``.
        :type bitrate:
            str or None

        :param video_codec:
            (optional) Video compression format.
        :type video_codec:
            str or None

        :param audio_codec:
            (optional) Audio compression format.
        :type audio_codec:
            str or None

        :param bool progressive:
            Excludes adaptive streams (one file contains both audio and video
            tracks).

        :param bool adaptive:
            Excludes progressive streams (audio and video are on separate
            tracks).

        :param bool is_dash:
            Include/exclude dash streams.

        :param bool only_audio:
            Excludes streams with video tracks.

        :param bool only_video:
            Excludes streams with audio tracks.

        :param custom_filter_functions:
            (optional) Interface for defining complex filters without
            subclassing.
        :type custom_filter_functions:
            list or None

        """
        filters = []
        if res or resolution:
            filters.append(lambda s: s.resolution == (res or resolution))

        if fps:
            filters.append(lambda s: s.fps == fps)

        if mime_type:
            filters.append(lambda s: s.mime_type == mime_type)

        if type:
            filters.append(lambda s: s.type == type)

        if subtype or file_extension:
            filters.append(lambda s: s.subtype == (subtype or file_extension))

        if abr or bitrate:
            filters.append(lambda s: s.abr == (abr or bitrate))

        if video_codec:
            filters.append(lambda s: s.video_codec == video_codec)

        if audio_codec:
            filters.append(lambda s: s.audio_codec == audio_codec)

        if only_audio:
            filters.append(
                lambda s: (s.includes_audio_track and not s.includes_video_track),
            )

        if only_video:
            filters.append(
                lambda s: (s.includes_video_track and not s.includes_audio_track),
            )

        if progressive:
            filters.append(lambda s: s.is_progressive)

        if adaptive:
            filters.append(lambda s: s.is_adaptive)

        if custom_filter_functions:
            for fn in custom_filter_functions:
                filters.append(fn)

        if is_dash is not None:
            filters.append(lambda s: s.is_dash == is_dash)

        fmt_streams = self.fmt_streams
        for fn in filters:
            fmt_streams = list(filter(fn, fmt_streams))
        return StreamQuery(fmt_streams)

    def order_by(self, attribute_name: str) -> "StreamQuery":
        """Apply a sort order. Filters out stream the do not have the attribute.

        :param str attribute_name:
            The name of the attribute to sort by.
        """
        has_attribute = [
            s for s in self.fmt_streams if getattr(s, attribute_name) is not None
        ]

        integer_attr_repr: Optional[Dict[str, int]] = None

        # check that the attribute value is a string
        if has_attribute and isinstance(getattr(has_attribute[0], attribute_name), str):
            # attempt to extract numerical values from string
            try:
                integer_attr_repr = {
                    getattr(s, attribute_name): int(
                        "".join(list(filter(str.isdigit, getattr(s, attribute_name))))
                    )
                    for s in has_attribute
                }
            except ValueError:
                integer_attr_repr = None

        # lookup integer values if we have them
        if integer_attr_repr is not None:
            return StreamQuery(
                sorted(
                    has_attribute,
                    key=lambda s: integer_attr_repr[getattr(s, attribute_name)],  # type: ignore  # noqa: E501
                )
            )

        return StreamQuery(
            sorted(has_attribute, key=lambda s: getattr(s, attribute_name))
        )

    def desc(self) -> "StreamQuery":
        """Sort streams in descending order.

        :rtype: :class:`StreamQuery <StreamQuery>`

        """
        return StreamQuery(self.fmt_streams[::-1])

    def asc(self) -> "StreamQuery":
        """Sort streams in ascending order.

        :rtype: :class:`StreamQuery <StreamQuery>`

        """
        return self

    def get_by_itag(self, itag: int) -> Optional[Stream]:
        """Get the corresponding :class:`Stream <Stream>` for a given itag.

        :param int itag:
            YouTube format identifier code.
        :rtype: :class:`Stream <Stream>` or None
        :returns:
            The :class:`Stream <Stream>` matching the given itag or None if
            not found.

        """
        return self.itag_index.get(int(itag))

    def get_by_resolution(self, resolution: str) -> Optional[Stream]:
        """Get the corresponding :class:`Stream <Stream>` for a given resolution.
        Stream must be a progressive mp4.

        :param str resolution:
            Video resolution i.e. "720p", "480p", "360p", "240p", "144p"
        :rtype: :class:`Stream <Stream>` or None
        :returns:
            The :class:`Stream <Stream>` matching the given itag or None if
            not found.

        """
        return self.filter(
            progressive=True, subtype="mp4", resolution=resolution
        ).first()

    def get_lowest_resolution(self) -> Optional[Stream]:
        """Get lowest resolution stream that is a progressive mp4.

        :rtype: :class:`Stream <Stream>` or None
        :returns:
            The :class:`Stream <Stream>` matching the given itag or None if
            not found.

        """
        return (
            self.filter(progressive=True, subtype="mp4").order_by("resolution").first()
        )

    def get_highest_resolution(self) -> Optional[Stream]:
        """Get highest resolution stream that is a progressive video.

        :rtype: :class:`Stream <Stream>` or None
        :returns:
            The :class:`Stream <Stream>` matching the given itag or None if
            not found.

        """
        return self.filter(progressive=True).order_by("resolution").last()

    def get_audio_only(self, subtype: str = "mp4") -> Optional[Stream]:
        """Get highest bitrate audio stream for given codec (defaults to mp4)

        :param str subtype:
            Audio subtype, defaults to mp4
        :rtype: :class:`Stream <Stream>` or None
        :returns:
            The :class:`Stream <Stream>` matching the given itag or None if
            not found.

        """
        return self.filter(only_audio=True, subtype=subtype).order_by("abr").last()

    def first(self) -> Optional[Stream]:
        """Get the first :class:`Stream <Stream>` in the results.

        :rtype: :class:`Stream <Stream>` or None
        :returns:
            the first result of this query or None if the result doesn't
            contain any streams.

        """
        try:
            return self.fmt_streams[0]
        except IndexError:
            return None

    def last(self):
        """Get the last :class:`Stream <Stream>` in the results.

        :rtype: :class:`Stream <Stream>` or None
        :returns:
            Return the last result of this query or None if the result
            doesn't contain any streams.

        """
        try:
            return self.fmt_streams[-1]
        except IndexError:
            pass

    def count(self) -> int:
        """Get the count the query would return.

        :rtype: int

        """
        return len(self.fmt_streams)

    def all(self) -> List[Stream]:
        """Get all the results represented by this query as a list.

        :rtype: list

        """
        return self.fmt_streams

class CaptionQuery:
    """Interface for querying the available captions."""

    def __init__(self, captions: List[Caption]):
        """Construct a :class:`Caption <Caption>`.

        param list captions:
            list of :class:`Caption <Caption>` instances.

        """
        self.captions = captions
        self.lang_code_index = {c.code: c for c in captions}

    def get_by_language_code(self, lang_code: str) -> Optional[Caption]:
        """Get the :class:`Caption <Caption>` for a given ``lang_code``.

        :param str lang_code:
            The code that identifies the caption language.
        :rtype: :class:`Caption <Caption>` or None
        :returns:
            The :class:`Caption <Caption>` matching the given ``lang_code`` or
            None if it does not exist.
        """
        return self.lang_code_index.get(lang_code)

    def all(self) -> List[Caption]:
        """Get all the results represented by this query as a list.

        :rtype: list

        """
        return self.captions
<|endoftext|>


<commit_before>"""
Reload programs.
"""
import re
import shutil
import subprocess

from .settings import __cache_dir__
from . import util

def xrdb(xrdb_file=None):
    """Merge the colors into the X db so new terminals use them."""
    xrdb_file = xrdb_file or __cache_dir__ / "colors.Xresources"

    if shutil.which("xrdb"):
        subprocess.call(["xrdb", "-merge", xrdb_file],
                        stdout=subprocess.DEVNULL,
                        stderr=subprocess.DEVNULL)

def i3():
    """Reload i3 colors."""
    if shutil.which("i3-msg"):
        util.disown("i3-msg", "reload")

def polybar():
    """Reload polybar colors."""
    if shutil.which("polybar"):
        util.disown("pkill", "-USR1", "polybar")

def env(xrdb_file=None):
    """Reload environment."""
    xrdb(xrdb_file)
    i3()
    polybar()
    print("reload: Reloaded environment.")

def colors(vte, cache_dir=__cache_dir__):
    """Reload the current scheme."""
    sequence_file = cache_dir / "sequences"

    if sequence_file.is_file():
        sequences = "".join(util.read_file(sequence_file))

        # If vte mode was used, remove the unsupported sequence.
        if vte:
            sequences = re.sub(r"\]708;(\[..\])?\#.{6}", "", sequences)

        print(sequences, end="")

    exit(0)
<commit_msg>general: Fix regex issue.<commit_after>"""
Reload programs.
"""
import re
import shutil
import subprocess

from .settings import __cache_dir__
from . import util

def xrdb(xrdb_file=None):
    """Merge the colors into the X db so new terminals use them."""
    xrdb_file = xrdb_file or __cache_dir__ / "colors.Xresources"

    if shutil.which("xrdb"):
        subprocess.call(["xrdb", "-merge", xrdb_file],
                        stdout=subprocess.DEVNULL,
                        stderr=subprocess.DEVNULL)

def i3():
    """Reload i3 colors."""
    if shutil.which("i3-msg"):
        util.disown("i3-msg", "reload")

def polybar():
    """Reload polybar colors."""
    if shutil.which("polybar"):
        util.disown("pkill", "-USR1", "polybar")

def env(xrdb_file=None):
    """Reload environment."""
    xrdb(xrdb_file)
    i3()
    polybar()
    print("reload: Reloaded environment.")

def colors(vte, cache_dir=__cache_dir__):
    """Reload the current scheme."""
    sequence_file = cache_dir / "sequences"

    if sequence_file.is_file():
        sequences = "".join(util.read_file(sequence_file))

        # If vte mode was used, remove the unsupported sequence.
        if vte:
            sequences = re.sub(r"\]708;(\[.{0,3}\])?\#.{6}", "", sequences)

        print(sequences, end="")

    exit(0)
<|endoftext|>


<commit_before>from qds_sdk.qubole import Qubole
from qds_sdk.resource import Resource
import argparse
import json

class UserCmdLine:
    @staticmethod
    def parsers():
        argparser = argparse.ArgumentParser(
            prog="qds.py user",
            description="User level operations for Qubole Data Service.")
        subparsers = argparser.add_subparsers(title="user operations")

        # To invite a new user
        invite = subparsers.add_parser(
            "invite", help="Invite a new user")
        invite.add_argument(
            "--email", dest="invitee_email", required=True,
            help="Email address of the new user to be added to the Qubole account")
        invite.add_argument(
            "--account-id", dest="account", required=True,
            help="Account ID of the current user")
        invite.add_argument(
            "--groups", dest="groups", required=False,
            help="Add groups to the new user, defaults to system-user")
        invite.set_defaults(func=UserCmdLine.invite)

        # enable qbol user
        enable = subparsers.add_parser(
            "enable", help="Enable a qbol user")
        enable.add_argument(
            "--qbol-user-id", dest="qbol_user_id", required=True,
            help="ID of the qbol_user, who should be enabled")
        enable.set_defaults(func=UserCmdLine.enable)

        # disable qbol user
        disable = subparsers.add_parser(
            "disable", help="Disable a qbol user")
        disable.add_argument(
            "--qbol-user-id", dest="qbol_user_id", required=True,
            help="ID of the qbol_user, who should be disabled")
        disable.set_defaults(func=UserCmdLine.disable)

        return argparser

    @staticmethod
    def run(args):
        parser = UserCmdLine.parsers()
        parsed = parser.parse_args(args)
        return parsed.func(parsed)

    @staticmethod
    def invite(args):
        data = vars(args)
        data.pop("func")
        result = User.invite("invite_new", data)
        return json.dumps(result, indent=4)

    @staticmethod
    def enable(args):
        data = vars(args)
        data.pop("func")
        result = Account.enable_disable("enable_qbol_user", data)
        return json.dumps(result, indent=4)

    @staticmethod
    def disable(args):
        data = vars(args)
        data.pop("func")
        result = Account.enable_disable("disable_qbol_user", data)
        return json.dumps(result, indent=4)

class User(Resource):

    rest_entity_path = "users"

    @classmethod
    def invite(cls, path, data):
        conn = Qubole.agent()
        return conn.post(cls.element_path(path), data)

class Account(Resource):
    rest_entity_path = "accounts"

    @classmethod
    def enable_disable(cls, path, data):
        conn = Qubole.agent()
        return conn.post(cls.element_path(path), data)
<commit_msg>changed name of classs<commit_after>from qds_sdk.qubole import Qubole
from qds_sdk.resource import Resource
import argparse
import json

class UserCmdLine:
    @staticmethod
    def parsers():
        argparser = argparse.ArgumentParser(
            prog="qds.py user",
            description="User level operations for Qubole Data Service.")
        subparsers = argparser.add_subparsers(title="user operations")

        # To invite a new user
        invite = subparsers.add_parser(
            "invite", help="Invite a new user")
        invite.add_argument(
            "--email", dest="invitee_email", required=True,
            help="Email address of the new user to be added to the Qubole account")
        invite.add_argument(
            "--account-id", dest="account", required=True,
            help="Account ID of the current user")
        invite.add_argument(
            "--groups", dest="groups", required=False,
            help="Add groups to the new user, defaults to system-user")
        invite.set_defaults(func=UserCmdLine.invite)

        # enable qbol user
        enable = subparsers.add_parser(
            "enable", help="Enable a qbol user")
        enable.add_argument(
            "--qbol-user-id", dest="qbol_user_id", required=True,
            help="ID of the qbol_user, who should be enabled")
        enable.set_defaults(func=UserCmdLine.enable)

        # disable qbol user
        disable = subparsers.add_parser(
            "disable", help="Disable a qbol user")
        disable.add_argument(
            "--qbol-user-id", dest="qbol_user_id", required=True,
            help="ID of the qbol_user, who should be disabled")
        disable.set_defaults(func=UserCmdLine.disable)

        return argparser

    @staticmethod
    def run(args):
        parser = UserCmdLine.parsers()
        parsed = parser.parse_args(args)
        return parsed.func(parsed)

    @staticmethod
    def invite(args):
        data = vars(args)
        data.pop("func")
        result = User.invite("invite_new", data)
        return json.dumps(result, indent=4)

    @staticmethod
    def enable(args):
        data = vars(args)
        data.pop("func")
        result = Accounts.enable_disable("enable_qbol_user", data)
        return json.dumps(result, indent=4)

    @staticmethod
    def disable(args):
        data = vars(args)
        data.pop("func")
        result = Accounts.enable_disable("disable_qbol_user", data)
        return json.dumps(result, indent=4)

class User(Resource):

    rest_entity_path = "users"

    @classmethod
    def invite(cls, path, data):
        conn = Qubole.agent()
        return conn.post(cls.element_path(path), data)

class Accounts(Resource):
    rest_entity_path = "accounts"

    @classmethod
    def enable_disable(cls, path, data):
        conn = Qubole.agent()
        return conn.post(cls.element_path(path), data)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
    pygments.lexers.teraterm
    ~~~~~~~~~~~~~~~~~~~~~~~~

    Lexer for Tera Term macro files.

    :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

import re

from pygments.lexer import RegexLexer, include, bygroups
from pygments.token import Text, Comment, Operator, Name, String, \
    Number, Keyword

__all__ = ['TeraTermLexer']

class TeraTermLexer(RegexLexer):
    """
    For `Tera Term <https://ttssh2.osdn.jp/>`_ macro source code.

    .. versionadded:: 2.4
    """
    name = 'Tera Term macro'
    aliases = ['ttl', 'teraterm', 'teratermmacro']
    filenames = ['*.ttl']
    mimetypes = ['text/x-teratermmacro']

    tokens = {
        'root': [
            include('comments'),
            include('labels'),
            include('commands'),
            include('builtin-variables'),
            include('user-variables'),
            include('operators'),
            include('numeric-literals'),
            include('string-literals'),
            include('all-whitespace'),
            (r'[^\s]', Text),
        ],
        'comments': [
            (r';[^\r\n]*', Comment.Single),
            (r'/\*', Comment.Multiline, 'in-comment'),
        ],
        'in-comment': [
            (r'\*/', Comment.Multiline, '#pop'),
            (r'[^*/]+', Comment.Multiline),
            (r'[*/]', Comment.Multiline)
        ],
        'labels': [
            (r'(?i)^(\s*)(:[0-9a-z_]+)', bygroups(Text, Name.Label)),
        ],
        'commands': [
            (
                r'(?i)\b('
                r'basename|beep|bplusrecv|bplussend|break|bringupbox|'
                r'callmenu|changedir|checksum16|checksum16file|'
                r'checksum32|checksum32file|checksum8|checksum8file|'
                r'clearscreen|clipb2var|closesbox|closett|code2str|'
                r'connect|continue|crc16|crc16file|crc32|crc32file|'
                r'cygconnect|delpassword|dirname|dirnamebox|disconnect|'
                r'dispstr|do|else|elseif|enablekeyb|end|endif|enduntil|'
                r'endwhile|exec|execcmnd|exit|expandenv|fileclose|'
                r'fileconcat|filecopy|filecreate|filedelete|filelock|'
                r'filemarkptr|filenamebox|fileopen|fileread|filereadln|'
                r'filerename|filesearch|fileseek|fileseekback|filestat|'
                r'filestrseek|filestrseek2|filetruncate|fileunlock|'
                r'filewrite|filewriteln|findclose|findfirst|findnext|'
                r'flushrecv|foldercreate|folderdelete|foldersearch|for|'
                r'getdate|getdir|getenv|getfileattr|gethostname|'
                r'getipv4addr|getipv6addr|getmodemstatus|getpassword|'
                r'getspecialfolder|gettime|gettitle|getttdir|getver|'
                r'if|ifdefined|include|inputbox|int2str|intdim|'
                r'ispassword|kmtfinish|kmtget|kmtrecv|kmtsend|listbox|'
                r'loadkeymap|logautoclosemode|logclose|loginfo|logopen|'
                r'logpause|logrotate|logstart|logwrite|loop|makepath|'
                r'messagebox|mpause|next|passwordbox|pause|quickvanrecv|'
                r'quickvansend|random|recvln|regexoption|restoresetup|'
                r'return|rotateleft|rotateright|scprecv|scpsend|send|'
                r'sendbreak|sendbroadcast|sendfile|sendkcode|sendln|'
                r'sendlnbroadcast|sendlnmulticast|sendmulticast|setbaud|'
                r'setdate|setdebug|setdir|setdlgpos|setdtr|setecho|'
                r'setenv|setexitcode|setfileattr|setflowctrl|'
                r'setmulticastname|setpassword|setrts|setsync|settime|'
                r'settitle|show|showtt|sprintf|sprintf2|statusbox|'
                r'str2code|str2int|strcompare|strconcat|strcopy|strdim|'
                r'strinsert|strjoin|strlen|strmatch|strremove|'
                r'strreplace|strscan|strspecial|strsplit|strtrim|'
                r'testlink|then|tolower|toupper|unlink|until|uptime|'
                r'var2clipb|wait|wait4all|waitevent|waitln|waitn|'
                r'waitrecv|waitregex|while|xmodemrecv|xmodemsend|'
                r'yesnobox|ymodemrecv|ymodemsend|zmodemrecv|zmodemsend'
                r')\b',
                Keyword,
            ),
            (
                r'(?i)(call|goto)([ \t]+)([0-9a-z_]+)',
                bygroups(Keyword, Text, Name.Label),
            )
        ],
        'builtin-variables': [
            (
                r'(?i)('
                r'groupmatchstr1|groupmatchstr2|groupmatchstr3|'
                r'groupmatchstr4|groupmatchstr5|groupmatchstr6|'
                r'groupmatchstr7|groupmatchstr8|groupmatchstr9|'
                r'param1|param2|param3|param4|param5|param6|'
                r'param7|param8|param9|paramcnt|params|'
                r'inputstr|matchstr|mtimeout|result|timeout'
                r')\b',
                Name.Builtin
            ),
        ],
        'user-variables': [
            (r'(?i)[A-Z_][A-Z0-9_]*', Name.Variable),
        ],
        'numeric-literals': [
            (r'(-?)([0-9]+)', bygroups(Operator, Number.Integer)),
            (r'(?i)\$[0-9a-f]+', Number.Hex),
        ],
        'string-literals': [
            (r'(?i)#(?:[0-9]+|\$[0-9a-f]+)', String.Char),
            (r"'", String.Single, 'in-single-string'),
            (r'"', String.Double, 'in-double-string'),
        ],
        'in-general-string': [
            (r'[\\][\\nt]', String.Escape),  # Only three escapes are supported.
            (r'.', String),
        ],
        'in-single-string': [
            (r"'", String.Single, '#pop'),
            include('in-general-string'),
        ],
        'in-double-string': [
            (r'"', String.Double, '#pop'),
            include('in-general-string'),
        ],
        'operators': [
            (r'and|not|or|xor', Operator.Word),
            (r'[!%&*+<=>^~\|\/-]+', Operator),
            (r'[()]', String.Symbol),
        ],
        'all-whitespace': [
            (r'[\s]+', Text),
        ],
    }

    # Turtle and Tera Term macro files share the same file extension
    # but each has a recognizable and distinct syntax.
    def analyse_text(text):
        result = 0.0
        if re.search(TeraTermLexer.tokens['commands'][0][0], text):
            result += 0.01
        return result
<commit_msg>Update the Tera Term macro lexer (new 'setspeed' command) (#1337)<commit_after># -*- coding: utf-8 -*-
"""
    pygments.lexers.teraterm
    ~~~~~~~~~~~~~~~~~~~~~~~~

    Lexer for Tera Term macro files.

    :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

import re

from pygments.lexer import RegexLexer, include, bygroups
from pygments.token import Text, Comment, Operator, Name, String, \
    Number, Keyword

__all__ = ['TeraTermLexer']

class TeraTermLexer(RegexLexer):
    """
    For `Tera Term <https://ttssh2.osdn.jp/>`_ macro source code.

    .. versionadded:: 2.4
    """
    name = 'Tera Term macro'
    aliases = ['ttl', 'teraterm', 'teratermmacro']
    filenames = ['*.ttl']
    mimetypes = ['text/x-teratermmacro']

    tokens = {
        'root': [
            include('comments'),
            include('labels'),
            include('commands'),
            include('builtin-variables'),
            include('user-variables'),
            include('operators'),
            include('numeric-literals'),
            include('string-literals'),
            include('all-whitespace'),
            (r'[^\s]', Text),
        ],
        'comments': [
            (r';[^\r\n]*', Comment.Single),
            (r'/\*', Comment.Multiline, 'in-comment'),
        ],
        'in-comment': [
            (r'\*/', Comment.Multiline, '#pop'),
            (r'[^*/]+', Comment.Multiline),
            (r'[*/]', Comment.Multiline)
        ],
        'labels': [
            (r'(?i)^(\s*)(:[0-9a-z_]+)', bygroups(Text, Name.Label)),
        ],
        'commands': [
            (
                r'(?i)\b('
                r'basename|'
                r'beep|'
                r'bplusrecv|'
                r'bplussend|'
                r'break|'
                r'bringupbox|'
                # 'call' is handled separately.
                r'callmenu|'
                r'changedir|'
                r'checksum16|'
                r'checksum16file|'
                r'checksum32|'
                r'checksum32file|'
                r'checksum8|'
                r'checksum8file|'
                r'clearscreen|'
                r'clipb2var|'
                r'closesbox|'
                r'closett|'
                r'code2str|'
                r'connect|'
                r'continue|'
                r'crc16|'
                r'crc16file|'
                r'crc32|'
                r'crc32file|'
                r'cygconnect|'
                r'delpassword|'
                r'dirname|'
                r'dirnamebox|'
                r'disconnect|'
                r'dispstr|'
                r'do|'
                r'else|'
                r'elseif|'
                r'enablekeyb|'
                r'end|'
                r'endif|'
                r'enduntil|'
                r'endwhile|'
                r'exec|'
                r'execcmnd|'
                r'exit|'
                r'expandenv|'
                r'fileclose|'
                r'fileconcat|'
                r'filecopy|'
                r'filecreate|'
                r'filedelete|'
                r'filelock|'
                r'filemarkptr|'
                r'filenamebox|'
                r'fileopen|'
                r'fileread|'
                r'filereadln|'
                r'filerename|'
                r'filesearch|'
                r'fileseek|'
                r'fileseekback|'
                r'filestat|'
                r'filestrseek|'
                r'filestrseek2|'
                r'filetruncate|'
                r'fileunlock|'
                r'filewrite|'
                r'filewriteln|'
                r'findclose|'
                r'findfirst|'
                r'findnext|'
                r'flushrecv|'
                r'foldercreate|'
                r'folderdelete|'
                r'foldersearch|'
                r'for|'
                r'getdate|'
                r'getdir|'
                r'getenv|'
                r'getfileattr|'
                r'gethostname|'
                r'getipv4addr|'
                r'getipv6addr|'
                r'getmodemstatus|'
                r'getpassword|'
                r'getspecialfolder|'
                r'gettime|'
                r'gettitle|'
                r'getttdir|'
                r'getver|'
                # 'goto' is handled separately.
                r'if|'
                r'ifdefined|'
                r'include|'
                r'inputbox|'
                r'int2str|'
                r'intdim|'
                r'ispassword|'
                r'kmtfinish|'
                r'kmtget|'
                r'kmtrecv|'
                r'kmtsend|'
                r'listbox|'
                r'loadkeymap|'
                r'logautoclosemode|'
                r'logclose|'
                r'loginfo|'
                r'logopen|'
                r'logpause|'
                r'logrotate|'
                r'logstart|'
                r'logwrite|'
                r'loop|'
                r'makepath|'
                r'messagebox|'
                r'mpause|'
                r'next|'
                r'passwordbox|'
                r'pause|'
                r'quickvanrecv|'
                r'quickvansend|'
                r'random|'
                r'recvln|'
                r'regexoption|'
                r'restoresetup|'
                r'return|'
                r'rotateleft|'
                r'rotateright|'
                r'scprecv|'
                r'scpsend|'
                r'send|'
                r'sendbreak|'
                r'sendbroadcast|'
                r'sendfile|'
                r'sendkcode|'
                r'sendln|'
                r'sendlnbroadcast|'
                r'sendlnmulticast|'
                r'sendmulticast|'
                r'setbaud|'
                r'setdate|'
                r'setdebug|'
                r'setdir|'
                r'setdlgpos|'
                r'setdtr|'
                r'setecho|'
                r'setenv|'
                r'setexitcode|'
                r'setfileattr|'
                r'setflowctrl|'
                r'setmulticastname|'
                r'setpassword|'
                r'setrts|'
                r'setspeed|'
                r'setsync|'
                r'settime|'
                r'settitle|'
                r'show|'
                r'showtt|'
                r'sprintf|'
                r'sprintf2|'
                r'statusbox|'
                r'str2code|'
                r'str2int|'
                r'strcompare|'
                r'strconcat|'
                r'strcopy|'
                r'strdim|'
                r'strinsert|'
                r'strjoin|'
                r'strlen|'
                r'strmatch|'
                r'strremove|'
                r'strreplace|'
                r'strscan|'
                r'strspecial|'
                r'strsplit|'
                r'strtrim|'
                r'testlink|'
                r'then|'
                r'tolower|'
                r'toupper|'
                r'unlink|'
                r'until|'
                r'uptime|'
                r'var2clipb|'
                r'wait|'
                r'wait4all|'
                r'waitevent|'
                r'waitln|'
                r'waitn|'
                r'waitrecv|'
                r'waitregex|'
                r'while|'
                r'xmodemrecv|'
                r'xmodemsend|'
                r'yesnobox|'
                r'ymodemrecv|'
                r'ymodemsend|'
                r'zmodemrecv|'
                r'zmodemsend'
                r')\b',
                Keyword,
            ),
            (
                r'(?i)(call|goto)([ \t]+)([0-9a-z_]+)',
                bygroups(Keyword, Text, Name.Label),
            )
        ],
        'builtin-variables': [
            (
                r'(?i)('
                r'groupmatchstr1|'
                r'groupmatchstr2|'
                r'groupmatchstr3|'
                r'groupmatchstr4|'
                r'groupmatchstr5|'
                r'groupmatchstr6|'
                r'groupmatchstr7|'
                r'groupmatchstr8|'
                r'groupmatchstr9|'
                r'inputstr|'
                r'matchstr|'
                r'mtimeout|'
                r'param1|'
                r'param2|'
                r'param3|'
                r'param4|'
                r'param5|'
                r'param6|'
                r'param7|'
                r'param8|'
                r'param9|'
                r'paramcnt|'
                r'params|'
                r'result|'
                r'timeout'
                r')\b',
                Name.Builtin
            ),
        ],
        'user-variables': [
            (r'(?i)[A-Z_][A-Z0-9_]*', Name.Variable),
        ],
        'numeric-literals': [
            (r'(-?)([0-9]+)', bygroups(Operator, Number.Integer)),
            (r'(?i)\$[0-9a-f]+', Number.Hex),
        ],
        'string-literals': [
            (r'(?i)#(?:[0-9]+|\$[0-9a-f]+)', String.Char),
            (r"'", String.Single, 'in-single-string'),
            (r'"', String.Double, 'in-double-string'),
        ],
        'in-general-string': [
            (r'[\\][\\nt]', String.Escape),  # Only three escapes are supported.
            (r'.', String),
        ],
        'in-single-string': [
            (r"'", String.Single, '#pop'),
            include('in-general-string'),
        ],
        'in-double-string': [
            (r'"', String.Double, '#pop'),
            include('in-general-string'),
        ],
        'operators': [
            (r'and|not|or|xor', Operator.Word),
            (r'[!%&*+<=>^~\|\/-]+', Operator),
            (r'[()]', String.Symbol),
        ],
        'all-whitespace': [
            (r'[\s]+', Text),
        ],
    }

    # Turtle and Tera Term macro files share the same file extension
    # but each has a recognizable and distinct syntax.
    def analyse_text(text):
        if re.search(TeraTermLexer.tokens['commands'][0][0], text):
            return 0.01
        return 0.0
<|endoftext|>


<commit_before>"""
The MIT License (MIT)

Copyright (c) 2016 Daniele Linguaglossa <d.linguaglossa@mseclab.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from errors import PJFInvalidType, PJFMissingArgument, PJFBaseException
from pjf_mutators import PJFMutators
from pjf_mutation import PJFMutation
from pjf_encoder import PJFEncoder
from pjf_logger import PJFLogger
import time
import json
import urllib

class PJFFactory(object):

    def __init__(self, configuration):
        """
        Class that represent a JSON object
        """
        self.logger = self.init_logger()
        if ["json", "json_file", "strong_fuzz", "parameters", "exclude_parameters", "url_encode", "indent",
                "utf8"] not in configuration:
            raise PJFMissingArgument("Some arguments are missing from PJFFactory object")

        self.config = configuration
        self.mutator = PJFMutation(self.config)
        other = self.config.json
        if not self.config.strong_fuzz:
            if type(other) == dict:
                self.json = other
            elif type(other) == list:
                self.json = {"array": other}
            else:
                raise PJFInvalidType(other, dict)
        else:
            if self.config.json_file:
                self.json = other
            else:
                self.json = json.dumps(other)
        self.logger.debug("[{0}] - PJFFactory successfully initialized".format(time.strftime("%H:%M:%S")))

    def __add__(self, other):
        """
        Add keys to dictionary merging with another dictionary object
        """
        self.json.update(other)
        return self

    def __sub__(self, other):
        """
        Removes keys from self dictionary based on provided list
        """
        if type(other) == list:
            for element in other:
                if element in self.json:
                    del self.json[element]
            return self
        else:
            raise PJFInvalidType(other, list)

    def __eq__(self, other):
        """
        Check if two object are equal
        """
        return self.json == other

    def __getitem__(self, item):
        """
        Extract an item from the JSON object
        """
        if type(item) == str:
            return self.json[item]
        else:
            return self.json

    def __setitem__(self, key, value):
        """
        Set a JSON attribute
        """
        self.json[key] = value

    def __contains__(self, items):
        """
        Check if JSON object contains a key
        """
        try:
            if type(items) != list:
                raise PJFInvalidType(items, list)
            ret = 0
            for item in items:
                for key in self.json:
                    if isinstance(self.json[key], PJFFactory):
                        ret += item in self.json[key]
                    elif item == key:
                        ret += 1
            return len(items) == ret
        except Exception as e:
            raise PJFBaseException(e.message)

    def __repr__(self):
        """
        Represent the JSON object
        """
        return str(self.json)

    def fuzz_elements(self, element):
        """
        Fuzz all elements inside the object
        """
        try:
            if type(element) == dict:
                tmp_element = {}
                for key in element:
                    if self.config.parameters:
                        if self.config.exclude_parameters:
                            fuzz = key not in self.config.parameters
                        else:
                            fuzz = key in self.config.parameters
                    else:
                        fuzz = True
                    if fuzz:
                        if type(element[key]) == dict:
                            tmp_element.update({key: self.fuzz_elements(element[key])})
                        elif type(element[key]) == list:
                            tmp_element.update({key: self.fuzz_elements(element[key])})
                        else:
                            tmp_element.update({key: self.mutator.fuzz(element[key])})
                    else:
                        tmp_element.update({key: element[key]})
                element = tmp_element
                del tmp_element
            elif type(element) == list:
                arr = []
                for key in element:
                    if type(key) == dict:
                        arr.append(self.fuzz_elements(key))
                    elif type(key) == list:
                        arr.append(self.fuzz_elements(key))
                    else:
                        arr.append(self.mutator.fuzz(key))
                element = arr
                del arr
        except Exception as e:
            raise PJFBaseException(e.message)
        return element

    def init_logger(self):
        """
        Init the default logger
        """
        return PJFLogger.init_logger()

    @property
    def fuzzed(self):
        """
        Get a printable fuzzed object
        """
        try:
            if self.config.strong_fuzz:
                fuzzer = PJFMutators(self.config)
                if self.config.url_encode:
                    return urllib.quote(fuzzer.fuzz(json.dumps(self.config.json)))
                else:
                    if type(self.config.json) in [list, dict]:
                        return fuzzer.fuzz(json.dumps(self.config.json))
                    else:
                        return fuzzer.fuzz(self.config.json)
            else:
                if self.config.url_encode:
                    return urllib.quote(self.get_fuzzed(self.config.indent, self.config.utf8))
                else:
                    return self.get_fuzzed(self.config.indent, self.config.utf8)
        except Exception as e:
            raise PJFBaseException(e.message)

    @PJFEncoder.json_encode
    def get_fuzzed(self, indent=False, utf8=False):
        """
        Return the fuzzed object
        """
        try:
            if "array" in self.json:
                return self.fuzz_elements(dict(self.json))["array"]
            else:
                return self.fuzz_elements(dict(self.json))
        except Exception as e:
            raise PJFBaseException(e.message)
<commit_msg>fixed bug with parameter fuzzing in nested object<commit_after>"""
The MIT License (MIT)

Copyright (c) 2016 Daniele Linguaglossa <d.linguaglossa@mseclab.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from errors import PJFInvalidType, PJFMissingArgument, PJFBaseException
from pjf_mutators import PJFMutators
from pjf_mutation import PJFMutation
from pjf_encoder import PJFEncoder
from pjf_logger import PJFLogger
import time
import json
import urllib

class PJFFactory(object):

    def __init__(self, configuration):
        """
        Class that represent a JSON object
        """
        self.logger = self.init_logger()
        if ["json", "json_file", "strong_fuzz", "parameters", "exclude_parameters", "url_encode", "indent",
                "utf8"] not in configuration:
            raise PJFMissingArgument("Some arguments are missing from PJFFactory object")

        self.config = configuration
        self.mutator = PJFMutation(self.config)
        other = self.config.json
        if not self.config.strong_fuzz:
            if type(other) == dict:
                self.json = other
            elif type(other) == list:
                self.json = {"array": other}
            else:
                raise PJFInvalidType(other, dict)
        else:
            if self.config.json_file:
                self.json = other
            else:
                self.json = json.dumps(other)
        self.logger.debug("[{0}] - PJFFactory successfully initialized".format(time.strftime("%H:%M:%S")))

    def __add__(self, other):
        """
        Add keys to dictionary merging with another dictionary object
        """
        self.json.update(other)
        return self

    def __sub__(self, other):
        """
        Removes keys from self dictionary based on provided list
        """
        if type(other) == list:
            for element in other:
                if element in self.json:
                    del self.json[element]
            return self
        else:
            raise PJFInvalidType(other, list)

    def __eq__(self, other):
        """
        Check if two object are equal
        """
        return self.json == other

    def __getitem__(self, item):
        """
        Extract an item from the JSON object
        """
        if type(item) == str:
            return self.json[item]
        else:
            return self.json

    def __setitem__(self, key, value):
        """
        Set a JSON attribute
        """
        self.json[key] = value

    def __contains__(self, items):
        """
        Check if JSON object contains a key
        """
        try:
            if type(items) != list:
                raise PJFInvalidType(items, list)
            ret = 0
            for item in items:
                for key in self.json:
                    if isinstance(self.json[key], PJFFactory):
                        ret += item in self.json[key]
                    elif item == key:
                        ret += 1
            return len(items) == ret
        except Exception as e:
            raise PJFBaseException(e.message)

    def __repr__(self):
        """
        Represent the JSON object
        """
        return str(self.json)

    def fuzz_elements(self, element):
        """
        Fuzz all elements inside the object
        """
        try:
            if type(element) == dict:
                tmp_element = {}
                for key in element:
                    if self.config.parameters:
                        if self.config.exclude_parameters:
                            fuzz = key not in self.config.parameters
                        else:
                            fuzz = key in self.config.parameters
                    else:
                        fuzz = True
                    if fuzz:
                        if type(element[key]) == dict:
                            tmp_element.update({key: self.fuzz_elements(element[key])})
                        elif type(element[key]) == list:
                            tmp_element.update({key: self.fuzz_elements(element[key])})
                        else:
                            tmp_element.update({key: self.mutator.fuzz(element[key])})
                    else:
                        tmp_element.update({key: self.fuzz_elements(element[key])})
                element = tmp_element
                del tmp_element
            elif type(element) == list:
                arr = []
                for key in element:
                    if type(key) == dict:
                        arr.append(self.fuzz_elements(key))
                    elif type(key) == list:
                        arr.append(self.fuzz_elements(key))
                    else:
                        arr.append(self.mutator.fuzz(key))
                element = arr
                del arr
        except Exception as e:
            raise PJFBaseException(e.message)
        return element

    def init_logger(self):
        """
        Init the default logger
        """
        return PJFLogger.init_logger()

    @property
    def fuzzed(self):
        """
        Get a printable fuzzed object
        """
        try:
            if self.config.strong_fuzz:
                fuzzer = PJFMutators(self.config)
                if self.config.url_encode:
                    return urllib.quote(fuzzer.fuzz(json.dumps(self.config.json)))
                else:
                    if type(self.config.json) in [list, dict]:
                        return fuzzer.fuzz(json.dumps(self.config.json))
                    else:
                        return fuzzer.fuzz(self.config.json)
            else:
                if self.config.url_encode:
                    return urllib.quote(self.get_fuzzed(self.config.indent, self.config.utf8))
                else:
                    return self.get_fuzzed(self.config.indent, self.config.utf8)
        except Exception as e:
            raise PJFBaseException(e.message)

    @PJFEncoder.json_encode
    def get_fuzzed(self, indent=False, utf8=False):
        """
        Return the fuzzed object
        """
        try:
            if "array" in self.json:
                return self.fuzz_elements(dict(self.json))["array"]
            else:
                return self.fuzz_elements(dict(self.json))
        except Exception as e:
            raise PJFBaseException(e.message)
<|endoftext|>


<commit_before>"""Unit tests for cartoframes.data."""

from pandas import concat
from geopandas import GeoDataFrame
from shapely.geometry import box, Point

from cartoframes import CartoDataFrame

class TestCartoDataFrame(object):

    def setup_method(self):
        self.gdf = GeoDataFrame(
            {
                'id': [1, 2],
                'my_geometry': [box(1, 1, 2, 2), box(3, 3, 4, 4)],
                'other_geometry': ['Point(0 0)', 'Point(1 1)']
            },
            geometry='my_geometry',
            crs='epsg:4326'
        )

    def test_constructor(self):
        cdf = CartoDataFrame(self.gdf)
        assert isinstance(cdf, CartoDataFrame)
        assert cdf._constructor == CartoDataFrame

    def test_crs_gdf(self):
        cdf = CartoDataFrame(self.gdf)
        assert cdf.crs == 'epsg:4326'

    def test_crs_custom(self):
        cdf = CartoDataFrame(self.gdf, crs='epsg:3857')
        assert cdf.crs == 'epsg:3857'

    def test_to_crs(self):
        cdf = CartoDataFrame(self.gdf)
        cdf = cdf.to_crs('epsg:3857')
        assert isinstance(cdf, CartoDataFrame)
        assert cdf.crs == 'epsg:3857'

    def test_geometry_gdf(self):
        cdf = CartoDataFrame(self.gdf)
        assert cdf.geometry.name == 'my_geometry'

    def test_geometry_custom(self):
        cdf = CartoDataFrame(self.gdf, geometry='other_geometry')
        assert cdf.geometry.name == 'other_geometry'

    def test_set_geometry(self):
        cdf = CartoDataFrame(self.gdf)
        cdf = cdf.set_geometry('other_geometry')
        assert isinstance(cdf, CartoDataFrame)
        assert cdf.geometry.name == 'other_geometry'

    def test_from_carto(self, mocker):
        mock = mocker.patch('cartoframes.io.carto.read_carto')
        CartoDataFrame.from_carto('table_name')
        mock.assert_called_once_with('table_name', None, None, 3, None, None, True)

    def test_from_file(self, mocker):
        mock = mocker.patch.object(GeoDataFrame, 'from_file')
        cdf = CartoDataFrame.from_file('file_name.geojson')
        mock.assert_called_once_with('file_name.geojson')
        assert isinstance(cdf, CartoDataFrame)

    def test_from_features(self, mocker):
        mock = mocker.patch.object(GeoDataFrame, 'from_features')
        cdf = CartoDataFrame.from_features('__features__')
        mock.assert_called_once_with('__features__')
        assert isinstance(cdf, CartoDataFrame)

    def test_to_carto(self, mocker):
        mock = mocker.patch('cartoframes.io.carto.to_carto')
        cdf = CartoDataFrame(self.gdf)
        cdf.to_carto('table_name')
        mock.assert_called_once_with(cdf, 'table_name', None, 'fail', None, False, None)

    def test_viz(self, mocker):
        mocker.patch('cartoframes.viz.Map', return_value='__map__')
        mock_layer = mocker.patch('cartoframes.viz.Layer')
        cdf = CartoDataFrame(self.gdf)
        viz = cdf.viz('__style__')
        mock_layer.assert_called_once_with(cdf, '__style__', None, None, None, None, None, None)
        assert viz == '__map__'

    def test_getitem_series(self):
        cdf = CartoDataFrame(self.gdf)
        cdf = cdf[cdf.id > 1]
        assert isinstance(cdf, CartoDataFrame)
        assert len(cdf) == 1

    def test_getitem_list(self):
        cdf = CartoDataFrame(self.gdf)
        cdf = cdf[['id']]
        assert isinstance(cdf, CartoDataFrame)
        assert len(cdf) == 2

    def test_getitem_slice(self):
        cdf = CartoDataFrame(self.gdf)
        cdf = cdf[slice(1)]
        assert isinstance(cdf, CartoDataFrame)
        assert len(cdf) == 1

    def test_astype(self):
        cdf = CartoDataFrame({'a': [1], 'geometry': [Point(0, 0)]})
        cdf = cdf.astype('object')
        assert isinstance(cdf, CartoDataFrame)

    def test_merge(self):
        cdf1 = CartoDataFrame({'lkey': ['foo', 'bar'], 'value': [1, 2]})
        cdf2 = CartoDataFrame({'rkey': ['foo', 'bar'], 'value': [5, 6]})
        cdf = cdf1.merge(cdf2, left_on='lkey', right_on='rkey')
        assert isinstance(cdf, CartoDataFrame)

    def test_dissolve(self):
        cdf = CartoDataFrame({'a': [1], 'geometry': [Point(0, 0)]})
        cdf = cdf.dissolve(by='a')
        assert isinstance(cdf, CartoDataFrame)

    def test_explode(self):
        cdf = CartoDataFrame({'a': [1], 'geometry': [Point(0, 0)]})
        cdf = cdf.explode()
        assert isinstance(cdf, CartoDataFrame)

    def test_concat(self):
        cdf1 = CartoDataFrame({'a': [1], 'geometry': [Point(0, 0)]})
        cdf2 = CartoDataFrame({'a': [2], 'geometry': [Point(1, 1)]})
        cdf = concat([cdf1, cdf2])
        assert isinstance(cdf, CartoDataFrame)

    def test_plot(self):
        cdf = CartoDataFrame({'x': [1], 'y': [1], 'geometry': [Point(0, 0)]})
        # Assert it works without exception
        cdf['x'].plot()
        cdf['geometry'].plot()
        cdf[['x', 'y']].plot()
        cdf[['x', 'geometry']].plot(column='x')
<commit_msg>Improve plot tests using mocks<commit_after>"""Unit tests for cartoframes.data."""

from pandas import concat
from pandas import DataFrame, Series
from geopandas import GeoDataFrame, GeoSeries
from shapely.geometry import box, Point

from cartoframes import CartoDataFrame

class TestCartoDataFrame(object):

    def setup_method(self):
        self.gdf = GeoDataFrame(
            {
                'id': [1, 2],
                'my_geometry': [box(1, 1, 2, 2), box(3, 3, 4, 4)],
                'other_geometry': ['Point(0 0)', 'Point(1 1)']
            },
            geometry='my_geometry',
            crs='epsg:4326'
        )

    def test_constructor(self):
        cdf = CartoDataFrame(self.gdf)
        assert isinstance(cdf, CartoDataFrame)
        assert cdf._constructor == CartoDataFrame

    def test_crs_gdf(self):
        cdf = CartoDataFrame(self.gdf)
        assert cdf.crs == 'epsg:4326'

    def test_crs_custom(self):
        cdf = CartoDataFrame(self.gdf, crs='epsg:3857')
        assert cdf.crs == 'epsg:3857'

    def test_to_crs(self):
        cdf = CartoDataFrame(self.gdf)
        cdf = cdf.to_crs('epsg:3857')
        assert isinstance(cdf, CartoDataFrame)
        assert cdf.crs == 'epsg:3857'

    def test_geometry_gdf(self):
        cdf = CartoDataFrame(self.gdf)
        assert cdf.geometry.name == 'my_geometry'

    def test_geometry_custom(self):
        cdf = CartoDataFrame(self.gdf, geometry='other_geometry')
        assert cdf.geometry.name == 'other_geometry'

    def test_set_geometry(self):
        cdf = CartoDataFrame(self.gdf)
        cdf = cdf.set_geometry('other_geometry')
        assert isinstance(cdf, CartoDataFrame)
        assert cdf.geometry.name == 'other_geometry'

    def test_from_carto(self, mocker):
        mock = mocker.patch('cartoframes.io.carto.read_carto')
        CartoDataFrame.from_carto('table_name')
        mock.assert_called_once_with('table_name', None, None, 3, None, None, True)

    def test_from_file(self, mocker):
        mock = mocker.patch.object(GeoDataFrame, 'from_file')
        cdf = CartoDataFrame.from_file('file_name.geojson')
        mock.assert_called_once_with('file_name.geojson')
        assert isinstance(cdf, CartoDataFrame)

    def test_from_features(self, mocker):
        mock = mocker.patch.object(GeoDataFrame, 'from_features')
        cdf = CartoDataFrame.from_features('__features__')
        mock.assert_called_once_with('__features__')
        assert isinstance(cdf, CartoDataFrame)

    def test_to_carto(self, mocker):
        mock = mocker.patch('cartoframes.io.carto.to_carto')
        cdf = CartoDataFrame(self.gdf)
        cdf.to_carto('table_name')
        mock.assert_called_once_with(cdf, 'table_name', None, 'fail', None, False, None)

    def test_viz(self, mocker):
        mocker.patch('cartoframes.viz.Map', return_value='__map__')
        mock_layer = mocker.patch('cartoframes.viz.Layer')
        cdf = CartoDataFrame(self.gdf)
        viz = cdf.viz('__style__')
        mock_layer.assert_called_once_with(cdf, '__style__', None, None, None, None, None, None)
        assert viz == '__map__'

    def test_getitem_series(self):
        cdf = CartoDataFrame(self.gdf)
        cdf = cdf[cdf.id > 1]
        assert isinstance(cdf, CartoDataFrame)
        assert len(cdf) == 1

    def test_getitem_list(self):
        cdf = CartoDataFrame(self.gdf)
        cdf = cdf[['id']]
        assert isinstance(cdf, CartoDataFrame)
        assert len(cdf) == 2

    def test_getitem_slice(self):
        cdf = CartoDataFrame(self.gdf)
        cdf = cdf[slice(1)]
        assert isinstance(cdf, CartoDataFrame)
        assert len(cdf) == 1

    def test_astype(self):
        cdf = CartoDataFrame({'a': [1], 'geometry': [Point(0, 0)]})
        cdf = cdf.astype('object')
        assert isinstance(cdf, CartoDataFrame)

    def test_merge(self):
        cdf1 = CartoDataFrame({'lkey': ['foo', 'bar'], 'value': [1, 2]})
        cdf2 = CartoDataFrame({'rkey': ['foo', 'bar'], 'value': [5, 6]})
        cdf = cdf1.merge(cdf2, left_on='lkey', right_on='rkey')
        assert isinstance(cdf, CartoDataFrame)

    def test_dissolve(self):
        cdf = CartoDataFrame({'a': [1], 'geometry': [Point(0, 0)]})
        cdf = cdf.dissolve(by='a')
        assert isinstance(cdf, CartoDataFrame)

    def test_explode(self):
        cdf = CartoDataFrame({'a': [1], 'geometry': [Point(0, 0)]})
        cdf = cdf.explode()
        assert isinstance(cdf, CartoDataFrame)

    def test_concat(self):
        cdf1 = CartoDataFrame({'a': [1], 'geometry': [Point(0, 0)]})
        cdf2 = CartoDataFrame({'a': [2], 'geometry': [Point(1, 1)]})
        cdf = concat([cdf1, cdf2])
        assert isinstance(cdf, CartoDataFrame)

    def test_plot_series(self, mocker):
        series_plot_mock = mocker.patch.object(Series, 'plot')
        cdf = CartoDataFrame({'x': [1]})
        cdf['x'].plot()
        series_plot_mock.assert_called_once()

    def test_plot_geoseries(self, mocker):
        geoseries_plot_mock = mocker.patch.object(GeoSeries, 'plot')
        cdf = CartoDataFrame({'geometry': [Point(0, 0)]})
        cdf['geometry'].plot()
        geoseries_plot_mock.assert_called_once()

    def test_plot_dataframe(self, mocker):
        dataframe_plot_mock = mocker.patch.object(DataFrame, 'plot')
        cdf = CartoDataFrame({'x': [1], 'y': [1]})
        cdf[['x', 'y']].plot()
        dataframe_plot_mock.assert_called_once()

    def test_plot_geodataframe(self, mocker):
        geodataframe_plot_mock = mocker.patch.object(GeoDataFrame, 'plot')
        cdf = CartoDataFrame({'x': [1], 'geometry': [Point(0, 0)]})
        cdf[['x', 'geometry']].plot(column='x')
        geodataframe_plot_mock.assert_called_once()
<|endoftext|>


<commit_before>import numpy as np
import scipy as sp
from scipy import stats as sps
import scipy.optimize as op

import qp

class composite(object):

    def __init__(self, components, vb=True):
        """
        A probability distribution that is a linear combination of scipy.stats.rv_continuous objects

        Parameters
        ----------
        components: list or tuple, dicts
            aggregation of dicts defining component functions and their coefficients
        vb: boolean
            report on progress to stdout?
        """
        self.components = components
        self.n_components = len(self.components)
        self.component_range = range(self.n_components)

        coefficients = np.array([component['coefficient'] for component in self.components])
        self.coefficients = coefficients / np.sum(coefficients)
        self.functions = np.array([component['function'] for component in self.components])

    def pdf(self, xs):
        """
        Evaluates the composite PDF at locations

        Parameters
        ----------
        xs: float or numpy.ndarray, float
            value(s) at which to evaluate the PDF

        Returns
        -------
        ps: float or numpy.ndarray, float
            value(s) of the PDF at xs
        """
        p = np.zeros(np.shape(xs))
        for c in self.component_range:
            p += self.coefficients[c] * self.functions[c].pdf(xs)
        return p

    def cdf(self, xs):
        """
        Evaluates the composite CDF at locations

        Parameters
        ----------
        xs: float or numpy.ndarray, float
            value(s) at which to evaluate the CDF

        Returns
        -------
        ps: float or numpy.ndarray, float
            value(s) of the CDF at xs
        """
        ps = np.zeros(np.shape(xs))
        for c in self.component_range:
            ps += self.coefficients[c] * self.functions[c].cdf(xs)
        return ps

    def rvs(self, size):
        """
        Samples the composite probability distribution

        Parameters
        ----------
        size: int
            number of samples to take

        Returns
        -------
        xs: numpy.ndarray, float
            samples from the PDF
        """
        groups = [0]*self.n_components
        for item in range(size):
            groups[qp.utils.choice(self.component_range, self.coefficients)] += 1
        samples = [] * size
        for c in self.component_range:
            for n in range(groups[c]):
                samples.append(self.functions[c].rvs())
        return np.array(samples)

    def ppf(self, cdfs, vb=True):
        """
        Evaluates the composite PPF at locations

        Parameters
        ----------
        cdfs: float or numpy.ndarray, float
            value(s) at which to find quantiles

        Returns
        -------
        xs: float or numpy.ndarray, float
            quantiles
        """
        N = np.shape(cdfs)
        xs = np.zeros(N)

        all_cdfs = np.zeros(N)
        for c in self.component_range:
            all_cdfs += self.functions[c].ppf(cdfs)
        xs0 = all_cdfs / self.n_components

        for n in range(N[0]):
            def ppf_helper(x):
                return np.absolute(cdfs[n] - self.cdf(x))
            res = op.basinhopping(ppf_helper, xs0[n])#, method="Nelder-Mead", options={"maxfev": 1e5, "maxiter":1e5})
            xs[n] += res.x
            # if vb:
            #     print(res.message, res.success)
        return xs
<commit_msg>reverted to quicker root-finder for now<commit_after>import numpy as np
import scipy as sp
from scipy import stats as sps
import scipy.optimize as op

import qp

class composite(object):

    def __init__(self, components, vb=True):
        """
        A probability distribution that is a linear combination of scipy.stats.rv_continuous objects

        Parameters
        ----------
        components: list or tuple, dicts
            aggregation of dicts defining component functions and their coefficients
        vb: boolean
            report on progress to stdout?
        """
        self.components = components
        self.n_components = len(self.components)
        self.component_range = range(self.n_components)

        coefficients = np.array([component['coefficient'] for component in self.components])
        self.coefficients = coefficients / np.sum(coefficients)
        self.functions = np.array([component['function'] for component in self.components])

    def pdf(self, xs):
        """
        Evaluates the composite PDF at locations

        Parameters
        ----------
        xs: float or numpy.ndarray, float
            value(s) at which to evaluate the PDF

        Returns
        -------
        ps: float or numpy.ndarray, float
            value(s) of the PDF at xs
        """
        p = np.zeros(np.shape(xs))
        for c in self.component_range:
            p += self.coefficients[c] * self.functions[c].pdf(xs)
        return p

    def cdf(self, xs):
        """
        Evaluates the composite CDF at locations

        Parameters
        ----------
        xs: float or numpy.ndarray, float
            value(s) at which to evaluate the CDF

        Returns
        -------
        ps: float or numpy.ndarray, float
            value(s) of the CDF at xs
        """
        ps = np.zeros(np.shape(xs))
        for c in self.component_range:
            ps += self.coefficients[c] * self.functions[c].cdf(xs)
        return ps

    def rvs(self, size):
        """
        Samples the composite probability distribution

        Parameters
        ----------
        size: int
            number of samples to take

        Returns
        -------
        xs: numpy.ndarray, float
            samples from the PDF
        """
        groups = [0]*self.n_components
        for item in range(size):
            groups[qp.utils.choice(self.component_range, self.coefficients)] += 1
        samples = [] * size
        for c in self.component_range:
            for n in range(groups[c]):
                samples.append(self.functions[c].rvs())
        return np.array(samples)

    def ppf(self, cdfs, vb=True):
        """
        Evaluates the composite PPF at locations

        Parameters
        ----------
        cdfs: float or numpy.ndarray, float
            value(s) at which to find quantiles

        Returns
        -------
        xs: float or numpy.ndarray, float
            quantiles
        """
        N = np.shape(cdfs)
        xs = np.zeros(N)

        all_cdfs = np.zeros(N)
        for c in self.component_range:
            all_cdfs += self.functions[c].ppf(cdfs)
        xs0 = all_cdfs / self.n_components

        for n in range(N[0]):
            def ppf_helper(x):
                return np.absolute(cdfs[n] - self.cdf(x))
            res = op.minimize(ppf_helper, xs0[n], method="Nelder-Mead", options={"maxfev": 1e5, "maxiter":1e5})
            # res = op.basinhopping(ppf_helper, xs0[n])#, method="Nelder-Mead", options={"maxfev": 1e5, "maxiter":1e5})
            xs[n] += res.x
            # if vb:
            #     print(res.message, res.success)
        return xs
<|endoftext|>