<commit_before># Copyright 2017 The Sonnet Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or  implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================

"""This python module contains Neural Network Modules for TensorFlow.

Each module is a Python object which conceptually "owns" any
variables required in that part of the Neural Network. The `__call__` function
on the object is used to connect that Module into the Graph, and this may be
called repeatedly with sharing automatically taking place.

Everything public should be imported by this top level `__init__.py` so that the
library can be used as follows:

```
import sonnet as snt

linear = snt.Linear(...)
```
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import importlib
import sys

import semantic_version

from sonnet.python import custom_getters
from sonnet.python.modules import experimental
from sonnet.python.modules import nets
from sonnet.python.modules.attention import AttentiveRead
from sonnet.python.modules.base import AbstractModule
from sonnet.python.modules.base import Module
from sonnet.python.modules.base import observe_connections
from sonnet.python.modules.base import Transposable
from sonnet.python.modules.base_errors import DifferentGraphError
from sonnet.python.modules.base_errors import Error
from sonnet.python.modules.base_errors import IncompatibleShapeError
from sonnet.python.modules.base_errors import ModuleInfoError
from sonnet.python.modules.base_errors import NotConnectedError
from sonnet.python.modules.base_errors import NotInitializedError
from sonnet.python.modules.base_errors import NotSupportedError
from sonnet.python.modules.base_errors import ParentNotBuiltError
from sonnet.python.modules.base_errors import UnderspecifiedError
from sonnet.python.modules.base_info import SONNET_COLLECTION_NAME
from sonnet.python.modules.basic import AddBias
from sonnet.python.modules.basic import BatchApply
from sonnet.python.modules.basic import BatchFlatten
from sonnet.python.modules.basic import BatchReshape
from sonnet.python.modules.basic import FlattenTrailingDimensions
from sonnet.python.modules.basic import Linear
from sonnet.python.modules.basic import merge_leading_dims
from sonnet.python.modules.basic import MergeDims
from sonnet.python.modules.basic import SelectInput
from sonnet.python.modules.basic import SliceByDim
from sonnet.python.modules.basic import split_leading_dim
from sonnet.python.modules.basic import TileByDim
from sonnet.python.modules.basic import TrainableVariable
from sonnet.python.modules.basic_rnn import BidirectionalRNN
from sonnet.python.modules.basic_rnn import DeepRNN
from sonnet.python.modules.basic_rnn import ModelRNN
from sonnet.python.modules.basic_rnn import VanillaRNN
from sonnet.python.modules.batch_norm import BatchNorm
from sonnet.python.modules.batch_norm_v2 import BatchNormV2
from sonnet.python.modules.clip_gradient import clip_gradient
from sonnet.python.modules.conv import CausalConv1D
from sonnet.python.modules.conv import Conv1D
from sonnet.python.modules.conv import Conv1DTranspose
from sonnet.python.modules.conv import Conv2D
from sonnet.python.modules.conv import Conv2DTranspose
from sonnet.python.modules.conv import Conv3D
from sonnet.python.modules.conv import Conv3DTranspose
from sonnet.python.modules.conv import DepthwiseConv2D
from sonnet.python.modules.conv import InPlaneConv2D
from sonnet.python.modules.conv import SAME
from sonnet.python.modules.conv import SeparableConv1D
from sonnet.python.modules.conv import SeparableConv2D
from sonnet.python.modules.conv import VALID
from sonnet.python.modules.embed import Embed
from sonnet.python.modules.gated_rnn import BatchNormLSTM
from sonnet.python.modules.gated_rnn import Conv1DLSTM
from sonnet.python.modules.gated_rnn import Conv2DLSTM
from sonnet.python.modules.gated_rnn import GRU
from sonnet.python.modules.gated_rnn import highway_core_with_recurrent_dropout
from sonnet.python.modules.gated_rnn import HighwayCore
from sonnet.python.modules.gated_rnn import LSTM
from sonnet.python.modules.gated_rnn import lstm_with_recurrent_dropout
from sonnet.python.modules.gated_rnn import lstm_with_zoneout
from sonnet.python.modules.gated_rnn import LSTMBlockCell
from sonnet.python.modules.gated_rnn import LSTMState
from sonnet.python.modules.layer_norm import LayerNorm
from sonnet.python.modules.pondering_rnn import ACTCore
from sonnet.python.modules.relational_memory import RelationalMemory
from sonnet.python.modules.residual import Residual
from sonnet.python.modules.residual import ResidualCore
from sonnet.python.modules.residual import SkipConnectionCore
from sonnet.python.modules.rnn_core import RNNCellWrapper
from sonnet.python.modules.rnn_core import RNNCore
from sonnet.python.modules.rnn_core import trainable_initial_state
from sonnet.python.modules.rnn_core import TrainableInitialState
from sonnet.python.modules.rnn_core import wrap_rnn_cell_class
from sonnet.python.modules.scale_gradient import scale_gradient
from sonnet.python.modules.sequential import Sequential
from sonnet.python.modules.spatial_transformer import AffineGridWarper
from sonnet.python.modules.spatial_transformer import AffineWarpConstraints
from sonnet.python.modules.spatial_transformer import GridWarper
from sonnet.python.modules.util import check_initializers
from sonnet.python.modules.util import check_partitioners
from sonnet.python.modules.util import check_regularizers
from sonnet.python.modules.util import count_variables_by_type
from sonnet.python.modules.util import custom_getter_router
from sonnet.python.modules.util import format_variable_map
from sonnet.python.modules.util import format_variables
from sonnet.python.modules.util import get_normalized_variable_map
from sonnet.python.modules.util import get_saver
from sonnet.python.modules.util import get_variables_in_module
from sonnet.python.modules.util import get_variables_in_scope
from sonnet.python.modules.util import has_variable_scope
from sonnet.python.modules.util import log_variables
from sonnet.python.modules.util import reuse_variables
from sonnet.python.modules.util import summarize_variables
from sonnet.python.modules.util import variable_map_items
from sonnet.python.ops import nest
from sonnet.python.ops.initializers import restore_initializer

def _ensure_dependency_available_at_version(package_name, min_version):
  """Throw helpful error if required dependencies not available."""

  try:
    pkg = importlib.import_module(package_name)
  except ImportError:
    pip_name = package_name.replace('_', '-')
    raise SystemError(
        'Sonnet requires %s (minimum version %s) to be installed. '
        'If using pip, run `pip install %s` or '
        '`pip install %s-gpu`' % (
            package_name, min_version, pip_name, pip_name))

  installed_version = semantic_version.Version(pkg.__version__)
  version_spec = semantic_version.Spec('>=' + min_version)
  if not version_spec.match(installed_version):
    raise SystemError(
        '%s version %s is installed, but Sonnet requires at least version %s.' %
        (package_name, pkg.__version__, min_version))

_ensure_dependency_available_at_version('tensorflow', '1.8.0')
_ensure_dependency_available_at_version('tensorflow_probability', '0.4.0')

__version__ = '1.25'

<commit_msg>Check dependencies before importing rest of library<commit_after># Copyright 2017 The Sonnet Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or  implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================

"""This python module contains Neural Network Modules for TensorFlow.

Each module is a Python object which conceptually "owns" any
variables required in that part of the Neural Network. The `__call__` function
on the object is used to connect that Module into the Graph, and this may be
called repeatedly with sharing automatically taking place.

Everything public should be imported by this top level `__init__.py` so that the
library can be used as follows:

```
import sonnet as snt

linear = snt.Linear(...)
```
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import importlib
import sys

import semantic_version

def _ensure_dependency_available_at_version(package_name, min_version):
  """Throw helpful error if required dependencies not available."""

  try:
    pkg = importlib.import_module(package_name)
  except ImportError:
    pip_name = package_name.replace('_', '-')
    raise SystemError(
        'Sonnet requires %s (minimum version %s) to be installed. '
        'If using pip, run `pip install %s` or '
        '`pip install %s-gpu`' % (
            package_name, min_version, pip_name, pip_name))

  installed_version = semantic_version.Version(pkg.__version__)
  version_spec = semantic_version.Spec('>=' + min_version)
  if not version_spec.match(installed_version):
    raise SystemError(
        '%s version %s is installed, but Sonnet requires at least version %s.' %
        (package_name, pkg.__version__, min_version))

_ensure_dependency_available_at_version('tensorflow', '1.8.0')
_ensure_dependency_available_at_version('tensorflow_probability', '0.4.0')

