if not hit_dtype:<EOL><INDENT>hit_dtype = np.dtype([])<EOL><DEDENT>else:<EOL><INDENT>hit_dtype = np.dtype(hit_dtype)<EOL><DEDENT>cluster_hits_descr = hit_dtype.descr<EOL>for dtype_name, dtype in self._default_cluster_hits_descr:<EOL><INDENT>if self._hit_fields_mapping[dtype_name] not in hit_dtype.fields:<EOL><INDENT>cluster_hits_descr.append((dtype_name, dtype))<EOL><DEDENT><DEDENT>self._cluster_hits_descr = cluster_hits_descr<EOL>self._init_arrays(size=<NUM_LIT:0>)<EOL>

Set the data type of the hits.

        Fields that are not mentioned here are NOT copied into the clustered hits array.
        Clusterizer has to know the hit data type to produce the clustered hit result with the same data types.

        Parameters:
        -----------
        hit_dtype : numpy.dtype or equivalent
            Defines the dtype of the hit array.

        Example:
        --------
        hit_dtype = [("column", np.uint16), ("row", np.uint16)], where
        "column", "row" is the field name of the input hit array.


self.cluster_functions._end_of_cluster_function = self._jitted(function)<EOL>self._end_of_cluster_function = function<EOL>

Adding function to module.
        This is maybe the only way to make the clusterizer to work with multiprocessing.


self._ignore_same_hits = value<EOL>

Whether a duplicate hit in the event with the same column and row is ignored or not.


def threshold_img(data, threshold, mask=None, mask_out='<STR_LIT>'):

if mask is not None:<EOL><INDENT>mask = threshold_img(mask, threshold, mask_out=mask_out)<EOL>return data * mask.astype(bool)<EOL><DEDENT>if mask_out.startswith('<STR_LIT:b>'):<EOL><INDENT>data[data < threshold] = <NUM_LIT:0><EOL><DEDENT>elif mask_out.startswith('<STR_LIT:a>'):<EOL><INDENT>data[data > threshold] = <NUM_LIT:0><EOL><DEDENT>return data<EOL>

Threshold data, setting all values in the array above/below threshold
    to zero.
    Args:
        data (ndarray): The image data to threshold.
        threshold (float): Numeric threshold to apply to image.
        mask (ndarray): Optional 1D-array with the same length as the data. If
            passed, the threshold is first applied to the mask, and the
            resulting indices are used to threshold the data. This is primarily
            useful when, e.g., applying a statistical threshold to a z-value
            image based on a p-value threshold.
        mask_out (str): Thresholding direction. Can be 'below' the threshold
            (default) or 'above' the threshold. Note: use 'above' when masking
            based on p values.


def get_sphere(coords, r=<NUM_LIT:4>, vox_dims=(<NUM_LIT:2>, <NUM_LIT:2>, <NUM_LIT:2>), dims=(<NUM_LIT>, <NUM_LIT>, <NUM_LIT>)):

r = float(r)<EOL>xx, yy, zz = [slice(-r / vox_dims[i], r / vox_dims[<EOL>i] + <NUM_LIT>, <NUM_LIT:1>) for i in range(len(coords))]<EOL>cube = np.vstack([row.ravel() for row in np.mgrid[xx, yy, zz]])<EOL>sphere = cube[:, np.sum(np.dot(np.diag(<EOL>vox_dims), cube) ** <NUM_LIT:2>, <NUM_LIT:0>) ** <NUM_LIT> <= r]<EOL>sphere = np.round(sphere.T + coords)<EOL>return sphere[(np.min(sphere, <NUM_LIT:1>) >= <NUM_LIT:0>) &<EOL>(np.max(np.subtract(sphere, dims), <NUM_LIT:1>) <= -<NUM_LIT:1>), :].astype(int)<EOL>

Return all points within r mm of coordinates. Generates a cube
   and then discards all points outside sphere. Only returns values that
   fall within the dimensions of the image.


def add(self, layers, above=None, below=None):

def add_named_layer(name, image):<EOL><INDENT>image = self.get_image(image, output='<STR_LIT>')<EOL>if above is not None:<EOL><INDENT>image[image < above] = <NUM_LIT:0.><EOL><DEDENT>if below is not None:<EOL><INDENT>image[image > below] = <NUM_LIT:0.><EOL><DEDENT>self.layers[name] = image<EOL>self.stack.append(name)<EOL><DEDENT>if isinstance(layers, dict):<EOL><INDENT>for (name, image) in layers.items():<EOL><INDENT>add_named_layer(name, image)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if not isinstance(layers, list):<EOL><INDENT>layers = [layers]<EOL><DEDENT>for image in layers:<EOL><INDENT>name = '<STR_LIT>' % len(self.stack)<EOL>add_named_layer(name, image)<EOL><DEDENT><DEDENT>self.set_mask()<EOL>

Add one or more layers to the stack of masking layers.
        Args:
            layers: A string, NiBabel image, list, or dict. If anything other
                than a dict is passed, assigns sequential layer names based on
                the current position in stack; if a dict, uses key as the name
                and value as the mask image.


self.layers = {}<EOL>self.stack = []<EOL>self.set_mask()<EOL>self.n_vox_in_vol = len(np.where(self.current_mask)[<NUM_LIT:0>])<EOL>

Reset/remove all layers, keeping only the initial volume.


def get_image(self, image, output='<STR_LIT>'):

if isinstance(image, string_types):<EOL><INDENT>image = nb.load(image)<EOL><DEDENT>if type(image).__module__.startswith('<STR_LIT>'):<EOL><INDENT>if output == '<STR_LIT:image>':<EOL><INDENT>return image<EOL><DEDENT>image = image.get_data()<EOL><DEDENT>if not type(image).__module__.startswith('<STR_LIT>'):<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if image.shape[:<NUM_LIT:3>] == self.volume.shape:<EOL><INDENT>if output == '<STR_LIT:image>':<EOL><INDENT>return nb.nifti1.Nifti1Image(image, None, self.get_header())<EOL><DEDENT>elif output == '<STR_LIT>':<EOL><INDENT>return image<EOL><DEDENT>else:<EOL><INDENT>image = image.ravel()<EOL><DEDENT><DEDENT>if output == '<STR_LIT>':<EOL><INDENT>return image.ravel()<EOL><DEDENT>image = np.reshape(image, self.volume.shape)<EOL>if output == '<STR_LIT>':<EOL><INDENT>return image<EOL><DEDENT>return nb.nifti1.Nifti1Image(image, None, self.get_header())<EOL>

A flexible method for transforming between different
        representations of image data.
        Args:
            image: The input image. Can be a string (filename of image),
                NiBabel image, N-dimensional array (must have same shape as
                self.volume), or vectorized image data (must have same length
                as current conjunction mask).
            output: The format of the returned image representation. Must be
                one of:
                    'vector': A 1D vectorized array
                    'array': An N-dimensional array, with
                        shape = self.volume.shape
                    'image': A NiBabel image
        Returns: An object containing image data; see output options above.


def mask(self, image, nan_to_num=True, layers=None, in_global_mask=False):

self.set_mask(layers)<EOL>image = self.get_image(image, output='<STR_LIT>')<EOL>if in_global_mask:<EOL><INDENT>masked_data = image[self.global_mask]<EOL>masked_data[~self.get_mask(in_global_mask=True)] = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>masked_data = image[self.current_mask]<EOL><DEDENT>if nan_to_num:<EOL><INDENT>masked_data = np.nan_to_num(masked_data)<EOL><DEDENT>return masked_data<EOL>

Vectorize an image and mask out all invalid voxels.

        Args:
            images: The image to vectorize and mask. Input can be any object
                handled by get_image().
            layers: Which mask layers to use (specified as int, string, or
                list of ints and strings). When None, applies the conjunction
                of all layers.
            nan_to_num: boolean indicating whether to convert NaNs to 0.
            in_global_mask: Whether to return the resulting masked vector in
                the globally masked space (i.e., n_voxels =
                len(self.global_mask)). If False (default), returns in the full
                image space (i.e., n_voxels = len(self.volume)).
        Returns:
          A 1D NumPy array of in-mask voxels.


self.dataset = dataset<EOL>self.data = pd.DataFrame()<EOL>

Initialize a new FeatureTable. Takes as input a parent DataSet
        instance and feature data (if provided).


counts = np.sum(self.get_feature_data() >= threshold, <NUM_LIT:0>)<EOL>return dict(zip(self.get_feature_names(), list(counts)))<EOL>

Returns a dictionary, where the keys are the feature names
        and the values are the number of studies tagged with the feature.


if isinstance(search, string_types):<EOL><INDENT>search = [search]<EOL><DEDENT>search = [s.replace('<STR_LIT:*>', '<STR_LIT>') for s in search]<EOL>cols = list(self.data.columns)<EOL>results = []<EOL>for s in search:<EOL><INDENT>results.extend([f for f in cols if re.match(s + '<STR_LIT:$>', f)])<EOL><DEDENT>return list(set(results))<EOL>

Returns all features that match any of the elements in the input
        list.

        Args:
            search (str, list): A string or list of strings defining the query.

        Returns:
            A list of matching feature names.


data = self.data.to_dense()<EOL>self.data = {<EOL>'<STR_LIT>': list(data.columns),<EOL>'<STR_LIT:index>': list(data.index),<EOL>'<STR_LIT>': sparse.csr_matrix(data.values)<EOL>}<EOL>

Convert FeatureTable to SciPy CSR matrix.


def add_features(self, features, append=True, merge='<STR_LIT>',<EOL>duplicates='<STR_LIT:ignore>', min_studies=<NUM_LIT:0.0>, threshold=<NUM_LIT>):

if (not append) or not hasattr(self, '<STR_LIT>'):<EOL><INDENT>self.feature_table = FeatureTable(self)<EOL><DEDENT>self.feature_table.add_features(features, merge=merge,<EOL>duplicates=duplicates,<EOL>min_studies=min_studies,<EOL>threshold=threshold)<EOL>

Construct a new FeatureTable from file.

        Args:
            features: Feature data to add. Can be:
                (a) A text file containing the feature data, where each row is
                a study in the database, with features in columns. The first
                column must contain the IDs of the studies to match up with the
                image data.
                (b) A pandas DataFrame, where studies are in rows, features are
                in columns, and the index provides the study IDs.
            append (bool): If True, adds new features to existing ones
                incrementally. If False, replaces old features.
            merge, duplicates, min_studies, threshold: Additional arguments
                passed to FeatureTable.add_features().


logger.info("<STR_LIT>")<EOL>if r is not None:<EOL><INDENT>self.r = r<EOL><DEDENT>self.image_table = ImageTable(self)<EOL>

Create and store a new ImageTable instance based on the current
        Dataset. Will generally be called privately, but may be useful as a
        convenience method in cases where the user wants to re-generate the
        table with a new smoothing kernel of different radius.

        Args:
            r (int): An optional integer indicating the radius of the smoothing
                kernel. By default, this is None, which will keep whatever
                value is currently set in the Dataset instance.


def get_image_data(self, ids=None, voxels=None, dense=True):

