pyBiocFileCache 0.3.0__tar.gz → 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/AUTHORS.md +1 -0
  2. pybiocfilecache-0.4.0/CHANGELOG.md +9 -0
  3. {pybiocfilecache-0.3.0/src/pyBiocFileCache.egg-info → pybiocfilecache-0.4.0}/PKG-INFO +1 -1
  4. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/docs/authors.md +1 -0
  5. pybiocfilecache-0.4.0/docs/changelog.md +9 -0
  6. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/docs/conf.py +3 -8
  7. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0/src/pyBiocFileCache.egg-info}/PKG-INFO +1 -1
  8. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/src/pyBiocFileCache.egg-info/SOURCES.txt +2 -2
  9. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/src/pybiocfilecache/BiocFileCache.py +82 -40
  10. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/src/pybiocfilecache/__init__.py +2 -2
  11. pybiocfilecache-0.4.0/src/pybiocfilecache/const.py +1 -0
  12. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/src/pybiocfilecache/db/__init__.py +1 -1
  13. pybiocfilecache-0.4.0/src/pybiocfilecache/db/db_config.py +84 -0
  14. pybiocfilecache-0.4.0/src/pybiocfilecache/utils.py +89 -0
  15. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/tests/conftest.py +1 -2
  16. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/tests/test_cache.py +8 -0
  17. pybiocfilecache-0.3.0/CHANGELOG.md +0 -5
  18. pybiocfilecache-0.3.0/docs/changelog.md +0 -5
  19. pybiocfilecache-0.3.0/src/pybiocfilecache/db/Base.py +0 -34
  20. pybiocfilecache-0.3.0/src/pybiocfilecache/db/schema.py +0 -35
  21. pybiocfilecache-0.3.0/src/pybiocfilecache/utils.py +0 -68
  22. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/.coveragerc +0 -0
  23. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/.github/workflows/pypi-publish.yml +0 -0
  24. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/.github/workflows/pypi-test.yml +0 -0
  25. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/.gitignore +0 -0
  26. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/.pre-commit-config.yaml +0 -0
  27. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/.readthedocs.yml +0 -0
  28. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/CONTRIBUTING.rst +0 -0
  29. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/LICENSE.txt +0 -0
  30. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/README.md +0 -0
  31. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/docs/Makefile +0 -0
  32. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/docs/_static/.gitignore +0 -0
  33. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/docs/contributing.rst +0 -0
  34. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/docs/index.md +0 -0
  35. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/docs/license.rst +0 -0
  36. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/docs/readme.md +0 -0
  37. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/docs/requirements.txt +0 -0
  38. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/pyproject.toml +0 -0
  39. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/setup.cfg +0 -0
  40. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/setup.py +0 -0
  41. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/src/pyBiocFileCache.egg-info/dependency_links.txt +0 -0
  42. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/src/pyBiocFileCache.egg-info/not-zip-safe +0 -0
  43. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/src/pyBiocFileCache.egg-info/requires.txt +0 -0
  44. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/src/pyBiocFileCache.egg-info/top_level.txt +0 -0
  45. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/src/pybiocfilecache/_exceptions.py +0 -0
  46. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/tests/data/test1.txt +0 -0
  47. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/tests/data/test2.txt +0 -0
  48. {pybiocfilecache-0.3.0 → pybiocfilecache-0.4.0}/tox.ini +0 -0
@@ -2,3 +2,4 @@
2
2
 
3
3
  * Jayaram Kancherla <jayaram.kancherla@gmail.com>
4
4
  * Max Hargreaves <max_hargreaves@outlook.com>
5
+ * Oleksandr Khoroshevskyi <khorosh@virginia.edu>
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ ## Version 0.4 (development)
4
+
5
+ - Migrate the schema to match R/Bioconductor's BiocFileCache (Check out [this issue](https://github.com/BiocPy/pyBiocFileCache/issues/11)). Thanks to [@khoroshevskyi ](https://github.com/khoroshevskyi) for the PR.
6
+
7
+ ## Version 0.1
8
+
9
+ - Initial release of the package, Setting up all the actions.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pyBiocFileCache
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: File based cache for resources and metadata
5
5
  Home-page: https://github.com/epiviz/pyBiocFileCache
