Ejemplo n.º 1
0
 def test_copy_dir_same_path(self):
     self._create_dir('a')
     self._create_file('a', 'a.txt')
     utils.copy(os.path.join(self.root_dir, 'a'),
                os.path.join(self.root_dir, 'b'))
     self._exist_dir('b')
     self._exist_file('b', 'a.txt')
Ejemplo n.º 2
0
 def test_copy_file_different_path(self):
     self._create_dir("a")
     self._create_dir("b")
     self._create_file("a", "a.txt")
     utils.copy(os.path.join(self.root_dir, "a", "a.txt"), os.path.join(self.root_dir, "b", "b.txt"))
     self._exist_dir("b")
     self._exist_file("b", "b.txt")
Ejemplo n.º 3
0
 def test_copy_dir_same_path(self):
     self._create_dir('a')
     self._create_file('a', 'a.txt')
     utils.copy(os.path.join(self.root_dir, 'a'),
                os.path.join(self.root_dir, 'b'))
     self._exist_dir('b')
     self._exist_file('b', 'a.txt')
Ejemplo n.º 4
0
 def _copy_paths(self, paths, source, destination, output_path, final_path=None):
     """Copy all the paths from source to destination"""
     for path in paths:
         if final_path:
             copy(os.path.join(source, path), os.path.join(output_path, destination, final_path))
         else:
             copy(os.path.join(source, path), os.path.join(output_path, destination, path))
Ejemplo n.º 5
0
 def test_copy_file_create_dirs(self):
     self._create_file("a.txt")
     utils.copy(os.path.join(self.root_dir, "a.txt"), os.path.join(self.root_dir, "b0", "b1", "b2", "b3", "b.txt"))
     self._exist_dir("b0")
     self._exist_dir("b0", "b1")
     self._exist_dir("b0", "b1", "b2")
     self._exist_dir("b0", "b1", "b2", "b3")
     self._exist_file("b0", "b1", "b2", "b3", "b.txt")
Ejemplo n.º 6
0
    def generate_output(self, writer):

        self._copy_paths(self.settings["STATIC_PATHS"], self.path, "static", self.output_path)
        self._copy_paths(self.settings["THEME_STATIC_PATHS"], self.theme, "theme", self.output_path, ".")

        # copy all the files needed
        for source, destination in self.settings["FILES_TO_COPY"]:
            copy(source, self.path, self.output_path, destination, overwrite=True)
Ejemplo n.º 7
0
 def test_copy_dir_different_path(self):
     self._create_dir('a0')
     self._create_dir('a0', 'a1')
     self._create_file('a0', 'a1', 'a.txt')
     self._create_dir('b0')
     utils.copy(os.path.join(self.root_dir, 'a0', 'a1'),
                os.path.join(self.root_dir, 'b0', 'b1'))
     self._exist_dir('b0', 'b1')
     self._exist_file('b0', 'b1', 'a.txt')
Ejemplo n.º 8
0
 def test_copy_file_create_dirs(self):
     self._create_file('a.txt')
     utils.copy(os.path.join(self.root_dir, 'a.txt'),
                os.path.join(self.root_dir, 'b0', 'b1', 'b2', 'b3', 'b.txt'))
     self._exist_dir('b0')
     self._exist_dir('b0', 'b1')
     self._exist_dir('b0', 'b1', 'b2')
     self._exist_dir('b0', 'b1', 'b2', 'b3')
     self._exist_file('b0', 'b1', 'b2', 'b3', 'b.txt')
Ejemplo n.º 9
0
    def generate_output(self, writer):
        self._copy_paths(self.settings['STATIC_PATHS'], self.path,
                         'static', self.output_path)
        self._copy_paths(self.settings['THEME_STATIC_PATHS'], self.theme,
                         'theme', self.output_path, '.')

        # copy all the files needed
        for source, destination in self.settings['FILES_TO_COPY']:
            copy(source, self.path, self.output_path, destination, overwrite=True)
Ejemplo n.º 10
0
 def test_copy_file_create_dirs(self):
     self._create_file('a.txt')
     utils.copy(os.path.join(self.root_dir, 'a.txt'),
                os.path.join(self.root_dir, 'b0', 'b1', 'b2', 'b3', 'b.txt'))
     self._exist_dir('b0')
     self._exist_dir('b0', 'b1')
     self._exist_dir('b0', 'b1', 'b2')
     self._exist_dir('b0', 'b1', 'b2', 'b3')
     self._exist_file('b0', 'b1', 'b2', 'b3', 'b.txt')
