Skip to content

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.py
index 5201feb42..7be7f7256 100644
--- a/scrapy/commands/shell.py
+++ b/scrapy/commands/shell.py
@@ -3,40 +3,13 @@ Scrapy Shell
See documentation in docs/topics/shell.rst
"""
-import re
-from six.moves.urllib.parse import urlparse
from threading import Thread
-from w3lib.url import any_to_uri
from scrapy.commands import ScrapyCommand
from scrapy.shell import Shell
from scrapy.http import Request
-from scrapy.utils.url import add_http_if_no_scheme
from 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_scheme
class Command(ScrapyCommand):
diff --git a/scrapy/utils/url.py b/scrapy/utils/url.py
index 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 + url
return 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.py
index a61d520fa..35a5fa21a 100644
--- a/tests/test_command_shell.py
+++ b/tests/test_command_shell.py
@@ -3,9 +3,9 @@ from os.path import join
from twisted.trial import unittest
from twisted.internet import defer
-from scrapy.commands.shell import guess_scheme
from scrapy.utils.testsite import SiteTest
from scrapy.utils.testproc import ProcessTest
+from scrapy.utils.url import guess_scheme
from tests import tests_datadir