# Check some version of TF is available.
from sonnet.python import custom_getters
from sonnet.python.modules import experimental
from sonnet.python.modules import nets
from sonnet.python.modules.attention import AttentiveRead
from sonnet.python.modules.base import AbstractModule
from sonnet.python.modules.base import Module
from sonnet.python.modules.base import observe_connections
from sonnet.python.modules.base import Transposable
from sonnet.python.modules.base_errors import DifferentGraphError
from sonnet.python.modules.base_errors import Error
from sonnet.python.modules.base_errors import IncompatibleShapeError
from sonnet.python.modules.base_errors import ModuleInfoError
from sonnet.python.modules.base_errors import NotConnectedError
from sonnet.python.modules.base_errors import NotInitializedError
from sonnet.python.modules.base_errors import NotSupportedError
from sonnet.python.modules.base_errors import ParentNotBuiltError
from sonnet.python.modules.base_errors import UnderspecifiedError
from sonnet.python.modules.base_info import SONNET_COLLECTION_NAME
from sonnet.python.modules.basic import AddBias
from sonnet.python.modules.basic import BatchApply
from sonnet.python.modules.basic import BatchFlatten
from sonnet.python.modules.basic import BatchReshape
from sonnet.python.modules.basic import FlattenTrailingDimensions
from sonnet.python.modules.basic import Linear
from sonnet.python.modules.basic import merge_leading_dims
from sonnet.python.modules.basic import MergeDims
from sonnet.python.modules.basic import SelectInput
from sonnet.python.modules.basic import SliceByDim
from sonnet.python.modules.basic import split_leading_dim
from sonnet.python.modules.basic import TileByDim
from sonnet.python.modules.basic import TrainableVariable
from sonnet.python.modules.basic_rnn import BidirectionalRNN
from sonnet.python.modules.basic_rnn import DeepRNN
from sonnet.python.modules.basic_rnn import ModelRNN
from sonnet.python.modules.basic_rnn import VanillaRNN
from sonnet.python.modules.batch_norm import BatchNorm
from sonnet.python.modules.batch_norm_v2 import BatchNormV2
from sonnet.python.modules.clip_gradient import clip_gradient
from sonnet.python.modules.conv import CausalConv1D
from sonnet.python.modules.conv import Conv1D
from sonnet.python.modules.conv import Conv1DTranspose
from sonnet.python.modules.conv import Conv2D
from sonnet.python.modules.conv import Conv2DTranspose
from sonnet.python.modules.conv import Conv3D
from sonnet.python.modules.conv import Conv3DTranspose
from sonnet.python.modules.conv import DepthwiseConv2D
from sonnet.python.modules.conv import InPlaneConv2D
from sonnet.python.modules.conv import SAME
from sonnet.python.modules.conv import SeparableConv1D
from sonnet.python.modules.conv import SeparableConv2D
from sonnet.python.modules.conv import VALID
from sonnet.python.modules.embed import Embed
from sonnet.python.modules.gated_rnn import BatchNormLSTM
from sonnet.python.modules.gated_rnn import Conv1DLSTM
from sonnet.python.modules.gated_rnn import Conv2DLSTM
from sonnet.python.modules.gated_rnn import GRU
from sonnet.python.modules.gated_rnn import highway_core_with_recurrent_dropout
from sonnet.python.modules.gated_rnn import HighwayCore
from sonnet.python.modules.gated_rnn import LSTM
from sonnet.python.modules.gated_rnn import lstm_with_recurrent_dropout
from sonnet.python.modules.gated_rnn import lstm_with_zoneout
from sonnet.python.modules.gated_rnn import LSTMBlockCell
from sonnet.python.modules.gated_rnn import LSTMState
from sonnet.python.modules.layer_norm import LayerNorm
from sonnet.python.modules.pondering_rnn import ACTCore
from sonnet.python.modules.relational_memory import RelationalMemory
from sonnet.python.modules.residual import Residual
from sonnet.python.modules.residual import ResidualCore
from sonnet.python.modules.residual import SkipConnectionCore
from sonnet.python.modules.rnn_core import RNNCellWrapper
from sonnet.python.modules.rnn_core import RNNCore
from sonnet.python.modules.rnn_core import trainable_initial_state
from sonnet.python.modules.rnn_core import TrainableInitialState
from sonnet.python.modules.rnn_core import wrap_rnn_cell_class
from sonnet.python.modules.scale_gradient import scale_gradient
from sonnet.python.modules.sequential import Sequential
from sonnet.python.modules.spatial_transformer import AffineGridWarper
from sonnet.python.modules.spatial_transformer import AffineWarpConstraints
from sonnet.python.modules.spatial_transformer import GridWarper
from sonnet.python.modules.util import check_initializers
from sonnet.python.modules.util import check_partitioners
from sonnet.python.modules.util import check_regularizers
from sonnet.python.modules.util import count_variables_by_type
from sonnet.python.modules.util import custom_getter_router
from sonnet.python.modules.util import format_variable_map
from sonnet.python.modules.util import format_variables
from sonnet.python.modules.util import get_normalized_variable_map
from sonnet.python.modules.util import get_saver
from sonnet.python.modules.util import get_variables_in_module
from sonnet.python.modules.util import get_variables_in_scope
from sonnet.python.modules.util import has_variable_scope
from sonnet.python.modules.util import log_variables
from sonnet.python.modules.util import reuse_variables
from sonnet.python.modules.util import summarize_variables
from sonnet.python.modules.util import variable_map_items
from sonnet.python.ops import nest
from sonnet.python.ops.initializers import restore_initializer

__version__ = '1.25'

<|endoftext|>


<commit_before>'''
Main file for this module.
Takes the gadget base as input and an output name.
Calculates the matter correlation function in a gadget output.
Downsamples the particles randomly by a certain fraction.
'''

import argparse
#import Corrfunc
from .readGadgetSnapshot import *

<commit_msg>Checking that my loading of snaps works as expected.<commit_after>'''
Main file for this module.
Takes the gadget base as input and an output name.
Calculates the matter correlation function in a gadget output.
Downsamples the particles randomly by a certain fraction.
'''

#TODO wrap this in a main()
import os
from argparse import ArgumentParser
from glob import glob
import numpy as np
#import Corrfunc
from .readGadgetSnapshot import *

desc = '''Main file for this module.
Takes the gadget base as input and an output name.
Calculates the matter correlation function in a gadget output.
Downsamples the particles randomly by a certain fraction.'''

parser = ArgumentParser(description=desc)

parser.add_argument('inbase', type=str, help= \
    'Path and file base for the gadget files.')
# May want to rename to tmp
parser.add_argument('outputfile', type=str, help= \
    'Filename to write the final correlation function.')

#TODO find a good value for p
p0 = 1e-3
parser.add_argument('-p', '--sample_frac', default = p0,type = float, help='Fraction to sample. Default is %d.'%p0)

args = vars(parser.parse_args())

inbase = args['inbase']
outputfile= args['outputfile']
p = args['sample_frac']

assert os.path.exists(inbase+ '.0')#check that the path is valid
assert os.path.exists(outputfile) or os.access(os.path.dirname(outputfile), os.W_OK) #check if it exists or we have access
assert 0 < p < 1 #p must be a valid fraction!

all_pos = np.array([])

for file in glob(inbase+'*'):
    pos = readGadgetSnapshot(file, read_pos=True)[1] #Think this returns some type of tuple; should check
    pos = pos[np.random.rand(pos.shape[0]) < p] #downsample
    all_pos = np.resize(all_pos, (all_pos.shape[0]+pos.shape[0], 3))
    all_pos[-pos.shape[0]:, :] = pos
    break

print all_pos

#all pos have been collected. now run corrFunc.<|endoftext|>


<commit_before>#!/usr/bin/python

# progress bar for various corpling_tools
# from http://nbviewer.ipython.org/github/ipython/ipython/blob/3607712653c66d63e0d7f13f073bde8c0f209ba8/docs/examples/notebooks/Animations_and_Progress.ipynb
# there is dirname as a second arg, which will display the subcorpus in the progress bar
# daniel mcdonald

