<commit_before>"""Definition of ephemeris
"""

import numpy as np
from datetime import timedelta

from .statevector import StateVector
from ..propagators.listeners import Speaker
from ..frames.frames import orbit2frame

class Ephem(Speaker):
    """This class represents a range of orbits

    It provides several useful interfaces in order to compute through them

    Example:

    .. code-block:: python

        ephem = orb.ephem(Date.now(), timedelta(hours=6), timedelta(minutes=2))
        ephem.frame = 'ITRF'
        ephem.form = 'spherical'
        latitudes = ephem[:,1]
        longitudes = ephem[:,2]
    """

    LINEAR = "linear"
    LAGRANGE = "lagrange"
    DEFAULT_ORDER = 8

    def __init__(self, orbits, method=None, order=None):
        self._orbits = list(sorted(orbits, key=lambda x: x.date))
        self.method = self.LAGRANGE if method is None else method
        self.order = order if isinstance(order, int) else self.DEFAULT_ORDER

    def __iter__(self):
        self._i = -1
        return self

    def __next__(self):
        self._i += 1
        if self._i >= len(self._orbits):
            raise StopIteration
        return self._orbits[self._i]

    def __getitem__(self, index):
        if isinstance(index, (slice, int)):
            # retrieve a defined Orbit object
            return self._orbits[index]
        else:
            # In order to comply with numpy array handling (e.g. column selection)
            return np.array(self._orbits)[index]

    def __len__(self):
        return len(self._orbits)

    @property
    def start(self):
        """Date of the first element"""
        return self._orbits[0].date

    @property
    def stop(self):
        """Date of the last element"""
        return self._orbits[-1].date

    @property
    def dates(self):
        """Generator yielding Dates of each Orbit object of the ephem"""
        return (o.date for o in self)

    # @property
    # def steps(self):
    #     """Time intervals used in the ephemeris
    #     """
    #     return set([self[i].date - self[i + 1].date for i in range(len(self) - 1)])

    @property
    def frame(self):
        """Get the frame of the first point"""
        return self._orbits[0].frame

    @frame.setter
    def frame(self, frame):  # pragma: no cover
        """Change the frames of all points"""
        for orb in self:
            orb.frame = frame

    @property
    def form(self):  # pragma: no cover
        """Get the form of the first point"""
        return self._orbits[0].form

    @form.setter
    def form(self, form):  # pragma: no cover
        """Change the form of all points"""
        for orb in self:
            orb.form = form

    def interpolate(self, date, method=None, order=None):
        """Interpolate data at a given date

        Args:
            date (Date):
            method (str): Method of interpolation to use
            order (int): In case of ``LAGRANGE`` method is used
        Return:
            Orbit:
        Raise:
            ValueError: when date is not in the range of the ephemeris
            ValueError: when the order of interpolation is insufficient
        """

        if not self.start <= date <= self.stop:
            raise ValueError(f"Date '{date}' not in range [{self.start}, {self.stop}]")

        prev_idx = 0
        ephem = self

        # Binary search of the orbit step just before the desired date
        while True:
            idx = len(ephem)
            if idx == 1:
                break
            k = idx // 2

            if date > ephem[k].date:
                prev_idx += k
                ephem = ephem[k:]
            else:
                ephem = ephem[:k]

        method = method if method is not None else self.method
        order = order if order is not None else self.order

        if method == self.LINEAR:

            y0 = self[prev_idx]
            y1 = self[prev_idx + 1]

            result = y0[:] + (y1[:] - y0[:]) * (date._mjd - y0.date._mjd) / (
                y1.date._mjd - y0.date._mjd
            )

        elif method == self.LAGRANGE:

            stop = prev_idx + 1 + order // 2 + order % 2
            start = prev_idx - order // 2 + 1
            if stop >= len(self):
                start -= stop - len(self)
            elif start < 0:
                stop -= start
                start = 0

            # selection of the subset of data, of length 'order' around the desired value
            subset = self[start:stop]
            date_subset = np.array([x.date._mjd for x in subset])

            if len(subset) != order:
                raise ValueError(
                    f"len={len(subset)} < order={order} : impossible to interpolate"
                )

            # Lagrange polynomial
            #        k
            # L(x) = Σ y_j * l_j(x)
            #        j=0
            #
            # l_j(x) = Π (x - x_m) / (x_j - x_m)
            #     0 <= m <= k
            #        m != j
            mask = ~np.identity(order, dtype=bool)
            dates = np.tile(date_subset, order).reshape(order, order)
            datesj = np.repeat(np.diag(dates), order, axis=0).reshape(order, order)
            lj = (
                ((date._mjd - dates[mask]) / (datesj[mask] - dates[mask]))
                .reshape(order, order - 1)
                .prod(axis=1)
            )
            result = lj @ subset
        else:
            raise ValueError("Unkown interpolation method", method)

        return StateVector(result, date, self.form, self.frame)

    def propagate(self, date):
        """Alias of :py:meth:`interpolate`"""
        return self.interpolate(date)

    def iter(
        self, *, dates=None, start=None, stop=None, step=None, strict=True, **kwargs
    ):
        """Ephemeris generator based on the data of this one, but with different dates

        Keyword Arguments:
            dates (list of :py:class:`~beyond.dates.date.Date`): Dates from which iterate over
            start (Date or None): Date of the first point
            stop (Date, timedelta or None): Date of the last point
            step (timedelta or None): Step to use during the computation. Use the same step as
                `self` if `None`
            listeners (list of:py:class:`~beyond.orbits.listeners.Listener`):
            strict (bool): If True, the method will return a ValueError if ``start`` or ``stop`` is
                not in the range of the ephemeris. If False, it will take the closest point in each
                case.
        Yield:
            :py:class:`Orbit`:
        Raise:
            ValueError

        There is two ways to use the iter() method.

        If *dates* is defined, it should be an iterable of dates. This could be
        a generator as per :py:meth:`Date.range <beyond.dates.date.Date.range>`, or a list.

        .. code-block:: python

            # Create two successive ranges of dates, with different steps
            dates = list(Date.range(Date(2019, 3, 23), Date(2019, 3, 24), timedelta(minutes=3)))
            dates.extend(Date.range(Date(2019, 3, 24), Date(2019, 3, 25), timedelta(minutes=10), inclusive=True))
            ephem.iter(dates=dates)

        The alternative, is the use of *start*, *stop* and *step* keyword arguments
        which work exactly as :code:`Date.range(start, stop, step, inclusive=True)`

        If one of *start*, *stop* or *step* arguments is set to ``None`` it will keep
        the same property as the generating ephemeris.

        .. code-block:: python

            # In the examples below, we consider the 'ephem' object to be an ephemeris starting on
            # 2017-01-01 00:00:00 UTC and ending and 2017-01-02 00:00:00 UTC (included) with a fixed
            # step of 3 minutes.

            # These two calls will generate exactly the same points starting at 00:00 and ending at
            # 12:00, as 12:02 does not fall on a date included in the original 'ephem' object.
            ephem.iter(stop=Date(2017, 1, 1, 12))
            ephem.iter(stop=Date(2017, 1, 1, 12, 2))

            # Similarly, these calls will generate the same points starting at 12:00 and ending at
            # 00:00, as 11:58 does not fall on date included in the 'ephem' object.
            ephem.iter(start=Date(2017, 1, 1, 11, 58))
            ephem.iter(start=Date(2017, 1, 1, 12))

            # This call will generate an ephemeris, wich is a subpart of the initial one
            ephem.iter(start=Date(2017, 1, 1, 8), stop=Date(2017, 1, 1, 16))
        """

        # To allow for a loose control of the dates we have to compute
        # the real starting date of the iterator

        listeners = kwargs.get("listeners", [])

        self.clear_listeners(listeners)

        if dates:
            for date in dates:
                orb = self.propagate(date)

                # Listeners
                for listen_orb in self.listen(orb, listeners):
                    yield listen_orb

                yield orb
        else:
            real_start = None

            if start is None:
                start = self.start
            elif start < self.start:
                if strict:
                    raise ValueError(
                        f"Start date not in range [{self.start}, {self.stop}]"
                    )
                else:
                    real_start = self.start

            if stop is None:
                stop = self.stop
            else:
                if isinstance(stop, timedelta):
                    stop = start + stop
                if stop > self.stop:
                    if strict:
                        raise ValueError(
                            f"Stop date not in range [{self.start}, {self.stop}]"
                        )
                    else:
                        stop = self.stop

            if real_start is not None:
                start = real_start

            if step is None:

                # The step stays the same as the original ephemeris
                for orb in self:

                    if orb.date < start:
                        continue

                    if orb.date > stop:
                        break

                    # Listeners
                    for listen_orb in self.listen(orb, listeners):
                        yield listen_orb

                    # yield a copy of the recorded orbit to avoid later modification
                    # which could have dire consequences
                    yield orb.copy()
            else:
                # create as ephemeris with a different step than the original
                date = start
                while date <= stop:

                    orb = self.propagate(date)

                    # Listeners
                    for listen_orb in self.listen(orb, listeners):
                        yield listen_orb

                    yield orb
                    date += step

    def ephemeris(self, *args, **kwargs):
        """Same as :py:meth:`self.iter() <iter>`

        Implemented to expose the same methods as :py:class:`Orbit`
        """
        return self.iter(*args, **kwargs)

    def ephem(self, *args, **kwargs):
        """Create an Ephem object which is a subset of this one

        Take the same keyword arguments as :py:meth:`ephemeris`

        Return:
            Ephem:
        """

        return self.__class__(self.ephemeris(*args, **kwargs))

    def copy(self, *, form=None, frame=None, same=None):  # pragma: no cover
        """Create a deep copy of the ephemeris. Optionally, allow frame and form changing

        Keyword Args:
            form (str or Form): Form to convert the new instance into
            frame (str or Frame): Frame to convert the new instance into
            same (StateVector): A statevector from which to copy the frame and form
        Return:
            Ephem : New ephemeris object

        If the argument *same* is used, it overwrites *frame* and *form*.

        Example:

        .. code-block:: python

            # New instance of the same ephemeris
            e1 = e.copy()

            # ephemeris converted into spherical form
            e2 = e.copy(form="spherical")

            # ephemeris converted into EME2000 frame, keplerian form
            e3 = e.copy(form="keplerian", frame="EME2000")

            # ephemeris in the same frame and form as e3 (EME2000, keplerian)
            e4 = e.copy(same=sv3)

        Override :py:meth:`numpy.ndarray.copy()` to include additional
        fields
        """

        new = self.ephem()

        if same is not None:
            if hasattr(same, "form") and hasattr(same, "frame"):
                frame = same.frame
                form = same.form
            else:
                raise TypeError("'same' does not have a frame and/or a form attribute")

        if frame:
            new.frame = frame
        if form:
            new.form = form

        return new

    def as_frame(self, name, **kwargs):  # pragma: no cover
        """Register the Ephem object as a frame

        see :py:func:`beyond.frames.frames.orbit2frame` for details of the arguments
        """
        return orbit2frame(name, self, **kwargs)
<commit_msg>fix : better error messages when out-of-range interpolation<commit_after>"""Definition of ephemeris
"""

import numpy as np
from datetime import timedelta

