Scrapy, a fast high-level web crawling & scraping framework for Python.
Move guess_scheme() to scrapy.utils.url
Paul Tremberthcae268402d13
diff --git a/scrapy/commands/shell.py b/scrapy/commands/shell.pyindex 5201feb42..7be7f7256 100644--- a/scrapy/commands/shell.py+++ b/scrapy/commands/shell.py@@ -3,40 +3,13 @@ Scrapy ShellSee documentation in docs/topics/shell.rst"""-import re-from six.moves.urllib.parse import urlparsefrom threading import Thread-from w3lib.url import any_to_urifrom scrapy.commands import ScrapyCommandfrom scrapy.shell import Shellfrom scrapy.http import Request-from scrapy.utils.url import add_http_if_no_schemefrom scrapy.utils.spider import spidercls_for_request, DefaultSpider---def guess_scheme(url):- """Given an URL as string,- returns a FileURI if it looks like a file path,- otherwise returns an HTTP URL- """- parts = urlparse(url)- if parts.scheme:- return url- # Note: this does not match Windows filepath- if re.match(r'''^ # start with...- (- \. # ...a single dot,- (- \. | [^/\.]+ # optionally followed by- )? # either a second dot or some characters- )? # optional match of ".", ".." or ".blabla"- / # at least one "/" for a file path,- . # and something after the "/"- ''', parts.path, flags=re.VERBOSE):- return any_to_uri(url)- else:- return add_http_if_no_scheme(url)+from scrapy.utils.url import guess_schemeclass Command(ScrapyCommand):diff --git a/scrapy/utils/url.py b/scrapy/utils/url.pyindex 0acbbb6ab..4b47566e5 100644--- a/scrapy/utils/url.py+++ b/scrapy/utils/url.py@@ -122,3 +122,27 @@ def add_http_if_no_scheme(url):url = scheme + urlreturn url+++def guess_scheme(url):+ """Given an URL as string,+ returns a FileURI if it looks like a file path,+ otherwise returns an HTTP URL+ """+ parts = urlparse(url)+ if parts.scheme:+ return url+ # Note: this does not match Windows filepath+ if re.match(r'''^ # start with...+ (+ \. # ...a single dot,+ (+ \. | [^/\.]+ # optionally followed by+ )? # either a second dot or some characters+ )? # optional match of ".", ".." or ".blabla"+ / # at least one "/" for a file path,+ . # and something after the "/"+ ''', parts.path, flags=re.VERBOSE):+ return any_to_uri(url)+ else:+ return add_http_if_no_scheme(url)diff --git a/tests/test_command_shell.py b/tests/test_command_shell.pyindex a61d520fa..35a5fa21a 100644--- a/tests/test_command_shell.py+++ b/tests/test_command_shell.py@@ -3,9 +3,9 @@ from os.path import joinfrom twisted.trial import unittestfrom twisted.internet import defer-from scrapy.commands.shell import guess_schemefrom scrapy.utils.testsite import SiteTestfrom scrapy.utils.testproc import ProcessTest+from scrapy.utils.url import guess_schemefrom tests import tests_datadir