def test_copytree_common(self): utils.copytree(self.content, self.output) files = [".hidden.txt", "hellosimiki.md", "zen_of_python.txt", "simiki.md"] assert(utils.check_path_exists(self.output)) for f in files: assert(utils.check_path_exists(os.path.join(self.output, f))) assert(not os.path.islink(os.path.join(self.output, "simiki.md")))
def test_copytree_common(self): utils.copytree(self.content, self.output) files = [ ".hidden.txt", "hellosimiki.md", "zen_of_python.txt", "simiki.md" ] assert (utils.check_path_exists(self.output)) for f in files: assert (utils.check_path_exists(os.path.join(self.output, f))) assert (not os.path.islink(os.path.join(self.output, "simiki.md")))
def test_all_files_exist(self): all_fd = self.files + self.dirs + [\ "content/intro/gettingstarted.md",\ "themes/simple"\ ] for fd in all_fd: assert check_path_exists(fd)
def setUp(self): os.chdir(os.path.dirname(__file__)) self.content = "sample" self.output = "output" if utils.check_path_exists(self.output): utils.emptytree(self.output) os.rmdir(self.output)
def __init__(self, config_file): self.config_file = config_file if not check_path_exists(self.config_file): logging.error("{} not exists".format(self.config_file)) sys.exit(1) self.configs = parse_configs(self.config_file) self.current_dir = os.getcwd()
def get_file(self, src, dst): if check_path_exists(dst): logging.warning("{} exists".format(dst)) return # Create parent directory dst_directory = osp.dirname(dst) if not check_path_exists(dst_directory): mkdir_p(dst_directory) logging.info("Creating directory: {}".format(dst_directory)) try: shutil.copyfile(src, dst) logging.info("Creating config file: {}".format(dst)) except (shutil.Error, IOError), e: logging.error(str(e))
def get_file(self, src, dst): if check_path_exists(dst): logging.warning("{} already exists! if you want overwrite it, " \ "please remove it first".format(dst)) return # Create parent directory dst_directory = osp.dirname(dst) if not check_path_exists(dst_directory): mkdir_p(dst_directory) logging.info("Creating directory: {}".format(dst_directory)) try: shutil.copyfile(src, dst) logging.info("Creating config file: {}".format(dst)) except (shutil.Error, IOError), e: logging.error(str(e))
def get_conf(self): dst_config_file = osp.join(self.current_dir, "_config.yml") if check_path_exists(dst_config_file): logging.warning("{} already exists! if you want overwrite it, " \ "please remove it first".format(dst_config_file)) try: shutil.copyfile(self.config_file, dst_config_file) logging.info("Creating config file: {}".format(dst_config_file)) except (shutil.Error, IOError), e: logging.error(str(e))
def __init__(self, config_file): self.config_file = config_file if not check_path_exists(self.config_file): logging.error("{} not exists".format(self.config_file)) sys.exit(1) try: self.configs = parse_configs(self.config_file) except Exception as e: logging.error(str(e)) self.current_dir = os.getcwd()
def __init__(self, config_file, target_path): self.config_file = config_file if not check_path_exists(self.config_file): logging.error("{} not exists".format(self.config_file)) sys.exit(1) try: self.configs = parse_configs(self.config_file) except Exception as e: logging.error(str(e)) self.source_path = os.path.dirname(__file__) self.target_path = target_path
def get_fabfile(self): src_fabfile = osp.join(osp.dirname(__file__), "conf_templates/fabfile.py") dst_fabfile = osp.join(self.current_dir, "fabfile.py") if check_path_exists(dst_fabfile): logging.warning("{} already exists! if you want overwrite it, " \ "please remove it first".format(dst_fabfile)) try: shutil.copyfile(src_fabfile, dst_fabfile) logging.info("Creating config file: {}".format(dst_fabfile)) except (shutil.Error, IOError), e: logging.error(str(e))
def output_to_file(self, html): """Write generated html to file""" category, mdown = self.get_category_and_mdown(self.mdown_file) output_category_path = osp.join(self.site_settings["destination"], category) if not utils.check_path_exists(output_category_path): logging.info( "The output category %s not exists, create it" \ % output_category_path ) os.mkdir(output_category_path) mdown_name = osp.splitext(mdown)[0] output_file = osp.join(output_category_path, mdown_name+".html") with codecs.open(output_file, "wb", "utf-8") as fd: fd.write(html)
def parse_configs(config_file): base_dir = osp.dirname(osp.dirname(osp.realpath(__file__))) if not utils.check_path_exists(config_file): sys.exit(utils.color_msg("error", "{} not exists".format(config_file))) try: with open(config_file, "rb") as fd: configs = yaml.load(fd) except yaml.YAMLError, e: msg = "Yaml format error in {}:\n{}".format( config_file, unicode(str(e), "utf-8") ) sys.exit(utils.color_msg("error", msg))
def parse_configs(config_file): if not check_path_exists(config_file): logging.error("{} not exists".format(config_file)) sys.exit(1) try: with open(config_file, "rb") as fd: configs = yaml.load(fd) except yaml.YAMLError, e: msg = "Yaml format error in {}:\n{}".format( config_file, unicode(str(e), "utf-8") ) logging.error(msg) sys.exit(1)
def write_file(content, ofile, ftype="page"): """Write content to output file. :param content: content to write to ofile :param ofile: output file :param ftype: file type, "page" or "index" """ if ftype == "page": output_category, _ = osp.split(ofile) if not check_path_exists(output_category): logging.info("The output category %s not exists, create it" % output_category) mkdir_p(output_category) with codecs.open(ofile, "wb", "utf-8") as fd: fd.write(content)
def parse_configs(config_file): default_configs = _set_default_configs() if not check_path_exists(config_file): logging.error("{} not exists".format(config_file)) sys.exit(1) try: with open(config_file, "rb") as fd: configs = yaml.load(fd) except yaml.YAMLError, e: msg = "Yaml format error in {}:\n{}".format(config_file, unicode(str(e), "utf-8")) logging.error(msg) sys.exit(1)
def write_file(content, ofile, ftype="page"): """Write content to output file. :param content: content to write to ofile :param ofile: output file :param ftype: file type, "page" or "index" """ if ftype == "page": output_category, _ = os.path.split(ofile) if not check_path_exists(output_category): logging.info( "The output category %s not exists, create it" % output_category ) mkdir_p(output_category) with codecs.open(ofile, "wb", "utf-8") as fd: fd.write(content)
def preview(path, port=8000): if check_path_exists(path): os.chdir(path) else: logger.error("Path {} not exists".format(path)) try: Handler = SimpleHTTPServer.SimpleHTTPRequestHandler httpd = Reuse_TCPServer(("", port), Handler) except OSError as e: logger.error("Could not listen on port {}".format(port)) sys.exit(getattr(e, 'exitcode', 1)) logger.info("Serving at port {}".format(port)) try: httpd.serve_forever() except KeyboardInterrupt as e: logger.info("Shutting down server") httpd.socket.close()
def preview(path, port=8000): if check_path_exists(path): os.chdir(path) else: logger.error("Path {} not exists".format(path)) try: Handler = SimpleHTTPServer.SimpleHTTPRequestHandler httpd = SocketServer.TCPServer(("", port), Handler) except OSError as e: logger.error("Could not listen on port {}".format(port)) sys.exit(getattr(e, 'exitcode', 1)) logger.info("Serving at port {}".format(port)) try: httpd.serve_forever() except KeyboardInterrupt as e: logger.info("Shutting down server") httpd.socket.close()
def setUp(self): self.content = "sample" self.output = "output" if utils.check_path_exists(self.output): utils.emptytree(self.output) os.rmdir(self.output)
def test_mkdir_p(self): path = os.path.join(self.output, "dir1/dir2/dir3") utils.mkdir_p(path) assert (utils.check_path_exists(path))
def test_listdir_nohidden(self): hidden_file = os.path.join(self.content, ".hidden.txt") assert (utils.check_path_exists(hidden_file))
def create_new_wiki(source, category, filename, title, date): try: meta = "\n".join([ "---", "title: \"{}\"".format(title), "date: {}".format(date), "---", ]) + "\n\n" except Exception, e: logger.error(str(e)) sys.exit(1) category_path = osp.join(source, category) if not check_path_exists(category_path): os.mkdir(category_path) logger.info("Creating category {}.".format(category)) fn = osp.join(category_path, filename) if check_path_exists(fn): logger.warning("wiki file exists: {}".format(fn)) else: logger.info("Creating wiki: {}".format(fn)) with codecs.open(fn, "wb", "utf-8") as fd: fd.write(meta) def install_theme(current_dir, theme_name): """Copy static directory under theme to output directory""" src_theme = osp.join(current_dir, "themes/{}/static".format(theme_name))
def tearDown(self): if utils.check_path_exists(self.output): utils.emptytree(self.output) os.rmdir(self.output)
def create_new_wiki(source, category, filename, title, date): try: meta = "\n".join([ "---", "title: \"{}\"".format(title), "date: {}".format(date), "---", ]) + "\n\n" except Exception, e: logger.error(str(e)) sys.exit(1) category_path = os.path.join(source, category) if not check_path_exists(category_path): os.mkdir(category_path) logger.info("Creating category {}.".format(category)) fn = os.path.join(category_path, filename) if check_path_exists(fn): logger.warning("wiki file exists: {}".format(fn)) else: logger.info("Creating wiki: {}".format(fn)) with codecs.open(fn, "wb", "utf-8") as fd: fd.write(meta) def install_theme(current_dir, theme_name): """Copy static directory under theme to output directory""" src_theme = os.path.join(