Ejemplo n.º 11
0
 def test_copy_dir_different_path(self):
     self._create_dir('a0')
     self._create_dir('a0', 'a1')
     self._create_file('a0', 'a1', 'a.txt')
     self._create_dir('b0')
     utils.copy(os.path.join(self.root_dir, 'a0', 'a1'),
                os.path.join(self.root_dir, 'b0', 'b1'))
     self._exist_dir('b0', 'b1')
     self._exist_file('b0', 'b1', 'a.txt')
Ejemplo n.º 12
0
 def _copy_paths(self, paths, source, destination, output_path,
                 final_path=None):
     """Copy all the paths from source to destination"""
     for path in paths:
         if final_path:
             copy(os.path.join(source, path),
                  os.path.join(output_path, destination, final_path))
         else:
             copy(os.path.join(source, path),
                  os.path.join(output_path, destination, path))
Ejemplo n.º 13
0
 def _copy_paths(self,
                 paths,
                 source,
                 destination,
                 output_path,
                 final_path=None):
     """Copy all the paths from source to destination"""
     for path in paths:
         copy(path, source, os.path.join(output_path, destination),
              final_path)
Ejemplo n.º 14
0
    def generate_output(self, writer):

        self._copy_paths(self.settings['STATIC_PATHS'], self.path,
                         'static', self.output_path)
        self._copy_paths(self.settings['THEME_STATIC_PATHS'], self.theme,
                         'theme', self.output_path, '.')

        # copy all the files needed
        for source, destination in self.settings['FILES_TO_COPY']:
            copy(source, self.path, self.output_path, destination,
                 overwrite=True)
Ejemplo n.º 15
0
def write_site(ctx):
    output_post_dir = os.path.join(SITE_DIR, 'posts')

    clean_output_dir(SITE_DIR, [])
    copy(SITE_ROOT_DIR, SITE_DIR)
    mkdir_p(output_post_dir)
    write_post_to_dir = functools.partial(write_post, output_post_dir, ctx)
    posts = itertools.imap(write_post_to_dir, get_posts())
    posts = itertools.ifilter(lambda x: x.get('published'), posts)
    posts = sorted(list(posts), key=lambda x: x.get('date'), reverse=True)
    write_index(SITE_DIR, ctx, posts)
    write_feed(SITE_DIR, ctx, posts)
Ejemplo n.º 16
0
def write_site(ctx):
    output_post_dir = os.path.join(SITE_DIR, 'posts')

    clean_output_dir(SITE_DIR, [])
    copy(SITE_ROOT_DIR, SITE_DIR)
    mkdir_p(output_post_dir)
    write_post_to_dir = functools.partial(write_post, output_post_dir, ctx)
    posts = itertools.imap(write_post_to_dir, get_posts())
    posts = itertools.ifilter(lambda x: x.get('published'), posts)
    posts = sorted(list(posts), key=lambda x: x.get('date'), reverse=True)
    write_index(SITE_DIR, ctx, posts)
    write_feed(SITE_DIR, ctx, posts)
Ejemplo n.º 17
0
    def _generate_output_for(self, writer, kind):
        extensions = list(STATIC_EXTENSIONS)
        extensions.extend([ext.upper() for ext in STATIC_EXTENSIONS])
        if 'STATIC_EXTENSIONS' in self.settings:
            extensions.extend(self.settings['STATIC_EXTENSIONS'])

        for f in self.get_files(
                self.settings[kind + '_PATHS'],
                exclude=self.settings[kind + '_EXCLUDES'],
                extensions=tuple(extensions)):
            # hack, remove "pages/" and put the HiPages directly
            # in the root, instead
            t = f
            if f.startswith("pages/"):
                t = f.split("pages/")[1]
            utils.copy(os.path.join(self.path, f),
                       os.path.join(self.output_path, t.lower()))