class TextProgressBar:
    from time import localtime, strftime
    try:
        from IPython.display import display, clear_output
        have_ipython = True
    except ImportError:
        have_ipython = False
    def __init__(self, iterations):
        from time import localtime, strftime

        try:
            from IPython.display import display, clear_output
            have_ipython = True
        except ImportError:
            have_ipython = False

        self.iterations = iterations
        self.prog_bar = '[]'
        self.fill_char = '*'
        self.width = 60
        self.__update_amount(0)
        self.animate = self.animate_ipython

    def animate_ipython(self, iter, dirname = None):
        try:
            from IPython.display import display, clear_output
            have_ipython = True
        except ImportError:
            have_ipython = False
        from time import localtime, strftime
        import sys
        print '\r', self,
        if have_ipython:
            try:
                sys.stdout.flush()
            except:
                pass
        self.update_iteration(iter + 1, dirname)

    def update_iteration(self, elapsed_iter, dirname = None):
        from time import localtime, strftime
        self.__update_amount((elapsed_iter / float(self.iterations)) * 100.0, dirname)
        self.prog_bar += ' ' # + ' %d of %s complete' % (elapsed_iter, self.iterations)

    def __update_amount(self, new_amount, dirname = None):
        from time import localtime, strftime
        percent_done = int(round((new_amount / 100.0) * 100.0, 2))
        all_full = self.width - 2
        num_hashes = int(round((percent_done / 100.0) * all_full))
        time = strftime("%H:%M:%S", localtime())
        self.prog_bar = time + ': [' + self.fill_char * num_hashes + ' ' * (all_full - num_hashes) + ']'
        pct_place = (len(self.prog_bar) // 2) + (3)
        if dirname:
            pct_string = '%d%% ' % percent_done + '(' + dirname + ')'
        else:
            pct_string = '%d%%' % percent_done # could pass dirname here!
        self.prog_bar = self.prog_bar[0:pct_place] + \
           (pct_string + self.prog_bar[pct_place + len(pct_string):])

    def __str__(self):
        return str(self.prog_bar)<commit_msg>print corpus name<commit_after>#!/usr/bin/python

# progress bar for various corpling_tools
# from http://nbviewer.ipython.org/github/ipython/ipython/blob/3607712653c66d63e0d7f13f073bde8c0f209ba8/docs/examples/notebooks/Animations_and_Progress.ipynb
# there is dirname as a second arg, which will display the subcorpus in the progress bar
# daniel mcdonald

class TextProgressBar:
    from time import localtime, strftime
    try:
        from IPython.display import display, clear_output
        have_ipython = True
    except ImportError:
        have_ipython = False
    def __init__(self, iterations, dirname = False):
        from time import localtime, strftime
        try:
            from IPython.display import display, clear_output
            have_ipython = True
        except ImportError:
            have_ipython = False

        self.iterations = iterations
        self.prog_bar = '[]'
        self.fill_char = '*'
        self.width = 60
        self.__update_amount(0, dirname = dirname)
        self.animate = self.animate_ipython

    def animate_ipython(self, iter, dirname = None):
        try:
            from IPython.display import display, clear_output
            have_ipython = True
        except ImportError:
            have_ipython = False
        from time import localtime, strftime
        import sys
        print '\r', self,
        if have_ipython:
            try:
                sys.stdout.flush()
            except:
                pass
        self.update_iteration(iter + 1, dirname)

    def update_iteration(self, elapsed_iter, dirname = None):
        from time import localtime, strftime
        self.__update_amount((elapsed_iter / float(self.iterations)) * 100.0, dirname)
        self.prog_bar += ' ' # + ' %d of %s complete' % (elapsed_iter, self.iterations)

    def __update_amount(self, new_amount, dirname = None):
        from time import localtime, strftime
        percent_done = int(round((new_amount / 100.0) * 100.0, 2))
        all_full = self.width - 2
        num_hashes = int(round((percent_done / 100.0) * all_full))
        time = strftime("%H:%M:%S", localtime())
        self.prog_bar = time + ': [' + self.fill_char * num_hashes + ' ' * (all_full - num_hashes) + ']'
        pct_place = (len(self.prog_bar) // 2) + (3)
        if dirname:
            pct_string = '%d%% ' % percent_done + '(' + dirname + ')'
        else:
            pct_string = '%d%%' % percent_done # could pass dirname here!
        self.prog_bar = self.prog_bar[0:pct_place] + \
           (pct_string + self.prog_bar[pct_place + len(pct_string):])

    def __str__(self):
        return str(self.prog_bar)<|endoftext|>


<commit_before>import os
import glob
import subprocess

try:
    from unittest.mock import patch
except ImportError:
    # py2
    from mock import patch

from notebook import jstest

import platform

IS_WIN = "Windows" in platform.system()

here = os.path.dirname(__file__)

# global npm installs are bad, add the local node_modules to the path
os.environ["PATH"] = os.pathsep.join([
    os.environ["PATH"],
    os.path.abspath(os.path.join(here, "node_modules", ".bin"))
])

TEST_LOG = ".jupyter.jstest.log"

class NBPresentTestController(jstest.JSController):
    """ Javascript test subclass that installs widget nbextension in test
        environment
    """
    def __init__(self, section, *args, **kwargs):
        extra_args = kwargs.pop('extra_args', None)
        super(NBPresentTestController, self).__init__(section, *args, **kwargs)
        self.xunit = True

        test_cases = glob.glob(os.path.join(here, 'js', 'test_notebook_*.js'))
        js_test_dir = jstest.get_js_test_dir()

        includes = [
            os.path.join(js_test_dir, 'util.js')
        ] + glob.glob(os.path.join(here, 'js', '_*.js'))

        self.cmd = [
            'casperjs', 'test',
            '--includes={}'.format(",".join(includes)),
            '--engine={}'.format(self.engine)
        ] + test_cases

        if extra_args is not None:
            self.cmd = self.cmd + extra_args

        if IS_WIN:
            self.cmd[0] = "{}.cmd".format(self.cmd[0])

    def launch(self, buffer_output=False, capture_output=False):
        # print('*** ENV:', self.env)  # dbg
        # print('*** CMD:', self.cmd)  # dbg
        env = os.environ.copy()
        env.update(self.env)
        if buffer_output:
            capture_output = True
        self.stdout_capturer = c = jstest.StreamCapturer(
            echo=not buffer_output)
        c.start()
        stdout = c.writefd if capture_output else None
        # stderr = subprocess.STDOUT if capture_output else None
        self.process = subprocess.Popen(
            self.cmd,
            stderr=subprocess.PIPE,
            stdout=stdout,
            env=env)

    def wait(self):
        self.process.communicate()
        self.stdout_capturer.halt()
        self.stdout = self.stdout_capturer.get_buffer()
        return self.process.returncode

    # copy pasta from...
    # https://github.com/jupyter/notebook/blob/master/notebook/jstest.py#L234
    def setup(self):
        self.ipydir = jstest.TemporaryDirectory()
        self.config_dir = jstest.TemporaryDirectory()
        self.nbdir = jstest.TemporaryDirectory()
        self.home = jstest.TemporaryDirectory()
        self.env = {
            'HOME': self.home.name,
            'JUPYTER_CONFIG_DIR': self.config_dir.name,
            'IPYTHONDIR': self.ipydir.name,
        }
        self.dirs.append(self.ipydir)
        self.dirs.append(self.home)
        self.dirs.append(self.config_dir)
        self.dirs.append(self.nbdir)
        os.makedirs(os.path.join(self.nbdir.name,
                                 os.path.join(u'sub dir1', u'sub dir 1a')))
        os.makedirs(os.path.join(self.nbdir.name,
                                 os.path.join(u'sub dir2', u'sub dir 1b')))

        if self.xunit:
            self.add_xunit()

        # If a url was specified, use that for the testing.
        if self.url:
            try:
                alive = jstest.requests.get(self.url).status_code == 200
            except:
                alive = False

            if alive:
                self.cmd.append("--url=%s" % self.url)
            else:
                raise Exception('Could not reach "%s".' % self.url)
        else:
            # start the ipython notebook, so we get the port number
            self.server_port = 0
            self._init_server()
            if self.server_port:
                self.cmd.append('--url=http://localhost:%i%s' % (
                    self.server_port, self.base_url))
            else:
                # don't launch tests if the server didn't start
                self.cmd = [
                    jstest.sys.executable, '-c', 'raise SystemExit(1)']

    def add_xunit(self):
        """ Hack the setup in the middle (after paths, before server)
        """
        super(NBPresentTestController, self).add_xunit()

        # commands to run to enable the system-of-interest
        cmds = [
            ["nbextension", "install"],
            ["nbextension", "enable"],
            ["serverextension", "enable"]
        ]

        # ensure the system-of-interest is installed and enabled!
        with patch.dict(os.environ, self.env):
            args = ["--py", "nbpresent"]
            prefix = (["--sys-prefix"] if ("CONDA_ENV_PATH" in os.environ) or
                      ("CONDA_DEFAULT_ENV" in os.environ) else ["--user"])

            for cmd in cmds:
                final_cmd = ["jupyter"] + cmd + prefix + args
                proc = subprocess.Popen(final_cmd,
                                        stdout=subprocess.PIPE,
                                        env=os.environ)
                out, err = proc.communicate()
                if proc.returncode:
                    raise Exception([proc.returncode, final_cmd, out, err])

    def cleanup(self):
        if hasattr(self, "stream_capturer"):
            captured = self.stream_capturer.get_buffer().decode(
                'utf-8', 'replace')
            with open(TEST_LOG, "a+") as fp:
                fp.write("-----------------------\n{} results:\n{}\n".format(
                    self.section,
                    self.server_command))
                fp.write(captured)

        super(NBPresentTestController, self).cleanup()

def prepare_controllers(options):
    """Monkeypatched prepare_controllers for running widget js tests

    instead of notebook js tests
    """
    if options.testgroups:
        groups = options.testgroups
    else:
        groups = ['']
    return [NBPresentTestController(g, extra_args=options.extra_args)
            for g in groups], []

def test_notebook():
    with patch.object(jstest, 'prepare_controllers', prepare_controllers):
        jstest.main()

if __name__ == '__main__':
    test_notebook()
<commit_msg>unicode<commit_after>import codecs
import glob
import os
import subprocess

try:
    from unittest.mock import patch
except ImportError:
    # py2
    from mock import patch

from notebook import jstest

import platform

IS_WIN = "Windows" in platform.system()

here = os.path.dirname(__file__)

# global npm installs are bad, add the local node_modules to the path
os.environ["PATH"] = os.pathsep.join([
    os.environ["PATH"],
    os.path.abspath(os.path.join(here, "node_modules", ".bin"))
])

TEST_LOG = ".jupyter.jstest.log"

class NBPresentTestController(jstest.JSController):
    """ Javascript test subclass that installs widget nbextension in test
        environment
    """
    def __init__(self, section, *args, **kwargs):
        extra_args = kwargs.pop('extra_args', None)
        super(NBPresentTestController, self).__init__(section, *args, **kwargs)
        self.xunit = True

        test_cases = glob.glob(os.path.join(here, 'js', 'test_notebook_*.js'))
        js_test_dir = jstest.get_js_test_dir()

        includes = [
            os.path.join(js_test_dir, 'util.js')
        ] + glob.glob(os.path.join(here, 'js', '_*.js'))

        self.cmd = [
            'casperjs', 'test',
            '--includes={}'.format(",".join(includes)),
            '--engine={}'.format(self.engine)
        ] + test_cases

        if extra_args is not None:
            self.cmd = self.cmd + extra_args

        if IS_WIN:
            self.cmd[0] = "{}.cmd".format(self.cmd[0])

    def launch(self, buffer_output=False, capture_output=False):
        # print('*** ENV:', self.env)  # dbg
        # print('*** CMD:', self.cmd)  # dbg
        env = os.environ.copy()
        env.update(self.env)
        if buffer_output:
            capture_output = True
        self.stdout_capturer = c = jstest.StreamCapturer(
            echo=not buffer_output)
        c.start()
        stdout = c.writefd if capture_output else None
        # stderr = subprocess.STDOUT if capture_output else None
        self.process = subprocess.Popen(
            self.cmd,
            stderr=subprocess.PIPE,
            stdout=stdout,
            env=env)

    def wait(self):
        self.process.communicate()
        self.stdout_capturer.halt()
        self.stdout = self.stdout_capturer.get_buffer()
        return self.process.returncode

    # copy pasta from...
    # https://github.com/jupyter/notebook/blob/master/notebook/jstest.py#L234
    def setup(self):
        self.ipydir = jstest.TemporaryDirectory()
        self.config_dir = jstest.TemporaryDirectory()
        self.nbdir = jstest.TemporaryDirectory()
        self.home = jstest.TemporaryDirectory()
        self.env = {
            'HOME': self.home.name,
            'JUPYTER_CONFIG_DIR': self.config_dir.name,
            'IPYTHONDIR': self.ipydir.name,
        }
        self.dirs.append(self.ipydir)
        self.dirs.append(self.home)
        self.dirs.append(self.config_dir)
        self.dirs.append(self.nbdir)
        os.makedirs(os.path.join(self.nbdir.name,
                                 os.path.join(u'sub dir1', u'sub dir 1a')))
        os.makedirs(os.path.join(self.nbdir.name,
                                 os.path.join(u'sub dir2', u'sub dir 1b')))

        if self.xunit:
            self.add_xunit()

        # If a url was specified, use that for the testing.
        if self.url:
            try:
                alive = jstest.requests.get(self.url).status_code == 200
            except:
                alive = False

            if alive:
                self.cmd.append("--url=%s" % self.url)
            else:
                raise Exception('Could not reach "%s".' % self.url)
        else:
            # start the ipython notebook, so we get the port number
            self.server_port = 0
            self._init_server()
            if self.server_port:
                self.cmd.append('--url=http://localhost:%i%s' % (
                    self.server_port, self.base_url))
            else:
                # don't launch tests if the server didn't start
                self.cmd = [
                    jstest.sys.executable, '-c', 'raise SystemExit(1)']

    def add_xunit(self):
        """ Hack the setup in the middle (after paths, before server)
        """
        super(NBPresentTestController, self).add_xunit()

        # commands to run to enable the system-of-interest
        cmds = [
            ["nbextension", "install"],
            ["nbextension", "enable"],
            ["serverextension", "enable"]
        ]

        # ensure the system-of-interest is installed and enabled!
        with patch.dict(os.environ, self.env):
            args = ["--py", "nbpresent"]
            prefix = (["--sys-prefix"] if ("CONDA_ENV_PATH" in os.environ) or
                      ("CONDA_DEFAULT_ENV" in os.environ) else ["--user"])

            for cmd in cmds:
                final_cmd = ["jupyter"] + cmd + prefix + args
                proc = subprocess.Popen(final_cmd,
                                        stdout=subprocess.PIPE,
                                        env=os.environ)
                out, err = proc.communicate()
                if proc.returncode:
                    raise Exception([proc.returncode, final_cmd, out, err])

    def cleanup(self):
        if hasattr(self, "stream_capturer"):
            captured = self.stream_capturer.get_buffer().decode(
                'utf-8', 'replace')
            with codecs.open(TEST_LOG, "a+", encoding="utf-8") as fp:
                fp.write("-----------------------\n{} results:\n{}\n".format(
                    self.section,
                    self.server_command))
                fp.write(captured)
                print("JUPYTER JSTESTLOG", captured)

        super(NBPresentTestController, self).cleanup()

def prepare_controllers(options):
    """Monkeypatched prepare_controllers for running widget js tests

    instead of notebook js tests
    """
    if options.testgroups:
        groups = options.testgroups
    else:
        groups = ['']
    return [NBPresentTestController(g, extra_args=options.extra_args)
            for g in groups], []

def test_notebook():
    with patch.object(jstest, 'prepare_controllers', prepare_controllers):
        jstest.main()

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


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2009 Maximillian Dornseif <md@hudora.de>
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.

"""
This script replicates databases from one CouchDB server to an other.

This is mainly for backup purposes or "priming" a new server before
setting up trigger based replication. But you can also use the
'--continuous' option to set up automatic replication on newer
CouchDB versions.

Use 'python replicate.py --help' to get more detailed usage instructions.
"""

from couchdb import http, client
import optparse
import sys
import time
import urlparse
import re

def findpath(parser, s):
    '''returns (server url, path component)'''

    if s == '.':
        return client.DEFAULT_BASE_URL, ''
    if not s.startswith('http'):
        return client.DEFAULT_BASE_URL, s

    bits = urlparse.urlparse(s)
    res = http.Resource('%s://%s/' % (bits.scheme, bits.netloc), None)
    parts = bits.path.split('/')[1:]
    if not parts[-1]:
        parts = parts[:-1]

    cut = None
    for i in range(0, len(parts) + 1):
        try:
            data = res.get_json(parts[:i])[2]
        except Exception:
            data = None
        if data and 'couchdb' in data:
            cut = i

    if cut is None:
        raise parser.error("'%s' does not appear to be a CouchDB" % s)

    base = res.url + ('/'.join(parts[:cut]) if parts[:cut] else '')
    return base, '/'.join(parts[cut:])

def main():

    usage = '%prog [options] <source> <target>'
    parser = optparse.OptionParser(usage=usage)
    parser.add_option('--continuous',
        action='store_true',
        dest='continuous',
        help='trigger continuous replication in cochdb')
    parser.add_option('--compact',
        action='store_true',
        dest='compact',
        help='compact target database after replication')

    options, args = parser.parse_args()
    if len(args) != 2:
        raise parser.error('need source and target arguments')

    # set up server objects

    src, tgt = args
    sbase, spath = findpath(parser, src)
    source = client.Server(sbase)
    tbase, tpath = findpath(parser, tgt)
    target = client.Server(tbase)

    # check database name specs

    if '*' in tpath:
        raise parser.error('invalid target path: must be single db or empty')
    elif '*' in spath and tpath:
        raise parser.error('target path must be empty with multiple sources')

    all = sorted(i for i in source)
    if not spath:
        raise parser.error('source database must be specified')
    elif spath in all:
        databases = [(spath, tpath if tpath else spath)]
    elif '*' in spath:
        check = re.compile(spath.replace('*', '.*?'))
        databases = [(i, i) for i in all if check.match(i)]
    else:
        databases = []

    if not databases:
        raise parser.error("no source databases match glob '%s'" % spath)

    # do the actual replication

    for sdb, tdb in databases:

        start = time.time()
        print sdb, '->', tdb,
        sys.stdout.flush()

        if tdb not in target:
            target.create(tdb)
            print "created",
            sys.stdout.flush()

        sdb = '%s%s' % (sbase, sdb)
        if options.continuous:
            target.replicate(sdb, tdb, continuous=options.continuous)
        else:
            target.replicate(sdb, tdb)
        print '%.1fs' % (time.time() - start)

    if not options.compact:
        return

    sys.stdout.flush()
    for dbname in dbnames:
        target_server[dbname].compact()

if __name__ == '__main__':
    main()
<commit_msg>Move the flush to after the print it's flushing.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2009 Maximillian Dornseif <md@hudora.de>
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.

"""
This script replicates databases from one CouchDB server to an other.

This is mainly for backup purposes or "priming" a new server before
setting up trigger based replication. But you can also use the
'--continuous' option to set up automatic replication on newer
CouchDB versions.

Use 'python replicate.py --help' to get more detailed usage instructions.
"""

from couchdb import http, client
import optparse
import sys
import time
import urlparse
import re

def findpath(parser, s):
    '''returns (server url, path component)'''

    if s == '.':
        return client.DEFAULT_BASE_URL, ''
    if not s.startswith('http'):
        return client.DEFAULT_BASE_URL, s

    bits = urlparse.urlparse(s)
    res = http.Resource('%s://%s/' % (bits.scheme, bits.netloc), None)
    parts = bits.path.split('/')[1:]
    if not parts[-1]:
        parts = parts[:-1]

    cut = None
    for i in range(0, len(parts) + 1):
        try:
            data = res.get_json(parts[:i])[2]
        except Exception:
            data = None
        if data and 'couchdb' in data:
            cut = i

    if cut is None:
        raise parser.error("'%s' does not appear to be a CouchDB" % s)

    base = res.url + ('/'.join(parts[:cut]) if parts[:cut] else '')
    return base, '/'.join(parts[cut:])

def main():

    usage = '%prog [options] <source> <target>'
    parser = optparse.OptionParser(usage=usage)
    parser.add_option('--continuous',
        action='store_true',
        dest='continuous',
        help='trigger continuous replication in cochdb')
    parser.add_option('--compact',
        action='store_true',
        dest='compact',
        help='compact target database after replication')

    options, args = parser.parse_args()
    if len(args) != 2:
        raise parser.error('need source and target arguments')

    # set up server objects

    src, tgt = args
    sbase, spath = findpath(parser, src)
    source = client.Server(sbase)
    tbase, tpath = findpath(parser, tgt)
    target = client.Server(tbase)

    # check database name specs

    if '*' in tpath:
        raise parser.error('invalid target path: must be single db or empty')
    elif '*' in spath and tpath:
        raise parser.error('target path must be empty with multiple sources')

    all = sorted(i for i in source)
    if not spath:
        raise parser.error('source database must be specified')
    elif spath in all:
        databases = [(spath, tpath if tpath else spath)]
    elif '*' in spath:
        check = re.compile(spath.replace('*', '.*?'))
        databases = [(i, i) for i in all if check.match(i)]
    else:
        databases = []

    if not databases:
        raise parser.error("no source databases match glob '%s'" % spath)

    # do the actual replication

    for sdb, tdb in databases:

        start = time.time()
        print sdb, '->', tdb,
        sys.stdout.flush()

        if tdb not in target:
            target.create(tdb)
            print "created",
            sys.stdout.flush()

        sdb = '%s%s' % (sbase, sdb)
        if options.continuous:
            target.replicate(sdb, tdb, continuous=options.continuous)
        else:
            target.replicate(sdb, tdb)
        print '%.1fs' % (time.time() - start)
        sys.stdout.flush()

    if not options.compact:
        return

    for dbname in dbnames:
        target_server[dbname].compact()

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


<commit_before>from pub import task
from pub.shortcuts import python, pip, rm, nosetests

@task
def clean():
    """clean up the crap left behind by setup.py build"""
    rm("-rf build dist pub.egg-info docs pubfilec")

@task
def build():
    """build pub"""
    python("setup.py build")

@task
def install():
    """install pub"""
    python("setup.py install")

@task
def reinstall():
    """uninstall and reinstall pub; probably only useful for developers"""
    pip("uninstall -y pub")
    install()

@task
def publish():
    python("setup.py sdist upload")

@task
def test():
    nosetests("-s")
<commit_msg>comments on pub.py<commit_after>from pub import task
from pub.shortcuts import python, pip, rm, nosetests

@task
def clean():
    """clean up the crap left behind by setup.py build"""
    rm("-rf build dist pub.egg-info docs pubfilec")

@task
def build():
    """build pub"""
    python("setup.py build")

@task
def install():
    """install pub"""
    python("setup.py install")

@task
def reinstall():
    """uninstall and reinstall pub; probably only useful for developers"""
    pip("uninstall -y pub")
    install()

@task
def publish():
    """upload pub to pyPI"""
    python("setup.py sdist upload")

@task
def test():
    """run pub's tests"""
    nosetests("-s")
<|endoftext|>


<commit_before># Copyright (C) Frederick Dean 2009, All rights reserved

"""
Unit tests for L{OpenSSL.rand}.
"""

from unittest import main
import os
import stat

from OpenSSL.test.util import TestCase
from OpenSSL import rand

class RandTests(TestCase):
    def test_bytes(self):
        """
        Verify that we can obtain bytes from rand_bytes() and
        that they are different each time.  Test the parameter
        of rand_bytes() for bad values.
        """
        b1 = rand.bytes(50)
        self.assertEqual(len(b1), 50)
        b2 = rand.bytes(num_bytes=50)  # parameter by name
        self.assertNotEqual(b1, b2)  #  Hip, Hip, Horay! FIPS complaince
        b3 = rand.bytes(num_bytes=0)
        self.assertEqual(len(b3), 0)
        exc = self.assertRaises(ValueError, rand.bytes, -1)
        self.assertEqual(exc.message, "num_bytes must not be negative")

    def test_add(self):
        """
        L{OpenSSL.rand.add} adds entropy to the PRNG.
        """
        rand.add('hamburger', 3)

    def test_seed(self):
        """
        L{OpenSSL.rand.seed} adds entropy to the PRNG.
        """
        rand.seed('milk shake')

    def test_status(self):
        """
        L{OpenSSL.rand.status} returns C{True} if the PRNG has sufficient
        entropy, C{False} otherwise.
        """
        # It's hard to know what it is actually going to return.  Different
        # OpenSSL random engines decide differently whether they have enough
        # entropy or not.
        self.assertTrue(rand.status() in (1, 2))

    def test_files(self):
        """
        Test reading and writing of files via rand functions.
        """
        # Write random bytes to a file
        tmpfile = self.mktemp()
        # Make sure it exists (so cleanup definitely succeeds)
        fObj = file(tmpfile, 'w')
        fObj.close()
        try:
            rand.write_file(tmpfile)
            # Verify length of written file
            size = os.stat(tmpfile)[stat.ST_SIZE]
            self.assertEquals(size, 1024)
            # Read random bytes from file
            rand.load_file(tmpfile)
            rand.load_file(tmpfile, 4)  # specify a length
        finally:
            # Cleanup
            os.unlink(tmpfile)

if __name__ == '__main__':
    main()
<commit_msg>Compatibility with Python < 2.5 - ValueError in older versions do not have a message attribute<commit_after># Copyright (C) Frederick Dean 2009, All rights reserved

"""
Unit tests for L{OpenSSL.rand}.
"""

from unittest import main
import os
import stat

from OpenSSL.test.util import TestCase
from OpenSSL import rand

class RandTests(TestCase):
    def test_bytes(self):
        """
        Verify that we can obtain bytes from rand_bytes() and
        that they are different each time.  Test the parameter
        of rand_bytes() for bad values.
        """
        b1 = rand.bytes(50)
        self.assertEqual(len(b1), 50)
        b2 = rand.bytes(num_bytes=50)  # parameter by name
        self.assertNotEqual(b1, b2)  #  Hip, Hip, Horay! FIPS complaince
        b3 = rand.bytes(num_bytes=0)
        self.assertEqual(len(b3), 0)
        exc = self.assertRaises(ValueError, rand.bytes, -1)
        self.assertEqual(str(exc), "num_bytes must not be negative")

    def test_add(self):
        """
        L{OpenSSL.rand.add} adds entropy to the PRNG.
        """
        rand.add('hamburger', 3)

    def test_seed(self):
        """
        L{OpenSSL.rand.seed} adds entropy to the PRNG.
        """
        rand.seed('milk shake')

    def test_status(self):
        """
        L{OpenSSL.rand.status} returns C{True} if the PRNG has sufficient
        entropy, C{False} otherwise.
        """
        # It's hard to know what it is actually going to return.  Different
        # OpenSSL random engines decide differently whether they have enough
        # entropy or not.
        self.assertTrue(rand.status() in (1, 2))

    def test_files(self):
        """
        Test reading and writing of files via rand functions.
        """
        # Write random bytes to a file
        tmpfile = self.mktemp()
        # Make sure it exists (so cleanup definitely succeeds)
        fObj = file(tmpfile, 'w')
        fObj.close()
        try:
            rand.write_file(tmpfile)
            # Verify length of written file
            size = os.stat(tmpfile)[stat.ST_SIZE]
            self.assertEquals(size, 1024)
            # Read random bytes from file
            rand.load_file(tmpfile)
            rand.load_file(tmpfile, 4)  # specify a length
        finally:
            # Cleanup
            os.unlink(tmpfile)

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


<commit_before>import os
import unittest
from contextlib import contextmanager

from IPython.nbformat import current
from IPython.html.services.contents.filemanager import FileContentsManager
from IPython.utils.tempdir import TemporaryDirectory

from ..tests import tools as nt
from ..gist import model_to_files, GistService, Gister
from nbx.nbmanager.tests.common import login, password, require_github, makeFakeGist

class TestGist(unittest.TestCase):

    def __init__(self, *args, **kwargs):
        unittest.TestCase.__init__(self, *args, **kwargs)

    def runTest(self):
        pass

    def setUp(self):
        pass

    def test_model_to_files(self):
        with TemporaryDirectory() as td:
            fm = FileContentsManager(root_dir=td)
            model = fm.create_file()
            # need content
            model = fm.get_model(model['name'], model['path'])
            files = model_to_files(model)
            name = model['name']
            # files should only contain one file
            nt.assert_items_equal(files, [name])

            # add a file
            model['__files'] = {'file1.txt': 'file1txt content'}
            files = model_to_files(model)
            nt.assert_items_equal(files, [name, 'file1.txt'])
            nt.assert_equal(files['file1.txt'],
                            'file1txt content')

@contextmanager
def create_gist_context(*args, **kwargs):
    """
    contextmanager that creates a gist and makes sure to delete it
    """
    delete_after = kwargs.pop('delete_after', True)
    gs = GistService()
    gs.login(login, password)
    gist = gs.create_gist(*args, **kwargs)
    yield gist
    if delete_after:
        gist.delete()

class TestGistService(unittest.TestCase):

    def __init__(self, *args, **kwargs):
        unittest.TestCase.__init__(self, *args, **kwargs)

    def runTest(self):
        pass

    def setUp(self):
        pass

    @require_github
    def test_get_gist(self):
        gs = GistService()
        gs.login(login, password)
        gist_id = '6705707'
        gist = gs.get_gist(gist_id)
        nt.assert_equal(gist.owner.login, 'dalejung')

    @require_github
    def test_create_gist(self):
        with create_gist_context() as gist:
            nt.assert_equal(gist.public, True)

        with create_gist_context(public=False) as gist:
            nt.assert_equal(gist.public, False)

        with create_gist_context(public=False, description="nbx test") as gist:
            nt.assert_equal(gist.public, False)
            nt.assert_equal(gist.description, "nbx test")
            nt.assert_equal(gist.owner.login, login)

        files = {'bob2.txt': 'bob2.txt content'}
        with create_gist_context(public=False, files=files) as gist:
            nt.assert_items_equal(gist.files, ['bob2.txt'])

    @require_github
    def test_edit_gist(self):
        with create_gist_context() as gist:
            updated_at = gist.gist.updated_at
            old_desc = gist.description
            # the following should not change the gist
            gist.edit()
            nt.assert_equal(gist.gist.updated_at, updated_at)
            gist.edit(old_desc)
            nt.assert_equal(gist.gist.updated_at, updated_at)
            gist.edit(old_desc, files={'empty.txt':'empty file created by nbx'})
            nt.assert_equal(gist.gist.updated_at, updated_at)

            # change desc
            gist.edit('new desc', files={'empty.txt':'empty file created by nbx'})
            nt.assert_equals(gist.description, 'new desc')
            nt.assert_equals(len(gist.gist.history), 2)

            # add file
            gist.edit('new desc', files={'new.txt':'new stuff'})
            # TODO: Need to deepdive and see why the assert fails. #15
            nt.assert_items_equal(gist.files, ['empty.txt', 'new.txt'])
            nt.assert_equals(len(gist.gist.history), 3)

            # don't modify the new txt. should be no change
            gist.edit('new desc', files={'new.txt':'new stuff'})
            nt.assert_equals(len(gist.gist.history), 3)

            # force a non change commit
            gist.edit('new desc', files={'new.txt':'new stuff'}, force=True)
            nt.assert_equals(len(gist.gist.history), 4)

            # modify the new file
            gist.edit('new desc', files={'new.txt':'new stuff222'})
            nt.assert_equals(len(gist.gist.history), 5)

    @require_github
    def test_is_owned(self):
        """ check whether gist is owned by local account """
        gs = GistService()
        gs.login(login, password)
        gist_id = '6705707'
        gist = gs.get_gist(gist_id)
        if login != 'dalejung':
            nt.assert_false(gs.is_owned(gist))
        # note, until we do something that requires auth
        # it won't error
        gs.login('dalejung', 'fakepw')
        nt.assert_true(gs.is_owned(gist))

class TestGister(unittest.TestCase):

    def __init__(self, *args, **kwargs):
        unittest.TestCase.__init__(self, *args, **kwargs)

    def runTest(self):
        pass

    def setUp(self):
        pass

    def test_save(self):
        gist = makeFakeGist()
        gister = Gister(gist, None)

        # delete all files
        gister.save(files=None)
        nt.assert_equal(gist.edit.call_count, 1)
        description, files = gist.edit.call_args[0]
        for fn in files:
            nt.assert_is_none(files[fn])

        gist = makeFakeGist()
        gister = Gister(gist, None)
        # add a new file, edit a.ipynb
        gister.save(files={'new.txt':'new.txt content', 'a.ipynb':'new content'})
        description, files = gist.edit.call_args[0]
        nt.assert_items_equal(files, ['new.txt', 'a.ipynb', 'b.ipynb', 'test.txt'])

        for fn in files:
            f = files[fn]
            if fn == 'new.txt':
                nt.assert_equal(f._InputFileContent__content, 'new.txt content')
            elif fn == 'a.ipynb':
                nt.assert_equal(f._InputFileContent__content, 'new content')
            else:
                nt.assert_is_none(f)

    @require_github
    def test_save_gist_live(self):
        gs = GistService()
        gs.login(login, password)
        gist = gs.create_gist()
        try:
            pass
        finally:
            gist.delete()

    def test_is_dirty(self):
        old_desc = 'Test Gist #notebook #pandas #woo'
        gist = makeFakeGist()
        gister = Gister(gist, None)

        # change desc
        nt.assert_true(gister._is_dirty('changed desc', files={}))

        # no change
        nt.assert_false(gister._is_dirty(old_desc, files={}))

        # new file
        nt.assert_true(gister._is_dirty(old_desc, files={'new file.txt': 'ewn'}))

        # change existing
        nt.assert_true(gister._is_dirty(old_desc, files={'a.ipynb': 'ewn'}))

        # same as previous file content
        nt.assert_false(gister._is_dirty(old_desc, files={'a.ipynb': 'a.ipynb content'}))

if __name__ == '__main__':
    import nose
    nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
                  exit=False)
<commit_msg>FIX: gist test<commit_after>import os
import unittest
from contextlib import contextmanager

from IPython.nbformat import current
from IPython.utils.tempdir import TemporaryDirectory

from ..tests import tools as nt
from ..gist import model_to_files, GistService, Gister
from ..filemanager import BackwardsFileContentsManager
from nbx.nbmanager.tests.common import login, password, require_github, makeFakeGist

class TestGist(unittest.TestCase):

    def __init__(self, *args, **kwargs):
        unittest.TestCase.__init__(self, *args, **kwargs)

    def runTest(self):
        pass

    def setUp(self):
        pass

    def test_model_to_files(self):
        with TemporaryDirectory() as td:
            fm = BackwardsFileContentsManager(root_dir=td)
            model = fm.new_untitled(type='notebook')
            model['path'] = ''
            # need content
            model = fm.get_model(model['name'], model['path'])
            files = model_to_files(model)
            name = model['name']
            # files should only contain one file
            nt.assert_items_equal(files, [name])

            # add a file
            model['__files'] = {'file1.txt': 'file1txt content'}
            files = model_to_files(model)
            nt.assert_items_equal(files, [name, 'file1.txt'])
            nt.assert_equal(files['file1.txt'],
                            'file1txt content')

@contextmanager
def create_gist_context(*args, **kwargs):
    """
    contextmanager that creates a gist and makes sure to delete it
    """
    delete_after = kwargs.pop('delete_after', True)
    gs = GistService()
    gs.login(login, password)
    gist = gs.create_gist(*args, **kwargs)
    yield gist
    if delete_after:
        gist.delete()

class TestGistService(unittest.TestCase):

    def __init__(self, *args, **kwargs):
        unittest.TestCase.__init__(self, *args, **kwargs)

    def runTest(self):
        pass

    def setUp(self):
        pass

    @require_github
    def test_get_gist(self):
        gs = GistService()
        gs.login(login, password)
        gist_id = '6705707'
        gist = gs.get_gist(gist_id)
        nt.assert_equal(gist.owner.login, 'dalejung')

    @require_github
    def test_create_gist(self):
        with create_gist_context() as gist:
            nt.assert_equal(gist.public, True)

        with create_gist_context(public=False) as gist:
            nt.assert_equal(gist.public, False)

        with create_gist_context(public=False, description="nbx test") as gist:
            nt.assert_equal(gist.public, False)
            nt.assert_equal(gist.description, "nbx test")
            nt.assert_equal(gist.owner.login, login)

        files = {'bob2.txt': 'bob2.txt content'}
        with create_gist_context(public=False, files=files) as gist:
            nt.assert_items_equal(gist.files, ['bob2.txt'])

    @require_github
    def test_edit_gist(self):
        with create_gist_context() as gist:
            updated_at = gist.gist.updated_at
            old_desc = gist.description
            # the following should not change the gist
            gist.edit()
            nt.assert_equal(gist.gist.updated_at, updated_at)
            gist.edit(old_desc)
            nt.assert_equal(gist.gist.updated_at, updated_at)
            gist.edit(old_desc, files={'empty.txt':'empty file created by nbx'})
            nt.assert_equal(gist.gist.updated_at, updated_at)

            # change desc
            gist.edit('new desc', files={'empty.txt':'empty file created by nbx'})
            nt.assert_equals(gist.description, 'new desc')
            nt.assert_equals(len(gist.gist.history), 2)

            # add file
            gist.edit('new desc', files={'new.txt':'new stuff'})
            # TODO: Need to deepdive and see why the assert fails. #15
            nt.assert_items_equal(gist.files, ['empty.txt', 'new.txt'])
            nt.assert_equals(len(gist.gist.history), 3)

            # don't modify the new txt. should be no change
            gist.edit('new desc', files={'new.txt':'new stuff'})
            nt.assert_equals(len(gist.gist.history), 3)

            # force a non change commit
            gist.edit('new desc', files={'new.txt':'new stuff'}, force=True)
            nt.assert_equals(len(gist.gist.history), 4)

            # modify the new file
            gist.edit('new desc', files={'new.txt':'new stuff222'})
            nt.assert_equals(len(gist.gist.history), 5)

    @require_github
    def test_is_owned(self):
        """ check whether gist is owned by local account """
        gs = GistService()
        gs.login(login, password)
        gist_id = '6705707'
        gist = gs.get_gist(gist_id)
        if login != 'dalejung':
            nt.assert_false(gs.is_owned(gist))
        # note, until we do something that requires auth
        # it won't error
        gs.login('dalejung', 'fakepw')
        nt.assert_true(gs.is_owned(gist))

class TestGister(unittest.TestCase):

    def __init__(self, *args, **kwargs):
        unittest.TestCase.__init__(self, *args, **kwargs)

    def runTest(self):
        pass

    def setUp(self):
        pass

    def test_save(self):
        gist = makeFakeGist()
        gister = Gister(gist, None)

        # delete all files
        gister.save(files=None)
        nt.assert_equal(gist.edit.call_count, 1)
        description, files = gist.edit.call_args[0]
        for fn in files:
            nt.assert_is_none(files[fn])

        gist = makeFakeGist()
        gister = Gister(gist, None)
        # add a new file, edit a.ipynb
        gister.save(files={'new.txt':'new.txt content', 'a.ipynb':'new content'})
        description, files = gist.edit.call_args[0]
        nt.assert_items_equal(files, ['new.txt', 'a.ipynb', 'b.ipynb', 'test.txt'])

        for fn in files:
            f = files[fn]
            if fn == 'new.txt':
                nt.assert_equal(f._InputFileContent__content, 'new.txt content')
            elif fn == 'a.ipynb':
                nt.assert_equal(f._InputFileContent__content, 'new content')
            else:
                nt.assert_is_none(f)

    @require_github
    def test_save_gist_live(self):
        gs = GistService()
        gs.login(login, password)
        gist = gs.create_gist()
        try:
            pass
        finally:
            gist.delete()

    def test_is_dirty(self):
        old_desc = 'Test Gist #notebook #pandas #woo'
        gist = makeFakeGist()
        gister = Gister(gist, None)

        # change desc
        nt.assert_true(gister._is_dirty('changed desc', files={}))

        # no change
        nt.assert_false(gister._is_dirty(old_desc, files={}))

        # new file
        nt.assert_true(gister._is_dirty(old_desc, files={'new file.txt': 'ewn'}))

        # change existing
        nt.assert_true(gister._is_dirty(old_desc, files={'a.ipynb': 'ewn'}))

        # same as previous file content
        nt.assert_false(gister._is_dirty(old_desc, files={'a.ipynb': 'a.ipynb content'}))

if __name__ == '__main__':
    import nose
    nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
                  exit=False)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Author: Douglas Creager <dcreager@dcreager.net>
