text:
"""Main module."""
import time as _time

import numpy as _np
import scipy.signal as _scysig
import scipy.integrate as _scyint
import scipy.optimize as _scyopt

import matplotlib.pyplot as _mplt
import matplotlib.gridspec as _mgs
from matplotlib.collections import PolyCollection as _PolyCollection

from siriuspy.devices import BunchbyBunch, PowerSupplyPU, EGTriggerPS

from ..utils import MeasBaseClass as _BaseClass, \
    ParamsBaseClass as _ParamsBaseClass, \
    ThreadedMeasBaseClass as _ThreadBaseClass
from .. import asparams as _asparams

class UtilClass:
    """."""

    @staticmethod
    def get_data(bbb, acqtype):
        """Get Raw data to file."""
        acq = bbb.sram if acqtype in 'SRAM' else bbb.bram
        rawdata = acq.data_raw.reshape(
            (-1, bbb.info.harmonic_number)).T.copy()
        dtime = acq.downsample / bbb.info.revolution_freq_nom
        return dict(
            rawdata=rawdata,
            time=_np.arange(rawdata.shape[1]) * dtime * 1000,
            dft_freq=_np.fft.fftfreq(rawdata.shape[1], d=dtime),
            stored_current=bbb.dcct.current,
            timestamp=_time.time(),
            cavity_data=UtilClass.get_cavity_data(bbb),
            acqtype=acqtype, downsample=acq.downsample,
            fb_set0=bbb.coeffs.set0, fb_set1=bbb.coeffs.set1,
            fb_set0_desc=bbb.coeffs.set0_desc,
            fb_set1_desc=bbb.coeffs.set1_desc,
            fb_downsample=bbb.feedback.downsample,
            fb_state=bbb.feedback.loop_state,
            fb_shift_gain=bbb.feedback.shift_gain,
            fb_setsel=bbb.feedback.coeff_set,
            fb_growdamp_state=bbb.feedback.grow_damp_state,
            fb_mask=bbb.feedback.mask,
            growth_time=acq.growthtime, acq_time=acq.acqtime,
            hold_time=acq.holdtime, post_time=acq.posttime,
            )

    @staticmethod
    def get_cavity_data(bbb):
        """."""
        return dict(
            temperature={
                'cell1': bbb.rfcav.dev_cavmon.temp_cell1,
                'cell2': bbb.rfcav.dev_cavmon.temp_cell2,
                'cell3': bbb.rfcav.dev_cavmon.temp_cell3,
                'cell4': bbb.rfcav.dev_cavmon.temp_cell4,
                'cell5': bbb.rfcav.dev_cavmon.temp_cell5,
                'cell6': bbb.rfcav.dev_cavmon.temp_cell6,
                'cell7': bbb.rfcav.dev_cavmon.temp_cell7,
                'coupler': bbb.rfcav.dev_cavmon.temp_coupler,
                },
            power={
                'cell2': bbb.rfcav.dev_cavmon.power_cell2,
                'cell4': bbb.rfcav.dev_cavmon.power_cell4,
                'cell6': bbb.rfcav.dev_cavmon.power_cell6,
                'forward': bbb.rfcav.dev_cavmon.power_forward,
                'reverse': bbb.rfcav.dev_cavmon.power_reverse,
                'voltage': bbb.rfcav.dev_cavmon.gap_voltage,
                },
            voltage=bbb.rfcav.dev_llrf.voltage_mon,
            phase=bbb.rfcav.dev_llrf.phase_mon,
            detune=bbb.rfcav.dev_llrf.detune,
            detune_error=bbb.rfcav.dev_llrf.detune_error,
            field_flatness_error=bbb.rfcav.dev_llrf.field_flatness_error,
            field_flatness_gain1=bbb.rfcav.dev_llrf.field_flatness_gain1,
            field_flatness_gain2=bbb.rfcav.dev_llrf.field_flatness_gain2,
            field_flatness_amp1=bbb.rfcav.dev_llrf.field_flatness_amp1,
            field_flatness_amp2=bbb.rfcav.dev_llrf.field_flatness_amp2,
            )

    @staticmethod
    def _process_data(data, params, rawdata=None):
        """."""
        rev_per = params.REV_PER
        calib = params.CALIBRATION_FACTOR
        harm_nr = params.HARM_NUM
        current = data.get('stored_current', None)
        if current is None:
            current = data['current']

        time = data['time'].copy()
        freq = data['dft_freq'].copy()

        if rawdata is None:
            dataraw = data['rawdata'].astype(float)
            dataraw *= 1 / (calib * current / harm_nr)
        else:
            dataraw = rawdata.astype(float)

        # remove DC component from bunches
        dataraw -= dataraw.mean(axis=1)[:, None]

        # get the analytic data vector, via discrete hilbert transform
        data_anal = _scysig.hilbert(dataraw, axis=1).copy()

        # calculate DFT:
        data_dft = _np.fft.fft(data_anal, axis=1)

        # compensate the different time samplings of each bunch:
        dts = _np.arange(data_anal.shape[0])/data_anal.shape[0] * rev_per
        comp = _np.exp(-1j*2*_np.pi * freq[None, :]*dts[:, None])
        data_dft *= comp

        # get the processed data by inverse DFT
        data_anal = _np.fft.ifft(data_dft, axis=1)

        # decompose data into even fill eigenvectors:
        data_modes = _np.fft.fft(data_anal, axis=0) / data_anal.shape[0]

        analysis = dict()
        analysis['bunch_numbers'] = _np.arange(1, dataraw.shape[0]+1)
        analysis['dft_freq'] = freq
        analysis['mode_numbers'] = _np.arange(data_modes.shape[0])
        analysis['time'] = time
        analysis['mode_data'] = data_modes
        analysis['bunch_data'] = data_anal

        return analysis

    @staticmethod
    def filter_data(freq, data, params):
        """."""
        center_freq = params.center_frequency
        sigma_freq = params.bandwidth
        ftype = params.filter_type

        data_dft = _np.fft.fft(data, axis=-1)

        if ftype.lower().startswith('gauss'):
            # Apply Gaussian filter to get only the synchrotron frequency
            H = _np.exp(-(freq - center_freq)**2/2/sigma_freq**2)
            H += _np.exp(-(freq + center_freq)**2/2/sigma_freq**2)
            H /= H.max()
            if len(data.shape) > 1:
                data_dft *= H[None, :]
            else:
                data_dft *= H
        else:
            indcsp = (freq > center_freq - sigma_freq)
            indcsp &= (freq < center_freq + sigma_freq)
            indcsn = (-freq > center_freq - sigma_freq)
            indcsn &= (-freq < center_freq + sigma_freq)
            indcs = indcsp | indcsn
            if len(data.shape) > 1:
                data_dft[:, ~indcs] = 0
            else:
                data_dft[~indcs] = 0
        return _np.fft.ifft(data_dft, axis=-1)

    @staticmethod
    def estimate_fitting_intervals(infos, int_type='both', clearance=0):
        """."""
        growth_time = infos['growth_time']
        post_time = infos['post_time']
        tim = infos['time']

        change_time = tim[-1] - (post_time - growth_time)
        change_time = max(tim[0], min(tim[-1], change_time))

        grow_int = [0 + clearance, change_time - clearance]
        damp_int = [change_time + clearance, tim[-1] - clearance]
        if int_type == 'both':
            return [grow_int, damp_int]
        if int_type == 'damp':
            return [damp_int, ]
        return [grow_int, ]

    @staticmethod
    def get_strongest_modes(
            data_modes, nr_modes=None, nr_std=4, ignore_mode0=True):
        """."""
        abs_modes = _np.abs(data_modes)

        avg_modes = abs_modes.max(axis=1)
        if ignore_mode0:
            avg_modes = avg_modes[1:]

        if nr_modes is not None:
            modes = _np.argsort(avg_modes)[::-1]
            modes = modes[:nr_modes]
        else:
            avg = avg_modes.mean()
            std = avg_modes.std()
            modes = (avg_modes > (avg + nr_std*std)).nonzero()[0]

        modes += ignore_mode0
        return modes

    @classmethod
    def fit_exponential(cls, times, data, t_ini=None, t_fin=None, offset=True):
        """Fit exponential function."""
        t_ini = t_ini or times.min()
        t_fin = t_fin or times.max()
        idx = (times >= t_ini) & (times <= t_fin)
        tim = times[idx]
        dtim = data[idx]

        # Exponential function without offset
        if not offset:
            log_amp, rate = _np.polynomial.polynomial.polyfit(
                tim, _np.log(dtim), deg=1, rcond=None)
            return tim, (0, _np.exp(log_amp), rate)

        # method to estimate fitting parameters of
        # y = a + b*exp(c*x)
        # based on:
        # https://www.scribd.com/doc/14674814/Regressions-et-equations-integrales
        # pages 16-18
        s = _scyint.cumtrapz(dtim, x=tim, initial=0.0)
        ym = dtim - dtim[0]
        xm = tim - tim[0]
        mat = _np.array([xm, s]).T
        (_, rate), *_ = _np.linalg.lstsq(mat, ym, rcond=None)
        theta = _np.exp(rate*tim)
        mat = _np.ones((theta.size, 2))
        mat[:, 1] = theta
        (off, amp), *_ = _np.linalg.lstsq(mat, dtim, rcond=None)

        # Now use scipy to refine the estimatives:
        coefs = (off, amp, rate)
        try:
            coefs, _ = _scyopt.curve_fit(
                cls.exponential_func, tim, dtim, p0=coefs)
        except RuntimeError:
            print("Curve fit didn't converge.")
        return tim, coefs

    @staticmethod
    def exponential_func(tim, off, amp, rate):
        """Return exponential function with offset."""
        return off + amp*_np.exp(rate*tim)

    @staticmethod
    def calc_instant_frequency(data, dtime):
        """."""
        freq = _np.unwrap(_np.angle(data))
        freq = _np.gradient(freq, axis=-1)
        freq /= 2*_np.pi*dtime
        return freq

class BbBLParams(_ParamsBaseClass):
    """."""

    DAC_NR_BITS = _asparams.BBBL_DAC_NR_BITS
    SAT_THRES = _asparams.BBBL_SAT_THRES
    CALIBRATION_FACTOR = _asparams.BBBL_CALIBRATION_FACTOR
    DAMPING_RATE = _asparams.BBBL_DAMPING_RATE
    RF_FREQ = _asparams.RF_FREQ
    HARM_NUM = _asparams.SI_HARM_NUM
    REV_FREQ = _asparams.SI_REV_FREQ
    REV_PER = 1 / REV_FREQ

    def __init__(self):
        """."""
        super().__init__()
        self.center_frequency = 2090  # [Hz]
        self.bandwidth = 200  # [Hz]
        self.filter_type = 'gauss'  # (gauss, sinc)
        self.acqtype = 'SRAM'

    def __str__(self):
        """."""
        ftmp = '{0:24s} = {1:9.3f}  {2:s}\n'.format
        stmp = '{0:24s} = {1:9s}  {2:s}\n'.format
        st = ftmp('center_frequency  [Hz]', self.center_frequency, '')
        st += ftmp('bandwidth [Hz]', self.bandwidth, '')
        st += stmp('filter_type', self.filter_type, '[gauss or sinc]')
        st += stmp('acqtype', self.acqtype, '[SRAM or BRAM]')
        return st

