Exemplo n.º 1
0
 def findContentFromSpec(self, spec):
     if os.path.isdir(spec):
         metadata = self._createGroupMetadata(spec)
         return ContentGroup(spec, metadata)
     elif os.path.isfile(spec):
         metadata = self._createItemMetadata(spec)
         return ContentItem(spec, metadata)
     return None
Exemplo n.º 2
0
    def findContentFromRoute(self, route_params):
        year = route_params.get('year')
        month = route_params.get('month')
        day = route_params.get('day')
        slug = route_params.get('slug')

        try:
            if year is not None:
                year = int(year)
            if month is not None:
                month = int(month)
            if day is not None:
                day = int(day)
        except ValueError:
            return None

        ext = route_params.get('ext')
        if ext is None:
            if len(self.supported_extensions) == 1:
                ext = self.supported_extensions[0]

        replacements = {
            'year': '%04d' % year if year is not None else None,
            'month': '%02d' % month if month is not None else None,
            'day': '%02d' % day if day is not None else None,
            'slug': slug,
            'ext': ext
        }
        needs_recapture = False
        if year is None:
            needs_recapture = True
            replacements['year'] = '????'
        if month is None:
            needs_recapture = True
            replacements['month'] = '??'
        if day is None:
            needs_recapture = True
            replacements['day'] = '??'
        if slug is None:
            needs_recapture = True
            replacements['slug'] = '*'
        if ext is None:
            needs_recapture = True
            replacements['ext'] = '*'
        path = os.path.normpath(
            os.path.join(self.fs_endpoint_path,
                         self.path_format % replacements))

        if needs_recapture:
            possible_paths = osutil.glob(path)
            if len(possible_paths) != 1:
                return None
            path = possible_paths[0]
        elif not os.path.isfile(path):
            return None

        metadata = self._parseMetadataFromPath(path)
        return ContentItem(path, metadata)
Exemplo n.º 3
0
 def getSimplePage(self, rel_path):
     app = self.getApp()
     source = app.getSource('pages')
     content_item = ContentItem(
         os.path.join(source.fs_endpoint_path, rel_path),
         {'route_params': {
             'slug': os.path.splitext(rel_path)[0]
         }})
     return app.getPage(source, content_item)
Exemplo n.º 4
0
 def findContentFromRoute(self, route_params):
     slugified_term = route_params[self.taxonomy.term_name]
     spec = '_index[%s]' % slugified_term
     metadata = {
         'term': slugified_term,
         'route_params': {
             self.taxonomy.term_name: slugified_term
         }
     }
     return ContentItem(spec, metadata)
Exemplo n.º 5
0
    def createContent(self, args):
        dt = datetime.date.today()
        date = args.get('date')
        if isinstance(date, str):
            if date == 'today':
                pass  # Keep the default we had.
            elif date == 'tomorrow':
                dt += datetime.timedelta(days=1)
            elif date.startswith('+'):
                try:
                    dt += datetime.timedelta(days=int(date.lstrip('+')))
                except ValueError:
                    raise Exception("Date offsets must be numbers.")
            else:
                try:
                    year, month, day = [int(s) for s in date.split('/')]
                except ValueError:
                    raise Exception(
                        "Dates must be of the form 'YEAR/MONTH/DAY', "
                        "got '%s'." % str(date))
                dt = datetime.date(year, month, day)
        elif isinstance(date, datetime.datetime):
            dt = datetime.date(date.year, date.month, date.day)
        else:
            try:
                dt = datetime.date(int(args.get('year')),
                                   int(args.get('month')),
                                   int(args.get('day')))
            except ValueError:
                raise Exception("Incorrect year/month/day values: %s" % args)

        slug = args.get('slug')
        if slug is None:
            raise Exception("No slug in args: %s" % args)
        slug, ext = os.path.splitext(slug)
        if not ext:
            ext = self.default_auto_format
        year, month, day = dt.year, dt.month, dt.day
        tokens = {
            'slug': args.get('slug'),
            'ext': ext,
            'year': '%04d' % year,
            'month': '%02d' % month,
            'day': '%02d' % day
        }
        rel_path = self.path_format % tokens
        path = os.path.join(self.fs_endpoint_path, rel_path)
        metadata = self._parseMetadataFromPath(path)
        metadata['config'] = {'title': uri_to_title(slug)}
        return ContentItem(path, metadata)