from .statevector import StateVector
from ..propagators.listeners import Speaker
from ..frames.frames import orbit2frame

class Ephem(Speaker):
    """This class represents a range of orbits

    It provides several useful interfaces in order to compute through them

    Example:

    .. code-block:: python

        ephem = orb.ephem(Date.now(), timedelta(hours=6), timedelta(minutes=2))
        ephem.frame = 'ITRF'
        ephem.form = 'spherical'
        latitudes = ephem[:,1]
        longitudes = ephem[:,2]
    """

    LINEAR = "linear"
    LAGRANGE = "lagrange"
    DEFAULT_ORDER = 8

    def __init__(self, orbits, method=None, order=None):
        self._orbits = list(sorted(orbits, key=lambda x: x.date))
        self.method = self.LAGRANGE if method is None else method
        self.order = order if isinstance(order, int) else self.DEFAULT_ORDER

    def __iter__(self):
        self._i = -1
        return self

    def __next__(self):
        self._i += 1
        if self._i >= len(self._orbits):
            raise StopIteration
        return self._orbits[self._i]

    def __getitem__(self, index):
        if isinstance(index, (slice, int)):
            # retrieve a defined Orbit object
            return self._orbits[index]
        else:
            # In order to comply with numpy array handling (e.g. column selection)
            return np.array(self._orbits)[index]

    def __len__(self):
        return len(self._orbits)

    @property
    def start(self):
        """Date of the first element"""
        return self._orbits[0].date

    @property
    def stop(self):
        """Date of the last element"""
        return self._orbits[-1].date

    @property
    def dates(self):
        """Generator yielding Dates of each Orbit object of the ephem"""
        return (o.date for o in self)

    # @property
    # def steps(self):
    #     """Time intervals used in the ephemeris
    #     """
    #     return set([self[i].date - self[i + 1].date for i in range(len(self) - 1)])

    @property
    def frame(self):
        """Get the frame of the first point"""
        return self._orbits[0].frame

    @frame.setter
    def frame(self, frame):  # pragma: no cover
        """Change the frames of all points"""
        for orb in self:
            orb.frame = frame

    @property
    def form(self):  # pragma: no cover
        """Get the form of the first point"""
        return self._orbits[0].form

    @form.setter
    def form(self, form):  # pragma: no cover
        """Change the form of all points"""
        for orb in self:
            orb.form = form

    def interpolate(self, date, method=None, order=None):
        """Interpolate data at a given date

        Args:
            date (Date):
            method (str): Method of interpolation to use
            order (int): In case of ``LAGRANGE`` method is used
        Return:
            Orbit:
        Raise:
            ValueError: when date is not in the range of the ephemeris
            ValueError: when the order of interpolation is insufficient
        """

        if not self.start <= date <= self.stop:
            raise ValueError(f"Date '{date}' not in range [{self.start}, {self.stop}]")

        prev_idx = 0
        ephem = self

        # Binary search of the orbit step just before the desired date
        while True:
            idx = len(ephem)
            if idx == 1:
                break
            k = idx // 2

            if date > ephem[k].date:
                prev_idx += k
                ephem = ephem[k:]
            else:
                ephem = ephem[:k]

        method = method if method is not None else self.method
        order = order if order is not None else self.order

        if method == self.LINEAR:

            y0 = self[prev_idx]
            y1 = self[prev_idx + 1]

            result = y0[:] + (y1[:] - y0[:]) * (date._mjd - y0.date._mjd) / (
                y1.date._mjd - y0.date._mjd
            )

        elif method == self.LAGRANGE:

            stop = prev_idx + 1 + order // 2 + order % 2
            start = prev_idx - order // 2 + 1
            if stop >= len(self):
                start -= stop - len(self)
            elif start < 0:
                stop -= start
                start = 0

            # selection of the subset of data, of length 'order' around the desired value
            subset = self[start:stop]
            date_subset = np.array([x.date._mjd for x in subset])

            if len(subset) != order:
                raise ValueError(
                    f"len={len(subset)} < order={order} : impossible to interpolate"
                )

            # Lagrange polynomial
            #        k
            # L(x) = Σ y_j * l_j(x)
            #        j=0
            #
            # l_j(x) = Π (x - x_m) / (x_j - x_m)
            #     0 <= m <= k
            #        m != j
            mask = ~np.identity(order, dtype=bool)
            dates = np.tile(date_subset, order).reshape(order, order)
            datesj = np.repeat(np.diag(dates), order, axis=0).reshape(order, order)
            lj = (
                ((date._mjd - dates[mask]) / (datesj[mask] - dates[mask]))
                .reshape(order, order - 1)
                .prod(axis=1)
            )
            result = lj @ subset
        else:
            raise ValueError("Unkown interpolation method", method)

        return StateVector(result, date, self.form, self.frame)

    def propagate(self, date):
        """Alias of :py:meth:`interpolate`"""
        return self.interpolate(date)

    def iter(
        self, *, dates=None, start=None, stop=None, step=None, strict=True, **kwargs
    ):
        """Ephemeris generator based on the data of this one, but with different dates

        Keyword Arguments:
            dates (list of :py:class:`~beyond.dates.date.Date`): Dates from which iterate over
            start (Date or None): Date of the first point
            stop (Date, timedelta or None): Date of the last point
            step (timedelta or None): Step to use during the computation. Use the same step as
                `self` if `None`
            listeners (list of:py:class:`~beyond.orbits.listeners.Listener`):
            strict (bool): If True, the method will return a ValueError if ``start`` or ``stop`` is
                not in the range of the ephemeris. If False, it will take the closest point in each
                case.
        Yield:
            :py:class:`Orbit`:
        Raise:
            ValueError

        There is two ways to use the iter() method.

        If *dates* is defined, it should be an iterable of dates. This could be
        a generator as per :py:meth:`Date.range <beyond.dates.date.Date.range>`, or a list.

        .. code-block:: python

            # Create two successive ranges of dates, with different steps
            dates = list(Date.range(Date(2019, 3, 23), Date(2019, 3, 24), timedelta(minutes=3)))
            dates.extend(Date.range(Date(2019, 3, 24), Date(2019, 3, 25), timedelta(minutes=10), inclusive=True))
            ephem.iter(dates=dates)

        The alternative, is the use of *start*, *stop* and *step* keyword arguments
        which work exactly as :code:`Date.range(start, stop, step, inclusive=True)`

        If one of *start*, *stop* or *step* arguments is set to ``None`` it will keep
        the same property as the generating ephemeris.

        .. code-block:: python

            # In the examples below, we consider the 'ephem' object to be an ephemeris starting on
            # 2017-01-01 00:00:00 UTC and ending and 2017-01-02 00:00:00 UTC (included) with a fixed
            # step of 3 minutes.

            # These two calls will generate exactly the same points starting at 00:00 and ending at
            # 12:00, as 12:02 does not fall on a date included in the original 'ephem' object.
            ephem.iter(stop=Date(2017, 1, 1, 12))
            ephem.iter(stop=Date(2017, 1, 1, 12, 2))

            # Similarly, these calls will generate the same points starting at 12:00 and ending at
            # 00:00, as 11:58 does not fall on date included in the 'ephem' object.
            ephem.iter(start=Date(2017, 1, 1, 11, 58))
            ephem.iter(start=Date(2017, 1, 1, 12))

            # This call will generate an ephemeris, wich is a subpart of the initial one
            ephem.iter(start=Date(2017, 1, 1, 8), stop=Date(2017, 1, 1, 16))
        """

        # To allow for a loose control of the dates we have to compute
        # the real starting date of the iterator

        listeners = kwargs.get("listeners", [])

        self.clear_listeners(listeners)

        if dates:
            for date in dates:
                orb = self.propagate(date)

                # Listeners
                for listen_orb in self.listen(orb, listeners):
                    yield listen_orb

                yield orb
        else:
            real_start = None

            if start is None:
                start = self.start
            elif start < self.start:
                if strict:
                    raise ValueError(
                        f"Start date '{start}' not in range [{self.start}, {self.stop}]"
                    )
                else:
                    real_start = self.start

            if stop is None:
                stop = self.stop
            else:
                if isinstance(stop, timedelta):
                    stop = start + stop
                if stop > self.stop:
                    if strict:
                        raise ValueError(
                            f"Stop date '{stop}' not in range [{self.start}, {self.stop}]"
                        )
                    else:
                        stop = self.stop

            if real_start is not None:
                start = real_start

            if step is None:

                # The step stays the same as the original ephemeris
                for orb in self:

                    if orb.date < start:
                        continue

                    if orb.date > stop:
                        break

                    # Listeners
                    for listen_orb in self.listen(orb, listeners):
                        yield listen_orb

                    # yield a copy of the recorded orbit to avoid later modification
                    # which could have dire consequences
                    yield orb.copy()
            else:
                # create as ephemeris with a different step than the original
                date = start
                while date <= stop:

                    orb = self.propagate(date)

                    # Listeners
                    for listen_orb in self.listen(orb, listeners):
                        yield listen_orb

                    yield orb
                    date += step

    def ephemeris(self, *args, **kwargs):
        """Same as :py:meth:`self.iter() <iter>`

        Implemented to expose the same methods as :py:class:`Orbit`
        """
        return self.iter(*args, **kwargs)

    def ephem(self, *args, **kwargs):
        """Create an Ephem object which is a subset of this one

        Take the same keyword arguments as :py:meth:`ephemeris`

        Return:
            Ephem:
        """

        return self.__class__(self.ephemeris(*args, **kwargs))

    def copy(self, *, form=None, frame=None, same=None):  # pragma: no cover
        """Create a deep copy of the ephemeris. Optionally, allow frame and form changing

        Keyword Args:
            form (str or Form): Form to convert the new instance into
            frame (str or Frame): Frame to convert the new instance into
            same (StateVector): A statevector from which to copy the frame and form
        Return:
            Ephem : New ephemeris object

        If the argument *same* is used, it overwrites *frame* and *form*.

        Example:

        .. code-block:: python

            # New instance of the same ephemeris
            e1 = e.copy()

            # ephemeris converted into spherical form
            e2 = e.copy(form="spherical")

            # ephemeris converted into EME2000 frame, keplerian form
            e3 = e.copy(form="keplerian", frame="EME2000")

            # ephemeris in the same frame and form as e3 (EME2000, keplerian)
            e4 = e.copy(same=sv3)

        Override :py:meth:`numpy.ndarray.copy()` to include additional
        fields
        """

        new = self.ephem()

        if same is not None:
            if hasattr(same, "form") and hasattr(same, "frame"):
                frame = same.frame
                form = same.form
            else:
                raise TypeError("'same' does not have a frame and/or a form attribute")

        if frame:
            new.frame = frame
        if form:
            new.form = form

        return new

    def as_frame(self, name, **kwargs):  # pragma: no cover
        """Register the Ephem object as a frame

        see :py:func:`beyond.frames.frames.orbit2frame` for details of the arguments
        """
        return orbit2frame(name, self, **kwargs)
<|endoftext|>


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

"""Run all test cases."""

import sys
import os
import re
import unittest

import numpy

import numexpr
import tables
from tables.req_versions import *
from tables.tests import common
from tables.utils import detect_number_of_cores