# This file is placed into the public domain.

# Calculates the current version number.  If possible, this is the
# output of “git describe”, modified to conform to the versioning
# scheme that setuptools uses.  If “git describe” returns an error
# (most likely because we're in an unpacked copy of a release tarball,
# rather than in a git working copy), then we fall back on reading the
# contents of the RELEASE-VERSION file.
#
# To use this script, simply import it your setup.py file, and use the
# results of get_git_version() as your package version:
#
# from version import *
#
# setup(
#     version=get_git_version(),
#     .
#     .
#     .
# )
#
# This will automatically update the RELEASE-VERSION file, if
# necessary.  Note that the RELEASE-VERSION file should *not* be
# checked into git; please add it to your top-level .gitignore file.
#
# You'll probably want to distribute the RELEASE-VERSION file in your
# sdist tarballs; to do this, just create a MANIFEST.in file that
# contains the following line:
#
#   include RELEASE-VERSION

__all__ = ("get_git_version")

from subprocess import Popen, PIPE

def call_git_describe(abbrev=4):
    try:
        p = Popen(['git', 'describe', '--abbrev=%d' % abbrev],
                  stdout=PIPE, stderr=PIPE)
        p.stderr.close()
        line = p.stdout.readlines()[0]
        return line.strip()

    except:
        return None

