# Path: kano_profile/paths.py
# TRACKER_UUIDS_PATH = os.path.join(kanoprofile_dir, 'tracker', 'uuids')
# PAUSED_SESSIONS_FILE = os.path.join(kanoprofile_dir, '.paused_sessions')
# 
# Path: kano_avatar/paths.py
# AVATAR_DEFAULT_LOC = os.path.join(get_home(), '.character-content')


import os
from kano.logging import logger
from kano.utils import (read_json, write_json, get_date_now, ensure_dir,
                        chown_path, get_user_unsudoed, run_bg, is_running,
                        list_dir)
from .paths import profile_file, profile_dir, kanoprofile_dir, bin_dir
from kano_avatar.paths import AVATAR_DEFAULT_LOC


def load_profile():
    ''' Read profile data from file containing profile information and return
    it as a dict. If such a file is not present, then a new dict is created
    :returns: profile data as a dict
    :rtype: dict
    '''
    data = read_json(profile_file)
    if not data:
        data = dict()
        # if the profile file doesn't exist make sure that the new one
        # is created with the right version
        data['version'] = 2
    if 'version' not in data:
        data.pop('avatar', None)
        data.pop('environment', None)
    data['username_linux'] = get_user_unsudoed()
    return data


def save_profile(data, skip_kdesk_refresh=False):
    ''' Write profile data to file
    :param data: JSON serialisable data about the profile
    '''
    logger.debug('save_profile')

    data.pop('cpu_id', None)
    data.pop('mac_addr', None)
    data['save_date'] = get_date_now()

# Path: kano_profile/paths.py
# TRACKER_UUIDS_PATH = os.path.join(kanoprofile_dir, 'tracker', 'uuids')
# PAUSED_SESSIONS_FILE = os.path.join(kanoprofile_dir, '.paused_sessions')
# 
# Path: kano_avatar/paths.py
# AVATAR_DEFAULT_LOC = os.path.join(get_home(), '.character-content')


# Path: kano_profile/profile.py
import os
from kano.logging import logger
from kano.utils import (read_json, write_json, get_date_now, ensure_dir,
                        chown_path, get_user_unsudoed, run_bg, is_running,
                        list_dir)
from .paths import profile_file, profile_dir, kanoprofile_dir, bin_dir
from kano_avatar.paths import AVATAR_DEFAULT_LOC



def load_profile():
    ''' Read profile data from file containing profile information and return
    it as a dict. If such a file is not present, then a new dict is created
    :returns: profile data as a dict
    :rtype: dict
    '''
    data = read_json(profile_file)
    if not data:
        data = dict()
        # if the profile file doesn't exist make sure that the new one
        # is created with the right version
        data['version'] = 2
    if 'version' not in data:
        data.pop('avatar', None)
        data.pop('environment', None)
    data['username_linux'] = get_user_unsudoed()
    return data


def save_profile(data, skip_kdesk_refresh=False):
    ''' Write profile data to file
    :param data: JSON serialisable data about the profile
    '''
    logger.debug('save_profile')

    data.pop('cpu_id', None)
    data.pop('mac_addr', None)
    data['save_date'] = get_date_now()

    ensure_dir(profile_dir)