Ejemplo n.º 18
0
    def generate_context(self):
        for f in self.get_files(self.settings.get('CONVERT_PATHS', [])):

          static_file = self.readers.read_file(
              base_path=self.path, path=f, content_class=Static,
              fmt='static', context=self.context)

          match_ext=[src_ext for( src_ext, rule) in self.rules if str(f).endswith(src_ext)]
          if len(match_ext)!=1 or match_ext[0]!='':
            source_path = os.path.join(self.path, static_file.source_path)
            temp_path=mkdtemp()
            dst_path=os.path.join(self.output_path, os.path.dirname(static_file.save_as))
            mkdir_p(dst_path)
            content=self.cache.get_cached_data(source_path)
            if content:
              with open(temp_path+"/tmp.zip", "wb") as zfile:
                zfile.write(content)
              with ZipFile(temp_path+"/tmp.zip", "r") as zfile:
                zfile.extractall(temp_path)
              os.remove(temp_path+"/tmp.zip")
            else:
              src_ext, rule=[(src_ext, rule) for( src_ext, rule) in self.rules if str(source_path).endswith(src_ext)][0]
              call(rule.format(src=source_path, src_path=os.path.dirname(source_path), basename=os.path.basename(source_path).replace(src_ext,''), dst_path=temp_path+'/'), shell=True)
              zf = ZipFile(temp_path+"/tmp.zip", "w")
              for dirname, subdirs, files in os.walk(temp_path):
                #zf.write(dirname)
                for filename in files:
                  if filename=="tmp.zip":
                    continue
                  zf.write(os.path.join(dirname, filename), filename)
              zf.close()
              with open(temp_path+"/tmp.zip", "rb") as zfile:
                content=zfile.read()
              os.remove(temp_path+"/tmp.zip")
              self.cache.cache_data(source_path, content)
            copy(temp_path, dst_path)
            rmtree(temp_path)
          else:
            source_path = os.path.join(self.path, static_file.source_path)
            save_as = os.path.join(self.output_path, static_file.save_as)
            mkdir_p(os.path.dirname(save_as))
            shutil.copy2(source_path, save_as)
          self.add_source_path(static_file)
        self.cache.save_cache()
Ejemplo n.º 19
0
    def _copy_paths(self, paths, source, destination, output_path,
                    final_path=None):
        """Copy all the paths from source to destination"""
        for path in paths:
            source_path = os.path.join(source, path)

            if final_path:
                if os.path.isfile(source_path):
                    destination_path = os.path.join(output_path, destination,
                                                    final_path,
                                                    os.path.basename(path))
                else:
                    destination_path = os.path.join(output_path, destination,
                                                    final_path)
            else:
                destination_path = os.path.join(output_path, destination, path)

            copy(source_path, destination_path,
                 self.settings['IGNORE_FILES'])
Ejemplo n.º 20
0
    def _copy_paths(self, paths, source, destination, output_path,
                    final_path=None):
        """Copy all the paths from source to destination"""
        for path in paths:
            source_path = os.path.join(source, path)

            if final_path:
                if os.path.isfile(source_path):
                    destination_path = os.path.join(output_path, destination,
                                                    final_path,
                                                    os.path.basename(path))
                else:
                    destination_path = os.path.join(output_path, destination,
                                                    final_path)
            else:
                destination_path = os.path.join(output_path, destination, path)

            copy(source_path, destination_path,
                 self.settings['IGNORE_FILES'])
Ejemplo n.º 21
0
def copy_assets(content, output, file_list):
    """
    Copy assets from content folders to output folders

    Parameters
    ----------
    content: a string, the file path of the content directoy
    output: a string, the file path of the output directory

    file_list: list
        List of files to be transferred

    Output
    ------
    Copies files from content to output
    """

    if not os.path.exists(output):
        os.makedirs(output)
    for file_ in file_list:
        file_content = os.path.join(content, file_)
        logger.info("psa: copy_assets %s to %s" % (file_content, output))
        utils.copy(file_content, output)
Ejemplo n.º 22
0
 def _copy_staticfile(self, sc):
     source_path = os.path.join(self.path, sc.source_path)
     save_as = os.path.join(self.output_path, sc.save_as)
     self._mkdir(os.path.dirname(save_as))
     copy(source_path, save_as)
     logger.info('Copying %s to %s', sc.source_path, sc.save_as)
Ejemplo n.º 23
0
 def _copy_paths(self, paths, source, destination, output_path,
         final_path=None):
     """Copy all the paths from source to destination"""
     for path in paths:
         copy(path, source, os.path.join(output_path, destination), final_path,
                 overwrite=True)
Ejemplo n.º 24
0
 def test_copy_file_same_path(self):
     self._create_file("a.txt")
     utils.copy(os.path.join(self.root_dir, "a.txt"), os.path.join(self.root_dir, "b.txt"))
     self._exist_file("b.txt")