class BbBHParams(BbBLParams):
    """."""

    CALIBRATION_FACTOR = _asparams.BBBH_CALIBRATION_FACTOR
    DAMPING_RATE = _asparams.BBBH_DAMPING_RATE

class BbBVParams(BbBLParams):
    """."""

    CALIBRATION_FACTOR = _asparams.BBBV_CALIBRATION_FACTOR
    DAMPING_RATE = _asparams.BBBV_DAMPING_RATE

class BbBAcqData(_BaseClass, UtilClass):
    """."""

    DEVICES = BunchbyBunch.DEVICES

    def __init__(self, devname):
        """."""
        if devname.endswith('L'):
            params = BbBLParams()
        elif devname.endswith('H'):
            params = BbBHParams()
        elif devname.endswith('V'):
            params = BbBVParams()

        super().__init__(params=params)
        if self.isonline:
            self.devices['bbb'] = BunchbyBunch(devname)

    def get_data(self):
        """Get Raw data to file."""
        acqtype = self.params.acqtype
        bbb = self.devices['bbb']
        self.data = UtilClass.get_data(bbb, acqtype)

    def get_cavity_data(self):
        """."""
        bbb = self.devices['bbb']
        return UtilClass.get_cavity_data(bbb)

    def process_data(self, rawdata=None):
        """."""
        analysis = UtilClass._process_data(
            self.data, self.params, rawdata=rawdata)

        freq = analysis['dft_freq']
        analysis['mode_data'] = UtilClass.filter_data(
            freq, analysis['mode_data'], self.params)
        analysis['bunch_data'] = UtilClass.filter_data(
            freq, analysis['bunch_data'], self.params)

        if rawdata is None:
            self.analysis.update(analysis)
        return analysis

    def load_and_apply_old_data(self, fname):
        """."""
        data = self.load_data(fname)
        if not isinstance(data['data'], _np.ndarray):
            self.load_and_apply(fname)
            return

        data.pop('rf_freq')
        data.pop('harmonic_number')
        data['rawdata'] = _np.array(
            data.pop('data').reshape((-1, self.params.HARM_NUM)).T,
            dtype=float)
        data['cavity_data'] = dict(
            temperature={
                'cell1': 0.0, 'cell2': 0.0, 'cell3': 0.0, 'cell4': 0.0,
                'cell5': 0.0, 'cell6': 0.0, 'cell7': 0.0, 'coupler': 0.0},
            power={
                'cell2': 0.0, 'cell4': 0.0, 'cell6': 0.0, 'forward': 0.0,
                'reverse': 0.0},
            voltage=0.0, phase=0.0, detune=0.0, detune_error=0.0,
            )
        self.data = data

    def get_dac_output(self, coeff=None, shift_gain=None, saturate=True):
        """."""
        if coeff is None:
            coeff = self.data[f"fb_set{self.data['fb_setsel']:d}"]
        if shift_gain is None:
            shift_gain = self.data['fb_shift_gain']
        dac_out = _scysig.convolve(
            self.data['rawdata'], coeff[None, :], mode='valid')
        dac_out *= 2**shift_gain
        if saturate:
            idcs = dac_out > self.params.SAT_THRES
            dac_out[idcs] = self.params.SAT_THRES
            idcs = dac_out < -self.params.SAT_THRES
            dac_out[idcs] = -self.params.SAT_THRES
        return dac_out

    def pca_analysis(self, rawdata=None):
        """."""
        calib = self.params.CALIBRATION_FACTOR
        harm_nr = self.params.HARM_NUM
        current = self.data.get('stored_current', None)
        if current is None:
            current = self.data['current']

        if rawdata is None:
            rawdata = self.data['rawdata'].astype(float)
            rawdata *= 1 / (calib * current / harm_nr)
        else:
            rawdata = rawdata.astype(float)

        rawdata -= rawdata.mean(axis=1)[:, None]
        return _np.linalg.svd(rawdata)

    def get_strongest_modes(
            self, nr_modes=None, nr_std=4, ignore_mode0=True, analysis=None):
        """."""
        if analysis is None:
            analysis = self.analysis
        data_modes = analysis['mode_data']

        return UtilClass.get_strongest_modes(
            data_modes, nr_modes=nr_modes, nr_std=nr_std,
            ignore_mode0=ignore_mode0)

    def estimate_fitting_intervals(self, int_type='both', clearance=0):
        """."""
        return super().estimate_fitting_intervals(
            self.data, int_type, clearance)

    def fit_and_plot_growth_rates(
            self, mode_num, intervals=None, offset=True, labels=None, title='',
            analysis=None):
        """."""
        if analysis is None:
            analysis = self.analysis
        if intervals is None:
            intervals = self.estimate_fitting_intervals()

        tim = analysis['time']
        dtime = (tim[1] - tim[0]) / 1000

        data_mode = analysis['mode_data'][mode_num]
        abs_mode = _np.abs(data_mode)

        labels = ['']*len(intervals) if labels is None else labels

        fig = _mplt.figure(figsize=(7, 8))
        gsp = _mgs.GridSpec(2, 1)
        gsp.update(left=0.15, right=0.95, top=0.94, bottom=0.1, hspace=0.2)
        aty = _mplt.subplot(gsp[0, 0])
        atx = _mplt.subplot(gsp[1, 0], sharex=aty)

        aty.plot(tim, abs_mode, label='Data')

        gtimes = []
        for inter, label in zip(intervals, labels):
            ini, fin = inter
            tim_fit, coef = self.fit_exponential(
                tim, abs_mode, t_ini=ini, t_fin=fin, offset=offset)
            fit = self.exponential_func(tim_fit, *coef)
            gtimes.append(coef[2] * 1000)
            aty.plot(tim_fit, fit, label=label)
            aty.annotate(
                f'rate = {coef[2]*1000:.2f} Hz', fontsize='x-small',
                xy=(tim_fit[fit.size//2], fit[fit.size//2]),
                textcoords='offset points', xytext=(-100, 10),
                arrowprops=dict(arrowstyle='->'),
                bbox=dict(boxstyle="round", fc="0.8"))

        aty.legend(loc='best', fontsize='small')
        aty.set_title(title, fontsize='small')
        aty.set_xlabel('time [ms]')
        aty.set_ylabel('Amplitude [°]')

        idx = abs_mode > abs_mode.max()/10
        inst_freq = self.calc_instant_frequency(data_mode, dtime)
        inst_freq /= 1e3
        atx.plot(tim[idx], inst_freq[idx])
        atx.set_xlabel('time [ms]')
        atx.set_ylabel('Instantaneous Frequency [kHz]')
        cenf = self.params.center_frequency / 1000
        sig = self.params.bandwidth / 1000
        atx.set_ylim([cenf - sig, cenf + sig])

        fig.show()
        return fig, aty, atx, gtimes

    def plot_modes_evolution(
            self, nr_modes=None, nr_std=4, ignore_mode0=True, title='',
            analysis=None):
        """."""
        if analysis is None:
            analysis = self.analysis

        data_modes = analysis['mode_data']
        abs_modes = _np.abs(data_modes)
        tim = analysis['time']
        dtime = (tim[1] - tim[0]) / 1000

        avg_modes = abs_modes.max(axis=1)

        indcs = self.get_strongest_modes(
            nr_modes=nr_modes, nr_std=nr_std, ignore_mode0=ignore_mode0,
            analysis=analysis)

        fig = _mplt.figure(figsize=(7, 10))
        gsp = _mgs.GridSpec(3, 1)
        gsp.update(left=0.15, right=0.95, top=0.96, bottom=0.07, hspace=0.23)
        ax = _mplt.subplot(gsp[0, 0])
        aty = _mplt.subplot(gsp[1, 0])
        atx = _mplt.subplot(gsp[2, 0], sharex=aty)

        ax.plot(avg_modes, 'k')
        for idx in indcs:
            data = data_modes[idx, :]
            abs_mode = abs_modes[idx, :]
            ax.plot(idx, avg_modes[idx], 'o')
            aty.plot(tim, abs_mode, label=f'{idx:03d}')
            inst_freq = self.calc_instant_frequency(data, dtime)
            nzer = abs_mode > abs_mode.max()/10
            atx.plot(tim[nzer], inst_freq[nzer]/1e3, label=f'{idx:03d}')

        aty.legend(loc='best', fontsize='small')
        ax.set_title(title)
        ax.set_ylabel('Max Amplitude [°]')
        ax.set_xlabel('Mode Number')
        aty.set_xlabel('time [ms]')
        aty.set_ylabel('Amplitude [°]')
        atx.set_xlabel('time [ms]')
        atx.set_ylabel('Instantaneous Frequency [kHz]')

        cenf = self.params.center_frequency / 1000
        sig = self.params.bandwidth / 1000
        atx.set_ylim([cenf - sig, cenf + sig])

        fig.show()
        return fig, ax, aty, atx

    def plot_average_spectrum(self, rawdata=None, subtract_mean=True):
        """."""
        if rawdata is None:
            rawdata = self.data['rawdata']
        rawdata = rawdata.astype(float)
        rev_per = self.params.REV_PER
        downsample = self.data['downsample']
        dtime = rev_per*downsample

        if subtract_mean:
            rawdata -= rawdata.mean(axis=1)[:, None]
        dataraw_dft = _np.fft.rfft(rawdata, axis=1)
        rfreq = _np.fft.rfftfreq(rawdata.shape[1], d=dtime)
        avg_dataraw = _np.abs(dataraw_dft).mean(axis=0)/rawdata.shape[1]

        f = _mplt.figure(figsize=(7, 4))
        gs = _mgs.GridSpec(1, 1)
        gs.update(
            left=0.15, right=0.95, top=0.97, bottom=0.18, wspace=0.35,
            hspace=0.2)
        aty = _mplt.subplot(gs[0, 0])

        aty.plot(rfreq, avg_dataraw)
        aty.set_yscale('log')
        f.show()
        return f, aty

    def plot_modes_summary(self, analysis=None):
        """."""
        if analysis is None:
            analysis = self.analysis

        data_modes = analysis['mode_data']
        data_anal = analysis['bunch_data']
        tim = analysis['time']
        mode_nums = analysis['mode_numbers']
        bunch_nums = analysis['bunch_numbers']

        f = _mplt.figure(figsize=(12, 9))
        gs = _mgs.GridSpec(2, 2)
        gs.update(
            left=0.10, right=0.95, top=0.97, bottom=0.10, wspace=0.35,
            hspace=0.2)
        # aty = _mplt.subplot(gs[0, :7], projection='3d')
        # afy = _mplt.subplot(gs[1, :7], projection='3d')
        aty = _mplt.subplot(gs[0, 0])
        afy = _mplt.subplot(gs[1, 0])
        atx = _mplt.subplot(gs[0, 1])
        afx = _mplt.subplot(gs[1, 1])

        abs_modes = _np.abs(data_modes)
        abs_dataf = _np.abs(data_anal)

        afx.plot(mode_nums, abs_modes.mean(axis=1))
        afx.set_xlabel('Mode Number')
        afx.set_ylabel('Average Amplitude [°]')
        # afx.set_yscale('log')

        # waterfall_plot(afy, tim, mode_nums, abs_modes)
        # afy.set_ylabel('\ntime [ms]')
        # afy.set_xlabel('\nmode number')
        # afy.set_zlabel('amplitude')
        T, M = _np.meshgrid(tim, mode_nums)
        cf = afy.pcolormesh(
            T, M, abs_modes, cmap='jet',
            vmin=abs_modes.min(), vmax=abs_modes.max())
        afy.set_xlabel('Time [ms]')
        afy.set_ylabel('Mode Number')
        cb = f.colorbar(cf, ax=afy, pad=0.01)
        cb.set_label('Amplitude [°]')

        atx.plot(bunch_nums, abs_dataf.mean(axis=1))
        atx.set_xlabel('Bunch Number')
        atx.set_ylabel('Average Amplitude [°]')

        # waterfall_plot(aty, tim, bunch_nums, abs_dataf)
        # aty.set_ylabel('\ntime [ms]')
        # aty.set_xlabel('\nbunch number')
        # aty.set_zlabel('amplitude')
        T, M = _np.meshgrid(tim, bunch_nums)
        cf = aty.pcolormesh(
            T, M, abs_dataf, cmap='jet',
            vmin=abs_dataf.min(), vmax=abs_dataf.max())
        aty.set_xlabel('Time [ms]')
        aty.set_ylabel('Bunch Number')
        cb = f.colorbar(cf, ax=aty, pad=0.01)
        cb.set_label('Amplitude [°]')

        f.show()
        return f

    @staticmethod
    def waterfall_plot(axis, xs, zs, data):
        """."""
        vertcs, colors = [], []
        cors = ['b', 'r', 'g', 'y', 'm', 'c']
        for i, y in enumerate(zs):
            ys = data[i, :].copy()
            ys[0], ys[-1] = 0, 0
            vertcs.append(list(zip(xs, ys)))
            colors.append(cors[i % len(cors)])
        poly = _PolyCollection(
            vertcs, closed=False, edgecolors='k',
            linewidths=1, facecolors=colors)

        poly.set_alpha(0.7)
        axis.add_collection3d(poly, zs=zs, zdir='x')
        axis.view_init(elev=35.5, azim=-135)
        axis.set_ylim3d(xs.min(), xs.max())
        axis.set_xlim3d(zs.min(), zs.max())
        axis.set_zlim3d(0, data.max())

class DriveDampLParams(BbBLParams):
    """."""

    def __init__(self):
        """."""
        super().__init__()
        self.modes_to_measure = _np.arange(1, self.HARM_NUM//2+1)
        self.drive_num = 0
        self.wait_acquisition = 1  # [s]
        self.wait_pv_update = 0  # [s]
        self.fitting_clearance_ini = 0  # [ms]
        self.fitting_clearance_fin = 100  # [ms]
        self.fitting_consider_offset = True  # (True, False)

    def __str__(self):
        """."""
        dtmp = '{0:24s} = {1:9d}\n'.format
        ftmp = '{0:24s} = {1:9.4f}  {2:s}\n'.format
        stmp = '{0:24s} = {1:}  {2:s}\n'.format

        stg = super().__str__()
        stg += dtmp('drive_num', self.drive_num)
        stg += ftmp('wait_acquisition', self.wait_acquisition, '[s]')
        stg += ftmp('wait_pv_update', self.wait_pv_update, '[s]')
        stg += ftmp(
            'fitting_clearance_ini', self.fitting_clearance_ini, '[ms]')
        stg += ftmp(
            'fitting_clearance_fin', self.fitting_clearance_fin, '[ms]')
        stg += stmp(
            'fitting_consider_offset', str(bool(self.fitting_consider_offset)),
            '(True | False)')

        modes = self.modes_to_measure
        if len(modes) > 6:
            modes_stg = ', '.join([f'{m:03d}' for m in modes[:3]])
            modes_stg += ', ..., '
            modes_stg += ', '.join([f'{m:03d}' for m in modes[-3:]])
        else:
            modes_stg = ', '.join([f'{m:03d}' for m in modes])
        stg += stmp(
            'modes_to_measure', f'[{modes_stg:s}] (size = {len(modes):d})', '')
        return stg

class DriveDampHParams(DriveDampLParams):
    """."""

    DAMPING_RATE = 1/16.9e-3  # [Hz]
    CALIBRATION_FACTOR = 1000  # [Counts/mA/um]

class DriveDampVParams(DriveDampLParams):
    """."""

    DAMPING_RATE = 1/22.0e-3  # [Hz]
    CALIBRATION_FACTOR = 1000  # [Counts/mA/um]

class MeasDriveDamp(_ThreadBaseClass, UtilClass):
    """."""

    DEVICES = BbBAcqData.DEVICES

    def __init__(self, devname, isonline=True):
        """."""
        if devname.endswith('L'):
            params = DriveDampLParams()
        elif devname.endswith('H'):
            params = DriveDampHParams()
        elif devname.endswith('V'):
            params = DriveDampVParams()

        super().__init__(
            params=params, target=self._do_measure, isonline=isonline)
        if self.isonline:
            self.devices['bbb'] = BunchbyBunch(devname)

    def get_property(self, propty):
        """Get a given property saved in `data` or `analysis` dictionary.

        Args:
            propty (str): Comma concatenated string of nested dictionary keys
                where the property is located. Examples:
                    - 'infos,cavity_data,temperature,cell4'
                    - 'coeffs';
                    - 'modes_measured';
                    - 'infos,cavity_data,field_flatness_amp1'.

        Raises:
            TypeError: if the concatenated string is not complete, that is do
                not lead to a unique property. Example:
                    - 'infos,cavity_data,temperature';
                    - '';
            KeyError: if the concatenated string contains keys not present in
                the nested dictionaries. Example:
                    - 'infos,blbalkdia';

        Returns:
            np.ndarray: array with the value of the desired property for all
                modes measured.

        """
        dic = dict()
        dic.update(self.data)
        dic.update(self.analysis)
        if not propty:
            raise TypeError(
                'String concatenation passed in propty is not complete.\n'
                'Possible options to continue are: '
                ', '.join(list(dic.keys())))

        propty = propty.split(',')
        lsts = dic[propty[0]]
        prop = []
        for lst in lsts:
            for p in propty[1:]:
                if p:
                    lst = lst[p]
            if isinstance(lst, dict):
                raise TypeError(
                    'String concatenation passed in propty is not complete.\n'
                    'Possible options to continue are: '
                    ', '.join(list(lst.keys())))
            prop.append(lst)

        if isinstance(prop[0], (int, float)):
            prop = _np.array(prop)
        elif isinstance(prop[0], (list, tuple)):
            prop = _np.hstack(prop)
        elif isinstance(prop[0], _np.ndarray):
            prop = _np.vstack(prop)
        else:
            return prop

        # Make sure the size of the first dimension of the array is equal to
        # the number of modes measured by repeating values when needed.
        modes_meas = dic['modes_measured']
        idcs = []
        for i, ms in enumerate(modes_meas):
            idcs.extend([i, ]*len(ms))

        if prop.shape[0] < len(idcs):
            prop = prop[idcs]
        return prop

    def load_merge_and_apply_data(self, fnames):
        """Load data from several files and merge them as single measurement.

        Args:
            fnames (list): filenames of the saved data.
        """
        self.clear_data()
        data = dict()
        params = self.params
        for fname in fnames:
            datum = self.load_data(fname)['data']
            if not data:
                data.update(datum)
                continue
            for key, val in datum.items():
                data[key] += val
        self.data = data
        self.params = params

    def clear_data(self):
        """Reset `data` and `analysis` dictionaries."""
        self.data = dict()
        self.analysis = dict()

    def process_data(self):
        """."""
        infos = self.data['infos']
        data = self.data['modes_data']

        analysis = dict(coeffs=[], tim_fits=[], fits=[], modes_filt=[])
        for i, (info, datum) in enumerate(zip(infos, data)):
            print('.', end='')
            if not ((i+1) % 80):
                print()
            tim = info['time']
            freq = info['dft_freq']
            interval = self.estimate_fitting_intervals(
                info, clearance_ini=self.params.fitting_clearance_ini,
                clearance_fin=self.params.fitting_clearance_fin)
            coeff, tim_fit, fit = self.fit_growth_rates(
                tim, datum, interval=interval,
                offset=self.params.fitting_consider_offset,
                full=True)

            mode_filt = self.filter_data(freq, datum, self.params)

            analysis['coeffs'].append(coeff)
            analysis['tim_fits'].append(tim_fit)
            analysis['fits'].append(fit)
            analysis['modes_filt'].append(mode_filt)
        self.analysis = analysis

    def estimate_fitting_intervals(
            self, infos, clearance_ini=0, clearance_fin=0):
        """."""
        interv = super().estimate_fitting_intervals(
            infos, int_type='damp', clearance=0)[0]
        interv[0] += max(clearance_ini, 0)
        interv[1] -= max(clearance_fin, 0)
        return interv

    def fit_growth_rates(self, tim, data, interval, offset=True, full=False):
        """."""
        freq = _np.fft.fftfreq(tim.size, d=(tim[1]-tim[0])/1000)
        data = UtilClass.filter_data(freq, data, self.params)
        abs_data = _np.abs(data)
        if len(abs_data.shape) == 1:
            abs_data = abs_data.reshape(-1, abs_data.size)

        coeffs, fittings = [], []
        for abs_mode in abs_data:
            tim_fit, coef = self.fit_exponential(
                tim, abs_mode, t_ini=interval[0], t_fin=interval[1],
                offset=offset)
            fit = self.exponential_func(tim_fit, *coef)
            coeffs.append(coef)
            fittings.append(fit)

        coeffs = _np.array(coeffs)
        fittings = _np.array(fittings)
        if not full:
            return coeffs[:, 2] * 1000
        return coeffs, tim_fit, fittings

    def plot_growth_rates(self, title=''):
        """."""
        fig = _mplt.figure(figsize=(7, 5))
        gsp = _mgs.GridSpec(1, 1)
        gsp.update(left=0.09, right=0.98, top=0.90, bottom=0.1)
        ax = _mplt.subplot(gsp[0, 0])

        modes_meas = self.data['modes_measured']
        coeffs = self.analysis['coeffs']
        for num, coeff in zip(modes_meas, coeffs):
            ax.plot(num, coeff[:, 2]*1000, 'ob')
        ax.set_title(title)
        ax.set_ylabel('Growth Rates [1/s]')
        ax.set_xlabel('Modes')
        return fig, ax

    def plot_modes_evolution(self, data_index=0, title=''):
        """."""
        mode_meas = self.data['modes_measured'][data_index]
        infos = self.data['infos'][data_index]
        mode_filt = self.analysis['modes_filt'][data_index]
        tfit = self.analysis['tim_fits'][data_index]
        fit = self.analysis['fits'][data_index]
        coeff = self.analysis['coeffs'][data_index]

        abs_mode = _np.abs(mode_filt)

        fig = _mplt.figure(figsize=(7, 8))
        gsp = _mgs.GridSpec(2, 1)
        gsp.update(left=0.15, right=0.95, top=0.94, bottom=0.1, hspace=0.2)
        aty = _mplt.subplot(gsp[0, 0])
        atx = _mplt.subplot(gsp[1, 0], sharex=aty)

        tim = infos['time']
        dtime = (tim[1] - tim[0]) / 1000
        for i, absm in enumerate(abs_mode):
            ffit = fit[i]
            cff = coeff[i]
            num = mode_meas[i]
            lin = aty.plot(tim, absm, label=f'{num:03d}', lw=1)[0]
            aty.plot(tfit, ffit, ls='--', lw=3, color=lin.get_color())

            idx = int(ffit.size/(abs_mode.shape[0]+1) * (i+1))
            aty.annotate(
                f'rate = {cff[2]*1000:.2f} Hz', fontsize='x-small',
                xy=(tfit[idx], ffit[idx]),
                textcoords='offset points', xytext=(10, 20),
                arrowprops=dict(arrowstyle='->'),
                bbox=dict(boxstyle="round", fc="0.8"))

            inst_freq = self.calc_instant_frequency(mode_filt[i], dtime)
            inst_freq /= 1e3
            idx = absm > absm.max()/10
            atx.plot(tim[idx], inst_freq[idx], color=lin.get_color())
        aty.legend(loc='best', fontsize='small')
        aty.set_title(title, fontsize='small')
        aty.set_xlabel('time [ms]')
        aty.set_ylabel('Amplitude [°]')

        atx.set_xlabel('time [ms]')
        atx.set_ylabel('Instantaneous Frequency [kHz]')
        cenf = self.params.center_frequency / 1000
        sig = self.params.bandwidth / 1000
        atx.set_ylim([cenf - sig, cenf + sig])

        fig.show()
        return fig, aty, atx, coeff[:, 2] * 1000

    def _do_measure(self):
        elt0 = _time.time()
        acqtype = self.params.acqtype
        drive_num = self.params.drive_num

        bbb = self.devices['bbb']
        drive = bbb.drive0 if drive_num == 0 else bbb.drive1
        drive = drive if drive_num != 2 else bbb.drive2

        harm_nr = bbb.info.harmonic_number
        modes_to_measure = self.params.modes_to_measure
        bunches = _np.arange(harm_nr)

        bbb.sram.cmd_data_dump(pv_update=True)
        _time.sleep(self.params.wait_pv_update)
        infos = self.get_data(bbb, acqtype)
        analysis = self._process_data(infos, self.params)
        interval = self.estimate_fitting_intervals(
            infos, clearance_ini=self.params.fitting_clearance_ini,
            clearance_fin=self.params.fitting_clearance_fin)
        tim = infos['time']

        if not self.data:
            self.data = dict(infos=[], modes_data=[], modes_measured=[])
        for mode in modes_to_measure:
            elt = _time.time()
            drive.mask = _np.cos(2*_np.pi*bunches*mode/harm_nr) > 0
            _time.sleep(self.params.wait_acquisition)
            bbb.sram.cmd_data_dump(pv_update=True)
            _time.sleep(self.params.wait_pv_update)

            infos = self.get_data(bbb, acqtype)
            analysis = self._process_data(infos, self.params)
            modei = sorted({mode, harm_nr - mode})
            data = analysis['mode_data'][modei]
            infos.pop('rawdata')

            self.data['modes_measured'].append(modei)
            self.data['modes_data'].append(data)
            self.data['infos'].append(infos)

            grt = self.fit_growth_rates(
                tim, data, interval=interval,
                offset=self.params.fitting_consider_offset,
                full=False)
            print(',      '.join([
                f'mode: {m:03d} --> growth: {gt:+7.2f}'
                for m, gt in zip(modei, grt.ravel())]), end='')
            elt -= _time.time()
            elt *= -1
            print(f',      ET: {elt:.2f}s')
            if self._stopevt.is_set():
                print('Stopping...')
                break
        elt0 -= _time.time()
        elt0 *= -1
        print(f'Finished!!  ET: {elt0/60:.2f}min')

class TuneShiftParams(_ParamsBaseClass):
    """."""

    REV_TIME = _asparams.SI_REV_TIME  # s
    WAIT_INJ = 0.2  # s
    DEF_TOL_CURRENT = 0.01  # mA

    def __init__(self):
        """."""
        super().__init__()
        self.kickh = -25/1000  # mrad
        self.kickv = +20/1000  # mrad
        self.wait_bbb = 9  # s
        self.currents = _np.arange(0.05, 2.1, 0.1)  # mA

    def __str__(self):
        """."""
        dtmp = '{0:10s} = {1:9d}  {2:s}\n'.format
        ftmp = '{0:10s} = {1:9.3f}  {2:s}\n'.format
        ltmp = '{0:6.3f},'.format
        stg = ''
        stg += ftmp('kickh', self.kickh, '[mrad]')
        stg += ftmp('kickv', self.kickv, '[mrad]')
        stg += dtmp('wait_bbb', self.wait_bbb, '[s]')
        stg += f"{'currents':10s} = ("
        stg += ''.join(map(ltmp, self.currents))
        stg += ' ) [mA] \n'
        return stg

class MeasTuneShift(_ThreadBaseClass):
    """."""

    def __init__(self, isonline=True):
        """."""
        params = TuneShiftParams()
        super().__init__(
            params=params, target=self._do_measure, isonline=isonline)
        self.pingers = list()
        if isonline:
            self.devices['bbbh'] = BunchbyBunch(BunchbyBunch.DEVICES.H)
            self.devices['bbbv'] = BunchbyBunch(BunchbyBunch.DEVICES.V)
            self.devices['pingh'] = PowerSupplyPU(
                PowerSupplyPU.DEVICES.SI_INJ_DPKCKR)
            self.devices['pingv'] = PowerSupplyPU(
                PowerSupplyPU.DEVICES.SI_PING_V)
            self.devices['egun'] = EGTriggerPS()
            self.pingers = [self.devices['pingh'], self.devices['pingv']]

    def get_data_from_plane(self, plane):
        """."""
        bbbtype = 'bbbh' if plane.upper() == 'H' else 'bbbv'
        bbb = self.devices[bbbtype]
        sb_tune = bbb.single_bunch
        data = {
            'timestamp': _time.time(),
            'stored_current': bbb.dcct.current,
            'data': sb_tune.data_raw,
            'spec_mag': sb_tune.spec_mag, 'spec_freq': sb_tune.spec_freq,
            'spec_phase': sb_tune.spec_phase,
            'bunch_id': sb_tune.bunch_id, 'fft_size': sb_tune.fft_size,
            'fft_overlap': sb_tune.fft_overlap,
            'delay_calibration': sb_tune.delay_calibration,
            'nr_averages': sb_tune.nr_averages}
        return data

    @staticmethod
    def merge_data(data1: dict, data2: dict):
        """."""
        if data1.keys() != data2.keys():
            raise Exception('Incompatible data sets')
        merge = dict()
        for key in data1:
            merge[key] = data1[key] + data2[key]
        return merge

    def turn_on_pingers_pulse(self):
        """."""
        for ping in self.pingers:
            ping.cmd_turn_on_pulse()

    def turn_off_pingers_pulse(self):
        """."""
        for ping in self.pingers:
            ping.cmd_turn_off_pulse()

    def turn_on_pingers(self):
        """."""
        for ping in self.pingers:
            ping.cmd_turn_on()

    def turn_off_pingers(self):
        """."""
        for ping in self.pingers:
            ping.cmd_turn_off()

    def prepare_pingers(self):
        """."""
        self.devices['pingh'].strength = self.params.kickh
        self.devices['pingv'].strength = self.params.kickv
        self.turn_on_pingers()
        self.turn_on_pingers_pulse()

    def inject_in_storage_ring(self, goal_curr):
        """."""
        self.devices['egun'].cmd_enable_trigger()
        while not self._check_stored_current(goal_curr):
            _time.sleep(TuneShiftParams.WAIT_INJ)
        self.devices['egun'].cmd_disable_trigger()

    def _check_stored_current(
            self, goal_curr, tol=TuneShiftParams.DEF_TOL_CURRENT):
        dcct_curr = self.devices['bbbh'].dcct.current
        return dcct_curr > goal_curr or abs(dcct_curr - goal_curr) < tol

    def _do_measure(self):
        """."""
        datah = list()
        datav = list()
        currs = list()
        self.prepare_pingers()
        for gcurr in self.params.currents:
            t0 = _time.time()
            print('Injecting...')
            self.inject_in_storage_ring(gcurr)
            curr = self.devices['bbbh'].dcct.current
            print(f'Stored Current: {curr:.3f}/{gcurr:.3f}mA.')

            print('Waiting BbB Update...')
            _time.sleep(self.params.wait_bbb)

            print('Acquiring data...')
            currs.append(self.devices['bbbh'].dcct.current)
            datah.append(self.get_data_from_plane(plane='H'))
            datav.append(self.get_data_from_plane(plane='V'))
            self.data['stored_current'] = currs
            self.data['horizontal'] = datah
            self.data['vertical'] = datav

            tf = _time.time()
            print(f'Elapsed time: {tf-t0:.2f}s \n')

            if self._stopevt.is_set():
                print('Stopping...')
                break
        print('Finished!')

    def plot_spectrum(
            self, plane, freq_min=None, freq_max=None, title=None,
            fit_sync_freq=1.8, fit_bet_freq=43, fit_max_curr=0.6,
            fit_min_curr=0.1,
            cut_spec=None, fname=None):
        """plane: must be 'H' or 'V'."""
        plane = plane.upper()
        if plane == 'H':
            data = self.data['horizontal']
            freq_min = freq_min or 38
            freq_max = freq_max or 52
        elif plane == 'V':
            data = self.data['vertical']
            freq_min = freq_min or 72
            freq_max = freq_max or 84
        else:
            raise Exception("plane input must be 'H' or 'V'.")

        def model(coefs, mag, freq, curr):
            lin_c = coefs[:3][:, None]
            ang_c = coefs[3:][:, None]

            mag = mag.ravel()
            freq = freq.ravel()[None, :]
            curr = curr.ravel()[None, :]

            mod = ang_c * curr
            mod += lin_c
            mod -= freq
            mod = _np.abs(mod)
            mod = _np.min(mod, axis=0) * mag
            return mod

        curr = _np.array(self.data['stored_current'])
        mag = [dta['spec_mag'] for dta in data]
        mag = _np.array(mag, dtype=float)
        freq = _np.array(data[-1]['spec_freq'])

        idcs = (freq > freq_min) & (freq < freq_max)
        freq = freq[idcs]
        idx = _np.argsort(curr)
        curr = curr[idx]
        mag = mag[idx, :]
        mag = mag[:, idcs]
        freq_, curr_ = _np.meshgrid(freq, curr)

        # Transform from dB to amplitude
        mag = 10**(mag/10)
        # Normalize data from each current to one:
        mag /= mag.max(axis=1)[:, None]

        if cut_spec:
            mag[mag < cut_spec] = 0

        idx = (curr > fit_min_curr) & (curr < fit_max_curr)
        mag_fit = mag[idx, :]
        freq_fit = freq_[idx, :]
        curr_fit = curr_[idx, :]
        lin_c0 = _np.arange(-1, 2) * fit_sync_freq + fit_bet_freq
        ang_c0 = _np.zeros(3)
        x0 = _np.r_[lin_c0, ang_c0]
        opt = _scyopt.least_squares(
            fun=model, x0=x0, method='lm', args=(mag_fit, freq_fit, curr_fit))
        lin_c = opt.x[:3]
        ang_c = opt.x[3:]

        errs = self._calc_fitting_error(opt)
        err_lin_c = errs[:3]
        err_ang_c = errs[3:]

        tunes_fit = ang_c[:, None] * curr[None, :] + lin_c[:, None]

        fig, ax = _mplt.subplots(figsize=(8, 6))

        ax.pcolormesh(curr_, freq_, 10*_np.log10(mag), shading='auto')
        lines = ax.plot(curr, tunes_fit.T, 'k')
        lss = ('--', '-', '-.')
        for i, (line, ls_) in enumerate(zip(lines, lss)):
            line.set_label(
                f'm={i-1:2d} -> '
                f'f = ({ang_c[i]:5.2f} ± {err_ang_c[i]:5.2f})*I'
                f' + ({lin_c[i]:6.2f} ± {err_lin_c[i]:6.2f})')
            line.set_ls(ls_)
        ax.legend(loc='best', fontsize='xx-small')
        ax.set_ylabel('Frequency [kHz]')
        ax.set_xlabel('Current [mA]')
        ax.set_title(title)
        fig.tight_layout()

        if fname:
            if not fname.endswith('.png'):
                fname += '.png'
            fig.savefig(fname, dpi=300)
        return fig, mag, opt.x, opt

    def plot_time_evolution(self, plane, title=None, fname=None):
        """plane: must be 'H' or 'V'."""
        plane = plane.upper()
        if plane == 'H':
            data = self.data['horizontal']
        elif plane == 'V':
            data = self.data['vertical']
        else:
            raise Exception("plane input must be 'H' or 'V'.")

        curr = _np.array(self.data['stored_current'])
        mag = [dta['data'] for dta in data]
        mag = _np.array(mag, dtype=float)
        mag -= _np.mean(mag, axis=1)[:, None]
        mag = _np.abs(mag)
        dtime = _np.arange(0, mag.shape[1]) * TuneShiftParams.REV_TIME

        idx = _np.argsort(curr)
        curr = curr[idx]
        mag = mag[idx, :]

        dtime, curr = _np.meshgrid(dtime, curr)
        dtime, curr, mag = dtime.T, curr.T, mag.T

        fig = _mplt.figure(figsize=(8, 6))
        gs = _mgs.GridSpec(1, 1)
        ax = fig.add_subplot(gs[0, 0])

        ax.pcolormesh(curr, dtime * 1e3, mag)
        ax.set_ylabel('Time [ms]')
        ax.set_xlabel('Current [mA]')
        ax.set_title(title)
        if fname:
            if not fname.endswith('.png'):
                fname += '.png'
            fig.savefig(fname, dpi=300)
        return fig

    @staticmethod
    def _calc_fitting_error(fit_params):
        # based on fitting error calculation of scipy.optimization.curve_fit
        # do Moore-Penrose inverse discarding zero singular values.
        _, smat, vhmat = _np.linalg.svd(
            fit_params['jac'], full_matrices=False)
        thre = _np.finfo(float).eps * max(fit_params['jac'].shape)
        thre *= smat[0]
        smat = smat[smat > thre]
        vhmat = vhmat[:smat.size]
        pcov = _np.dot(vhmat.T / (smat*smat), vhmat)

        # multiply covariance matrix by residue 2-norm
        ysize = len(fit_params['fun'])
        cost = 2 * fit_params['cost']  # res.cost is half sum of squares!
        popt = fit_params['x']
        if ysize > popt.size:
            # normalized by degrees of freedom
            s_sq = cost / (ysize - popt.size)
            pcov = pcov * s_sq
        else:
            pcov.fill(_np.nan)
            print('# of fitting parameters larger than # of data points!')
        return _np.sqrt(_np.diag(pcov))


text:
angular.module('app', ['ngDeferredShow'])
    .controller('Main', function ($scope) {
        $scope.visible = false;
        $scope.items = [];

        for (var i = 0; i < 1000; i++) {
            $scope.items.push({index: i, visible: i < 10});
        }

        $scope.onClick = function () {
            for (var i = 0; i < $scope.items.length; i++) {
                $scope.items[i].visible = $scope.visible;
            }
        };

        $scope.onCompileDone = function (item) {
            var i = 0;
        };

        $scope.onBatchDone = function () {
            var i = 0;
        };

        $scope.timeout = function (item) {
            return (batchDistribution(item.index, 5) + 1) * 1000;
        };

        function batchDistribution(index, batchSize) {
            return Math.floor(Math.max(index - batchSize, 0) / batchSize)
        }
    });

redpajama_set_name: RedPajamaGithub


text:
require 'emaildirect'

module EmailDirect::RelaySend
end
require 'emaildirect/relay_send/category'
require 'emaildirect/relay_send/receipt'
require 'emaildirect/relay_send/email'


text:
================

This extension is a set of tools to work on corners of an outline. It comprises a tool available in the glyph editing view that allows you to round/cut corners by dragging to define the radius, and a pop-up window (accessible via the Extensions menu, under Corner Tools) which provides a more global set of controls.

### Glyph view rounding tool

#### Actions
+ **Double-click**: apply rounding
+ **Command+Double-click**: reset all radiuses

#### Holding modifier keys
+ **Shift**: radius steps 5 by 5
+ **Option**: make a flat corner rather than round
+ **Shift**+**Command**: reset radius

![alt tag](images/cornerTools-RoundingTool.png)

### Corner Tools controller

The controller works as a wysiwyg corner editor, working with point selection. There are three modes:

#### Build
Needs a selection of one or several segments and will try to grow new corners where it can. Results may vary.

![alt tag](images/cornerTool-build.png)

#### Break
This mode allows similar operations to what the rounding tool but with finer control and the ability to apply the same value to several corners at once. The two parameters allow you to do a number of things:

![alt tag](images/cornerTool-break-round.png)
![alt tag](images/cornerTool-break-cut.png)
![alt tag](images/cornerTool-break-overlap.png)

#### Pit
This mode implements 'ink-traps' or 'light-wells' or whatever name fits your ideology. Having no opinion on the matter, I decided to take on another term, pits. Something wicked this way comes.

![alt tag](images/cornerTool-pit.png)

(If you get unexpected results, it may be that your contour direction isn't right. And beyond that, it may well be that you overstep the capabilities of this code: bear in mind that outline modification is a tricky business, so don't expect miracles.)


text:
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>W28547_text</title>
        <link rel="stylesheet" type="text/css" href="style.css" />

                «

    <p class="styleSans18.0000<enum PANGO_WEIGHT_NORMAL of type PangoWeight><enum PANGO_STYLE_NORMAL of type PangoStyle>">#5 Energ y Services.

    <p class="styleSans8.0000<enum PANGO_WEIGHT_NORMAL of type PangoWeight><enum PANGO_STYLE_NORMAL of type PangoStyle>">March 17, 2015

    <p class="styleSans8.0000<enum PANGO_WEIGHT_NORMAL of type PangoWeight><enum PANGO_STYLE_NORMAL of type PangoStyle>">North Dakota Mineral Resources Survey Certification Sheet

    <p class="styleSans10.0000<enum PANGO_WEIGHT_NORMAL of type PangoWeight><enum PANGO_STYLE_NORMAL of type PangoStyle>">Company: XTO Energy Inc. Well: Schettler 14X
Well Number: 9F
Location: Dunn County, ND Job Number: DDMT-l40815
Well API# 33-025-02566

    <p class="styleSans12.0000<enum PANGO_WEIGHT_NORMAL of type PangoWeight><enum PANGO_STYLE_NORMAL of type PangoStyle>">Attached please find the original surveys performed on the above referenced well by MS Energy Services. The data is true, correct, complete and within the limitations of the tool as set forth by MS Energy Services. I am authorized and qualified to make this report and it conforms to the principles and procedures as set forth by MS Energy Services. The surveys were performed as listed below.
m
Name of Surveyor Drain hole No. Surveyed Depths Dates Performed Survey Tim Coleman Original Wellbore 2,542' — 17,510' MD 10/23/2014 to 11/09/2014 MWD

    <p class="styleSans10.0000<enum PANGO_WEIGHT_NORMAL of type PangoWeight><enum PANGO_STYLE_NORMAL of type PangoStyle>">If any other information is required, please contact the undersigned at the letterhead address and telephone number.
Sincerely,
Amber Greer MWD Operations Office Administrator

    <p class="styleSans6.0000<enum PANGO_WEIGHT_NORMAL of type PangoWeight><enum PANGO_STYLE_NORMAL of type PangoStyle>">Attachments

    <p class="styleSans7.0000<enum PANGO_WEIGHT_NORMAL of type PangoWeight><enum PANGO_STYLE_NORMAL of type PangoStyle>">M MS Directional | M5 Guidance | MS Survey I M5 Wireline

    <p class="styleSans8.0000<enum PANGO_WEIGHT_NORMAL of type PangoWeight><enum PANGO_STYLE_NORMAL of type PangoStyle>">7821 Will Rogers Blvd. Fort Worth. Texas 76140
817.568.1038 (office) 817.568.1499 (fax) www.msenergyservices.00m


text:
package com.mikimedia.android.view;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AbsListView;
import android.widget.ScrollView;

/**
 * Swipe or Pull to finish a Activity.
 *

 * This layout must be a root layout and contains only one direct child view.
 *

 * The activity must use a theme that with translucent style.
 *
 *

 */
public class SwipeBackLayout extends ViewGroup {

    private static final String TAG = SwipeBackLayout.class.getSimpleName();

    public enum DragEdge {
        LEFT,

        TOP,

        RIGHT,

        BOTTOM
    }

    private DragEdge dragEdge = DragEdge.TOP;

    public void setDragEdge(DragEdge dragEdge) {
        this.dragEdge = dragEdge;
    }

    private static final double AUTO_FINISHED_SPEED_LIMIT = 2000.0;

    private final ViewDragHelper viewDragHelper;

    private View target;

    private View scrollChild;

    private int verticalDragRange = 0;

    private int horizontalDragRange = 0;

    private int draggingState = 0;

    private int draggingOffset;

    /**
     * Whether allow to pull this layout.
     */
    private boolean enablePullToBack = true;

    private static final float BACK_FACTOR = 0.5f;

    /**
     * the anchor of calling finish.
     */
    private float finishAnchor = 0;

    /**
     * Set the anchor of calling finish.
     *
     * @param offset
     */
    public void setFinishAnchor(float offset) {
        finishAnchor = offset;
    }

    private boolean enableFlingBack = true;

    /**
     * Whether allow to finish activity by fling the layout.
     *
     * @param b
     */
    public void setEnableFlingBack(boolean b) {
        enableFlingBack = b;
    }

    private SwipeBackListener swipeBackListener;

    @Deprecated
    public void setOnPullToBackListener(SwipeBackListener listener) {
        swipeBackListener = listener;
    }

    public void setOnSwipeBackListener(SwipeBackListener listener) {
        swipeBackListener = listener;
    }

    public SwipeBackLayout(Context context) {
        this(context, null);
    }

    public SwipeBackLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        viewDragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelperCallBack());
    }

    public void setScrollChild(View view) {
        scrollChild = view;
    }

    public void setEnablePullToBack(boolean b) {
        enablePullToBack = b;
    }

    private void ensureTarget() {
        if (target == null) {
            if (getChildCount() > 1) {
                throw new IllegalStateException("SwipeBackLayout must contains only one direct child");
            }
            target = getChildAt(0);

            if (scrollChild == null && target != null) {
                if (target instanceof ViewGroup) {
                    findScrollView((ViewGroup) target);
                } else {
                    scrollChild = target;
                }

            }
        }
    }

    /**
     * Find out the scrollable child view from a ViewGroup.
     *
     * @param viewGroup
     */
    private void findScrollView(ViewGroup viewGroup) {
        scrollChild = viewGroup;
        if (viewGroup.getChildCount() > 0) {
            int count = viewGroup.getChildCount();
            View child;
            for (int i = 0; i < count; i++) {
                child = viewGroup.getChildAt(i);
                if (child instanceof AbsListView || child instanceof ScrollView || child instanceof ViewPager || child instanceof WebView) {
                    scrollChild = child;
                    return;
                }
            }
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        if (getChildCount() == 0) return;

        View child = getChildAt(0);

        int childWidth = width - getPaddingLeft() - getPaddingRight();
        int childHeight = height - getPaddingTop() - getPaddingBottom();
        int childLeft = getPaddingLeft();
        int childTop = getPaddingTop();
        int childRight = childLeft + childWidth;
        int childBottom = childTop + childHeight;
        child.layout(childLeft, childTop, childRight, childBottom);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (getChildCount() > 1) {
            throw new IllegalStateException("SwipeBackLayout must contains only one direct child.");
        }

        if (getChildCount() > 0) {
            int measureWidth = MeasureSpec.makeMeasureSpec(getMeasuredWidth() - getPaddingLeft() - getPaddingRight(), MeasureSpec.EXACTLY);
            int measureHeight = MeasureSpec.makeMeasureSpec(getMeasuredHeight() - getPaddingTop() - getPaddingBottom(), MeasureSpec.EXACTLY);
            getChildAt(0).measure(measureWidth, measureHeight);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        verticalDragRange = h;
        horizontalDragRange = w;

        switch (dragEdge) {
            case TOP:
            case BOTTOM:
                finishAnchor = finishAnchor > 0 ? finishAnchor : verticalDragRange * BACK_FACTOR;
                break;
            case LEFT:
            case RIGHT:
                finishAnchor = finishAnchor > 0 ? finishAnchor : horizontalDragRange * BACK_FACTOR;
                break;
        }
    }

    private int getDragRange() {
        switch (dragEdge) {
            case TOP:
            case BOTTOM:
                return verticalDragRange;
            case LEFT:
            case RIGHT:
                return horizontalDragRange;
            default:
                return verticalDragRange;
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean handled = false;
        ensureTarget();
        if (isEnabled()) {
            handled = viewDragHelper.shouldInterceptTouchEvent(ev);
        } else {
            viewDragHelper.cancel();
        }
        return !handled ? super.onInterceptTouchEvent(ev) : handled;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        viewDragHelper.processTouchEvent(event);
        return true;
    }

    @Override
    public void computeScroll() {
        if (viewDragHelper.continueSettling(true)) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }

    public boolean canChildScrollUp() {
        return ViewCompat.canScrollVertically(scrollChild, -1);
    }

    public boolean canChildScrollDown() {
        return ViewCompat.canScrollVertically(scrollChild, 1);
    }

    private boolean canChildScrollRight() {
        return ViewCompat.canScrollHorizontally(scrollChild, -1);
    }

    private boolean canChildScrollLeft() {
        return ViewCompat.canScrollHorizontally(scrollChild, 1);
    }

    private void finish() {
        Activity act = (Activity) getContext();
        act.finish();
        act.overridePendingTransition(0, android.R.anim.fade_out);
    }

    private class ViewDragHelperCallBack extends ViewDragHelper.Callback {

        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            return child == target && enablePullToBack;
        }

        @Override
        public int getViewVerticalDragRange(View child) {
            return verticalDragRange;
        }

        @Override
        public int getViewHorizontalDragRange(View child) {
            return horizontalDragRange;
        }

        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {

            int result = 0;

            if (dragEdge == DragEdge.TOP && !canChildScrollUp() && top > 0) {
                final int topBound = getPaddingTop();
                final int bottomBound = verticalDragRange;
                result = Math.min(Math.max(top, topBound), bottomBound);
            } else if (dragEdge == DragEdge.BOTTOM && !canChildScrollDown() && top < 0) {
                final int topBound = -verticalDragRange;
                final int bottomBound = getPaddingTop();
                result = Math.min(Math.max(top, topBound), bottomBound);
            }

            return result;
        }

        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {

            int result = 0;

            if (dragEdge == DragEdge.LEFT && !canChildScrollRight() && left > 0) {
                final int leftBound = getPaddingLeft();
                final int rightBound = horizontalDragRange;
                result = Math.min(Math.max(left, leftBound), rightBound);
            } else if (dragEdge == DragEdge.RIGHT && !canChildScrollLeft() && left < 0) {
                final int leftBound = -horizontalDragRange;
                final int rightBound = getPaddingLeft();
                result = Math.min(Math.max(left, leftBound), rightBound);
            }

            return result;
        }

        @Override
        public void onViewDragStateChanged(int state) {
            if (state == draggingState) return;

            if ((draggingState == ViewDragHelper.STATE_DRAGGING || draggingState == ViewDragHelper.STATE_SETTLING) &&
                    state == ViewDragHelper.STATE_IDLE) {
                // the view stopped from moving.
                if (draggingOffset == getDragRange()) {
//                    if (swipeBackListener != null) swipeBackListener.onFinish();
                    finish();
                }
            }

            draggingState = state;
        }

        @Override
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
            switch (dragEdge) {
                case TOP:
                case BOTTOM:
                    draggingOffset = Math.abs(top);
                    break;
                case LEFT:
                case RIGHT:
                    draggingOffset = Math.abs(left);
                    break;
                default:
                    break;
            }

            //The proportion of the sliding.
            float fractionAnchor = (float) draggingOffset / finishAnchor;
            if (fractionAnchor >= 1) fractionAnchor = 1;

            float fractionScreen = (float) draggingOffset / (float) getDragRange();
            if (fractionScreen >= 1) fractionScreen = 1;

            if (swipeBackListener != null) {
                swipeBackListener.onViewPositionChanged(fractionAnchor, fractionScreen, draggingOffset);
            }
        }

        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            if (draggingOffset == 0) return;

            if (draggingOffset == getDragRange()) return;

            boolean isBack = false;

            if (enableFlingBack && backBySpeed(xvel, yvel)) {
                isBack = !canChildScrollUp();
            } else if (draggingOffset >= finishAnchor) {
                isBack = true;
            } else if (draggingOffset < finishAnchor) {
                isBack = false;
            }

            if (isBack && swipeBackListener != null) swipeBackListener.onFinish();

            int finalLeft;
            int finalTop;
            switch (dragEdge) {
                case LEFT:
                    finalLeft = isBack ? horizontalDragRange : 0;
                    smoothScrollToX(finalLeft);
                    break;
                case RIGHT:
                    finalLeft = isBack ? -horizontalDragRange : 0;
                    smoothScrollToX(finalLeft);
                    break;
                case TOP:
                    finalTop = isBack ? verticalDragRange : 0;
                    smoothScrollToY(finalTop);
                    break;
                case BOTTOM:
                    finalTop = isBack ? -verticalDragRange : 0;
                    smoothScrollToY(finalTop);
                    break;
            }

        }
    }

    private boolean backBySpeed(float xvel, float yvel) {
        switch (dragEdge) {
            case TOP:
            case BOTTOM:
                if (Math.abs(yvel) > Math.abs(xvel) && Math.abs(yvel) > AUTO_FINISHED_SPEED_LIMIT) {
                    return dragEdge == DragEdge.TOP ? !canChildScrollUp() : !canChildScrollDown();
                }
                break;
            case LEFT:
            case RIGHT:
                if (Math.abs(xvel) > Math.abs(yvel) && Math.abs(xvel) > AUTO_FINISHED_SPEED_LIMIT) {
                    return dragEdge == DragEdge.LEFT ? !canChildScrollLeft() : !canChildScrollRight();
                }
                break;
        }
        return false;
    }

    private void smoothScrollToX(int finalLeft) {
        if (viewDragHelper.settleCapturedViewAt(finalLeft, 0)) {
            ViewCompat.postInvalidateOnAnimation(SwipeBackLayout.this);
        }
    }

    private void smoothScrollToY(int finalTop) {
        if (viewDragHelper.settleCapturedViewAt(0, finalTop)) {
            ViewCompat.postInvalidateOnAnimation(SwipeBackLayout.this);
        }
    }

    public interface SwipeBackListener {

        /**
         * Return scrolled fraction of the layout.
         *
         * @param fractionAnchor relative to the anchor.
         * @param fractionScreen relative to the screen.
         */
        public void onViewPositionChanged(float fractionAnchor, float fractionScreen, int draggingOffset);

        public void onFinish();
    }

}


text:
package net.swisstech.swissarmyknife.util;

import java.util.Deque;
import java.util.LinkedList;

/**
 * simple non-vector based stack, allows null values
 */
public class Stack<T> {

    private final Deque<T> elements = new LinkedList<>();

    public T peek() {
        return elements.peekFirst();
    }

    public T pop() {
        return elements.removeFirst();
    }

    public void push(T element) {
        elements.offerFirst(element);
    }

    public int size() {
        return elements.size();
    }

    public int clear() {
        int size = size();
        elements.clear();
        return size;
    }
}


text:
package de.pfabulist.lindwurm.niotest.tests.topics;

public interface SlowTest extends Topic {
}


text:
package org.apache.uima.flow;

import org.apache.uima.analysis_engine.ResultSpecification;

/**
 * Special type of SimpleStep intended only for backwards compatibility with the behavior of the
 * Capability Language Flow in UIMA 1.x. Allows the Flow Controller to set a Result Specification
 * for the AE that will be called next.
 *
 * @deprecated For backwards compatibility with Capability Language Flow only. User-developed Flow
 *             Controllers should not use this.
 */
@Deprecated
public class SimpleStepWithResultSpec extends SimpleStep {

  /**
   * @param aCasProcessorKey
   *          key
   * @param aResultSpec
   *          Result Specification
   */
  public SimpleStepWithResultSpec(String aCasProcessorKey, ResultSpecification aResultSpec) {
    super(aCasProcessorKey);
    setResultSpecification(aResultSpec);
  }

  /**
   * Gets the results specification
   *
   * @return the results specification
   */
  public ResultSpecification getResultSpecification() {
    return mResultSpec;
  }

  /**
   * Sets the results specification
   *
   * @param aResultSpec
   *          the results specification to set
   */
  public void setResultSpecification(ResultSpecification aResultSpec) {
    mResultSpec = aResultSpec;
  }

  private ResultSpecification mResultSpec;
}


text:
package sun.security.util;

import java.net.URI;
import java.net.URISyntaxException;
import java.security.GeneralSecurityException;

/**
 * A utility class to expand properties embedded in a string.
 * Strings of the form ${some.property.name} are expanded to
 * be the value of the property. Also, the special ${/} property
 * is expanded to be the same as file.separator. If a property
 * is not set, a GeneralSecurityException will be thrown.
 *
 * @author Roland Schemers
 */
public class PropertyExpander {

    public static class ExpandException extends GeneralSecurityException {

        private static final long serialVersionUID = -7941948581406161702L;

        public ExpandException(String msg) {
            super(msg);
        }
    }

    public static String expand(String value)
        throws ExpandException
    {
        return expand(value, false);
    }

     public static String expand(String value, boolean encodeURL)
         throws ExpandException
     {
        if (value == null)
            return null;

        int p = value.indexOf("${", 0);

        // no special characters
        if (p == -1) return value;

        StringBuffer sb = new StringBuffer(value.length());
        int max = value.length();
        int i = 0;  // index of last character we copied

    scanner:
        while (p < max) {
            if (p > i) {
                // copy in anything before the special stuff
                sb.append(value.substring(i, p));
                i = p;
            }
            int pe = p+2;

            // do not expand ${{ ... }}
            if (pe < max && value.charAt(pe) == '{') {
                pe = value.indexOf("}}", pe);
                if (pe == -1 || pe+2 == max) {
                    // append remaining chars
                    sb.append(value.substring(p));
                    break scanner;
                } else {
                    // append as normal text
                    pe++;
                    sb.append(value.substring(p, pe+1));
                }
            } else {
                while ((pe < max) && (value.charAt(pe) != '}')) {
                    pe++;
                }
                if (pe == max) {
                    // no matching '}' found, just add in as normal text
                    sb.append(value.substring(p, pe));
                    break scanner;
                }
                String prop = value.substring(p+2, pe);
                if (prop.equals("/")) {
                    sb.append(java.io.File.separatorChar);
                } else {
                    String val = System.getProperty(prop);
                    if (val != null) {
                        if (encodeURL) {
                            // encode 'val' unless it's an absolute URI
                            // at the beginning of the string buffer
                            try {
                                if (sb.length() > 0 ||
                                    !(new URI(val)).isAbsolute()) {
                                    val = sun.net.www.ParseUtil.encodePath(val);
                                }
                            } catch (URISyntaxException use) {
                                val = sun.net.www.ParseUtil.encodePath(val);
                            }
                        }
                        sb.append(val);
                    } else {
                        throw new ExpandException(
                                             "unable to expand property " +
                                             prop);
                    }
                }
            }
            i = pe+1;
            p = value.indexOf("${", i);
            if (p == -1) {
                // no more to expand. copy in any extra
                if (i < max) {
                    sb.append(value.substring(i, max));
                }
                // break out of loop
                break scanner;
            }
        }
        return sb.toString();
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="Domain" namespace="Domain" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Floor" table="Floor" lazy="true" >
    <id name="Id" column="Id">
      <generator class="identity" />
    </id>
    <property name="NumOfFloor">
      <column name="NumOfFloot" sql-type="int" not-null="true" />
    </property>
    <bag name="Rooms">
      <key column="FloorId" />
      <one-to-many class="Room" />
    </bag>
  </class>
</hibernate-mapping>


text:
ActiveRecord::Schema.define(version: 20160513130337) do

  create_table "addresses", force: :cascade do |t|
    t.string   "street"
    t.string   "city"
    t.string   "country"
    t.integer  "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["user_id"], name: "index_addresses_on_user_id"
  end

  create_table "comments", force: :cascade do |t|
    t.text     "body"
    t.integer  "commentable_id"
    t.string   "commentable_type"
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.index ["commentable_id"], name: "index_comments_on_commentable_id"
  end

  create_table "enrollments", force: :cascade do |t|
    t.integer  "game_id"
    t.integer  "user_id"
    t.string   "category"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["game_id"], name: "index_enrollments_on_game_id"
    t.index ["user_id"], name: "index_enrollments_on_user_id"
  end

  create_table "events", force: :cascade do |t|
    t.string   "title"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "events_users", force: :cascade do |t|
    t.integer "user_id"
    t.integer "event_id"
    t.index ["event_id"], name: "index_events_users_on_event_id"
    t.index ["user_id"], name: "index_events_users_on_user_id"
  end

  create_table "games", force: :cascade do |t|
    t.string   "title"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "payment_histories", force: :cascade do |t|
    t.integer  "purse_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["purse_id"], name: "index_payment_histories_on_purse_id"
  end

  create_table "posts", force: :cascade do |t|
    t.integer  "user_id"
    t.text     "body"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["user_id"], name: "index_posts_on_user_id"
  end

  create_table "purses", force: :cascade do |t|
    t.integer  "user_id"
    t.integer  "funds"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["user_id"], name: "index_purses_on_user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end


text:
<?php

namespace Mundocente;

use Illuminate\Database\Eloquent\Model;

class Favorito extends Model
{
    protected $table = 'favoritos';

    protected $fillable = ['id_favorito', 'id_user_fk', 'id_publication_fk'];
}


text:
require 'test_helper'

class AttendanceTest < ActiveSupport::TestCase

  test 'has attributes' do
    attendance = attendances(:one)
    assert(attendance.rating == 1)
    assert(attendance.text == 'Attendance')
  end

  test 'belongs to session' do
    attendance = attendances(:one)
    session = sessions(:keynote)
    assert(attendance.session.class.name == 'Session')
    assert(attendance.session == session)
  end

  test 'belongs to speaker' do
    attendance = attendances(:one)
    person = people(:landon)
    assert(attendance.person.class.name == 'Person')
    assert(attendance.person == person)
  end

end


text:
package com.github.ethan0w.sjblog.model;

/**
 * category is a kind of tag
 * @author hy
 *
 */
public class Category extends Tag {

}


text:
import jinja2
import netaddr
from oslo_config import cfg
import six

from neutron.agent.linux import external_process
from neutron.agent.linux import utils
from neutron.common import constants
from neutron.openstack.common import log as logging

RADVD_SERVICE_NAME = 'radvd'
RADVD_SERVICE_CMD = 'radvd'

LOG = logging.getLogger(__name__)

OPTS = [
    cfg.StrOpt('ra_confs',
               default='$state_path/ra',
               help=_('Location to store IPv6 RA config files')),
]

cfg.CONF.register_opts(OPTS)

CONFIG_TEMPLATE = jinja2.Template("""interface {{ interface_name }}
{
   AdvSendAdvert on;
   MinRtrAdvInterval 3;
   MaxRtrAdvInterval 10;

   {% if ra_mode == constants.DHCPV6_STATELESS %}
   AdvOtherConfigFlag on;
   {% endif %}

   {% if ra_mode == constants.DHCPV6_STATEFUL %}
   AdvManagedFlag on;
   {% endif %}

   {% if ra_mode in (constants.IPV6_SLAAC, constants.DHCPV6_STATELESS) %}
   prefix {{ prefix }}
   {
        AdvOnLink on;
        AdvAutonomous on;
   };
   {% endif %}
};
""")

class DaemonMonitor(object):
    """Manage the data and state of an radvd process."""

    def __init__(self, router_id, router_ns, process_monitor, dev_name_helper):
        self._router_id = router_id
        self._router_ns = router_ns
        self._process_monitor = process_monitor
        self._dev_name_helper = dev_name_helper

    def _generate_radvd_conf(self, router_ports):
        radvd_conf = utils.get_conf_file_name(cfg.CONF.ra_confs,
                                              self._router_id,
                                              'radvd.conf',
                                              True)
        buf = six.StringIO()
        for p in router_ports:
            prefix = p['subnet']['cidr']
            if netaddr.IPNetwork(prefix).version == 6:
                interface_name = self._dev_name_helper(p['id'])
                ra_mode = p['subnet']['ipv6_ra_mode']
                buf.write('%s' % CONFIG_TEMPLATE.render(
                    ra_mode=ra_mode,
                    interface_name=interface_name,
                    prefix=prefix,
                    constants=constants))

        utils.replace_file(radvd_conf, buf.getvalue())
        return radvd_conf

    def _get_radvd_process_manager(self, callback=None):
        return external_process.ProcessManager(
            uuid=self._router_id,
            default_cmd_callback=callback,
            namespace=self._router_ns,
            service=RADVD_SERVICE_NAME,
            conf=cfg.CONF)

    def _spawn_radvd(self, radvd_conf):
        def callback(pid_file):
            # we need to use -m syslog and f.e. not -m stderr (the default)
            # or -m stderr_syslog so that radvd 2.0+ will close stderr and
            # exit after daemonization; otherwise, the current thread will
            # be locked waiting for result from radvd that won't ever come
            # until the process dies
            radvd_cmd = [RADVD_SERVICE_CMD,
                         '-C', '%s' % radvd_conf,
                         '-p', '%s' % pid_file,
                         '-m', 'syslog']
            return radvd_cmd

        pm = self._get_radvd_process_manager(callback)
        pm.enable(reload_cfg=True)
        self._process_monitor.register(uuid=self._router_id,
                                       service_name=RADVD_SERVICE_NAME,
                                       monitored_process=pm)
        LOG.debug("radvd enabled for router %s", self._router_id)

    def enable(self, router_ports):
        for p in router_ports:
            if netaddr.IPNetwork(p['subnet']['cidr']).version == 6:
                break
        else:
            # Kill the daemon if it's running
            self.disable()
            return

        LOG.debug("Enable IPv6 RA for router %s", self._router_id)
        radvd_conf = self._generate_radvd_conf(router_ports)
        self._spawn_radvd(radvd_conf)

    def disable(self):
        self._process_monitor.unregister(uuid=self._router_id,
                                         service_name=RADVD_SERVICE_NAME)
        pm = self._get_radvd_process_manager()
        pm.disable()
        utils.remove_conf_files(cfg.CONF.ra_confs, self._router_id)
        LOG.debug("radvd disabled for router %s", self._router_id)

    @property
    def enabled(self):
        return self._get_radvd_process_manager().active


text:
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <malloc.h>

#include "circbuf.h"

// --- Circbuf data structure ----

/** Offset in void* buffer */
#define PV_OFFS(pvBuf, elem_size, index) ((uint8_t*)(pvBuf) + ((elem_size)*(index)))

// Instance structure
struct circbuf_struct {
	void *buf;
	size_t elem_size;
	size_t cap;
	size_t lr; // last read pos
	size_t nw; // next write pos
};

/**
 * @brief Write data to a CircBuf slot
 * @param cb     : circbuf
 * @param index  : slot index
 * @param source : data source
 */
static void write_buffer(CircBuf *cb, size_t index, const void *source)
{
	memcpy(PV_OFFS(cb->buf, cb->elem_size, index), source, cb->elem_size);
}

/**
 * @brief Copy data from a CircBuf slot to a buffer
 * @param cb     : circbuf
 * @param index  : slot index
 * @param dest   : destination buffer
 */
static void read_buffer(const CircBuf *cb, size_t index, void *dest)
{
	memcpy(dest, PV_OFFS(cb->buf, cb->elem_size, index), cb->elem_size);
}

/** Create a cbuf */
CircBuf *cbuf_create(size_t capacity, size_t elem_size)
{
	// add one, because one is always unused.
	capacity++;

	// Allocate the structure
	CircBuf *cb = malloc(sizeof(CircBuf));
	if (cb == NULL) {
		return NULL;
	}

	// allocate the buffer
	cb->buf = malloc(capacity * elem_size);
	if (cb->buf == NULL) {
		free(cb); // fail - free structure.
		return NULL;
	}

	// set capacity, clear state
	cb->elem_size = elem_size;
	cb->cap = capacity;
	cbuf_clear(cb);

	return cb;
}

/** Release cbuf memory */
void cbuf_destroy(CircBuf *cb)
{
	if (cb != NULL) {
		if (cb->buf != NULL) {
			free(cb->buf);
		}

		free(cb);
	}
}

/** Check if cbuf is full */
bool cbuf_full(const CircBuf *cb)
{
	if (cb == NULL) return false;

	return (cb->lr == cb->nw);
}

/** Check if cbuf is empty */
bool cbuf_empty(const CircBuf *cb)
{
	if (cb == NULL) return true;

	return ((cb->lr + 1) % cb->cap) == cb->nw;
}

/** Write a byte to the buffer, if space left */
bool cbuf_append(CircBuf *cb, const void *source)
{
	if (cb == NULL) return false;
	if (source == NULL) return false;
	if (cbuf_full(cb)) return false;

	write_buffer(cb, cb->nw, source);

	// increment
	cb->nw++;
	if (cb->nw == cb->cap) cb->nw = 0;

	return true;
}

/** Push value to the end, like a stack. */
bool cbuf_push(CircBuf *cb, const void *source)
{
	if (cb == NULL) return false;
	if (source == NULL) return false;
	if (cbuf_full(cb)) return false;

	write_buffer(cb, cb->lr, source);

	// move lr back
	if (cb->lr == 0) {
		cb->lr = cb->cap - 1; // wrap to the end
	} else {
		cb->lr--;
	}

	return true;
}

/** Read one byte, if not empty. */
bool cbuf_pop(CircBuf *cb, void *dest)
{
	if (cb == NULL || dest == NULL) return false;
	if (cbuf_empty(cb)) return false;

	// increment
	cb->lr++;
	if (cb->lr == cb->cap) cb->lr = 0;

	read_buffer(cb, cb->lr, dest);

	return true;
}

/** Clear a cbuf */
void cbuf_clear(CircBuf *cb)
{
	if (cb == NULL) return;

	cb->lr = cb->cap - 1;
	cb->nw = 0;
}


text:
<!DOCTYPE html>

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <title>OpenLayers 2 KML Track in a PointTrack Layer Example</title>
        <link rel="stylesheet" href="../theme/default/style.css" type="text/css">
        <link rel="stylesheet" href="../theme/default/google.css" type="text/css">
        <link rel="stylesheet" href="style.css" type="text/css">

Parsing gx:Track in KML

            Demonstrates populating a PointTrack layer with gx:Track elements from KML.

                If a KML document contains <gx:Track>
                elements and the extractTracks property is set true on the
                parser, features will be created that represent track points.
                These track points can easily be visualized as track lines with
                a PointTrack layer, preserving the KML's original
                styles.

                View the kml-pointtrack.js
                source to see how this is done.


text:
<?xml version="1.0" encoding="UTF-8" ?>
<module>
    <name>ulozenka</name>
    <displayName><![CDATA[Uloženka]]></displayName>
    <version><![CDATA[1.3b]]></version>
    <description><![CDATA[Modul pro ulozenka.cz]]></description>
    <author><![CDATA[prestahost.cz]]></author>
    <tab><![CDATA[shipping_logistics]]></tab>
    <confirmUninstall><![CDATA[Opravdu odinstalovat?]]></confirmUninstall>
    <is_configurable>1</is_configurable>
    <need_instance>1</need_instance>
    <limited_countries></limited_countries>
</module>


text:
import { Match } from 'found';
declare type Props = {
    match: Match;
};
declare const Support: ({ match }: Props) => JSX.Element | null;
export default Support;


text:
require 'test_helper'

class ListasControllerTest < ActionController::TestCase
  setup do
    @lista = listas(:one)
  end

  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:listas)
  end

  test "should get new" do
    get :new
    assert_response :success
  end

  test "should create lista" do
    assert_difference('Lista.count') do
      post :create, lista: {  }
    end

    assert_redirected_to lista_path(assigns(:lista))
  end

  test "should show lista" do
    get :show, id: @lista
    assert_response :success
  end

  test "should get edit" do
    get :edit, id: @lista
    assert_response :success
  end

  test "should update lista" do
    patch :update, id: @lista, lista: {  }
    assert_redirected_to lista_path(assigns(:lista))
  end

  test "should destroy lista" do
    assert_difference('Lista.count', -1) do
      delete :destroy, id: @lista
    end

    assert_redirected_to listas_path
  end
end


text:
import mock
from mock import MagicMock
from requests import Response

from summary import Summary

@mock.patch('summary.request.get')
def test_summary(mock_request):

    # Given
    url = 'http://www.google.com'
    mock_response = Response()
    mock_response.headers.get = MagicMock(return_value = 'html')
    mock_response.url = url
    mock_response.encoding = 'UTF-8'
    mock_response.consumed = False
    mock_response.raw = MagicMock()
    mock_response.iter_content = lambda s: ['<title>Test Title']
    mock_request.return_value = mock_response

    # When
    summ = Summary(url)
    # summ._html = '<title>Test Title'
    summ.extract()

    # Then
    # mock_response.raw.close.assert_called_with()
    assert summ.title == 'Test Title'


text:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".activity.BlackActivity">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <!-- 标题 -->

        <TextView
            style="@style/title_style"
            android:text="通讯卫士"/>

        <!-- 添加黑名单数据的按钮 -->

        <ImageView
            android:id="@+id/iv_black_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dip"
            android:src="@drawable/black_add_iv_selector"/>
    </RelativeLayout>

    <!-- 数据部分 -->

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">

        <!-- 加载数据进度 -->

        <include layout="@layout/progressbar_circle"/>

        <!-- 有数据 -->

        <ListView
            android:id="@+id/lv_black_showdata"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:visibility="gone">
        </ListView>

        <!-- 没有数据 -->

        <ImageView
            android:id="@+id/iv_black_nodata"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@mipmap/empty"
            android:visibility="gone"/>
    </FrameLayout>
    <!-- 分页按钮的添加-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <!-- 首页-->
        <Button
            android:onClick="shou"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/selector_bt_gv"
            android:text="首"/>
        <!-- 尾页页-->
        <Button
            android:onClick="wei"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/selector_bt_gv"
            android:text="尾"/>
        <!-- 上一页-->
        <Button
            android:onClick="shang"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/selector_bt_gv"
            android:text="上"/>
        <!-- 下一页-->
        <Button
            android:onClick="xia"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/selector_bt_gv"
            android:text="下"/>
        <!-- 跳转-->
        <Button
            android:onClick="tiao"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/selector_bt_gv"
            android:text="跳"/>
        <!-- EditText-->
        <EditText
            android:id="@+id/et_web_black_tiaopage"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="10"/>
        <TextView
            android:inputType="number"
            android:id="@+id/tv_web_balck_pagemsg"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="3/10"/>
    </LinearLayout>
</LinearLayout>


text:
'use strict';

angular.module('geboBlogHaiApp', ['ngResource', 'ngRoute', 'ui.bootstrap',
                                  'gebo-client-token', 'gebo-client-token'])
  .config(['$routeProvider', function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  }]);


text:
package org.spongepowered.common.data.manipulator.immutable.block;

import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.manipulator.immutable.block.ImmutableSuspendedData;
import org.spongepowered.api.data.manipulator.mutable.block.SuspendedData;
import org.spongepowered.api.data.value.immutable.ImmutableValue;
import org.spongepowered.common.data.manipulator.immutable.common.AbstractImmutableBooleanData;
import org.spongepowered.common.data.manipulator.mutable.block.SpongeSuspendedData;
import org.spongepowered.common.data.value.immutable.ImmutableSpongeValue;

public class ImmutableSpongeSuspendedData extends AbstractImmutableBooleanData<ImmutableSuspendedData, SuspendedData>
    implements ImmutableSuspendedData {

    public ImmutableSpongeSuspendedData(boolean value) {
        super(ImmutableSuspendedData.class, value, Keys.SUSPENDED, SpongeSuspendedData.class);
    }

    @Override
    public ImmutableValue<Boolean> suspended() {
        return ImmutableSpongeValue.cachedOf(Keys.SUSPENDED, false, this.value);
    }

    @Override
    protected ImmutableValue<?> getValueGetter() {
        return suspended();
    }
}


text:
ActiveRecord::Schema.define(version: 20131109133336) do

  create_table "menus", force: true do |t|
    t.string   "name"
    t.integer  "main_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end


text:
SDRSharp AudioScope
========================

Oscilloscope plugin for SDRSharp (SDR#, http://sdrsharp.com/)

Igor Koshelev (igor@lifemotion.ru), 2014.

Plugin source code licensed on Apache Licence. Use it as you wish.

refs folder contains some files from SDR#. These files included only for easy plugin compilation.
It owned by SDR# respective autors.