def magic(dataset, method='<STR_LIT>', roi_mask=None,<EOL>coactivation_mask=None, features=None, feature_threshold=<NUM_LIT>,<EOL>min_voxels_per_study=None, min_studies_per_voxel=None,<EOL>reduce_reference='<STR_LIT>', n_components=<NUM_LIT:100>,<EOL>distance_metric='<STR_LIT>', clustering_algorithm='<STR_LIT>',<EOL>n_clusters=<NUM_LIT:5>, clustering_kwargs={}, output_dir=None, filename=None,<EOL>coactivation_images=False, coactivation_threshold=<NUM_LIT:0.1>):

roi = Clusterable(dataset, roi_mask, min_voxels=min_voxels_per_study,<EOL>min_studies=min_studies_per_voxel, features=features,<EOL>feature_threshold=feature_threshold)<EOL>if method == '<STR_LIT>':<EOL><INDENT>reference = Clusterable(dataset, coactivation_mask,<EOL>min_voxels=min_voxels_per_study,<EOL>min_studies=min_studies_per_voxel,<EOL>features=features,<EOL>feature_threshold=feature_threshold)<EOL><DEDENT>elif method == '<STR_LIT>':<EOL><INDENT>reference = deepcopy(roi)<EOL>feature_data = dataset.feature_table.data<EOL>n_studies = len(feature_data)<EOL>reference.data = reference.data.dot(feature_data.values) / n_studies<EOL><DEDENT>elif method == '<STR_LIT>':<EOL><INDENT>reference = roi<EOL><DEDENT>if reduce_reference is not None:<EOL><INDENT>if isinstance(reduce_reference, string_types):<EOL><INDENT>n_feat = reference.data.shape[<NUM_LIT:1>]<EOL>n_components = min(n_components, n_feat)<EOL>reduce_reference = {<EOL>'<STR_LIT>': sk_decomp.PCA,<EOL>'<STR_LIT>': sk_decomp.FastICA<EOL>}[reduce_reference](n_components)<EOL><DEDENT>transpose = (method == '<STR_LIT>')<EOL>reference = reference.transform(reduce_reference, transpose=transpose)<EOL><DEDENT>if method == '<STR_LIT>':<EOL><INDENT>distances = pairwise_distances(roi.data, reference.data,<EOL>metric=distance_metric)<EOL><DEDENT>else:<EOL><INDENT>distances = reference.data<EOL><DEDENT>if isinstance(clustering_algorithm, string_types):<EOL><INDENT>clustering_algorithm = {<EOL>'<STR_LIT>': sk_cluster.KMeans,<EOL>'<STR_LIT>': sk_cluster.MiniBatchKMeans<EOL>}[clustering_algorithm](n_clusters, **clustering_kwargs)<EOL><DEDENT>labels = clustering_algorithm.fit_predict(distances) + <NUM_LIT:1.><EOL>header = roi.masker.get_header()<EOL>header['<STR_LIT>'] = labels.max()<EOL>header['<STR_LIT>'] = labels.min()<EOL>voxel_labels = roi.masker.unmask(labels)<EOL>img = nifti1.Nifti1Image(voxel_labels, None, header)<EOL>if output_dir is not None:<EOL><INDENT>if not exists(output_dir):<EOL><INDENT>makedirs(output_dir)<EOL><DEDENT>if filename is None:<EOL><INDENT>filename = '<STR_LIT>' % n_clusters<EOL><DEDENT>outfile = join(output_dir, filename)<EOL>img.to_filename(outfile)<EOL>if coactivation_images:<EOL><INDENT>for l in np.unique(voxel_labels):<EOL><INDENT>roi_mask = np.copy(voxel_labels)<EOL>roi_mask[roi_mask != l] = <NUM_LIT:0><EOL>ids = dataset.get_studies(<EOL>mask=roi_mask, activation_threshold=coactivation_threshold)<EOL>ma = meta.MetaAnalysis(dataset, ids)<EOL>ma.save_results(output_dir=join(output_dir, '<STR_LIT>'),<EOL>prefix='<STR_LIT>' % l)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>return img<EOL><DEDENT>

Execute a full clustering analysis pipeline.
    Args:
        dataset: a Dataset instance to extract all data from.
        method (str): the overall clustering approach to use. Valid options:
            'coactivation' (default): Clusters voxel within the ROI mask based
                on shared pattern of coactivation with the rest of the brain.
            'studies': Treat each study as a feature in an n-dimensional space.
                I.e., voxels will be assigned to the same cluster if they tend
                to be co-reported in similar studies.
            'features': Voxels will be assigned to the same cluster if they
                tend to have similar feature vectors (i.e., the studies that
                activate those voxels tend to use similar terms).
        roi_mask: A string, nibabel image, or numpy array providing an
            inclusion mask of voxels to cluster. If None, the default mask
            in the Dataset instance is used (typically, all in-brain voxels).
        coactivation_mask: If method='coactivation', this mask defines the
            voxels to use when generating the pairwise distance matrix. For
            example, if a PFC mask is passed, all voxels in the roi_mask will
            be clustered based on how similar their patterns of coactivation
            with PFC voxels are. Can be a str, nibabel image, or numpy array.
        features (str or list): Optional string or list of strings specifying
            any feature names to use for study selection. E.g., passing
            ['emotion', 'reward'] would retain for analysis only those studies
            associated with the features emotion or reward at a frequency
            greater than feature_threshold.
        feature_threshold (float): The threshold to use when selecting studies
            on the basis of features.
        min_voxels_per_study (int): Minimum number of active voxels a study
            must report in order to be retained in the dataset. By default,
            all studies are used.
        min_studies_per_voxel (int): Minimum number of studies a voxel must be
            active in in order to be retained in analysis. By default, all
            voxels are used.
        reduce_reference (str, scikit-learn object or None): The dimensionality
            reduction algorithm to apply to the feature space prior to the
            computation of pairwise distances. If a string is passed (either
            'pca' or 'ica'), n_components must be specified. If None, no
            dimensionality reduction will be applied. Otherwise, must be a
            scikit-learn-style object that exposes a transform() method.
        n_components (int): Number of components to extract during the
            dimensionality reduction step. Only used if reduce_reference is
            a string.
        distance_metric (str): The distance metric to use when computing
            pairwise distances on the to-be-clustered voxels. Can be any of the
            metrics supported by sklearn.metrics.pairwise_distances.
        clustering_algorithm (str or scikit-learn object): the clustering
            algorithm to use. If a string, must be one of 'kmeans' or 'minik'.
            Otherwise, any sklearn class that exposes a fit_predict() method.
        n_clusters (int): If clustering_algorithm is a string, the number of
            clusters to extract.
        clustering_kwargs (dict): Additional keywords to pass to the clustering
            object.
        output_dir (str): The directory to write results to. If None (default),
            returns the cluster label image rather than saving to disk.
        filename (str): Name of cluster label image file. Defaults to
            cluster_labels_k{k}.nii.gz, where k is the number of clusters.
        coactivation_images (bool): If True, saves a meta-analytic coactivation
            map for every ROI in the resulting cluster map.
        coactivation_threshold (float or int): If coactivation_images is True,
            this is the threshold used to define whether or not a study is
            considered to activation within a cluster ROI. Integer values are
            interpreted as minimum number of voxels within the ROI; floats
            are interpreted as the proportion of voxels. Defaults to 0.1 (i.e.,
            10% of all voxels within ROI must be active).


def transform(self, transformer, transpose=False):

data = self.data.T if transpose else self.data<EOL>data = transformer.fit_transform(data)<EOL>self.data = data.T if transpose else data<EOL>return self<EOL>

Apply a transformation to the Clusterable instance. Accepts any
        scikit-learn-style class that implements a fit_transform() method.


self.login = api_login<EOL>self.key = api_key<EOL>self.api_url = self.api_base_url.format(api_version=self.api_version)<EOL>

Initializes OpenLoad instance with given parameters and formats api base url.

        Args:
            api_login (str): API Login found in openload.co
            api_key (str): API Key found in openload.co

        Returns:
            None


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


cls._check_status(response_json)<EOL>return response_json['<STR_LIT:result>']<EOL>

Check the incoming response, raise error if it's needed otherwise return the incoming response_json

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

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


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.


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
                      }


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

Makes a request to prepare for file download,
        this download preparation will be used before get_download_link method.

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

        Returns:
            dict: dictionary containing (ticket, captcha info, ...). ::

                  {
                    "ticket": "72fA-_Lq8Ak~~1440353112~n~~0~nXtN3RI-nsEa28Iq",
                    "captcha_url": "https://openload.co/dlcaptcha/b92eY_nfjV4.png",
                    "captcha_w": 140,
                    "captcha_h": 70,
                    "wait_time": 10,
                    "valid_until": "2015-08-23 18:20:13"
                 }


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"
                  }


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",
                     },
                     ...
                   }


def upload_link(self, folder_id=None, sha1=None, httponly=False):

kwargs = {'<STR_LIT>': folder_id, '<STR_LIT>': sha1, '<STR_LIT>': httponly}<EOL>params = {key: value for key, value in kwargs.items() if value}<EOL>return self._get('<STR_LIT>', params=params)<EOL>

Makes a request to prepare for file upload.

        Note:
            If folder_id is not provided, it will make and upload link to the ``Home`` folder.

        Args:
            folder_id (:obj:`str`, optional): folder-ID to upload to.
            sha1 (:obj:`str`, optional): expected sha1 If sha1 of uploaded file doesn't match this value, upload fails.
            httponly (:obj:`bool`, optional): If this is set to true, use only http upload links.

        Returns:
            dict: dictionary containing (url: will be used in actual upload, valid_until). ::

                {
                    "url": "https://1fiafqj.oloadcdn.net/uls/nZ8H3X9e0AotInbU",
                    "valid_until": "2017-08-19 19:06:46"
                }


def upload_file(self, file_path, folder_id=None, sha1=None, httponly=False):

upload_url_response_json = self.upload_link(folder_id=folder_id, sha1=sha1, httponly=httponly)<EOL>upload_url = upload_url_response_json['<STR_LIT:url>']<EOL>with open(file_path, '<STR_LIT:rb>') as f:<EOL><INDENT>response_json = requests.post(upload_url, files={'<STR_LIT>': f}).json()<EOL><DEDENT>self._check_status(response_json)<EOL>return response_json['<STR_LIT:result>']<EOL>

