def dcrd(netParams): """ Attempt to fetch the dcrd configuration settings. Values will be parsed for 'rpcuser', 'rpcpass', 'rpclisten', and 'rpccert'. The setting for 'rpcuser' must be present in the file. If values are not found for 'rpccert' or 'rpclisten', default locations are populated. Args: netParams (bool): The network parameters. Returns: dict or None: The parsed configuration settings. """ dcrdCfgDir = helpers.appDataDir("dcrd") cfgPath = os.path.join(dcrdCfgDir, "dcrd.conf") if not os.path.isfile(cfgPath): return {} dcrdCfg = helpers.readINI(cfgPath, ["rpclisten", "rpcuser", "rpcpass", "rpccert"]) if "rpcuser" not in dcrdCfg: return None if "rpccert" not in dcrdCfg: dcrdCfg["rpccert"] = os.path.join(dcrdCfgDir, "rpc.cert") # Tinywallet uses the protocol (scheme) of the URL for now. if "rpclisten" in dcrdCfg: dcrdCfg["rpclisten"] = "https://" + dcrdCfg["rpclisten"] else: dcrdCfg[ "rpclisten"] = f"https://localhost:{nets.DcrdPorts[netParams.Name]}" return dcrdCfg
def dcrdConfig(): dcrdCfgDir = helpers.appDataDir("dcrd") cfgPath = os.path.join(dcrdCfgDir, "dcrd.conf") if not os.path.isfile(cfgPath): return None cfg = helpers.readINI(cfgPath, ["rpcuser", "rpcpass", "rpccert"]) assert "rpcuser" in cfg assert "rpcpass" in cfg if "rpccert" not in cfg: cfg["rpccert"] = os.path.join(dcrdCfgDir, "rpc.cert") if "rpclisten" not in cfg: cfg["rpclisten"] = "localhost:9109" return cfg
def dcrd(netParams): """ Attempt to fetch the dcrd configuration settings. Values will be parsed for 'notls', 'rpcuser', 'rpcpass', 'rpclisten', and 'rpccert'. The setting for 'rpcuser' must be present in the file. If values are not found for 'rpccert' or 'rpclisten', default locations are populated. Args: netParams (bool): The network parameters. Returns: dict or None: The parsed configuration settings. """ dcrdCfgDir = helpers.appDataDir("dcrd") cfgPath = os.path.join(dcrdCfgDir, "dcrd.conf") if not os.path.isfile(cfgPath): return {} dcrdCfg = helpers.readINI( cfgPath, ["notls", "rpclisten", "rpcuser", "rpcpass", "rpccert"]) if "rpcuser" not in dcrdCfg: return None if "rpccert" not in dcrdCfg: dcrdCfg["rpccert"] = os.path.join(dcrdCfgDir, "rpc.cert") rpcHost = "localhost" rpcPort = nets.DcrdPorts[netParams.Name] if "rpclisten" in dcrdCfg: try: rpcUrl = urlparse(f"//{dcrdCfg['rpclisten']}") if rpcUrl.hostname: rpcHost = rpcUrl.hostname.replace("::1", "").strip(":") or rpcHost if rpcUrl.port: rpcPort = rpcUrl.port except ValueError: pass dcrdCfg["rpclisten"] = f"{rpcHost}:{rpcPort}" dcrdCfg["notls"] = dcrdCfg["notls"] == "1" if "notls" in dcrdCfg else False return dcrdCfg
def test_appDataDir(monkeypatch): """ Tests appDataDir to ensure it gives expected results for various operating systems. Test adapted from dcrd TestAppDataDir. """ # App name plus upper and lowercase variants. appName = "myapp" appNameUpper = appName.capitalize() appNameLower = appName # Get the home directory to use for testing expected results. homeDir = Path.home() # When we're on Windows, set the expected local and roaming directories # per the environment vars. When we aren't on Windows, the function # should return the current directory when forced to provide the # Windows path since the environment variables won't exist. winLocal = "." currentOS = platform.system() if currentOS == "Windows": localAppData = os.getenv("LOCALAPPDATA") winLocal = Path(localAppData, appNameUpper) else: # This is kinda cheap, since this is exactly what the function does. # But it's all I got to pass testing when testing OS is not Windows. winLocal = AppDirs(appNameUpper, "").user_data_dir # Mac app data directory. macAppData = homeDir / "Library" / "Application Support" posixPath = Path(homeDir, "." + appNameLower) macPath = Path(macAppData, appNameUpper) """ Tests are 3-tuples: opSys (str): Operating system. appName (str): The appDataDir argument. want (str): The expected result """ tests = [ # Various combinations of application name casing, leading # period, operating system, and roaming flags. ("Windows", appNameLower, winLocal), ("Windows", appNameUpper, winLocal), ("Windows", "." + appNameLower, winLocal), ("Windows", "." + appNameUpper, winLocal), ("Linux", appNameLower, posixPath), ("Linux", appNameUpper, posixPath), ("Linux", "." + appNameLower, posixPath), ("Linux", "." + appNameUpper, posixPath), ("Darwin", appNameLower, macPath), ("Darwin", appNameUpper, macPath), ("Darwin", "." + appNameLower, macPath), ("Darwin", "." + appNameUpper, macPath), ("OpenBSD", appNameLower, posixPath), ("OpenBSD", appNameUpper, posixPath), ("OpenBSD", "." + appNameLower, posixPath), ("OpenBSD", "." + appNameUpper, posixPath), ("FreeBSD", appNameLower, posixPath), ("FreeBSD", appNameUpper, posixPath), ("FreeBSD", "." + appNameLower, posixPath), ("FreeBSD", "." + appNameUpper, posixPath), ("NetBSD", appNameLower, posixPath), ("NetBSD", appNameUpper, posixPath), ("NetBSD", "." + appNameLower, posixPath), ("NetBSD", "." + appNameUpper, posixPath), ("unrecognized", appNameLower, posixPath), ("unrecognized", appNameUpper, posixPath), ("unrecognized", "." + appNameLower, posixPath), ("unrecognized", "." + appNameUpper, posixPath), # No application name provided, so expect current directory. ("Windows", "", "."), ("Linux", "", "."), ("Darwin", "", "."), ("OpenBSD", "", "."), ("FreeBSD", "", "."), ("NetBSD", "", "."), ("unrecognized", "", "."), # Single dot provided for application name, so expect current # directory. ("Windows", ".", "."), ("Linux", ".", "."), ("Darwin", ".", "."), ("OpenBSD", ".", "."), ("FreeBSD", ".", "."), ("NetBSD", ".", "."), ("unrecognized", ".", "."), ] def testplatform(): return opSys monkeypatch.setattr(platform, "system", testplatform) for opSys, name, want in tests: ret = helpers.appDataDir(name) assert str(want) == str(ret), (opSys, name, want) def testexpanduser(s): return "" def testgetenv(s): return "" opSys = "Linux" monkeypatch.setattr(os.path, "expanduser", testexpanduser) monkeypatch.setattr(os, "getenv", testgetenv) assert helpers.appDataDir(appName) == "."