def read_release_version():
    try:
        f = open("RELEASE-VERSION", "r")

        try:
            version = f.readlines()[0]
            return version.strip()

        finally:
            f.close()

    except:
        return None

def write_release_version(version):
    f = open("RELEASE-VERSION", "w")
    f.write("%s\n" % version)
    f.close()

def get_git_version(abbrev=4):
    # Read in the version that's currently in RELEASE-VERSION.

    release_version = read_release_version()

    # First try to get the current version using “git describe”.

    version = call_git_describe(abbrev)

    # If that doesn't work, fall back on the value that's in
    # RELEASE-VERSION.

    if version is None:
        version = release_version

    # If we still don't have anything, that's an error.

    if version is None:
        raise ValueError("Cannot find the version number!")

    # If the current version is different from what's in the
    # RELEASE-VERSION file, update the file to be current.

    if version != release_version:
        write_release_version(version)

    # Finally, return the current version.

    return version

if __name__ == "__main__":
    print(get_git_version())
<commit_msg>python 3: explicitly decode the git output as ASCII<commit_after># -*- coding: utf-8 -*-
# Author: Douglas Creager <dcreager@dcreager.net>
# This file is placed into the public domain.

# Calculates the current version number.  If possible, this is the
# output of “git describe”, modified to conform to the versioning
# scheme that setuptools uses.  If “git describe” returns an error
# (most likely because we're in an unpacked copy of a release tarball,
# rather than in a git working copy), then we fall back on reading the
# contents of the RELEASE-VERSION file.
#
# To use this script, simply import it your setup.py file, and use the
# results of get_git_version() as your package version:
#
# from version import *
#
# setup(
#     version=get_git_version(),
#     .
#     .
#     .
# )
#
# This will automatically update the RELEASE-VERSION file, if
# necessary.  Note that the RELEASE-VERSION file should *not* be
# checked into git; please add it to your top-level .gitignore file.
#
# You'll probably want to distribute the RELEASE-VERSION file in your
# sdist tarballs; to do this, just create a MANIFEST.in file that
# contains the following line:
#
#   include RELEASE-VERSION

