Merge with Bitcoin v0.6.3
[novacoin.git] / bitcointools / util.py
1 #
2 # Misc util routines
3 #
4
5 from bsddb.db import *
6
7 def long_hex(bytes):
8   return bytes.encode('hex_codec')
9
10 def short_hex(bytes):
11   t = bytes.encode('hex_codec')
12   if len(t) < 11:
13     return t
14   return t[0:4]+"..."+t[-4:]
15
16 def determine_db_dir():
17   import os
18   import os.path
19   import platform
20   if platform.system() == "Darwin":
21     return os.path.expanduser("~/Library/Application Support/Bitcoin/")
22   elif platform.system() == "Windows":
23     return os.path.join(os.environ['APPDATA'], "Bitcoin")
24   return os.path.expanduser("~/.bitcoin")
25
26 def create_env(db_dir=None):
27   if db_dir is None:
28     db_dir = determine_db_dir()
29   db_env = DBEnv(0)
30   r = db_env.open(db_dir,
31                   (DB_CREATE|DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|
32                    DB_INIT_TXN|DB_THREAD|DB_RECOVER))
33   return db_env