Handle options for RPC connection.

Handle config options for RPC connection, and also implement loading
default user/password from namecoin config file.  No UI yet.
This commit is contained in:
Daniel Kraft 2013-07-05 20:08:19 +02:00
parent 09c0aa993f
commit 03bb54fc98
3 changed files with 78 additions and 5 deletions

View File

@ -14,7 +14,7 @@ except ImportError:
from addresses import *
import shared
from bitmessageui import *
from class_namecoin import namecoinConnection
from namecoin import namecoinConnection
from newaddressdialog import *
from newsubscriptiondialog import *
from regenerateaddresses import *

View File

@ -3,6 +3,8 @@ import ConfigParser
import sys
import os
from namecoin import ensureNamecoinOptions
storeConfigFilesInSameDirectoryAsProgramByDefault = False # The user may de-select Portable Mode in the settings if they want the config files to stay in the application data folder.
def loadConfig():
@ -64,6 +66,7 @@ def loadConfig():
'bitmessagesettings', 'maxacceptablenoncetrialsperbyte', '0')
shared.config.set(
'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes', '0')
ensureNamecoinOptions()
if storeConfigFilesInSameDirectoryAsProgramByDefault:
# Just use the same directory as the program and forget about

View File

@ -22,6 +22,11 @@
import base64
import json
import socket
import sys
import shared
configSection = "bitmessagesettings"
# Error thrown when the RPC call returns an error.
class RPCError (Exception):
@ -40,10 +45,11 @@ class namecoinConnection (object):
queryid = 1
def __init__ (self):
self.user = "daniel"
self.password = "password"
self.host = "localhost"
self.port = "8336"
ensureNamecoinOptions ()
self.user = shared.config.get (configSection, "namecoinrpcuser")
self.password = shared.config.get (configSection, "namecoinrpcpassword")
self.host = shared.config.get (configSection, "namecoinrpchost")
self.port = shared.config.get (configSection, "namecoinrpcport")
# Query for the bitmessage address corresponding to the given identity
# string. If it doesn't contain a slash, id/ is prepended. We return
@ -138,3 +144,67 @@ class namecoinConnection (object):
except socket.error as exc:
raise Exception ("Socket error in RPC connection: %s" % str (exc))
# Look up the namecoin data folder.
# FIXME: Check whether this works on other platforms as well!
def lookupNamecoinFolder ():
app = "namecoin"
from os import path, environ
if sys.platform == "darwin":
if "HOME" in environ:
dataFolder = path.join (os.environ["HOME"],
"Library/Application Support/", app) + '/'
else:
print ("Could not find home folder, please report this message"
+ " and your OS X version to the BitMessage Github.")
sys.exit()
elif "win32" in sys.platform or "win64" in sys.platform:
dataFolder = path.join(environ["APPDATA"], app) + "\\"
else:
dataFolder = path.join(environ["HOME"], ".%s" % app) + "/"
return dataFolder
# Ensure all namecoin options are set, by setting those to default values
# that aren't there.
def ensureNamecoinOptions ():
if not shared.config.has_option (configSection, "namecoinrpchost"):
shared.config.set (configSection, "namecoinrpchost", "localhost")
if not shared.config.has_option (configSection, "namecoinrpcport"):
shared.config.set (configSection, "namecoinrpcport", "8336")
hasUser = shared.config.has_option (configSection, "namecoinrpcuser")
hasPass = shared.config.has_option (configSection, "namecoinrpcpassword")
# Try to read user/password from .namecoin configuration file.
if (not hasUser) or (not hasPass):
try:
nmcFolder = lookupNamecoinFolder ()
nmcConfig = nmcFolder + "bitcoin.conf"
nmc = open (nmcConfig, "r")
while True:
line = nmc.readline ()
if line == "":
break
parts = line.split ("=")
if len (parts) == 2:
key = parts[0]
val = parts[1].rstrip ()
if key == "rpcuser" and not hasUser:
shared.config.set (configSection,
"namecoinrpcuser", val)
if key == "rpcpassword" and not hasPass:
shared.config.set (configSection,
"namecoinrpcpassword", val)
nmc.close ()
except Exception as exc:
print "Failure reading namecoin config file: %s" % str (exc)
if (not hasUser):
shared.config.set (configSection, "namecoinrpcuser", "")
if (not hasPass):
shared.config.set (configSection, "namecoinrpcpassword", "")