Skip to content

Commit

Permalink
refactor(data): Move all Data Paths to Data Module
Browse files Browse the repository at this point in the history
  • Loading branch information
BradenM committed Oct 5, 2019
1 parent 7db8014 commit a0df27b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
28 changes: 26 additions & 2 deletions micropy/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,32 @@

from pathlib import Path

__all__ = [
"ROOT",
"SCHEMAS",
"REPO_SOURCES",
"FILES",
"STUB_DIR",
"LOG_FILE",
"STUBBER"
]

# Paths
MOD_PATH = Path(__file__).parent
PATH = MOD_PATH.resolve()
PATH = MOD_PATH.absolute()
ROOT = MOD_PATH.parent.absolute()

# Stub Schemas
SCHEMAS = PATH / 'schemas'

__all__ = ["PATH", "SCHEMAS"]
# Default Stub Sources
REPO_SOURCES = PATH / 'sources.json'

# Application Data
FILES = Path.home() / '.micropy'
STUB_DIR = FILES / 'stubs'
LOG_FILE = FILES / 'micropy.log'

# Libraries
LIB = ROOT / 'lib'
STUBBER = LIB / 'stubber'
5 changes: 3 additions & 2 deletions micropy/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import re
from contextlib import contextmanager
from logging.handlers import RotatingFileHandler
from pathlib import Path

import click

from micropy import data


class Log:
"""Borg for easy access to any Log from anywhere in the package"""
Expand Down Expand Up @@ -48,7 +49,7 @@ class ServiceLog:
:type base_color: str
"""
LOG_FILE = Path.home() / '.micropy' / 'micropy.log'
LOG_FILE = data.LOG_FILE

def __init__(
self, service_name='MicroPy', base_color='bright_green', **kwargs):
Expand Down
18 changes: 6 additions & 12 deletions micropy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,26 @@

class MicroPy:
"""Handles App State Management"""
LIB = Path(__file__).parent / 'lib'
STUBBER = LIB / 'stubber'
FILES = Path.home() / '.micropy'
STUB_DIR = FILES / 'stubs'

REPOS = data.PATH / 'sources.json'

def __init__(self):
self.log = Log.get_logger('MicroPy')
self.verbose = True
self.log.debug("\n---- MicropyCLI Session ----")
if not self.STUB_DIR.exists():
if not data.STUB_DIR.exists():
self.setup()

def setup(self):
"""creates necessary directories for micropy"""
self.log.debug("Running first time setup...")
self.log.debug(f"Creating .micropy directory @ {self.FILES}")
self.FILES.mkdir(exist_ok=True)
self.STUB_DIR.mkdir()
self.log.debug(f"Creating .micropy directory @ {data.FILES}")
data.FILES.mkdir(exist_ok=True)
data.STUB_DIR.mkdir()

@utils.lazy_property
def stubs(self):
repo_list = self.REPOS.read_text()
repo_list = data.REPO_SOURCES.read_text()
repos = source.StubRepo.from_json(repo_list)
return StubManager(resource=self.STUB_DIR, repos=repos)
return StubManager(resource=data.STUB_DIR, repos=repos)

@utils.lazy_property
def project(self):
Expand Down
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ def ask(self):


@pytest.fixture
def mock_micropy_path(monkeypatch, tmp_path):
def mock_micropy_path(mocker, tmp_path):
path = tmp_path / '.micropy'
stub_path = path / 'stubs'
log_path = path / 'micropy.log'
monkeypatch.setattr(micropy.logger.ServiceLog, 'LOG_FILE', log_path)
monkeypatch.setattr(micropy.main.MicroPy, 'FILES', path)
monkeypatch.setattr(micropy.main.MicroPy, 'STUB_DIR', stub_path)
mocker.patch("micropy.data.FILES", path)
mocker.patch("micropy.data.STUB_DIR", stub_path)
mocker.patch("micropy.data.LOG_FILE", log_path)
return path


Expand Down
10 changes: 4 additions & 6 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest

import micropy.exceptions as exc
from micropy import main
from micropy import data, main
from micropy.stubs import stubs


Expand All @@ -16,8 +16,6 @@ def test_setup(mock_micropy_path):
expect_mp_dir = mock_micropy_path
expect_stubs_dir = mock_micropy_path / 'stubs'
mp = main.MicroPy()
print("MP FILES:", mp.FILES)
print("MP Content:", list(mp.FILES.iterdir()))
assert expect_mp_dir.exists()
assert expect_stubs_dir.exists()
# Test after inital setup
Expand All @@ -30,10 +28,10 @@ def test_add_stub(mock_micropy, shared_datadir):
fware_path = shared_datadir / 'fware_test_stub'
stub_path = shared_datadir / 'esp8266_test_stub'
stubs = mock_micropy.stubs
fware_stub = stubs.add(fware_path, mock_micropy.STUB_DIR)
stub = stubs.add(stub_path, mock_micropy.STUB_DIR)
fware_stub = stubs.add(fware_path, data.STUB_DIR)
stub = stubs.add(stub_path, data.STUB_DIR)
assert stub in list(mock_micropy.stubs)
assert stub.path in mock_micropy.STUB_DIR.iterdir()
assert stub.path in data.STUB_DIR.iterdir()
assert stub.path.exists()
assert fware_stub in list(mock_micropy.stubs._firmware)

Expand Down

0 comments on commit a0df27b

Please sign in to comment.