-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Factory class to help create objects needed by Poetry (#1349)
- Loading branch information
Showing
27 changed files
with
695 additions
and
483 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.