Пример #1
0
def setup(pelican):
    dest = os.path.join(
        pelican.output_path,
        'static',
        'jwidget',
        ''
    )
    data_path = os.path.join(
        os.path.abspath(os.path.dirname(__file__)),
        'data'
    )

    if not os.path.isdir(dest):
        try:
            log.info('copying {s} to {d}'.format(
                s=data_path,
                d=dest
                )
            )
            shutil.copytree(data_path, dest)
        except Exception, e:
            log.error('Cannot copy "{s}" to "{d}"'.format(
                s=data_path,
                d=dest
                )
            )
            log.error('The Jamendo plugin will be disabled...')
            return None
Пример #2
0
 def _create_pdf(self, obj, output_path):
     if obj.filename.endswith(".rst"):
         filename = obj.slug + ".pdf"
         output_pdf = os.path.join(output_path, filename)
         # print "Generating pdf for", obj.filename, " in ", output_pdf
         with open(obj.filename) as f:
             self.pdfcreator.createPdf(text=f, output=output_pdf)
         info(u' [ok] writing %s' % output_pdf)
Пример #3
0
def copytree(path, origin, destination, topath=None):
    """Copy path from origin to destination, silent any errors"""
    
    if not topath:
        topath = path
    try:
        fromp = os.path.expanduser(os.path.join(origin, path))
        to = os.path.expanduser(os.path.join(destination, topath))
        shutil.copytree(fromp, to)
        info('copying %s to %s' % (fromp, to))

    except OSError:
        pass
Пример #4
0
 def _write_file(template, localcontext, output_path, name):
     """Render the template write the file."""
     old_locale = locale.setlocale(locale.LC_ALL)
     locale.setlocale(locale.LC_ALL, 'C')
     try:
         output = template.render(localcontext)
     finally:
         locale.setlocale(locale.LC_ALL, old_locale)
     filename = os.sep.join((output_path, name))
     try:
         os.makedirs(os.path.dirname(filename))
     except Exception:
         pass
     with open(filename, 'w', encoding='utf-8') as f:
         f.write(output)
     info(u'writing %s' % filename)
Пример #5
0
    def generate_output(self, writer=None):
        # we don't use the writer passed as argument here
        # since we write our own files
        info(u' Generating PDF files...')
        pdf_path = os.path.join(self.output_path, 'pdf')
        if not os.path.exists(pdf_path):
            try:
                os.mkdir(pdf_path)
            except OSError:
                error("Couldn't create the pdf output folder in " + pdf_path)
                pass

        for article in self.context['articles']:
            self._create_pdf(article, pdf_path)

        for page in self.context['pages']:
            self._create_pdf(page, pdf_path)
Пример #6
0
def copy(path, source, destination, destination_path=None, overwrite=False):
    """Copy path from origin to destination.

    The function is able to copy either files or directories.

    :param path: the path to be copied from the source to the destination
    :param source: the source dir
    :param destination: the destination dir
    :param destination_path: the destination path (optional)
    :param overwrite: wether to overwrite the destination if already exists or not

    """
    if not destination_path:
        destination_path = path

    source_ = os.path.abspath(os.path.expanduser(os.path.join(source, path)))
    destination_ = os.path.abspath(
        os.path.expanduser(os.path.join(destination, destination_path)))

    if os.path.isdir(source_):
        try:
            shutil.copytree(source_, destination_)
            info('copying %s to %s' % (source_, destination_))
        except OSError:
            if overwrite:
                shutil.rmtree(destination_)
                shutil.copytree(source_, destination_)
                info('replacement of %s with %s' % (source_, destination_))

    elif os.path.isfile(source_):
        shutil.copy(source_, destination_)
        info('copying %s to %s' % (source_, destination_))
Пример #7
0
def copy(path, source, destination, destination_path=None, overwrite=False):
    """Copy path from origin to destination.

    The function is able to copy either files or directories.

    :param path: the path to be copied from the source to the destination
    :param source: the source dir
    :param destination: the destination dir
    :param destination_path: the destination path (optional)
    :param overwrite: wether to overwrite the destination if already exists or not

    """
    if not destination_path:
        destination_path = path

    source_ = os.path.abspath(os.path.expanduser(os.path.join(source, path)))
    destination_ = os.path.abspath(
        os.path.expanduser(os.path.join(destination, destination_path)))

    if os.path.isdir(source_):
        try:
            shutil.copytree(source_, destination_)
            info('copying %s to %s' % (source_, destination_))
        except OSError:
            if overwrite:
                shutil.rmtree(destination_)
                shutil.copytree(source_, destination_)
                info('replacement of %s with %s' % (source_, destination_))

    elif os.path.isfile(source_):
        shutil.copy(source_, destination_)
        info('copying %s to %s' % (source_, destination_))
Пример #8
0
    def write_feed(self, elements, context, filename=None, feed_type='atom'):
        """Generate a feed with the list of articles provided

        Return the feed. If no output_path or filename is specified, just
        return the feed object.

        :param elements: the articles to put on the feed.
        :param context: the context to get the feed metadata.
        :param filename: the filename to output.
        :param feed_type: the feed type to use (atom or rss)
        """
        old_locale = locale.setlocale(locale.LC_ALL)
        locale.setlocale(locale.LC_ALL, 'C')
        try:
            self.site_url = context.get('SITEURL', get_relative_path(filename))
            self.feed_url = '%s/%s' % (self.site_url, filename)

            feed = self._create_new_feed(feed_type, context)

            max_items = len(elements)
            if self.settings['FEED_MAX_ITEMS']:
                max_items = min(self.settings['FEED_MAX_ITEMS'], max_items)
            for i in xrange(max_items):
                self._add_item_to_the_feed(feed, elements[i])

            if filename:
                complete_path = os.path.join(self.output_path, filename)
                try:
                    os.makedirs(os.path.dirname(complete_path))
                except Exception:
                    pass
                fp = open(complete_path, 'w')
                feed.write(fp, 'utf-8')
                info('writing %s' % complete_path)

                fp.close()
            return feed
        finally:
            locale.setlocale(locale.LC_ALL, old_locale)