Calls upload_link request to get valid url, then it makes a post request with given file to be uploaded.
        No need to call upload_link explicitly since upload_file calls it.

        Note:
            If folder_id is not provided, the file will be uploaded to ``Home`` folder.

        Args:
            file_path (str): full path of the file to be uploaded.
            folder_id (:obj:`str`, optional): folder-ID to upload to.
            sha1 (:obj:`str`, optional): expected sha1 If sha1 of uploaded file doesn't match this value, upload fails.
            httponly (:obj:`bool`, optional): If this is set to true, use only http upload links.

        Returns:
            dict: dictionary containing uploaded file info. ::

                {
                    "content_type": "application/zip",
                    "id": "0yiQTPzi4Y4",
                    "name": 'favicons.zip',
                    "sha1": 'f2cb05663563ec1b7e75dbcd5b96d523cb78d80c',
                    "size": '24160',
                    "url": 'https://openload.co/f/0yiQTPzi4Y4/favicons.zip'
                 }


def remote_upload(self, remote_url, folder_id=None, headers=None):

kwargs = {'<STR_LIT>': folder_id, '<STR_LIT>': headers}<EOL>params = {'<STR_LIT:url>': remote_url}<EOL>params.update({key: value for key, value in kwargs.items() if value})<EOL>return self._get('<STR_LIT>', params=params)<EOL>

Used to make a remote file upload to openload.co

        Note:
            If folder_id is not provided, the file will be uploaded to ``Home`` folder.

        Args:
            remote_url (str): direct link of file to be remotely downloaded.
            folder_id (:obj:`str`, optional): folder-ID to upload to.
            headers (:obj:`dict`, optional): additional HTTP headers (e.g. Cookies or HTTP Basic-Auth)

        Returns:
            dict: dictionary containing ("id": uploaded file id, "folderid"). ::

                {
                    "id": "12",
                    "folderid": "4248"
                }


def remote_upload_status(self, limit=None, remote_upload_id=None):

kwargs = {'<STR_LIT>': limit, '<STR_LIT:id>': remote_upload_id}<EOL>params = {key: value for key, value in kwargs.items() if value}<EOL>return self._get('<STR_LIT>', params=params)<EOL>

Checks a remote file upload to status.

        Args:
            limit (:obj:`int`, optional): Maximum number of results (Default: 5, Maximum: 100).
            remote_upload_id (:obj:`str`, optional): Remote Upload ID.

        Returns:
            dict: dictionary containing all remote uploads, each dictionary element is a dictionary. ::

                {
                    "24": {
                      "id": "24",
                      "remoteurl": "http://proof.ovh.net/files/100Mio.dat",
                      "status": "new",
                      "folderid": "4248",
                      "added": "2015-02-21 09:20:26",
                      "last_update": "2015-02-21 09:20:26",
                      "extid": False,
                      "url": False
                    },
                    "22": {
                      "id": "22",
                      "remoteurl": "http://proof.ovh.net/files/1Gio.dat",
                      "status": "downloading",
                      "bytes_loaded": "823997062",
                      "bytes_total": "1073741824",
                      "folderid": "4248",
                      "added": "2015-02-21 09:20:26",
                      "last_update": "2015-02-21 09:21:56",
                      "extid": False,
                      "url": False
                    },
                    ...
                }


params = {'<STR_LIT>': folder_id} if folder_id else {}<EOL>return self._get('<STR_LIT>', params=params)<EOL>

Request a list of files and folders in specified folder.

        Note:
            if folder_id is not provided, ``Home`` folder will be listed

        Args:
            folder_id (:obj:`str`, optional): id of the folder to be listed.

        Returns:
            dict: dictionary containing only two keys ("folders", "files"), \
                  each key represents a list of dictionaries. ::

                      {
                        "folders": [
                          {
                            "id": "5144",
                            "name": ".videothumb"
                          },
                          {
                            "id": "5792",
                            "name": ".subtitles"
                          },
                          ...
                        ],
                        "files": [
                          {
                            "name": "big_buck_bunny.mp4.mp4",
                            "sha1": "c6531f5ce9669d6547023d92aea4805b7c45d133",
                            "folderid": "4258",
                            "upload_at": "1419791256",
                            "status": "active",
                            "size": "5114011",
                            "content_type": "video/mp4",
                            "download_count": "48",
                            "cstatus": "ok",
                            "link": "https://openload.co/f/UPPjeAk--30/big_buck_bunny.mp4.mp4",
                            "linkextid": "UPPjeAk--30"
                          },
                          ...
                        ]
                      }


def rename_folder(self, folder_id, name):

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

Sets a new name for a folders

        Note:
            folder_id(s) can be found in list_folder return.

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

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


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.


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.


Converts previously uploaded files to a browser-streamable format (mp4 / h.264).

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

        Returns:
            bool: True if conversion started, otherwise False.


Shows running file converts by folder

        Note:
            If folder_id is not provided, ``Home`` folder will be used.

        Args:
            folder_id (:obj:`str`, optional): id of the folder to list conversions of files exist in it.

        Returns:
            list: list of dictionaries, each dictionary represents a file conversion info. ::

                      [
                        {
                          "name": "Geysir.AVI",
                          "id": "3565411",
                          "status": "pending",
                          "last_update": "2015-08-23 19:41:40",
                          "progress": 0.32,
                          "retries": "0",
                          "link": "https://openload.co/f/f02JFG293J8/Geysir.AVI",
                          "linkextid": "f02JFG293J8"
                        },
                        ....
                      ]


raise NotImplementedError<EOL>

Not yet implemented, openload.co said "Coming soon ...".

Raises:
    NotImplementedError


Shows the video splash image (thumbnail)

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

        Returns:
            str: url for the splash image.


import os<EOL>tp = os.path.split(__file__)[<NUM_LIT:0>] + '<STR_LIT:/>'<EOL>return tp<EOL>

Find where this test module is located so we can reference the fixtures
    folder.

    Named with tst to avoid Nose's pattern-matching.


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.


commit = {}<EOL>commit['<STR_LIT>'] = parts['<STR_LIT>']<EOL>commit['<STR_LIT>'] = parts['<STR_LIT>']<EOL>parent_block = parts['<STR_LIT>']<EOL>commit['<STR_LIT>'] = [<EOL>parse_parent_line(parentline)<EOL>for parentline in<EOL>parent_block.splitlines()<EOL>]<EOL>commit['<STR_LIT>'] = parse_author_line(parts['<STR_LIT>'])<EOL>commit['<STR_LIT>'] = parse_committer_line(parts['<STR_LIT>'])<EOL>message_lines = [<EOL>parse_message_line(msgline)<EOL>for msgline in<EOL>parts['<STR_LIT:message>'].split("<STR_LIT:\n>")<EOL>]<EOL>commit['<STR_LIT:message>'] = "<STR_LIT:\n>".join(<EOL>msgline<EOL>for msgline in<EOL>message_lines<EOL>if msgline is not None<EOL>)<EOL>commit['<STR_LIT>'] = [<EOL>parse_numstat_line(numstat)<EOL>for numstat in<EOL>parts['<STR_LIT>'].splitlines()<EOL>]<EOL>return commit<EOL>

Accept a parsed single commit. Some of the named groups
    require further processing, so parse those groups.
    Return a dictionary representing the completely parsed
    commit.