if dense and ids is None and voxels is None:<EOL><INDENT>logger.warning(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>result = self.data<EOL>if ids is not None:<EOL><INDENT>idxs = np.where(np.in1d(np.array(self.ids), np.array(ids)))[<NUM_LIT:0>]<EOL>result = result[:, idxs]<EOL><DEDENT>if voxels is not None:<EOL><INDENT>result = result[voxels, :]<EOL><DEDENT>return result.toarray() if dense else result<EOL>

Slices and returns a subset of image data.

        Args:
            ids (list, array): A list or 1D numpy array of study ids to
                return. If None, returns data for all studies.
            voxels (list, array): A list or 1D numpy array of voxel indices
                (i.e., rows) to return. If None, returns data for all voxels.
            dense (bool): Optional boolean. When True (default), convert the
                result to a dense array before returning. When False, keep as
                sparse matrix.

        Returns:
          A 2D numpy array with voxels in rows and studies in columns.


def get_feature_data(self, ids=None, features=None, dense=True):

result = self.data<EOL>if ids is not None:<EOL><INDENT>result = result.ix[ids]<EOL><DEDENT>if features is not None:<EOL><INDENT>result = result.ix[:, features]<EOL><DEDENT>return result.to_dense() if dense else result<EOL>

Slices and returns a subset of feature data.

        Args:
            ids (list, array): A list or 1D numpy array of study ids to
                return rows for. If None, returns data for all studies
                (i.e., all rows in array).
            features (list, array): A list or 1D numpy array of named features
                to return. If None, returns data for all features (i.e., all
                columns in array).
            dense (bool): Optional boolean. When True (default), convert the
                result to a dense array before returning. When False, keep as
                sparse matrix. Note that if ids is not None, the returned array
                will always be dense.
        Returns:
          A pandas DataFrame with study IDs in rows and features incolumns.


def save_images_to_file(self, ids, outroot='<STR_LIT>'):

pass<EOL>

Reconstructs vectorized images corresponding to the specified
        study ids and saves them to file, prepending with the outroot
        (default: current directory).


logger.info("<STR_LIT>" % filename)<EOL>activations = pd.read_csv(filename, sep='<STR_LIT:\t>')<EOL>activations.columns = [col.lower()<EOL>for col in list(activations.columns)]<EOL>mc = ['<STR_LIT:x>', '<STR_LIT:y>', '<STR_LIT:z>', '<STR_LIT:id>', '<STR_LIT>']<EOL>if (set(mc) - set(list(activations.columns))):<EOL><INDENT>logger.error(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL>return<EOL><DEDENT>spaces = activations['<STR_LIT>'].unique()<EOL>xyz = activations[['<STR_LIT:x>', '<STR_LIT:y>', '<STR_LIT:z>']].values<EOL>for s in spaces:<EOL><INDENT>if s != self.transformer.target:<EOL><INDENT>inds = activations['<STR_LIT>'] == s<EOL>xyz[inds] = self.transformer.apply(s, xyz[inds])<EOL><DEDENT><DEDENT>activations[['<STR_LIT:x>', '<STR_LIT:y>', '<STR_LIT:z>']] = xyz<EOL>ijk = pd.DataFrame(<EOL>transformations.xyz_to_mat(xyz), columns=['<STR_LIT:i>', '<STR_LIT>', '<STR_LIT:k>'])<EOL>activations = pd.concat([activations, ijk], axis=<NUM_LIT:1>)<EOL>return activations<EOL>

Load activation data from a text file.

        Args:
            filename (str): a string pointing to the location of the txt file
                to read from.


return np.array([[<NUM_LIT>, <NUM_LIT>, -<NUM_LIT>, -<NUM_LIT>],<EOL>[-<NUM_LIT>, <NUM_LIT>, -<NUM_LIT>, -<NUM_LIT>],<EOL>[<NUM_LIT>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT>],<EOL>[<NUM_LIT:0.0>, <NUM_LIT:0.0>, <NUM_LIT:0.0>, <NUM_LIT:1.0>]]).T<EOL>

Convert Talairach to MNI coordinates using the Lancaster transform.
    Adapted from BrainMap scripts; see http://brainmap.org/icbm2tal/
    Details are described in Lancaster et al. (2007)
    (http://brainmap.org/new/pubs/LancasterHBM07.pdf).


def mat_to_xyz(foci, mat_dims=None, xyz_dims=None):

foci = np.hstack((foci, np.ones((foci.shape[<NUM_LIT:0>], <NUM_LIT:1>))))<EOL>mat = np.array([[-<NUM_LIT:2>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT>], [<NUM_LIT:0>, <NUM_LIT:2>, <NUM_LIT:0>, -<NUM_LIT>], [<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:2>, -<NUM_LIT>]]).T<EOL>result = np.dot(foci, mat)[:, ::-<NUM_LIT:1>]  <EOL>return np.round_(result).astype(int)<EOL>

Convert an N x 3 array of matrix indices to XYZ coordinates.


def classify_regions(dataset, masks, method='<STR_LIT>', threshold=<NUM_LIT>,<EOL>remove_overlap=True, regularization='<STR_LIT>',<EOL>output='<STR_LIT>', studies=None, features=None,<EOL>class_weight='<STR_LIT>', classifier=None,<EOL>cross_val='<STR_LIT>', param_grid=None, scoring='<STR_LIT>'):

(X, y) = get_studies_by_regions(dataset, masks, threshold, remove_overlap,<EOL>studies, features,<EOL>regularization=regularization)<EOL>return classify(X, y, method, classifier, output, cross_val,<EOL>class_weight, scoring=scoring, param_grid=param_grid)<EOL>

Perform classification on specified regions

        Given a set of masks, this function retrieves studies associated with
        each mask at the specified threshold, optionally removes overlap and
        filters by studies and features. Then it trains an algorithm to
        classify studies based on features and tests performance.

        Args:
            dataset: a Neurosynth dataset
            maks: a list of paths to Nifti masks
            method: a string indicating which method to used.
                'SVM': Support Vector Classifier with rbf kernel
                'ERF': Extremely Randomized Forest classifier
                'Dummy': A dummy classifier using stratified classes as
                    predictor
            threshold: percentage of voxels active within the mask for study
                to be included
            remove_overlap: A boolean indicating if studies studies that
                appear in more than one mask should be excluded
            regularization: A string indicating type of regularization to use.
                If None, performs no regularization.
                'scale': Unit scale without demeaning
            output: A string indicating output type
                'summary': Dictionary with summary statistics including score
                    and n
                'summary_clf': Same as above but also includes classifier
                'clf': Only returns classifier
                Warning: using cv without grid will return an untrained
                classifier
            studies: An optional list of study names used to constrain the set
                used in classification. If None, will use all features in the
                dataset.
            features: An optional list of feature names used to constrain the
                set used in classification. If None, will use all features in
                the dataset.
            class_weight: Parameter to pass to classifier determining how to
                weight classes
            classifier: An optional sci-kit learn classifier to use instead of
                pre-set up classifiers set up using 'method'
            cross_val: A string indicating type of cross validation to use.
                Can also pass a scikit_classifier
            param_grid: A dictionary indicating which parameters to optimize
                using GridSearchCV. If None, no GridSearch will be used

        Returns:
            A tuple (X, y) of np arrays.
            X is a feature by studies matrix and y is a vector of class labels


all_features = dataset.get_feature_names()<EOL>i = [all_features.index(f) for f in features]<EOL>return i<EOL>

Returns a list with the order that features requested appear in
    dataset


x, y = imgs_to_decode.astype(float), self.feature_images.astype(float)<EOL>return self._xy_corr(x, y)<EOL>

Decode images using Pearson's r.

        Computes the correlation between each input image and each feature
        image across voxels.

        Args:
            imgs_to_decode: An ndarray of images to decode, with voxels in rows
                and images in columns.

        Returns:
            An n_features x n_images 2D array, with each cell representing the
            pearson correlation between the i'th feature and the j'th image
            across all voxels.


def load_features(self, features, image_type=None, from_array=False,<EOL>threshold=<NUM_LIT>):

if from_array:<EOL><INDENT>if isinstance(features, list):<EOL><INDENT>features = features[<NUM_LIT:0>]<EOL><DEDENT>self._load_features_from_array(features)<EOL><DEDENT>elif path.exists(features[<NUM_LIT:0>]):<EOL><INDENT>self._load_features_from_images(features)<EOL><DEDENT>else:<EOL><INDENT>self._load_features_from_dataset(<EOL>features, image_type=image_type, threshold=threshold)<EOL><DEDENT>

Load features from current Dataset instance or a list of files.
        Args:
            features: List containing paths to, or names of, features to
                extract. Each element in the list must be a string containing
                either a path to an image, or the name of a feature (as named
                in the current Dataset). Mixing of paths and feature names
                within the list is not allowed.
            image_type: Optional suffix indicating which kind of image to use
                for analysis. Only used if features are taken from the Dataset;
                if features is a list of filenames, image_type is ignored.
            from_array: If True, the features argument is interpreted as a
                string pointing to the location of a 2D ndarray on disk
                containing feature data, where rows are voxels and columns are
                individual features.
            threshold: If features are taken from the dataset, this is the
                threshold passed to the meta-analysis module to generate fresh
                images.


def decode(self, images, save=None, round=<NUM_LIT:4>, names=None, **kwargs):

if isinstance(images, string_types):<EOL><INDENT>images = [images]<EOL><DEDENT>if isinstance(images, list):<EOL><INDENT>imgs_to_decode = imageutils.load_imgs(images, self.masker)<EOL><DEDENT>else:<EOL><INDENT>imgs_to_decode = images<EOL><DEDENT>methods = {<EOL>'<STR_LIT>': self._pearson_correlation,<EOL>'<STR_LIT>': self._dot_product,<EOL>'<STR_LIT>': self._roi_association<EOL>}<EOL>result = np.around(<EOL>methods[self.method](imgs_to_decode, **kwargs), round)<EOL>if names is None:<EOL><INDENT>if type(images).__module__ == np.__name__:<EOL><INDENT>names = ['<STR_LIT>' % i for i in range(images.shape[<NUM_LIT:1>])]<EOL><DEDENT>elif self.method == '<STR_LIT>':<EOL><INDENT>names = ['<STR_LIT>' % i for i in range(result.shape[<NUM_LIT:1>])]<EOL><DEDENT>else:<EOL><INDENT>names = images<EOL><DEDENT><DEDENT>result = pd.DataFrame(result, columns=names, index=self.feature_names)<EOL>if save is not None:<EOL><INDENT>result.to_csv(save, index_label='<STR_LIT>')<EOL><DEDENT>return result<EOL>

Decodes a set of images.

        Args:
          images: The images to decode. Can be:
            - A single String specifying the filename of the image to decode
            - A list of filenames
            - A single NumPy array containing the image data
          save: Optional filename to save results to. If None (default), returns
            all results as an array.
          round: Optional integer indicating number of decimals to round result
            to. Defaults to 4.
          names: Optional list of names corresponding to the images in filenames.
            If passed, must be of same length and in same order as filenames.
            By default, the columns in the output will be named using the image
            filenames.

        Returns:
          An n_features x n_files numpy array, where each feature is a row and
          each image is a column. The meaning of the values depends on the
          decoding method used.


def _load_features_from_dataset(self, features=None, image_type=None,<EOL>threshold=<NUM_LIT>):

self.feature_names = self.dataset.feature_table.feature_names<EOL>if features is not None:<EOL><INDENT>self.feature_names = [f for f in features<EOL>if f in self.feature_names]<EOL><DEDENT>from neurosynth.analysis import meta<EOL>self.feature_images = meta.analyze_features(<EOL>self.dataset, self.feature_names, image_type=image_type,<EOL>threshold=threshold)<EOL>if self.masker.layers:<EOL><INDENT>in_mask = self.masker.get_mask(in_global_mask=True)<EOL>self.feature_images = self.feature_images[in_mask, :]<EOL><DEDENT>

Load feature image data from the current Dataset instance. See
        load_features() for documentation.


<EOL>warnings.simplefilter("<STR_LIT:ignore>", RuntimeWarning)<EOL>cells = cells.astype('<STR_LIT>')  <EOL>total = np.apply_over_axes(np.sum, cells, [<NUM_LIT:1>, <NUM_LIT:2>]).ravel()<EOL>chi_sq = np.zeros(cells.shape, dtype='<STR_LIT>')<EOL>for i in range(<NUM_LIT:2>):<EOL><INDENT>for j in range(<NUM_LIT:2>):<EOL><INDENT>exp = np.sum(cells[:, i, :], <NUM_LIT:1>).ravel() *np.sum(cells[:, :, j], <NUM_LIT:1>).ravel() / total<EOL>bad_vox = np.where(exp == <NUM_LIT:0>)[<NUM_LIT:0>]<EOL>chi_sq[:, i, j] = (cells[:, i, j] - exp) ** <NUM_LIT:2> / exp<EOL>chi_sq[bad_vox, i, j] = <NUM_LIT:1.0>  <EOL><DEDENT><DEDENT>chi_sq = np.apply_over_axes(np.sum, chi_sq, [<NUM_LIT:1>, <NUM_LIT:2>]).ravel()<EOL>return special.chdtrc(<NUM_LIT:1>, chi_sq)<EOL>

Two-way chi-square test of independence.
    Takes a 3D array as input: N(voxels) x 2 x 2, where the last two dimensions
    are the contingency table for each of N voxels. Returns an array of
    p-values.


return self._get('<STR_LIT>')<EOL>

Requests everything account related (total used storage, reward, ...).

        Returns:
            dict: dictionary containing account related info. ::

                      {
                        "extid": "extuserid",
                        "email": "jeff@openload.io",
                        "signup_at": "2015-01-09 23:59:54",
                        "storage_left": -1,
                        "storage_used": "32922117680",
                        "traffic": {
                          "left": -1,
                          "used_24h": 0
                        },
                        "balance": 0
                      }


if not params:<EOL><INDENT>params = {}<EOL><DEDENT>params.update({'<STR_LIT>': self.login, '<STR_LIT:key>': self.key})<EOL>response_json = requests.get(self.api_url + url, params).json()<EOL>return self._process_response(response_json)<EOL>

Used by every other method, it makes a GET request with the given params.

        Args:
            url (str): relative path of a specific service (account_info, ...).
            params (:obj:`dict`, optional): contains parameters to be sent in the GET request.

        Returns:
            dict: results of the response of the GET request.


def get_download_link(self, file_id, ticket, captcha_response=None):

params = {'<STR_LIT>': ticket, '<STR_LIT:file>': file_id}<EOL>if captcha_response:<EOL><INDENT>params['<STR_LIT>'] = captcha_response<EOL><DEDENT>return self._get('<STR_LIT>', params)<EOL>

Requests direct download link for requested file,
        this method makes use of the response of prepare_download, prepare_download must be called first.

        Args:
            file_id (str): id of the file to be downloaded.

            ticket (str): preparation ticket is found in prepare_download response,\
                          this is why we need to call prepare_download before get_download_link.

            captcha_response (:obj:`str`, optional): sometimes prepare_download will have captcha url to be solved, \
                                                     first, this is the solution of the captcha.

        Returns:
            dict: dictionary containing (file info, download url, ...). ::

                  {
                    "name": "The quick brown fox.txt",
                    "size": 12345,
                    "sha1": "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
                    "content_type": "plain/text",
                    "upload_at": "2011-01-26 13:33:37",
                    "url": "https://abvzps.example.com/dl/l/4spxX_-cSO4/The+quick+brown+fox.txt",
                    "token": "4spxX_-cSO4"
                  }


status = response_json['<STR_LIT:status>']<EOL>msg = response_json['<STR_LIT>']<EOL>if status == <NUM_LIT>:<EOL><INDENT>raise BadRequestException(msg)<EOL><DEDENT>elif status == <NUM_LIT>:<EOL><INDENT>raise PermissionDeniedException(msg)<EOL><DEDENT>elif status == <NUM_LIT>:<EOL><INDENT>raise FileNotFoundException(msg)<EOL><DEDENT>elif status == <NUM_LIT>:<EOL><INDENT>raise UnavailableForLegalReasonsException(msg)<EOL><DEDENT>elif status == <NUM_LIT>:<EOL><INDENT>raise BandwidthUsageExceeded(msg)<EOL><DEDENT>elif status >= <NUM_LIT>:<EOL><INDENT>raise ServerErrorException(msg)<EOL><DEDENT>

Check the status of the incoming response, raise exception if status is not 200.

        Args:
            response_json (dict): results of the response of the GET request.

        Returns:
           None


return self._get('<STR_LIT>', params={'<STR_LIT:file>': file_id, '<STR_LIT:name>': name})<EOL>

Sets a new name for a file

        Args:
            file_id (str): id of the file to be renamed.
            name (str): new name for the provided file.

        Returns:
            bool: True if file is renamed, otherwise False.


return self._get('<STR_LIT>', params={'<STR_LIT:file>': file_id})<EOL>

Used to request info for a specific file, info like size, name, .....

        Args:
            file_id (str): File-ID(s), single file or comma-separated (max. 50)

        Returns:
            dict: dictionary containing file(s) info, each key represents a file_id. ::

                  {
                     "72fA-_Lq8Ak3": {
                        "id": "72fA-_Lq8Ak3",
                        "status": 200,
                        "name": "The quick brown fox.txt",
                        "size": 123456789012,
                        "sha1": "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
                        "content_type": "plain/text",
                     },
                     "72fA-_Lq8Ak4": {
                        "id": "72fA-_Lq8Ak4",
                        "status": 500,
                        "name": "The quick brown fox.txt",
                        "size": false,
                        "sha1": "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
                        "content_type": "plain/text",
                     },
                     ...
                   }


Removes one of your files

        Args:
            file_id (str): id of the file to be deleted.

        Returns:
            bool: True if file is deleted, otherwise False.


Shows the video splash image (thumbnail)

        Args:
            file_id (str): id of the target file.

        Returns:
            str: url for the splash image.


raw_commits = RE_COMMIT.finditer(data)<EOL>for rc in raw_commits:<EOL><INDENT>full_commit = rc.groups()[<NUM_LIT:0>]<EOL>parts = RE_COMMIT.match(full_commit).groupdict()<EOL>parsed_commit = parse_commit(parts)<EOL>yield parsed_commit<EOL><DEDENT>

Accept a string and parse it into many commits.
    Parse and yield each commit-dictionary.
    This function is a generator.


other, sep, ext = name.partition('<STR_LIT:.>')<EOL>return ext<EOL>

Return the extension only for a name (like a filename)

>>> get_ext('foo.bar')
'bar'
>>> get_ext('.only')
'only'
>>> get_ext('')
''
>>> get_ext('noext')
''
>>> get_ext('emptyext.')
''

Note that for any non-empty string, the result will never be the
same as the input. This is a useful property for basepack.


target = target or getattr(self, '<STR_LIT:filename>', None)<EOL>if isinstance(target, six.string_types):<EOL><INDENT>self.filename = target<EOL>target = open(target, '<STR_LIT:wb>')<EOL><DEDENT>if target is None:<EOL><INDENT>msg = (<EOL>"<STR_LIT>"<EOL>% self.__class__.__name__<EOL>)<EOL>raise ValueError(msg)<EOL><DEDENT>self._store(target)<EOL>

Save this package to target, which should be a filename or open
file stream. If target is not supplied, and this package has a
filename attribute (such as when this package was created from
an existing file), it will be used.


return self.parts[name]<EOL>


parts = []<EOL>package = getattr(self, '<STR_LIT>', None) or self<EOL>for rel in self.relationships.types.get(reltype, []):<EOL><INDENT>parts.append(package[posixpath.join(self.base, rel.target)])<EOL><DEDENT>return parts<EOL>

Return a list of parts related to this one via reltype.


<EOL>cls = type.__new__(mcs, name, bases, attrs)<EOL>if not hasattr(cls, '<STR_LIT>'):<EOL><INDENT>cls.classes_by_rel_type = defaultdict(lambda: cls)<EOL><DEDENT>rt = attrs.get('<STR_LIT>', None)<EOL>if rt:<EOL><INDENT>cls.classes_by_rel_type[rt] = cls<EOL><DEDENT>return cls<EOL>

This is called when a new class is created of this type


map = self.items<EOL>return map.get(name, None) or map.get(get_ext(name) or None, None)<EOL>

Get the correct content type for a given name


self.content_types.update(ContentTypes.load(source))<EOL>

Load up the content_types object with value from source XML.


def derived(self, name, relative_coords, formula):

relZ, relN = relative_coords<EOL>daughter_idx = [(x[<NUM_LIT:0>] + relZ, x[<NUM_LIT:1>] + relN) for x in self.df.index]<EOL>values = formula(self.df.values, self.df.loc[daughter_idx].values)<EOL>return Table(df=pd.Series(values, index=self.df.index, name=name + '<STR_LIT:(>' + self.name + '<STR_LIT:)>'))<EOL>


M_P = <NUM_LIT>         <EOL>f = lambda parent, daugther: -parent + daugther + M_P<EOL>return self.derived('<STR_LIT>', (-<NUM_LIT:1>, <NUM_LIT:0>), f)<EOL>


M_N = <NUM_LIT>         <EOL>f = lambda parent, daugther: -parent + daugther + M_N<EOL>return self.derived('<STR_LIT>', (<NUM_LIT:0>, -<NUM_LIT:1>), f)<EOL>


return self.Z + self.N<EOL>

Return the mass number A for all nuclei in the table as a numpy array.


def select(self, condition, name='<STR_LIT>'):

if condition.__code__.co_argcount == <NUM_LIT:1>:<EOL><INDENT>idx = [(Z, N) for (Z, N), M in self if condition(M)]<EOL><DEDENT>if condition.__code__.co_argcount == <NUM_LIT:2>:<EOL><INDENT>idx = [(Z, N) for (Z, N) in self.index if condition(Z, N)]<EOL><DEDENT>if condition.__code__.co_argcount == <NUM_LIT:3>:<EOL><INDENT>idx = [(Z, N) for (Z, N), M in self if condition(Z, N, M)]<EOL><DEDENT>index = pd.MultiIndex.from_tuples(idx, names=['<STR_LIT>', '<STR_LIT:N>'])<EOL>return Table(df=self.df.ix[index], name=name)<EOL>

Selects nuclei according to a condition on Z,N or M

Parameters
----------
condition : function,
    Can have one of the signatures f(M), f(Z,N) or f(Z, N, M)
    must return a boolean value
name: string, optional name for the resulting Table

Example:
--------
Select all nuclei with A > 160:

>>> A_gt_160 = lambda Z,N: Z + N > 160
>>> Table('AME2003').select(A_gt_160)


filename = os.path.join(package_dir, '<STR_LIT:data>', name + '<STR_LIT>')<EOL>return cls.from_file(filename, name)<EOL>


return self.df.index.get_level_values('<STR_LIT>').values<EOL>

Return the proton number Z for all nuclei in the table as a numpy array.


return self.select(lambda Z, N: not(Z % <NUM_LIT:2>) and (N % <NUM_LIT:2>), name=self.name)<EOL>


return self.select(lambda Z, N: (Z % <NUM_LIT:2>) and (N % <NUM_LIT:2>), name=self.name)<EOL>

Selects odd-odd nuclei from the table:

        >>> Table('FRDM95').odd_odd
        Out[13]:
        Z   N
        9   9       1.21
            11      0.10
            13      3.08
            15      9.32
        ...


def __init__(self, name='<STR_LIT>', df=None):

if df is not None:  <EOL><INDENT>self.df = df<EOL>self.name = name<EOL><DEDENT>elif name in self._names:  <EOL><INDENT>self.name = name<EOL>self.df = self.from_name(name).df<EOL><DEDENT>else:<EOL><INDENT>print ('<STR_LIT>')<EOL>print(('<STR_LIT:U+0020>'.join(Table.names)))<EOL>return None<EOL><DEDENT>

Init from a Series/Dataframe (df) of a file (name)


return len(self.df)<EOL>

Return the total number of nuclei in the table:

        >>> Table('AME2012').count
        2438

        It is also possible to do:

        >>> len(Table('AME2012'))
        2438


df = self.df - Table(relative_to).df<EOL>return Table(df=df)<EOL>

Calculate error difference

Parameters
----------
relative_to : string,
    a valid mass table name.

Example:
----------
>>> Table('DUZU').error(relative_to='AME2003')


M_P = <NUM_LIT>         <EOL>f = lambda parent, daugther: -parent + daugther + <NUM_LIT:2> * M_P<EOL>return self.derived('<STR_LIT>', (-<NUM_LIT:2>, <NUM_LIT:0>), f)<EOL>


@classmethod<EOL><INDENT>def from_file(cls, filename, name='<STR_LIT>'):<DEDENT>

df = pd.read_csv(filename, header=<NUM_LIT:0>, delim_whitespace=True, index_col=[<NUM_LIT:0>, <NUM_LIT:1>])['<STR_LIT:M>']<EOL>df.name = name<EOL>return cls(df=df, name=name)<EOL>


M_ALPHA = <NUM_LIT>         <EOL>f = lambda parent, daugther: parent - daugther - M_ALPHA<EOL>return self.derived('<STR_LIT>', (-<NUM_LIT:2>, -<NUM_LIT:2>), f)<EOL>


def chart_plot(self, ax=None, cmap='<STR_LIT>',<EOL>xlabel='<STR_LIT:N>', ylabel='<STR_LIT>', grid_on=True, colorbar=True):

from matplotlib.mlab import griddata<EOL>from numpy import linspace, meshgrid<EOL>import matplotlib.pyplot as plt<EOL>x = self.dropna().N<EOL>y = self.dropna().Z<EOL>z = self.dropna().values<EOL>xi = linspace(min(x), max(x), max(x) - min(x) + <NUM_LIT:1>)<EOL>yi = linspace(min(y), max(y), max(y) - min(y) + <NUM_LIT:1>)<EOL>Z = griddata(x, y, z, xi, yi)<EOL>X, Y = meshgrid(xi, yi)<EOL>if ax is None:<EOL><INDENT>ax = plt.gca()<EOL><DEDENT>chart = ax.pcolormesh(X, Y, Z, cmap=cmap)<EOL>ax.set_xlabel(xlabel)<EOL>ax.set_ylabel(ylabel)<EOL>ax.grid(grid_on)<EOL>ax.set_aspect('<STR_LIT>')<EOL>if colorbar:<EOL><INDENT>plt.colorbar(chart)<EOL><DEDENT>return ax<EOL>

Plot a nuclear chart with (N,Z) as axis and the values
        of the Table as a color scale

        Parameters
        ----------
        ax: optional matplotlib axes
                defaults to current axes
        cmap: a matplotlib colormap
                default: 'RdBu'
        xlabel: string representing the label of the x axis
            default: 'N'
        ylabel: string, default: 'Z'
            the label of the x axis
        grid_on: boolean, default: True,
            whether to draw the axes grid or not
        colorbar: boolean, default: True
            whether to draw a colorbar or not

        Returns
        -------
        ax: a matplotlib axes object

        Example
        -------
        Plot the theoretical deviation for the Möller's model::

        >>> Table('FRDM95').error().chart_plot()


arr = arr[..., np.newaxis]<EOL>return np.concatenate((arr, np.ones_like(arr)), axis=-<NUM_LIT:1>)<EOL>


@command(aliases='<STR_LIT>')<EOL>def fm(client, event, channel, nick, rest):

if rest:<EOL><INDENT>rest = rest.strip()<EOL>Karma.store.change(rest, <NUM_LIT:2>)<EOL>rcpt = rest<EOL><DEDENT>else:<EOL><INDENT>rcpt = channel<EOL><DEDENT>return f'<STR_LIT>'<EOL>


expression = self.add_operator(Operator('<STR_LIT:;>'))<EOL>for element in elements:<EOL><INDENT>expression.add_element(element)<EOL><DEDENT>return expression<EOL>

Update the ``Expression`` by joining the specified additional
        ``elements`` using an "AND" ``Operator``

        Args:
            *elements (BaseExpression): The ``Expression`` and/or
                ``Constraint`` elements which the "AND" ``Operator`` applies
                to.

        Returns:
            Expression: ``self`` or related ``Expression``.


self.parent = None<EOL>

Initialize instance of ``BaseExpression``.


sub = Expression()<EOL>self.add_element(sub)<EOL>return sub<EOL>

Create a nested ``Expression``, add it as an element to this
        ``Expression``, and return it.

        Returns:
            Expression: The newly created nested ``Expression``.


super(Expression, self).__init__()<EOL>self.elements = []<EOL>self.operator = None<EOL>self._working_fragment = self<EOL>self._last_element = None<EOL>


if len(self.elements) == <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT>if len(self.elements) == <NUM_LIT:1>:<EOL><INDENT>return self.elements[<NUM_LIT:0>].to_python()<EOL><DEDENT>operator = self.operator or Operator('<STR_LIT:;>')<EOL>return [operator.to_python()] +[elem.to_python() for elem in self.elements]<EOL>

Deconstruct the ``Expression`` instance to a list or tuple
        (If ``Expression`` contains only one ``Constraint``).

        Returns:
            list or tuple: The deconstructed ``Expression``.


if not isinstance(operator, Operator):<EOL><INDENT>raise FiqlObjectException("<STR_LIT>" % (<EOL>operator.__class__))<EOL><DEDENT>if not self._working_fragment.operator:<EOL><INDENT>self._working_fragment.operator = operator<EOL><DEDENT>elif operator > self._working_fragment.operator:<EOL><INDENT>last_constraint = self._working_fragment.elements.pop()<EOL>self._working_fragment = self._working_fragment.create_nested_expression()<EOL>self._working_fragment.add_element(last_constraint)<EOL>self._working_fragment.add_operator(operator)<EOL><DEDENT>elif operator < self._working_fragment.operator:<EOL><INDENT>if self._working_fragment.parent:<EOL><INDENT>return self._working_fragment.parent.add_operator(operator)<EOL><DEDENT>else:<EOL><INDENT>return Expression().add_element(self._working_fragment).add_operator(operator)<EOL><DEDENT><DEDENT>return self<EOL>

Add an ``Operator`` to the ``Expression``.

        The ``Operator`` may result in a new ``Expression`` if an ``Operator``
        already exists and is of a different precedence.

        There are three possibilities when adding an ``Operator`` to an
        ``Expression`` depending on whether or not an ``Operator`` already
        exists:

          - No ``Operator`` on the working ``Expression``; Simply set the
            ``Operator`` and return ``self``.
          - ``Operator`` already exists and is higher in precedence; The
            ``Operator`` and last ``Constraint`` belong in a sub-expression of
            the working ``Expression``.
          - ``Operator`` already exists and is lower in precedence; The
            ``Operator`` belongs to the parent of the working ``Expression``
            whether one currently exists or not. To remain in the context of
            the top ``Expression``, this method will return the parent here
            rather than ``self``.

        Args:
            operator (Operator): What we are adding.

        Returns:
            Expression: ``self`` or related ``Expression``.

        Raises:
            FiqlObjectExpression: Operator is not a valid ``Operator``.


return OPERATOR_MAP[self.value][<NUM_LIT:1>] == OPERATOR_MAP[other.value][<NUM_LIT:1>]<EOL>

Of equal precendence.

        Args:
            other (Operator): The ``Operator`` we are comparing precedence
                against.

        Returns:
            boolean: ``True`` if of equal precendence of ``other``.


reload_module(sys)<EOL>if six.PY2:<EOL><INDENT>sys.setdefaultencoding('<STR_LIT:utf-8>')<EOL><DEDENT>response = None<EOL>original_credentials = {}<EOL>refreshed_credentials = {}<EOL>if result:<EOL><INDENT>if result.user:<EOL><INDENT>result.user.update()<EOL>if result.user.credentials:<EOL><INDENT>original_credentials.update(result.user.credentials.__dict__)<EOL>time.sleep(<NUM_LIT:2>)<EOL>response = result.user.credentials.refresh(force=True)<EOL>refreshed_credentials.update(result.user.credentials.__dict__)<EOL><DEDENT><DEDENT>user_properties = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT:email>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT:id>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT:location>', '<STR_LIT:name>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT:username>']<EOL>access_token_content = None<EOL>if hasattr(result.provider, '<STR_LIT>'):<EOL><INDENT>access_token_content = result.provider.access_token_response.content  <EOL><DEDENT>template = env.get_template('<STR_LIT>')<EOL>return template.render(result=result,<EOL>providers=ASSEMBLED_CONFIG.values(),<EOL>oauth2_providers=OAUTH2_PROVIDERS,<EOL>oauth1_providers=OAUTH1_PROVIDERS,<EOL>openid_providers=OPENID_PROVIDERS,<EOL>user_properties=user_properties,<EOL>error=result.error,<EOL>credentials_response=response,<EOL>original_credentials=original_credentials,<EOL>refreshed_credentials=refreshed_credentials,<EOL>framework_name=framework_name,<EOL>access_token_content=access_token_content,<EOL>birth_date_format=BIRTH_DATE_FORMAT)<EOL><DEDENT>

Renders the login handler.

:param result:

    The :class:`.authomatic.core.LoginResult` returned by the
    :meth:`.authomatic.Authomatic.login` method.


<EOL>class metaclass(meta):<EOL><INDENT>def __new__(cls, name, this_bases, d):<EOL><INDENT>return meta(name, bases, d)<EOL><DEDENT><DEDENT>return type.__new__(metaclass, '<STR_LIT>', (), {})<EOL>


__import__(name)<EOL>return sys.modules[name]<EOL>

Import module, returning the module after the last dot.


Must write specified value to response.

:param str value:
    String to be written to response.


Called only once per request.

Should implement a mechanism for setting the the session
**cookie** and saving the session **data** to storage.


if not len(cls.query().fetch()):<EOL><INDENT>example = cls.get_or_insert('<STR_LIT>')<EOL>example.class_ = '<STR_LIT>' +'<STR_LIT>'<EOL>example.provider_name = '<STR_LIT>'<EOL>example.consumer_key = '<STR_LIT>'<EOL>example.consumer_secret = '<STR_LIT>'<EOL>example.provider_id = <NUM_LIT:1><EOL>example.scope = '<STR_LIT>'<EOL>example.identifier_param = '<STR_LIT>' +'<STR_LIT>'<EOL>example.put()<EOL>raise GAEError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>

Creates an **"Example"** entity of kind **"NDBConfig"** in the
datastore if the model is empty and raises and error to inform you that
you should populate the model with data.

.. note::

    The *Datastore Viewer* in the ``_ah/admin/`` won't let you add
    properties to a model if there is not an entity with that
    property already. Therefore it is a good idea to keep the
    **"Example"** entity (which has all possible properties set) in
    the datastore.


<EOL>results = cls.query().fetch()<EOL>return [result.to_dict() for result in results]<EOL>

Resembles the :meth:`dict.values` method.


AUTHOMATIC_HEADER = '<STR_LIT>'<EOL>request_type = adapter.params.get('<STR_LIT:type>', '<STR_LIT>')<EOL>json_input = adapter.params.get('<STR_LIT>')<EOL>credentials = adapter.params.get('<STR_LIT>')<EOL>url = adapter.params.get('<STR_LIT:url>')<EOL>method = adapter.params.get('<STR_LIT>', '<STR_LIT:GET>')<EOL>body = adapter.params.get('<STR_LIT:body>', '<STR_LIT>')<EOL>params = adapter.params.get('<STR_LIT>')<EOL>params = json.loads(params) if params else {}<EOL>headers = adapter.params.get('<STR_LIT>')<EOL>headers = json.loads(headers) if headers else {}<EOL>ProviderClass = Credentials.deserialize(<EOL>self.config, credentials).provider_class<EOL>if request_type == '<STR_LIT>':<EOL><INDENT>jsonp = params.get('<STR_LIT>')<EOL>if ProviderClass.supports_jsonp and method is '<STR_LIT:GET>':<EOL><INDENT>request_type = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>if jsonp:<EOL><INDENT>params.pop('<STR_LIT>')<EOL><DEDENT>request_type = '<STR_LIT>'<EOL><DEDENT><DEDENT>if request_type == '<STR_LIT>':<EOL><INDENT>response = self.access(<EOL>credentials, url, params, method, headers, body)<EOL>result = response.content<EOL>adapter.status = str(response.status) + '<STR_LIT:U+0020>' + str(response.reason)<EOL>for k, v in response.getheaders():<EOL><INDENT>logging.info('<STR_LIT>'.format(k, v))<EOL>adapter.set_header(k, v)<EOL><DEDENT><DEDENT>elif request_type == '<STR_LIT>':<EOL><INDENT>if json_input:<EOL><INDENT>result = self.request_elements(<EOL>json_input=json_input, return_json=True)<EOL><DEDENT>else:<EOL><INDENT>result = self.request_elements(credentials=credentials,<EOL>url=url,<EOL>method=method,<EOL>params=params,<EOL>headers=headers,<EOL>body=body,<EOL>return_json=True)<EOL><DEDENT>adapter.set_header('<STR_LIT:Content-Type>', '<STR_LIT:application/json>')<EOL><DEDENT>else:<EOL><INDENT>result = '<STR_LIT>'<EOL><DEDENT>adapter.set_header(AUTHOMATIC_HEADER, request_type)<EOL>adapter.write(result)<EOL>

Converts a *request handler* to a JSON backend which you can use with
:ref:`authomatic.js <js>`.

Just call it inside a *request handler* like this:

::

    class JSONHandler(webapp2.RequestHandler):
        def get(self):
            authomatic.backend(Webapp2Adapter(self))

:param adapter:
    The only argument is an :doc:`adapter <adapters>`.

The *request handler* will now accept these request parameters:

:param str type:
    Type of the request. Either ``auto``, ``fetch`` or ``elements``.
    Default is ``auto``.

:param str credentials:
    Serialized :class:`.Credentials`.

:param str url:
    URL of the **protected resource** request.

:param str method:
    HTTP method of the **protected resource** request.

:param str body:
    HTTP body of the **protected resource** request.

:param JSON params:
    HTTP params of the **protected resource** request as a JSON object.

:param JSON headers:
    HTTP headers of the **protected resource** request as a
    JSON object.

:param JSON json:
    You can pass all of the aforementioned params except ``type``
    in a JSON object.

    .. code-block:: javascript

        {
            "credentials": "######",
            "url": "https://example.com",
            "method": "POST",
            "params": {"foo": "bar"},
            "headers": {"baz": "bing"},
            "body": "the body of the request"
        }

Depending on the ``type`` param, the handler will either write
a JSON object with *request elements* to the response,
and add an ``Authomatic-Response-To: elements`` response header, ...

.. code-block:: javascript

    {
        "url": "https://example.com/api",
        "method": "POST",
        "params": {
            "access_token": "###",
            "foo": "bar"
        },
        "headers": {
            "baz": "bing",
            "Authorization": "Bearer ###"
        }
    }

... or make a fetch to the **protected resource** and forward
it's response content, status and headers with an additional
``Authomatic-Response-To: fetch`` header to the response.

.. warning::

    The backend will not work if you write anything to the
    response in the handler!


def popup_js(self, callback_name=None, indent=None,<EOL>custom=None, stay_open=False):

custom_callback = """<STR_LIT>""".format(cb=callback_name) if callback_name else '<STR_LIT>'<EOL>return """<STR_LIT>""".format(result=self.to_json(indent),<EOL>custom=json.dumps(custom),<EOL>custom_callback=custom_callback,<EOL>stay_open='<STR_LIT>' if stay_open else '<STR_LIT>')<EOL>

Returns JavaScript that:

#.  Triggers the ``options.onLoginComplete(result, closer)``
    handler set with the :ref:`authomatic.setup() <js_setup>`
    function of :ref:`javascript.js <js>`.
#.  Calls the JavasScript callback specified by :data:`callback_name`
    on the opener of the *login handler popup* and passes it the
    *login result* JSON object as first argument and the `closer`
    function which you should call in your callback to close the popup.

:param str callback_name:
    The name of the javascript callback e.g ``foo.bar.loginCallback``
    will result in ``window.opener.foo.bar.loginCallback(result);``
    in the HTML.

:param int indent:
    The number of spaces to indent the JSON result object.
    If ``0`` or negative, only newlines are added.
    If ``None``, no newlines are added.

:param custom:
    Any JSON serializable object that will be passed to the
    ``result.custom`` attribute.

:param str stay_open:
    If ``True``, the popup will stay open.

:returns:
    :class:`str` with JavaScript.


def __init__(self, adapter, secret, name='<STR_LIT>', max_age=<NUM_LIT>,<EOL>secure=False):

self.adapter = adapter<EOL>self.name = name<EOL>self.secret = secret<EOL>self.max_age = max_age<EOL>self.secure = secure<EOL>self._data = {}<EOL>

:param str secret:
    Session secret used to sign the session cookie.
:param str name:
    Session cookie name.
:param int max_age:
    Maximum allowed age of session cookie nonce in seconds.
:param bool secure:
    If ``True`` the session cookie will be saved with ``Secure``
    attribute.


def __init__(<EOL>self, config, secret, session_max_age=<NUM_LIT>, secure_cookie=False,<EOL>session=None, session_save_method=None, report_errors=True,<EOL>debug=False, logging_level=logging.INFO, prefix='<STR_LIT>',<EOL>logger=None<EOL>):

self.config = config<EOL>self.secret = secret<EOL>self.session_max_age = session_max_age<EOL>self.secure_cookie = secure_cookie<EOL>self.session = session<EOL>self.session_save_method = session_save_method<EOL>self.report_errors = report_errors<EOL>self.debug = debug<EOL>self.logging_level = logging_level<EOL>self.prefix = prefix<EOL>self._logger = logger or logging.getLogger(str(id(self)))<EOL>if logger is None:<EOL><INDENT>self._logger.setLevel(logging_level)<EOL><DEDENT>

Encapsulates all the functionality of this package.

:param dict config:
    :doc:`config`

:param str secret:
    A secret string that will be used as the key for signing
    :class:`.Session` cookie and as a salt by *CSRF* token generation.

:param session_max_age:
    Maximum allowed age of :class:`.Session` cookie nonce in seconds.

:param bool secure_cookie:
    If ``True`` the :class:`.Session` cookie will be saved wit
    ``Secure`` attribute.

:param session:
    Custom dictionary-like session implementation.

:param callable session_save_method:
    A method of the supplied session or any mechanism that saves the
    session data and cookie.

:param bool report_errors:
    If ``True`` exceptions encountered during the **login procedure**
    will be caught and reported in the :attr:`.LoginResult.error`
    attribute.
    Default is ``True``.

:param bool debug:
    If ``True`` traceback of exceptions will be written to response.
    Default is ``False``.

:param int logging_level:
    The logging level threshold for the default logger as specified in
    the standard Python
    `logging library <http://docs.python.org/2/library/logging.html>`_.
    This setting is ignored when :data:`logger` is set.
    Default is ``logging.INFO``.

:param str prefix:
    Prefix used as the :class:`.Session` cookie name.

:param logger:
    A :class:`logging.logger` instance.


return self[<NUM_LIT:2>]<EOL>


return self.httplib_response.getheaders()<EOL>


return self.url + '<STR_LIT:?>' + self.query_string<EOL>


return dict([(k, v[<NUM_LIT:0>] if not isinstance(v, str) and len(v) == <NUM_LIT:1> else v)<EOL>for k, v in list(dict_.items())])<EOL>

Replaces all values that are single-item iterables with the value of its
index 0.

:param dict dict_:
    Dictionary to normalize.

:returns:
    Normalized dictionary.


value = '<STR_LIT>' if delete else self._serialize(self.data)<EOL>split_url = parse.urlsplit(self.adapter.url)<EOL>domain = split_url.netloc.split('<STR_LIT::>')[<NUM_LIT:0>]<EOL>if '<STR_LIT:.>' not in domain:<EOL><INDENT>template = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>template = ('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>return template.format(<EOL>name=self.name,<EOL>value=value,<EOL>domain=domain,<EOL>path=split_url.path,<EOL>secure='<STR_LIT>' if self.secure else '<STR_LIT>',<EOL>expires='<STR_LIT>' if delete else '<STR_LIT>'<EOL>)<EOL>

Creates the value for ``Set-Cookie`` HTTP header.

:param bool delete:
    If ``True`` the cookie value will be ``deleted`` and the
    Expires value will be ``Thu, 01-Jan-1970 00:00:01 GMT``.


if self.expiration_time:<EOL><INDENT>return self.expiration_time > int(time.time())<EOL><DEDENT>else:<EOL><INDENT>return True<EOL><DEDENT>

``True`` if credentials are valid, ``False`` if expired.


return Future(self.update)<EOL>

Same as :meth:`.update` but runs asynchronously in a separate thread.

.. warning::

    |async|

:returns:
    :class:`.Future` instance representing the separate thread.


def login(self, adapter, provider_name, callback=None,<EOL>session=None, session_saver=None, **kwargs):

if provider_name:<EOL><INDENT>provider_settings = self.config.get(provider_name)<EOL>if not provider_settings:<EOL><INDENT>raise ConfigError('<STR_LIT>'<EOL>.format(provider_name))<EOL><DEDENT>if not (session is None or session_saver is None):<EOL><INDENT>session = session<EOL>session_saver = session_saver<EOL><DEDENT>else:<EOL><INDENT>session = Session(adapter=adapter,<EOL>secret=self.secret,<EOL>max_age=self.session_max_age,<EOL>name=self.prefix,<EOL>secure=self.secure_cookie)<EOL>session_saver = session.save<EOL><DEDENT>class_ = provider_settings.get('<STR_LIT>')<EOL>if not class_:<EOL><INDENT>raise ConfigError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(provider_name))<EOL><DEDENT>ProviderClass = resolve_provider_class(class_)<EOL>ProviderClass._logger = self._logger<EOL>provider = ProviderClass(self,<EOL>adapter=adapter,<EOL>provider_name=provider_name,<EOL>callback=callback,<EOL>session=session,<EOL>session_saver=session_saver,<EOL>**kwargs)<EOL>return provider.login()<EOL><DEDENT>else:<EOL><INDENT>self.backend(adapter)<EOL><DEDENT>

If :data:`provider_name` specified, launches the login procedure for
corresponding :doc:`provider </reference/providers>` and returns
:class:`.LoginResult`.

If :data:`provider_name` is empty, acts like
:meth:`.Authomatic.backend`.

.. warning::

    The method redirects the **user** to the **provider** which in
    turn redirects **him/her** back to the *request handler* where
    it has been called.

:param str provider_name:
    Name of the provider as specified in the keys of the :doc:`config`.

:param callable callback:
    If specified the method will call the callback with
    :class:`.LoginResult` passed as argument and will return nothing.

:param bool report_errors:

.. note::

    Accepts additional keyword arguments that will be passed to
    :doc:`provider <providers>` constructor.

:returns:
    :class:`.LoginResult`


<EOL>if value:<EOL><INDENT>self._expiration_time = int(time.time()) + int(value)<EOL>self._expire_in = value<EOL><DEDENT>

Computes :attr:`.expiration_time` when the value is set.


<EOL>encoded, timestamp, signature = value.split('<STR_LIT:|>')<EOL>if not signature == self._signature(self.name, encoded, timestamp):<EOL><INDENT>raise SessionError('<STR_LIT>'.format(signature))<EOL><DEDENT>if int(timestamp) < int(time.time()) - self.max_age:<EOL><INDENT>return None<EOL><DEDENT>decoded = parse.unquote(encoded)<EOL>deserialized = pickle.loads(decoded.encode('<STR_LIT>'))<EOL>return deserialized<EOL>

Deserializes and verifies the value created by :meth:`._serialize`.

:param str value:
    The serialized value.

:returns:
    Deserialized object.


def refresh(self, force=False, soon=<NUM_LIT>):

if hasattr(self.provider_class, '<STR_LIT>'):<EOL><INDENT>if force or self.expire_soon(soon):<EOL><INDENT>logging.info('<STR_LIT>'.format(self.provider_name))<EOL>return self.provider_class(<EOL>self, None, self.provider_name).refresh_credentials(self)<EOL><DEDENT><DEDENT>

Refreshes the credentials only if the **provider** supports it and if
it will expire in less than one day. It does nothing in other cases.

.. note::

    The credentials will be refreshed only if it gives sense
    i.e. only |oauth2|_ has the notion of credentials
    *refreshment/extension*.
    And there are also differences across providers e.g. Google
    supports refreshment only if there is a ``refresh_token`` in
    the credentials and that in turn is present only if the
    ``access_type`` parameter was set to ``offline`` in the
    **user authorization request**.

:param bool force:
    If ``True`` the credentials will be refreshed even if they
    won't expire soon.

:param int soon:
    Number of seconds specifying what means *soon*.


super(OAuth2, self).__init__(*args, **kwargs)<EOL>self.scope = self._kwarg(kwargs, '<STR_LIT>', [])<EOL>self.offline = self._kwarg(kwargs, '<STR_LIT>', False)<EOL>

Accepts additional keyword arguments:

:param list scope:
    List of strings specifying requested permissions as described
    in the
    `OAuth 2.0 spec <http://tools.ietf.org/html/rfc6749#section-3.3>`_.

:param bool offline:
    If ``True`` the **provider** will be set up to request an
    *offline access token*.
    Default is ``False``.

As well as those inherited from :class:`.AuthorizationProvider`
constructor.


return '<STR_LIT:U+0020>'.join(scope)<EOL>


super(AuthorizationProvider, self).__init__(*args, **kwargs)<EOL>self.consumer_key = self._kwarg(kwargs, '<STR_LIT>')<EOL>self.consumer_secret = self._kwarg(kwargs, '<STR_LIT>')<EOL>self.user_authorization_params = self._kwarg(<EOL>kwargs, '<STR_LIT>', {})<EOL>self.access_token_headers = self._kwarg(<EOL>kwargs, '<STR_LIT>', {})<EOL>self.access_token_params = self._kwarg(<EOL>kwargs, '<STR_LIT>', {})<EOL>self.id = self._kwarg(kwargs, '<STR_LIT:id>')<EOL>self.access_headers = self._kwarg(kwargs, '<STR_LIT>', {})<EOL>self.access_params = self._kwarg(kwargs, '<STR_LIT>', {})<EOL>self.credentials = authomatic.core.Credentials(<EOL>self.settings.config, provider=self)<EOL>self.access_token_response = None<EOL>

Accepts additional keyword arguments:

:arg str consumer_key:
    The *key* assigned to our application (**consumer**) by the
    **provider**.

:arg str consumer_secret:
    The *secret* assigned to our application (**consumer**) by the
    **provider**.

:arg int id:
    A unique numeric ID used to serialize :class:`.Credentials`.

:arg dict user_authorization_params:
    A dictionary of additional request parameters for
    **user authorization request**.

:arg dict access_token_params:
    A dictionary of additional request parameters for
    **access_with_credentials token request**.

:arg dict access_headers:
    A dictionary of default HTTP headers that will be used when
    accessing **user's** protected resources.
    Applied by :meth:`.access()`, :meth:`.update_user()` and
    :meth:`.User.update()`

:arg dict access_params:
    A dictionary of default query string parameters that will be used
    when accessing **user's** protected resources.
    Applied by :meth:`.access()`, :meth:`.update_user()` and
    :meth:`.User.update()`


cls = self.__class__<EOL>mod = sys.modules.get(cls.__module__)<EOL>return str(self.PROVIDER_TYPE_ID) + '<STR_LIT:->' +str(mod.PROVIDER_ID_MAP.index(cls))<EOL>

A short string representing the provider implementation id used for
serialization of :class:`.Credentials` and to identify the type of
provider in JavaScript.

The part before hyphen denotes the type of the provider, the part
after hyphen denotes the class id e.g.
``oauth2.Facebook.type_id = '2-5'``,
``oauth1.Twitter.type_id = '1-5'``.


if cls._x_use_authorization_header:<EOL><INDENT>res = '<STR_LIT::>'.join(<EOL>(credentials.consumer_key,<EOL>credentials.consumer_secret))<EOL>res = base64.b64encode(six.b(res)).decode()<EOL>return {'<STR_LIT>': '<STR_LIT>'.format(res)}<EOL><DEDENT>else:<EOL><INDENT>return {}<EOL><DEDENT>

Creates authorization headers if the provider supports it. See:
http://en.wikipedia.org/wiki/Basic_access_authentication.

:param credentials:
    :class:`.Credentials`

:returns:
    Headers as :class:`dict`.


return credentials<EOL>

Override this to handle differences in naming conventions across
providers.

:param credentials:
    :class:`.Credentials`

:param dict data:
    Response data dictionary.

:returns:
    :class:`.Credentials`


Launches the *login procedure* to get **user's credentials** from
**provider**.

Should be decorated with :func:`.login_decorator`. The *login
procedure* is considered finished when the :attr:`.user`
attribute is not empty when the method runs out of it's flow or
when there are errors.


@abc.abstractmethod<EOL><INDENT>def create_request_elements(self, request_type, credentials,<EOL>url, method='<STR_LIT:GET>', params=None, headers=None,<EOL>body='<STR_LIT>'):<DEDENT>

Must return :class:`.RequestElements`.

.. warning::

    |classmethod|

:param int request_type:
    Type of the request specified by one of the class's constants.

:param credentials:
    :class:`.Credentials` of the **user** whose
    **protected resource** we want to access.

:param str url:
    URL of the request.

:param str method:
    HTTP method of the request.

:param dict params:
    Dictionary of request parameters.

:param dict headers:
    Dictionary of request headers.

:param str body:
    Body of ``POST``, ``PUT`` and ``PATCH`` requests.

:returns:
    :class:`.RequestElements`


<EOL>hashed = hashlib.md5(uuid.uuid4().bytes + six.b(secret)).hexdigest()<EOL>span = <NUM_LIT:5><EOL>shift = random.randint(<NUM_LIT:0>, span)<EOL>return hashed[shift:shift - span - <NUM_LIT:1>]<EOL>

Generates CSRF token.

Inspired by this article:
http://blog.ptsecurity.com/2012/10/random-number-security-in-python.html

:returns:
    :class:`str` Random unguessable string.


split = parse.urlsplit(url)<EOL>base = parse.urlunsplit((split.scheme, split.netloc, split.path, <NUM_LIT:0>, <NUM_LIT:0>))<EOL>params = parse.parse_qsl(split.query, True)<EOL>return base, params<EOL>

Splits given url to url base and params converted to list of tuples.


return '<STR_LIT>'.format(self.settings.prefix, self.name, key)<EOL>

Generates session key string.

:param str key:
    e.g. ``"authomatic:facebook:key"``


def access(self, url, params=None, method='<STR_LIT:GET>', headers=None,<EOL>body='<STR_LIT>', max_redirects=<NUM_LIT:5>, content_parser=None):

if not self.user and not self.credentials:<EOL><INDENT>raise CredentialsError(u'<STR_LIT>')<EOL><DEDENT>headers = headers or {}<EOL>self._log(<EOL>logging.INFO,<EOL>u'<STR_LIT>'.format(url))<EOL>request_elements = self.create_request_elements(<EOL>request_type=self.PROTECTED_RESOURCE_REQUEST_TYPE,<EOL>credentials=self.credentials,<EOL>url=url,<EOL>body=body,<EOL>params=params,<EOL>headers=headers,<EOL>method=method<EOL>)<EOL>response = self._fetch(*request_elements,<EOL>max_redirects=max_redirects,<EOL>content_parser=content_parser)<EOL>self._log(<EOL>logging.INFO,<EOL>u'<STR_LIT>'.format(<EOL>response.status))<EOL>return response<EOL>

Fetches the **protected resource** of an authenticated **user**.

:param credentials:
    The **user's** :class:`.Credentials` (serialized or normal).

:param str url:
    The URL of the **protected resource**.

:param str method:
    HTTP method of the request.

:param dict headers:
    HTTP headers of the request.

:param str body:
    Body of ``POST``, ``PUT`` and ``PATCH`` requests.

:param int max_redirects:
    Maximum number of HTTP redirects to follow.

:param function content_parser:
    A function to be used to parse the :attr:`.Response.data`
    from :attr:`.Response.content`.

:returns:
    :class:`.Response`


:class:`str` URL where we can get the |oauth1| request token.
see http://oauth.net/core/1.0a/#auth_step1.


response = super(Bitbucket, self)._access_user_info()<EOL>response.data.setdefault("<STR_LIT:email>", None)<EOL>email_response = self.access(self.user_email_url)<EOL>if email_response.data:<EOL><INDENT>for item in email_response.data:<EOL><INDENT>if item.get("<STR_LIT>", False):<EOL><INDENT>response.data.update(email=item.get("<STR_LIT:email>", None))<EOL><DEDENT><DEDENT><DEDENT>return response<EOL>

Email is available in separate method so second request is needed.


for c in self.classes():<EOL><INDENT>ms = inspect.getmembers(c, lambda f: inspect.ismethod(f) or inspect.isfunction(f))<EOL>method_name = getattr(self, '<STR_LIT>', '<STR_LIT>')<EOL>method_regex = '<STR_LIT>'<EOL>if method_name:<EOL><INDENT>if method_name.startswith(self.method_prefix):<EOL><INDENT>method_regex = re.compile(r'<STR_LIT>'.format(method_name), flags=re.I)<EOL><DEDENT>else:<EOL><INDENT>if method_name.startswith("<STR_LIT:*>"):<EOL><INDENT>method_name = method_name.strip("<STR_LIT:*>")<EOL>method_regex = re.compile(<EOL>r'<STR_LIT>'.format(self.method_prefix, method_name),<EOL>flags=re.I<EOL>)<EOL><DEDENT>else:<EOL><INDENT>method_regex = re.compile(<EOL>r'<STR_LIT>'.format(self.method_prefix, method_name),<EOL>flags=re.I<EOL>)<EOL><DEDENT><DEDENT><DEDENT>for m_name, m in ms:<EOL><INDENT>if not m_name.startswith(self.method_prefix): continue<EOL>can_yield = True<EOL>if method_regex and not method_regex.match(m_name):<EOL><INDENT>can_yield = False<EOL><DEDENT>if can_yield:<EOL><INDENT>logger.debug('<STR_LIT>'.format(m_name, method_name))<EOL>yield c, m_name<EOL><DEDENT><DEDENT><DEDENT>

return the actual test methods that matched self.method_name


for tc in self.possible:<EOL><INDENT>tc.raise_found_error()<EOL><DEDENT>

raise any found error in the possible PathFinders


possible = []<EOL>name = self.name<EOL>logger.debug('<STR_LIT>'.format(name))<EOL>name_f = self.name.lower()<EOL>filepath = "<STR_LIT>"<EOL>if name_f.endswith("<STR_LIT>") or "<STR_LIT>" in name_f:<EOL><INDENT>bits = name.split("<STR_LIT::>", <NUM_LIT:1>)<EOL>filepath = bits[<NUM_LIT:0>]<EOL>logger.debug('<STR_LIT>'.format(filepath))<EOL>name = bits[<NUM_LIT:1>] if len(bits) > <NUM_LIT:1> else "<STR_LIT>"<EOL>if name:<EOL><INDENT>logger.debug('<STR_LIT>'.format(name, filepath))<EOL><DEDENT><DEDENT>bits = name.split('<STR_LIT:.>')<EOL>basedir = self.basedir<EOL>method_prefix = self.method_prefix<EOL>if re.search(r'<STR_LIT>', bits[-<NUM_LIT:1>]):<EOL><INDENT>logger.debug('<STR_LIT>'.format(bits[-<NUM_LIT:1>]))<EOL>possible.append(PathFinder(basedir, method_prefix, **{<EOL>'<STR_LIT>': bits[-<NUM_LIT:1>],<EOL>'<STR_LIT>': bits[-<NUM_LIT:2>] if len(bits) > <NUM_LIT:1> else '<STR_LIT>',<EOL>'<STR_LIT>': os.sep.join(bits[<NUM_LIT:0>:-<NUM_LIT:2>]),<EOL>'<STR_LIT>': filepath,<EOL>}))<EOL><DEDENT>elif len(bits) > <NUM_LIT:1> and re.search(r'<STR_LIT>', bits[-<NUM_LIT:2>]):<EOL><INDENT>logger.debug('<STR_LIT>'.format(bits[-<NUM_LIT:2>]))<EOL>possible.append(PathFinder(basedir, method_prefix, **{<EOL>'<STR_LIT>': bits[-<NUM_LIT:2>],<EOL>'<STR_LIT>': bits[-<NUM_LIT:1>],<EOL>'<STR_LIT>': bits[-<NUM_LIT:3>] if len(bits) > <NUM_LIT:2> else '<STR_LIT>',<EOL>'<STR_LIT>': os.sep.join(bits[<NUM_LIT:0>:-<NUM_LIT:3>]),<EOL>'<STR_LIT>': filepath,<EOL>}))<EOL><DEDENT>else:<EOL><INDENT>if self.name:<EOL><INDENT>if filepath:<EOL><INDENT>if len(bits):<EOL><INDENT>possible.append(PathFinder(basedir, method_prefix, **{<EOL>'<STR_LIT>': filepath,<EOL>'<STR_LIT>': bits[<NUM_LIT:0>],<EOL>}))<EOL><DEDENT>else:<EOL><INDENT>possible.append(PathFinder(basedir, method_prefix, **{<EOL>'<STR_LIT>': filepath,<EOL>}))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>logger.debug('<STR_LIT>')<EOL>possible.append(PathFinder(basedir, method_prefix, **{<EOL>'<STR_LIT>': bits[-<NUM_LIT:1>],<EOL>'<STR_LIT>': os.sep.join(bits[<NUM_LIT:0>:-<NUM_LIT:1>]),<EOL>'<STR_LIT>': filepath,<EOL>}))<EOL>possible.append(PathFinder(basedir, method_prefix, **{<EOL>'<STR_LIT>': bits[-<NUM_LIT:1>],<EOL>'<STR_LIT>': bits[-<NUM_LIT:2>] if len(bits) > <NUM_LIT:1> else '<STR_LIT>',<EOL>'<STR_LIT>': os.sep.join(bits[<NUM_LIT:0>:-<NUM_LIT:2>]),<EOL>'<STR_LIT>': filepath,<EOL>}))<EOL>possible.append(PathFinder(basedir, method_prefix, **{<EOL>'<STR_LIT>': os.sep.join(bits),<EOL>'<STR_LIT>': filepath,<EOL>}))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>possible.append(PathFinder(basedir, method_prefix, filepath=filepath))<EOL><DEDENT><DEDENT>logger.debug("<STR_LIT>".format(len(possible)))<EOL>self.possible = possible<EOL>

break up a module path to its various parts (prefix, module, class, method)

this uses PEP 8 conventions, so foo.Bar would be foo module with class Bar

return -- list -- a list of possible interpretations of the module path
    (eg, foo.bar can be bar module in foo module, or bar method in foo module)


def _find_basename(self, name, basenames, is_prefix=False):

ret = "<STR_LIT>"<EOL>fileroots = [(os.path.splitext(n)[<NUM_LIT:0>], n) for n in basenames]<EOL>glob = False<EOL>if name.startswith("<STR_LIT:*>"):<EOL><INDENT>glob = True<EOL><DEDENT>name = name.strip("<STR_LIT:*>")<EOL>for fileroot, basename in fileroots:<EOL><INDENT>if name in fileroot or fileroot in name:<EOL><INDENT>for pf in self.module_postfixes:<EOL><INDENT>logger.debug(<EOL>'<STR_LIT>'.format(<EOL>basename,<EOL>name,<EOL>pf<EOL>))<EOL>if glob:<EOL><INDENT>if name in fileroot and fileroot.endswith(pf):<EOL><INDENT>ret = basename<EOL>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if fileroot.startswith(name) and fileroot.endswith(pf):<EOL><INDENT>ret = basename<EOL>break<EOL><DEDENT><DEDENT><DEDENT>if not ret:<EOL><INDENT>for pf in self.module_prefixes:<EOL><INDENT>n = pf + name<EOL>logger.debug('<STR_LIT>'.format(basename, n))<EOL>if glob:<EOL><INDENT>if fileroot.startswith(pf) and name in fileroot:<EOL><INDENT>ret = basename<EOL>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if fileroot.startswith(n):<EOL><INDENT>ret = basename<EOL>break<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if not ret:<EOL><INDENT>if is_prefix:<EOL><INDENT>logger.debug('<STR_LIT>'.format(basename, name))<EOL>if basename.startswith(name) or (glob and name in basename):<EOL><INDENT>ret = basename<EOL><DEDENT>else:<EOL><INDENT>logger.debug(<EOL>'<STR_LIT>'.format(<EOL>basename,<EOL>name<EOL>))<EOL>if glob:<EOL><INDENT>if name in basename and self._is_module_path(basename):<EOL><INDENT>ret = basename<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if basename.startswith(name) and self._is_module_path(basename):<EOL><INDENT>ret = basename<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>if ret:<EOL><INDENT>logger.debug('<STR_LIT>'.format(ret))<EOL>break<EOL><DEDENT><DEDENT><DEDENT>return ret<EOL>

check if name combined with test prefixes or postfixes is found anywhere
        in the list of basenames

        :param name: string, the name you're searching for
        :param basenames: list, a list of basenames to check
        :param is_prefix: bool, True if this is a prefix search, which means it will
            also check if name matches any of the basenames without the prefixes or
            postfixes, if it is False then the prefixes or postfixes must be present
            (ie, the module we're looking for is the actual test module, not the parent
             modules it's contained in)
        :returns: string, the basename if it is found


module_name = getattr(self, '<STR_LIT>', '<STR_LIT>')<EOL>module_prefix = getattr(self, '<STR_LIT>', '<STR_LIT>')<EOL>filepath = getattr(self, '<STR_LIT>', '<STR_LIT>')<EOL>if filepath:<EOL><INDENT>if os.path.isabs(filepath):<EOL><INDENT>yield filepath<EOL><DEDENT>else:<EOL><INDENT>yield os.path.join(self.basedir, filepath)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if module_prefix:<EOL><INDENT>basedirs = self._find_prefix_paths(self.basedir, module_prefix)<EOL><DEDENT>else:<EOL><INDENT>basedirs = [self.basedir]<EOL><DEDENT>for basedir in basedirs:<EOL><INDENT>try:<EOL><INDENT>if module_name:<EOL><INDENT>path = self._find_module_path(basedir, module_name)<EOL><DEDENT>else:<EOL><INDENT>path = basedir<EOL><DEDENT>if os.path.isfile(path):<EOL><INDENT>logger.debug('<STR_LIT>'.format(path))<EOL>yield path<EOL><DEDENT>else:<EOL><INDENT>seen_paths = set()<EOL>for root, dirs, files in self.walk(path):<EOL><INDENT>for basename in files:<EOL><INDENT>if basename.startswith("<STR_LIT>"):<EOL><INDENT>if self._is_module_path(root):<EOL><INDENT>filepath = os.path.join(root, basename)<EOL>if filepath not in seen_paths:<EOL><INDENT>logger.debug('<STR_LIT>'.format(filepath))<EOL>seen_paths.add(filepath)<EOL>yield filepath<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>fileroot = os.path.splitext(basename)[<NUM_LIT:0>]<EOL>for pf in self.module_postfixes:<EOL><INDENT>if fileroot.endswith(pf):<EOL><INDENT>filepath = os.path.join(root, basename)<EOL>if filepath not in seen_paths:<EOL><INDENT>logger.debug('<STR_LIT>'.format(filepath))<EOL>seen_paths.add(filepath)<EOL>yield filepath<EOL><DEDENT><DEDENT><DEDENT>for pf in self.module_prefixes:<EOL><INDENT>if fileroot.startswith(pf):<EOL><INDENT>filepath = os.path.join(root, basename)<EOL>if filepath not in seen_paths:<EOL><INDENT>logger.debug('<STR_LIT>'.format(filepath))<EOL>seen_paths.add(filepath)<EOL>yield filepath<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>except IOError as e:<EOL><INDENT>logger.warning(e, exc_info=True)<EOL>pass<EOL><DEDENT><DEDENT><DEDENT>

given a basedir, yield all test modules paths recursively found in
basedir that are test modules

return -- generator


Members = namedtuple("<STR_LIT>", ["<STR_LIT:name>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>"])<EOL>log_manager = logging.Logger.manager<EOL>loggers = []<EOL>ignore = set([modname()])<EOL>if log_manager.root:<EOL><INDENT>loggers = list(log_manager.loggerDict.items())<EOL>loggers.append(("<STR_LIT:root>", log_manager.root))<EOL><DEDENT>for logger_name, logger in loggers:<EOL><INDENT>if logger_name in ignore: continue<EOL>for handler in getattr(logger, "<STR_LIT>", []):<EOL><INDENT>members = inspect.getmembers(handler)<EOL>for member_name, member in members:<EOL><INDENT>yield Members(logger_name, handler, member_name, member)<EOL><DEDENT><DEDENT><DEDENT>

iterate through the attributes of every logger's handler

    this is used to switch out stderr and stdout in tests when buffer is True

    :returns: generator of tuples, each tuple has (name, handler, member_name, member_val)


return glob('<STR_LIT>' % path)<EOL>

Finds files by globbing on the local filesystem.


def delete(self, filename, storage_type=None, bucket_name=None):

if not (storage_type and bucket_name):<EOL><INDENT>self._delete_local(filename)<EOL><DEDENT>else:<EOL><INDENT>if storage_type != '<STR_LIT>':<EOL><INDENT>raise ValueError('<STR_LIT>' % storage_type)<EOL><DEDENT>self._delete_s3(filename, bucket_name)<EOL><DEDENT>

Deletes the specified file, either locally or from S3, depending on the file's storage type.


def _save_local(self, temp_file, filename, obj):

path = self._get_path(filename)<EOL>if not os.path.exists(os.path.dirname(path)):<EOL><INDENT>os.makedirs(os.path.dirname(path), self.permission | <NUM_LIT>)<EOL><DEDENT>fd = open(path, '<STR_LIT:wb>')<EOL>temp_file.seek(<NUM_LIT:0>)<EOL>t = temp_file.read(<NUM_LIT>)<EOL>while t:<EOL><INDENT>fd.write(t)<EOL>t = temp_file.read(<NUM_LIT>)<EOL><DEDENT>fd.close()<EOL>if self.filesize_field:<EOL><INDENT>setattr(obj, self.filesize_field, os.path.getsize(path))<EOL><DEDENT>return filename<EOL>

Saves the specified file to the local file system.


def _delete_s3(self, filename, bucket_name):

conn = S3Connection(self.access_key_id, self.access_key_secret)<EOL>bucket = conn.get_bucket(bucket_name)<EOL>if type(filename).__name__ == '<STR_LIT>':<EOL><INDENT>filename = '<STR_LIT:/>' + filename.name<EOL><DEDENT>path = self._get_s3_path(filename)<EOL>k = Key(bucket)<EOL>k.key = path<EOL>try:<EOL><INDENT>bucket.delete_key(k)<EOL><DEDENT>except S3ResponseError:<EOL><INDENT>pass<EOL><DEDENT>

Deletes the specified file from the given S3 bucket.


parsed_interfaces = collections.defaultdict(dict)<EOL>for m, d in iteritems(interfaces):<EOL><INDENT>app, func = m.split('<STR_LIT:.>', <NUM_LIT:1>)<EOL>method = parsed_interfaces[app][func] = {}<EOL>method['<STR_LIT>'] = ['<STR_LIT>', '<STR_LIT>']<EOL>method['<STR_LIT>'] = '<STR_LIT:POST>'<EOL>method['<STR_LIT>'] = {}<EOL>method['<STR_LIT>'] = {}<EOL>for name, type_info in iteritems(dict(d['<STR_LIT>'])):<EOL><INDENT>optionality = '<STR_LIT>'<EOL>param_type = '<STR_LIT:string>'<EOL>type_info = TYPE_INFO_COMMENT_RE.sub('<STR_LIT>', type_info)<EOL>info_pieces = TYPE_INFO_SPLITTER_RE.findall(type_info)<EOL>for info_piece in info_pieces:<EOL><INDENT>if info_piece in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>optionality = info_piece<EOL><DEDENT>elif info_piece == '<STR_LIT>':<EOL><INDENT>optionality = '<STR_LIT>'<EOL>param_type = '<STR_LIT:string>'<EOL><DEDENT>elif info_piece == '<STR_LIT>':<EOL><INDENT>optionality = '<STR_LIT>'<EOL><DEDENT>elif info_piece == '<STR_LIT>':<EOL><INDENT>optionality = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>param_type = info_piece<EOL><DEDENT><DEDENT>method[optionality][name] = map_param_type(param_type)<EOL><DEDENT><DEDENT>return dict(parsed_interfaces)<EOL>

Parse the conduit.query json dict response
This performs the logic of parsing the non-standard params dict
    and then returning a dict Resource can understand


init_py = open(os.path.join(package, '<STR_LIT>')).read()<EOL>return re.search("<STR_LIT>",<EOL>init_py, re.MULTILINE).group(<NUM_LIT:1>)<EOL>

Return package version as listed in `__version__` in `init.py`.


@cli.command()<EOL>@click.argument('<STR_LIT>')<EOL>@click.pass_context<EOL>def retrieve(ctx, preview_id, *args, **kwargs):

file_previews = ctx.obj['<STR_LIT>']<EOL>results = file_previews.retrieve(preview_id)<EOL>click.echo(results)<EOL>


def _run_ssh(entries, username, idfile, no_prompt=False, command=None,<EOL>show=None, only=None, sort_by=None, limit=None, tunnel=None,<EOL>num=None, random=False):

_print_entries = num is None<EOL>_print_help = False<EOL>if len(entries) == <NUM_LIT:0>:<EOL><INDENT>exit('<STR_LIT>')<EOL><DEDENT>if no_prompt is True and command is not None:<EOL><INDENT>return _run_ssh_command(entries, username, idfile, command, tunnel)<EOL><DEDENT>elif len(entries) == <NUM_LIT:1> or random is True:<EOL><INDENT>entry = py_random.choice(entries)<EOL>if command is None:<EOL><INDENT>return _connect_ssh(entry, username, idfile, tunnel)<EOL><DEDENT>else:<EOL><INDENT>return _run_ssh_command([entry], username, idfile, command, tunnel)<EOL><DEDENT><DEDENT>elif command is not None:<EOL><INDENT>print(HostEntry.render_entries(entries,<EOL>additional_columns=show,<EOL>only_show=only, numbers=True))<EOL>if no_prompt is False:<EOL><INDENT>get_input("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>.format(cyan(command), len(entries)))<EOL><DEDENT>return _run_ssh_command(entries, username, idfile, command, tunnel)<EOL><DEDENT>else:<EOL><INDENT>while True:<EOL><INDENT>if sort_by is not None:<EOL><INDENT>entries = HostEntry.sort_by(entries, sort_by)<EOL><DEDENT>if limit is not None:<EOL><INDENT>entries = entries[:limit]<EOL><DEDENT>if _print_entries is True:<EOL><INDENT>print(HostEntry.render_entries(entries,<EOL>additional_columns=show,<EOL>only_show=only, numbers=True))<EOL>print('<STR_LIT>' % len(entries))<EOL>_print_entries = False<EOL><DEDENT>if _print_help is True:<EOL><INDENT>cmd_str = green(command) if command is not None else '<STR_LIT>'<EOL>msg = COMMANDS_STRING.format(username=username or '<STR_LIT>',<EOL>idfile=idfile or '<STR_LIT>',<EOL>cur_cmd=cmd_str)<EOL>print(msg)<EOL>_print_help = False<EOL><DEDENT>elif command is not None:<EOL><INDENT>print('<STR_LIT>' % cyan(command))<EOL><DEDENT>msg = '<STR_LIT>' % (cyan('<STR_LIT:h>'),<EOL>cyan('<STR_LIT:q>'))<EOL>if num is not None:<EOL><INDENT>choice = num<EOL><DEDENT>else:<EOL><INDENT>choice = get_input(msg)<EOL><DEDENT>if isinstance(choice, int):<EOL><INDENT>if <NUM_LIT:0> <= choice <= len(entries):<EOL><INDENT>break<EOL><DEDENT>else:<EOL><INDENT>if num is not None:<EOL><INDENT>exit("<STR_LIT>"<EOL>.format(num, len(entries) - <NUM_LIT:1>))<EOL><DEDENT>else:<EOL><INDENT>msg = "<STR_LIT>"<EOL>print(msg % (len(entries) - <NUM_LIT:1>))<EOL><DEDENT><DEDENT><DEDENT>elif choice == '<STR_LIT:x>':<EOL><INDENT>if command is None:<EOL><INDENT>print('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>return _run_ssh_command(entries, username, idfile,<EOL>command, tunnel)<EOL><DEDENT><DEDENT>elif choice == '<STR_LIT:h>':<EOL><INDENT>_print_help = True<EOL><DEDENT>elif choice in ['<STR_LIT:q>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>print('<STR_LIT>')<EOL>return<EOL><DEDENT>else:<EOL><INDENT>commands = choice.split()<EOL>if len(commands) < <NUM_LIT:2>:<EOL><INDENT>print(yellow('<STR_LIT>' % choice))<EOL><DEDENT>else:<EOL><INDENT>cmd = commands[<NUM_LIT:0>]<EOL>if cmd in ['<STR_LIT:u>', '<STR_LIT:i>', '<STR_LIT:p>']:<EOL><INDENT>if cmd == '<STR_LIT:u>':<EOL><INDENT>username = commands[<NUM_LIT:1>]<EOL><DEDENT>elif cmd == '<STR_LIT:i>':<EOL><INDENT>_idfile = commands[<NUM_LIT:1>]<EOL>if not os.path.exists(_idfile):<EOL><INDENT>print(yellow('<STR_LIT>' % _idfile))<EOL>continue<EOL><DEDENT>idfile = _idfile<EOL><DEDENT>elif cmd == '<STR_LIT:p>':<EOL><INDENT>p = commands[<NUM_LIT:1>]<EOL>try:<EOL><INDENT>profile = LsiProfile.load(p)<EOL>_username = profile.username<EOL>_idfile = expanduser(profile.identity_file)<EOL><DEDENT>except LsiProfile.LoadError:<EOL><INDENT>print(yellow('<STR_LIT>' % repr(p)))<EOL>continue<EOL><DEDENT>username = _username<EOL>idfile = _idfile<EOL><DEDENT>print('<STR_LIT>' % green(repr(username)))<EOL>print('<STR_LIT>' % green(repr(idfile)))<EOL><DEDENT>elif cmd == '<STR_LIT:f>':<EOL><INDENT>entries = filter_entries(entries, commands[<NUM_LIT:1>:], [])<EOL>_print_entries = True<EOL><DEDENT>elif cmd == '<STR_LIT:e>':<EOL><INDENT>entries = filter_entries(entries, [], commands[<NUM_LIT:1>:])<EOL>_print_entries = True<EOL><DEDENT>elif cmd == '<STR_LIT:c>':<EOL><INDENT>command = '<STR_LIT:U+0020>'.join(commands[<NUM_LIT:1>:])<EOL><DEDENT>elif cmd == '<STR_LIT>':<EOL><INDENT>try:<EOL><INDENT>limit = int(commands[<NUM_LIT:1>])<EOL>_print_entries = True<EOL><DEDENT>except ValueError:<EOL><INDENT>print(yellow('<STR_LIT>'))<EOL><DEDENT><DEDENT>elif cmd == '<STR_LIT>':<EOL><INDENT>sort_by = commands[<NUM_LIT:1>]<EOL>if sort_by not in show:<EOL><INDENT>show.append(sort_by)<EOL><DEDENT>_print_entries = True<EOL><DEDENT>elif cmd == '<STR_LIT>':<EOL><INDENT>if show is None:<EOL><INDENT>show = commands[<NUM_LIT:1>:]<EOL><DEDENT>else:<EOL><INDENT>show.extend(commands[<NUM_LIT:1>:])<EOL><DEDENT>_print_entries = True<EOL><DEDENT>else:<EOL><INDENT>print(yellow('<STR_LIT>' % cmd))<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return _connect_ssh(entries[choice], username, idfile, tunnel)<EOL><DEDENT>

Lets the user choose which instance to SSH into.

:param entries: The list of host entries.
:type entries: [:py:class:`HostEntry`]
:param username: The SSH username to use. Defaults to current user.
:type username: ``str`` or ``NoneType``
:param idfile: The identity file to use. Optional.
:type idfile: ``str`` or ``NoneType``
:param no_prompt: Whether to disable confirmation for SSH command.
:type no_prompt: ``bool``
:param command: SSH command to run on matching instances.
:type command: ``str`` or ``NoneType``
:param show: Instance attributes to show in addition to defaults.
:type show: ``NoneType`` or ``list`` of ``str``
:param only: If not ``None``, will *only* show these attributes.
:type only: ``NoneType`` or ``list`` of ``str``
:param sort_by: What to sort columns by. By default, sort by 'name'.
:type sort_by: ``str``
:param limit: At most how many results to show.
:type limit: ``int`` or ``NoneType``
:param num: If not None, choose the given entry from within filters.
:type num: ``int`` or ``NoneType``
:param random: If true, choose a random entry from within filters.
:type random: ``bool``


def _run_ssh_command(entries, username, idfile, command, tunnel,<EOL>parallel=False):

if len(entries) == <NUM_LIT:0>:<EOL><INDENT>print('<STR_LIT>')<EOL>return <NUM_LIT:1><EOL><DEDENT>if command.strip() == '<STR_LIT>' or command is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>print('<STR_LIT>'<EOL>.format(green(repr(command)), len(entries)))<EOL>shell_cmds = []<EOL>for entry in entries:<EOL><INDENT>hname = entry.hostname or entry.public_ip<EOL>cmd = _build_ssh_command(hname, username, idfile, command, tunnel)<EOL>shell_cmds.append({<EOL>'<STR_LIT>': cmd,<EOL>'<STR_LIT:description>': entry.display()<EOL>})<EOL><DEDENT>stream_commands(shell_cmds, parallel=parallel)<EOL>print(green('<STR_LIT>'))<EOL>

Runs the given command over SSH in parallel on all hosts in `entries`.

:param entries: The host entries the hostnames from.
:type entries: ``list`` of :py:class:`HostEntry`
:param username: To use a specific username.
:type username: ``str`` or ``NoneType``
:param idfile: The SSH identity file to use, or none.
:type idfile: ``str`` or ``NoneType``
:param command: The command to run.
:type command: ``str``
:param parallel: If true, commands will be run in parallel.
:type parallel: ``bool``


def _build_ssh_command(hostname, username, idfile, ssh_command, tunnel):

command = [_get_path('<STR_LIT>'),<EOL>'<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>']<EOL>if idfile is not None:<EOL><INDENT>command.extend(['<STR_LIT>', idfile])<EOL><DEDENT>if tunnel is not None:<EOL><INDENT>command.extend(['<STR_LIT>', '<STR_LIT>', tunnel, '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'])<EOL><DEDENT>if username is not None:<EOL><INDENT>command.append('<STR_LIT>'.format(username, hostname))<EOL><DEDENT>else:<EOL><INDENT>command.append(hostname)<EOL><DEDENT>if ssh_command is not None:<EOL><INDENT>command.append(repr(ssh_command))<EOL><DEDENT>return('<STR_LIT:U+0020>'.join(command))<EOL>

Uses hostname and other info to construct an SSH command.


parser = argparse.ArgumentParser(description='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', action='<STR_LIT:store_true>', default=False,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', action='<STR_LIT:store_true>', default=False,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', action='<STR_LIT:store_true>', default=False,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', help='<STR_LIT>',<EOL>default=None)<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', action='<STR_LIT:store_true>',<EOL>help='<STR_LIT>', default=False)<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', help='<STR_LIT>',<EOL>default=None)<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', default=None,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', nargs='<STR_LIT:*>',<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', nargs='<STR_LIT:+>',<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT:-c>', '<STR_LIT>', type=str,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', action='<STR_LIT:store_true>', default=False,<EOL>help="<STR_LIT>"<EOL>"<STR_LIT>")<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', type=str,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', nargs='<STR_LIT:+>', default=None,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', nargs='<STR_LIT:+>', default=None,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', type=str, default=None,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', type=str, default="<STR_LIT:name>",<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', type=int, default=None,<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', action='<STR_LIT:store_true>',<EOL>help='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', nargs=<NUM_LIT:2>, default=None,<EOL>help='<STR_LIT>'<EOL>'<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', nargs=<NUM_LIT:2>, default=None,<EOL>help='<STR_LIT>'<EOL>'<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', '<STR_LIT>', default=None,<EOL>help='<STR_LIT>')<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", action="<STR_LIT:store_true>", default=False,<EOL>help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", type=int, default=None,<EOL>help="<STR_LIT>")<EOL>args = parser.parse_args()<EOL>if args.exclude is None:<EOL><INDENT>args.exclude = []<EOL><DEDENT>if args.sort_by is not None:<EOL><INDENT>args.show = (args.show or []) + [args.sort_by]<EOL><DEDENT>return args<EOL>


lsi_location = os.path.expanduser('<STR_LIT>')<EOL>if not os.path.exists(lsi_location):<EOL><INDENT>return LsiProfile()<EOL><DEDENT>cfg_parser = ConfigParser()<EOL>cfg_parser.read(lsi_location)<EOL>if profile_name is None:<EOL><INDENT>if cfg_parser.has_section('<STR_LIT:default>'):<EOL><INDENT>profile_name = '<STR_LIT:default>'<EOL><DEDENT>else:<EOL><INDENT>return cls()<EOL><DEDENT><DEDENT>elif not cfg_parser.has_section(profile_name):<EOL><INDENT>raise cls.LoadError('<STR_LIT>'.format(profile_name))<EOL><DEDENT>def _get(option, alt=None):<EOL><INDENT>"""<STR_LIT>"""<EOL>if cfg_parser.has_option(profile_name, option):<EOL><INDENT>return cfg_parser.get(profile_name, option)<EOL><DEDENT>else:<EOL><INDENT>return alt<EOL><DEDENT><DEDENT>if cfg_parser.has_option(profile_name, '<STR_LIT>'):<EOL><INDENT>profile = cls.load(cfg_parser.get(profile_name, '<STR_LIT>'))<EOL><DEDENT>else:<EOL><INDENT>profile = cls()<EOL><DEDENT>profile.override('<STR_LIT:username>', _get('<STR_LIT:username>'))<EOL>profile.override('<STR_LIT>', _get('<STR_LIT>'))<EOL>profile.override('<STR_LIT>', _get('<STR_LIT>'))<EOL>filters = [s for s in _get('<STR_LIT>', '<STR_LIT>').split('<STR_LIT:U+002C>') if len(s) > <NUM_LIT:0>]<EOL>exclude = [s for s in _get('<STR_LIT>', '<STR_LIT>').split('<STR_LIT:U+002C>') if len(s) > <NUM_LIT:0>]<EOL>profile.filters.extend(filters)<EOL>profile.exclude.extend(exclude)<EOL>return profile<EOL>

Loads the user's LSI profile, or provides a default.


num_columns = max(len(row) for row in table)<EOL>for row in table:<EOL><INDENT>while len(row) < num_columns:<EOL><INDENT>row.append('<STR_LIT>')<EOL><DEDENT>for i in range(num_columns):<EOL><INDENT>row[i] = str(row[i]) if row[i] is not None else '<STR_LIT>'<EOL><DEDENT><DEDENT>return table<EOL>

Prepare the rows so they're all strings, and all the same length.

:param table: A 2D grid of anything.
:type table: [[``object``]]

:return: A table of strings, where every row is the same length.
:rtype: [[``str``]]


def render_row(num, columns, widths, column_colors=None):

row_str = '<STR_LIT:|>'<EOL>cell_strs = []<EOL>for i, column in enumerate(columns):<EOL><INDENT>try:<EOL><INDENT>cell = column[num]<EOL>spaces = '<STR_LIT:U+0020>' * (widths[i] - len(cell))<EOL>if column_colors is not None and column_colors[i] is not None:<EOL><INDENT>cell = column_colors[i](cell)<EOL><DEDENT>cell_strs.append('<STR_LIT>' % (cell, spaces))<EOL><DEDENT>except IndexError:<EOL><INDENT>cell_strs.append('<STR_LIT:U+0020>' * (widths[i] + <NUM_LIT:2>))<EOL><DEDENT><DEDENT>return '<STR_LIT>' % '<STR_LIT:|>'.join(cell_strs)<EOL>

Render the `num`th row of each column in `columns`.

:param num: Which row to render.
:type num: ``int``
:param columns: The list of columns.
:type columns: [[``str``]]
:param widths: The widths of each column.
:type widths: [``int``]
:param column_colors: An optional list of coloring functions.
:type column_colors: [``str`` -> ``str``] or ``NoneType``

:return: The rendered row.
:rtype: ``str``


if len(table) == <NUM_LIT:0>:<EOL><INDENT>return table<EOL><DEDENT>else:<EOL><INDENT>num_columns = len(table[<NUM_LIT:0>])<EOL>return [[row[i] for row in table] for i in range(num_columns)]<EOL><DEDENT>

Transposes a table, turning rows into columns.
:param table: A 2D string grid.
:type table: [[``str``]]

:return: The same table, with rows and columns flipped.
:rtype: [[``str``]]


try:<EOL><INDENT>return int(os.popen('<STR_LIT>', '<STR_LIT:r>').read().split()[<NUM_LIT:1>])<EOL><DEDENT>except Exception:<EOL><INDENT>return <NUM_LIT><EOL><DEDENT>

Returns the current terminal size, in characters.
:rtype: ``int``


f = {'<STR_LIT>': '<STR_LIT>', '<STR_LIT>': name}<EOL>ec2 = boto.connect_ec2(region=get_region())<EOL>rs = ec2.get_all_instances(filters=f)<EOL>if len(rs) == <NUM_LIT:0>:<EOL><INDENT>raise Exception('<STR_LIT>' % name)<EOL><DEDENT>print(rs[<NUM_LIT:0>].instances[<NUM_LIT:0>].public_dns_name)<EOL>

Prints the public dns name of `name`, if it exists.

:param name: The instance name.
:type name: ``str``


with open(get_cache_location()) as f:<EOL><INDENT>contents = f.read()<EOL>objects = json.loads(contents)<EOL>return [HostEntry.from_dict(obj) for obj in objects]<EOL><DEDENT>

Reads the description cache, returning each instance's information.
:return: A list of host entries.
:rtype: [:py:class:`HostEntry`]


if attrib_name.startswith('<STR_LIT>'):<EOL><INDENT>tagname = attrib_name[len('<STR_LIT>'):]<EOL>return '<STR_LIT>'.format(tagname)<EOL><DEDENT>elif attrib_name in cls.COLUMN_NAMES:<EOL><INDENT>return cls.COLUMN_NAMES[attrib_name]<EOL><DEDENT>else:<EOL><INDENT>return attrib_name<EOL><DEDENT>

Returns the "pretty name" (capitalized, etc) of an attribute, by
looking it up in ``cls.COLUMN_NAMES`` if it exists there.

:param attrib_name: An attribute name.
:type attrib_name: ``str``

:rtype: ``str``


return cls(<EOL>name=instance.tags.get('<STR_LIT:Name>'),<EOL>private_ip=instance.private_ip_address,<EOL>public_ip=instance.ip_address,<EOL>instance_type=instance.instance_type,<EOL>instance_id=instance.id,<EOL>hostname=instance.dns_name,<EOL>stack_id=instance.tags.get('<STR_LIT>'),<EOL>stack_name=instance.tags.get('<STR_LIT>'),<EOL>logical_id=instance.tags.get('<STR_LIT>'),<EOL>security_groups=[g.name for g in instance.groups],<EOL>launch_time=instance.launch_time,<EOL>ami_id=instance.image_id,<EOL>tags={k.lower(): v for k, v in six.iteritems(instance.tags)}<EOL>)<EOL>

Loads a ``HostEntry`` from a boto instance.

:param instance: A boto instance object.
:type instance: :py:class:`boto.ec2.instanceInstance`

:rtype: :py:class:`HostEntry`


try:<EOL><INDENT>return fmat_string.format(**vars(self))<EOL><DEDENT>except KeyError as e:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>'.format(repr(fmat_string),<EOL>repr(e)))<EOL><DEDENT>

Takes a string containing 0 or more {variables} and formats it
according to this instance's attributes.

:param fmat_string: A string, e.g. '{name}-foo.txt'
:type fmat_string: ``str``

:return: The string formatted according to this instance. E.g.
         'production-runtime-foo.txt'
:rtype: ``str``


region = get_region()<EOL>return join(CACHE_DIRECTORY, region.name + "<STR_LIT>")<EOL>

Use the region name to get the location of the cache JSON file.


if isinstance(self.name, six.string_types) and len(self.name) > <NUM_LIT:0>:<EOL><INDENT>return '<STR_LIT>'.format(self.name, self.public_ip)<EOL><DEDENT>else:<EOL><INDENT>return self.public_ip<EOL><DEDENT>

Returns the best name to display for this host. Uses the instance
name if available; else just the public IP.

:rtype: ``str``


fake_args = [None for _ in inspect.getargspec(cls.__init__).args[<NUM_LIT:1>:]]<EOL>fake_instance = cls(*fake_args)<EOL>return vars(fake_instance).keys()<EOL>

Lists all of the attributes to be found on an instance of this class.
It creates a "fake instance" by passing in `None` to all of the
``__init__`` arguments, then returns all of the attributes of that
instance.

:return: A list of instance attributes of this class.
:rtype: ``list`` of ``str``


seen = set()<EOL>result = []<EOL>for x in _list:<EOL><INDENT>if x not in seen:<EOL><INDENT>result.append(x)<EOL>seen.add(x)<EOL><DEDENT><DEDENT>return result<EOL>


fpath = os.path.join(self.tempdir, path)<EOL>if not os.path.exists(os.path.dirname(fpath)):<EOL><INDENT>os.makedirs(os.path.dirname(fpath))<EOL><DEDENT>open(fpath, '<STR_LIT:a>').close()<EOL>return fpath<EOL>

Create an empty file in the temporary directory, return the full path.


try:<EOL><INDENT>return super(BugwarriorConfigParser, self).getint(section, option)<EOL><DEDENT>except ValueError:<EOL><INDENT>if self.get(section, option) == u'<STR_LIT>':<EOL><INDENT>return None<EOL><DEDENT>else:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>".format(<EOL>section=section, option=option))<EOL><DEDENT><DEDENT>


if os.environ.get(BUGWARRIORRC):<EOL><INDENT>return os.environ[BUGWARRIORRC]<EOL><DEDENT>xdg_config_home = (<EOL>os.environ.get('<STR_LIT>') or os.path.expanduser('<STR_LIT>'))<EOL>xdg_config_dirs = (<EOL>(os.environ.get('<STR_LIT>') or '<STR_LIT>').split('<STR_LIT::>'))<EOL>paths = [<EOL>os.path.join(xdg_config_home, '<STR_LIT>', '<STR_LIT>'),<EOL>os.path.expanduser("<STR_LIT>")]<EOL>paths += [<EOL>os.path.join(d, '<STR_LIT>', '<STR_LIT>') for d in xdg_config_dirs]<EOL>for path in paths:<EOL><INDENT>if os.path.exists(path):<EOL><INDENT>return path<EOL><DEDENT><DEDENT>return paths[<NUM_LIT:0>]<EOL>

Determine the path to the config file. This will return, in this order of
precedence:
- the value of $BUGWARRIORRC if set
- $XDG_CONFIG_HOME/bugwarrior/bugwarriorc if exists
- ~/.bugwarriorrc if exists
- <dir>/bugwarrior/bugwarriorc if exists, for dir in $XDG_CONFIG_DIRS
- $XDG_CONFIG_HOME/bugwarrior/bugwarriorc otherwise