コード例 #1
0
ファイル: easy_writing.py プロジェクト: topman/blog
    def build_database(self, dirname=None, suffix="rst"):
        """Build the links database by searching all the files
        with the specified suffix recursively.

        if dirname is None, then find the current directory
        """
        DB_NAME = "links_database.py"
        import os
        dirname = os.getcwd() if dirname is None else dirname

        for obj in os.listdir(dirname):
            curobj = os.path.join(dirname, obj)
            if os.path.isdir(curobj):
                self.build_database(curobj, suffix)
            elif curobj.split(".")[-1] == suffix:
                # update the database
                try:
                    fh = open(DB_NAME)
                    old = fh.read()
                    fh.close()
                    exec(old)
                except IOError:
                    LINKS = {}
                matches = self.extract_links(curobj)
                for m in matches:
                    link_name = m[0]
                    link = m[1]
                    if link in ["http://", "https://"]:
                        continue
                    if link_name not in LINKS:
                        key = u"%s" % link_name.decode("utf-8")
                        LINKS.update({ key : link })
                from django.utils import simplejson
                tpl = """# coding: utf-8\nLINKS = %s"""
                new = tpl % simplejson.dumps(LINKS, indent=4, ensure_ascii=False)
                new = new.encode("utf-8")
                fh = open(DB_NAME, "w")
                fh.write(new)
                fh.close()
コード例 #2
0
ファイル: easy_writing.py プロジェクト: topman/blog
 def handle_links(self, infile):
     links = self.extract(infile)
     try:
         from links_database import LINKS
     except ImportError:
         LINKS = {}
     out = {}
     for link in links:
         value = ""
         if link in LINKS:
             value = LINKS.get(link)
         out.update({ link : value })
     cont = ""
     tpl = "\n.. _%s: %s"
     for k, v in out.iteritems():
         cont += tpl % (k, v)
     content = self.get_content(infile)
     content += cont
     self.write_content(infile, content)