import subprocess<EOL>if git_dir:<EOL><INDENT>command = [<EOL>'<STR_LIT>',<EOL>'<STR_LIT>' + git_dir,<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>'<EOL>]<EOL><DEDENT>else:<EOL><INDENT>command = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL><DEDENT>if git_since is not None:<EOL><INDENT>command.append('<STR_LIT>' + git_since)<EOL><DEDENT>raw_git_log = subprocess.Popen(<EOL>command,<EOL>stdout=subprocess.PIPE<EOL>)<EOL>if sys.version_info < (<NUM_LIT:3>, <NUM_LIT:0>):<EOL><INDENT>return raw_git_log.stdout<EOL><DEDENT>else:<EOL><INDENT>return raw_git_log.stdout.read().decode('<STR_LIT:utf-8>', '<STR_LIT:ignore>')<EOL><DEDENT>

run_git_log([git_dir]) -> File

    Run `git log --numstat --pretty=raw` on the specified
    git repository and return its stdout as a pseudo-File.


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.


return name.lstrip('<STR_LIT:/>')<EOL>

Packages store items with names prefixed with slashes, but zip files
prefer them without. This method strips the leading slash.


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.


stream = io.BytesIO()<EOL>self._store(stream)<EOL>stream.seek(<NUM_LIT:0>)<EOL>return stream<EOL>

Return a zipped package as a readable stream


def _get_matching_segments(self, zf, name):

for n in zf.namelist():<EOL><INDENT>if n.startswith(name):<EOL><INDENT>yield zf.read(n)<EOL><DEDENT><DEDENT>

Return a generator yielding each of the segments who's names
match name.


assert part.name.startswith(self.base)<EOL>name = part.name[len(self.base):].lstrip('<STR_LIT:/>')<EOL>rel = Relationship(self, name, part.rel_type, id=id)<EOL>self.relationships.add(rel)<EOL>return rel<EOL>

Relate this package component to the supplied part.


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>self.relationships.load(source=self, data=source)<EOL>


return self.parts[name]<EOL>


ct_add_method = [<EOL>self.content_types.add_default,<EOL>self.content_types.add_override,<EOL>][override]<EOL>self[part.name] = part<EOL>ct_add_method(part)<EOL>

Add a part to the package.

        It will also add a content-type - by default an override.  If
        override is False then it will add a content-type for the extension
        if one isn't already present.


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

Load up the content_types object with value from source XML.


def _load_part(self, rel_type, name, data):

if self.content_types.find_for(name) is None:<EOL><INDENT>log.warning('<STR_LIT>' % vars())<EOL>return<EOL><DEDENT>cls = Part.classes_by_rel_type[rel_type]<EOL>part = cls(self, name)<EOL>part.load(data)<EOL>self[name] = part<EOL>return part<EOL>

Load a part into this package based on its relationship type


return (part for part in self.parts.values() if isinstance(part, cls))<EOL>

Return all parts of this package that are instances of cls
(where cls is passed directly to isinstance, so can be a class
or sequence of classes).


<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


return iter(self.data or [])<EOL>

Should return an iterator for the underlying content.


data = self.data<EOL>if isinstance(data, ElementClass):<EOL><INDENT>data = tostring(data, encoding='<STR_LIT:utf-8>', pretty_print=True)<EOL><DEDENT>if isinstance(data, six.text_type):<EOL><INDENT>return data.encode('<STR_LIT:utf-8>')<EOL><DEDENT>return data<EOL>


def __init__(self, package, source, encoding=None):

name = self._name_from_source(source)<EOL>Part.__init__(self, package, name)<EOL>self.ids = set()<EOL>self.children = set()<EOL>self.types = {}<EOL>self.encoding = encoding or '<STR_LIT:utf-8>'<EOL>

@param source package or part where from which the relationship is
       derived
@ptype source Package or Part


elem = fromstring(data)<EOL>for rel in elem:<EOL><INDENT>mode = rel.get('<STR_LIT>')<EOL>target = rel.get('<STR_LIT>')<EOL>rtype = rel.get('<STR_LIT>')<EOL>id = rel.get('<STR_LIT>')<EOL>relationship = Relationship(source, target, rtype, id, mode)<EOL>self.add(relationship)<EOL><DEDENT>

@param source The source Part for each relationship in this
              collection
@ptype source Part
@param data Relationship XML from a previous dump operation
@ptype data string


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


<EOL>ns, class_name = parse_tag(element.tag)<EOL>class_ = getattr(ContentType, class_name)<EOL>if not class_:<EOL><INDENT>msg = '<STR_LIT>' % vars()<EOL>raise ValueError(msg)<EOL><DEDENT>key = element.get(class_.key_name)<EOL>name = element.get('<STR_LIT>')<EOL>return class_(name, key)<EOL>

given an element, parse out the proper ContentType


attrs = '<STR_LIT>'.split()<EOL>all_attrs_eq = all(<EOL>getattr(self, attr, None) == getattr(other, attr, None)<EOL>for attr in attrs<EOL>)<EOL>return isinstance(other, self.__class__) and all_attrs_eq<EOL>

This object is equal to another if the other is of the
same class and the name and key match.


Edit a part from an OOXML Package without unzipping it


parser = argparse.ArgumentParser(description=inspect.getdoc(part_edit_cmd))<EOL>parser.add_argument(<EOL>'<STR_LIT:path>',<EOL>help=(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>),<EOL>)<EOL>args = parser.parse_args()<EOL>for item, is_file in sorted(list_contents(args.path)):<EOL><INDENT>prefix = '<STR_LIT>' if not is_file else '<STR_LIT:U+0020>'<EOL>msg = prefix + item<EOL>print(msg)<EOL><DEDENT>

List the contents of a subdirectory of a zipfile


drive, path = os.path.splitdrive(path)<EOL>head, tail = os.path.split(path)<EOL>terminators = [os.path.sep, os.path.altsep, '<STR_LIT>']<EOL>parts = split_all(head) if head not in terminators else [head]<EOL>return [drive] + parts + [tail]<EOL>

recursively call os.path.split until we have all of the components
of a pathname suitable for passing back to os.path.join.


path_components = split_all(path)<EOL>def get_assemblies():<EOL><INDENT>"""<STR_LIT>"""<EOL>for n in range(len(path_components), <NUM_LIT:0>, -<NUM_LIT:1>):<EOL><INDENT>file_c = path_components[:n]<EOL>part_c = path_components[n:] or ['<STR_LIT>']<EOL>yield (os.path.join(*file_c), posixpath.join(*part_c))<EOL><DEDENT><DEDENT>for file_path, part_path in get_assemblies():<EOL><INDENT>if os.path.isfile(file_path):<EOL><INDENT>return file_path, part_path<EOL><DEDENT><DEDENT>

Given a path to a part in a zip file, return a path to the file and
the path to the part.

Assuming /foo.zipx exists as a file,

>>> find_file('/foo.zipx/dir/part') # doctest: +SKIP
('/foo.zipx', '/dir/part')

>>> find_file('/foo.zipx') # doctest: +SKIP
('/foo.zipx', '')


default_editor = ['<STR_LIT>', '<STR_LIT>'][sys.platform.startswith('<STR_LIT:win32>')]<EOL>return os.environ.get(<EOL>'<STR_LIT>',<EOL>os.environ.get('<STR_LIT>', default_editor),<EOL>)<EOL>

Give preference to an XML_EDITOR or EDITOR defined in the
environment. Otherwise use notepad on Windows and edit on other
platforms.


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 cls._names<EOL>

Return a list of the names of all supported mass models

        >>> Table.names()
        ['AME2003', 'AME2003all', 'AME2012', 'AME2012all', 'AME1995',
        'AME1995all', 'DUZU', 'FRDM95', 'KTUY05', 'ETFSI12', 'HFB14',
        'HFB26', 'TCSM12', 'TCSM13', 'BR2013', 'MAJA88', 'GK88', 'WS32010', 'WS32011',
        'SVM13']


filename = os.path.join(package_dir, '<STR_LIT:data>', name + '<STR_LIT>')<EOL>return cls.from_file(filename, name)<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>


@classmethod<EOL><INDENT>def from_ZNM(cls, Z, N, M, name='<STR_LIT>'):<DEDENT>

df = pd.DataFrame.from_dict({'<STR_LIT>': Z, '<STR_LIT:N>': N, '<STR_LIT:M>': M}).set_index(['<STR_LIT>', '<STR_LIT:N>'])['<STR_LIT:M>']<EOL>df.name = name<EOL>return cls(df=df, name=name)<EOL>


with open(path, '<STR_LIT:w>') as f:<EOL><INDENT>f.write('<STR_LIT>')<EOL><DEDENT>self.df.to_csv(path, sep='<STR_LIT:\t>', mode='<STR_LIT:a>')<EOL>

Export the contents to a file as comma separated values.

        Parameters
        ----------
        path : string
            File path where the data should be saved to

        Example
        -------
        Export the last ten elements of AME2012 to a new file:

        >>> Table('AME2012').tail(10).to_file('last_ten.txt')


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.df.index.get_level_values('<STR_LIT:N>').values<EOL>

Return the neutron number N for all nuclei in the table as a numpy array.


return self.Z + self.N<EOL>

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


if isinstance(index, tuple) and len(index)==<NUM_LIT:2>:<EOL><INDENT>if isinstance(index[<NUM_LIT:0>], int):               <EOL><INDENT>startZ, stopZ = index[<NUM_LIT:0>], index[<NUM_LIT:0>]<EOL><DEDENT>if isinstance(index[<NUM_LIT:1>], int):<EOL><INDENT>startN, stopN = index[<NUM_LIT:1>], index[<NUM_LIT:1>]      <EOL><DEDENT>if isinstance(index[<NUM_LIT:0>], slice):             <EOL><INDENT>startZ, stopZ, stepZ = index[<NUM_LIT:0>].start, index[<NUM_LIT:0>].stop, index[<NUM_LIT:0>].step<EOL><DEDENT>if isinstance(index[<NUM_LIT:1>], slice):              <EOL><INDENT>startN, stopN, stepN = index[<NUM_LIT:1>].start, index[<NUM_LIT:1>].stop, index[<NUM_LIT:1>].step<EOL><DEDENT>if not startZ: startZ = self.Z.min()  <EOL>if not stopZ:  stopZ = self.Z.max()<EOL>if not startN: startN = self.N.min()<EOL>if not stopN:  stopN = self.N.max()<EOL>x = self.df.reset_index()<EOL>x = x.loc[(x.Z>=startZ)&(x.Z<=stopZ)&(x.N>=startN)&(x.N<=stopN)]<EOL>df = x.set_index(['<STR_LIT>', '<STR_LIT:N>']).sortlevel(<NUM_LIT:0>)<EOL>return Table(df=df[df.columns[<NUM_LIT:0>]], name=self.name)<EOL><DEDENT>


instance_method = getattr(self.df, attr)<EOL>if callable(instance_method):<EOL><INDENT>def fn(*args, **kwargs):<EOL><INDENT>result = instance_method(*args, **kwargs)   <EOL>if isinstance(result, (pd.DataFrame, pd.Series)):<EOL><INDENT>try:<EOL><INDENT>name = result.name<EOL><DEDENT>except AttributeError:<EOL><INDENT>name = None<EOL><DEDENT>return Table(name=name, df=result)  <EOL><DEDENT><DEDENT>return fn<EOL><DEDENT>else:<EOL><INDENT>return instance_method<EOL><DEDENT>

Pass properties and method calls to the DataFrame object


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)


index = pd.MultiIndex.from_tuples(nuclei, names=['<STR_LIT>', '<STR_LIT:N>'])<EOL>return Table(df=self.df.ix[index], name=self.name)<EOL>

Return a selection of the Table at positions given by ``nuclei``

        Parameters
        ----------
        nuclei: list of tuples
            A list where each element is tuple of the form (Z,N)

        Example
        -------
        Return binding energies at magic nuclei:

        >>> magic_nuclei = [(20,28), (50,50), (50,82), (82,126)]
        >>> Table('AME2012').binding_energy.at(magic_nuclei)
        Z   N
        20  28      416.014215
        50  50      825.325172
            82     1102.876416
        82  126    1636.486450


return len(self.df)<EOL>

Return the total number of nuclei:

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


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


idx = self.df.index & table.df.index<EOL>return Table(df=self.df[idx], name=self.name)<EOL>

Select nuclei which also belong to ``table``

Parameters
----------
table: Table, Table object

Example:
----------
Table('AME2003').intersection(Table('AME1995'))


idx = self.df.index - table.df.index<EOL>return Table(df=self.df[idx], name=self.name)<EOL>

Select nuclei not in table

Parameters
----------
table: Table, Table object from where nuclei should be removed

Example:
----------
Find the new nuclei in AME2003 with Z,N >= 8:

>>> Table('AME2003').not_in(Table('AME1995'))[8:,8:].count
389


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
        ...


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


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: not(Z % <NUM_LIT:2>) and not(N % <NUM_LIT:2>), name=self.name)<EOL>


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')


