Cleanup lost utility

pull/1983/head
Kieran Prasch 2020-05-12 13:34:32 -07:00
parent f90b83ed86
commit 0b1c118441
No known key found for this signature in database
GPG Key ID: 199AB839D4125A62
4 changed files with 53 additions and 51 deletions

View File

@ -18,6 +18,9 @@ along with nucypher. If not, see <https://www.gnu.org/licenses/>.
import json
import os
import re
import click
from json.decoder import JSONDecodeError
@ -30,6 +33,7 @@ from nucypher.cli.actions.literature import (
SUCCESSFUL_FORGET_NODES, INVALID_CONFIGURATION_FILE_WARNING, INVALID_JSON_IN_CONFIGURATION_WARNING,
SUCCESSFUL_UPDATE_CONFIGURATION_VALUES
)
from nucypher.config.characters import UrsulaConfiguration
def get_or_update_configuration(emitter, config_class, filepath: str, config_options):
@ -99,3 +103,40 @@ def get_provider_process(start_now: bool = False):
if start_now:
process.start()
return process
def extract_checksum_address_from_filepath(filepath, config_class=UrsulaConfiguration):
pattern = re.compile(r'''
(^\w+)-
(0x{1} # Then, 0x the start of the string, exactly once
[0-9a-fA-F]{40}) # Followed by exactly 40 hex chars
''',
re.VERBOSE)
filename = os.path.basename(filepath)
match = pattern.match(filename)
if match:
character_name, checksum_address = match.groups()
else:
# Extract from default by "peeking" inside the configuration file.
default_name = config_class.generate_filename()
if filename == default_name:
checksum_address = config_class.peek(filepath=filepath, field='checksum_address')
###########
# TODO: Cleanup and deprecate worker_address in config files, leaving only checksum_address
if config_class == UrsulaConfiguration:
federated = bool(config_class.peek(filepath=filepath, field='federated_only'))
if not federated:
checksum_address = config_class.peek(filepath=filepath, field='worker_address')
###########
else:
raise ValueError(f"Cannot extract checksum from filepath '{filepath}'")
if not is_checksum_address(checksum_address):
raise RuntimeError(f"Invalid checksum address detected in configuration file at '{filepath}'.")
return checksum_address

View File

@ -31,7 +31,7 @@ from nucypher.blockchain.eth.networks import NetworksInventory
from nucypher.blockchain.eth.registry import InMemoryContractRegistry, IndividualAllocationRegistry
from nucypher.blockchain.eth.signers import Signer
from nucypher.blockchain.eth.token import Stake, NU
from nucypher.cli.config import extract_checksum_address_from_filepath
from nucypher.cli.actions.config import extract_checksum_address_from_filepath
from nucypher.cli.actions.literature import (
NO_CONFIGURATIONS_ON_DISK,
SELECT_NETWORK,

View File

@ -15,7 +15,7 @@ You should have received a copy of the GNU Affero General Public License
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
"""
from distutils.util import strtobool
import click
import os
@ -156,3 +156,12 @@ def connect_to_blockchain(provider_uri, emitter, debug: bool = False, light: boo
raise
emitter.echo(str(e), bold=True, color='red')
raise click.Abort
def get_env_bool(var_name: str, default: bool) -> bool:
if var_name in os.environ:
# TODO: which is better: to fail on an incorrect envvar, or to use the default?
# Currently doing the former.
return strtobool(os.environ[var_name])
else:
return default

View File

@ -15,29 +15,18 @@ You should have received a copy of the GNU Affero General Public License
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
"""
from distutils.util import strtobool
import click
import os
import re
from twisted.logger import Logger
from nucypher.characters.control.emitters import StdoutEmitter, JSONRPCStdoutEmitter
from nucypher.cli.actions.utils import get_env_bool
from nucypher.cli.options import group_options
from nucypher.config.characters import UrsulaConfiguration
from nucypher.config.constants import NUCYPHER_SENTRY_ENDPOINT
from nucypher.utilities.logging import GlobalLoggerSettings
def get_env_bool(var_name: str, default: bool) -> bool:
if var_name in os.environ:
# TODO: which is better: to fail on an incorrect envvar, or to use the default?
# Currently doing the former.
return strtobool(os.environ[var_name])
else:
return default
class GroupGeneralConfig:
__option_name__ = 'general_config'
@ -157,40 +146,3 @@ group_general_config = group_options(
"and turns off Sentry logging.",
is_flag=True),
)
def extract_checksum_address_from_filepath(filepath, config_class=UrsulaConfiguration):
pattern = re.compile(r'''
(^\w+)-
(0x{1} # Then, 0x the start of the string, exactly once
[0-9a-fA-F]{40}) # Followed by exactly 40 hex chars
''',
re.VERBOSE)
filename = os.path.basename(filepath)
match = pattern.match(filename)
if match:
character_name, checksum_address = match.groups()
else:
# Extract from default by "peeking" inside the configuration file.
default_name = config_class.generate_filename()
if filename == default_name:
checksum_address = config_class.peek(filepath=filepath, field='checksum_address')
###########
# TODO: Cleanup and deprecate worker_address in config files, leaving only checksum_address
if config_class == UrsulaConfiguration:
federated = bool(config_class.peek(filepath=filepath, field='federated_only'))
if not federated:
checksum_address = config_class.peek(filepath=filepath, field='worker_address')
###########
else:
raise ValueError(f"Cannot extract checksum from filepath '{filepath}'")
if not is_checksum_address(checksum_address):
raise RuntimeError(f"Invalid checksum address detected in configuration file at '{filepath}'.")
return checksum_address