Skip to content

Commit

Permalink
storage fixes, refactoring, code style
Browse files Browse the repository at this point in the history
  • Loading branch information
Nayjest committed Nov 3, 2023
1 parent 3b4990a commit 330dcc7
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 16 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ venv
*.env.test.*
.pytest_cache
storage
dist
dist
#pytest-cov
.coverage
coverage.xml
4 changes: 2 additions & 2 deletions microcore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import os

from .embedding_db.base import AbstractEmbeddingDB, SearchResult
from .storage import storage
from .env import configure, env
from .file_storage import storage
from .internal_env import configure, env
from .logging import use_logging
from .message_types import UserMsg, AssistantMsg, SysMsg, Msg
from .config import ApiType, LLMConfigError
Expand Down
2 changes: 1 addition & 1 deletion microcore/ai_modules.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import builtins
import yaml
from jinja2 import PackageLoader
from .env import env
from .internal_env import env

_original_import = builtins.__import__

Expand Down
11 changes: 5 additions & 6 deletions microcore/storage.py → microcore/file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import os
import shutil
from pathlib import Path

import chardet

from . import env
from .internal_env import env


class _Storage:
Expand Down Expand Up @@ -55,20 +54,20 @@ def write(
content = name
name = "out.txt"

base_name = Path(name).stem
base_name = Path(name).with_suffix('')
ext = Path(name).suffix or ".txt"

counter = 0
while True:
file_name = f"{base_name}{'_%d' % counter if counter else ''}.{ext}" # noqa
full_path = Path(self.storage_path) / file_name
full_path = self.storage_path / file_name
if not full_path.is_file() or rewrite_existing:
break
counter += 1

full_path.parent.mkdir(parents=True, exist_ok=True)
full_path.write_text(content, encoding=encoding)
return str(full_path.name)
return file_name

def clean(self, path: str):
"""
Expand All @@ -78,7 +77,7 @@ def clean(self, path: str):
full_path = (self.storage_path / path).resolve()

# Verify that the path is inside the storage_path
if self.storage_path not in full_path.parents:
if self.storage_path.resolve() not in full_path.parents:
raise ValueError("Cannot delete directories outside the storage path.")

if full_path.exists() and full_path.is_dir():
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion microcore/llm_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .wrappers.llm_response_wrapper import LLMResponse
from .env import env
from .internal_env import env


async def allm(prompt, **kwargs) -> str | LLMResponse:
Expand Down
2 changes: 1 addition & 1 deletion microcore/logging.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dataclasses
from colorama import Fore, Style

from .env import env
from .internal_env import env
from .prepare_llm_args import prepare_chat_messages, prepare_prompt
from .utils import is_chat_model

Expand Down
9 changes: 8 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@ license = { file = "LICENSE" }
"Source Code" = "https://github.com/ai-microcore/microcore"

[tool.flit.module]
name = "microcore"
name = "microcore"

[tool.pytest.ini_options]
minversion = "6.0"
addopts = "--cov=microcore --cov-report=xml:coverage.xml --cov-report=term"
testpaths = [
"tests/basic",
]
3 changes: 2 additions & 1 deletion requirements/tests.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-r extended.txt
pytest~=7.4.3
pytest-asyncio>=0.21.0
pytest-mock~=3.12.0
pytest-mock~=3.12.0
pytest-cov~=4.1.0
1 change: 0 additions & 1 deletion tests/apis/test_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from .setup_env import setup_env # noqa



def test_streaming_count(setup_env): # noqa
microcore.use_logging()
out = []
Expand Down
1 change: 0 additions & 1 deletion tests/apis/test_streaming_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from .setup_env import setup_env # noqa



@pytest.mark.asyncio
async def test_streaming_count_async(setup_env): # noqa
microcore.use_logging()
Expand Down
29 changes: 29 additions & 0 deletions tests/basic/test_storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import microcore as mc


def test_storage_read_write():
mc.storage.clean('tests_tmp')
filename = mc.storage.write("tests_tmp/test_file", "test content")
content = mc.storage.read(filename)
assert content == "test content"
mc.storage.clean('tests_tmp')



def test_storage_write_existing():
mc.storage.clean('tests_tmp')
filename = mc.storage.write("tests_tmp/test_b", "old content")
filename2 = mc.storage.write("tests_tmp/test_b", "new content")
assert mc.storage.read(filename) == "old content"
assert mc.storage.read(filename2) == "new content"
assert filename != filename2
mc.storage.clean('tests_tmp')


def test_storage_clean():

mc.storage.clean("tests_tmp")
filename = mc.storage.write("tests_tmp/file", "")
assert (mc.storage.storage_path / filename).exists()
mc.storage.clean("tests_tmp")
assert not (mc.storage.storage_path / filename).exists()
33 changes: 33 additions & 0 deletions tests/basic/test_ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from unittest.mock import patch
from microcore.ui import info, debug, error, ask_yn


def test_info():
with patch("builtins.print") as mock_print:
info("Test message")
args, _ = mock_print.call_args
assert "Test message" in args[0]


def test_debug():
with patch("builtins.print") as mock_print:
debug("Debug message")
args, _ = mock_print.call_args
assert "Debug message" in args[0]


def test_error():
with patch("builtins.print") as mock_print:
error("Error message")
args, _ = mock_print.call_args
assert "Error message" in args[0]


def test_ask_yn_yes():
with patch("builtins.input", return_value="y"):
assert ask_yn("Confirm?") is True


def test_ask_yn_no():
with patch("builtins.input", return_value="n"):
assert ask_yn("Confirm?") is False

0 comments on commit 330dcc7

Please sign in to comment.