__all__ = ("get_git_version")

from subprocess import Popen, PIPE

def call_git_describe(abbrev=4):
    try:
        p = Popen(['git', 'describe', '--abbrev=%d' % abbrev],
                  stdout=PIPE, stderr=PIPE)
        p.stderr.close()
        line = p.stdout.readlines()[0].decode('ascii')
        return line.strip()

    except:
        return None

def read_release_version():
    try:
        f = open("RELEASE-VERSION", "r")

        try:
            version = f.readlines()[0]
            return version.strip()

        finally:
            f.close()

    except:
        return None

def write_release_version(version):
    f = open("RELEASE-VERSION", "w")
    f.write("%s\n" % version)
    f.close()

def get_git_version(abbrev=4):
    # Read in the version that's currently in RELEASE-VERSION.

    release_version = read_release_version()

    # First try to get the current version using “git describe”.

    version = call_git_describe(abbrev)

    # If that doesn't work, fall back on the value that's in
    # RELEASE-VERSION.

    if version is None:
        version = release_version

    # If we still don't have anything, that's an error.

    if version is None:
        raise ValueError("Cannot find the version number!")

    # If the current version is different from what's in the
    # RELEASE-VERSION file, update the file to be current.

    if version != release_version:
        write_release_version(version)

    # Finally, return the current version.

    return version

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