def get_tuple_version(hexversion):
    """Get a tuple from a compact version in hex."""
    h = hexversion
    return(h & 0xff0000) >> 16, (h & 0xff00) >> 8, h & 0xff

def suite():
    test_modules = [
        'tables.tests.test_attributes',
        'tables.tests.test_basics',
        'tables.tests.test_create',
        'tables.tests.test_backcompat',
        'tables.tests.test_types',
        'tables.tests.test_lists',
        'tables.tests.test_tables',
        'tables.tests.test_tablesMD',
        'tables.tests.test_array',
        'tables.tests.test_earray',
        'tables.tests.test_carray',
        'tables.tests.test_vlarray',
        'tables.tests.test_tree',
        'tables.tests.test_timetype',
        'tables.tests.test_do_undo',
        'tables.tests.test_enum',
        'tables.tests.test_nestedtypes',
        'tables.tests.test_hdf5compat',
        'tables.tests.test_numpy',
        'tables.tests.test_queries',
        'tables.tests.test_expression',
        'tables.tests.test_links',
        'tables.tests.test_indexes',
        'tables.tests.test_indexvalues',
        'tables.tests.test_index_backcompat',
        # Sub-packages
        'tables.nodes.tests.test_filenode',
    ]

    # print '-=' * 38

    # The test for garbage must be run *in the last place*.
    # Else, it is not as useful.
    test_modules.append('tables.tests.test_garbage')

    alltests = unittest.TestSuite()
    if common.show_memory:
        # Add a memory report at the beginning
        alltests.addTest(unittest.makeSuite(common.ShowMemTime))
    for name in test_modules:
        # Unexpectedly, the following code doesn't seem to work anymore
        # in python 3
        # exec('from %s import suite as test_suite' % name)
        __import__(name)
        test_suite = sys.modules[name].suite

        alltests.addTest(test_suite())
        if common.show_memory:
            # Add a memory report after each test module
            alltests.addTest(unittest.makeSuite(common.ShowMemTime))
    return alltests

def print_versions():
    """Print all the versions of software that PyTables relies on."""
    print '-=' * 38
    print "PyTables version:  %s" % tables.__version__
    print "HDF5 version:      %s" % tables.which_lib_version("hdf5")[1]
    print "NumPy version:     %s" % numpy.__version__
    tinfo = tables.which_lib_version("zlib")
    if numexpr.use_vml:
        # Get only the main version number and strip out all the rest
        vml_version = numexpr.get_vml_version()
        vml_version = re.findall("[0-9.]+", vml_version)[0]
        vml_avail = "using VML/MKL %s" % vml_version
    else:
        vml_avail = "not using Intel's VML/MKL"
    print "Numexpr version:   %s (%s)" % (numexpr.__version__, vml_avail)
    if tinfo is not None:
        print "Zlib version:      %s (%s)" % (tinfo[1], "in Python interpreter")
    tinfo = tables.which_lib_version("lzo")
    if tinfo is not None:
        print "LZO version:       %s (%s)" % (tinfo[1], tinfo[2])
    tinfo = tables.which_lib_version("bzip2")
    if tinfo is not None:
        print "BZIP2 version:     %s (%s)" % (tinfo[1], tinfo[2])
    tinfo = tables.which_lib_version("blosc")
    if tinfo is not None:
        blosc_date = tinfo[2].split()[1]
        print "Blosc version:     %s (%s)" % (tinfo[1], blosc_date)
    try:
        from Cython.Compiler.Main import Version as Cython_Version
        print 'Cython version:    %s' % Cython_Version.version
    except:
        pass
    print 'Python version:    %s' % sys.version
    if os.name == 'posix':
        (sysname, nodename, release, version, machine) = os.uname()
        print 'Platform:          %s-%s' % (sys.platform, machine)
    print 'Byte-ordering:     %s' % sys.byteorder
    print 'Detected cores:    %s' % detect_number_of_cores()
    print '-=' * 38

def print_heavy(heavy):
    if heavy:
        print """\
Performing the complete test suite!"""
    else:
        print """\
Performing only a light (yet comprehensive) subset of the test suite.
If you want a more complete test, try passing the --heavy flag to this script
(or set the 'heavy' parameter in case you are using tables.test() call).
The whole suite will take more than 4 hours to complete on a relatively
modern CPU and around 512 MB of main memory."""
    print '-=' * 38

def test(verbose=False, heavy=False):
    """Run all the tests in the test suite.

    If *verbose* is set, the test suite will emit messages with full
    verbosity (not recommended unless you are looking into a certain
    problem).

    If *heavy* is set, the test suite will be run in *heavy* mode (you
    should be careful with this because it can take a lot of time and
    resources from your computer).

    Return 0 (os.EX_OK) if all tests pass, 1 in case of failure
    """

    print_versions()
    print_heavy(heavy)

    # What a context this is!
    oldverbose, common.verbose = common.verbose, verbose
    oldheavy, common.heavy = common.heavy, heavy
    try:
        result = unittest.TextTestRunner().run(suite())
        if result.wasSuccessful():
            return 0
        else:
            return 1
    finally:
        common.verbose = oldverbose
        common.heavy = oldheavy  # there are pretty young heavies, too ;)

if __name__ == '__main__':

    hdf5_version = get_tuple_version(tables.which_lib_version("hdf5")[0])
    if hdf5_version < min_hdf5_version:
        print "*Warning*: HDF5 version is lower than recommended: %s < %s" % \
              (hdf5_version, min_hdf5_version)

    if numpy.__version__ < min_numpy_version:
        print "*Warning*: NumPy version is lower than recommended: %s < %s" % \
              (numpy.__version__, min_numpy_version)

    # Handle some global flags (i.e. only useful for test_all.py)
    only_versions = 0
    args = sys.argv[:]
    for arg in args:
        # Remove 'show-versions' for PyTables 2.3 or higher
        if arg in ['--print-versions', '--show-versions']:
            only_versions = True
            sys.argv.remove(arg)
        elif arg == '--show-memory':
            common.show_memory = True
            sys.argv.remove(arg)

    print_versions()
    if not only_versions:
        print_heavy(common.heavy)
        unittest.main(defaultTest='tables.tests.suite')
<commit_msg>Print default encoding in print_versions<commit_after># -*- coding: utf-8 -*-

"""Run all test cases."""

import os
import re
import sys
import locale
import unittest

import numpy

import numexpr
import tables
from tables.req_versions import *
from tables.tests import common
from tables.utils import detect_number_of_cores

def get_tuple_version(hexversion):
    """Get a tuple from a compact version in hex."""
    h = hexversion
    return(h & 0xff0000) >> 16, (h & 0xff00) >> 8, h & 0xff

def suite():
    test_modules = [
        'tables.tests.test_attributes',
        'tables.tests.test_basics',
        'tables.tests.test_create',
        'tables.tests.test_backcompat',
        'tables.tests.test_types',
        'tables.tests.test_lists',
        'tables.tests.test_tables',
        'tables.tests.test_tablesMD',
        'tables.tests.test_array',
        'tables.tests.test_earray',
        'tables.tests.test_carray',
        'tables.tests.test_vlarray',
        'tables.tests.test_tree',
        'tables.tests.test_timetype',
        'tables.tests.test_do_undo',
        'tables.tests.test_enum',
        'tables.tests.test_nestedtypes',
        'tables.tests.test_hdf5compat',
        'tables.tests.test_numpy',
        'tables.tests.test_queries',
        'tables.tests.test_expression',
        'tables.tests.test_links',
        'tables.tests.test_indexes',
        'tables.tests.test_indexvalues',
        'tables.tests.test_index_backcompat',
        # Sub-packages
        'tables.nodes.tests.test_filenode',
    ]

    # print '-=' * 38

    # The test for garbage must be run *in the last place*.
    # Else, it is not as useful.
    test_modules.append('tables.tests.test_garbage')

    alltests = unittest.TestSuite()
    if common.show_memory:
        # Add a memory report at the beginning
        alltests.addTest(unittest.makeSuite(common.ShowMemTime))
    for name in test_modules:
        # Unexpectedly, the following code doesn't seem to work anymore
        # in python 3
        # exec('from %s import suite as test_suite' % name)
        __import__(name)
        test_suite = sys.modules[name].suite

        alltests.addTest(test_suite())
        if common.show_memory:
            # Add a memory report after each test module
            alltests.addTest(unittest.makeSuite(common.ShowMemTime))
    return alltests

def print_versions():
    """Print all the versions of software that PyTables relies on."""
    print '-=' * 38
    print "PyTables version:  %s" % tables.__version__
    print "HDF5 version:      %s" % tables.which_lib_version("hdf5")[1]
    print "NumPy version:     %s" % numpy.__version__
    tinfo = tables.which_lib_version("zlib")
    if numexpr.use_vml:
        # Get only the main version number and strip out all the rest
        vml_version = numexpr.get_vml_version()
        vml_version = re.findall("[0-9.]+", vml_version)[0]
        vml_avail = "using VML/MKL %s" % vml_version
    else:
        vml_avail = "not using Intel's VML/MKL"
    print "Numexpr version:   %s (%s)" % (numexpr.__version__, vml_avail)
    if tinfo is not None:
        print "Zlib version:      %s (%s)" % (tinfo[1], "in Python interpreter")
    tinfo = tables.which_lib_version("lzo")
    if tinfo is not None:
        print "LZO version:       %s (%s)" % (tinfo[1], tinfo[2])
    tinfo = tables.which_lib_version("bzip2")
    if tinfo is not None:
        print "BZIP2 version:     %s (%s)" % (tinfo[1], tinfo[2])
    tinfo = tables.which_lib_version("blosc")
    if tinfo is not None:
        blosc_date = tinfo[2].split()[1]
        print "Blosc version:     %s (%s)" % (tinfo[1], blosc_date)
    try:
        from Cython.Compiler.Main import Version as Cython_Version
        print 'Cython version:    %s' % Cython_Version.version
    except:
        pass
    print 'Python version:    %s' % sys.version
    if os.name == 'posix':
        (sysname, nodename, release, version, machine) = os.uname()
        print 'Platform:          %s-%s' % (sys.platform, machine)
    print 'Byte-ordering:     %s' % sys.byteorder
    print 'Detected cores:    %s' % detect_number_of_cores()
    print 'Default encoding:  %s' % sys.getdefaultencoding()
    print '-=' * 38

def print_heavy(heavy):
    if heavy:
        print """\
Performing the complete test suite!"""
    else:
        print """\
Performing only a light (yet comprehensive) subset of the test suite.
If you want a more complete test, try passing the --heavy flag to this script
(or set the 'heavy' parameter in case you are using tables.test() call).
The whole suite will take more than 4 hours to complete on a relatively
modern CPU and around 512 MB of main memory."""
    print '-=' * 38