error = self.error(relative_to=relative_to)<EOL>return math.sqrt((error.df ** <NUM_LIT:2>).mean())<EOL>


M_P = <NUM_LIT><EOL>M_E = <NUM_LIT><EOL>M_N = <NUM_LIT><EOL>AMU = <NUM_LIT><EOL>df = self.Z * (M_P + M_E) + (self.A - self.Z) * M_N - (self.df + self.A * AMU)<EOL>return Table(df=df, name='<STR_LIT>' + '<STR_LIT:(>' + self.name + '<STR_LIT:)>')<EOL>

Return binding energies instead of mass excesses


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>


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


M_N = <NUM_LIT>         <EOL>f = lambda parent, daugther: -parent + daugther + <NUM_LIT:2> * M_N<EOL>return self.derived('<STR_LIT>', (<NUM_LIT:0>, -<NUM_LIT:2>), 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>


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>


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>


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>


idx = [(x[<NUM_LIT:0>] + <NUM_LIT:0>, x[<NUM_LIT:1>] + <NUM_LIT:2>) for x in self.df.index]<EOL>values = self.s2n.values - self.s2n.loc[idx].values<EOL>return Table(df=pd.Series(values, index=self.df.index, name='<STR_LIT>' + '<STR_LIT:(>' + self.name + '<STR_LIT:)>'))<EOL>

Calculates the derivative of the neutron separation energies:

        ds2n(Z,A) = s2n(Z,A) - s2n(Z,A+2)


idx = [(x[<NUM_LIT:0>] + <NUM_LIT:2>, x[<NUM_LIT:1>]) for x in self.df.index]<EOL>values = self.s2p.values - self.s2p.loc[idx].values<EOL>return Table(df=pd.Series(values, index=self.df.index, name='<STR_LIT>' + '<STR_LIT:(>' + self.name + '<STR_LIT:)>'))<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()


def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,<EOL>to_dir=os.curdir, delay=<NUM_LIT:15>):

<EOL>to_dir = os.path.abspath(to_dir)<EOL>try:<EOL><INDENT>from urllib.request import urlopen<EOL><DEDENT>except ImportError:<EOL><INDENT>from urllib2 import urlopen<EOL><DEDENT>tgz_name = "<STR_LIT>" % version<EOL>url = download_base + tgz_name<EOL>saveto = os.path.join(to_dir, tgz_name)<EOL>src = dst = None<EOL>if not os.path.exists(saveto):  <EOL><INDENT>try:<EOL><INDENT>log.warn("<STR_LIT>", url)<EOL>src = urlopen(url)<EOL>data = src.read()<EOL>dst = open(saveto, "<STR_LIT:wb>")<EOL>dst.write(data)<EOL><DEDENT>finally:<EOL><INDENT>if src:<EOL><INDENT>src.close()<EOL><DEDENT>if dst:<EOL><INDENT>dst.close()<EOL><DEDENT><DEDENT><DEDENT>return os.path.realpath(saveto)<EOL>

Download distribute from a specified location and return its filename

    `version` should be a valid distribute version number that is available
    as an egg for download under the `download_base` URL (which should end
    with a '/'). `to_dir` is the directory where the egg will be downloaded.
    `delay` is the number of seconds to pause before an actual download
    attempt.


existing_content = open(path).read()<EOL>if existing_content == content:<EOL><INDENT>log.warn('<STR_LIT>')<EOL>return False<EOL><DEDENT>log.warn('<STR_LIT>')<EOL>_rename_path(path)<EOL>f = open(path, '<STR_LIT:w>')<EOL>try:<EOL><INDENT>f.write(content)<EOL><DEDENT>finally:<EOL><INDENT>f.close()<EOL><DEDENT>return True<EOL>


def _extractall(self, path="<STR_LIT:.>", members=None):

import copy<EOL>import operator<EOL>from tarfile import ExtractError<EOL>directories = []<EOL>if members is None:<EOL><INDENT>members = self<EOL><DEDENT>for tarinfo in members:<EOL><INDENT>if tarinfo.isdir():<EOL><INDENT>directories.append(tarinfo)<EOL>tarinfo = copy.copy(tarinfo)<EOL>tarinfo.mode = <NUM_LIT> <EOL><DEDENT>self.extract(tarinfo, path)<EOL><DEDENT>if sys.version_info < (<NUM_LIT:2>, <NUM_LIT:4>):<EOL><INDENT>def sorter(dir1, dir2):<EOL><INDENT>return cmp(dir1.name, dir2.name)<EOL><DEDENT>directories.sort(sorter)<EOL>directories.reverse()<EOL><DEDENT>else:<EOL><INDENT>directories.sort(key=operator.attrgetter('<STR_LIT:name>'), reverse=True)<EOL><DEDENT>for tarinfo in directories:<EOL><INDENT>dirpath = os.path.join(path, tarinfo.name)<EOL>try:<EOL><INDENT>self.chown(tarinfo, dirpath)<EOL>self.utime(tarinfo, dirpath)<EOL>self.chmod(tarinfo, dirpath)<EOL><DEDENT>except ExtractError:<EOL><INDENT>e = sys.exc_info()[<NUM_LIT:1>]<EOL>if self.errorlevel > <NUM_LIT:1>:<EOL><INDENT>raise<EOL><DEDENT>else:<EOL><INDENT>self._dbg(<NUM_LIT:1>, "<STR_LIT>" % e)<EOL><DEDENT><DEDENT><DEDENT>

Extract all members from the archive to the current working
       directory and set owner, modification time and permissions on
       directories afterwards. `path' specifies a different directory
       to extract to. `members' is optional and must be a subset of the
       list returned by getmembers().


tarball = download_setuptools()<EOL>_install(tarball)<EOL>

Install or upgrade setuptools and EasyInstall


w = Parallel(n_jobs=_CPUS)<EOL>res = w(delayed(images.translation)(img) for img in images)<EOL>for i,translation in enumerate(res):<EOL><INDENT>images[i].translation = translation<EOL><DEDENT>return np.array(res)<EOL>

Calculate image translations in parallel.

    Parameters
    ----------
    images : ImageCollection
        Images as instance of ImageCollection.

    Returns
    -------
    2d array, (ty, tx)
        ty and tx is translation to previous image in respectively
        x or y direction.


if type(images) != ImageCollection:<EOL><INDENT>images = ImageCollection(images)<EOL><DEDENT>calc_translations_parallel(images)<EOL>_translation_warn(images)<EOL>yoffset, xoffset = images.median_translation()<EOL>if xoffset != yoffset:<EOL><INDENT>warn('<STR_LIT>' % (yoffset, xoffset))<EOL><DEDENT>y, x = imread(images[<NUM_LIT:0>].path).shape<EOL>height = y*len(images.rows) + yoffset*(len(images.rows)-<NUM_LIT:1>)<EOL>width = x*len(images.cols) + xoffset*(len(images.cols)-<NUM_LIT:1>)<EOL>merged = np.zeros((height, width, <NUM_LIT:2>), dtype=np.int)<EOL>for image in images:<EOL><INDENT>r, c = image.row, image.col<EOL>mask = _merge_slice(r, c, y, x, yoffset, xoffset)<EOL>img = _add_ones_dim(imread(image.path))<EOL>merged[mask] += img<EOL><DEDENT>merged[..., <NUM_LIT:0>] /= merged[..., <NUM_LIT:1>]<EOL>return merged[..., <NUM_LIT:0>].astype(np.uint8), (yoffset, xoffset)<EOL>

Stitch regular spaced images.

    Parameters
    ----------
    images : ImageCollection or list of tuple(path, row, column)
        Each image-tuple should contain path, row and column. Row 0,
        column 0 is top left image.

        Example:
        >>> images = [('1.png', 0, 0), ('2.png', 0, 1)]

    Returns
    -------
    tuple (stitched, offset)
        Stitched image and registered offset (y, x).


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


<EOL>pass<EOL>


def ratio(a, b):<EOL><INDENT>"""<STR_LIT>"""<EOL>return SequenceMatcher(None, a, b).ratio()<EOL><DEDENT>def scan_corpus(step):<EOL><INDENT>"""<STR_LIT>"""<EOL>match_values = []<EOL>m = <NUM_LIT:0><EOL>while m + qlen - step <= len(corpus):<EOL><INDENT>match_values.append(ratio(query, corpus[m : m-<NUM_LIT:1>+qlen]))<EOL>m += step<EOL><DEDENT>return match_values<EOL><DEDENT>def index_max(v):<EOL><INDENT>"""<STR_LIT>"""<EOL>return max(range(len(v)), key=v.__getitem__)<EOL><DEDENT>def adjust_left_right_positions():<EOL><INDENT>"""<STR_LIT>"""<EOL>p_l, bp_l = [pos] * <NUM_LIT:2><EOL>p_r, bp_r = [pos + qlen] * <NUM_LIT:2><EOL>bmv_l = match_values[round_decimal(p_l / step)]<EOL>bmv_r = match_values[round_decimal(p_r / step)]<EOL>for f in range(flex):<EOL><INDENT>ll = ratio(query, corpus[p_l - f: p_r])<EOL>if ll > bmv_l:<EOL><INDENT>bmv_l = ll<EOL>bp_l = p_l - f<EOL><DEDENT>lr = ratio(query, corpus[p_l + f: p_r])<EOL>if lr > bmv_l:<EOL><INDENT>bmv_l = lr<EOL>bp_l = p_l + f<EOL><DEDENT>rl = ratio(query, corpus[p_l: p_r - f])<EOL>if rl > bmv_r:<EOL><INDENT>bmv_r = rl<EOL>bp_r = p_r - f<EOL><DEDENT>rr = ratio(query, corpus[p_l: p_r + f])<EOL>if rr > bmv_r:<EOL><INDENT>bmv_r = rr<EOL>bp_r = p_r + f<EOL><DEDENT><DEDENT>return bp_l, bp_r, ratio(query, corpus[bp_l : bp_r])<EOL><DEDENT>if not case_sensitive:<EOL><INDENT>query = query.lower()<EOL>corpus = corpus.lower()<EOL><DEDENT>qlen = len(query)<EOL>if flex >= qlen/<NUM_LIT:2>:<EOL><INDENT>print("<STR_LIT>")<EOL>flex = <NUM_LIT:3><EOL><DEDENT>match_values = scan_corpus(step)<EOL>pos = index_max(match_values) * step<EOL>pos_left, pos_right, match_value = adjust_left_right_positions()<EOL>return corpus[pos_left: pos_right].strip(), match_value<EOL>

