Browse Source

globalvars.py: implement `data_dir` and `data_dir_root` as properties

The MMGen Project 2 years ago
parent
commit
a385c98c17
2 changed files with 34 additions and 33 deletions
  1. 32 1
      mmgen/globalvars.py
  2. 2 32
      mmgen/opts.py

+ 32 - 1
mmgen/globalvars.py

@@ -148,7 +148,6 @@ class GlobalConfig(Lockable):
 	else:
 		die(2,'$HOME is not set!  Unable to determine home directory')
 
-	data_dir_root,data_dir = (None,None)
 	daemon_data_dir = '' # set by user
 	daemon_id = ''
 	blacklisted_daemons = ''
@@ -360,4 +359,36 @@ class GlobalConfig(Lockable):
 	def release_date(self):
 		return self.get_mmgen_data_file(filename='release_date').strip()
 
+	@property
+	def data_dir_root(self):
+		"""
+		location of mmgen.cfg
+		"""
+		if hasattr(self,'_data_dir_root'):
+			return self._data_dir_root
+		else:
+			if self.data_dir_root_override:
+				self._data_dir_root = os.path.normpath(os.path.abspath(self.data_dir_root_override))
+			elif self.test_suite:
+				from test.include.common import get_test_data_dir
+				self._data_dir_root = get_test_data_dir()
+			else:
+				self._data_dir_root = os.path.join(self.home_dir,'.'+self.proj_name.lower())
+			return self._data_dir_root
+
+	@property
+	def data_dir(self):
+		"""
+		location of wallet and other data - same as data_dir_root for mainnet
+		"""
+		if hasattr(self,'_data_dir'):
+			return self._data_dir
+		else:
+			self._data_dir = os.path.normpath(os.path.join(*{
+				'regtest': (self.data_dir_root, 'regtest', self.coin.lower(), (self.regtest_user or 'none') ),
+				'testnet': (self.data_dir_root, 'testnet'),
+				'mainnet': (self.data_dir_root,),
+			}[self.network] ))
+			return self._data_dir
+
 g = GlobalConfig()

+ 2 - 32
mmgen/opts.py

@@ -343,24 +343,8 @@ def init(
 
 	env_globals = tuple(override_globals_from_env())
 
-	"""
-	NB: user opt --data-dir is actually data_dir_root
-	- data_dir is data_dir_root plus optionally 'regtest' or 'testnet', so for mainnet
-	  data_dir == data_dir_root
-	- As with Bitcoin Core, cfg file is in data_dir_root, wallets and other data are
-	  in data_dir
-	- Since cfg file is in data_dir_root, data_dir_root must be finalized before we
-	  can process cfg file
-	- Since data_dir depends on the values of g.testnet and g.regtest, these must be
-	  finalized before setting data_dir
-	"""
-	if opt.data_dir:
-		g.data_dir_root = os.path.normpath(os.path.abspath(opt.data_dir))
-	elif g.test_suite:
-		from test.include.common import get_test_data_dir
-		g.data_dir_root = get_test_data_dir()
-	else:
-		g.data_dir_root = os.path.join(g.home_dir,'.'+g.proj_name.lower())
+	# --data-dir overrides computed value of data_dir_root
+	g.data_dir_root_override = opt.data_dir
 
 	from .fileutil import check_or_create_dir
 	check_or_create_dir(g.data_dir_root)
@@ -403,13 +387,6 @@ def init(
 		from .color import init_color
 		init_color(num_colors=('auto',256)[bool(g.force_256_color)])
 
-	"""
-	g.testnet and g.regtest are finalized, so we can set g.data_dir
-	"""
-	g.data_dir = os.path.normpath(os.path.join(
-		g.data_dir_root,
-		('regtest' if g.regtest else 'testnet' if g.testnet else '') ))
-
 	# Set user opts from globals:
 	# - if opt is unset, set it to global value
 	# - if opt is set, convert its type to that of global value
@@ -419,13 +396,6 @@ def init(
 		else:
 			setattr(opt,k,getattr(g,k))
 
-	if g.network == 'regtest':
-		g.data_dir = os.path.join(
-			g.data_dir_root,
-			'regtest',
-			g.coin.lower(),
-			(g.regtest_user or 'none') )
-
 	if need_proto:
 		from .protocol import warn_trustlevel
 		warn_trustlevel(g.coin)