def test_init_themes_list(self): basepath = GIT_THEMING_PATH_HIST themeobj_1 = webobject.Theme(name='zombie', description='a theme description', parents='living corpse', meta=json.dumps({'source': './a/path'})) themeobj_2 = webobject.Theme(name='AI rights', description='another theme description', parents='human rights issue', meta=json.dumps( {'source': './another/path'})) themeobj_3 = webobject.Theme( name='a common enemy unites', description='yet another theme description', parents='human nature', meta=json.dumps({'source': './yet/another/path'})) themeobjs_list = [themeobj_1, themeobj_2, themeobj_3] expected_theme_count = len( themeobjs_list ) + 1 # the three test themes plus the 'literary thematic entity' root theme themes_list = webtask.cache_themes.init_themes_list( themeobjs_list, basepath) self.assertEqual(len(themes_list), expected_theme_count) # ' If the alphabetical sorting by name works, then the first and last themes in the list of # ' themes will be named as follows: first_theme_name = 'a common enemy unites' last_theme_name = 'zombie' self.assertEqual(themes_list[0]['name'], first_theme_name) self.assertEqual(themes_list[-1]['name'], last_theme_name)
def test_init_themes_od(self): basepath = GIT_THEMING_PATH_HIST theme_name = 'romantic love' theme_description = 'Featured is that peculiar sort of love between people so often associated with sexual attraction' theme_parents = 'love' theme_references = 'https://en.wikipedia.org/wiki/Romance_(love)' source_path = '/notes/themes/primary-themes.th.txt' raw_source_path = basepath + source_path theme_meta = json.dumps({'source': raw_source_path}) test_themeobj = webobject.Theme(name=theme_name, description=theme_description, parents=theme_parents, references=theme_references, meta=theme_meta) theme_od = webtask.cache_themes.init_theme_od(test_themeobj, basepath) self.assertEqual(theme_od['name'], theme_name) self.assertEqual(type(theme_od['aliases']), list) self.assertEqual(theme_od['description'], theme_description) self.assertEqual(type(theme_od['notes']), list) self.assertEqual(theme_od['parents'][0], theme_parents) self.assertEqual(type(theme_od['references']), list) self.assertEqual(type(theme_od['examples']), list) self.assertEqual(type(theme_od['relatedthemes']), list) self.assertEqual(theme_od['source'], '.' + source_path)
def read_themes_from_txt(filename, verbose=True, addextras=False, combinedescription=True, strict=True): """ Themes in our special text file format. """ themestuff, notices = parse(filename) out_themes = {} out_composites = defaultdict(lambda: defaultdict(str)) field_map = { "parents": "parents", "description": "description", } composite_fields = { "description": "description", "examples": "examples", "notes": "notes", "references": "references", "aliases": "aliases", } unused_fieldnames = { "related themes", "other parents", } meta = { "source": filename, } for notice in notices: lib.log.warn("%s: %s", filename, notice) for theme, field, data in themestuff: if theme not in out_themes: out_themes[theme] = webobject.Theme(name=theme, meta=json.dumps(meta)) themeobj = out_themes[theme] lfield = field.strip().lower() attr = field_map.get(lfield, "") if lfield in composite_fields: out_composites[theme][lfield] = '\n'.join(data) if attr in themeobj.fields: setattr(themeobj, attr, data[0]) elif addextras: exattr = lfield.replace(" ", "") setattr(themeobj, exattr, '\n'.join(data)) themeobj.extra_fields += (exattr,) else: if verbose: if lfield not in composite_fields and lfield not in unused_fieldnames: lib.log.warn("""%s: "%s": unrecognized field name "%s" """, filename, theme, field) for key in sorted(out_themes): themeobj = out_themes[key] if combinedescription: description = getattr(themeobj, "description") examples = out_composites[key]["examples"].strip() aliases = out_composites[key]["aliases"].strip() notes = out_composites[key]["notes"].strip() references = out_composites[key]["references"].strip() if notes: description += "\n\nNotes:\n" + notes if examples: description += "\n\nExamples:\n" + examples if aliases: description += "\n\nAliases:\n" + aliases if references: description += "\n\nReferences:\n" for line in references.split("\n"): line = line.strip() if line: description += line + "\n" themeobj.description = description try: if strict: themeobj.test_fields() yield themeobj except ValueError as e: if verbose: lib.log.warn("%s: %s.%s - %s", filename, theme, field, str(e))