Exemplo n.º 1
0
    def __init__(self,
                 email=default_email,
                 cache_path=default_cache_path,
                 default_args=default_default_args,
                 request_interval=time_dep_request_interval,
                 tool=default_tool,
                 ):
        """
        :param str email: email of user (for abuse reports)
        :param str cache_path: full path to sqlite file (created if necessary)
        :param dict default_args: dictionary of query args that should accompany all requests
        :param request_interval: seconds between requests
        :type request_interval: int or a callable returning an int
        :param str tool: name of client
        :rtype: None
        :raises OSError: if sqlite file can't be opened

        """

        self.default_args = default_args
        self.email = email
        self.request_interval = request_interval
        self.tool = tool

        self._last_request_clock = 0
        self._cache = SQLiteCache(cache_path) if cache_path else None
        self._ident_args = {'tool': tool, 'email': email}
        self._request_count = 0
Exemplo n.º 2
0
    def __init__(self,
                 email=default_email,
                 cache=False,
                 default_args=default_default_args,
                 request_interval=None,
                 tool=default_tool,
                 api_key=None):
        """
        :param str email: email of user (for abuse reports)
        :param str cache: if True, cache at ~/.cache/eutils-db.sqlite; if string, cache there; if False, don't cache
        :param dict default_args: dictionary of query args that should accompany all requests
        :param request_interval: seconds between requests; default: auto-select based on API key
        :type request_interval: int or a callable returning an int
        :param str api_key: api key assigned by NCBI
        :param str tool: name of client
        :rtype: None
        :raises OSError: if sqlite file can't be opened

        """

        self.default_args = default_args
        self.email = email
        self.tool = tool
        self.api_key = api_key

        if request_interval is not None:
            _logger.warning(
                "eutils QueryService: request_interval no longer supported; ignoring passed parameter"
            )

        if self.api_key is None:
            requests_per_second = 3
            _logger.warning(
                "No NCBI API key provided; throttling to {} requests/second; see "
                "https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/"
                .format(requests_per_second))
        else:
            requests_per_second = 10
            _logger.info(
                "Using NCBI API key; throttling to {} requests/second".format(
                    requests_per_second))

        self.request_interval = 1.0 / requests_per_second

        self._last_request_clock = 0
        self._ident_args = {'tool': tool, 'email': email}
        self._request_count = 0

        if cache is True:
            cache_path = default_cache_path
        elif cache:
            cache_path = cache  # better act like a path string
        else:
            cache_path = False
        self._cache = SQLiteCache(cache_path) if cache_path else None
Exemplo n.º 3
0
    def __init__(
        self,
        cache_path=default_cache_path,
        default_args=default_default_args,
        email=default_email,
        request_interval=time_dep_request_interval,
        tool=default_tool,
    ):
        self.default_args = default_args
        self.email = email
        self.request_interval = request_interval
        self.tool = tool

        self._logger = logging.getLogger(__name__)
        self._last_request_clock = 0
        self._cache = SQLiteCache(cache_path) if cache_path else None
        self._ident_args = {'tool': tool, 'email': email}
        self._request_count = 0
        self.session = requests.Session()
Exemplo n.º 4
0
    def __init__(
        self,
        email=default_email,
        cache=False,
        default_args=default_default_args,
        request_interval=time_dep_request_interval,
        tool=default_tool,
    ):
        """
        :param str email: email of user (for abuse reports)
        :param str cache: if True, cache at ~/.cache/eutils-db.sqlite; if string, cache there; if False, don't cache
        :param dict default_args: dictionary of query args that should accompany all requests
        :param request_interval: seconds between requests
        :type request_interval: int or a callable returning an int
        :param str tool: name of client
        :rtype: None
        :raises OSError: if sqlite file can't be opened

        """

        self.default_args = default_args
        self.email = email
        self.request_interval = request_interval
        self.tool = tool

        self._last_request_clock = 0
        self._ident_args = {'tool': tool, 'email': email}
        self._request_count = 0

        if cache is True:
            cache_path = default_cache_path
        elif cache:
            cache_path = cache  # better act like a path string
        else:
            cache_path = False
        self._cache = SQLiteCache(cache_path) if cache_path else None
Exemplo n.º 5
0
    def setUp(self):
        _, self._fn = tempfile.mkstemp(suffix='.db')

        atexit.register(lambda: os.remove(self._fn))

        self.cache = SQLiteCache(self._fn)