# Path: OSMPythonTools/api.py
# class Api(CacheObject):
#     def __init__(self, endpoint='http://www.openstreetmap.org/api/0.6/', **kwargs):
#         super().__init__('api', endpoint, jsonResult=False, **kwargs)
#     
#     def _queryString(self, query, params={}, history=False):
#         query = query + ('/history' if history else '')
#         return (query, query, params)
#     
#     def _queryRequest(self, endpoint, queryString, params={}):
#         return endpoint + queryString
#     
#     def _rawToResult(self, data, queryString, params, kwargs, cacheMetadata=None, shallow=False):
#         return ApiResult(data, queryString, params, cacheMetadata=cacheMetadata, shallow=shallow, history=kwargs['history'] if 'history' in kwargs else False)
# 
# Path: OSMPythonTools/cachingStrategy/json.py
# class JSON(CachingStrategyBase):
#     def __init__(self, cacheDir='cache'):
#         self._cacheDir = cacheDir
# 
#     def _filename(self, key):
#         return os.path.join(self._cacheDir, key)
# 
#     def get(self, key):
#         filename = self._filename(key)
#         data = None
#         if not os.path.exists(self._cacheDir):
#             os.makedirs(self._cacheDir)
#         if os.path.exists(filename):
#             with open(filename, 'r') as file:
#                 data = ujson.load(file)
#         return data
# 
#     def set(self, key, value):
#         with open(self._filename(key), 'w') as file:
#             ujson.dump(value, file)
# 
# Path: OSMPythonTools/cachingStrategy/pickle.py
# class Pickle(CachingStrategyBase):
#     def __init__(self, cacheFile='cache', gzip=True):
#         self._cacheFile = cacheFile + '.pickle' + ('.gzip' if gzip else '')
#         self._open = libraryGzip.open if gzip else open
#         self.close()
# 
#     def get(self, key):
#         if self._cache is None:
#             self.open()
#         return self._cache[key] if key in self._cache else None
# 
#     def set(self, key, value):
#         if self._cache is None:
#             self.open()
#         with self._open(self._cacheFile, 'ab') as file:
#             pickle.dump((key, value), file)
#         self._cache[key] = value
# 
#     def open(self):
#         if os.path.exists(self._cacheFile):
#             with self._open(self._cacheFile, 'rb') as file:
#                 self._cache = {}
#                 try:
#                     while True:
#                         k, v = pickle.load(file)
#                         self._cache[k] = v
#                 except EOFError:
#                     pass
#         else:
#             self._cache = {}
# 
#     def close(self):
#         self._cache = None
# 
# Path: OSMPythonTools/cachingStrategy/strategy.py
# class CachingStrategy():
#     __strategy = JSON()
#     @classmethod
#     def use(cls, strategy, **kwargs):
#         cls.__strategy.close()
#         cls.__strategy = strategy(**kwargs)
#         return cls.__strategy
#     @classmethod
#     def get(cls, key):
#         return cls.__strategy.get(key)
#     @classmethod
#     def set(cls, key, value):
#         cls.__strategy.set(key, value)


import pytest
from OSMPythonTools.api import Api
from OSMPythonTools.cachingStrategy import CachingStrategy, JSON, Pickle


@pytest.mark.parametrize(('cachingStrategyF'), [
  lambda: CachingStrategy.use(JSON),
  lambda: CachingStrategy.use(Pickle),

# Path: OSMPythonTools/api.py
# class Api(CacheObject):
#     def __init__(self, endpoint='http://www.openstreetmap.org/api/0.6/', **kwargs):
#         super().__init__('api', endpoint, jsonResult=False, **kwargs)
#     
#     def _queryString(self, query, params={}, history=False):
#         query = query + ('/history' if history else '')
#         return (query, query, params)
#     
#     def _queryRequest(self, endpoint, queryString, params={}):
#         return endpoint + queryString
#     
#     def _rawToResult(self, data, queryString, params, kwargs, cacheMetadata=None, shallow=False):
#         return ApiResult(data, queryString, params, cacheMetadata=cacheMetadata, shallow=shallow, history=kwargs['history'] if 'history' in kwargs else False)
# 
# Path: OSMPythonTools/cachingStrategy/json.py
# class JSON(CachingStrategyBase):
#     def __init__(self, cacheDir='cache'):
#         self._cacheDir = cacheDir
# 
#     def _filename(self, key):
#         return os.path.join(self._cacheDir, key)
# 
#     def get(self, key):
#         filename = self._filename(key)
#         data = None
#         if not os.path.exists(self._cacheDir):
#             os.makedirs(self._cacheDir)
#         if os.path.exists(filename):
#             with open(filename, 'r') as file:
#                 data = ujson.load(file)
#         return data
# 
#     def set(self, key, value):
#         with open(self._filename(key), 'w') as file:
#             ujson.dump(value, file)
# 
# Path: OSMPythonTools/cachingStrategy/pickle.py
# class Pickle(CachingStrategyBase):
#     def __init__(self, cacheFile='cache', gzip=True):
#         self._cacheFile = cacheFile + '.pickle' + ('.gzip' if gzip else '')
#         self._open = libraryGzip.open if gzip else open
#         self.close()
# 
#     def get(self, key):
#         if self._cache is None:
#             self.open()
#         return self._cache[key] if key in self._cache else None
# 
#     def set(self, key, value):
#         if self._cache is None:
#             self.open()
#         with self._open(self._cacheFile, 'ab') as file:
#             pickle.dump((key, value), file)
#         self._cache[key] = value
# 
#     def open(self):
#         if os.path.exists(self._cacheFile):
#             with self._open(self._cacheFile, 'rb') as file:
#                 self._cache = {}
#                 try:
#                     while True:
#                         k, v = pickle.load(file)
#                         self._cache[k] = v
#                 except EOFError:
#                     pass
#         else:
#             self._cache = {}
# 
#     def close(self):
#         self._cache = None
# 
# Path: OSMPythonTools/cachingStrategy/strategy.py
# class CachingStrategy():
#     __strategy = JSON()
#     @classmethod
#     def use(cls, strategy, **kwargs):
#         cls.__strategy.close()
#         cls.__strategy = strategy(**kwargs)
#         return cls.__strategy
#     @classmethod
#     def get(cls, key):
#         return cls.__strategy.get(key)
#     @classmethod
#     def set(cls, key, value):
#         cls.__strategy.set(key, value)


# Path: tests/test_cache.py
import pytest
from OSMPythonTools.api import Api
from OSMPythonTools.cachingStrategy import CachingStrategy, JSON, Pickle



@pytest.mark.parametrize(('cachingStrategyF'), [
  lambda: CachingStrategy.use(JSON),
  lambda: CachingStrategy.use(Pickle),

  lambda: CachingStrategy.use(Pickle, gzip=False),