Ejemplo n.º 25
0
 def _create_source(self, obj, output_path):
     output_path = os.path.splitext(obj.save_as)[0]
     dest = os.path.join(output_path, output_path + self.output_extension)
     copy('', obj.source_path, dest)
Ejemplo n.º 26
0
 def _create_source(self, obj):
     output_path, _ = os.path.splitext(obj.save_as)
     dest = os.path.join(self.output_path,
                         output_path + self.output_extension)
     copy('', obj.source_path, dest)
Ejemplo n.º 27
0
 def _create_source(self, obj, output_path):
     filename = os.path.splitext(obj.save_as)[0]
     dest = os.path.join(output_path, filename + self.output_extension)
     copy('', obj.filename, dest)
Ejemplo n.º 28
0
 def _create_source(self, obj, output_path):
     output_path = os.path.splitext(obj.save_as)[0]
     dest = os.path.join(output_path, output_path + self.output_extension)
     copy("", obj.source_path, dest)
Ejemplo n.º 29
0
 def _create_source(self, obj, output_path):
     filename = os.path.splitext(obj.save_as)[0]
     dest = os.path.join(output_path, filename + self.output_extension)
     copy('', obj.filename, dest)
Ejemplo n.º 30
0
 def _copy_staticfile(self, sc):
     source_path = os.path.join(self.path, sc.source_path)
     save_as = os.path.join(self.output_path, sc.save_as)
     self._mkdir(os.path.dirname(save_as))
     copy(source_path, save_as)
     logger.info('Copying %s to %s', sc.source_path, sc.save_as)
Ejemplo n.º 31
0
 def _create_source(self, obj):
     output_path, _ = os.path.splitext(obj.save_as)
     dest = os.path.join(self.output_path,
                         output_path + self.output_extension)
     copy(obj.source_path, dest)
Ejemplo n.º 32
0
def get_gallery(name, params={}):
    """
    Look for images in the gallery dir, process all images and create a
    gallery.

    :param String name: Name of the gallery
    :param Dict params: Additional parameters for the processing
    """
    settings = prepare_settings(params)

    # check content path
    content_path = os.path.join(generator.settings.get("PATH"), settings.get("content_path"))
    gallery_content_path = os.path.join(content_path, name)

    if not os.path.isdir(gallery_content_path):
        logging.warning("Unable to find gallery: %s", name)
        return None

    # prepare output directories
    output_path = os.path.join(generator.settings.get("OUTPUT_PATH"), settings.get("output_path"), name)
    thumbnail_path = os.path.join(output_path, settings.get("thumbnail_path"))

    # create directories
    utils.mkdir_p(output_path)
    if settings.get("thumbnail_create") == True:
        utils.mkdir_p(thumbnail_path)

    # process the images
    images = {}
    for filename in os.listdir(gallery_content_path):
        if not os.path.isfile(os.path.join(gallery_content_path, filename)):
            continue

        image = {}
        # copy original
        if settings.get("copy_original") == True:
            utils.copy(filename, gallery_content_path, output_path, "./")
            image["filename"] = filename

        # load the image
        img = Image.open(os.path.join(gallery_content_path, filename))
        # process the thumbnail
        if settings.get("thumbnail_create") == True:
            size = settings.get("thumbnail_size")
            if type(size) == int:
                size = (size, size)

            mode = settings.get("thumbnail_mode")
            if mode == "fit":
                img_new = ImageOps.fit(img, size)
            else:
                img_new = ImageOps.fit(img, size)

            img_new.save(os.path.join(thumbnail_path, filename))
            image["thumbnail"] = settings.get("thumbnail_path") + "/" + filename

        # process the image sizes
        img_sizes = {}
        for sname, sopt in settings.get("sizes").iteritems():
            size = sopt.get("size")
            if type(size) == int:
                size = (size, size)
            x, y = img.size
            if x > size[0]:
                y = int(max(y * size[0] / x, 1))
                x = int(size[0])
            if y > size[1]:
                x = int(max(x * size[1] / y, 1))
                y = int(size[1])
            size = (x, y)
            img_new = img.resize(size)
            img_name = sopt.get("name", "{root}_{size_name}.{ext}").format(
                root=os.path.splitext(filename)[0], ext=os.path.splitext(filename)[1][1:], size_name=sname
            )

            img_new.save(os.path.join(output_path, img_name))
            img_sizes[sname] = img_name

        image["sizes"] = img_sizes

        images[filename] = image

    return {"name": name, "images": images}