Exemple #1
0
def guess_bookmarks_type(filename, verbose=0):
    if filename == "-":
        fp = sys.stdin
    else:
        fp = open(filename)
    type = bookmarks.get_format(fp)
    if verbose:
        print "%s: %s" % (filename, type)
    else:
        print type
Exemple #2
0
def guess_bookmarks_type(filename, verbose=0):
    if filename == "-":
        fp = sys.stdin
    else:
        fp = open(filename)
    type = bookmarks.get_format(fp)
    if verbose:
        print "%s: %s" % (filename, type)
    else:
        print type
Exemple #3
0
             import urllib
             infile = urllib.urlopen(ifn)
             baseurl = infile.url
         else:
             error(1, "could not open %s: %s" % (ifn, message))
     else:
         baseurl = "file:" + os.path.join(os.getcwd(), ifn)
 #
 # get the parser class, bypassing completely if the formats are the same
 #
 if options.scrape_links:
     from .formats import html_scraper
     parser = html_scraper.Parser(ifn)
     parser.set_baseurl(baseurl)
 else:
     format = bookmarks.get_format(infile)
     if not format:
         error(1, "could not identify input file format")
     parser_class = bookmarks.get_parser_class(format)
     parser = parser_class(ifn)
 #
 # do the real work
 #
 writer_class = bookmarks.get_writer_class(options.output_format)
 parser.feed(infile.read())
 parser.close()
 infile.close()
 root = parser.get_root()
 if options.search:
     from . import search
     from .search import KeywordSearch
Exemple #4
0
             import urllib
             infile = urllib.urlopen(ifn)
             baseurl = infile.url
         else:
             error(1, "could not open %s: %s" % (ifn, message))
     else:
         baseurl = "file:" + os.path.join(os.getcwd(), ifn)
 #
 # get the parser class, bypassing completely if the formats are the same
 #
 if options.scrape_links:
     import formats.html_scraper
     parser = formats.html_scraper.Parser(ifn)
     parser.set_baseurl(baseurl)
 else:
     format = bookmarks.get_format(infile)
     if not format:
         error(1, "could not identify input file format")
     parser_class = bookmarks.get_parser_class(format)
     parser = parser_class(ifn)
 #
 # do the real work
 #
 writer_class = bookmarks.get_writer_class(options.output_format)
 parser.feed(infile.read())
 parser.close()
 infile.close()
 root = parser.get_root()
 if options.search:
     import search
     import search.KeywordSearch
Exemple #5
0
def main():
    try:
        options = Options(sys.argv[1:])
    except getopt.error as message:
        usage(2, message)
    args = options.args
    if options.guess_type:
        if not args:
            args = ["-"]
        for filename in args:
            guess_bookmarks_type(filename, len(args) != 1)
        return
    if len(args) > 2:
        usage(2, "too many command line arguments")
    while len(args) < 2:
        args.append('-')
    [ifn, ofn] = args
    if ifn == '-':
        infile = sys.stdin
    else:
        try:
            infile = open(ifn, 'rb')  # binary in case it's a binary pickle
        except IOError as message:
            if options.scrape_links:
                # try to open as URL
                import urllib
                infile = urllib.urlopen(ifn)
                baseurl = infile.url
            else:
                error(1, "could not open %s: %s" % (ifn, message))
        else:
            baseurl = "file:" + os.path.join(os.getcwd(), ifn)
    #
    # get the parser class, bypassing completely if the formats are the same
    #
    if options.scrape_links:
        from .formats import html_scraper
        parser = html_scraper.Parser(ifn)
        parser.set_baseurl(baseurl)
    else:
        format = bookmarks.get_format(infile)
        if not format:
            error(1, "could not identify input file format")
        parser_class = bookmarks.get_parser_class(format)
        parser = parser_class(ifn)
    #
    # do the real work
    #
    writer_class = bookmarks.get_writer_class(options.output_format)
    parser.feed(infile.read())
    parser.close()
    infile.close()
    root = parser.get_root()
    if options.search:
        from . import search
        from .search import KeywordSearch
        search_options = KeywordSearch.KeywordOptions()
        search_options.set_keywords(str.join(options.keywords))
        matcher = search.get_matcher("Keyword", search_options)
        root = search.find_nodes(root, matcher)
        if root is None:
            sys.stderr.write("No matches.\n")
            sys.exit(1)
    writer = writer_class(root)
    if options.export:
        from . import exporter
        export_options = exporter.ExportOptions()
        for s in options.export_fields:
            setattr(export_options, "remove_" + s, 0)
        walker = exporter.ExportWalker(root, export_options)
        walker.walk()
    if options.info:
        report_info(root)
    else:
        try:
            writer.write_tree(get_outfile(ofn))
        except IOError as err:
            # Ignore the error if we lost a pipe into another process.
            if err != errno.EPIPE:
                raise