def getTemplSub(self, subName, keyName): name = 'templ.sub.%s.py'%str(subName) subFile = os.path.join(self.getPath('templ'), name) if not os.path.exists(subFile): raise TEAM1201Error('Cannnot find the template sub-file "%s"'%subFile) subStr = read_file(subFile) return eval(subStr)[keyName]
def _get_article_content(self, afile, istxt=False): txt = None if istxt: txt = afile else: if not os.path.exists(afile): slog.error('The file "%s" is inexistance!' % afile) return None, None, None, None txt = read_file(afile) FencedBlockPreprocessor.FENCED_BLOCK_RE = re.compile( r''' (?P<fence>^(?:~{3,}|`{3,}))[ ]* # Opening ``` or ~~~ # Optional {, lang="lang" or lang (\{?\.?(?:lang=")?(?P<lang>[a-zA-Z0-9_+-]*)"?)?[ ]* # Optional highlight lines, single- or double-quote-delimited (hl_lines=(?P<quot>"|')(?P<hl_lines>.*?)(?P=quot))?[ ]* }?[ ]*\n # Optional closing } (?P<code>.*?)(?<=\n) (?P=fence)[ ]*$''', re.MULTILINE | re.DOTALL | re.VERBOSE) fencedcode = FencedCodeExtension() codehilite = CodeHiliteExtension(linenums=False, guess_lang=False) md = markdown.Markdown(extensions=[ 'markdown.extensions.meta', 'markdown.extensions.tables', fencedcode, codehilite, ]) html = md.convert(txt) meta = md.Meta adict = self._get_article_metadata(meta) return html, adict, txt, self._get_medias(txt)
def _get_article_content(self, afile, istxt=False): txt = None if istxt: txt = afile else: if not os.path.exists(afile): slog.error('The file "%s" is inexistance!'%afile) return None, None, None, None txt = read_file(afile) FencedBlockPreprocessor.FENCED_BLOCK_RE = re.compile(r''' (?P<fence>^(?:~{3,}|`{3,}))[ ]* # Opening ``` or ~~~ # Optional {, lang="lang" or lang (\{?\.?(?:lang=")?(?P<lang>[a-zA-Z0-9_+-]*)"?)?[ ]* # Optional highlight lines, single- or double-quote-delimited (hl_lines=(?P<quot>"|')(?P<hl_lines>.*?)(?P=quot))?[ ]* }?[ ]*\n # Optional closing } (?P<code>.*?)(?<=\n) (?P=fence)[ ]*$''', re.MULTILINE | re.DOTALL | re.VERBOSE) fencedcode = FencedCodeExtension() codehilite = CodeHiliteExtension(linenums=False, guess_lang=False) md = markdown.Markdown(extensions=[ 'markdown.extensions.meta', 'markdown.extensions.tables', fencedcode, codehilite, ]) html = md.convert(txt) meta = md.Meta adict = self._get_article_metadata(meta) return html,adict,txt,self._get_medias(txt)
def parseModules(self, files): for path in files: print("parse file: " + os.path.basename(path)) self.module = self.sheetCheck(eval(read_file(path))) for sheet in self.module.get("sheets"): self.parseSheet(sheet) print("parse complete")
def decode_file(luafile): """将 lua文件 解析成 python 对象。 将 luafile 解析成字符串,然后调用 :func:`zrong.lua.decode()` 。 :param str luafile: lua文件路径。 :return: python 对象 :raise: :class:`zrong.base.ZrongError` """ luastr = read_file(luafile) return decode(luastr)
def _rewrite_category(self): md = markdown.Markdown(extensions=[ 'markdown.extensions.meta', ]) num = 0 for adir, name, fpath in self.conf.get_mdfiles('post'): md.convert(read_file(fpath)) cats = [cat.strip() for cat in md.Meta['category'][0].split(',')] if len(cats) > 1: print(name, cats) num = num + 1 print(num)
def _rewrite_category(self): md = markdown.Markdown(extensions=[ 'markdown.extensions.meta', ]) num = 0 for adir,name,fpath in self.conf.get_mdfiles('post'): md.convert(read_file(fpath)) cats = [cat.strip() for cat in md.Meta['category'][0].split(',')] if len(cats)>1: print(name, cats) num = num + 1 print(num)
def parseHero(self, files): tmpStr = '' for path in files: heroId = os.path.basename(path).split('.')[0] # print("parse ect:", heroId) jsontxt = read_file(path) # replace // jsontxt = re.sub(r'\/\/.*$', '', jsontxt, flags=re.M) # replace /**/ jsontxt = re.sub(r'\/\*[\w\W]*?\*\/', '', jsontxt, flags=re.M) # print(jsontxt) obj = json.loads(jsontxt) tmpStr += self._parseHero(obj, heroId) self.saveFile(tmpStr, "etc")
def call(heroPath, sszPath, exportPath): parser = Parser(exportPath) # parse heros files = filter(herosFiles, get_files(heroPath, ["json"])) slog.info("parse: etc.json") parser.parseHero(files) # parse ssz files = filter(confFiles, get_files(sszPath, ["json"])) for path in files: slog.info("parse: %s", os.path.basename(path)) jsontxt = read_file(path) jsontxt = re.sub(r'\/\/.*$', '', jsontxt, flags=re.M) obj = json.loads(jsontxt) parser.parseConf(obj, os.path.basename(path).split('.')[0])
def call(heroPath, sszPath, exportPath): parser = Parser(exportPath) #parse tmpl parser.parseTmpl(heroPath) # parse heros files = filter(herosFiles, get_files(heroPath, ["json"])) slog.info("parse: etc.json") parser.parseHero(files) # parse ssz files = filter(confFiles, get_files(sszPath, ["json"])) for path in files: slog.info("parse: %s", os.path.basename(path)) jsontxt = read_file(path) jsontxt = re.sub(r'\/\/.*$', '', jsontxt, flags=re.M) obj = json.loads(jsontxt) parser.parseConf(obj, os.path.basename(path).split('.')[0])
def parseTmpl(self, path): jsontxt = read_file(os.path.join(path, "tmpl.json")) self.tmpl = json.loads(jsontxt) pass
def write_by_jinja(templ, target, content): templ_txt = read_file(templ) templ = Template(templ_txt) write_file(target, templ.render(content))
def test_read_file(): base.read_file(__file__)