walktree browser filenames problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dimitri pater

    #1

    walktree browser filenames problem

    Hello,

    I use the following script to list the files and download files from
    my website (99% of the code is from
    http://aspn.activestate.com/ASPN/Coo...Recipe/200131).
    The problem is that the filenames are cut off in the status bar of the
    browser because of the white space (eg 'hello.pdf' works, but 'hello
    there.pdf' is displayed as hello).
    The filenames are displayed correctly in the main page of the browser.
    I tried several things, maybe somebody knows how to do it?

    script: (sorry for the long list)

    #! /usr/bin/python

    import os, sys
    import cgi
    print "Content-type: text/html"
    print
    print "<pre>"

    try:
    import cgitb
    cgitb.enable()
    except:
    sys.stderr = sys.stdout

    print "</pre>"

    def walktree(top = "../upload", depthfirst = True):
    import os, stat, types
    names = os.listdir(top)
    if not depthfirst:
    yield top, names
    for name in names:
    try:
    st = os.lstat(os.pat h.join(top, name))
    except os.error:
    continue
    if stat.S_ISDIR(st .st_mode):
    for (newtop, children) in walktree (os.path.join(t op,
    name), depthfirst):
    yield newtop, children
    if depthfirst:
    yield top, names

    def makeHTMLtable(t op, depthfirst=Fals e):
    from xml.sax.saxutil s import escape # To quote out things like &amp;
    ret = ['<table class="fileList ">\n']
    for top, names in walktree(top):
    ret.append(' <tr><td class="director y">%s</td></tr>\n'%escape(t op))
    for name in names:
    ret.append(' <tr><td class="file"><a
    href=http://e-bench.serpia.co m/upload/%s>%s</a></td></tr>\n' %
    (escape(name),e scape(name)))
    ret.append('</table>')
    return ''.join(ret) # Much faster than += method

    def makeHTMLpage(to p, depthfirst=Fals e):
    return '\n'.join(['<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"',
    '"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
    '<html>'
    '<head>',
    ' <title>Search results</title>',
    ' <style type="text/css">',
    ' table.fileList { text-align: left; }',
    ' td.directory { font-weight: bold; }',
    ' td.file { padding-left: 4em; }',
    ' </style>',
    '</head>',
    '<body>',
    '<h1>Documente n e-bench</h1>',
    makeHTMLtable(t op, depthfirst),
    '<BR><BR><HR><B ><A
    HREF="http://e-bench.serpia.co m">Home</A></B>',
    '<h5>To do: escape chars verwijderen</h5>',
    '<h5>...</h5>',
    '<h5>Aparte HTML template maken als in upload.py</h5>',
    '</body>',
    '</html>'])

    if __name__ == '__main__':
    if len(sys.argv) == 2:
    top = sys.argv[1]
    else: top = '.'
    print makeHTMLpage('. ./upload')

    --
    Please visit dimitri's website: www.serpia.com
  • Richard Brodie

    #2
    Re: walktree browser filenames problem


    "dimitri pater" <dimitri.pater@ gmail.com> wrote in message
    news:mailman.18 77.1107516622.2 2381.python-list@python.org ...
    [color=blue]
    > The problem is that the filenames are cut off in the status bar of the
    > browser because of the white space (eg 'hello.pdf' works, but 'hello
    > there.pdf' is displayed as hello).[/color]

    urllib.quote() should do the trick. You need to follow the rules for
    encoding URIs as well as the HTML ones.


    Comment

    • dimitri pater

      #3
      Re: walktree browser filenames problem

      thanks!
      now it works:
      ret.append(' <tr><td class="file"><a
      href=http://e-bench.serpia.co m/upload/%s>%s</a></td></tr>\n' %
      (urllib.quote(e scape(name)),es cape(name)))

      bye,
      Dimitri


      On Fri, 4 Feb 2005 11:45:17 -0000, Richard Brodie <R.Brodie@rl.ac .uk> wrote:[color=blue]
      >
      > "dimitri pater" <dimitri.pater@ gmail.com> wrote in message
      > news:mailman.18 77.1107516622.2 2381.python-list@python.org ...
      >[color=green]
      > > The problem is that the filenames are cut off in the status bar of the
      > > browser because of the white space (eg 'hello.pdf' works, but 'hello
      > > there.pdf' is displayed as hello).[/color]
      >
      > urllib.quote() should do the trick. You need to follow the rules for
      > encoding URIs as well as the HTML ones.
      >
      > --
      > http://mail.python.org/mailman/listinfo/python-list
      >[/color]


      --
      Please visit dimitri's website: www.serpia.com

      Comment

      Working...