<commit_before># MusicPlayer, https://github.com/albertz/music-player
# Copyright (c) 2013, Albert Zeyer, www.az2000.de
# All rights reserved.
# This code is under the 2-clause BSD license, see License.txt in the root directory of this project.

try:
	import argparse
	argParser = argparse.ArgumentParser()
except ImportError:
	print "Error, argparse not available. This needs Python >=2.7."
	# Dummy fallback
	class DummyArgParser:
		def add_argument(self, key, **kwargs):
			assert key[0:2] == "--"
			setattr(self, key[2:], False)
		def parse_args(self): return self
	argParser = DummyArgParser()

argParser.add_argument(
	"--shell", action="store_true",
	help="uses a Python shell instead of the standard stdin control")
argParser.add_argument(
	"--qtgui", action="store_true",
	help="force Qt GUI")
argParser.add_argument(
	"--forkExecProc", nargs=2)
<commit_msg>avoid spamming<commit_after># MusicPlayer, https://github.com/albertz/music-player
# Copyright (c) 2013, Albert Zeyer, www.az2000.de
# All rights reserved.
# This code is under the 2-clause BSD license, see License.txt in the root directory of this project.

try:
	import argparse
	argParser = argparse.ArgumentParser()
except ImportError:
	if len(sys.argv) > 1:
		print "Error, argparse not available. This needs Python >=2.7."
	# Dummy fallback
	class DummyArgParser:
		def add_argument(self, key, **kwargs):
			assert key[0:2] == "--"
			setattr(self, key[2:], False)
		def parse_args(self): return self
	argParser = DummyArgParser()