def test(verbose=False, heavy=False):
    """Run all the tests in the test suite.

    If *verbose* is set, the test suite will emit messages with full
    verbosity (not recommended unless you are looking into a certain
    problem).

    If *heavy* is set, the test suite will be run in *heavy* mode (you
    should be careful with this because it can take a lot of time and
    resources from your computer).

    Return 0 (os.EX_OK) if all tests pass, 1 in case of failure
    """

    print_versions()
    print_heavy(heavy)

    # What a context this is!
    oldverbose, common.verbose = common.verbose, verbose
    oldheavy, common.heavy = common.heavy, heavy
    try:
        result = unittest.TextTestRunner().run(suite())
        if result.wasSuccessful():
            return 0
        else:
            return 1
    finally:
        common.verbose = oldverbose
        common.heavy = oldheavy  # there are pretty young heavies, too ;)

if __name__ == '__main__':

    hdf5_version = get_tuple_version(tables.which_lib_version("hdf5")[0])
    if hdf5_version < min_hdf5_version:
        print "*Warning*: HDF5 version is lower than recommended: %s < %s" % \
              (hdf5_version, min_hdf5_version)

    if numpy.__version__ < min_numpy_version:
        print "*Warning*: NumPy version is lower than recommended: %s < %s" % \
              (numpy.__version__, min_numpy_version)

    # Handle some global flags (i.e. only useful for test_all.py)
    only_versions = 0
    args = sys.argv[:]
    for arg in args:
        # Remove 'show-versions' for PyTables 2.3 or higher
        if arg in ['--print-versions', '--show-versions']:
            only_versions = True
            sys.argv.remove(arg)
        elif arg == '--show-memory':
            common.show_memory = True
            sys.argv.remove(arg)

    print_versions()
    if not only_versions:
        print_heavy(common.heavy)
        unittest.main(defaultTest='tables.tests.suite')
<|endoftext|>


<commit_before>from django import forms
from django.conf import settings
try:
    # Django 1.9
    from django.forms.utils import flatatt
except ImportError:
    # Django <1.9
    from django.forms.util import flatatt
from django.utils.safestring import mark_safe
from django.utils import six

from taggit.models import Tag
from taggit.utils import edit_string_for_tags

class LabelWidget(forms.TextInput):
    """
    Widget class for rendering an item's tags - and all existing tags - as
    selectable labels.
    """
    input_type = 'hidden'
    model = Tag

    def __init__(self, *args, **kwargs):
        self.model = kwargs.pop('model', None) or self.model
        super(LabelWidget, self).__init__(*args, **kwargs)

    @property
    def is_hidden(self):
        return False

    def tag_list(self, tags):
        """
        Generates a list of tags identifying those previously selected.

        Returns a list of tuples of the form (<tag name>, <CSS class name>).

        Uses the string names rather than the tags themselves in order to work
        with tag lists built from forms not fully submitted.
        """
        return [(tag.name, 'selected taggit-tag' if tag.name in tags else 'taggit-tag')
                for tag in self.model.objects.all()]

    def format_value(self, value):
        if value is not None and not isinstance(value, six.string_types):
            value = edit_string_for_tags([o.tag for o in value.select_related("tag")])
        return value

    def render(self, name, value, attrs={}):
        # Case in which a new form is dispalyed
        if value is None:
            current_tags = []
            formatted_value = ""
            selected_tags = self.tag_list([])

        # Case in which a form is displayed with submitted but not saved
        # details, e.g. invalid form submission
        elif isinstance(value, six.string_types):
            current_tags = [tag.strip(" \"") for tag in value.split(",") if tag]
            formatted_value = value
            selected_tags = self.tag_list(current_tags)

        # Case in which value is loaded from saved tags
        else:
            current_tags = [o.tag for o in value.select_related("tag")]
            formatted_value = self.format_value(value)
            selected_tags = self.tag_list([t.name for t in current_tags])

        input_field = super(LabelWidget, self).render(name, formatted_value, attrs)

        if attrs.get('class') is None:
            attrs.update({'class': 'taggit-labels taggit-list'})
        list_attrs = flatatt(attrs)

        tag_li = "".join([u"
{0}
".format(
            tag[0], tag[1]) for tag in selected_tags])
        tag_ul = u"
{1}
".format(list_attrs, tag_li)
        return mark_safe(u"{0}{1}".format(tag_ul, input_field))

    @property
    def media(self):
        extra = '' if settings.DEBUG else '.min'
        admin_prefix = 'admin/js'
        js = [
            '%s/vendor/jquery/jquery%s.js' % (admin_prefix, extra),
            '%s/jquery.init.js' % admin_prefix,
            '%s/core.js' % admin_prefix,
            'taggit_labels/js/taggit_labels.js'
        ]
        css = {
            'all': ('taggit_labels/css/taggit_labels.css',)
        }
<commit_msg>Update widgets.py to fix bug with Django >= 2.0<commit_after>from django import forms
from django.conf import settings
try:
    # Django 1.9
    from django.forms.utils import flatatt
except ImportError:
    # Django <1.9
    from django.forms.util import flatatt
from django.utils.safestring import mark_safe
from django.utils import six

from taggit.models import Tag
from taggit.utils import edit_string_for_tags

class LabelWidget(forms.TextInput):
    """
    Widget class for rendering an item's tags - and all existing tags - as
    selectable labels.
    """
    input_type = 'hidden'
    model = Tag

    def __init__(self, *args, **kwargs):
        self.model = kwargs.pop('model', None) or self.model
        super(LabelWidget, self).__init__(*args, **kwargs)

    @property
    def is_hidden(self):
        return False

    def tag_list(self, tags):
        """
        Generates a list of tags identifying those previously selected.

        Returns a list of tuples of the form (<tag name>, <CSS class name>).

        Uses the string names rather than the tags themselves in order to work
        with tag lists built from forms not fully submitted.
        """
        return [(tag.name, 'selected taggit-tag' if tag.name in tags else 'taggit-tag')
                for tag in self.model.objects.all()]

    def format_value(self, value):
        if value is not None and not isinstance(value, six.string_types):
            value = edit_string_for_tags([o.tag for o in value.select_related("tag")])
        return value

    def render(self, name, value, attrs={}):
        # Case in which a new form is dispalyed
        if value is None:
            current_tags = []
            formatted_value = ""
            selected_tags = self.tag_list([])

        # Case in which a form is displayed with submitted but not saved
        # details, e.g. invalid form submission
        elif isinstance(value, six.string_types):
            current_tags = [tag.strip(" \"") for tag in value.split(",") if tag]
            formatted_value = value
            selected_tags = self.tag_list(current_tags)

        # Case in which value is loaded from saved tags
        else:
            current_tags = [o.tag for o in value.select_related("tag")]
            formatted_value = self.format_value(value)
            selected_tags = self.tag_list([t.name for t in current_tags])

        input_field = super(LabelWidget, self).render(name, formatted_value, attrs)

        if attrs.get('class') is None:
            attrs.update({'class': 'taggit-labels taggit-list'})
        list_attrs = flatatt(attrs)

        tag_li = "".join([u"
{0}
".format(
            tag[0], tag[1]) for tag in selected_tags])
        tag_ul = u"
{1}
".format(list_attrs, tag_li)
        return mark_safe(u"{0}{1}".format(tag_ul, input_field))

    @property
    def media(self):
        extra = '' if settings.DEBUG else '.min'
        admin_prefix = 'admin/js'
        js = [
            '%s/vendor/jquery/jquery%s.js' % (admin_prefix, extra),
            '%s/jquery.init.js' % admin_prefix,
            '%s/core.js' % admin_prefix,
            'taggit_labels/js/taggit_labels.js'
        ]
        css = {
            'all': ('taggit_labels/css/taggit_labels.css',)
        }

        return forms.Media(js=js, css=css)
<|endoftext|>


<commit_before># -*- coding:utf8 -*-
# File   : netsrc.py
# Author : Jiayuan Mao
# Email  : maojiayuan@gmail.com
# Date   : 12/31/16
#
# This file is part of TensorArtist

import numpy as np
import tensorflow as tf

from ._defaults import __default_dtype__
from .helper import device_context, wrap_varnode_func, wrap_named_op, unique_opr_name
from ..graph.env import get_default_env
from ..graph.node import as_varnode, OprNode, __valid_tensor_types__
from ..tfutils import assign_variable, fetch_variable, TArtGraphKeys, extend_collection_list
from ...core.utils.meta import assert_notnone

__all__ = ['placeholder', 'variable', 'scalar', 'constant', 'ensure_variable',
           'get_scalar', 'get_scalar_value', 'set_scalar_value']

@wrap_varnode_func
def placeholder(name, shape=None, dtype=__default_dtype__, device=None):
    with device_context(device):
        var = as_varnode(tf.placeholder(name=name, shape=shape, dtype=dtype))
        return var

class VariableOp(OprNode):
    def __init__(self, name, output, owner_env):
        super().__init__(name, [], [output])
        self._owner_env = owner_env

    def get_value(self):
        var = self.outputs[0].impl
        return fetch_variable(var, self._owner_env.session)

    def set_value(self, value, use_locking=False):
        var = self.outputs[0].impl
        assign_variable(var, value, self._owner_env.session, use_locking=use_locking)
        return self

@wrap_named_op(use_scope=False)
@wrap_varnode_func
def variable(name, value_or_initializer, shape=None, dtype=__default_dtype__, device=None, trainable=True,
             collections=None):
    opr_name = unique_opr_name(name)

    with device_context(device):
        if isinstance(value_or_initializer, (np.ndarray, float, int)):
            if type(value_or_initializer) is float:
                dtype = dtype or tf.float32
            elif type(value_or_initializer) is int:
                dtype = dtype or tf.int32
            var = tf.Variable(initial_value=value_or_initializer, trainable=trainable, name=name, dtype=dtype,
                              collections=collections)
        else:
            assert_notnone(shape, name='shape')
            var = tf.get_variable(name, shape=shape, dtype=dtype, initializer=value_or_initializer,
                                  trainable=trainable, collections=collections)
        var = as_varnode(var)
        return VariableOp(opr_name, var, get_default_env()).outputs[0]

def scalar(name, value, dtype=__default_dtype__, device=None, trainable=False,
           collections=None, summary=False):

    collections = extend_collection_list(collections, TArtGraphKeys.SCALAR_VARIABLES, tf.GraphKeys.GLOBAL_VARIABLES)
    value = float(value)
    var = variable(name, value, shape=0, dtype=dtype, device=device, trainable=trainable, collections=collections)
    if summary:
        from .. import summary
        summary.scalar(name, var)
    return var

def get_scalar(name, env=None, collection=TArtGraphKeys.SCALAR_VARIABLES):
    env = env or get_default_env()
    sym = env.find_in_collection_by_name(collection, name)
    return sym.taop

def get_scalar_value(name, env=None, collection=TArtGraphKeys.SCALAR_VARIABLES):
    env = env or get_default_env()
    sym = env.find_in_collection_by_name(collection, name)
    return sym.taop.get_value()

def set_scalar_value(name, value, env=None, collection=TArtGraphKeys.SCALAR_VARIABLES):
    env = env or get_default_env()
    sym = env.find_in_collection_by_name(collection, name)
    return sym.taop.set_value(value)

@wrap_varnode_func
def constant(value, shape=None, dtype=None, name='const', device=None, verify_shape=False):
    with device_context(device):
        return tf.constant(value, dtype=dtype, shape=shape, name=name, verify_shape=verify_shape)

