get_links.py :  » Language-Interface » Python-Knowledge-Engine » pyke-1.1.1 » doc » source » bin » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » Python Knowledge Engine 
Python Knowledge Engine » pyke 1.1.1 » doc » source » bin » get_links.py
#!/usr/bin/env python

# get_links.py

from __future__ import with_statement
import os
import os.path

def run_command(start_dir, outfilename):
    dir = start_dir

    links_seen = set()

    def doctor(link_dir, path):
        # Don't mess with paths that just refer to another link:
        if path.rstrip()[-1] == '_': return path

        path = path.lstrip()

        # Don't mess with paths that point somewhere in the outside universe:
        if path.startswith('http://'): return ' ' + path

        # Prepend link_dir to path
        if link_dir.startswith('./'): path = link_dir[2:] + '/' + path
        elif link_dir != '.': path = link_dir + '/' + path

        # Prepare dir (start_dir, minus initial './')
        if start_dir == '.': dir = ''
        elif start_dir.startswith('./'): dir = start_dir[2:]
        else: dir = start_dir

        rest=' '
        last_dir = None
        while dir and dir != last_dir:
            if path.startswith(dir + '/'):
                ans = rest + path[len(dir) + 1:]
                #print "doctor(%s) abbr:" % (path.rstrip(),), ans
                return ans
            rest += '../'
            last_dir = dir
            dir, ignore = os.path.split(dir)
        ans = rest + path
        #print "doctor(%s) abs:" % (path.rstrip(),), ans
        return ans

    with open(outfilename, "w") as outfile:
        outfile.write("\n")
        while True:
            try:
                with open(os.path.join(dir, 'links')) as links:
                    for line in links:
                        link, path = line.split(':', 1)
                        if link not in links_seen:
                            links_seen.add(link)
                            outfile.write(":".join((link, doctor(dir, path))))
            except IOError:
                pass
            if dir == '.': break
            dir = os.path.dirname(dir)

if __name__ == "__main__":
    import sys
    if len(sys.argv) != 3:
        print >> sys.stderr, "usage: get_links.py dir outfile"
        sys.exit(2)
    run_command(sys.argv[1], sys.argv[2])

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.