Ejemplo n.º 1
0
def emptytree(directory, exclude_list=None):
    """Delete all the files and dirs under specified directory"""

    if not isinstance(directory, unicode):
        directory = unicode(directory, 'utf-8')
    if not exclude_list:
        exclude_list = []
    for p in os.listdir(directory):
        if p in exclude_list:
            continue
        fp = os.path.join(directory, p)
        if os.path.isdir(fp):
            try:
                shutil.rmtree(fp)
                logger.debug("Delete directory %s", fp)
            except OSError as e:
                logger.error("Unable to delete directory %s: %s",
                             fp, unicode(e))
        elif os.path.isfile(fp):
            try:
                logging.debug("Delete file %s", fp)
                os.remove(fp)
            except OSError as e:
                logger.error("Unable to delete file %s: %s", fp, unicode(e))
        else:
            logger.error("Unable to delete %s, unknown filetype", fp)
Ejemplo n.º 2
0
def emptytree(directory, exclude_list=None):
    """Delete all the files and dirs under specified directory"""

    if not isinstance(directory, unicode):
        directory = unicode(directory, 'utf-8')
    if not exclude_list:
        exclude_list = []
    for p in os.listdir(directory):
        if p in exclude_list:
            continue
        fp = os.path.join(directory, p)
        if os.path.isdir(fp):
            try:
                shutil.rmtree(fp)
                logger.debug("Delete directory %s", fp)
            except OSError as e:
                logger.error("Unable to delete directory %s: %s", fp,
                             unicode(e))
        elif os.path.isfile(fp):
            try:
                logging.debug("Delete file %s", fp)
                os.remove(fp)
            except OSError as e:
                logger.error("Unable to delete file %s: %s", fp, unicode(e))
        else:
            logger.error("Unable to delete %s, unknown filetype", fp)
Ejemplo n.º 3
0
def listdir_nohidden(path):
    """List not hidden files or directories under path"""
    for f in os.listdir(path):
        if isinstance(f, str):
            f = unicode(f)
        if not f.startswith('.'):
            yield f
Ejemplo n.º 4
0
def listdir_nohidden(path):
    """List not hidden files or directories under path"""
    for f in os.listdir(path):
        if isinstance(f, str):
            f = unicode(f)
        if not f.startswith('.'):
            yield f
Ejemplo n.º 5
0
 def test_to_catalog(self):
     catalog_html = self.generator.generate_catalog_html()
     # trip site generated paragraph
     catalog_html = re.sub("(?m)^\s*<p>Site Generated .*?<\/p>$\n", "", catalog_html)
     fd = open(os.path.join(self.wiki_path, "expected_catalog.html"), "rb")
     year = datetime.date.today().year
     expected_html = unicode(fd.read(), "utf-8") % year
     assert catalog_html == expected_html
Ejemplo n.º 6
0
 def test_to_catalog(self):
     catalog_html = self.generator.generate_catalog_html()
     # trip site generated paragraph
     catalog_html = re.sub('(?m)^\s*<p>Site Generated .*?<\/p>$\n', '',
                           catalog_html)
     fd = open(os.path.join(self.wiki_path, 'expected_catalog.html'), "rb")
     year = datetime.date.today().year
     expected_html = unicode(fd.read(), "utf-8") % year
     assert catalog_html == expected_html
Ejemplo n.º 7
0
    def test_to_html(self):
        src_file = os.path.join(self.wiki_path, 'content', 'foo目录',
                                'foo_page_中文.md')
        html = self.generator.to_html(src_file).strip()
        expected_output = os.path.join(self.wiki_path, 'expected_output.html')
        fd = open(expected_output, "rb")
        year = datetime.date.today().year
        expected_html = unicode(fd.read(), "utf-8") % year
        assert html == expected_html

        # load template error
        src_file = os.path.join(self.wiki_path, 'content', 'foo目录',
                                'foo_page_中文.md')
        self.assertRaises(Exception, PageGenerator, self.config,
                          'wrong_basepath', src_file)