def ensure_variable(name, value_or_intializer, *args, **kwargs):
    if not isinstance(value_or_intializer, __valid_tensor_types__):
        return variable(name, value_or_intializer, *args, **kwargs)
    return value_or_intializer
<commit_msg>[dev-mjy] O.placeholder now automatically add collection<commit_after># -*- coding:utf8 -*-
# File   : netsrc.py
# Author : Jiayuan Mao
# Email  : maojiayuan@gmail.com
# Date   : 12/31/16
#
# This file is part of TensorArtist

import numpy as np
import tensorflow as tf

from ._defaults import __default_dtype__
from .helper import device_context, wrap_varnode_func, wrap_named_op, unique_opr_name
from ..graph.env import get_default_env
from ..graph.node import as_varnode, OprNode, __valid_tensor_types__
from ..tfutils import assign_variable, fetch_variable, TArtGraphKeys, extend_collection_list
from ...core.utils.meta import assert_notnone

__all__ = ['placeholder', 'variable', 'scalar', 'constant', 'ensure_variable',
           'get_scalar', 'get_scalar_value', 'set_scalar_value']

@wrap_varnode_func
def placeholder(name, shape=None, dtype=__default_dtype__, device=None):
    with device_context(device):
        var = as_varnode(tf.placeholder(name=name, shape=shape, dtype=dtype))
        tf.add_to_collection(TArtGraphKeys.PLACEHOLDERS, var)
        return var

class VariableOp(OprNode):
    def __init__(self, name, output, owner_env):
        super().__init__(name, [], [output])
        self._owner_env = owner_env

    def get_value(self):
        var = self.outputs[0].impl
        return fetch_variable(var, self._owner_env.session)

    def set_value(self, value, use_locking=False):
        var = self.outputs[0].impl
        assign_variable(var, value, self._owner_env.session, use_locking=use_locking)
        return self

@wrap_named_op(use_scope=False)
@wrap_varnode_func
def variable(name, value_or_initializer, shape=None, dtype=__default_dtype__, device=None, trainable=True,
             collections=None):
    opr_name = unique_opr_name(name)

    with device_context(device):
        if isinstance(value_or_initializer, (np.ndarray, float, int)):
            if type(value_or_initializer) is float:
                dtype = dtype or tf.float32
            elif type(value_or_initializer) is int:
                dtype = dtype or tf.int32
            var = tf.Variable(initial_value=value_or_initializer, trainable=trainable, name=name, dtype=dtype,
                              collections=collections)
        else:
            assert_notnone(shape, name='shape')
            var = tf.get_variable(name, shape=shape, dtype=dtype, initializer=value_or_initializer,
                                  trainable=trainable, collections=collections)
        var = as_varnode(var)
        return VariableOp(opr_name, var, get_default_env()).outputs[0]

def scalar(name, value, dtype=__default_dtype__, device=None, trainable=False,
           collections=None, summary=False):

    collections = extend_collection_list(collections, TArtGraphKeys.SCALAR_VARIABLES, tf.GraphKeys.GLOBAL_VARIABLES)
    value = float(value)
    var = variable(name, value, shape=0, dtype=dtype, device=device, trainable=trainable, collections=collections)
    if summary:
        from .. import summary
        summary.scalar(name, var)
    return var

def get_scalar(name, env=None, collection=TArtGraphKeys.SCALAR_VARIABLES):
    env = env or get_default_env()
    sym = env.find_in_collection_by_name(collection, name)
    return sym.taop

def get_scalar_value(name, env=None, collection=TArtGraphKeys.SCALAR_VARIABLES):
    env = env or get_default_env()
    sym = env.find_in_collection_by_name(collection, name)
    return sym.taop.get_value()

def set_scalar_value(name, value, env=None, collection=TArtGraphKeys.SCALAR_VARIABLES):
    env = env or get_default_env()
    sym = env.find_in_collection_by_name(collection, name)
    return sym.taop.set_value(value)

@wrap_varnode_func
def constant(value, shape=None, dtype=None, name='const', device=None, verify_shape=False):
    with device_context(device):
        return tf.constant(value, dtype=dtype, shape=shape, name=name, verify_shape=verify_shape)

def ensure_variable(name, value_or_intializer, *args, **kwargs):
    if not isinstance(value_or_intializer, __valid_tensor_types__):
        return variable(name, value_or_intializer, *args, **kwargs)
    return value_or_intializer
<|endoftext|>


<commit_before>#!/usr/bin/python

# $Id: tcp_half_closed_srv.py 143 2010-08-20 09:15:53Z m.lukaszuk $
from fcntl import fcntl,F_SETFL
from OpenSSL import SSL
from select import select
import socket, sys, os, pwd, grp

phost = ''
pport = 443
dhost = '127.0.0.1'
dport = 80

def deamonsetup(uid_name='nobody', gid_name='nogroup'):

  os.chdir("/")

  if os.getuid() != 0:
    # We're not root so, like, whatever dude
    return

  # Get the uid/gid from the name
  running_uid = pwd.getpwnam(uid_name).pw_uid
  running_gid = grp.getgrnam(gid_name).gr_gid

  # Try setting the new uid/gid
  os.setgid(running_gid)
  os.setuid(running_uid)

  # Remove group privileges
  #os.setgroups([])

  # Ensure a very conservative umask
  old_umask = os.umask(077)

# main data exchnage function
def exchange(s,c):
  # input:
  # s - ssl socket object
  # c - normal socket object
  # return:
  # nothing :)

  # setting every descriptor to be non blocking
  #fcntl(s, F_SETFL, os.O_NONBLOCK)
  #s.setblocking(0)
  #fcntl(c, F_SETFL, os.O_NONBLOCK)

  s_recv = s.recv
  s_send = s.sendall
  c_recv = c.recv
  c_send = c.send

  while 1:
    toread,[],[] = select([c,s],[],[],30)
    [],towrite,[] = select([],[c,s],[],30)

    if c in towrite and s in toread:
      try:
        data = s_recv(4096)
      except SSL.WantReadError:
        data = ''
      except SSL.SysCallError:
        s.sock_shutdown(socket.SHUT_RDWR)
        c.shutdown(socket.SHUT_RDWR)
        break

      if len(data) > 0:
        c_send(data)

    elif c in toread and s in towrite:

      data = c_recv(4096)
      if len(data) == 0:
        c.shutdown(socket.SHUT_RDWR)
        s.sock_shutdown(socket.SHUT_RDWR)
        break

      else:
        s_send(data)

##### main crap
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_CORK, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((phost, pport))
s.listen(1)

deamonsetup()

### SSL context
ctx = SSL.Context(SSL.SSLv3_METHOD)
ctx.use_privatekey_file('/usr/local/certs/server.key')
ctx.use_certificate_file('/usr/local/certs/server.crt')
ctx.set_cipher_list('HIGH')

while (True):
    conn, addr = s.accept()

    try:
      os.waitpid(0,os.WNOHANG)
    except OSError:
      pass

    pid = os.fork()

    if pid == 0:

      # let's add SSL to this socket
      ssl = SSL.Connection(ctx,conn)
      ssl.setblocking(True)
      ssl.set_accept_state()
      ssl.do_handshake()

      data = ssl.recv(1024)

      ssl.setblocking(False)

      if data and 'qwerty' in data:
        dport = 22

      print "[c] connecting to "+str(dhost)+":"+str(dport)+" from "+str(addr)
      proxy = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      proxy.setsockopt(socket.IPPROTO_TCP, socket.TCP_CORK,1)

      try:
        proxy.connect((dhost, dport))
      except socket.error:
        proxy.close()
        conn.close()
        sys.exit()

      print "[c] connected to "+str(dhost)+":"+str(dport)+" from "+str(addr)

      if dport == 80:
        proxy.send(data)

      fcntl(proxy, F_SETFL, os.O_NONBLOCK)

      print "[c] going into exchange between "+str(dhost)+":"+str(dport)+" and "+str(addr)

      exchange(ssl,proxy)
      break

    else:
      print "[p] Child forked "+str(pid)
<commit_msg><commit_after>#!/usr/bin/python

# $Id: tcp_half_closed_srv.py 143 2010-08-20 09:15:53Z m.lukaszuk $
from fcntl import fcntl,F_SETFL
from OpenSSL import SSL
from select import select
import socket, sys, os, pwd, grp

phost = ''
pport = 443
dhost = '127.0.0.1'
dport = 80

def deamonsetup(uid_name='nobody', gid_name='nogroup'):

  os.chdir("/")

  if os.getuid() != 0:
    # We're not root so, like, whatever dude
    return

  # Get the uid/gid from the name
  running_uid = pwd.getpwnam(uid_name).pw_uid
  running_gid = grp.getgrnam(gid_name).gr_gid

  # Try setting the new uid/gid
  os.setgid(running_gid)
  os.setuid(running_uid)

  # Remove group privileges
  #os.setgroups([])

  # Ensure a very conservative umask
  old_umask = os.umask(077)

# main data exchnage function
def exchange(s,c):
  # input:
  # s - ssl socket object
  # c - normal socket object
  # return:
  # nothing :)

  # setting every descriptor to be non blocking
  #fcntl(s, F_SETFL, os.O_NONBLOCK)
  #s.setblocking(0)
  #fcntl(c, F_SETFL, os.O_NONBLOCK)

  s_recv = s.recv
  s_send = s.sendall
  c_recv = c.recv
  c_send = c.send

  while 1:
    toread,[],[] = select([c,s],[],[],30)
    [],towrite,[] = select([],[c,s],[],30)

    if c in towrite and s in toread:
      try:
        data = s_recv(4096)
      except SSL.WantReadError:
        data = ''
      except SSL.SysCallError,SSL.ZeroReturnError:
        s.sock_shutdown(socket.SHUT_RDWR)
        c.shutdown(socket.SHUT_RDWR)
        break

      if len(data) > 0:
        c_send(data)

    elif c in toread and s in towrite:

      data = c_recv(4096)
      if len(data) == 0:
        c.shutdown(socket.SHUT_RDWR)
        s.sock_shutdown(socket.SHUT_RDWR)
        break

      else:
        s_send(data)

##### main crap
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_CORK, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((phost, pport))
s.listen(1)

deamonsetup()

### SSL context
ctx = SSL.Context(SSL.SSLv3_METHOD)
ctx.use_privatekey_file('/usr/local/certs/server.key')
ctx.use_certificate_file('/usr/local/certs/server.crt')
ctx.set_cipher_list('HIGH')

while (True):
    conn, addr = s.accept()

    try:
      os.waitpid(0,os.WNOHANG)
    except OSError:
      pass

    pid = os.fork()

    if pid == 0:

      # let's add SSL to this socket
      ssl = SSL.Connection(ctx,conn)
      ssl.setblocking(True)
      ssl.set_accept_state()
      ssl.do_handshake()

      data = ssl.recv(1024)

      ssl.setblocking(False)

      if data and 'qwerty' in data:
        dport = 22

      print "[c] connecting to "+str(dhost)+":"+str(dport)+" from "+str(addr)
      proxy = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      proxy.setsockopt(socket.IPPROTO_TCP, socket.TCP_CORK,1)

      try:
        proxy.connect((dhost, dport))
      except socket.error:
        proxy.close()
        conn.close()
        sys.exit()

      print "[c] connected to "+str(dhost)+":"+str(dport)+" from "+str(addr)

      if dport == 80:
        proxy.send(data)

      fcntl(proxy, F_SETFL, os.O_NONBLOCK)

      print "[c] going into exchange between "+str(dhost)+":"+str(dport)+" and "+str(addr)

      exchange(ssl,proxy)
      break

    else:
      print "[p] Child forked "+str(pid)
