Esempio n. 1
0
def check_opts(usr_opts):       # Returns false if any check fails

	def opt_splits(val,sep,n,desc):
		sepword = "comma" if sep == "," else (
					"colon" if sep == ":" else ("'"+sep+"'"))
		try: l = val.split(sep)
		except:
			msg("'%s': invalid %s (not %s-separated list)" % (val,desc,sepword))
			return False

		if len(l) == n: return True
		else:
			msg("'%s': invalid %s (%s %s-separated items required)" %
					(val,desc,n,sepword))
			return False

	def opt_compares(val,op,target,desc,what=""):
		if what: what += " "
		if not eval("%s %s %s" % (val, op, target)):
			msg("%s: invalid %s (%snot %s %s)" % (val,desc,what,op,target))
			return False
		return True

	def opt_is_int(val,desc):
		try: int(val)
		except:
			msg("'%s': invalid %s (not an integer)" % (val,desc))
			return False
		return True

	def opt_is_in_list(val,lst,desc):
		if val not in lst:
			q,sep = ("'","','") if type(lst[0]) == str else ("",",")
			msg("{q}{v}{q}: invalid {w}\nValid choices: {q}{o}{q}".format(
					v=val,w=desc,q=q,
					o=sep.join([str(i) for i in sorted(lst)])
				))
			return False
		return True

	def opt_unrecognized(key,val,desc):
		msg("'%s': unrecognized %s for option '%s'"
				% (val,desc,fmt_opt(key)))
		return False

	def opt_display(key,val='',beg="For selected",end=":\n"):
		s = "%s=%s" % (fmt_opt(key),val) if val else fmt_opt(key)
		msg_r("%s option '%s'%s" % (beg,s,end))

	global opt
	for key,val in [(k,getattr(opt,k)) for k in usr_opts]:

		desc = "parameter for '%s' option" % fmt_opt(key)

		from mmgen.util import check_infile,check_outfile,check_outdir
		# Check for file existence and readability
		if key in ('keys_from_file','mmgen_keys_from_file',
				'passwd_file','keysforaddrs','comment_file'):
			check_infile(val)  # exits on error
			continue

		if key == 'outdir':
			check_outdir(val)  # exits on error
		elif key == 'label':
			if not is_mmgen_wallet_label(val):
				msg("Illegal value for option '%s': '%s'" % (fmt_opt(key),val))
				return False
		# NEW
		elif key in ('in_fmt','out_fmt'):
			from mmgen.seed import SeedSource,IncogWallet,Brainwallet,IncogWalletHidden
			sstype = SeedSource.fmt_code_to_sstype(val)
			if not sstype:
				return opt_unrecognized(key,val,"format code")
			if key == 'out_fmt':
				p = 'hidden_incog_output_params'
				if sstype == IncogWalletHidden and not getattr(opt,p):
						die(1,"Hidden incog format output requested. You must supply"
						+ " a file and offset with the '%s' option" % fmt_opt(p))
				if issubclass(sstype,IncogWallet) and opt.old_incog_fmt:
					opt_display(key,val,beg="Selected",end=" ")
					opt_display('old_incog_fmt',beg="conflicts with",end=":\n")
					die(1,"Export to old incog wallet format unsupported")
				elif issubclass(sstype,Brainwallet):
					die(1,"Output to brainwallet format unsupported")
		elif key in ('hidden_incog_input_params','hidden_incog_output_params'):
			a = val.split(",")
			if len(a) != 2:
				opt_display(key,val)
				msg("Option requires two comma-separated arguments")
				return False
			if not opt_is_int(a[1],desc): return False
			if key == 'hidden_incog_input_params':
				check_infile(a[0],blkdev_ok=True)
				key2 = 'in_fmt'
			else:
				import os
				try: os.stat(a[0])
				except:
					b = os.path.dirname(a[0])
					if b: check_outdir(b)
				else: check_outfile(a[0],blkdev_ok=True)
				key2 = 'out_fmt'
			if hasattr(opt,key2):
				val2 = getattr(opt,key2)
				from mmgen.seed import IncogWalletHidden
				if val2 and val2 not in IncogWalletHidden.fmt_codes:
					die(1,
						"Option conflict:\n  %s, with\n  %s=%s" % (
						fmt_opt(key),fmt_opt(key2),val2
					))
		elif key == 'seed_len':
			if not opt_is_int(val,desc): return False
			if not opt_is_in_list(int(val),g.seed_lens,desc): return False
		elif key == 'hash_preset':
			if not opt_is_in_list(val,g.hash_presets.keys(),desc): return False
		elif key == 'brain_params':
			a = val.split(",")
			if len(a) != 2:
				opt_display(key,val)
				msg("Option requires two comma-separated arguments")
				return False
			d = "seed length " + desc
			if not opt_is_int(a[0],d): return False
			if not opt_is_in_list(int(a[0]),g.seed_lens,d): return False
			d = "hash preset " + desc
			if not opt_is_in_list(a[1],g.hash_presets.keys(),d): return False
		elif key == 'usr_randchars':
			if val == 0: continue
			if not opt_is_int(val,desc): return False
			if not opt_compares(val,">=",g.min_urandchars,desc): return False
			if not opt_compares(val,"<=",g.max_urandchars,desc): return False
		else:
			if g.debug: Msg("check_opts(): No test for opt '%s'" % key)

	return True