def set_chromium_policies(policies): if not os.path.exists(chromium_policy_file): ensure_dir(os.path.dirname(chromium_policy_file)) policy_config = {} else: policy_config = read_json(chromium_policy_file) for policy in policies: policy_config[policy[0]] = policy[1] write_json(chromium_policy_file, policy_config)
def set_chromium_policies(policies): if not os.path.exists(chromium_policy_file): ensure_dir(os.path.dirname(chromium_policy_file)) policy_config = {} else: policy_config = read_json(chromium_policy_file) for policy in policies: policy_config[policy[0]] = policy[1] write_json(chromium_policy_file, policy_config)
def set_setting(variable, value): if username == 'root': return logger.debug('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)
def save_app_state_variable_all_users(app, variable, value): if os.environ['LOGNAME'] != 'root': logger.error("Error: save_app_state_variable_all_users must be executed with root privileges") return users = get_all_users() for user in users: dir_path = os.path.join( "/home", user, ".kanoprofile", "apps", app ) if not os.path.exists(dir_path): os.makedirs(dir_path) chown_path(dir_path, user, user) state_path = os.path.join(dir_path, "state.json") data = {variable: value} data['save_date'] = get_date_now() write_json(state_path, data) chown_path(state_path, user, user)
def save_profile(data): ''' 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) write_json(profile_file, data) if 'SUDO_USER' in os.environ: chown_path(kanoprofile_dir) chown_path(profile_dir) chown_path(profile_file) if os.path.exists('/usr/bin/kdesk') and not is_running('kano-sync'): logger.info('refreshing kdesk from save_profile') run_bg('kdesk -a profile')
def save_app_state_variable_all_users(app, variable, value): if os.environ['LOGNAME'] != 'root': logger.error( "Error: save_app_state_variable_all_users must be executed with root privileges" ) return users = get_all_users() for user in users: dir_path = os.path.join("/home", user, ".kanoprofile", "apps", app) if not os.path.exists(dir_path): os.makedirs(dir_path) chown_path(dir_path, user, user) state_path = os.path.join(dir_path, "state.json") data = {variable: value} data['save_date'] = get_date_now() write_json(state_path, data) chown_path(state_path, user, user)
def save_app_state(app_name, data): """ Save a state of an application to the user's Kano profile. :param app_name: The application that this data are associated with. :type app_name: str :param data: The data to be stored. :type data: dict """ logger.debug('save_app_state {}'.format(app_name)) app_state_file = get_app_state_file(app_name) data['save_date'] = get_date_now() ensure_dir(get_app_dir(app_name)) write_json(app_state_file, data) if 'SUDO_USER' in os.environ: chown_path(kanoprofile_dir) chown_path(apps_dir) chown_path(get_app_dir(app_name)) chown_path(app_state_file)
def save_app_state(app_name, data): """ Save a state of an application to the user's Kano profile. :param app_name: The application that this data are associated with. :type app_name: str :param data: The data to be stored. :type data: dict """ logger.debug("save_app_state {}".format(app_name)) app_state_file = get_app_state_file(app_name) data['save_date'] = get_date_now() ensure_dir(get_app_dir(app_name)) write_json(app_state_file, data) if 'SUDO_USER' in os.environ: chown_path(kanoprofile_dir) chown_path(apps_dir) chown_path(get_app_dir(app_name)) chown_path(app_state_file)
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) write_json(profile_file, data) if 'SUDO_USER' in os.environ: chown_path(kanoprofile_dir) chown_path(profile_dir) chown_path(profile_file) if (not skip_kdesk_refresh and os.path.exists('/usr/bin/kdesk') and not is_running('kano-sync')): logger.info("refreshing kdesk from save_profile") run_bg('kdesk -a profile')
import os import sys if __name__ == '__main__' and __package__ is None: dir_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) if dir_path != '/usr': sys.path.insert(1, dir_path) from kano_profile.badges import load_badge_rules from kano.utils import write_json, uniqify_list all_rules = load_badge_rules() variables_needed = dict() for category, subcats in all_rules.iteritems(): for subcat, items in subcats.iteritems(): for item, rules in items.iteritems(): targets = rules['targets'] for target in targets: app = target[0] variable = target[1] variables_needed.setdefault(app, list()).append(variable) for key in variables_needed.iterkeys(): variables_needed[key] = uniqify_list(variables_needed[key]) write_json('variables_needed.json', variables_needed, False)
def download_share(entry): app = entry['app'] title = entry['title'] description = entry['description'] attachment_url = entry['attachment_url'] cover_url = entry['cover_url'] resource_url = entry['resource_url'] data = { 'title': title, 'description': description } app_profiles = read_json(app_profiles_file) if app not in app_profiles: logger.error("Cannot download share, app not found in app-profiles") return app_profile = app_profiles[app] folder = os.path.join(get_home(), app_profile['dir'], 'webload') ensure_dir(folder) title_slugified = slugify(title) # Download attachment attachment_ext = attachment_url.split('.')[-1] attachment_name = '{}.{}'.format(title_slugified, attachment_ext) attachment_path = os.path.join(folder, attachment_name) success, text = download_url(attachment_url, attachment_path) if not success: msg = "Error with downloading share file: {}".format(text) logger.error(msg) return False, msg # Download screenshot if cover_url: cover_ext = cover_url.split('.')[-1] cover_name = '{}.{}'.format(title_slugified, cover_ext) cover_path = os.path.join(folder, cover_name) success, text = download_url(cover_url, cover_path) if not success: msg = "Error with downloading cover file: {}".format(text) logger.error(msg) return False, msg # Download resource file if resource_url: resource_ext = resource_url.split('.')[-1] # Make sure we don't remove the tar from gz if 'tar.gz' in resource_url: resource_ext = 'tar.' + resource_ext resource_name = '{}.{}'.format(title_slugified, resource_ext) resource_path = os.path.join(folder, resource_name) success, text = download_url(resource_url, resource_path) if not success: msg = "Error with downloading resource file: {}".format(text) logger.error(msg) return False, msg # JSON file json_name = '{}.{}'.format(title_slugified, 'json') json_path = os.path.join(folder, json_name) write_json(json_path, data) return True, [title, attachment_path, app, attachment_name, folder]
def download_share(entry): app = entry['app'] title = entry['title'] description = entry['description'] attachment_url = entry['attachment_url'] cover_url = entry['cover_url'] resource_url = entry['resource_url'] data = {'title': title, 'description': description} app_profiles = read_json(app_profiles_file) if app not in app_profiles: logger.error("Cannot download share, app not found in app-profiles") return app_profile = app_profiles[app] folder = os.path.join(get_home(), app_profile['dir'], 'webload') ensure_dir(folder) title_slugified = slugify(title) # Download attachment attachment_ext = attachment_url.split('.')[-1] attachment_name = '{}.{}'.format(title_slugified, attachment_ext) attachment_path = os.path.join(folder, attachment_name) success, text = download_url(attachment_url, attachment_path) if not success: msg = "Error with downloading share file: {}".format(text) logger.error(msg) return False, msg # Download screenshot if cover_url: cover_ext = cover_url.split('.')[-1] cover_name = '{}.{}'.format(title_slugified, cover_ext) cover_path = os.path.join(folder, cover_name) success, text = download_url(cover_url, cover_path) if not success: msg = "Error with downloading cover file: {}".format(text) logger.error(msg) return False, msg # Download resource file if resource_url: resource_ext = resource_url.split('.')[-1] # Make sure we don't remove the tar from gz if 'tar.gz' in resource_url: resource_ext = 'tar.' + resource_ext resource_name = '{}.{}'.format(title_slugified, resource_ext) resource_path = os.path.join(folder, resource_name) success, text = download_url(resource_url, resource_path) if not success: msg = "Error with downloading resource file: {}".format(text) logger.error(msg) return False, msg # JSON file json_name = '{}.{}'.format(title_slugified, 'json') json_path = os.path.join(folder, json_name) write_json(json_path, data) return True, [title, attachment_path, app, attachment_name, folder]