<|endoftext|>


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

from urllib.parse import quote as urlquote

class DatabaseAPI:
    ####
    ## Database API
    ##

    def list_databases(self):
        """
        TODO: add docstring
        => [name:str]
        """
        with self.get("/v3/database/list") as res:
            code, body = res.status, res.read()
            if code != 200:
                self.raise_error("List databases failed", res, body)
            js = self.checked_json(body, ["databases"])
            result = {}
            for m in js["databases"]:
                name = m.get("name")
                m = dict(m)
                m["created_at"] = self._parsedate(
                    self.get_or_else(m, "created_at", "1970-01-01T00:00:00Z"),
                    "%Y-%m-%dT%H:%M:%SZ",
                )
                m["updated_at"] = self._parsedate(
                    self.get_or_else(m, "updated_at", "1970-01-01T00:00:00Z"),
                    "%Y-%m-%dT%H:%M:%SZ",
                )
                m["org_name"] = None  # set None to org for API copatibility
                result[name] = m
            return result

    def delete_database(self, db):
        """
        TODO: add docstring
        => True
        """
        with self.post("/v3/database/delete/%s" % urlquote(str(db))) as res:
            code, body = res.status, res.read()
            if code != 200:
                self.raise_error("Delete database failed", res, body)
            return True

    def create_database(self, db, params=None):
        """
        TODO: add docstring
        => True
        """
        params = {} if params is None else params
        with self.post("/v3/database/create/%s" % urlquote(str(db)), params) as res:
            code, body = res.status, res.read()
            if code != 200:
                self.raise_error("Create database failed", res, body)
            return True
<commit_msg>doc: add docstring for DatabaseAPI<commit_after>#!/usr/bin/env python

from urllib.parse import quote as urlquote

class DatabaseAPI:
    ####
    ## Database API
    ##

    def list_databases(self):
        """Get the list of all the databases of the account.

        Returns:
            dict: Detailed database information. Each key of the dict is database name.
        """
        with self.get("/v3/database/list") as res:
            code, body = res.status, res.read()
            if code != 200:
                self.raise_error("List databases failed", res, body)
            js = self.checked_json(body, ["databases"])
            result = {}
            for m in js["databases"]:
                name = m.get("name")
                m = dict(m)
                m["created_at"] = self._parsedate(
                    self.get_or_else(m, "created_at", "1970-01-01T00:00:00Z"),
                    "%Y-%m-%dT%H:%M:%SZ",
                )
                m["updated_at"] = self._parsedate(
                    self.get_or_else(m, "updated_at", "1970-01-01T00:00:00Z"),
                    "%Y-%m-%dT%H:%M:%SZ",
                )
                m["org_name"] = None  # set None to org for API copatibility
                result[name] = m
            return result

    def delete_database(self, db):
        """Delete a database.

        Args:
            db (str): Target database name.
        Returns:
            bool: `True` if succeeded.
        """
        with self.post("/v3/database/delete/%s" % urlquote(str(db))) as res:
            code, body = res.status, res.read()
            if code != 200:
                self.raise_error("Delete database failed", res, body)
            return True

    def create_database(self, db, params=None):
        """Create a new database with the given name.

        Args:
            db (str): Target database name.
            params (dict): Extra parameters.
        Returns:
            bool: `True` if succeeded.
        """
        params = {} if params is None else params
        with self.post("/v3/database/create/%s" % urlquote(str(db)), params) as res:
            code, body = res.status, res.read()
            if code != 200:
                self.raise_error("Create database failed", res, body)
            return True
<|endoftext|>


<commit_before>from django.core.urlresolvers import reverse
from django.test import TestCase

from survey.models import User, Item

