sugar-toolkit-gtk3/sugar/conf/Profile.py

66 lines
1.4 KiB
Python
Raw Normal View History

2006-08-22 16:15:52 +02:00
import os
from ConfigParser import ConfigParser
2006-08-23 21:03:17 +02:00
from sugar.canvas.IconColor import IconColor
2006-08-22 16:15:52 +02:00
class Profile:
def __init__(self):
self._nick_name = None
def _ensure_dirs(self):
try:
os.makedirs(self._path)
except OSError, exc:
if exc[0] != 17: # file exists
print "Could not create user directory."
2006-08-23 21:03:17 +02:00
def get_color(self):
return self._color
def set_color(self, color):
self._color = color
self.save()
2006-08-22 16:15:52 +02:00
def get_nick_name(self):
return self._nick_name
def set_nick_name(self, nick_name):
self._nick_name = nick_name
self.save()
def get_path(self):
return self._path
def read(self, profile_id):
self._profile_id = profile_id
base_path = os.path.expanduser('~/.sugar')
self._path = os.path.join(base_path, profile_id)
2006-08-23 21:03:17 +02:00
self._color = None
2006-08-22 16:15:52 +02:00
self._ensure_dirs()
cp = ConfigParser()
parsed = cp.read([self._get_config_path()])
if cp.has_option('Buddy', 'NickName'):
self._nick_name = cp.get('Buddy', 'NickName')
2006-08-23 21:03:17 +02:00
if cp.has_option('Buddy', 'Color'):
self._color = IconColor(cp.get('Buddy', 'Color'))
if self._color == None:
self.set_color(IconColor())
2006-08-22 16:15:52 +02:00
def save(self):
cp = ConfigParser()
section = 'Buddy'
cp.add_section(section)
cp.set(section, 'NickName', self._nick_name)
2006-08-23 21:03:17 +02:00
cp.set(section, 'Color', self._color.get_fill_color())
2006-08-22 16:15:52 +02:00
fileobject = open(self._get_config_path(), 'w')
cp.write(fileobject)
fileobject.close()
def _get_config_path(self):
return os.path.join(self._path, 'config')