Skip to content

Commit

Permalink
Add a Factory class to help create objects needed by Poetry
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater committed Sep 2, 2019
1 parent 8c0e1d6 commit c2b4386
Show file tree
Hide file tree
Showing 27 changed files with 687 additions and 483 deletions.
25 changes: 24 additions & 1 deletion poetry/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
from poetry.utils._compat import Path
from poetry.utils._compat import basestring

from .config_source import ConfigSource
from .dict_config_source import DictConfigSource

_NOT_SET = object()

boolean_validator = lambda val: val in {"true", "false", "1", "0"}
boolean_normalizer = lambda val: val in ["true", "1"]


class Config:
class Config(object):

default_config = {
"cache-dir": str(CACHE_DIR),
Expand All @@ -34,6 +37,8 @@ def __init__(
self._config = deepcopy(self.default_config)
self._use_environment = use_environment
self._base_dir = base_dir
self._config_source = DictConfigSource()
self._auth_config_source = DictConfigSource()

@property
def name(self):
Expand All @@ -43,6 +48,24 @@ def name(self):
def config(self):
return self._config

@property
def config_source(self): # type: () -> ConfigSource
return self._config_source

@property
def auth_config_source(self): # type: () -> ConfigSource
return self._auth_config_source

def set_config_source(self, config_source): # type: (ConfigSource) -> Config
self._config_source = config_source

return self

def set_auth_config_source(self, config_source): # type: (ConfigSource) -> Config
self._auth_config_source = config_source

return self

def merge(self, config): # type: (Dict[str, Any]) -> None
from poetry.utils.helpers import merge_dicts

Expand Down
77 changes: 3 additions & 74 deletions poetry/config/config_source.py
Original file line number Diff line number Diff line change
@@ -1,80 +1,9 @@
import io
import os
from contextlib import contextmanager
from typing import Any

from tomlkit import document
from tomlkit import table

from poetry.utils.toml_file import TomlFile


class ConfigSource:
def __init__(self, file, auth_config=False): # type: (TomlFile, bool) -> None
self._file = file
self._auth_config = auth_config

@property
def name(self): # type: () -> str
return str(self._file.path)

@property
def file(self): # type: () -> TomlFile
return self._file

class ConfigSource(object):
def add_property(self, key, value): # type: (str, Any) -> None
with self.secure() as config:
keys = key.split(".")

for i, key in enumerate(keys):
if key not in config and i < len(keys) - 1:
config[key] = table()

if i == len(keys) - 1:
config[key] = value
break

config = config[key]
raise NotImplementedError()

def remove_property(self, key): # type: (str) -> None
with self.secure() as config:
keys = key.split(".")

current_config = config
for i, key in enumerate(keys):
if key not in current_config:
return

if i == len(keys) - 1:
del current_config[key]

break

current_config = current_config[key]

@contextmanager
def secure(self):
if self.file.exists():
initial_config = self.file.read()
config = self.file.read()
else:
initial_config = document()
config = document()

new_file = not self.file.exists()

yield config

try:
# Ensuring the file is only readable and writable
# by the current user
mode = 0o600

if new_file:
self.file.touch(mode=mode)

self.file.write(config)
except Exception:
self.file.write(initial_config)

raise
raise NotImplementedError()
42 changes: 42 additions & 0 deletions poetry/config/dict_config_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import Any
from typing import Dict

from .config_source import ConfigSource


class DictConfigSource(ConfigSource):
def __init__(self): # type: () -> None
self._config = {}

@property
def config(self): # type: () -> Dict[str, Any]
return self._config

def add_property(self, key, value): # type: (str, Any) -> None
keys = key.split(".")
config = self._config

for i, key in enumerate(keys):
if key not in config and i < len(keys) - 1:
config[key] = {}

if i == len(keys) - 1:
config[key] = value
break

config = config[key]

def remove_property(self, key): # type: (str) -> None
keys = key.split(".")

config = self._config
for i, key in enumerate(keys):
if key not in config:
return

if i == len(keys) - 1:
del config[key]

break

config = config[key]
80 changes: 80 additions & 0 deletions poetry/config/file_config_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from contextlib import contextmanager
from typing import Any

from tomlkit import document
from tomlkit import table

from poetry.utils.toml_file import TomlFile

from .config_source import ConfigSource


class FileConfigSource(ConfigSource):
def __init__(self, file, auth_config=False): # type: (TomlFile, bool) -> None
self._file = file
self._auth_config = auth_config

@property
def name(self): # type: () -> str
return str(self._file.path)

@property
def file(self): # type: () -> TomlFile
return self._file

def add_property(self, key, value): # type: (str, Any) -> None
with self.secure() as config:
keys = key.split(".")

for i, key in enumerate(keys):
if key not in config and i < len(keys) - 1:
config[key] = table()

if i == len(keys) - 1:
config[key] = value
break

config = config[key]

def remove_property(self, key): # type: (str) -> None
with self.secure() as config:
keys = key.split(".")

current_config = config
for i, key in enumerate(keys):
if key not in current_config:
return

if i == len(keys) - 1:
del current_config[key]

break

current_config = current_config[key]

@contextmanager
def secure(self):
if self.file.exists():
initial_config = self.file.read()
config = self.file.read()
else:
initial_config = document()
config = document()

new_file = not self.file.exists()

yield config

try:
# Ensuring the file is only readable and writable
# by the current user
mode = 0o600

if new_file:
self.file.touch(mode=mode)

self.file.write(config)
except Exception:
self.file.write(initial_config)

raise
5 changes: 3 additions & 2 deletions poetry/console/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ def __init__(self):

@property
def poetry(self):
from poetry.poetry import Poetry
from poetry.factory import Factory
from poetry.utils._compat import Path

if self._poetry is not None:
return self._poetry

self._poetry = Poetry.create(os.getcwd())
self._poetry = Factory().create_poetry(Path.cwd())

return self._poetry

Expand Down
6 changes: 3 additions & 3 deletions poetry/console/commands/check.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from poetry.poetry import Poetry
from poetry.factory import Factory
from poetry.utils._compat import Path
from poetry.utils.toml_file import TomlFile

Expand All @@ -12,9 +12,9 @@ class CheckCommand(Command):

def handle(self):
# Load poetry config and display errors, if any
poetry_file = Poetry.locate(Path.cwd())
poetry_file = Factory.locate(Path.cwd())
config = TomlFile(str(poetry_file)).read()["tool"]["poetry"]
check_result = Poetry.check(config, strict=True)
check_result = Factory.validate(config, strict=True)
if not check_result["errors"] and not check_result["warnings"]:
self.info("All set!")

Expand Down
Loading

0 comments on commit c2b4386

Please sign in to comment.