Example #1
0
 def test_returns_extension_if_file_has_extension(self):
     d = self.get_ext_dir()
     self.eq_(tools.what_ext(["txt", "html"], os.path.join(d, "a")),
              "txt")
     self.eq_(tools.what_ext(["txt", "html"], os.path.join(d, "b")),
              "html")
     self.eq_(tools.what_ext(["txt", "html"], os.path.join(d, "espaƱol")),
              "txt")
Example #2
0
def route_file(cfg, url, data):
    path = os.path.join(cfg['datadir'], data['path'].lstrip('/'))
    ext = tools.what_ext(cfg['extensions'].keys(), path)
    if ext:
        data.update({
            'root_datadir': path + '.' + ext,
            'bl_type': 'entry'
        })
        return data
Example #3
0
def blosxom_process_path_info(args):
    """Process HTTP ``PATH_INFO`` for URI according to path
    specifications, fill in data dict accordingly.

    The paths specification looks like this:

    - ``/foo.html`` and ``/cat/foo.html`` - file foo.* in / and /cat
    - ``/cat`` - category
    - ``/2002`` - category
    - ``/2002`` - year
    - ``/2002/Feb`` and ``/2002/02`` - Year and Month
    - ``/cat/2002/Feb/31`` and ``/cat/2002/02/31``- year and month day
      in category.

    :param args: dict containing the incoming Request object
    """
    request = args['request']
    config = request.get_configuration()
    data = request.get_data()
    pyhttp = request.get_http()

    form = request.get_form()

    # figure out which theme to use.  the theme is determined by
    # looking at the "theme" post-data variable, the "theme" query
    # string variable, the "default_theme" setting in the config.py
    # file, or "html"
    theme = config.get("default_theme", "html")
    if form.has_key("them"):
        theme = form["theme"].value

    data['theme'] = theme

    data['pi_yr'] = ''
    data['pi_mo'] = ''
    data['pi_da'] = ''

    path_info = pyhttp.get("PATH_INFO", "")

    data['root_datadir'] = config['datadir']

    data["pi_bl"] = path_info

    # first we check to see if this is a request for an index and we
    # can pluck the extension (which is certainly a theme) right
    # off.
    newpath, ext = os.path.splitext(path_info)
    if newpath.endswith("/index") and ext:
        # there is a theme-like thing, so that's our new theme and
        # we adjust the path_info to the new filename
        data["theme"] = ext[1:]
        path_info = newpath

    while path_info and path_info.startswith("/"):
        path_info = path_info[1:]

    absolute_path = os.path.join(config["datadir"], path_info)

    path_info = path_info.split("/")

    if os.path.isdir(absolute_path):

        # this is an absolute path

        data['root_datadir'] = absolute_path
        data['bl_type'] = 'dir'

    elif absolute_path.endswith("/index") and \
             os.path.isdir(absolute_path[:-6]):

        # this is an absolute path with /index at the end of it

        data['root_datadir'] = absolute_path[:-6]
        data['bl_type'] = 'dir'

    else:
        # this is either a file or a date

        ext = tools.what_ext(data["extensions"].keys(), absolute_path)
        if not ext:
            # it's possible we didn't find the file because it's got a
            # theme thing at the end--so try removing it and
            # checking again.
            newpath, theme = os.path.splitext(absolute_path)
            if theme:
                ext = tools.what_ext(data["extensions"].keys(), newpath)
                if ext:
                    # there is a theme-like thing, so that's our new
                    # theme and we adjust the absolute_path and
                    # path_info to the new filename
                    data["theme"] = theme[1:]
                    absolute_path = newpath
                    path_info, theme = os.path.splitext("/".join(path_info))
                    path_info = path_info.split("/")

        if ext:
            # this is a file
            data["bl_type"] = "file"
            data["root_datadir"] = absolute_path + "." + ext

        else:
            data["bl_type"] = "dir"

            # it's possible to have category/category/year/month/day
            # (or something like that) so we pluck off the categories
            # here.
            pi_bl = ""
            while len(path_info) > 0 and \
                      not (len(path_info[0]) == 4 and path_info[0].isdigit()):
                pi_bl = os.path.join(pi_bl, path_info.pop(0))

            # handle the case where we do in fact have a category
            # preceeding the date.
            if pi_bl:
                pi_bl = pi_bl.replace("\\", "/")
                data["pi_bl"] = pi_bl
                data["root_datadir"] = os.path.join(config["datadir"], pi_bl)

            if len(path_info) > 0:
                item = path_info.pop(0)
                # handle a year token
                if len(item) == 4 and item.isdigit():
                    data['pi_yr'] = item
                    item = ""

                    if (len(path_info) > 0):
                        item = path_info.pop(0)
                        # handle a month token
                        if item in tools.MONTHS:
                            data['pi_mo'] = item
                            item = ""

                            if (len(path_info) > 0):
                                item = path_info.pop(0)
                                # handle a day token
                                if len(item) == 2 and item.isdigit():
                                    data["pi_da"] = item
                                    item = ""

                                    if len(path_info) > 0:
                                        item = path_info.pop(0)

                # if the last item we picked up was "index", then we
                # just ditch it because we don't need it.
                if item == "index":
                    item = ""

                # if we picked off an item we don't recognize and/or
                # there is still stuff in path_info to pluck out, then
                # it's likely this wasn't a date.
                if item or len(path_info) > 0:
                    data["bl_type"] = "dir"
                    data["root_datadir"] = absolute_path

    # construct our final URL
    url = config['base_url']
    if data['pi_bl'].startswith("/") and url.endswith("/"):
        url = url[:-1] + data['pi_bl']
    elif data['pi_bl'].startswith("/") or url.endswith("/"):
        url = url + data["pi_bl"]
    else:
        url = url + "/" + data['pi_bl']
    data['url'] = url

    # set path_info to our latest path_info
    data['path_info'] = path_info

    if data.get("pi_yr"):
        data["truncate"] = config.get("truncate_date", False)
    elif data.get("bl_type") == "dir":
        if data["path_info"] == [''] or data["path_info"] == ['index']:
            data["truncate"] = config.get("truncate_frontpage", True)
        else:
            data["truncate"] = config.get("truncate_category", True)
    else:
        data["truncate"] = False