Return best matching substring of corpus.

    Parameters
    ----------
    query : str
    corpus : str
    step : int
        Step size of first match-value scan through corpus. Can be thought of
        as a sort of "scan resolution". Should not exceed length of query.
    flex : int
        Max. left/right substring position adjustment value. Should not
        exceed length of query / 2.

    Outputs
    -------
    output0 : str
        Best matching substring.
    output1 : float
        Match ratio of best matching substring. 1 is perfect match.


@command(aliases='<STR_LIT>')<EOL>def pm(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>if random.random() > <NUM_LIT>:<EOL><INDENT>return f"<STR_LIT>"<EOL><DEDENT>return f"<STR_LIT>"<EOL>


@command(aliases='<STR_LIT>')<EOL>def lm(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>


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


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

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


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

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


@command()<EOL>def schneier(client, event, channel, nick, rest):

rcpt = rest.strip() or channel<EOL>if rest.strip():<EOL><INDENT>Karma.store.change(rcpt, <NUM_LIT:2>)<EOL><DEDENT>url = '<STR_LIT>'<EOL>d = requests.get(url).text<EOL>start_tag = re.escape('<STR_LIT>')<EOL>end_tag = re.escape('<STR_LIT>')<EOL>p = re.compile(<EOL>start_tag + '<STR_LIT>' + end_tag,<EOL>flags=re.DOTALL | re.MULTILINE)<EOL>match = p.search(d)<EOL>if not match:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>phrase = match.group(<NUM_LIT:1>).replace('<STR_LIT:\n>', '<STR_LIT:U+0020>').strip()<EOL>if rcpt != channel:<EOL><INDENT>phrase = re.sub('<STR_LIT>', rcpt, phrase, flags=re.I)<EOL>phrase = re.sub('<STR_LIT>', rcpt, phrase, flags=re.I)<EOL><DEDENT>h = html.parser.HTMLParser()<EOL>phrase = h.unescape(phrase)<EOL>phrase = ftfy.fix_encoding(phrase)<EOL>return phrase<EOL>


result = []<EOL>for (field, values) in query.items():<EOL><INDENT>field = SEARCH_FIELD_MAP.get(field, field)<EOL>if field is None:<EOL><INDENT>continue<EOL><DEDENT>for value in values:<EOL><INDENT>if field == '<STR_LIT>':<EOL><INDENT>value = _transform_year(value)<EOL>if value is not None:<EOL><INDENT>result.append('<STR_LIT>' % (field, value))<EOL><DEDENT><DEDENT>elif field == '<STR_LIT>':<EOL><INDENT>result.append('<STR_LIT>' % value)<EOL><DEDENT>else:<EOL><INDENT>result.append('<STR_LIT>' % (field, value))<EOL><DEDENT><DEDENT><DEDENT>return '<STR_LIT:U+0020>'.join(result)<EOL>

Translate a Mopidy search query to a Spotify search query


value = response.headers.get('<STR_LIT>')<EOL>if not value:<EOL><INDENT>seconds = <NUM_LIT:0><EOL><DEDENT>elif re.match(r'<STR_LIT>', value):<EOL><INDENT>seconds = int(value)<EOL><DEDENT>else:<EOL><INDENT>date_tuple = email.utils.parsedate(value)<EOL>if date_tuple is None:<EOL><INDENT>seconds = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>seconds = time.mktime(date_tuple) - time.time()<EOL><DEDENT><DEDENT>return max(<NUM_LIT:0>, seconds)<EOL>

Parse Retry-After header from response if it is set.


def __init__(self, selector, comparison=None, argument=None):

super(Constraint, self).__init__()<EOL>self.selector = selector<EOL>if comparison and COMPARISON_COMP.match(comparison) is None:<EOL><INDENT>raise FiqlObjectException(<EOL>"<STR_LIT>" % comparison)<EOL><DEDENT>self.comparison = comparison<EOL>self.argument = argument<EOL>

Initialize instance of ``Constraint``.

        Args:
            selector (string): URL decoded constraint ``selector``.
            comparison (string, optional): Parsed/mapped ``comparison``
                operator. Defaults to ``None``.
            argument (string, optional): URL decoded constraint ``argument``.
                Defaults to ``None``.

        Raises:
            FiqlObjectException: Not a valid FIQL comparison.


return Expression().op_and(self, *elements)<EOL>

Create an ``Expression`` using this ``Constraint`` and the specified
        additional ``elements`` joined using an "AND" ``Operator``

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

        Returns:
            Expression: Newly created ``Expression`` including this
                ``Constraint``, the elements passed in, and the "AND"
                ``Operator``.


return Expression().op_or(self, *elements)<EOL>

Create an ``Expression`` using this ``Constraint`` and the specified
        additional ``elements`` joined using an "OR" ``Operator``

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

        Returns:
            Expression: Newly created ``Expression`` including this
                ``Constraint``, the elements passed in, and the "OR"
                ``Operator``.


return (<EOL>self.selector,<EOL>COMPARISON_MAP.get(self.comparison, self.comparison),<EOL>self.argument<EOL>)<EOL>

Deconstruct the ``Constraint`` instance to a tuple.

        Returns:
            tuple: The deconstructed ``Constraint``.


if self.argument:<EOL><INDENT>return "<STR_LIT>".format(quote_plus(self.selector),<EOL>self.comparison,<EOL>quote_plus(self.argument))<EOL><DEDENT>return self.selector<EOL>

Represent the ``Constraint`` instance as a string.

        Returns:
            string: The represented ``Constraint``.


while len(fiql_str):<EOL><INDENT>constraint_match = CONSTRAINT_COMP.split(fiql_str, <NUM_LIT:1>)<EOL>if len(constraint_match) < <NUM_LIT:2>:<EOL><INDENT>yield (constraint_match[<NUM_LIT:0>], None, None, None)<EOL>break<EOL><DEDENT>yield (<EOL>constraint_match[<NUM_LIT:0>],<EOL>unquote_plus(constraint_match[<NUM_LIT:1>]),<EOL>constraint_match[<NUM_LIT:4>],<EOL>unquote_plus(constraint_match[<NUM_LIT:6>])if constraint_match[<NUM_LIT:6>] else None<EOL>)<EOL>fiql_str = constraint_match[<NUM_LIT:8>]<EOL><DEDENT>

Iterate through the FIQL string. Yield a tuple containing the
    following FIQL components for each iteration:

      - preamble: Any operator or opening/closing paranthesis preceding a
        constraint or at the very end of the FIQL string.
      - selector: The selector portion of a FIQL constraint or ``None`` if
        yielding the last portion of the string.
      - comparison: The comparison portion of a FIQL constraint or ``None``
        if yielding the last portion of the string.
      - argument: The argument portion of a FIQL constraint or ``None`` if
        yielding the last portion of the string.

    For usage see :func:`parse_str_to_expression`.

    Args:
        fiql_str (string): The FIQL formatted string we want to parse.

    Yields:
        tuple: Preamble, selector, comparison, argument.


<EOL>nesting_lvl = <NUM_LIT:0><EOL>last_element = None<EOL>expression = Expression()<EOL>for (preamble, selector, comparison, argument) in iter_parse(fiql_str):<EOL><INDENT>if preamble:<EOL><INDENT>for char in preamble:<EOL><INDENT>if char == '<STR_LIT:(>':<EOL><INDENT>if isinstance(last_element, BaseExpression):<EOL><INDENT>raise FiqlFormatException(<EOL>"<STR_LIT>" % (<EOL>last_element.__class__, Expression))<EOL><DEDENT>expression = expression.create_nested_expression()<EOL>nesting_lvl += <NUM_LIT:1><EOL><DEDENT>elif char == '<STR_LIT:)>':<EOL><INDENT>expression = expression.get_parent()<EOL>last_element = expression<EOL>nesting_lvl -= <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>if not expression.has_constraint():<EOL><INDENT>raise FiqlFormatException(<EOL>"<STR_LIT>" % (<EOL>Operator, Constraint))<EOL><DEDENT>if isinstance(last_element, Operator):<EOL><INDENT>raise FiqlFormatException(<EOL>"<STR_LIT>" % (<EOL>Operator, Operator))<EOL><DEDENT>last_element = Operator(char)<EOL>expression = expression.add_operator(last_element)<EOL><DEDENT><DEDENT><DEDENT>if selector:<EOL><INDENT>if isinstance(last_element, BaseExpression):<EOL><INDENT>raise FiqlFormatException("<STR_LIT>" % (<EOL>last_element.__class__, Constraint))<EOL><DEDENT>last_element = Constraint(selector, comparison, argument)<EOL>expression.add_element(last_element)<EOL><DEDENT><DEDENT>if nesting_lvl != <NUM_LIT:0>:<EOL><INDENT>raise FiqlFormatException(<EOL>"<STR_LIT>")<EOL><DEDENT>if not expression.has_constraint():<EOL><INDENT>raise FiqlFormatException(<EOL>"<STR_LIT>" % fiql_str)<EOL><DEDENT>return expression<EOL>

Parse a FIQL formatted string into an ``Expression``.

    Args:
        fiql_str (string): The FIQL formatted string we want to parse.

    Returns:
        Expression: An ``Expression`` object representing the parsed FIQL
        string.

    Raises:
        FiqlFormatException: Unable to parse string due to incorrect
            formatting.

    Example:

        >>> expression = parse_str_to_expression(
        ...         "name==bar,dob=gt=1990-01-01")


self.parent = None<EOL>

Initialize instance of ``BaseExpression``.


if not isinstance(parent, Expression):<EOL><INDENT>raise FiqlObjectException("<STR_LIT>" % (<EOL>Expression, type(parent)))<EOL><DEDENT>self.parent = parent<EOL>

Set parent ``Expression`` for this object.

        Args:
            parent (Expression): The ``Expression`` which contains this object.

        Raises:
            FiqlObjectException: Parent must be of type ``Expression``.


if not isinstance(self.parent, Expression):<EOL><INDENT>raise FiqlObjectException("<STR_LIT>" % (<EOL>Expression, type(self.parent)))<EOL><DEDENT>return self.parent<EOL>

Get the parent ``Expression`` for this object.

        Returns:
            Expression: The ``Expression`` which contains this object.

        Raises:
            FiqlObjectException: Parent is ``None``.


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


return len(self.elements)<EOL>

Return whether or not the working ``Expression`` has any
        ``Constraints``.

        Returns:
            integer: Number of logical elements within this ``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``.


if isinstance(element, BaseExpression):<EOL><INDENT>element.set_parent(self._working_fragment)<EOL>self._working_fragment.elements.append(element)<EOL>return self<EOL><DEDENT>else:<EOL><INDENT>return self.add_operator(element)<EOL><DEDENT>

Add an element of type ``Operator``, ``Constraint``, or
        ``Expression`` to the ``Expression``.

        Args:
            element: ``Constraint``, ``Expression``, or ``Operator``.

        Returns:
            Expression: ``self``

        Raises:
            FiqlObjectException: Element is not a valid type.


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``.


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``.


expression = self.add_operator(Operator('<STR_LIT:U+002C>'))<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 "OR" ``Operator``

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

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


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``.


operator = self.operator or Operator('<STR_LIT:;>')<EOL>elements_str = str(operator).join(<EOL>["<STR_LIT>".format(elem) for elem in self.elements])<EOL>if self.parent:<EOL><INDENT>parent_operator = self.parent.operator or Operator('<STR_LIT:;>')<EOL>if parent_operator > operator:<EOL><INDENT>return "<STR_LIT:(>" + elements_str + "<STR_LIT:)>"<EOL><DEDENT><DEDENT>return elements_str<EOL>

Represent the ``Expression`` instance as a string.

        Returns:
            string: The represented ``Expression``.


if not fiql_op_str in OPERATOR_MAP:<EOL><INDENT>raise FiqlObjectException(<EOL>"<STR_LIT>" % fiql_op_str)<EOL><DEDENT>self.value = fiql_op_str<EOL>

Initialize instance of ``Operator``.

        Args:
            fiql_op_str (string): The FIQL operator (e.g., ";").

        Raises:
            FiqlObjectException: Invalid FIQL operator.


return OPERATOR_MAP[self.value][<NUM_LIT:0>]<EOL>

Deconstruct the ``Operator`` instance to a string.

        Returns:
            string: The deconstructed ``Operator``.


return self.value<EOL>

Represent the ``Operator`` instance as a string.

        Returns:
            string: The represented ``Operator``.


prec_self = OPERATOR_MAP[self.value][<NUM_LIT:1>]<EOL>prec_other = OPERATOR_MAP[other.value][<NUM_LIT:1>]<EOL>if prec_self < prec_other:<EOL><INDENT>return -<NUM_LIT:1><EOL><DEDENT>if prec_self > prec_other:<EOL><INDENT>return <NUM_LIT:1><EOL><DEDENT>return <NUM_LIT:0><EOL>

Compare using operator precedence.

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

        Returns:
            integer: ``1`` if greater than ``other``, ``-1`` if less than
            ``other``, and ``0`` if of equal precedence of ``other``.


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``.


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

Of less than precedence.

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

        Returns:
            boolean: ``True`` if of less than precendence of ``other``.


def login(request, browser, app, attempt=<NUM_LIT:1>):

success = False<EOL>provider_name, provider = request.param<EOL>log(<NUM_LIT:1>, provider_name, '<STR_LIT>'.format(attempt))<EOL>def wait(indent, seconds):<EOL><INDENT>seconds = seconds or <NUM_LIT:0><EOL>seconds = seconds * config.WAIT_MULTIPLIER<EOL>if seconds < config.MIN_WAIT:<EOL><INDENT>seconds = config.MIN_WAIT<EOL><DEDENT>if seconds:<EOL><INDENT>log(indent, provider_name,<EOL>u'<STR_LIT>'.format(seconds))<EOL>time.sleep(seconds)<EOL><DEDENT><DEDENT>def human_interaction_needed(xpath, seconds=<NUM_LIT:0>):<EOL><INDENT>log(<NUM_LIT:2>, provider_name, '<STR_LIT>')<EOL>try:<EOL><INDENT>wait(<NUM_LIT:2>, seconds)<EOL>el = browser.find_element_by_xpath(xpath)<EOL>if el.is_displayed():<EOL><INDENT>print('<STR_LIT>')<EOL>print('<STR_LIT>')<EOL>if os.environ.get('<STR_LIT>'):<EOL><INDENT>message = ('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>log(<NUM_LIT:3>, provider_name, message)<EOL>pytest.fail(message)<EOL>return<EOL><DEDENT>log(<NUM_LIT:3>, provider_name, '<STR_LIT>')<EOL>import pdb<EOL>pdb.set_trace()<EOL>log(<NUM_LIT:3>, provider_name, '<STR_LIT>')<EOL>return<EOL><DEDENT><DEDENT>except NoSuchElementException:<EOL><INDENT>pass<EOL><DEDENT>log(<NUM_LIT:3>, provider_name, '<STR_LIT>')<EOL><DEDENT>try:<EOL><INDENT>provider['<STR_LIT:name>'] = provider_name<EOL>conf = fixtures.get_configuration(provider_name)<EOL>url = parse.urljoin(app.check_url, '<STR_LIT>' + provider['<STR_LIT>'])<EOL>login_url = provider.get('<STR_LIT>')<EOL>login_xpath = provider.get('<STR_LIT>')<EOL>password_xpath = provider.get('<STR_LIT>')<EOL>pre_login_xpaths = provider.get('<STR_LIT>')<EOL>if login_url:<EOL><INDENT>log(<NUM_LIT:2>, provider_name, '<STR_LIT>'.format(login_url))<EOL>browser.get(login_url)<EOL><DEDENT>else:<EOL><INDENT>browser.get(url)<EOL><DEDENT>try:<EOL><INDENT>alert_wait = provider.get('<STR_LIT>', <NUM_LIT:0>)<EOL>WebDriverWait(browser, alert_wait).until(expected_conditions.alert_is_present())<EOL>if alert_wait:<EOL><INDENT>log(<NUM_LIT:2>, provider_name, '<STR_LIT>'<EOL>.format(alert_wait))<EOL><DEDENT>alert = browser.switch_to_alert()<EOL>log(<NUM_LIT:2>, provider_name, '<STR_LIT>'.format(alert.text))<EOL>alert.accept()<EOL><DEDENT>except TimeoutException:<EOL><INDENT>pass<EOL><DEDENT>if request.config.getoption("<STR_LIT>"):<EOL><INDENT>log(<NUM_LIT:2>, provider_name, '<STR_LIT>')<EOL>import pdb<EOL>pdb.set_trace()<EOL><DEDENT>if login_xpath:<EOL><INDENT>if pre_login_xpaths:<EOL><INDENT>for xpath in pre_login_xpaths:<EOL><INDENT>log(<NUM_LIT:2>, provider_name,<EOL>'<STR_LIT>'.format(xpath))<EOL>pre_login = browser.find_element_by_xpath(xpath)<EOL>log(<NUM_LIT:3>, provider_name,<EOL>'<STR_LIT>'.format(xpath))<EOL>pre_login.click()<EOL><DEDENT><DEDENT>log(<NUM_LIT:2>, provider_name,<EOL>'<STR_LIT>'.format(login_xpath))<EOL>login_element = browser.find_element_by_xpath(login_xpath)<EOL>log(<NUM_LIT:3>, provider_name, '<STR_LIT>')<EOL>login_element.send_keys(conf.user_login)<EOL>enter_after_login_input = provider.get('<STR_LIT>')<EOL>if enter_after_login_input:<EOL><INDENT>log(<NUM_LIT:3>, provider_name, '<STR_LIT>')<EOL>login_element.send_keys(Keys.ENTER)<EOL><DEDENT>hi = provider.get('<STR_LIT>')<EOL>if hi:<EOL><INDENT>human_interaction_needed(*hi)<EOL><DEDENT>wait(<NUM_LIT:2>, provider.get('<STR_LIT>'))<EOL>log(<NUM_LIT:2>, provider_name,<EOL>'<STR_LIT>'.format(password_xpath))<EOL>password_element = browser.find_element_by_xpath(password_xpath)<EOL>log(<NUM_LIT:3>, provider_name, '<STR_LIT>')<EOL>password_element.send_keys(conf.user_password)<EOL>wait(<NUM_LIT:2>, provider.get('<STR_LIT>'))<EOL>log(<NUM_LIT:2>, provider_name, '<STR_LIT>')<EOL>password_element.send_keys(Keys.ENTER)<EOL>wait(<NUM_LIT:2>, provider.get('<STR_LIT>'))<EOL><DEDENT>after_login_hook = provider.get('<STR_LIT>')<EOL>if after_login_hook:<EOL><INDENT>log(<NUM_LIT:3>, provider_name, '<STR_LIT>')<EOL>after_login_hook(browser, log)<EOL><DEDENT>if login_url:<EOL><INDENT>log(<NUM_LIT:2>, provider_name, '<STR_LIT>'<EOL>.format(url))<EOL>browser.get(url)<EOL><DEDENT>consent_xpaths = provider.get('<STR_LIT>')<EOL>if consent_xpaths:<EOL><INDENT>for xpath in consent_xpaths:<EOL><INDENT>try:<EOL><INDENT>wait(<NUM_LIT:2>, provider.get('<STR_LIT>'))<EOL>log(<NUM_LIT:2>, provider_name,<EOL>'<STR_LIT>'.format(xpath))<EOL>button = browser.find_element_by_xpath(xpath)<EOL>log(<NUM_LIT:3>, provider_name, '<STR_LIT>')<EOL>button.click()<EOL><DEDENT>except NoSuchElementException as e:<EOL><INDENT>log(<NUM_LIT:3>, provider_name,<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT><DEDENT>wait(<NUM_LIT:2>, provider.get('<STR_LIT>'))<EOL>try:<EOL><INDENT>log(<NUM_LIT:2>, provider_name, '<STR_LIT>')<EOL>browser.find_element_by_id('<STR_LIT>')<EOL>log(<NUM_LIT:3>, provider_name, '<STR_LIT>')<EOL>success = True<EOL><DEDENT>except NoSuchElementException:<EOL><INDENT>log(<NUM_LIT:3>, provider_name, '<STR_LIT>')<EOL><DEDENT><DEDENT>except WebDriverException as e:<EOL><INDENT>if request.config.getoption('<STR_LIT>'):<EOL><INDENT>log(<NUM_LIT:2>, provider_name, '<STR_LIT>')<EOL>import pdb<EOL>pdb.set_trace()<EOL><DEDENT>try:<EOL><INDENT>log(<NUM_LIT:2>, provider_name,<EOL>'<STR_LIT>'.format(e.msg))<EOL>browser.find_element_by_id('<STR_LIT>')<EOL>log(<NUM_LIT:3>, provider_name, '<STR_LIT>')<EOL>success = True<EOL><DEDENT>except NoSuchElementException:<EOL><INDENT>log(<NUM_LIT:3>, provider_name, '<STR_LIT>')<EOL><DEDENT><DEDENT>if success:<EOL><INDENT>log(<NUM_LIT:0>, provider_name, '<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>if attempt < config.MAX_LOGIN_ATTEMPTS:<EOL><INDENT>login(request, browser, app, attempt + <NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>log(<NUM_LIT:1>, provider_name,<EOL>'<STR_LIT>'.format(attempt))<EOL>pytest.fail(<EOL>'<STR_LIT>'.format(provider_name))<EOL><DEDENT><DEDENT>return provider<EOL>


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>conf = {}<EOL>conf.update(config.COMMON)<EOL>try:<EOL><INDENT>conf.update(config.PROVIDERS[provider])<EOL><DEDENT>except KeyError:<EOL><INDENT>raise Exception('<STR_LIT>'<EOL>'<STR_LIT>'.format(provider))<EOL><DEDENT>class_name = '<STR_LIT>'.format(provider.capitalize())<EOL>Res = namedtuple(class_name, sorted(conf.keys()))<EOL>Res.BIRTH_DATE_FORMAT = BIRTH_DATE_FORMAT<EOL>bday = conf['<STR_LIT>']<EOL>Res.user_birth_date_str = bday.strftime(Res.BIRTH_DATE_FORMAT)<EOL>Res.no_birth_date = ['<STR_LIT>']<EOL>Res.no_birth_year = [conf['<STR_LIT>'], '<STR_LIT>', '<STR_LIT>']<EOL>Res.no_birth_month = [conf['<STR_LIT>'], '<STR_LIT>', '<STR_LIT>']<EOL>Res.no_birth_day = [conf['<STR_LIT>'], '<STR_LIT>', '<STR_LIT>']<EOL>Res.no_city = [conf['<STR_LIT>'], '<STR_LIT>']<EOL>Res.no_country = [conf['<STR_LIT>'], '<STR_LIT>']<EOL>Res.no_email = [conf['<STR_LIT>'], '<STR_LIT:email>']<EOL>Res.no_first_name = ['<STR_LIT>'.format(conf['<STR_LIT>']), '<STR_LIT>']<EOL>Res.no_last_name = ['<STR_LIT>'.format(conf['<STR_LIT>']), '<STR_LIT>']<EOL>Res.no_gender = [conf['<STR_LIT>'], '<STR_LIT>']<EOL>Res.no_link = ['<STR_LIT>']<EOL>Res.no_locale = [conf['<STR_LIT>'], '<STR_LIT>', '<STR_LIT>']<EOL>Res.no_nickname = ['<STR_LIT>', conf['<STR_LIT>']]<EOL>Res.no_phone = [conf['<STR_LIT>'], '<STR_LIT>']<EOL>Res.no_picture = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT:image>']<EOL>Res.no_postal_code = [conf['<STR_LIT>'], '<STR_LIT>', '<STR_LIT>']<EOL>Res.no_timezone = ['<STR_LIT>']<EOL>Res.no_username = ['<STR_LIT:username>', '<STR_LIT>'.format(conf['<STR_LIT>'])]<EOL>Res.no_location = [<EOL>conf['<STR_LIT>'],<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT:location>'<EOL>] + Res.no_postal_code + Res.no_city<EOL>return Res(**conf)<EOL>

Creates the user configuration which holds the tested values.

It merges the ``config.COMMON`` and the ``config.PROVIDERS[provider]``
dictionaries and returns a named tuple.

:param str provider:
    Provider slug used in ``config.PROVIDERS``.

:returns:

    A named tuple.


func.__doc__ = doc<EOL>


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

Import module, returning the module after the last dot.


setattr(_MovedItems, move.name, move)<EOL>


try:<EOL><INDENT>delattr(_MovedItems, name)<EOL><DEDENT>except AttributeError:<EOL><INDENT>try:<EOL><INDENT>del moves.__dict__[name]<EOL><DEDENT>except KeyError:<EOL><INDENT>raise AttributeError("<STR_LIT>" % (name,))<EOL><DEDENT><DEDENT>


<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>


def wrapper(cls):<EOL><INDENT>orig_vars = cls.__dict__.copy()<EOL>slots = orig_vars.get('<STR_LIT>')<EOL>if slots is not None:<EOL><INDENT>if isinstance(slots, str):<EOL><INDENT>slots = [slots]<EOL><DEDENT>for slots_var in slots:<EOL><INDENT>orig_vars.pop(slots_var)<EOL><DEDENT><DEDENT>orig_vars.pop('<STR_LIT>', None)<EOL>orig_vars.pop('<STR_LIT>', None)<EOL>return metaclass(cls.__name__, cls.__bases__, orig_vars)<EOL><DEDENT>return wrapper<EOL>

Class decorator for creating a class with a metaclass.


if PY2:<EOL><INDENT>if '<STR_LIT>' not in klass.__dict__:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>" %<EOL>klass.__name__)<EOL><DEDENT>klass.__unicode__ = klass.__str__<EOL>klass.__str__ = lambda self: self.__unicode__().encode('<STR_LIT:utf-8>')<EOL><DEDENT>return klass<EOL>

A decorator that defines __unicode__ and __str__ methods under Python 2.
Under Python 3 it does nothing.

To support Python 2 and 3 with a single code base, define a __str__ method
returning text and apply this decorator to the class.


return hasattr(self.__get_module(fullname), "<STR_LIT>")<EOL>

Return true, if the named module is a package.

We need this method to get correct spec objects with
Python 3.4 (see PEP451)


self.__get_module(fullname)  <EOL>return None<EOL>

Return None

        Required, if is_package is implemented


Must return a :class:`dict` of all request parameters of any HTTP
method.

:returns:
    :class:`dict`


Must return the url of the actual request including path but without
query and fragment.

:returns:
    :class:`str`


Must return cookies as a :class:`dict`.

:returns:
    :class:`dict`


Must write specified value to response.

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


@abc.abstractmethod<EOL><INDENT>def set_header(self, key, value):<DEDENT>

Must set response headers to ``Key: value``.

:param str key:
    Header name.

:param str value:
    Header value.


Must set the response status e.g. ``'302 Found'``.

:param str status:
    The HTTP response status.


self.request = request<EOL>self.response = response<EOL>

:param request:
    An instance of the :class:`django.http.HttpRequest` class.

:param response:
    An instance of the :class:`django.http.HttpResponse` class.


:param request:
    A |webob|_ :class:`Request` instance.

:param response:
    A |webob|_ :class:`Response` instance.


self.request = handler.request<EOL>self.response = handler.response<EOL>

:param handler:
    A :class:`webapp2.RequestHandler` instance.


:param request:
    Instance of the :class:`werkzeug.wrappers.Request` class.

:param response:
    Instance of the :class:`werkzeug.wrappers.Response` class.


Called only once per request.

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


@abc.abstractmethod<EOL><INDENT>def __setitem__(self, key, value):<DEDENT>


NDBConfig.initialize()<EOL>return NDBConfig<EOL>

Allows you to have a **datastore** :doc:`config` instead of a hardcoded
one.

This function 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* of the |gae|_ 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 properties set) in the datastore.

:raises:
    :exc:`.GAEError`

:returns:
    :class:`.NDBConfig`


def __init__(self, handler, session=None, secret=None,<EOL>cookie_name='<STR_LIT>', backend='<STR_LIT>',<EOL>config=None):

self.handler = handler<EOL>if session is None:<EOL><INDENT>if not secret:<EOL><INDENT>raise GAEError('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>cfg = config or dict(<EOL>secret_key=secret, cookie_name=cookie_name)<EOL>session_store = sessions.SessionStore(handler.request, cfg)<EOL>self.session_dict = session_store.get_session(backend=backend)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>self.session_dict = session<EOL><DEDENT>

.. warning::

    Do not use the ``'securecookie'`` backend with
    :class:`.providers.OpenID` provider. The
    `python-openid`_ library saves **non json serializable** objects
    to session which the ``'securecookie'`` backend cannot cope with.

:param handler:
    A :class:`webapp2.RequestHandler` instance.

:param session:
    A :class:`webapp2_extras.session.SessionDict` instance.

:param str secret:
    The session secret.

:param str cookie_name:
    The name of the session cookie.

:param backend:
    The session backend. One of ``'memcache'`` or ``'datastore'``.

:param config:
    The session config.


@classmethod<EOL><INDENT>def get(cls, key, default=None):<DEDENT>

<EOL>result = cls.query(cls.provider_name == key).get()<EOL>if result:<EOL><INDENT>result_dict = result.to_dict()<EOL>result_dict['<STR_LIT:store>'] = NDBOpenIDStore<EOL>for i in ('<STR_LIT>', ):<EOL><INDENT>prop = result_dict.get(i)<EOL>if prop:<EOL><INDENT>result_dict[i] = [s.strip() for s in prop.split('<STR_LIT:U+002C>')]<EOL><DEDENT>else:<EOL><INDENT>result_dict[i] = None<EOL><DEDENT><DEDENT>return result_dict<EOL><DEDENT>else:<EOL><INDENT>return default<EOL><DEDENT>

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

:returns:
    A configuration dictionary for specified provider.


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

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


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.


def login(self, *login_args, **login_kwargs):

def decorator(f):<EOL><INDENT>@wraps(f)<EOL>def decorated(*args, **kwargs):<EOL><INDENT>self.response = make_response()<EOL>adapter = WerkzeugAdapter(request, self.response)<EOL>login_kwargs.setdefault('<STR_LIT>', session)<EOL>login_kwargs.setdefault('<STR_LIT>', self.session_saver)<EOL>self.result = super(FlaskAuthomatic, self).login(<EOL>adapter,<EOL>*login_args,<EOL>**login_kwargs)<EOL>return f(*args, **kwargs)<EOL><DEDENT>return decorated<EOL><DEDENT>return decorator<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.


res = collections.defaultdict(list)<EOL>for k, v in items:<EOL><INDENT>res[k].append(v)<EOL><DEDENT>return normalize_dict(dict(res))<EOL>

Converts list of tuples to dictionary with duplicate keys converted to
lists.

:param list items:
    List of tuples.

:returns:
    :class:`dict`


return _counter.count()<EOL>

A simple counter to be used in the config to generate unique `IDs`.

:returns:
    :class:`int`.

Use it in the :doc:`config` like this:
::

    import authomatic

    CONFIG = {
        'facebook': {
             'class_': authomatic.providers.oauth2.Facebook,
             'id': authomatic.provider_id(), # returns 1
             'consumer_key': '##########',
             'consumer_secret': '##########',
             'scope': ['user_about_me', 'email']
        },
        'google': {
             'class_': 'authomatic.providers.oauth2.Google',
             'id': authomatic.provider_id(), # returns 2
             'consumer_key': '##########',
             'consumer_secret': '##########',
             'scope': ['https://www.googleapis.com/auth/userinfo.profile',
                       'https://www.googleapis.com/auth/userinfo.email']
        },
        'windows_live': {
             'class_': 'oauth2.WindowsLive',
             'id': authomatic.provider_id(), # returns 3
             'consumer_key': '##########',
             'consumer_secret': '##########',
             'scope': ['wl.basic', 'wl.emails', 'wl.photos']
        },
    }


return parse.quote(s.encode('<STR_LIT:utf-8>'), safe='<STR_LIT>')<EOL>


try:<EOL><INDENT>return json.loads(body)<EOL><DEDENT>except (OverflowError, TypeError, ValueError):<EOL><INDENT>pass<EOL><DEDENT>try:<EOL><INDENT>return ElementTree.fromstring(body)<EOL><DEDENT>except (ElementTree.ParseError, TypeError, ValueError):<EOL><INDENT>pass<EOL><DEDENT>return dict(parse.parse_qsl(body))<EOL>

Parses response body from JSON, XML or query string.

:param body:
    string

:returns:
    :class:`dict`, :class:`list` if input is JSON or query string,
    :class:`xml.etree.ElementTree.Element` if XML.


try:<EOL><INDENT>if '<STR_LIT:.>' in import_name:<EOL><INDENT>module, obj = import_name.rsplit('<STR_LIT:.>', <NUM_LIT:1>)<EOL>return getattr(__import__(module, None, None, [obj]), obj)<EOL><DEDENT>else:<EOL><INDENT>return __import__(import_name)<EOL><DEDENT><DEDENT>except (ImportError, AttributeError) as e:<EOL><INDENT>if not silent:<EOL><INDENT>raise ImportStringError('<STR_LIT>'<EOL>.format(import_name), str(e))<EOL><DEDENT><DEDENT>

Imports an object by string in dotted notation.

taken `from webapp2.import_string() <http://webapp-
improved.appspot.com/api/webapp2.html#webapp2.import_string>`_


if isinstance(class_, str):<EOL><INDENT>path = '<STR_LIT:.>'.join([__package__, '<STR_LIT>', class_])<EOL>return import_string(class_, True) or import_string(path)<EOL><DEDENT>else:<EOL><INDENT>return class_<EOL><DEDENT>

Returns a provider class.

:param class_name: :class:`string` or
:class:`authomatic.providers.BaseProvider` subclass.


for k, v in list(config.items()):<EOL><INDENT>if v.get('<STR_LIT:id>') == short_name:<EOL><INDENT>return k<EOL><DEDENT><DEDENT>raise Exception(<EOL>'<STR_LIT>'.format(short_name))<EOL>

Returns the provider :doc:`config` key based on it's ``id`` value.

:param dict config:
    :doc:`config`.
:param id:
    Value of the id parameter in the :ref:`config` to search for.


def __init__(self, func, *args, **kwargs):

super(Future, self).__init__()<EOL>self._func = func<EOL>self._args = args<EOL>self._kwargs = kwargs<EOL>self._result = None<EOL>self.start()<EOL>

:param callable func:
    The function to be run in separate thread.

Calls :data:`func` in separate thread and returns immediately.
Accepts arbitrary positional and keyword arguments which will be
passed to :data:`func`.


self.join(timeout)<EOL>return self._result<EOL>

Waits for the wrapped :data:`func` to finish and returns its result.

.. note::

    This will block the **calling thread** until the :data:`func`
    returns.

:param timeout:
    :class:`float` or ``None`` A timeout for the :data:`func` to
    return in seconds.

:returns:
    The result of the wrapped :data:`func`.


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.


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.data:<EOL><INDENT>cookie = self.create_cookie()<EOL>cookie_len = len(cookie)<EOL>if cookie_len > <NUM_LIT>:<EOL><INDENT>raise SessionError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>.format(cookie_len))<EOL><DEDENT>self.adapter.set_header('<STR_LIT>', cookie)<EOL>self._data = {}<EOL><DEDENT>


cookie = self.adapter.cookies.get(self.name)<EOL>return self._deserialize(cookie) if cookie else {}<EOL>


if not self._data:<EOL><INDENT>self._data = self._get_data()<EOL><DEDENT>if self._data is None:<EOL><INDENT>self._data = {}<EOL><DEDENT>return self._data<EOL>


signature = hmac.new(six.b(self.secret), digestmod=hashlib.sha1)<EOL>signature.update(six.b('<STR_LIT:|>'.join(parts)))<EOL>return signature.hexdigest()<EOL>


<EOL>data = value<EOL>serialized = pickle.dumps(data).decode('<STR_LIT>')<EOL>encoded = parse.quote(serialized, '<STR_LIT>')<EOL>timestamp = str(int(time.time()))<EOL>signature = self._signature(self.name, encoded, timestamp)<EOL>concatenated = '<STR_LIT:|>'.join([encoded, timestamp, signature])<EOL>return concatenated<EOL>

Converts the value to a signed string with timestamp.

:param value:
    Object to be serialized.

:returns:
    Serialized value.


<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.


return self.provider.update_user()<EOL>

Updates the user info by fetching the **provider's** user info URL.

:returns:
    Updated instance of this class.


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.


<EOL>d = copy.copy(self.__dict__)<EOL>d['<STR_LIT>'] = self.provider.name<EOL>d['<STR_LIT>'] = self.credentials.serialize(<EOL>) if self.credentials else None<EOL>d['<STR_LIT>'] = str(d['<STR_LIT>'])<EOL>d.pop('<STR_LIT:content>')<EOL>if isinstance(self.data, ElementTree.Element):<EOL><INDENT>d['<STR_LIT:data>'] = None<EOL><DEDENT>return d<EOL>

Converts the :class:`.User` instance to a :class:`dict`.

:returns:
    :class:`dict`


<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.


if self.expire_in < <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT>else:<EOL><INDENT>return datetime.datetime.fromtimestamp(self.expiration_time)<EOL><DEDENT>

Expiration date as :class:`datetime.datetime` or ``None`` if
credentials never expire.


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.


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

Returns ``True`` if credentials expire sooner than specified.

:param int seconds:
    Number of seconds.

:returns:
    ``True`` if credentials expire sooner than specified,
    else ``False``.


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*.


def async_refresh(self, *args, **kwargs):

return Future(self.refresh, *args, **kwargs)<EOL>

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

.. warning::

    |async|

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


return resolve_provider_class(self.provider_type)<EOL>

Returns the :doc:`provider <providers>` class specified in the
:doc:`config`.

:returns:
    :class:`authomatic.providers.BaseProvider` subclass.


if self.provider_id is None:<EOL><INDENT>raise ConfigError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>rest = self.provider_type_class().to_tuple(self)<EOL>result = (self.provider_id, self.provider_type_id) + rest<EOL>stringified = [str(i) for i in result]<EOL>concatenated = '<STR_LIT:\n>'.join(stringified)<EOL>return parse.quote(concatenated, '<STR_LIT>')<EOL>

Converts the credentials to a percent encoded string to be stored for
later use.

:returns:
    :class:`string`


@classmethod<EOL><INDENT>def deserialize(cls, config, credentials):<DEDENT>

<EOL>if isinstance(credentials, Credentials):<EOL><INDENT>return credentials<EOL><DEDENT>decoded = parse.unquote(credentials)<EOL>split = decoded.split('<STR_LIT:\n>')<EOL>if split[<NUM_LIT:0>] is None:<EOL><INDENT>raise CredentialsError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>provider_name = id_to_name(config, int(split[<NUM_LIT:0>]))<EOL>cfg = config.get(provider_name)<EOL>ProviderClass = resolve_provider_class(cfg.get('<STR_LIT>'))<EOL>deserialized = Credentials(config)<EOL>deserialized.provider_id = provider_id<EOL>deserialized.provider_type = ProviderClass.get_type()<EOL>deserialized.provider_type_id = split[<NUM_LIT:1>]<EOL>deserialized.provider_class = ProviderClass<EOL>deserialized.provider_name = provider_name<EOL>deserialized.provider_class = ProviderClass<EOL>return ProviderClass.reconstruct(split[<NUM_LIT:2>:], deserialized, cfg)<EOL>

A *class method* which reconstructs credentials created by
:meth:`serialize`. You can also pass it a :class:`.Credentials`
instance.

:param dict config:
    The same :doc:`config` used in the :func:`.login` to get the
    credentials.
:param str credentials:
    :class:`string` The serialized credentials or
    :class:`.Credentials` instance.

:returns:
    :class:`.Credentials`