def __init__(self): self.data = os.fspath(XDG_DATA_HOME.joinpath('quikey/')) self.config = os.fspath(XDG_CONFIG_HOME.joinpath('quikey/')) self.cache = os.fspath(XDG_CACHE_HOME.joinpath('quikey/')) os.makedirs(self.data, exist_ok=True) os.makedirs(self.config, exist_ok=True) os.makedirs(self.cache, exist_ok=True)
def __init__( self, db_file=XDG_DATA_HOME.joinpath(DEFAULT_DB_FILE), ): self.db_file = db_file create = False if not os.path.exists(self.db_file): create = True self.conn = sqlite3.connect(self.db_file) if create: cursor = self.conn.cursor() cursor.execute(GR_SETUP_TABLE) cursor.execute(GR_OAUTH_TABLE) self.conn.commit()
def __init__( self, testing=True, db_file=XDG_DATA_HOME.joinpath(DEFAULT_DB_FILE), ): # use tmpfile db for testing purposes if testing and self.testing_tmpfile is None: BossDB.testing_tmpfile = tempfile.NamedTemporaryFile().name self.db_file = db_file if not testing else self.testing_tmpfile self.conn = sqlite3.connect( self.db_file, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES, ) if not self.schema_ran: cursor = self.conn.cursor() cursor.executescript(BOSSBOT_DB_SCHEMA) self.conn.commit() BossDB.schema_ran = True
def _init_vars(self): self._execution_date = str(datetime.datetime.now()) self._are_errors = False # if script ran with errors, switch to True self._config = None self._queries_config = None self._defaults = dict() self._queries = dict() self._results = dict() self._results_txt = str() self._counts = dict() self._new_papers = dict() self._history = list() self._send_notification = bool() self._cache_dir = str(XDG_CACHE_HOME.absolute()) + "/pubmednotifier" self._check_if_folder_exists(self._cache_dir) self._data_dir = str(XDG_DATA_HOME.absolute()) + "/pubmednotifier" self._check_if_folder_exists(self._data_dir) self._history_file = self._data_dir + "/history" self._check_if_file_exists(self._history_file) self._queries_file = self._data_dir + "/queries" self._check_if_file_exists(self._queries_file) self._results_dir = self._data_dir + "/results" self._check_if_folder_exists(self._results_dir) self._new_papers_file = self._results_dir + "/results_" + self._execution_date + ".md" self._config_dir = str(XDG_CONFIG_HOME.absolute()) + "/pubmednotifier" self._check_if_folder_exists(self._config_dir) self._config_file = self._config_dir + "/config" self._log_dir = self._data_dir + "/logs" self._log_file_name = "log_" + self._execution_date self._check_if_folder_exists(self._log_dir) self._log_file = self._log_dir + "/" + self._log_file_name
class RaidenConfigurationFile: FOLDER_PATH = XDG_DATA_HOME.joinpath("raiden") def __init__(self, account: Account, network: Network, ethereum_client_rpc_endpoint: str, **kw): self.account = account self.network = network self.ethereum_client_rpc_endpoint = ethereum_client_rpc_endpoint self.accept_disclaimer = kw.get("accept_disclaimer", True) self.enable_monitoring = kw.get("enable_monitoring", settings.monitoring_enabled) self.routing_mode = kw.get("routing_mode", settings.routing_mode) @property def path_finding_service_url(self): return ( f"https://pfs-{self.network.name}.services-{settings.services_version}.raiden.network" ) @property def configuration_data(self): base_config = { "environment-type": self.environment_type, "keystore-path": str(self.account.__class__.find_keystore_folder_path()), "keystore-file-path": str(self.account.keystore_file_path), "address": to_checksum_address(self.account.address), "password-file": str(self.passphrase_file_path), "user-deposit-contract-address": get_contract_address(self.network.chain_id, CONTRACT_USER_DEPOSIT), "network-id": self.network.name, "accept-disclaimer": self.accept_disclaimer, "eth-rpc-endpoint": self.ethereum_client_rpc_endpoint, "routing-mode": self.routing_mode, "enable-monitoring": self.enable_monitoring, } if self.routing_mode == "pfs": base_config.update( {"pathfinding-service-address": self.path_finding_service_url}) return base_config @property def environment_type(self): return "production" if self.network.name == "mainnet" else "development" @property def file_name(self): return f"config-{self.account.address}-{self.network.name}.toml" @property def path(self): return self.FOLDER_PATH.joinpath(self.file_name) @property def passphrase_file_path(self): return self.FOLDER_PATH.joinpath( f"{self.account.address}.passphrase.txt") @property def ethereum_balance(self): w3 = make_web3_provider(self.ethereum_client_rpc_endpoint, self.account) return self.account.get_ethereum_balance(w3) def save(self): if not self.account.check_passphrase(self.account.passphrase): raise ValueError("no valid passphrase for account collected") self.FOLDER_PATH.mkdir(parents=True, exist_ok=True) passphrase_file = PassphraseFile(self.passphrase_file_path) passphrase_file.store(self.account.passphrase) with open(self.path, "w") as config_file: toml.dump(self.configuration_data, config_file) @classmethod def list_existing_files(cls) -> List[Path]: config_glob = str(cls.FOLDER_PATH.joinpath("config-*.toml")) return [Path(file_path) for file_path in glob.glob(config_glob)] @classmethod def get_available_configurations(cls): configurations = [] for config_file_path in cls.list_existing_files(): try: configurations.append(cls.load(config_file_path)) except (ValueError, KeyError) as exc: log.warn( f"Failed to load {config_file_path} as configuration file: {exc}" ) return configurations @classmethod def load(cls, file_path: Path): file_name, _ = os.path.splitext(os.path.basename(file_path)) _, _, network_name = file_name.split("-") with file_path.open() as config_file: data = toml.load(config_file) passphrase = PassphraseFile(Path(data["password-file"])).retrieve() account = Account(data["keystore-file-path"], passphrase=passphrase) return cls( account=account, ethereum_client_rpc_endpoint=data["eth-rpc-endpoint"], network=Network.get_by_name(network_name), routing_mode=data["routing-mode"], enable_monitoring=data["enable-monitoring"], ) @classmethod def get_by_filename(cls, file_name): file_path = cls.FOLDER_PATH.joinpath(file_name) if not file_path.exists(): raise ValueError( f"{file_path} is not a valid configuration file path") return cls.load(file_path) @classmethod def get_ethereum_rpc_endpoints(cls): endpoints = [] config_glob = glob.glob(cls.FOLDER_PATH.joinpath("*.toml")) for config_file_path in config_glob: with open(config_file_path) as config_file: data = toml.load(config_file) endpoints.append( EthereumRPCProvider.make_from_url( data["eth-rpc-endpoint"])) return endpoints
class RaidenConfigurationFile: FOLDER_PATH = XDG_DATA_HOME.joinpath("raiden") def __init__(self, account_filename: str, network: Network, ethereum_client_rpc_endpoint: str, **kw): if 'passphrase' in kw: self.account = Account(account_filename, passphrase=kw.get('passphrase')) else: self.account = Account(account_filename) self.account_filename = account_filename self.network = network self.settings = network_settings[network.name] self.ethereum_client_rpc_endpoint = ethereum_client_rpc_endpoint self.accept_disclaimer = kw.get("accept_disclaimer", True) self.enable_monitoring = kw.get("enable_monitoring", self.settings.monitoring_enabled) self.routing_mode = kw.get("routing_mode", self.settings.routing_mode) self.services_version = self.settings.services_version self._initial_funding_txhash = kw.get("_initial_funding_txhash") @property def path_finding_service_url(self): return f"https://pfs-{self.network.name}.services-{self.services_version}.raiden.network" @property def configuration_data(self): base_config = { "environment-type": self.environment_type, "keystore-path": str(self.account.__class__.find_keystore_folder_path()), "address": to_checksum_address(self.account.address), "network-id": self.network.name, "accept-disclaimer": self.accept_disclaimer, "eth-rpc-endpoint": self.ethereum_client_rpc_endpoint, "routing-mode": self.routing_mode, "enable-monitoring": self.enable_monitoring, "_initial_funding_txhash": self._initial_funding_txhash, } # If the config is for a demo-env we'll need to add/overwrite some settings if self.settings.client_release_channel == "demo_env": # noqa base_config.update({"matrix-server": self.settings.matrix_server}) # noqa base_config["routing-mode"] = "pfs" base_config[ "pathfinding-service-address"] = self.settings.pathfinding_service_address # noqa return base_config @property def environment_type(self): return "production" if self.network.name == "mainnet" else "development" @property def file_name(self): return f"config-{self.account.address}-{self.network.name}.toml" @property def path(self): return self.FOLDER_PATH.joinpath(self.file_name) @property def ethereum_balance(self): w3 = make_web3_provider(self.ethereum_client_rpc_endpoint, self.account) return self.account.get_ethereum_balance(w3) def save(self): self.FOLDER_PATH.mkdir(parents=True, exist_ok=True) with open(self.path, "w") as config_file: toml.dump(self.configuration_data, config_file) @classmethod def list_existing_files(cls) -> List[Path]: config_glob = str(cls.FOLDER_PATH.joinpath("config-*.toml")) return [Path(file_path) for file_path in glob.glob(config_glob)] @classmethod def get_available_configurations(cls): configurations = [] for config_file_path in cls.list_existing_files(): try: configurations.append(cls.load(config_file_path)) except (ValueError, KeyError) as exc: log.warn( f"Failed to load {config_file_path} as configuration file: {exc}" ) return configurations @classmethod def load(cls, file_path: Path): file_name, _ = os.path.splitext(os.path.basename(file_path)) _, _, network_name = file_name.split("-") with file_path.open() as config_file: data = toml.load(config_file) keystore_file_path = Account.find_keystore_file_path( data["address"], Path(data["keystore-path"])) return cls( account_filename=keystore_file_path, ethereum_client_rpc_endpoint=data["eth-rpc-endpoint"], network=Network.get_by_name(network_name), routing_mode=data["routing-mode"], enable_monitoring=data["enable-monitoring"], _initial_funding_txhash=data.get("_initial_funding_txhash"), ) @classmethod def get_by_filename(cls, file_name): file_path = cls.FOLDER_PATH.joinpath(file_name) if not file_path.exists(): raise ValueError( f"{file_path} is not a valid configuration file path") return cls.load(file_path) @classmethod def get_ethereum_rpc_endpoints(cls): endpoints = [] config_glob = glob.glob(cls.FOLDER_PATH.joinpath("*.toml")) for config_file_path in config_glob: with open(config_file_path) as config_file: data = toml.load(config_file) endpoints.append( EthereumRPCProvider.make_from_url( data["eth-rpc-endpoint"])) return endpoints
class RaidenConfigurationFile: FOLDER_PATH = XDG_DATA_HOME.joinpath("raiden") def __init__(self, account_filename: Union[Path, str], settings: Settings, ethereum_client_rpc_endpoint: str, **kw): self.account = Account(account_filename) self.account_filename = account_filename self.settings = settings self.network = Network.get_by_name(self.settings.network) self.ethereum_client_rpc_endpoint = ethereum_client_rpc_endpoint self.accept_disclaimer = kw.get("accept_disclaimer", True) self.enable_monitoring = kw.get("enable_monitoring", self.settings.monitoring_enabled) self.routing_mode = kw.get("routing_mode", self.settings.routing_mode) self.services_version = self.settings.services_version self._initial_funding_txhash = kw.get("_initial_funding_txhash") @property def configuration_data(self): base_config = { "environment-type": self.environment_type, "keystore-path": str(self.account.keystore_file_path.parent), "address": to_checksum_address(self.account.address), "network-id": self.network.name, "accept-disclaimer": self.accept_disclaimer, "eth-rpc-endpoint": self.ethereum_client_rpc_endpoint, "routing-mode": self.routing_mode, "enable-monitoring": self.enable_monitoring, "_initial_funding_txhash": self._initial_funding_txhash, } # If the config is for a demo-env we'll need to add/overwrite some settings if self.settings.client_release_channel == "demo_env": base_config.update({ "matrix-server": self.settings.matrix_server, "routing-mode": "pfs", "pathfinding-service-address": self.settings.pathfinding_service_address, }) return base_config @property def environment_type(self): return "production" if self.network.name == "mainnet" else "development" @property def file_name(self): return f"config-{to_checksum_address(self.account.address)}-{self.settings.name}.toml" @property def path(self): return self.FOLDER_PATH.joinpath(self.file_name) def save(self): self.FOLDER_PATH.mkdir(parents=True, exist_ok=True) with open(self.path, "w") as config_file: toml.dump(self.configuration_data, config_file) @classmethod def list_existing_files(cls, settings: Settings) -> List[Path]: config_glob = str( cls.FOLDER_PATH.joinpath(f"config-*-{settings.name}.toml")) return [Path(file_path) for file_path in glob.glob(config_glob)] @classmethod def get_available_configurations(cls, settings: Settings): configurations = [] for config_file_path in cls.list_existing_files(settings): try: configurations.append(cls.load(config_file_path)) except (ValueError, KeyError) as exc: log.warn( f"Failed to load {config_file_path} as configuration file: {exc}" ) return configurations @classmethod def load(cls, file_path: Path): file_name, _ = os.path.splitext(os.path.basename(file_path)) _, _, settings_name = file_name.split("-") try: settings = load_settings(settings_name) except FileNotFoundError as exc: raise ValueError( f"There are no Wizard settings {settings_name} for Raiden configuration {file_path}" ) with file_path.open() as config_file: data = toml.load(config_file) keystore_file_path = Account.find_keystore_file_path( to_canonical_address(data["address"]), Path(data["keystore-path"])) if keystore_file_path is None: raise ValueError( f"{data['keystore-path']} does not contain the account file for config {file_path}" ) return cls( account_filename=keystore_file_path, ethereum_client_rpc_endpoint=data["eth-rpc-endpoint"], settings=settings, routing_mode=data["routing-mode"], enable_monitoring=data["enable-monitoring"], _initial_funding_txhash=data.get("_initial_funding_txhash"), ) @classmethod def get_by_filename(cls, file_name): file_path = cls.FOLDER_PATH.joinpath(file_name) if not file_path.exists(): raise ValueError( f"{file_path} is not a valid configuration file path") return cls.load(file_path)
class RaidenConfigurationFile: FOLDER_PATH = XDG_DATA_HOME.joinpath("raiden") def __init__(self, account: Account, network: Network, ethereum_client_rpc_endpoint: str, **kw): self.account = account self.network = network self.ethereum_client_rpc_endpoint = ethereum_client_rpc_endpoint self.accept_disclaimer = kw.get("accept_disclaimer", True) self.enable_monitoring = kw.get("enable_monitoring", True) self.routing_mode = kw.get("routing_mode", "pfs") self.environment_type = kw.get("environment_type", "development") def save(self): if not self.account.check_passphrase(self.account.passphrase): raise ValueError("no valid passphrase for account collected") self.FOLDER_PATH.mkdir(parents=True, exist_ok=True) passphrase_file = PassphraseFile(self.passphrase_file_path) passphrase_file.store(self.account.passphrase) with open(self.path, "w") as config_file: toml.dump(self.configuration_data, config_file) @property def path_finding_service_url(self): return f"https://pfs-{self.network.name}-with-fee.services-dev.raiden.network" @property def configuration_data(self): base_config = { "environment-type": self.environment_type, "keystore-path": str(self.account.__class__.find_keystore_folder_path()), "keystore-file-path": str(self.account.keystore_file_path), "address": to_checksum_address(self.account.address), "password-file": str(self.passphrase_file_path), "user-deposit-contract-address": self.network.get_contract_address(CONTRACT_USER_DEPOSIT), "network-id": self.network.name, "accept-disclaimer": self.accept_disclaimer, "eth-rpc-endpoint": self.ethereum_client_rpc_endpoint, "routing-mode": self.routing_mode, "enable-monitoring": self.enable_monitoring, } if self.routing_mode == "pfs": base_config.update( {"pathfinding-service-address": self.path_finding_service_url}) return base_config @property def file_name(self): return f"config-{self.account.address}-{self.network.name}.toml" @property def path(self): return self.FOLDER_PATH.joinpath(self.file_name) @property def passphrase_file_path(self): return self.FOLDER_PATH.joinpath( f"{self.account.address}.passphrase.txt") @property def balance(self): return self.account.get_balance(self.ethereum_client_rpc_endpoint) @property def is_launchable(self): return self.balance >= self.network.MINIMUM_ETHEREUM_BALANCE_REQUIRED @property def short_description(self): account_description = f"Account {self.account.address} (Balance: {self.balance})" network_description = f"{self.network.name} via {self.ethereum_client_rpc_endpoint}" return " - ".join( (str(self.path), account_description, network_description)) @classmethod def get_available_configurations(cls): config_glob = str(cls.FOLDER_PATH.joinpath("config-*.toml")) configurations = [] for config_file_path in glob.glob(config_glob): try: configurations.append(cls.load(Path(config_file_path))) except (ValueError, KeyError) as exc: logger.warn( f"Failed to load {config_file_path} as configuration file: {exc}" ) return configurations @classmethod def load(cls, file_path: Path): file_name, _ = os.path.splitext(os.path.basename(file_path)) _, _, network_name = file_name.split("-") with file_path.open() as config_file: data = toml.load(config_file) passphrase = PassphraseFile(Path(data["password-file"])).retrieve() account = Account(data["keystore-file-path"], passphrase=passphrase) return cls( account=account, ethereum_client_rpc_endpoint=data["eth-rpc-endpoint"], network=Network.get_by_name(network_name), routing_mode=data["routing-mode"], enable_monitoring=data["enable-monitoring"], ) @classmethod def get_by_filename(cls, file_name): return cls.load(cls.FOLDER_PATH.joinpath(file_name)) @classmethod def get_launchable_configurations(cls): return [ cfg for cfg in cls.get_available_configurations() if cfg.is_launchable ] @classmethod def get_ethereum_rpc_endpoints(cls): endpoints = [] config_glob = glob.glob(cls.FOLDER_PATH.joinpath("*.toml")) for config_file_path in config_glob: with open(config_file_path) as config_file: data = toml.load(config_file) endpoints.append( EthereumRPCProvider.make_from_url( data["eth-rpc-endpoint"])) return endpoints