def valid_output_format(format): try: bookmarks.get_writer_class(format) except ImportError: return 0 else: return 1
# 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(string.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)
# 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 search_options = search.KeywordSearch.KeywordOptions() search_options.set_keywords(string.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)
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