def test_novel_serialized(self): scode0 = SCode(None, SCmd.BE, (), '') scode1 = SCode(None, SCmd.INFO_DATA, (), '') data = [ # (src, expect) (True, Chapter('a', Episode('b', Scene('c', scode0))), 1), (True, Chapter('a', scode1, Episode('b', Scene('c', scode0))), 2), ] validate_with_fail( self, 'novel_serialized', lambda src, expect: self.assertEqual( len(sl.Serializer()._novel_serialized(src)), expect), data)
def test_instance(self): s0 = SCode(None, SCmd.BE, (), '') s1 = SCode(None, SCmd.INFO_DATA, (), '') data = [ # (args, expect) (True, (s0, s1), (s0, s1)), ] def checker(args, expect): tmp = cd.CodeList(*args) self.assertIsInstance(tmp, cd.CodeList) self.assertEqual(tmp.data, expect) validate_with_fail(self, 'instance', checker, data)
def test_replaced_scode(self): data = [ # src, tags, expect (True, SCode(None, SCmd.DO, ('$taroは話した',),), {'taro': '太郎'}, ('太郎は話した',),), (True, SCode(self.taro, SCmd.DO, ('$meは話した', '$herは逃げた'),), {'her': '華子'}, ('俺は話した','華子は逃げた'),), ] validate_with_fail(self, 'replaced_scode', lambda src, tags, expect: self.assertEqual( tp.TagReplacer()._replaced_scode(src, tags).script, expect), data)
def _update_container_info( self, src: (Chapter, Episode, Scene, Material), columns: int, rows: int) -> (Chapter, Episode, Scene, Material): LOG.info('HEAD_UPDATER: update_container_info') LOG.debug(f'-- src: {src}') LOG.debug(f'-- columns/rows: {columns}/{rows}') assertion.is_instance(src, (Chapter, Episode, Scene, Material)) tmp = [] tmp.append(self._containerhead_of(src)) tmp.append(self._collect_header_info(src, columns, rows)) tmp.append(self._title_of(src)) if isinstance(src, Scene): tmp.append(self._collect_scene_info(src)) if src.outline: tmp.append(self._outline_of(src)) for child in src.children: if isinstance(child, (Chapter, Episode, Scene)): tmp.append(self._update_container_info(child, columns, rows)) elif isinstance(child, SCode): if Checker().has_then(child): tmp.append(SCode(None, SCmd.THEN, (), '')) tmp.append(child) elif isinstance(child, Material): tmp.append(self._update_container_info(child, columns, rows)) else: LOG.error(f'Invalid child value!: {type(child)} | {child}') tmp.append(self._end_of(src)) return src.inherited(*tmp)
def test_execute_internal(self): data = [ # (src, priority, expect, exp_val) (True, Chapter('test', Episode('a'), Episode('b'), Episode('c')), 5, True, 3), (True, Chapter('test', Episode('a'), Episode('b').set_priority(2), Episode('c')), 5, True, 2), (True, Chapter('test', Episode('a'), Episode('b'), Episode('c')), 5, True, 3), (True, SCode(None, SCmd.BE, (), '').set_priority(2), 5, True, None), ] def checker(src, pri, expect, exp_val): tmp = ft.Filter()._exec_internal(src, pri) self.assertEqual(tmp[1], expect) if hasattr(tmp[0], 'children'): self.assertEqual(len(tmp[0].children), exp_val) elif isinstance(tmp[0], (list, tuple)): self.assertEqual(len(tmp[0], exp_val)) elif tmp[0]: self.assertIsInstance(tmp[0], SCode) else: self.assertEqual(tmp[0], exp_val) validate_with_fail(self, 'execute_internal', checker, data)
def _exec_internal( self, src: ContainerLike ) -> (Story, Chapter, Episode, Scene, SCode, None): LOG.debug(f'-- src: {src}') tmp = [] if isinstance(src, (Story, Chapter, Episode, Scene, Material)): tmp = [] for child in src.children: if isinstance(child, (Chapter, Episode, Scene, Material)): ret = self._exec_internal(child) if ret: tmp.append(ret) elif isinstance(child, SCode): tmp.append(child) elif isinstance(child, str): tmp.append(SCode(None, SCmd.TAG_COMMENT, (child, ), '')) else: # NOTE: error check? pass return src.inherited(*tmp) elif isinstance(src, SCode): return src else: LOG.error() return None
def _get_contents(self, src: Story, info_level: int) -> SCode: collect = Collecter() tmp = [] ch_n = ep_n = sc_n = 1 titles = [] if info_level == 1: # for Story titles = collect.container_titles_without_material(src) elif info_level == 2: # Materials titles = collect.container_titles_only_materials(src) else: # All (for Plot) titles = collect.container_titles(src) for title_set in titles: level, title = title_set.split(':') if level == '0': continue elif level == '1': tmp.append(f'{ch_n}. {title}') ch_n += 1 elif level == '2': tmp.append(f' {ep_n}. {title}') ep_n += 1 elif level == '3': tmp.append(f' {sc_n}. {title}') sc_n += 1 else: continue return SCode(None, SCmd.TAG_TITLE, ('\n'.join(tmp), ), f'contents:{info_level}')
def test_has_then(self): data = [ # (src, expect[bool]) (True, SCode(None, SCmd.THEN, (),""), True), ] validate_with_fail(self, 'has_then', lambda src, expect: self.assertEqual( ck.Checker().has_then(src), expect), data)
def test_is_empty_script(self): data = [ # (src, expect[bool]) (True, SCode(None, SCmd.BE, ('&',), ""), True), ] validate_with_fail(self, 'is_empty_script', lambda src, expect: self.assertEqual( ck.Checker().is_empty_script(src), expect), data)
def test_scodes_of_without_info(self): scode0 = SCode(None, SCmd.BE, (), '') sinfo = SCode(None, SCmd.INFO_DATA, (), '') data = [ # (src, expect[int]) (True, Story('a', Chapter('b', Episode('c', Scene('d', scode0, sinfo)))), 1), (True, Chapter('a', Episode('b', Scene('c', scode0, sinfo))), 1), (True, Episode('a', Scene('b', scode0, sinfo)), 1), (True, Scene('a', scode0, sinfo), 1), (True, scode0, 1), (True, sinfo, 0), ] validate_with_fail( self, 'scode_of_without_info', lambda src, expect: self.assertEqual( cnt.Counter().scodes_of_without_info(src), expect), data)
def test_conv_to_novel(self): data = [ # (src, is_cmt, expect) (True, CodeList(*[SCode(None, SCmd.BE, ('太郎', ), '')]), False, (FormatTag.DESCRIPTION_HEAD, ' 太郎。', '\n')), ] validate_with_fail( self, 'conv_to_novel', lambda src, is_cmt, expect: self.assertEqual( cp.Compiler()._conv_to_novel(src, is_cmt).data, expect), data)
def test_scodes_of(self): data = [ # (src, expect[int]) (True, Story( 'a', Chapter( 'b', Episode('c', Scene('d', SCode(None, SCmd.BE, (), ''))))), 1), (True, Chapter('a', Episode('b', Scene('c', SCode(None, SCmd.BE, (), '')))), 1), (True, Episode('a', Scene('b', SCode(None, SCmd.BE, (), ''))), 1), (True, Scene('a', SCode(None, SCmd.BE, (), '')), 1), (True, SCode(None, SCmd.BE, (), ''), 1), ] validate_with_fail( self, 'scodes_of', lambda src, expect: self.assertEqual( cnt.Counter().scodes_of(src), expect), data)
def _collect_scene_info(self, src: Scene) -> SCode: collect = Collecter() cameras = collect.cameras_in_scene(src) stages = collect.stages_in_scene(src) days = collect.days_in_scene(src) times = collect.times_in_scene(src) return SCode(None, SCmd.INFO_DATA, (SceneInfo( cameras[0] if cameras else None, stages[0] if stages else None, days[0] if days else None, times[0] if times else None, ), ), '')
def _inserted_after_storyinfo(self, src: Story, infos: list) -> Story: assertion.is_instance(src, Story) LOG.debug(f'- info in inserted_after_storyinfo:{infos}') tmp = [] for child in src.children: if isinstance(child, SCode) and child.cmd is SCmd.INFO_STORY: tmp.append(child) for info in infos: tmp.append(SCode(None, SCmd.INFO_DATA, (info,), "")) else: tmp.append(child) return src.inherited(*tmp)
def _containerhead_of( self, src: (Chapter, Episode, Scene, Material)) -> (SCode, None): cmd = None if isinstance(src, Chapter): cmd = SCmd.HEAD_CHAPTER elif isinstance(src, Episode): cmd = SCmd.HEAD_EPISODE elif isinstance(src, Scene): cmd = SCmd.HEAD_SCENE elif isinstance(src, Material): cmd = SCmd.HEAD_MATERIAL else: LOG.error(f'Invalid source in containerhead_of: {src}') return SCode(None, cmd, (), '') if cmd else None
def _replaced_scode(self, src: SCode, tags: dict) -> Scode: script = assertion.is_instance(src, SCode).script _tags = tags def _conv(val, tags): if isinstance(val, str): return string_replaced_by_tag(val, tags) else: return val if hasattr(src.src, 'calling'): _tags = dict_sorted(combine_dict(tags, src.src.calling), True) script = tuple(_conv(v, _tags) for v in script) return src.inherited(script=script, )
def test_description_characters_of(self): scode0 = SCode(None, SCmd.BE, ('apple', ), '') scode1 = SCode(None, SCmd.DO, ('orange', ), '') sinfo = SCode(None, SCmd.INFO_DATA, ('test', ), '') data = [ # (src, expect[int]) (True, Story( 'a', Chapter('b', Episode('c', Scene('d', scode0, scode1, sinfo)))), 13), (True, Chapter('a', Episode('b', Scene('c', scode0, scode1, sinfo))), 13), (True, Episode('a', Scene('b', scode0, scode1, sinfo)), 13), (True, Scene('a', scode0, scode1, sinfo), 13), (True, scode0, 6), (True, sinfo, 0), (True, [scode0, scode1], 13), ] validate_with_fail( self, 'description_characters_of', lambda src, expect: self.assertEqual( cnt.Counter().description_characters_of(src), expect), data)
def test_conv_from_tag(self): data = [ # (src, head, nums, is_cmt, is_plot, is_data, in_mate, expect, exp_nums) (True, SCode(None, SCmd.TAG_BR, (), ''), '#', (1, 1, 1), True, False, False, False, '\n\n', (1, 1, 1)), ] def checker(src, head, nums, is_cmt, is_plot, is_data, in_mate, expect, exp_nums): tmp = cp.Compiler()._conv_from_tag(src, head, nums, is_cmt, is_plot, is_data, in_mate) self.assertEqual(tmp[0], expect) self.assertEqual(tmp[1], exp_nums) validate_with_fail(self, 'conv_from_tag', checker, data)
def test_manupaper_rows_of(self): scode0 = SCode(None, SCmd.BE, ('apple', ), '') scode1 = SCode(None, SCmd.DO, ('orange', ), '') scode2 = SCode(None, SCmd.TALK, ('melon', ), '') data = [ # (src, columns, expect[int]) (True, Story( 'a', Chapter('b', Episode('c', Scene('d', scode0, scode1, scode2)))), 20, 3), (True, Chapter('a', Episode('b', Scene('c', scode0, scode1, scode2))), 20, 3), (True, Episode('a', Scene('b', scode0, scode1, scode2)), 20, 3), (True, Scene('a', scode0, scode1, scode2), 20, 3), (True, scode0, 20, 1), (True, [scode0, scode1, scode2], 20, 3), (True, SCode(None, SCmd.DO, ('a' * 10, )), 5, 3), ] validate_with_fail( self, 'manupaper_rows_of', lambda src, columns, expect: self.assertEqual( cnt.Counter().manupaper_rows_of(src, columns), expect), data)
def _title_of( self, src: (Story, Chapter, Episode, Scene, Material)) -> SCode: level = 0 if isinstance(src, Story): level = 1 elif isinstance(src, Chapter): level = 2 elif isinstance(src, Episode): level = 3 elif isinstance(src, Scene): level = 4 elif isinstance(src, Material): level = 2 else: LOG.critical( f'Invalid source of a story object: {type(src)}: {src}') return SCode(None, SCmd.TAG_TITLE, (src.title, ), level)
def _get_storydata(self, src: Story, config: StoryConfig) -> SCode: return SCode( None, SCmd.INFO_STORY, ({ 'title': config.title, 'copy': config.copy, 'oneline': config.oneline, 'outline': config.outline, 'contest_info': config.contest_info, 'caution': config.caution, 'note': config.note, 'sites': config.sites, 'tags': config.taginfos, 'total_chars': Counter().description_characters_of(src), 'version': config.version, 'modified': config.modified, 'released': config.released, }, ), '')
def _collect_header_info(self, src: (Story, Chapter, Episode, Scene, Material), columns: int, rows: int) -> SCode: count = Counter() total_lines = count.manupaper_rows_of(src, columns, True) lines = count.manupaper_rows_of(src, columns) total = count.total_characters_of(src, isinstance(src, Material)) return SCode(None, SCmd.INFO_DATA, (HeaderInfo( total, total_lines, count.manupaper_numbers_of(total_lines, rows), count.description_characters_of(src), lines, count.manupaper_numbers_of(lines, rows), count.chapters_of(src), count.episodes_of(src), count.scenes_of(src), count.scodes_of_without_info(src), ), ), '')
def go(self, *args: str) -> SCode: return SCode(self._src, SCmd.GO, args)
def _get_story_info(self, src: Story, config: StoryConfig) -> SCode: version = config.version return SCode(None, SCmd.INFO_CONTENT, (f'version: {version}', ), '')
def look(self, *args: str) -> SCode: return SCode(self._src, SCmd.LOOK, args)
def talk(self, *args: str) -> SCode: return SCode(self._src, SCmd.TALK, args)
def _outline_of( self, src: (Story, Chapter, Episode, Scene, Material)) -> SCode: return SCode(None, SCmd.TAG_COMMENT, (src.outline, ), "outline")
def think(self, *args: str) -> SCode: return SCode(self._src, SCmd.THINK, args)
def voice(self, *args: str) -> SCode: return SCode(self._src, SCmd.VOICE, args)
def wear(self, *args: str) -> SCode: return SCode(self._src, SCmd.WEAR, args)