File: config.py

package info (click to toggle)
libtpclient-py 0.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 444 kB
  • ctags: 849
  • sloc: python: 5,260; makefile: 49
file content (46 lines) | stat: -rw-r--r-- 870 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""\
This file contains function useful for storing and retriving config.
"""

import sys
import os
import os.path

try:
	import cPickle as pickle
except ImportError:
	import pickle

def configpath():
	"""\
	Figures out where to save the preferences.
	"""
	dirs = [("APPDATA", "Thousand Parsec"), ("HOME", ".tp"), (".", "var")]
	for base, extra in dirs:
		if base in os.environ:
			base = os.environ[base]
		elif base != ".":
			continue
			
		rc = os.path.join(base, extra)
		if not os.path.exists(rc):
			os.mkdir(rc)
		return rc

def load_data(file):
	"""\
	Loads preference data from a file.
	"""
	try:
		f = open(os.path.join(configpath(), file), "r")
		data = pickle.load(f)
	except IOError:
		return None
	return data
	
def save_data(file, data):
	"""\
	Saves preference data to a file.
	"""
	f = open(os.path.join(configpath(), file), "w")
	pickle.dump(data, f)