Ejemplo n.º 8
0
    def test_to_html(self):
        src_file = os.path.join(self.wiki_path, 'content', 'foo目录',
                                'foo_page_中文.md')
        html = self.generator.to_html(src_file).strip()
        expected_output = os.path.join(self.wiki_path, 'expected_output.html')
        fd = open(expected_output, "rb")
        year = datetime.date.today().year
        expected_html = unicode(fd.read(), "utf-8") % year
        assert html == expected_html

        # load template error
        src_file = os.path.join(self.wiki_path, 'content', 'foo目录',
                                'foo_page_中文.md')
        self.assertRaises(Exception, PageGenerator, self.config,
                          'wrong_basepath', src_file)
Ejemplo n.º 9
0
    def test_to_html(self):
        src_file = os.path.join(self.wiki_path, "content", "foo目录", "foo_page_中文.md")
        html = self.generator.to_html(src_file).strip()
        # trip page updated and site generated paragraph
        html = re.sub('(?sm)\\n\s*<span class="updated">Updated.*?<\/span>', "", html)
        html = re.sub("(?m)^\s*<p>Site Generated .*?<\/p>$\n", "", html)
        expected_output = os.path.join(self.wiki_path, "expected_output.html")
        fd = open(expected_output, "rb")
        year = datetime.date.today().year
        expected_html = unicode(fd.read(), "utf-8") % year
        assert html == expected_html

        # load template error
        src_file = os.path.join(self.wiki_path, "content", "foo目录", "foo_page_中文.md")
        self.assertRaises(Exception, PageGenerator, self.config, "wrong_basepath", src_file)
Ejemplo n.º 10
0
    def test_to_html(self):
        src_file = os.path.join(self.wiki_path, 'content', 'foo目录',
                                'foo_page_中文.md')
        html = self.generator.to_html(src_file).strip()
        # trip page updated and site generated paragraph
        html = re.sub('(?sm)\\n\s*<span class="updated">Updated.*?<\/span>',
                      '', html)
        html = re.sub('(?m)^\s*<p>Site Generated .*?<\/p>$\n', '', html)
        expected_output = os.path.join(self.wiki_path, 'expected_output.html')
        fd = open(expected_output, "rb")
        year = datetime.date.today().year
        expected_html = unicode(fd.read(), "utf-8") % year
        assert html == expected_html

        # load template error
        src_file = os.path.join(self.wiki_path, 'content', 'foo目录',
                                'foo_page_中文.md')
        self.assertRaises(Exception, PageGenerator, self.config,
                          'wrong_basepath', src_file)
Ejemplo n.º 11
0
 def test_logging_init(self):
     l2c = {
         "debug": "blue",
         "info": "green",
         "warning": "yellow",
         "error": "red",
         "critical": "bgred"
     }
     for level in l2c:
         # self.handler.flush()
         self.stream.truncate(0)
         # in python 3.x, truncate(0) would not change the current file pos
         # via <http://stackoverflow.com/a/4330829/1276501>
         self.stream.seek(0)
         func = getattr(self.logger, level)
         func(level)
         expected_output = "[{0}]: {1}" \
             .format(color_msg(l2c[level], level.upper()), level)
         stream_output = self.stream.getvalue().strip()
         if is_py2:
             stream_output = unicode(stream_output)
         self.assertEqual(stream_output, expected_output)
Ejemplo n.º 12
0
    def test_to_html(self):
        src_file = os.path.join(self.wiki_path, 'content', 'foo目录',
                                'foo_page_中文.md')
        html_generator_config = self.config
        html_generator_config["markdown_ext"] = {"wikilinks": None}
        html_generator_generator = PageGenerator(html_generator_config,
                                                 self.wiki_path)
        html = html_generator_generator.to_html(src_file).strip()
        # trip page updated and site generated paragraph
        html = re.sub(
            '(?sm)\\n\s*<span class="updated">Page Updated.*?<\/span>',
            '', html)
        html = re.sub('(?m)^\s*<p>Site Generated .*?<\/p>$\n', '', html)
        expected_output = os.path.join(self.wiki_path, 'expected_output.html')
        fd = open(expected_output, "rb")
        year = datetime.date.today().year
        expected_html = unicode(fd.read(), "utf-8") % year
        assert html.rstrip() == expected_html.rstrip()

        # load template error
        src_file = os.path.join(self.wiki_path, 'content', 'foo目录',
                                'foo_page_中文.md')
        self.assertRaises(Exception, PageGenerator, self.config,
                          'wrong_basepath', src_file)