argParser.add_argument(
	"--shell", action="store_true",
	help="uses a Python shell instead of the standard stdin control")
argParser.add_argument(
	"--qtgui", action="store_true",
	help="force Qt GUI")
argParser.add_argument(
	"--forkExecProc", nargs=2)
<|endoftext|>


<commit_before>#coding=utf-8
#! python
#-*- coding:utf-8 -*-
import os
import string
import ntpath
import glob
import re
import tempfile
import shutil
import datetime    #调用事件模块

def findAllFiles(infolder, pat = r'.txt$'):
    fileList = []
    for root, dirs, files in os.walk(infolder):
        for file in files:
            match = re.search(pat, file, re.I)
            if match:
                fileList.append(os.path.join(root, file))
    return fileList

# 用户名
usr = 'loongfee'
# 获取日期：
today =datetime.date.today()    #获取今天日期
# 格式化输出
ISOFORMAT='%Y%m%d' #设置输出格式
date = today.strftime(ISOFORMAT)
# 临时编译目录
build_path = tempfile.gettempdir() + '\\Jekyll_build'
# 创建临时编译目录
# 如果已经存在则删除后重新创建
if os.path.exists(build_path):
    shutil.rmtree(build_path)
os.mkdir(build_path)

# 支持utf-8编码
os.system('chcp 65001')
# 编译网站
os.system('jekyll build -d %s' %build_path)

# 如果编译没有错误就发布网站
print('cd /d %s' %build_path)
os.system('cd /d %s' %build_path)
os.system('C:')
os.system('del /q/a/f/s %s\*.bat'%build_path)
os.system('git init')
os.system('git add .')
os.system('git commit -m "updated site %s"' %date)
os.system(r'git remote add origin git@github.com:%s/%s.github.com.git'%(usr,usr))
os.system(r'git remote set-url origin git@github.com:%s/%s.github.com.git'%(usr,usr))
os.system('git push origin master --force')

os.system('pause')
<commit_msg>updated site 20141213<commit_after>#coding=utf-8
#! python
#-*- coding:utf-8 -*-
import os
import string
import ntpath
import glob
import re
import tempfile
import shutil
import datetime    #调用事件模块

def findAllFiles(infolder, pat = r'.txt$'):
    fileList = []
    for root, dirs, files in os.walk(infolder):
        for file in files:
            match = re.search(pat, file, re.I)
            if match:
                fileList.append(os.path.join(root, file))
    return fileList

# 用户名
usr = 'loongfee'
# 获取日期：
today =datetime.date.today()    #获取今天日期
# 格式化输出
ISOFORMAT='%Y%m%d' #设置输出格式
date = today.strftime(ISOFORMAT)
# 临时编译目录
build_path = tempfile.gettempdir() + '\\Jekyll_build'
# 创建临时编译目录
# 如果已经存在则删除后重新创建
if os.path.exists(build_path):
    shutil.rmtree(build_path)
os.mkdir(build_path)

# 支持utf-8编码
os.system('chcp 65001')
# 编译网站
os.system('jekyll build -d %s' %build_path)

# 如果编译没有错误就发布网站
print('cd %s' %build_path)
os.system('cd /d %s' %build_path)
os.system('C:')
os.system('del /q/a/f/s %s\*.bat'%build_path)
os.system('git init')
os.system('git add .')
os.system('git commit -m "updated site %s"' %date)
os.system(r'git remote add origin git@github.com:%s/%s.github.com.git'%(usr,usr))
os.system(r'git remote set-url origin git@github.com:%s/%s.github.com.git'%(usr,usr))
os.system('git push origin master --force')

os.system('pause')
<|endoftext|>


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

###
# Copyright (c) 2002, Jeremiah Fincher
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#     this list of conditions, and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions, and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#   * Neither the name of the author of this software nor the name of
#     contributors to this software may be used to endorse or promote products
#     derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###

from test import *

class HttpTest(PluginTestCase, PluginDocumentation):
    plugins = ('Http',)
    def testStockquote(self):
        self.assertNotError('stockquote MSFT')

    def testFreshmeat(self):
        self.assertNotError('freshmeat supybot')

    def testTitle(self):
        self.assertResponse('title slashdot.org',
                            'Slashdot: News for nerds, stuff that matters')
        self.assertResponse('title http://www.slashdot.org/',
                            'Slashdot: News for nerds, stuff that matters')
        self.assertNotRegexp('title '
                             'http://www.amazon.com/exec/obidos/tg/detail/-/'
                             '1884822312/qid=1063140754/sr=8-1/ref=sr_8_1/'
                             '002-9802970-2308826?v=glance&s=books&n=507846',
                             'no HTML title')
        # Checks the non-greediness of the regexp
        self.assertResponse('title '
                            'http://www.space.com/scienceastronomy/'
                            'jupiter_dark_spot_031023.html',
                            'Mystery Spot on Jupiter Baffles Astronomers')

    def testGeekquote(self):
        self.assertNotError('geekquote')
        self.assertNotError('geekquote --id=4848')
        self.assertError('geekquote --id=48a')

    def testAcronym(self):
        self.assertNotError('acronym PERL')
        self.assertNotRegexp('acronym UNIX', 'not an acronym')

    def testNetcraft(self):
        self.assertNotError('netcraft slashdot.org')

    def testWeather(self):
        self.assertNotError('weather Columbus, OH')
        self.assertNotError('weather 43221')
        self.assertNotRegexp('weather Paris, FR', 'Virginia')
        self.assertError('weather alsdkfjasdl, asdlfkjsadlfkj')

    def testKernel(self):
        self.assertNotError('kernel')

    def testPgpkey(self):
        self.assertNotError('pgpkey jeremiah fincher')

# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

<commit_msg>Added a test for a website that causes @title to break<commit_after>#!/usr/bin/env python

###
# Copyright (c) 2002, Jeremiah Fincher
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#     this list of conditions, and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions, and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#   * Neither the name of the author of this software nor the name of
#     contributors to this software may be used to endorse or promote products
#     derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###

from test import *

class HttpTest(PluginTestCase, PluginDocumentation):
    plugins = ('Http',)
    def testStockquote(self):
        self.assertNotError('stockquote MSFT')

    def testFreshmeat(self):
        self.assertNotError('freshmeat supybot')

    def testTitle(self):
        self.assertResponse('title slashdot.org',
                            'Slashdot: News for nerds, stuff that matters')
        self.assertResponse('title http://www.slashdot.org/',
                            'Slashdot: News for nerds, stuff that matters')
        self.assertNotRegexp('title '
                             'http://www.amazon.com/exec/obidos/tg/detail/-/'
                             '1884822312/qid=1063140754/sr=8-1/ref=sr_8_1/'
                             '002-9802970-2308826?v=glance&s=books&n=507846',
                             'no HTML title')
        # Checks the non-greediness of the regexp
        self.assertResponse('title '
                            'http://www.space.com/scienceastronomy/'
                            'jupiter_dark_spot_031023.html',
                            'Mystery Spot on Jupiter Baffles Astronomers')
        # Checks for @title not-working correctly
        self.assertResponse('title '\
            'http://www.catb.org/~esr/jargon/html/F/entry/foo.html',
            'foo')

    def testGeekquote(self):
        self.assertNotError('geekquote')
        self.assertNotError('geekquote --id=4848')
        self.assertError('geekquote --id=48a')

    def testAcronym(self):
        self.assertNotError('acronym PERL')
        self.assertNotRegexp('acronym UNIX', 'not an acronym')

    def testNetcraft(self):
        self.assertNotError('netcraft slashdot.org')

    def testWeather(self):
        self.assertNotError('weather Columbus, OH')
        self.assertNotError('weather 43221')
        self.assertNotRegexp('weather Paris, FR', 'Virginia')
        self.assertError('weather alsdkfjasdl, asdlfkjsadlfkj')

    def testKernel(self):
        self.assertNotError('kernel')

    def testPgpkey(self):
        self.assertNotError('pgpkey jeremiah fincher')

# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

<|endoftext|>