<commit_before># -*- coding: utf-8 -*-
# Author: <NAME> <<EMAIL>>
#
# This module was developed with funding provided by
# the ESA Summer of Code (2011).
#
#pylint: disable=W0401,C0103,R0904,W0141

from __future__ import absolute_import

"""
This module provides a wrapper around the VSO API.
"""

import re
import os
import sys
import tempfile
import threading

# For Content-Disposition parsing
from email.parser import FeedParser
from datetime import datetime, timedelta
from functools import partial
from collections import defaultdict

from suds import client, TypeNotFound

from sunpy.net import download
from sunpy.net.attr import and_, Attr
from sunpy.net.vso.attrs import walker, TIMEFORMAT
from sunpy.util.util import to_angstrom, print_table
from sunpy.time import parse_time

DEFAULT_URL = 'http://docs.virtualsolar.org/WSDL/VSOi_rpc_literal.wsdl'
DEFAULT_PORT = 'nsoVSOi'
RANGE = re.compile(r'(\d+)(\s*-\s*(\d+))?(\s*([a-zA-Z]+))?')

def get_filename(content_disposition):
    parser = FeedParser()
    parser.feed("Content-Disposition: " + content_disposition)
    name = parser.close().get_filename()
    if not isinstance(name, unicode):
        name = name.decode("latin1")
    return name


# TODO: Name
class NoData(Exception):
    """ Risen for callbacks of VSOClient that are unable to supply
    information for the request. """
    pass


class _Str(str):
    """ Subclass of string that contains a meta attribute for the
    record_item associated with the file. """
    pass


# ----------------------------------------

class Results(object):
    """ Returned by VSOClient.get. Use .wait to wait
    for completion of download.
    """
    def __init__(self, callback, n=0, done=None):
        self.callback = callback
        self.n = n
        self.map_ = {}
        self.done = done
        self.evt = threading.Event()
        self.errors = []
    
    def submit(self, keys, value):
        """
        
        Parameters
        ----------
        keys : list
            names under which to save the value
        value : object
            value to save
        """
        for key in keys:
            self.map_[key] = value
        self.poke()
    
    def poke(self):
        self.n -= 1
        """ Signal completion of one item that was waited for. This can be
        because it was submitted, because it lead to an error or for any
        other reason. """
        if not self.n:
            if self.done is not None:
                self.map_ = self.done(self.map_)
            self.callback(self.map_)
            self.evt.set()
    
    def require(self, keys):
        """ Require that keys be submitted before the Results object is
        finished (i.e., wait returns). Returns a callback method that can
        be used to submit the result by simply calling it with the result.
        
        keys : list
            name of keys under which to save the result
        """
        self.n += 1
        return partial(self.submit, keys)
    
    def wait(self):
        """ Wait for result to be complete and return it. """
        self.evt.wait()
        return self.map_
    
    def add_error(self, exception):
        """ Signal a required result cannot be submitted because of an
        error. """
        self.errors.append(exception)
        self.poke()


def _parse_waverange(string):
    min_, max_, unit = RANGE.match(string)[::2]<commit_msg>Improve Result.wait.<commit_after># -*- coding: utf-8 -*-
# Author: <NAME> <<EMAIL>>
#
# This module was developed with funding provided by
# the ESA Summer of Code (2011).
#
#pylint: disable=W0401,C0103,R0904,W0141

from __future__ import absolute_import

"""
This module provides a wrapper around the VSO API.
"""

import re
import os
import sys
import tempfile
import threading

# For Content-Disposition parsing
from email.parser import FeedParser
from datetime import datetime, timedelta
from functools import partial
from collections import defaultdict

from suds import client, TypeNotFound

from sunpy.net import download
from sunpy.net.attr import and_, Attr
from sunpy.net.vso.attrs import walker, TIMEFORMAT
from sunpy.util.util import to_angstrom, print_table
from sunpy.time import parse_time

DEFAULT_URL = 'http://docs.virtualsolar.org/WSDL/VSOi_rpc_literal.wsdl'
DEFAULT_PORT = 'nsoVSOi'
RANGE = re.compile(r'(\d+)(\s*-\s*(\d+))?(\s*([a-zA-Z]+))?')

def get_filename(content_disposition):
    parser = FeedParser()
    parser.feed("Content-Disposition: " + content_disposition)
    name = parser.close().get_filename()
    if not isinstance(name, unicode):
        name = name.decode("latin1")
    return name


# TODO: Name
class NoData(Exception):
    """ Risen for callbacks of VSOClient that are unable to supply
    information for the request. """
    pass


class _Str(str):
    """ Subclass of string that contains a meta attribute for the
    record_item associated with the file. """
    pass


# ----------------------------------------

class Results(object):
    """ Returned by VSOClient.get. Use .wait to wait
    for completion of download.
    """
    def __init__(self, callback, n=0, done=None):
        self.callback = callback
        self.n = n
        self.map_ = {}
        self.done = done
        self.evt = threading.Event()
        self.errors = []
    
    def submit(self, keys, value):
        """
        
        Parameters
        ----------
        keys : list
            names under which to save the value
        value : object
            value to save
        """
        for key in keys:
            self.map_[key] = value
        self.poke()
    
    def poke(self):
        self.n -= 1
        """ Signal completion of one item that was waited for. This can be
        because it was submitted, because it lead to an error or for any
        other reason. """
        if not self.n:
            if self.done is not None:
                self.map_ = self.done(self.map_)
            self.callback(self.map_)
            self.evt.set()
    
    def require(self, keys):
        """ Require that keys be submitted before the Results object is
        finished (i.e., wait returns). Returns a callback method that can
        be used to submit the result by simply calling it with the result.
        
        keys : list
            name of keys under which to save the result
        """
        self.n += 1
        return partial(self.submit, keys)
    
    def wait(self, timeout=100):
        """ Wait for result to be complete and return it. """
        # Giving wait a timeout somehow circumvents a CPython bug that the
        # call gets ininterruptible.
        while not self.evt.wait(timeout):
            pass

        return self.map_
    
    def add_error(self, exception):
        """ Signal a required result cannot be submitted because of an
        error. """
        self.errors.append(exception)
        self.poke()


def _parse_waverange(string):
    min_, max_, unit = RANGE.match(string)[::2]
{'fraction_non_alphanumeric': 0.10834049871023216, 'fraction_numerical': 0.007881914588707366, 'mean_word_length': 2.2765258215962443, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5493275', 'n_tokens_mistral': 2031, 'n_tokens_neox': 1965, 'n_words': 694}