def process_item(item): """ Process an item """ cmd = "{} {}".format(item['command'], item['command_postfix']) code, out, err = execute(cmd) if code != 0 or out == '': if err.strip() == 'No Instance(s) Available.': objects = [] else: raise Exception(cmd) else: objects = parse_out(item['name'], out) file_name = "raw_{}".format(item['file']) path = get_write_path(file_name) # Get old objects if os.path.isfile(path): with open(path, 'r', encoding='utf-8') as f: old_objects = json.loads(f.read()) else: old_objects = [] # Write new objects with open(path, 'w', encoding='utf-8') as f: f.write(json.dumps(objects, indent=4, sort_keys=True)) changed, new, removed = get_diff(item, old_objects, objects) write_diff(item, changed, new, removed) report(item, changed, new, removed) return changed or new or removed
def load_settings(): """ Loads setting for each category """ path = get_write_path(FILE_NAME) if os.path.isfile(path): # settings file exists with open(path, 'r', encoding='utf-8') as f: settings = json.load(f) for key in categories.CATEGORIES: s = copy.deepcopy(_DEFAULT) setting = settings.get(key, s) categories.CATEGORIES[key]['settings'] = setting else: settings = {} for key in categories.CATEGORIES: s = copy.deepcopy(_DEFAULT) settings[key] = s categories.CATEGORIES[key]['settings'] = s settings = json.dumps(settings, sort_keys=True, indent=4) with open(path, 'w', encoding='utf-8') as f: f.write(settings)
def write_diff(item, changed, new, removed): """ Write diff files """ diff = { 'changed': changed, 'new': new, 'removed': removed, } file_name = "diff_{}".format(item['file']) existing_changed, existing_new, existing_removed = load_existing_diff( file_name) changed.extend(existing_changed) new.extend(existing_new) removed.extend(existing_removed) path = get_write_path(file_name) if changed or new or removed: with open(path, 'w', encoding='utf-8') as f: f.write(json.dumps(diff, indent=4, sort_keys=True)) else: if os.path.exists(path): os.remove(path)
def delete_diffs(): """ Delete diff files """ for c, value in CATEGORIES.items(): file_name = "diff_{}".format(value['file']) path = get_write_path(file_name) if os.path.exists(path): os.remove(path)
def load_categories(): """ Load difference data for categories """ for _, value in CATEGORIES.items(): file_name = "diff_{}".format(value['file']) path = get_write_path(file_name) if os.path.isfile(path): with open(path, 'r', encoding='utf-8') as f: value['diff'] = json.load(f) else: value['diff'] = None
def save_settings(): """ Write settings file """ write = {} for key, value in categories.CATEGORIES.items(): write[key] = {'collect': value['settings']['collect']} write = json.dumps(write, indent=4, sort_keys=True) path = get_write_path(FILE_NAME) with open(path, 'w', encoding='utf-8') as f: f.write(write)
def load_existing_diff(file_name: str) -> tuple: """ Load existing diffs :param file_name: :return: Tuple of changed items, new items and removed items """ path = get_write_path(file_name) if os.path.exists(path): with open(path, encoding='utf-8') as f: diff = f.read() diff = json.loads(diff) changed = diff['changed'] new = diff['new'] removed = diff['removed'] else: changed, new, removed = [], [], [] return changed, new, removed
def get_data(category: str) -> str: """ Generate HTML for Data :param category: Item to generate report for :return: HTML for Data """ category = _get_category(category) file_name = "raw_{}".format(category['file']) path = get_write_path(file_name) if not os.path.exists(path): html = "Data has not been generated. Cannot find '{}'".format( file_name) else: data = _load_json(path) html = '' for item in data: title = category['title'] title = title.format(*[item[k] for k in category['title_keys']]) header = "<strong>{}</strong><br>".format(title) html += header for key, value in item.items(): line = "{}{}: {}<br>".format(TAB, key, value) html += line html += "<br>" if html == '': html = "Your system did not report any data for {}".format( category['name']) html = _add_html_tags(html) return html