Example #4
0
def cb_filelist(args):
    req = args["request"]

    pyhttp = req.get_http()
    data = req.get_data()
    config = req.get_configuration()
    page_name = None

    if not (is_trigger(pyhttp, config) or is_frontpage(pyhttp, config)):
        return

    data[INIT_KEY] = 1
    datadir = config["datadir"]
    data['root_datadir'] = config['datadir']
    pagesdir = config["pagesdir"]

    pagesdir = pagesdir.replace("/", os.sep)
    if not pagesdir[-1] == os.sep:
        pagesdir = pagesdir + os.sep

    pathinfo = pyhttp.get("PATH_INFO", "")
    path, ext = os.path.splitext(pathinfo)
    if pathinfo == "/" or path == "/index":
        page_name = "frontpage"
    else:
        page_name = pyhttp["PATH_INFO"][len("/" + TRIGGER) + 1:]

    if not page_name:
        return

    # FIXME - need to do a better job of sanitizing
    page_name = page_name.replace(os.sep, "/")

    if not page_name:
        return

    if page_name[-1] == os.sep:
        page_name = page_name[:-1]
    if page_name.find("/") > 0:
        page_name = page_name[page_name.rfind("/"):]

    # if the page has a theme, we use that.  otherwise
    # we default to the default theme.
    page_name, theme = os.path.splitext(page_name)
    if theme:
        data["theme"] = theme[1:]

    ext = tools.what_ext(data["extensions"].keys(), pagesdir + page_name)

    if not ext:
        return []

    data['root_datadir'] = page_name + '.' + ext
    data['bl_type'] = 'file'
    filename = pagesdir + page_name + "." + ext

    if not os.path.isfile(filename):
        return []

    fe = FileEntry(req, filename, pagesdir)
    # now we evaluate python code blocks
    body = fe.get_data()
    body = eval_python_blocks(req, body)
    body = ("<!-- PAGES PAGE START -->\n\n" +
            body +
            "<!-- PAGES PAGE END -->\n")
    fe.set_data(body)

    fe["absolute_path"] = TRIGGER
    fe["fn"] = page_name
    fe["file_path"] = TRIGGER + "/" + page_name
    fe["template_name"] = "pages"

    data['blog_title_with_path'] = (
        config.get("blog_title", "") + " : " + fe.get("title", ""))

    # set the datadir back
    config["datadir"] = datadir

    return [fe]
