# Path: kano_settings/system/get_username.py
# def get_first_username():
#     real_users = get_real_users()
#     if real_users:
#         real_users.sort(key=lambda x: x[pwd_field_id['pw_uid']])
#         if len(real_users) > 0:
#             return real_users[0][pwd_field_id['pw_name']]
# 
#     return None
# 
# Path: kano_settings/common.py
# IMAGES_DIR = os.path.join(media, "Graphics")
# 
# Path: kano_settings/config_file.py
# def get_setting(variable):
# 
#     try:
#         value = read_json(settings_file)[variable]
#     except Exception:
#         if variable not in DEFAULTS:
#             logger.info("Defaults not found for variable: {}".format(variable))
#         value = DEFAULTS[variable]
#     return value
# 
# def set_setting(variable, value):
# 
#     if username == 'root':
#         return
# 
#     logger.debug(u"config_file / set_setting: {} {}".format(variable, value))
# 
#     data = read_json(settings_file)
#     if not data:
#         data = dict()
# 
#     data[variable] = value
#     write_json(settings_file, data)
#     chown_path(settings_file)


import os
import shutil
import hashlib
import subprocess
import signal
import urllib2
import gzip
import re
    import platform
    import pycountry
    import pycountry
    import json
from bs4 import BeautifulSoup
from kano.logging import logger
from kano_settings.system.get_username import get_first_username
from kano.network import set_dns, restore_dns_interfaces, \
    clear_dns_interfaces, refresh_resolvconf
from kano_settings.common import settings_dir
from kano_settings.config_file import get_setting, set_setting
from kano.utils.file_operations import ensure_dir, read_file_contents, \
    write_file_contents, read_file_contents_as_lines, read_json, write_json
from kano.utils.shell import run_bg
from kano.utils.user import get_user_unsudoed


# Dns-related parental config
####################################################

def set_dns_parental(ultimate, safesearch, opendns):
    if ultimate or safesearch:
        # if server is running, kill it and restart it
        kill_server()

        # this is to get the most up to date whitelist
        restore_dns_interfaces()
        redirect_traffic_to_google()
        if ultimate:
            set_setting('use_sentry', 'whitelist')
            parse_whitelist_to_config_file(sentry_config)
        elif safesearch:
            set_setting('use_sentry', 'safesearch')
            make_safesearch_config_file(sentry_config, opendns)

        # Now set resolv.conf to point to localhost
        clear_dns_interfaces()
        redirect_traffic_to_localhost()
        launch_sentry_server(sentry_config)

    else:
        restore_dns_interfaces()
        redirect_traffic_to_google()
        kill_server()


# Path: kano_settings/system/get_username.py
# def get_first_username():
#     real_users = get_real_users()
#     if real_users:
#         real_users.sort(key=lambda x: x[pwd_field_id['pw_uid']])
#         if len(real_users) > 0:
#             return real_users[0][pwd_field_id['pw_name']]
# 
#     return None
# 
# Path: kano_settings/common.py
# IMAGES_DIR = os.path.join(media, "Graphics")
# 
# Path: kano_settings/config_file.py
# def get_setting(variable):
# 
#     try:
#         value = read_json(settings_file)[variable]
#     except Exception:
#         if variable not in DEFAULTS:
#             logger.info("Defaults not found for variable: {}".format(variable))
#         value = DEFAULTS[variable]
#     return value
# 
# def set_setting(variable, value):
# 
#     if username == 'root':
#         return
# 
#     logger.debug(u"config_file / set_setting: {} {}".format(variable, value))
# 
#     data = read_json(settings_file)
#     if not data:
#         data = dict()
# 
#     data[variable] = value
#     write_json(settings_file, data)
#     chown_path(settings_file)


# Path: kano_settings/system/advanced.py
import os
import shutil
import hashlib
import subprocess
import signal
import urllib2
import gzip
import re
    import platform
    import pycountry
    import pycountry
    import json
from bs4 import BeautifulSoup
from kano.logging import logger
from kano_settings.system.get_username import get_first_username
from kano.network import set_dns, restore_dns_interfaces, \
    clear_dns_interfaces, refresh_resolvconf
from kano_settings.common import settings_dir
from kano_settings.config_file import get_setting, set_setting
from kano.utils.file_operations import ensure_dir, read_file_contents, \
    write_file_contents, read_file_contents_as_lines, read_json, write_json
from kano.utils.shell import run_bg
from kano.utils.user import get_user_unsudoed



# Dns-related parental config
####################################################

def set_dns_parental(ultimate, safesearch, opendns):
    if ultimate or safesearch:
        # if server is running, kill it and restart it
        kill_server()

        # this is to get the most up to date whitelist
        restore_dns_interfaces()
        redirect_traffic_to_google()
        if ultimate:
            set_setting('use_sentry', 'whitelist')
            parse_whitelist_to_config_file(sentry_config)
        elif safesearch:
            set_setting('use_sentry', 'safesearch')
            make_safesearch_config_file(sentry_config, opendns)

        # Now set resolv.conf to point to localhost
        clear_dns_interfaces()
        redirect_traffic_to_localhost()
        launch_sentry_server(sentry_config)

    else:
        restore_dns_interfaces()
        redirect_traffic_to_google()
        kill_server()


        set_setting('use_sentry', '')