Exemplo n.º 6
0
    def run(self, job, ctx, result):
        year = job['year']
        content_item = ContentItem('_index[%04d]' % year, {
            'year': year,
            'route_params': {
                'year': year
            }
        })
        page = Page(self.source, content_item)

        prev_entry = ctx.previous_entry
        rdr_subs = self._pagebaker.bake(page, prev_entry)

        result['subs'] = rdr_subs
        result['year'] = page.source_metadata['year']
Exemplo n.º 7
0
    def createContent(self, args):
        slug = args.get('slug')
        if not slug:
            slug = '_index'
        path = os.path.join(self.fs_endpoint_path, slug)
        _, ext = os.path.splitext(path)
        if ext == '':
            path = '%s.%s' % (path, self.default_auto_format)

        metadata = self._createItemMetadata(path)
        config = metadata.setdefault('config', {})
        config.update({
            'title':
            uri_to_title(os.path.basename(metadata['route_params']['slug']))
        })
        return ContentItem(path, metadata)
Exemplo n.º 8
0
    def run(self, job, ctx, result):
        term = job['term']
        content_item = ContentItem('_index[%s]' % term, {
            'term': term,
            'route_params': {
                self.taxonomy.term_name: term
            }
        })
        page = Page(self.source, content_item)

        logger.debug("Rendering '%s' page: %s" %
                     (self.taxonomy.name, page.source_metadata['term']))
        prev_entry = ctx.previous_entry
        rdr_subs = self._pagebaker.bake(page, prev_entry)

        result['subs'] = rdr_subs
        result['term'] = page.source_metadata['term']
Exemplo n.º 9
0
    def _getRelatedAssetsContents(self, item):
        spec_no_ext, _ = os.path.splitext(item.spec)
        assets_dir = spec_no_ext + assets_suffix
        try:
            asset_files = list(osutil.listdir(assets_dir))
        except (OSError, FileNotFoundError):
            return None

        assets = []
        for f in asset_files:
            fpath = os.path.join(assets_dir, f)
            name, _ = os.path.splitext(f)
            assets.append(ContentItem(
                fpath,
                {'name': name,
                 'filename': f,
                 '__is_asset': True}))
        return assets
Exemplo n.º 10
0
    def findContentFromRoute(self, route_params):
        uri_path = route_params.get('slug', '')
        if not uri_path:
            uri_path = '_index'
        path = os.path.join(self.fs_endpoint_path, uri_path)
        _, ext = os.path.splitext(path)

        if ext == '':
            paths_to_check = [
                '%s.%s' % (path, e) for e in self.supported_extensions
            ]
        else:
            paths_to_check = [path]
        for path in paths_to_check:
            if os.path.isfile(path):
                metadata = self._createItemMetadata(path)
                return ContentItem(path, metadata)
        return None
Exemplo n.º 11
0
    def findContentFromRoute(self, route_params):
        uri_path = route_params.get('slug', '')
        if uri_path == '':
            uri_path = '_index'

        path = self.fs_endpoint_path
        uri_parts = uri_path.split('/')
        for i, p in enumerate(uri_parts):
            if i == len(uri_parts) - 1:
                # Last part, this is the filename. We need to check for either
                # the name, or the name with the prefix, but also handle a
                # possible extension.
                p_pat = r'(\d+_)?' + re.escape(p)

                _, ext = os.path.splitext(uri_path)
                if ext == '':
                    p_pat += r'\.[\w\d]+'

                found = False
                for name in os.listdir(path):
                    if re.match(p_pat, name):
                        path = os.path.join(path, name)
                        found = True
                        break
                if not found:
                    return None
            else:
                # Find each sub-directory. It can either be a directory with
                # the name itself, or the name with a number prefix.
                p_pat = r'(\d+_)?' + re.escape(p) + '$'
                found = False
                for name in os.listdir(path):
                    if re.match(p_pat, name):
                        path = os.path.join(path, name)
                        found = True
                        break
                if not found:
                    return None

        metadata = self._createItemMetadata(path)
        rel_path = os.path.relpath(path, self.fs_endpoint_path)
        config = self._extractConfigFragment(rel_path)
        metadata.setdefault('config', {}).update(config)
        return ContentItem(path, metadata)