6
6
  Author: jkanche
@@ -2,3 +2,4 @@
2
2
 
3
3
  * Jayaram Kancherla <jayaram.kancherla@gmail.com>
4
4
  * Max Hargreaves <max_hargreaves@outlook.com>
5
+ * Oleksandr Khoroshevskyi <khorosh@virginia.edu>
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ ## Version 0.4 (development)
4
+
5
+ - Migrate the schema to match R/Bioconductor's BiocFileCache (Check out [this issue](https://github.com/BiocPy/pyBiocFileCache/issues/11)). Thanks to [@khoroshevskyi ](https://github.com/khoroshevskyi) for the PR.
6
+
7
+ ## Version 0.1
8
+
9
+ - Initial release of the package, Setting up all the actions.
@@ -179,10 +179,7 @@ html_theme = "furo"
179
179
  # Theme options are theme-specific and customize the look and feel of a theme
180
180
  # further. For a list of options available for each theme, see the
181
181
  # documentation.
182
- html_theme_options = {
183
- "sidebar_width": "300px",
184
- "page_width": "1200px"
185
- }
182
+ html_theme_options = {"sidebar_width": "300px", "page_width": "1200px"}
186
183
 
187
184
  # Add any paths that contain custom themes here, relative to this directory.
188
185
  # html_theme_path = []