class StartPageTest(TestCase):
    def post_survey(self, values):
        self.client.get(reverse('survey:survey'))
        self.client.post(reverse('survey:record'), values)

    def get_stored_item(self, key):
        usercode = self.client.cookies['usercode']
        u = User.objects.get(code=usercode.value)

        responses = Item.objects.filter(user_id=u.id)
        self.assertTrue(len(responses) == 1)

        try:
            response = Item.objects.get(user_id=u.id, key=key)
        except Item.DoesNotExist:
            return None
        return response

    def test_front_page_displays(self):
        response = self.client.get(reverse('survey:survey'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "University of Manchester")

    def test_cannot_complete_survey_twice(self):
        response = self.client.get(reverse('survey:survey'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "University of Manchester")

        self.client.post(reverse('survey:record'))
        self.assertIsNotNone(self.client.cookies['usercode'])

        response = self.client.get(reverse('survey:survey'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "already completed")

    def test_can_override_completion_check(self):
        response = self.client.get(reverse('survey:survey'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "University of Manchester")

        self.client.post(reverse('survey:record'))
        self.assertIsNotNone(self.client.cookies['usercode'])

        response = self.client.get(reverse('survey:survey'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "already completed")

        response = self.client.get(reverse('survey:survey'), {'ignorecookie': 1})
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "already completed")

        # override should only work on staging sites
        with self.settings(DEBUG='1'):
            response = self.client.get(reverse('survey:survey'), {'ignorecookie': 1})
            self.assertEqual(response.status_code, 200)
            self.assertContains(response, "University of Manchester")

    def test_completing_survey_creates_user(self):
        self.post_survey({})
        self.assertIsNotNone(self.client.cookies['usercode'].value)

        usercode = self.client.cookies['usercode']
        u = User.objects.get(code=usercode.value)
        self.assertIsNotNone(u.id)

    def test_survey_is_recorded(self):
        self.post_survey({'1':'a'})

        usercode = self.client.cookies['usercode']
        u = User.objects.get(code=usercode.value)

        responses = Item.objects.filter(user_id=u.id)
        self.assertTrue(len(responses) == 1)

        response = Item.objects.filter(user_id=u.id).filter(key='1').filter(value='a')
        self.assertTrue(len(responses) == 1)

    def test_multi_value_answers_recorded(self):
        self.post_survey({'1':['b','d']})
        stored = self.get_stored_item('1')
        self.assertEqual('b,d', stored.value)

    def test_email_is_recorded_in_user(self):
        self.post_survey({'1':'a', 'email': 'test@example.org'})

        stored = self.get_stored_item('email')
        self.assertIsNone(stored)

        usercode = self.client.cookies['usercode']
        u = User.objects.get(code=usercode.value)
        self.assertEqual('test@example.org', u.email)

    def test_email_is_blank_if_not_provided(self):
        self.post_survey({'1':'a'})

        stored = self.get_stored_item('email')
        self.assertIsNone(stored)

        usercode = self.client.cookies['usercode']
        u = User.objects.get(code=usercode.value)
        self.assertIsNone(u.email)

<commit_msg>better name for survey test case<commit_after>from django.core.urlresolvers import reverse
from django.test import TestCase

from survey.models import User, Item

class SurveyTest(TestCase):
    def post_survey(self, values):
        self.client.get(reverse('survey:survey'))
        self.client.post(reverse('survey:record'), values)

    def get_stored_item(self, key):
        usercode = self.client.cookies['usercode']
        u = User.objects.get(code=usercode.value)

        responses = Item.objects.filter(user_id=u.id)
        self.assertTrue(len(responses) == 1)

        try:
            response = Item.objects.get(user_id=u.id, key=key)
        except Item.DoesNotExist:
            return None
        return response

    def test_front_page_displays(self):
        response = self.client.get(reverse('survey:survey'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "University of Manchester")

    def test_cannot_complete_survey_twice(self):
        response = self.client.get(reverse('survey:survey'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "University of Manchester")

        self.client.post(reverse('survey:record'))
        self.assertIsNotNone(self.client.cookies['usercode'])

        response = self.client.get(reverse('survey:survey'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "already completed")

    def test_can_override_completion_check(self):
        response = self.client.get(reverse('survey:survey'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "University of Manchester")

        self.client.post(reverse('survey:record'))
        self.assertIsNotNone(self.client.cookies['usercode'])

        response = self.client.get(reverse('survey:survey'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "already completed")

        response = self.client.get(reverse('survey:survey'), {'ignorecookie': 1})
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "already completed")

        # override should only work on staging sites
        with self.settings(DEBUG='1'):
            response = self.client.get(reverse('survey:survey'), {'ignorecookie': 1})
            self.assertEqual(response.status_code, 200)
            self.assertContains(response, "University of Manchester")

    def test_completing_survey_creates_user(self):
        self.post_survey({})
        self.assertIsNotNone(self.client.cookies['usercode'].value)

        usercode = self.client.cookies['usercode']
        u = User.objects.get(code=usercode.value)
        self.assertIsNotNone(u.id)

    def test_survey_is_recorded(self):
        self.post_survey({'1':'a'})

        usercode = self.client.cookies['usercode']
        u = User.objects.get(code=usercode.value)

        responses = Item.objects.filter(user_id=u.id)
        self.assertTrue(len(responses) == 1)

        response = Item.objects.filter(user_id=u.id).filter(key='1').filter(value='a')
        self.assertTrue(len(responses) == 1)

    def test_multi_value_answers_recorded(self):
        self.post_survey({'1':['b','d']})
        stored = self.get_stored_item('1')
        self.assertEqual('b,d', stored.value)

    def test_email_is_recorded_in_user(self):
        self.post_survey({'1':'a', 'email': 'test@example.org'})

        stored = self.get_stored_item('email')
        self.assertIsNone(stored)

        usercode = self.client.cookies['usercode']
        u = User.objects.get(code=usercode.value)
        self.assertEqual('test@example.org', u.email)

    def test_email_is_blank_if_not_provided(self):
        self.post_survey({'1':'a'})

        stored = self.get_stored_item('email')
        self.assertIsNone(stored)

        usercode = self.client.cookies['usercode']
        u = User.objects.get(code=usercode.value)
        self.assertIsNone(u.email)
<|endoftext|>


<commit_before># svntrac
#
# Copyright (C) 2003 Xyche Software
# Copyright (C) 2003 Jonas Borgstrm <jonas@xyche.com>
#
# svntrac is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# svntrac is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: Jonas Borgstrm <jonas@xyche.com>

import re
import time
import os
import StringIO
import auth
import perm
from Href import href
from Module import Module
from db import get_connection
from util import *

class Formatter:
    """
    A simple Wiki formatter
    """
    _url_re = r'(((new|file|(ht|f)tp)s?://))([a-z0-9_-]+:[a-z0-9_-]+\@)?([a-z0-9]+(\-*[a-z0-9]+)*\.)+[a-z]{2,7}(/~?[a-z0-9_\.%\-]+)?(/[a-z0-9_\.%\-]+)*(/?[a-z0-9_\%\-]*(\.[a-z0-9]+)?(\#[a-zA-Z0-9_\.]+)?)(\?([a-z0-9_\.\%\-]+)\=[a-z0-9_\.\%\/\-]*)?(&([a-z0-9_\.\%\-]+)\=[a-z0-9_\.\%\/\-]*)?(#[a-z0-9]+)?'

    _rules = r"""(?:(?P<bold>''')""" \
             r"""|(?P<italic>'')""" \
             r"""|(?P
-{5,})""" \
             r"""|(?P<heading>^\s*(?P<hdepth>=+)\s.*\s(?P=hdepth)$)""" \
             r"""|(?P<listitem>^(?P<ldepth>\s+)(?:\*|[0-9]+\.) )""" \
             r"""|(?P<wikilink>(^|(?<=[^A-Za-z]))[A-Z][a-z]*(?:[A-Z][a-z]+)+)""" \
             r"""|(?P<indent>^(?P<idepth>\s+)(?=[^\s]))""" \
             r"""|(?P<imgurl>%(url_re)s(\.png|\.jpg|\.jpeg|\.gif))""" \
             r"""|(?P<url>%(url_re)s)""" \
             r"""|(?P<fancylink>\[(?P<fancyurl>%(url_re)s) (?P<linkname>.*?)\])""" \
             r"""|(?P<underline>__))""" % { 'url_re': _url_re}

    # RE patterns used by other patterna
    _helper_patterns = ('idepth', 'ldepth', 'hdepth', 'fancyurl', 'linkname')

    def _bold_formatter(self, match, fullmatch):
        self._is_bold = not self._is_bold
        return ['', ''][self._is_bold]

    def _italic_formatter(self, match, fullmatch):
        self._is_italic = not self._is_italic
        return ['', ''][self._is_italic]

    def _hr_formatter(self, match, fullmatch):
        return '
'

    def _underline_formatter(self, match, fullmatch):
        self._is_underline = not self._is_underline
        return ['', ''][self._is_underline]

    def _heading_formatter(self, match, fullmatch):
        depth = min(len(fullmatch.group('hdepth')), 5)
        self.is_heading = 1
        return '<h%d>%s</h%d>' % (depth, match[depth + 1:len(match) - depth - 1], depth)

    def _wikilink_formatter(self, match, fullmatch):
        return '%s' % (href.wiki(match), match)

    def _imgurl_formatter(self, match, fullmatch):
        return '' % match

    def _url_formatter(self, match, fullmatch):
        return '%s' % (match, match)

    def _fancylink_formatter(self, match, fullmatch):
        link = fullmatch.group('fancyurl')
        name = fullmatch.group('linkname')
        return '%s' % (link, name)

    def _set_list_depth(self, depth, type):
        self._in_list = depth > 0
        current_depth = len(self._list_stack)
        diff = depth - current_depth
        if diff > 0:
            for i in range(diff):
                self.out.write('<%s>' % type)
                self._list_stack.append('</%s>' % type)
        elif diff < 0:
            for i in range(-diff):
                self.out.write(self._list_stack.pop())
            # If the list type changes...
            if self._list_stack != [] and self._list_stack[0][2:4] != type:
                self.out.write(self._list_stack.pop())
                self.out.write('<%s>' % type)
                self._list_stack.append('</%s>' % type)
        # If the list type changes...
        elif self._list_stack != [] and self._list_stack[0][2:4] != type:
            self.out.write(self._list_stack.pop())
            self.out.write('<%s>' % type)
            self._list_stack.append('</%s>' % type)

    def _listitem_formatter(self, match, fullmatch):
        depth = int((len(fullmatch.group('ldepth')) + 1) / 2)
        #self.out.write('depth:%d' % depth)
        type = ['ol', 'ul'][match[depth * 2 - 1] == '*']
        self._li_open = 1
        self._set_list_depth(depth, type)
        return '
'
        #return '
%s
' % match[depth * 2 + 1:]

    def _indent_formatter(self, match, fullmatch):
        depth = int((len(fullmatch.group('idepth')) + 1) / 2)
        #self.out.write('depth:%d' % depth)
        self._set_list_depth(depth, 'ul')
        return ' '
        #return '
%s
' % match[depth * 2 + 1:]

    def replace(self, fullmatch):
        for type, match in fullmatch.groupdict().items():
            if match and not type in Formatter._helper_patterns:
                return getattr(self, '_' + type + '_formatter')(match, fullmatch)

    def format(self, text, out):
        self.out = out
        rules = re.compile(Formatter._rules)
        p_open = 0
        self.is_heading = 0
        self._li_open = 0
        self._list_stack = []
        self._in_pre = 0
        for line in text.splitlines():
            self._is_bold = 0
            self._is_italic = 0
            self._is_underline = 0
            self._in_list = 0
            line = escape(line)
            result = re.sub(rules, self.replace, line)
            # close any open list item
            if self._li_open:
                self._li_open = 0
                result = result + '
'
            # close the paragraph when a heading starts
            # or on an empty line
            if p_open and self._list_stack != []:
                out.write ('
')
                p_open = 0

            if not self._in_list:
                self._set_list_depth(0, None)
            if p_open and (self.is_heading or result == ''):
                out.write ('
')
                p_open = 0
            elif not p_open and not self.is_heading and not \
                     self._in_list and result != '':
                p_open = 1
                out.write ('
')

            out.write(result)
            self.is_heading = 0
        # clean up before we are done
        self._set_list_depth(0, None)
        if p_open:
            out.write('
')

def wiki_to_html(wikitext):
    out = StringIO.StringIO()
    Formatter().format(wikitext, out)
    return out.getvalue()

class Page:
    def __init__(self, name, version):

        self.name = name
        cnx = get_connection ()
        cursor = cnx.cursor ()
        if version:
            cursor.execute ('SELECT version, text FROM wiki '
                            'WHERE name=%s AND version=%s',
                            name, version)
        else:
            cursor.execute ('SELECT version, text FROM wiki '
                            'WHERE name=%s ORDER BY version DESC LIMIT 1', name)
        row = cursor.fetchone()
        if row:
            self.new = 0
            self.version = int(row[0])
            self.text = row[1]
        else:
            self.version = 0
            self.text = 'describe %s here' % name
            self.new = 1

    def set_content (self, text):
        self.text = text
        self.version = self.version + 1

    def commit (self):
        if self.new:
            perm.assert_permission (perm.WIKI_CREATE)
        else:
            perm.assert_permission (perm.WIKI_MODIFY)
        cnx = get_connection ()
        cursor = cnx.cursor ()
        cursor.execute ('SELECT MAX(version)+1 FROM '
                        '(SELECT version FROM wiki WHERE name=%s '
                        'UNION ALL SELECT 0 as version)', self.name)
        row = cursor.fetchone()
        new_version = int(row[0])

        cursor.execute ('INSERT INTO WIKI '
                        '(name, version, time, author, ipnr, locked, text) '
                        'VALUES (%s, %s, %s, %s, %s, %s, %s)',
                        self.name, new_version, int(time.time()),
                        auth.get_authname(), os.getenv('REMOTE_ADDR'),
                        0, self.text)
        cnx.commit ()

    def render_edit(self, out):
        perm.assert_permission (perm.WIKI_MODIFY)
        out.write ('
source
')
        out.write ('<form action="%s" method="POST">' % get_cgi_name())
        out.write ('<input type="hidden" name="page" value="%s">' % self.name)
        out.write ('<input type="hidden" name="mode" value="wiki">')
        out.write ('<textarea name="text" rows="15" cols="80">')
        out.write(escape(self.text))
        out.write ('</textarea>
')
        out.write ('<input type="submit" name="action" value="preview"> ')
        out.write ('<input type="submit" name="action" value="save changes">')
        out.write ('</form>')

    def render_view(self, out, edit_button=1):
        self.render_history(out)
        perm.assert_permission (perm.WIKI_VIEW)
        out.write ('
')
        #format_wiki(self.text, out)
        Formatter().format(self.text, out)
        out.write ('

')
        if edit_button and perm.has_permission (perm.WIKI_MODIFY):
            out.write ('<form action="%s" method="POST">' % get_cgi_name())
            out.write ('<input type="hidden" name="mode" value="wiki">')
            out.write ('<input type="hidden" name="page" value="%s">' % self.name)
            out.write ('<input type="submit" name="action" value=" edit page ">')
            out.write ('</form>')

    def render_preview (self, out):
        perm.assert_permission (perm.WIKI_MODIFY)

        self.render_edit (out)
        out.write ('
preview
')
        self.render_view (out, edit_button=0)

    def render_history (self, out):
        cnx = get_connection ()
        cursor = cnx.cursor ()

        cursor.execute ('SELECT version, time, author, ipnr FROM wiki '
                        'WHERE name=%s ORDER BY version DESC', self.name)

        out.write ('
'
                   'show/hide history'
                   '
')
        out.write ('
')
        out.write ('
version	'
                   'time	author	ipnr
')
	while 1:
	    row = cursor.fetchone()
	    if row == None:
		break
		   #        for row in cursor:
            t = int(row[1])
            if t:
                time_str = time.strftime('%F', time.localtime(t))
            else:
                time_str = ''
            out.write ('
%s	%s	'
                       '%s	%s
'
                       % (href.wiki(self.name, row[0]), row[0], time_str,
                          row[2], row[3]))
        out.write ('
')

class Wiki(Module):
    template_name = 'wiki.template'

    def render(self):
        if self.args.has_key('page'):
            name = self.args['page']
        else:
            name = 'WikiStart'
        if self.args.has_key('version'):
            version = self.args['version']
        else:
            version = 0

        if self.args.has_key('action'):
            action = self.args['action']
        else:
            action = 'view'

        page = Page(name, version)

        if self.args.has_key('text'):
            page.set_content (self.args['text'])

        out = StringIO.StringIO()
        if action == 'save changes':
            page.commit ()
            redirect (href.wiki(page.name))
        elif action == ' edit page ':
            out.write ('
edit %s
' %
                       (href.wiki(page.name), page.name))
            page.render_edit (out)
            self.namespace['title'] = 'wiki - edit'
        elif action == 'preview':
            out.write ('
edit %s
' %
                       (href.wiki(page.name), page.name))
            page.render_preview (out)
            self.namespace['title'] = 'wiki - preview'
        else:
            page.render_view (out)
            self.namespace['title'] = 'wiki - view'
        self.namespace['content'] = out.getvalue()

test_in = '''
 * Foo
   * Foo 2
 1. Foo 3
=== FooBar ===
  Hoj
  Hoj2
Hoj3
'''
test_out = '''

Foo

Foo 2

Foo 3

FooBar

 Hoj Hoj2

Hoj3
'''

def test():
    result = StringIO.StringIO()
    Formatter().format(test_in, result)
    if result.getvalue() != test_out:
        print 'now:', result.getvalue()
        print 'correct:', test_out

if __name__ == '__main__':
    test()
<commit_msg>Code cleanup<commit_after># svntrac
#
# Copyright (C) 2003 Xyche Software
# Copyright (C) 2003 Jonas Borgstrm <jonas@xyche.com>
#
# svntrac is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# svntrac is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: Jonas Borgstrm <jonas@xyche.com>

import re
import time
import os
import StringIO
import auth
import perm
from Href import href
from Module import Module
from db import get_connection
from util import *

class Formatter:
    """
    A simple Wiki formatter
    """
    _url_re = r'(((new|file|(ht|f)tp)s?://))([a-z0-9_-]+:[a-z0-9_-]+\@)?([a-z0-9]+(\-*[a-z0-9]+)*\.)+[a-z]{2,7}(/~?[a-z0-9_\.%\-]+)?(/[a-z0-9_\.%\-]+)*(/?[a-z0-9_\%\-]*(\.[a-z0-9]+)?(\#[a-zA-Z0-9_\.]+)?)(\?([a-z0-9_\.\%\-]+)\=[a-z0-9_\.\%\/\-]*)?(&([a-z0-9_\.\%\-]+)\=[a-z0-9_\.\%\/\-]*)?(#[a-z0-9]+)?'

    _rules = r"""(?:(?P<bold>''')""" \
             r"""|(?P<italic>'')""" \
             r"""|(?P
-{5,})""" \
             r"""|(?P<heading>^\s*(?P<hdepth>=+)\s.*\s(?P=hdepth)$)""" \
             r"""|(?P<listitem>^(?P<ldepth>\s+)(?:\*|[0-9]+\.) )""" \
             r"""|(?P<wikilink>(^|(?<=[^A-Za-z]))[A-Z][a-z]*(?:[A-Z][a-z]+)+)""" \
             r"""|(?P<indent>^(?P<idepth>\s+)(?=[^\s]))""" \
             r"""|(?P<imgurl>%(url_re)s(\.png|\.jpg|\.jpeg|\.gif))""" \
             r"""|(?P<url>%(url_re)s)""" \
             r"""|(?P<fancylink>\[(?P<fancyurl>%(url_re)s) (?P<linkname>.*?)\])""" \
             r"""|(?P<underline>__))""" % { 'url_re': _url_re}

    # RE patterns used by other patterna
    _helper_patterns = ('idepth', 'ldepth', 'hdepth', 'fancyurl', 'linkname')

    def _bold_formatter(self, match, fullmatch):
        self._is_bold = not self._is_bold
        return ['', ''][self._is_bold]

    def _italic_formatter(self, match, fullmatch):
        self._is_italic = not self._is_italic
        return ['', ''][self._is_italic]

    def _hr_formatter(self, match, fullmatch):
        return '
'

    def _underline_formatter(self, match, fullmatch):
        self._is_underline = not self._is_underline
        return ['', ''][self._is_underline]

    def _heading_formatter(self, match, fullmatch):
        depth = min(len(fullmatch.group('hdepth')), 5)
        self.is_heading = 1
        return '<h%d>%s</h%d>' % (depth, match[depth + 1:len(match) - depth - 1], depth)

    def _wikilink_formatter(self, match, fullmatch):
        return '%s' % (href.wiki(match), match)

    def _imgurl_formatter(self, match, fullmatch):
        return '' % match

    def _url_formatter(self, match, fullmatch):
        return '%s' % (match, match)

    def _fancylink_formatter(self, match, fullmatch):
        link = fullmatch.group('fancyurl')
        name = fullmatch.group('linkname')
        return '%s' % (link, name)

    def _set_list_depth(self, depth, type):
        self._in_list = depth > 0
        current_depth = len(self._list_stack)
        diff = depth - current_depth
        if diff > 0:
            for i in range(diff):
                self.out.write('<%s>' % type)
                self._list_stack.append('</%s>' % type)
        elif diff < 0:
            for i in range(-diff):
                self.out.write(self._list_stack.pop())
            # If the list type changes...
            if self._list_stack != [] and self._list_stack[0][2:4] != type:
                self.out.write(self._list_stack.pop())
                self.out.write('<%s>' % type)
                self._list_stack.append('</%s>' % type)
        # If the list type changes...
        elif self._list_stack != [] and self._list_stack[0][2:4] != type:
            self.out.write(self._list_stack.pop())
            self.out.write('<%s>' % type)
            self._list_stack.append('</%s>' % type)

    def _listitem_formatter(self, match, fullmatch):
        depth = int((len(fullmatch.group('ldepth')) + 1) / 2)
        #self.out.write('depth:%d' % depth)
        type = ['ol', 'ul'][match[depth * 2 - 1] == '*']
        self._li_open = 1
        self._set_list_depth(depth, type)
        return '
'
        #return '
%s
' % match[depth * 2 + 1:]

    def _indent_formatter(self, match, fullmatch):
        depth = int((len(fullmatch.group('idepth')) + 1) / 2)
        #self.out.write('depth:%d' % depth)
        self._set_list_depth(depth, 'ul')
        return ' '
        #return '
%s
' % match[depth * 2 + 1:]

    def replace(self, fullmatch):
        for type, match in fullmatch.groupdict().items():
            if match and not type in Formatter._helper_patterns:
                return getattr(self, '_' + type + '_formatter')(match, fullmatch)

    def format(self, text, out):
        self.out = out
        rules = re.compile(Formatter._rules)
        p_open = 0
        self.is_heading = 0
        self._li_open = 0
        self._list_stack = []
        self._in_pre = 0
        for line in text.splitlines():
            self._is_bold = 0
            self._is_italic = 0
            self._is_underline = 0
            self._in_list = 0
            line = escape(line)
            result = re.sub(rules, self.replace, line)
            # close any open list item
            if self._li_open:
                self._li_open = 0
                result = result + '
'
            # close the paragraph when a heading starts
            # or on an empty line
            if p_open and self._list_stack != []:
                out.write ('
')
                p_open = 0

            if not self._in_list:
                self._set_list_depth(0, None)
            if p_open and (self.is_heading or result == ''):
                out.write ('
')
                p_open = 0
            elif not p_open and not self.is_heading and not \
                     self._in_list and result != '':
                p_open = 1
                out.write ('
')

            out.write(result)
            self.is_heading = 0
        # clean up before we are done
        self._set_list_depth(0, None)
        if p_open:
            out.write('
')

def wiki_to_html(wikitext):
    out = StringIO.StringIO()
    Formatter().format(wikitext, out)
    return out.getvalue()

class Page:
    def __init__(self, name, version):

        self.name = name
        cnx = get_connection ()
        cursor = cnx.cursor ()
        if version:
            cursor.execute ('SELECT version, text FROM wiki '
                            'WHERE name=%s AND version=%s',
                            name, version)
        else:
            cursor.execute ('SELECT version, text FROM wiki '
                            'WHERE name=%s ORDER BY version DESC LIMIT 1', name)
        row = cursor.fetchone()
        if row:
            self.new = 0
            self.version = int(row[0])
            self.text = row[1]
        else:
            self.version = 0
            self.text = 'describe %s here' % name
            self.new = 1

    def set_content (self, text):
        self.text = text
        self.version = self.version + 1

    def commit (self):
        if self.new:
            perm.assert_permission (perm.WIKI_CREATE)
        else:
            perm.assert_permission (perm.WIKI_MODIFY)
        cnx = get_connection ()
        cursor = cnx.cursor ()
        cursor.execute ('SELECT MAX(version)+1 FROM '
                        '(SELECT version FROM wiki WHERE name=%s '
                        'UNION ALL SELECT 0 as version)', self.name)
        row = cursor.fetchone()
        new_version = int(row[0])

        cursor.execute ('INSERT INTO WIKI '
                        '(name, version, time, author, ipnr, locked, text) '
                        'VALUES (%s, %s, %s, %s, %s, %s, %s)',
                        self.name, new_version, int(time.time()),
                        auth.get_authname(), os.getenv('REMOTE_ADDR'),
                        0, self.text)
        cnx.commit ()

    def render_edit(self, out):
        perm.assert_permission (perm.WIKI_MODIFY)
        out.write ('
source
')
        out.write ('<form action="%s" method="POST">' % get_cgi_name())
        out.write ('<input type="hidden" name="page" value="%s">' % self.name)
        out.write ('<input type="hidden" name="mode" value="wiki">')
        out.write ('<textarea name="text" rows="15" cols="80">')
        out.write(escape(self.text))
        out.write ('</textarea>
')
        out.write ('<input type="submit" name="action" value="preview"> ')
        out.write ('<input type="submit" name="action" value="save changes">')
        out.write ('</form>')

    def render_view(self, out, edit_button=1):
        self.render_history(out)
        perm.assert_permission (perm.WIKI_VIEW)
        out.write ('
')
        #format_wiki(self.text, out)
        Formatter().format(self.text, out)
        out.write ('

')
        if edit_button and perm.has_permission (perm.WIKI_MODIFY):
            out.write ('<form action="%s" method="POST">' % get_cgi_name())
            out.write ('<input type="hidden" name="mode" value="wiki">')
            out.write ('<input type="hidden" name="page" value="%s">' % self.name)
            out.write ('<input type="submit" name="action" value=" edit page ">')
            out.write ('</form>')

    def render_preview (self, out):
        perm.assert_permission (perm.WIKI_MODIFY)

        self.render_edit (out)
        out.write ('
preview
')
        self.render_view (out, edit_button=0)

    def render_history (self, out):
        cnx = get_connection ()
        cursor = cnx.cursor ()

        cursor.execute ('SELECT version, time, author, ipnr FROM wiki '
                        'WHERE name=%s ORDER BY version DESC', self.name)

        out.write ('
'
                   'show/hide history'
                   '
')
        out.write ('
')
        out.write ('
version	'
                   'time	author	ipnr
')
	while 1:
	    row = cursor.fetchone()
	    if row == None:
		break
		   #        for row in cursor:
            t = int(row[1])
            if t:
                time_str = time.strftime('%F', time.localtime(t))
            else:
                time_str = ''
            out.write ('
%s	%s	'
                       '%s	%s
'
                       % (href.wiki(self.name, row[0]), row[0], time_str,
                          row[2], row[3]))
        out.write ('
')

class Wiki(Module):
    template_name = 'wiki.template'

    def render(self):
        name = dict_get_with_default(self.args, 'page', 'WikiStart')
        action = dict_get_with_default(self.args, 'action', 'view')
        version = dict_get_with_default(self.args, 'verion', 0)

        page = Page(name, version)

        if self.args.has_key('text'):
            page.set_content (self.args['text'])

        out = StringIO.StringIO()
        if action == 'save changes':
            page.commit ()
            redirect (href.wiki(page.name))
        elif action == ' edit page ':
            out.write ('
edit %s
' %
                       (href.wiki(page.name), page.name))
            page.render_edit (out)
            self.namespace['title'] = 'wiki - edit'
        elif action == 'preview':
            out.write ('
edit %s
' %
                       (href.wiki(page.name), page.name))
            page.render_preview (out)
            self.namespace['title'] = 'wiki - preview'
        else:
            page.render_view (out)
            self.namespace['title'] = 'wiki - view'
        self.namespace['content'] = out.getvalue()

###
### A simple unit test
###

test_in = '''
 * Foo
   * Foo 2
 1. Foo 3
=== FooBar ===
  Hoj
  Hoj2
Hoj3
'''
test_out = '''

Foo

Foo 2

Foo 3

FooBar

 Hoj Hoj2

Hoj3
'''

def test():
    result = StringIO.StringIO()
    Formatter().format(test_in, result)
    if result.getvalue() != test_out:
        print 'now:', result.getvalue()
        print 'correct:', test_out

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