~vomun-developers/anonplus/vomun-trunk

« back to all changes in this revision

Viewing changes to src/libs/config.py

  • Committer: AJ00200
  • Date: 2011-11-26 00:03:57 UTC
  • Revision ID: git-v1:f4f4352eb338a5f57a5a92e4bf23bec36d298d13
Blocks we receive are now stored in ~/.vomun/blocks.json. libs.globals and libs.config are now used directly after importing libs; they are set in libs/__init__.py now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
'''Load the config from ~/.vomun/config.json'''
2
 
 
3
 
import os
4
 
import json
5
 
import libs.globals
6
 
from libs.morado.functions import register_with_api
7
 
 
8
 
CONFIG_PATH = os.path.expanduser("~/.vomun/config.json")
9
 
 
10
 
 
11
 
def open_config():
12
 
    '''Open the configuration file from ~/.vomun/config.json'''
13
 
    try:
14
 
        configfile = open(CONFIG_PATH,"r+")
15
 
    except IOError:
16
 
        default_config = {   
17
 
            'vomundir': os.getenv('HOME') + '/.vomun/',
18
 
            'nodekey': ''
19
 
        }
20
 
        configfile = open(CONFIG_PATH, 'a')
21
 
        configfile.write(json.dumps(default_config, indent = 4))
22
 
        configfile.flush()
23
 
        configfile.close()
24
 
        configfile = open(CONFIG_PATH, 'r+')
25
 
    return configfile
26
 
 
27
 
 
28
 
config_file = open_config()
29
 
 
30
 
@register_with_api
31
 
def load_config():
32
 
    '''Load the configuration file'''
33
 
    libs.globals.global_vars['config'] = json.loads(config_file.read())
34
 
    config_file.seek(0) # return read/write position to beginning of the file
35
 
 
36
 
@register_with_api
37
 
def get_config():
38
 
    '''Return the contents of the configuration file'''
39
 
    return libs.globals.global_vars["config"]
40
 
 
41
 
 
42
 
@register_with_api
43
 
def save_config():
44
 
    '''Write the configuration file to the hard disk'''
45
 
    config_file.write(json.dumps(libs.globals.global_vars['config'],
46
 
                                 indent = 4))
47
 
 
48
 
load_config()