@@ -266,9 +263,7 @@ latex_elements = {
266
263
 
267
264
  # Grouping the document tree into LaTeX files. List of tuples
268
265
  # (source start file, target name, title, author, documentclass [howto/manual]).
269
- latex_documents = [
270
- ("index", "user_guide.tex", "pyBiocFileCache Documentation", "jkanche", "manual")
271
- ]
266
+ latex_documents = [("index", "user_guide.tex", "pyBiocFileCache Documentation", "jkanche", "manual")]
272
267
 
273
268
  # The name of an image file (relative to this directory) to place at the top of
274
269
  # the title page.
@@ -304,4 +299,4 @@ intersphinx_mapping = {
304
299
  "pyscaffold": ("https://pyscaffold.org/en/stable", None),
305
300
  }
306
301
 
307
- print(f"loading configurations for {project} {version} ...", file=sys.stderr)
302
+ print(f"loading configurations for {project} {version} ...", file=sys.stderr)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pyBiocFileCache
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: File based cache for resources and metadata
5
5
  Home-page: https://github.com/epiviz/pyBiocFileCache
6
6
  Author: jkanche
@@ -32,10 +32,10 @@ src/pyBiocFileCache.egg-info/top_level.txt
32
32
  src/pybiocfilecache/BiocFileCache.py
33
33
  src/pybiocfilecache/__init__.py
34
34
  src/pybiocfilecache/_exceptions.py
35
+ src/pybiocfilecache/const.py
35
36
  src/pybiocfilecache/utils.py
36
- src/pybiocfilecache/db/Base.py
37
37
  src/pybiocfilecache/db/__init__.py
38
- src/pybiocfilecache/db/schema.py
38
+ src/pybiocfilecache/db/db_config.py
39
39
  tests/conftest.py
40
40
  tests/test_cache.py
41
41
  tests/data/test1.txt
@@ -3,17 +3,16 @@
3
3
  import os
4
4
  from pathlib import Path
5
5
  from time import sleep, time
6
- from typing import List, Optional, Union
6
+ from typing import List, Literal, Optional, Union
7
7
 
8
8
  from sqlalchemy import func
9
9
  from sqlalchemy.orm import Session
10
10
 
11
- from .db import create_schema
12
- from .db.schema import Resource
13
- from .utils import copy_or_move, create_tmp_dir, generate_id
14
11
  from ._exceptions import NoFpathError, RnameExistsError, RpathTimeoutError
12
+ from .db.db_config import create_schema, Resource
13
+ from .utils import copy_or_move, create_tmp_dir, generate_id
15
14
 
16
- __author__ = "jkanche"
15
+ __author__ = "Jayaram Kancherla"
17
16
  __copyright__ = "jkanche"
18
17
  __license__ = "MIT"
19
18
 
@@ -25,8 +24,10 @@ class BiocFileCache:
25
24
  """Initialize BiocFileCache.
26
25
 
27
26
  Args:
28
- cacheDirOrPath (Union[str, Path], optional): Path to cache.
29
- directory. Defaults to tmp location, `create_tmp_dir()`.
27
+ cacheDirOrPath:
28
+ Path to cache directory.
29
+
30
+ Defaults to tmp location, :py:func:`~.utils.create_tmp_dir`.
30
31
 
31
32
  Raises:
32
33
  Exception: Failed to initialize cache.
@@ -51,38 +52,54 @@ class BiocFileCache:
51
52
  self,
52
53
  rname: str,
53
54
  fpath: Union[str, Path],
54
- rtype: str = "local",
55
- action: str = "copy",
55
+ rtype: Literal["local", "web", "relative"] = "local",
56
+ action: Literal["copy", "move", "asis"] = "copy",
56
57
  ext: bool = False,
57
58
  ) -> Resource:
58
59
  """Add a resource from the provided `fpath` to cache as `rname`.
59
60
 
60
61
  Args:
61
- rname (str): Name of the resource to add to cache.
62
- fpath (Union[str, Path]): Location of the resource.
63
- rtype (str, optional): One of `"local"`, `"web"`, or `"relative"`.
64
- Defaults to `"local"`.
65
- action (str, optional): Either `"copy"`, `"move"` or `"asis"`.
66
- Defaults to `"copy"`.
67
- ext (bool, optional): Use filepath extension when storing in cache.
68
- Defaults to `False`.
62
+ rname:
63
+ Name of the resource to add to cache.
69
64
 
70
- Returns:
71
- Resource: Database record of the new resource in cache.
65
+ fpath:
66
+ Location of the resource.
67
+
68
+ rtype:
69
+ One of ``local``, ``web``, or ``relative``.
70
+ Defaults to ``local``.
71
+
72
+ action:
73
+ Either ``copy``, ``move`` or ``asis``.
74
+ Defaults to ``copy``.
75
+
76
+ ext:
77
+ Whether to use filepath extension when storing in cache.
78
+ Defaults to `False`.
72
79
 
73
80
  Raises:
74
- NoFpathError: When the `fpath` does not exist.
75
- RnameExistsError: When the `rname` already exists in the cache.
76
- sqlalchemy exceptions: When something is up with the cache.
81
+ NoFpathError:
82
+ When the `fpath` does not exist.
83
+
84
+ RnameExistsError:
85
+ When the `rname` already exists in the cache.
86
+ sqlalchemy exceptions: When something is up with the cache.
87
+
88
+ Returns:
89
+ Database record of the new resource in cache.
77
90
  """
78
91
  if isinstance(fpath, str):
79
92
  fpath = Path(fpath)
80
93
 
81
94
  if not fpath.exists():
82
- raise NoFpathError(f"Resource at {fpath} does not exist.")
95
+ raise NoFpathError(f"Resource at '{fpath}' does not exist.")
83
96
 
84
97
  rid = generate_id()
85
- rpath = f"{self.cache}/{rid}" + (f".{fpath.suffix}" if ext else "")
98
+ rpath = (
99
+ f"{self.cache}/{rid}" + (f".{fpath.suffix}" if ext else "")
100
+ if action != "asis"
101
+ else str(fpath)
102
+ )
86
103
 
87
104
  # create new record in the database
88
105
  res = Resource(
@@ -123,11 +140,15 @@ class BiocFileCache:
123
140
  """Search cache for a resource.
124
141
 
125
142
  Args:
126
- query (str): query or keywords to search.
127
- field (str, optional): Field to search. Defaults to "rname".
143
+ query:
144
+ Query string or keywords to search.
145
+
146
+ field:
147
+ Field to search.
148
+ Defaults to "rname".
128
149
 
129
150
  Returns:
130
- List[Resource]: list of matching resources from cache.
151
+ List of matching resources from cache.
131
152
  """
132
153
  with self.sessionLocal() as session:
133
154
  return (
@@ -140,11 +161,14 @@ class BiocFileCache:
140
161
  """Get a resource with `rname` from given `Session`.
141
162
 
142
163
  Args:
143
- session (Session): The `Session` object to use.
144
- rname (str): The `rname` of the `Resource` to get.
164
+ session:
165
+ The `Session` object to use.
166
+
167
+ rname:
168
+ The `rname` of the `Resource` to get.
145
169
 
146
170
  Returns:
147
- (Resource, optional): The `Resource` for the `rname` if any.
171
+ The `Resource` for the `rname` if available.
148
172
  """
149
173
  resource: Optional[Resource] = (
150
174
  session.query(Resource).filter(Resource.rname == rname).first()
@@ -169,10 +193,11 @@ class BiocFileCache:
169
193
  """Get resource by name from cache.
170
194
 
171
195
  Args:
172
- rname (str): Name of the file to search.
196
+ rname:
197
+ Name of the file to search.
173
198
 
174
199
  Returns:
175
- Optional[Resource]: matched resource from cache if exists.
200
+ Matched `Resource` from cache if exists.
176
201
  """
177
202
  return self._get(self.sessionLocal(), rname)
178
203
 
@@ -180,7 +205,8 @@ class BiocFileCache:
180
205
  """Remove a resource from cache by name.
181
206
 
182
207
  Args:
183
- rname (str): Name of the resource to remove.
208
+ rname:
209
+ Name of the resource to remove.
184
210
  """
185
211
  with self.sessionLocal() as session:
186
212
  res: Optional[Resource] = self._get(session, rname)
@@ -196,18 +222,30 @@ class BiocFileCache:
196
222
  for file in os.scandir(self.cache):
197
223
  os.remove(file.path)
198
224
 
225
+ return True
226
+
199
227
  def update(
200
- self, rname: str, fpath: Union[str, Path], action: str = "copy"
228
+ self,
229
+ rname: str,
230
+ fpath: Union[str, Path],
231
+ action: Literal["copy", "move", "asis"] = "copy",
201
232
  ) -> Resource:
202
233
  """Update a resource in cache.
203
234
 
204
235
  Args:
205
- rname (str): name of the resource in cache.
206
- fpath (Union[str, Path]): new resource to replace existing file in cache.
207
- action (str, optional): either copy of move. defaults to copy.
236
+ rname:
237
+ Name of the resource in cache.
238
+
239
+ fpath:
240
+ New resource to replace existing file in cache.
241
+
242
+ action:
243
+ Either ``copy``, ``move`` or ``asis``.
244
+
245
+ Defaults to ``copy``.
208
246
 
209
247
  Returns:
210
- Resource: Updated resource record in cache.
248
+ Updated resource record in cache.
211
249
  """
212
250
 
213
251
  if isinstance(fpath, str):
@@ -220,8 +258,12 @@ class BiocFileCache:
220
258
  res = self._get(session, rname)
221
259
 
222
260
  if res is not None:
223
- # copy the file to cache
224
- copy_or_move(str(fpath), str(res.rpath), rname, action)
261
+ if action != "asis":
262
+ # copy the file to cache
263
+ copy_or_move(str(fpath), str(res.rpath), rname, action)
264
+ else:
265
+ res.rpath = str(fpath)
266
+
225
267
  res.access_time = res.last_modified_time = func.now()
226
268
  session.merge(res)
227
269
  session.commit()
@@ -17,8 +17,8 @@ finally:
17
17
 
18
18
  from .BiocFileCache import BiocFileCache as BiocFileCache
19
19
 
20
- from .db.schema import Metadata as Metadata
21
- from .db.schema import Resource as Resource
20
+ from .db.db_config import Metadata as Metadata
21
+ from .db.db_config import Resource as Resource
22
22
 
23
23
  from ._exceptions import NoFpathError as NoFpathError
24
24
  from ._exceptions import RnameExistsError as RnameExistsError
@@ -0,0 +1 @@
1
+ SCHEMA_VERSION = "0.99.4"
@@ -2,4 +2,4 @@ __author__ = "jkanche"
2
2
  __copyright__ = "jkanche"
3
3
  __license__ = "MIT"
4
4
 
5
- from .Base import create_schema
5
+ from .db_config import create_schema
@@ -0,0 +1,84 @@
1
+ from typing import Tuple
2
+
3
+ from sqlalchemy import create_engine, select, Column, Integer, Text, DateTime, func
4
+ from sqlalchemy.engine import Engine
5
+ from sqlalchemy.orm.session import Session
6
+
7
+ from sqlalchemy.orm import declarative_base, sessionmaker
8
+
9
+ from ..const import SCHEMA_VERSION
10
+
11
+ __author__ = "jkanche"
12
+ __copyright__ = "jkanche"
13
+ __license__ = "MIT"
14
+
15
+ Base = declarative_base()
16
+
17
+
18
+ class Metadata(Base):
19
+ __tablename__ = "metadata"
20
+ key = Column(Text(), primary_key=True, index=True)
21
+ value = Column(Text())
22
+
23
+ def __repr__(self):
24
+ return "<Metadata(key='%s', valye='%s')>" % (self.key, self.value)
25
+
26
+
27
+ class Resource(Base):
28
+ __tablename__ = "resource"
29
+ id = Column(Integer, primary_key=True, index=True, autoincrement=True)
30
+ rid = Column(Text())
31
+ rname = Column(Text())
32
+ create_time = Column(DateTime, server_default=func.now())
33
+ access_time = Column(DateTime, server_default=func.now())
34
+ rpath = Column(Text())
35
+ rtype = Column(Text())
36
+ fpath = Column(Text())
37
+ last_modified_time = Column(DateTime, onupdate=func.now())
38
+ etag = Column(Text())
39
+ expires = Column(DateTime)
40
+
41
+ def __repr__(self):
42
+ return "<Resource(id='%s', rname='%s')>" % (self.id, self.rname)
43
+
44
+
45
+ def add_metadata(key: str, value: str, engine: Engine) -> None:
46
+ """Add metadata to the database.
47
+
48
+ Args:
49
+ key:
50
+ Key of the metadata.
51
+ value:
52
+ Value of the metadata.
53
+ engine:
54
+ Engine
55
+ """
56
+ with Session(engine) as session:
57
+ if session.scalar(select(Metadata).where(Metadata.key == key)):
58
+ pass
59
+ else:
60
+ new_metadata = Metadata(key=key, value=value)
61
+ session.add(new_metadata)
62
+ session.commit()
63
+
64
+
65
+ def create_schema(cache_dir: str) -> Tuple[Engine, sessionmaker]:
66
+ """Create the schema in the sqlite database.
67
+
68
+ Args:
69
+ cache_dir:
70
+ Location where the cache directory.
71
+
72
+ Returns:
73
+ A tuple of sqlalchemy engine and session maker.
74
+ """
75
+ engine = create_engine(
76
+ f"sqlite:///{cache_dir}", connect_args={"check_same_thread": False}
77
+ )
78
+
79
+ Base.metadata.create_all(bind=engine, checkfirst=True)
80
+ sessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
81
+
82
+ add_metadata("schema_version", SCHEMA_VERSION, engine)
83
+
84
+ return (engine, sessionLocal)
@@ -0,0 +1,89 @@
1
+ import logging
2
+ import sys
3
+ import tempfile
4
+ import uuid
5
+ from pathlib import Path
6
+ from shutil import copy2, move
7
+ from typing import Literal, Union
8
+
9
+ __author__ = "Jayaram Kancherla"
10
+ __copyright__ = "jkanche"
11
+ __license__ = "MIT"
12
+
13
+
14
+ def create_tmp_dir() -> str:
15
+ """Create a temporary directory.
16
+
17
+ Returns:
18
+ Temporary path to the directory.
19
+ """
20
+ return tempfile.mkdtemp()
21
+
22
+
23
+ def generate_id() -> str:
24
+ """Generate uuid.
25
+
26
+ Returns:
27
+ Unique string for use as id.
28
+ """
29
+ return uuid.uuid4().hex
30
+
31
+
32
+ def copy_or_move(
33
+ source: Union[str, Path],
34
+ target: Union[str, Path],
35
+ rname: str,
36
+ action: Literal["copy", "move", "asis"] = "copy",
37
+ ) -> None:
38
+ """Copy or move a resource from ``source`` to ``target``.
39
+
40
+ Args:
41
+ source:
42
+ Source location of the resource to copy of move.
43
+
44
+ target:
45
+ Destination to copy of move to.
46
+
47
+ rname:
48
+ Name of resource to add to cache.
49
+
50
+ action:
51
+ Copy of move file from source.
52
+ Defaults to copy.
53
+
54
+ Raises:
55
+ ValueError:
56
+ If action is not `copy`, `move` or `asis`.
57
+
58
+ Exception:
59
+ Error storing resource in the cache directory.
60
+ """
61
+
62
+ if action not in ["copy", "move", "asis"]:
63
+ raise ValueError(
64
+ f"Action must be either 'move', 'copy' or 'asis', provided {action}."
65
+ )
66
+
67
+ try:
68
+ if action == "copy":
69
+ copy2(source, target)
70
+ elif action == "move":
71
+ move(str(source), target)
72
+ elif action == "asis":
73
+ pass
74
+ except Exception as e:
75
+ raise Exception(
76
+ f"Error storing resource: '{rname}' from: '{source}' in '{target}'.",
77
+ ) from e
78
+
79
+
80
+ def setup_logging(loglevel):
81
+ """Setup basic logging.
82
+
83
+ Args:
84
+ loglevel (int): minimum loglevel for emitting messages
85
+ """
86
+ logformat = "[%(asctime)s] %(levelname)s:%(name)s:%(message)s"
87
+ logging.basicConfig(
88
+ level=loglevel, stream=sys.stdout, format=logformat, datefmt="%Y-%m-%d %H:%M:%S"
89
+ )
@@ -1,9 +1,8 @@
1
1
  """Dummy conftest.py for pybiocfilecache.
2
-
3
2
  If you don't know what this is for, just leave it empty.
4
3
  Read more about conftest.py under:
5
4
  - https://docs.pytest.org/en/stable/fixture.html
6
5
  - https://docs.pytest.org/en/stable/writing_plugins.html
7
6
  """
8
7
 
9
- # import pytest
8
+ # import pytest
@@ -33,6 +33,14 @@ def test_add_get_operations():
33
33
  frec2 = open(rec2.rpath, "r").read().strip()
34
34
  assert frec2 == "test2"
35
35
 
36
+ bfc.add("test3_asis", os.getcwd() + "/tests/data/test2.txt", action="asis")
37
+ rec3 = bfc.get("test3_asis")
38
+ assert rec3 is not None
39
+ assert rec3.rpath == os.getcwd() + "/tests/data/test2.txt"
40
+
41
+ frec3 = open(rec3.rpath, "r").read().strip()
42
+ assert frec3 == "test2"
43
+
36
44
  bfc.purge()
37
45
 
38
46
 
@@ -1,5 +0,0 @@
1
- # Changelog
2
-
3
- ## Version 0.1 (development)
4
-
5
- - Initial release
@@ -1,5 +0,0 @@
1
- # Changelog
2
-
3
- ## Version 0.1 (development)
4
-
5
- - Initial release
@@ -1,34 +0,0 @@
1
- from sqlalchemy import create_engine
2
- from sqlalchemy.engine import Engine
3
-
4
- # from sqlalchemy.ext.declarative import declarative_base
5
- from sqlalchemy.orm import sessionmaker, declarative_base
6
- from typing import Tuple
7
-
8
- __author__ = "jkanche"
9
- __copyright__ = "jkanche"
10
- __license__ = "MIT"
11
-
12
- Base = declarative_base()
13
-
14
-
15
- def create_schema(cache_dir: str) -> Tuple[Engine, sessionmaker]:
16
- """Create the schema in the sqlite database.
17
-
18
- Args:
19
- cache_dir (str): Location where the cache directory
20
-
21
- Returns:
22
- a tuple of sqlalchemy engine and session maker
23
- """
24
- try:
25
- engine = create_engine(
26
- f"sqlite:///{cache_dir}", connect_args={"check_same_thread": False}
27
- )
28
-
29
- Base.metadata.create_all(bind=engine, checkfirst=True)
30
- sessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
31
-
32
- return (engine, sessionLocal)
33
- except Exception as e:
34
- raise e
@@ -1,35 +0,0 @@
1
- from sqlalchemy import Column, DateTime, Integer, String
2
- from sqlalchemy.sql import func
3
-
4
- from .Base import Base
5
-
6
- __author__ = "jkanche"
7
- __copyright__ = "jkanche"
8
- __license__ = "MIT"
9
-
10
-
11
- class Metadata(Base):
12
- __tablename__ = "metadata"
13
- key = Column(String(), primary_key=True, index=True)
14
- value = Column(String())
15
-
16
- def __repr__(self):
17
- return "<Metadata(key='%s', valye='%s')>" % (self.key, self.value)
18
-
19
-
20
- class Resource(Base):
21
- __tablename__ = "resource"
22
- id = Column(Integer, primary_key=True, index=True, autoincrement=True)
23
- rid = Column(String())
24
- rname = Column(String())
25
- create_time = Column(DateTime, server_default=func.now())
26
- access_time = Column(DateTime, server_default=func.now())
27
- rpath = Column(String())
28
- rtype = Column(String())
29
- fpath = Column(String())
30
- last_modified_time = Column(DateTime, onupdate=func.now())
31
- etag = Column(String())
32
- expires = Column(DateTime)
33
-
34
- def __repr__(self):
35
- return "<Resource(id='%s', rname='%s')>" % (self.id, self.rname)
@@ -1,68 +0,0 @@
1
- import tempfile
2
- import uuid
3
- from pathlib import Path
4
- from shutil import copy2, move
5
-
6
- from typing import Union
7
- import logging
8
- import sys
9
-
10
-
11
- def create_tmp_dir() -> str:
12
- """Create a temporary directory.
13
-
14
- Returns:
15
- str: path to the directory
16
- """
17
- return tempfile.mkdtemp()
18
-
19
-
20
- def generate_id() -> str:
21
- """Generate uuid.
22
-
23
- Returns:
24
- str: unique string for use as id
25
- """
26
- return uuid.uuid4().hex
27
-
28
-
29
- def copy_or_move(
30
- source: Union[str, Path], target: Union[str, Path], rname: str, action: str = "copy"
31
- ) -> None:
32
- """Copy or move a resource from `source` to `target`
33
-
34
- Args:
35
- source (Union[str, Path]): source location of the resource to copy of move.
36
- target (Union[str, Path]): destination to copy of move to.
37
- rname (str): Name of resource to add to cache
38
- action (str): copy of move file from source. Defaults to copy.
39
-
40
- Raises:
41
- ValueError: if action is not `copy` or `move`.
42
- Exception: Error storing resource in the cache directory.
43
- """
44
-
45
- if action not in ["copy", "move"]:
46
- raise ValueError(f"Action must be either 'move' or 'copy', provided {action}")
47
-
48
- try:
49
- if action == "copy":
50
- copy2(source, target)
51
- elif action == "move":
52
- move(str(source), target)
53
- except Exception as e:
54
- raise Exception(
55
- f"Error storing resource: '{rname}' from: '{source}' in '{target}'",
56
- ) from e
57
-
58
-
59
- def setup_logging(loglevel):
60
- """Setup basic logging.
61
-
62
- Args:
63
- loglevel (int): minimum loglevel for emitting messages
64
- """
65
- logformat = "[%(asctime)s] %(levelname)s:%(name)s:%(message)s"
66
- logging.basicConfig(
67
- level=loglevel, stream=sys.stdout, format=logformat, datefmt="%Y-%m-%d %H:%M:%S"
68
- )
File without changes