Example #5
0
def cb_filelist(args):
    req = args["request"]

    pyhttp = req.get_http()
    data = req.get_data()
    config = req.get_configuration()
    page_name = None

    if not (is_trigger(pyhttp, config) or is_frontpage(pyhttp, config)):
        return

    data[INIT_KEY] = 1
    datadir = config['datadir']
    data['root_datadir'] = config['datadir']
    data['bl_type'] = 'page'
    pagesdir = config['pagesdir']

    if not pagesdir.endswith(os.sep):
        pagesdir = pagesdir + os.sep

    pathinfo = pyhttp.get('PATH_INFO', '')
    path, ext = os.path.splitext(pathinfo)
    if pathinfo in ('/', '/index'):
        page_name = 'frontpage'
    else:
        page_name = pathinfo[len('/' + TRIGGER) + 1:]

    # FIXME - do better job of sanitizing here
    page_name = page_name.replace('\\', '').replace('/', '')

    if not page_name:
        return

    # if the page has a theme, we use that.  otherwise
    # we default to the default theme.
    page_name, theme = os.path.splitext(page_name)
    if theme:
        data['theme'] = theme[1:]

    ext = tools.what_ext(config['extensions'].keys(), pagesdir + page_name)

    if not ext:
        return []

    data['root_datadir'] = page_name + '.' + ext
    filename = pagesdir + page_name + '.' + ext

    if not os.path.isfile(filename):
        return []

    fe = FileEntry(req, filename, pagesdir)

    fe.update({
        'body': eval_python_blocks(req, fe['body']),
        'absolute_path': TRIGGER,
        'fn': page_name,
        'file_path': TRIGGER + '/' + page_name,
    })

    data['bl_type'] = 'page'

    # set the datadir back
    config['datadir'] = datadir

    return [fe]
Example #6
0
 def test_returns_None_if_extension_not_present(self):
     d = self.get_ext_dir()
     self.eq_(tools.what_ext([], os.path.join(d, "a")), None)
     self.eq_(tools.what_ext(["html"], os.path.join(d, "a")), None)
Example #7
0
def cb_filelist(args):
    req = args['request']

    pyhttp = req.get_http()
    data = req.get_data()
    config = req.get_configuration()

    if not is_trigger(pyhttp, config):
        return

    data[INIT_KEY] = 1
    draftdir = config['draftdir']
    draftdir = draftdir.replace('/', os.sep)

    if not draftdir.endswith(os.sep):
        draftdir += os.sep

    if not os.path.exists(draftdir):
        return

    pathinfo = pyhttp.get("PATH_INFO", "")
    path, ext = os.path.splitext(pathinfo)
    draft_name = pyhttp["PATH_INFO"][len("/" + TRIGGER) + 1:]

    if not draft_name:
        trigger = config.get('draft_trigger', TRIGGER)

        entry = EntryBase(req)
        entry['title'] = 'Drafts'
        entry['filename'] = 'drafts'
        entry['file_path'] = 'drafts'
        entry._id = 'drafts'

        files = os.listdir(draftdir)

        baseurl = config.get('base_url', '')
        output = []
        output.append('<ul>')
        for fn in files:
            fn, ext = os.path.splitext(fn)
            output.append('<li><a href="%s/%s/%s.html">%s</a></li>' %
                          (baseurl, trigger, fn, fn))
        output.append('</ul>')
        entry.set_data('\n'.join(output))
        return [entry]

    # FIXME - need to do a better job of sanitizing
    draft_name = draft_name.replace(os.sep, '/')

    if draft_name.endswith(os.sep):
        draft_name = draft_name[:-1]
    if draft_name.find('/') > 0:
        draft_name = draft_name[draft_name.rfind('/'):]

    # If the draft has a theme, we use that. Otherwise we default to
    # the default theme.
    draft_name, theme = os.path.splitext(draft_name)
    if theme:
        data["theme"] = theme[1:]

    ext = tools.what_ext(data["extensions"].keys(), draftdir + draft_name)

    if not ext:
        return []

    data['root_datadir'] = draft_name + '.' + ext
    data['bl_type'] = 'file'
    filename = draftdir + draft_name + '.' + ext

    if not os.path.isfile(filename):
        return []

    fe = FileEntry(req, filename, draftdir)

    trigger = config.get("draft_trigger", TRIGGER)

    fe["absolute_path"] = trigger
    fe["fn"] = draft_name
    fe["file_path"] = trigger + "/" + draft_name

    # FIXME - this is icky
    config['blog_title'] = 'DRAFT : ' + config.get('blog_title', '')

    return [fe]