~vomun-developers/anonplus/vomun-trunk

« back to all changes in this revision

Viewing changes to src/libs/config.py

  • Committer: AJ00200
  • Date: 2011-11-25 21:30:53 UTC
  • Revision ID: git-v1:adec72eb3a3f154a85bce3e01e408b6e40b3d3da
Basic structure for database storage. Also added a start function.

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
            'gnupgdir': os.getenv('HOME') + '/.vomun/gnupg/',
 
19
            'nodekey': ''
 
20
        }
 
21
        configfile = open(CONFIG_PATH, 'a')
 
22
        configfile.write(json.dumps(default_config, indent = 4))
 
23
        configfile.flush()
 
24
        configfile.close()
 
25
        configfile = open(CONFIG_PATH, 'r+')
 
26
    return configfile
 
27
 
 
28
 
 
29
config_file = open_config()
 
30
 
 
31
@register_with_api
 
32
def load_config():
 
33
    '''Load the configuration file'''
 
34
    libs.globals.global_vars['config'] = json.loads(config_file.read())
 
35
    config_file.seek(0) # return read/write position to beginning of the file
 
36
 
 
37
@register_with_api
 
38
def get_config():
 
39
    '''Return the contents of the configuration file'''
 
40
    return libs.globals.global_vars["config"]
 
41
 
 
42
 
 
43
@register_with_api
 
44
def save_config():
 
45
    '''Write the configuration file to the hard disk'''
 
46
    config_file.write(json.dumps(libs.globals.global_vars['config'],
 
47
                                 indent = 4))
 
48
 
 
49
load_config()