Exemplo n.º 12
0
    def findContentFromRoute(self, route_params):
        # Pages from this source are effectively flattened, so we need to
        # find pages using a brute-force kinda way.
        route_slug = route_params.get('slug', '')
        if not route_slug:
            route_slug = '_index'

        for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path):
            for f in filenames:
                slug, _ = os.path.splitext(f)
                if slug == route_slug:
                    path = os.path.join(dirpath, f)
                    metadata = self._createItemMetadata(path)
                    path = os.path.join(dirpath, f)
                    rel_path = os.path.relpath(path, self.fs_endpoint_path)
                    config = self._extractConfigFragment(rel_path)
                    metadata.setdefault('config', {}).update(config)
                    return ContentItem(path, metadata)
        return None
Exemplo n.º 13
0
    def _makeContentItem(self, rel_path, slug, year, month, day):
        path = os.path.join(self.fs_endpoint_path, rel_path)
        timestamp = datetime.date(year, month, day)
        metadata = {
            'route_params': {
                'slug': slug,
                'year': year,
                'month': month,
                'day': day
            },
            'date': timestamp
        }

        _, ext = os.path.splitext(path)
        if ext:
            fmt = self.auto_formats.get(ext.lstrip('.'))
            if fmt:
                metadata['config'] = {'format': fmt}

        return ContentItem(path, metadata)
Exemplo n.º 14
0
    def getRelatedContents(self, item, relationship):
        if relationship == REL_PARENT_GROUP:
            parent_dir = os.path.dirname(item.spec)
            if len(parent_dir) >= len(self.fs_endpoint_path):
                metadata = self._createGroupMetadata(parent_dir)
                return ContentGroup(parent_dir, metadata)

            # Don't return a group for paths that are outside of our
            # endpoint directory.
            return None

        if relationship == REL_LOGICAL_PARENT_ITEM:
            # If we want the logical parent item of a folder, we find a
            # page file with the same name as the folder.
            if not item.is_group:
                raise ValueError()
            parent_glob = item.spec.rstrip('/\\') + '.*'
            for n in glob.iglob(parent_glob):
                if os.path.isfile(n):
                    metadata = self._createItemMetadata(n)
                    return ContentItem(n, metadata)
            return None

        if relationship == REL_LOGICAL_CHILD_GROUP:
            # If we want the children items of an item, we look for
            # a directory that has the same name as the item's file.
            if item.is_group:
                raise ValueError(
                    "'%s' is a content group and doesn't have a logical "
                    "child. Did you call `family.children` on a group? "
                    "You need to check `is_group` first.")
            dir_path, _ = os.path.splitext(item.spec)
            if os.path.isdir(dir_path):
                metadata = self._createGroupMetadata(dir_path)
                return ContentGroup(dir_path, metadata)
            return None

        return None
Exemplo n.º 15
0
    def getContents(self, group):
        if not self._checkFSEndpoint():
            return None

        parent_path = self.fs_endpoint_path
        if group is not None:
            parent_path = group.spec

        names = filter(_filter_crap_files, osutil.listdir(parent_path))

        items = []
        groups = []
        for name in names:
            path = os.path.join(parent_path, name)
            if self._filterPath(path):
                if os.path.isdir(path):
                    metadata = self._createGroupMetadata(path)
                    groups.append(ContentGroup(path, metadata))
                else:
                    metadata = self._createItemMetadata(path)
                    items.append(ContentItem(path, metadata))
        self._finalizeContent(group, items, groups)
        return items + groups
Exemplo n.º 16
0
 def findContentFromSpec(self, spec):
     metadata = self._parseMetadataFromPath(spec)
     return ContentItem(spec, metadata)
Exemplo n.º 17
0
 def findContentFromRoute(self, route_params):
     year = route_params['year']
     return ContentItem('_index[%s]' % year,
                        {'route_params': {
                            'year': year
                        }})