code string | signature string | docstring string | loss_without_docstring float64 | loss_with_docstring float64 | factor float64 |
|---|---|---|---|---|---|
'''Print warnings about any enabled hazardous options.
This function will print messages complaining about:
* ``--save-headers``
* ``--no-iri``
* ``--output-document``
* ``--ignore-fatal-errors``
'''
enabled_options = []
for option_name in cls.U... | def _warn_unsafe_options(cls, args) | Print warnings about any enabled hazardous options.
This function will print messages complaining about:
* ``--save-headers``
* ``--no-iri``
* ``--output-document``
* ``--ignore-fatal-errors`` | 6.039191 | 3.130624 | 1.92907 |
'''Print warnings about any options that may be silly.'''
if 'page-requisites' in args.span_hosts_allow \
and not args.page_requisites:
_logger.warning(
_('Spanning hosts is allowed for page requisites, '
'but the page requisites option is no... | def _warn_silly_options(cls, args) | Print warnings about any options that may be silly. | 4.920646 | 4.522014 | 1.088154 |
'''Guess whether order of the year, month, day and 12/24 hour.
Returns:
tuple: First item is either str ``ymd``, ``dmy``, ``mdy``
or ``None``.
Second item is either True for 12-hour time or False for 24-hour time
or None.
'''
time_12_score = 0
time_24_score = 0
d... | def guess_datetime_format(lines: Iterable[str], threshold: int=5) \
-> Tuple[Optional[str], Optional[bool]] | Guess whether order of the year, month, day and 12/24 hour.
Returns:
tuple: First item is either str ``ymd``, ``dmy``, ``mdy``
or ``None``.
Second item is either True for 12-hour time or False for 24-hour time
or None. | 2.295828 | 1.838226 | 1.248937 |
'''Parse date/time from a line of text into datetime object.'''
datetime_now = datetime_now or \
datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
year = datetime_now.year
month = datetime_now.month
day = datetime_now.day
hour = 0
minute = 0
second = 0
date_ok ... | def parse_datetime(text: str, date_format: str=None,
is_day_period: Optional[bool]=None,
datetime_now: datetime.datetime=None) \
-> Tuple[datetime.datetime, int, int] | Parse date/time from a line of text into datetime object. | 2.122995 | 2.083925 | 1.018748 |
'''Parse month string into integer.'''
text = text.lower()
try:
return MONTH_MAP[text]
except KeyError:
pass
try:
return MONTH_MAP[text[:3]]
except KeyError:
pass
raise ValueError('Month {} not found.'.format(repr(text))) | def parse_month(text: str) -> int | Parse month string into integer. | 3.180573 | 2.942173 | 1.081029 |
'''Convert two digit year to four digit year.'''
assert 0 <= year <= 99, 'Not a two digit year {}'.format(year)
return year + 1000 if year >= 69 else year + 2000 | def y2k(year: int) -> int | Convert two digit year to four digit year. | 4.451751 | 3.788875 | 1.174953 |
'''Parse CLDR JSON datasets to for date time things.'''
am_strings = set()
pm_strings = set()
month_to_int = {}
for lang in language_codes:
path = os.path.join(directory, 'main', lang, 'ca-gregorian.json')
with open(path) as in_file:
doc = json.load(in_file)
m... | def parse_cldr_json(directory, language_codes=DEFAULT_LANGUAGE_CODES,
massage=True) | Parse CLDR JSON datasets to for date time things. | 2.179244 | 1.966964 | 1.107922 |
'''Check out a connection.
This function is the same as acquire but with extra arguments
concerning proxies.
Coroutine.
'''
if self._host_filter and not self._host_filter.test(host):
connection = yield from \
super().acquire(host, port, use_s... | def acquire_proxy(self, host, port, use_ssl=False, host_key=None,
tunnel=True) | Check out a connection.
This function is the same as acquire but with extra arguments
concerning proxies.
Coroutine. | 3.72249 | 3.003156 | 1.239526 |
'''Establish a TCP tunnel.
Coroutine.
'''
host = '[{}]'.format(address[0]) if ':' in address[0] else address[0]
port = address[1]
request = RawRequest('CONNECT', '{0}:{1}'.format(host, port))
self.add_auth_header(request)
stream = Stream(connection, kee... | def _establish_tunnel(self, connection, address) | Establish a TCP tunnel.
Coroutine. | 4.049041 | 3.829849 | 1.057232 |
'''Return whether the file is likely to be HTML.'''
peeked_data = wpull.string.printable_bytes(
wpull.util.peek_file(file)).lower()
if b'<!doctype html' in peeked_data \
or b'<head' in peeked_data \
or b'<title' in peeked_data \
or b'<html' in peeked... | def is_file(cls, file) | Return whether the file is likely to be HTML. | 4.241719 | 3.50971 | 1.208567 |
'''Convert a HTTP request.
Args:
request: An instance of :class:`.http.request.Request`.
referrer_host (str): The referrering hostname or IP address.
Returns:
Request: An instance of :class:`urllib.request.Request`
'''
new_request = urllib.request.Request(
request.u... | def convert_http_request(request, referrer_host=None) | Convert a HTTP request.
Args:
request: An instance of :class:`.http.request.Request`.
referrer_host (str): The referrering hostname or IP address.
Returns:
Request: An instance of :class:`urllib.request.Request` | 3.669879 | 2.3208 | 1.581299 |
'''Return the header fields as a Message:
Returns:
Message: An instance of :class:`email.message.Message`. If
Python 2, returns an instance of :class:`mimetools.Message`.
'''
if sys.version_info[0] == 2:
return mimetools.Message(io.StringIO(str(self._... | def info(self) | Return the header fields as a Message:
Returns:
Message: An instance of :class:`email.message.Message`. If
Python 2, returns an instance of :class:`mimetools.Message`. | 5.040745 | 2.16404 | 2.329321 |
'''Wrapped ``add_cookie_header``.
Args:
request: An instance of :class:`.http.request.Request`.
referrer_host (str): An hostname or IP address of the referrer
URL.
'''
new_request = convert_http_request(request, referrer_host)
self._cookie... | def add_cookie_header(self, request, referrer_host=None) | Wrapped ``add_cookie_header``.
Args:
request: An instance of :class:`.http.request.Request`.
referrer_host (str): An hostname or IP address of the referrer
URL. | 4.311406 | 2.721541 | 1.584178 |
'''Wrapped ``extract_cookies``.
Args:
response: An instance of :class:`.http.request.Response`.
request: An instance of :class:`.http.request.Request`.
referrer_host (str): An hostname or IP address of the referrer
URL.
'''
new_respons... | def extract_cookies(self, response, request, referrer_host=None) | Wrapped ``extract_cookies``.
Args:
response: An instance of :class:`.http.request.Response`.
request: An instance of :class:`.http.request.Request`.
referrer_host (str): An hostname or IP address of the referrer
URL. | 4.378606 | 2.522736 | 1.735658 |
'''Save the cookie jar if needed.'''
if self._save_filename:
self._cookie_jar.save(
self._save_filename,
ignore_discard=self._keep_session_cookies
) | def close(self) | Save the cookie jar if needed. | 8.068757 | 4.958349 | 1.627307 |
'''Start the executable.
Args:
use_atexit (bool): If True, the process will automatically be
terminated at exit.
'''
assert not self._process
_logger.debug('Starting process %s', self._proc_args)
process_future = asyncio.create_subprocess_ex... | def start(self, use_atexit=True) | Start the executable.
Args:
use_atexit (bool): If True, the process will automatically be
terminated at exit. | 2.887882 | 2.580177 | 1.119258 |
'''Terminate or kill the subprocess.
This function is blocking.
'''
if not self._process:
return
if self._process.returncode is not None:
return
_logger.debug('Terminate process.')
try:
self._process.terminate()
exce... | def close(self) | Terminate or kill the subprocess.
This function is blocking. | 2.562576 | 2.1918 | 1.169165 |
'''Continuously read the stdout for messages.'''
try:
while self._process.returncode is None:
line = yield from self._process.stdout.readline()
_logger.debug('Read stdout line %s', repr(line))
if not line:
break
... | def _read_stdout(self) | Continuously read the stdout for messages. | 4.306691 | 3.597849 | 1.197018 |
'''Continuously read stderr for error messages.'''
try:
while self._process.returncode is None:
line = yield from self._process.stderr.readline()
if not line:
break
if self._stderr_callback:
yield from ... | def _read_stderr(self) | Continuously read stderr for error messages. | 4.683593 | 3.862346 | 1.212629 |
'''Read from connection to file.
Args:
file: A file object or a writer stream.
'''
if file:
file_is_async = hasattr(file, 'drain')
while True:
data = yield from self._connection.read(4096)
if not data:
break
... | def read_file(self, file: Union[IO, asyncio.StreamWriter]=None) | Read from connection to file.
Args:
file: A file object or a writer stream. | 4.337477 | 3.261185 | 1.330031 |
'''Connected the stream if needed.
Coroutine.
'''
if self._connection.closed():
self._connection.reset()
yield from self._connection.connect() | def reconnect(self) | Connected the stream if needed.
Coroutine. | 15.811276 | 6.106815 | 2.58912 |
'''Write a command to the stream.
Args:
command: The command.
Coroutine.
'''
_logger.debug('Write command.')
data = command.to_bytes()
yield from self._connection.write(data)
self._data_event_dispatcher.notify_write(data) | def write_command(self, command: Command) | Write a command to the stream.
Args:
command: The command.
Coroutine. | 7.280462 | 4.736557 | 1.537079 |
'''Read a reply from the stream.
Returns:
.ftp.request.Reply: The reply
Coroutine.
'''
_logger.debug('Read reply')
reply = Reply()
while True:
line = yield from self._connection.readline()
if line[-1:] != b'\n':
... | def read_reply(self) -> Reply | Read a reply from the stream.
Returns:
.ftp.request.Reply: The reply
Coroutine. | 6.868052 | 4.386003 | 1.565902 |
'''Return whether the request can be fetched based on the pool.'''
url_info = request.url_info
user_agent = request.fields.get('User-agent', '')
if self._robots_txt_pool.has_parser(url_info):
return self._robots_txt_pool.can_fetch(url_info, user_agent)
else:
... | def can_fetch_pool(self, request: Request) | Return whether the request can be fetched based on the pool. | 5.717547 | 4.404008 | 1.29826 |
'''Fetch the robots.txt file for the request.
Coroutine.
'''
url_info = request.url_info
url = URLInfo.parse('{0}://{1}/robots.txt'.format(
url_info.scheme, url_info.hostname_with_port)).url
if not file:
file = wpull.body.new_temp_file(os.getcwd(... | def fetch_robots_txt(self, request: Request, file=None) | Fetch the robots.txt file for the request.
Coroutine. | 5.311103 | 4.875911 | 1.089254 |
'''Return whether the request can fetched.
Args:
request: Request.
file: A file object to where the robots.txt contents are written.
Coroutine.
'''
try:
return self.can_fetch_pool(request)
except NotInPoolError:
pass
... | def can_fetch(self, request: Request, file=None) -> bool | Return whether the request can fetched.
Args:
request: Request.
file: A file object to where the robots.txt contents are written.
Coroutine. | 6.85532 | 2.998532 | 2.286226 |
'''Read response and parse the contents into the pool.'''
data = response.body.read(4096)
url_info = original_url_info
try:
self._robots_txt_pool.load_robots_txt(url_info, data)
except ValueError:
_logger.warning(__(
_('Failed to parse {ur... | def _read_content(self, response: Response, original_url_info: URLInfo) | Read response and parse the contents into the pool. | 6.997296 | 5.566297 | 1.257083 |
'''Mark the URL as OK in the pool.'''
_logger.debug(__('Got empty robots.txt for {0}.', url_info.url))
self._robots_txt_pool.load_robots_txt(url_info, '') | def _accept_as_blank(self, url_info: URLInfo) | Mark the URL as OK in the pool. | 11.749733 | 7.259795 | 1.618466 |
'''Format as detached DNS information as text.'''
return '\n'.join(itertools.chain(
(self.fetch_date.strftime('%Y%m%d%H%M%S'), ),
(rr.to_text() for rr in self.resource_records),
(),
)) | def to_text_format(self) | Format as detached DNS information as text. | 9.236078 | 5.649911 | 1.63473 |
'''The first IPv4 address.'''
for info in self._address_infos:
if info.family == socket.AF_INET:
return info | def first_ipv4(self) -> Optional[AddressInfo] | The first IPv4 address. | 4.226957 | 4.263265 | 0.991484 |
'''The first IPV6 address.'''
for info in self._address_infos:
if info.family == socket.AF_INET6:
return info | def first_ipv6(self) -> Optional[AddressInfo] | The first IPV6 address. | 4.399712 | 4.117081 | 1.068648 |
'''Move the first address to the last position.'''
item = self._address_infos.pop(0)
self._address_infos.append(item) | def rotate(self) | Move the first address to the last position. | 11.234889 | 6.316999 | 1.778517 |
'''Resolve hostname.
Args:
host: Hostname.
Returns:
Resolved IP addresses.
Raises:
DNSNotFound if the hostname could not be resolved or
NetworkError if there was an error connecting to DNS servers.
Coroutine.
'''
... | def resolve(self, host: str) -> ResolveResult | Resolve hostname.
Args:
host: Hostname.
Returns:
Resolved IP addresses.
Raises:
DNSNotFound if the hostname could not be resolved or
NetworkError if there was an error connecting to DNS servers.
Coroutine. | 3.316786 | 2.940119 | 1.128113 |
'''Query DNS using Python.
Coroutine.
'''
record_type = {socket.AF_INET: 'A', socket.AF_INET6: 'AAAA'}[family]
event_loop = asyncio.get_event_loop()
query = functools.partial(
self._dns_resolver.query, host, record_type,
source=self._bind_address... | def _query_dns(self, host: str, family: int=socket.AF_INET) \
-> dns.resolver.Answer | Query DNS using Python.
Coroutine. | 3.183479 | 2.930892 | 1.086181 |
'''Query DNS using system resolver.
Coroutine.
'''
event_loop = asyncio.get_event_loop()
query = event_loop.getaddrinfo(host, 0, family=family,
proto=socket.IPPROTO_TCP)
if self._timeout:
query = asyncio.wait_for(query,... | def _getaddrinfo(self, host: str, family: int=socket.AF_UNSPEC) \
-> List[tuple] | Query DNS using system resolver.
Coroutine. | 2.990604 | 2.619635 | 1.141611 |
'''Convert the DNS answer to address info.'''
assert answer.rdtype in (dns.rdatatype.A, dns.rdatatype.AAAA)
if answer.rdtype == dns.rdatatype.A:
family = socket.AF_INET
else:
family = socket.AF_INET6
for record in answer:
ip_address = record.... | def _convert_dns_answer(cls, answer: dns.resolver.Answer) \
-> Iterable[AddressInfo] | Convert the DNS answer to address info. | 2.432552 | 2.398297 | 1.014283 |
'''Convert the result list to address info.'''
for result in results:
family = result[0]
address = result[4]
ip_address = address[0]
if family == socket.AF_INET6:
flow_info = address[2]
control_id = address[3]
e... | def _convert_addrinfo(cls, results: List[tuple]) -> Iterable[AddressInfo] | Convert the result list to address info. | 3.114991 | 2.92907 | 1.063474 |
'''Extract the flow info and control id.'''
results = socket.getaddrinfo(
ip_address, 0, proto=socket.IPPROTO_TCP,
flags=socket.AI_NUMERICHOST)
flow_info = results[0][4][2]
control_id = results[0][4][3]
return flow_info, control_id | def _get_ipv6_info(cls, ip_address: str) -> tuple | Extract the flow info and control id. | 3.914187 | 2.615057 | 1.496788 |
'''Raise FTPServerError if not expected reply code.
Args:
action: Label to use in the exception message.
expected_code: Expected 3 digit code.
reply: Reply from the server.
'''
if isinstance(expected_code, int):
expected_codes = (expected_... | def raise_if_not_match(cls, action: str,
expected_code: Union[int, Sequence[int]],
reply: Reply) | Raise FTPServerError if not expected reply code.
Args:
action: Label to use in the exception message.
expected_code: Expected 3 digit code.
reply: Reply from the server. | 3.266941 | 2.238412 | 1.45949 |
'''Read the welcome message.
Coroutine.
'''
reply = yield from self._control_stream.read_reply()
self.raise_if_not_match(
'Server ready', ReplyCodes.service_ready_for_new_user, reply) | def read_welcome_message(self) | Read the welcome message.
Coroutine. | 14.705783 | 11.987078 | 1.226803 |
'''Log in.
Coroutine.
'''
yield from self._control_stream.write_command(Command('USER', username))
reply = yield from self._control_stream.read_reply()
if reply.code == ReplyCodes.user_logged_in_proceed:
return
self.raise_if_not_match(
... | def login(self, username: str='anonymous', password: str='-wpull-lib@') | Log in.
Coroutine. | 3.792711 | 3.423512 | 1.107842 |
'''Enable passive mode.
Returns:
The address (IP address, port) of the passive port.
Coroutine.
'''
yield from self._control_stream.write_command(Command('PASV'))
reply = yield from self._control_stream.read_reply()
self.raise_if_not_match(
... | def passive_mode(self) -> Tuple[str, int] | Enable passive mode.
Returns:
The address (IP address, port) of the passive port.
Coroutine. | 7.311674 | 5.56839 | 1.313068 |
'''Create and setup a data stream.
This function will set up passive and binary mode and handle
connecting to the data connection.
Args:
connection_factory: A coroutine callback that returns a connection
data_stream_factory: A callback that returns a data stream... | def setup_data_stream(
self,
connection_factory: Callable[[tuple], Connection],
data_stream_factory: Callable[[Connection], DataStream]=DataStream) -> \
DataStream | Create and setup a data stream.
This function will set up passive and binary mode and handle
connecting to the data connection.
Args:
connection_factory: A coroutine callback that returns a connection
data_stream_factory: A callback that returns a data stream
C... | 8.39632 | 5.14132 | 1.633106 |
'''Start sending content on the data stream.
Args:
command: A command that tells the server to send data over the
data connection.
Coroutine.
Returns:
The begin reply.
'''
yield from self._control_stream.write_command(command)
... | def begin_stream(self, command: Command) -> Reply | Start sending content on the data stream.
Args:
command: A command that tells the server to send data over the
data connection.
Coroutine.
Returns:
The begin reply. | 7.802737 | 4.348727 | 1.794258 |
'''Read from the data stream.
Args:
file: A destination file object or a stream writer.
data_stream: The stream of which to read from.
Coroutine.
Returns:
Reply: The final reply.
'''
yield from data_stream.read_file(file=file)
... | def read_stream(self, file: IO, data_stream: DataStream) -> Reply | Read from the data stream.
Args:
file: A destination file object or a stream writer.
data_stream: The stream of which to read from.
Coroutine.
Returns:
Reply: The final reply. | 7.571666 | 3.957274 | 1.913354 |
'''Get size of file.
Coroutine.
'''
yield from self._control_stream.write_command(Command('SIZE', filename))
reply = yield from self._control_stream.read_reply()
self.raise_if_not_match('File size', ReplyCodes.file_status, reply)
try:
return int(re... | def size(self, filename: str) -> int | Get size of file.
Coroutine. | 6.991732 | 6.122795 | 1.141918 |
'''Send restart command.
Coroutine.
'''
yield from self._control_stream.write_command(Command('REST', str(offset)))
reply = yield from self._control_stream.read_reply()
self.raise_if_not_match('Restart', ReplyCodes.requested_file_action_pending_further_information, rep... | def restart(self, offset: int) | Send restart command.
Coroutine. | 12.149048 | 9.050671 | 1.342337 |
'''Get the version string of youtube-dl.'''
process = subprocess.Popen(
[exe_path, '--version'],
stdout=subprocess.PIPE
)
version_string = process.communicate()[0]
version_string = version_string.decode().strip()
assert ' ' not in version_string, version_string
return versi... | def get_version(exe_path='youtube-dl') | Get the version string of youtube-dl. | 2.672759 | 2.803789 | 0.953267 |
'''Return the path prefix and output template.'''
path = self._file_writer_session.extra_resource_path('.youtube-dl')
if not path:
self._temp_dir = tempfile.TemporaryDirectory(
dir=self._root_path, prefix='tmp-wpull-youtubedl'
)
path = '{}/tmp... | def _get_output_template(self) | Return the path prefix and output template. | 7.84901 | 6.415105 | 1.22352 |
'''Write the JSON metadata to WARC.
Uses pywb spec.
'''
uri = 'metadata://{}{}'.format(self._item_session.url_record.url_info.authority,
self._item_session.url_record.url_info.resource)
glob_pattern = self._path_prefix + '*.info.json'
... | def _write_warc_metadata(self) | Write the JSON metadata to WARC.
Uses pywb spec. | 6.752751 | 5.669173 | 1.191135 |
'''Populate the visits from the CDX into the URL table.'''
if not session.args.warc_dedup:
return
iterable = wpull.warc.format.read_cdx(
session.args.warc_dedup,
encoding=session.args.local_encoding or 'utf-8'
)
missing_url_msg = _('The URL (... | def process(self, session: AppSession) | Populate the visits from the CDX into the URL table. | 4.145125 | 3.479849 | 1.19118 |
'''Check if lxml supports the specified encoding.
Returns:
str, None
'''
# XXX: Workaround lxml not liking utf-16-le
try:
lxml.html.HTMLParser(encoding=encoding)
except LookupError:
encoding = encoding.replace('-', '')
else:
return encoding
try:
l... | def to_lxml_encoding(encoding) | Check if lxml supports the specified encoding.
Returns:
str, None | 3.150594 | 2.643881 | 1.191655 |
'''Return an iterator of elements found in the document.
Args:
file: A file object containing the document.
encoding (str): The encoding of the document.
target_class: A class to be used for target parsing.
parser_type (str): The type of parser to use. Ac... | def parse_lxml(self, file, encoding=None, target_class=HTMLParserTarget,
parser_type='html') | Return an iterator of elements found in the document.
Args:
file: A file object containing the document.
encoding (str): The encoding of the document.
target_class: A class to be used for target parsing.
parser_type (str): The type of parser to use. Accepted valu... | 3.465718 | 2.532896 | 1.368283 |
'''Get the doctype from the document.
Returns:
str, None
'''
if encoding:
lxml_encoding = to_lxml_encoding(encoding) or 'latin1'
else:
lxml_encoding = encoding
try:
parser = lxml.etree.XMLParser(encoding=lxml_encoding, rec... | def parse_doctype(cls, file, encoding=None) | Get the doctype from the document.
Returns:
str, None | 3.72657 | 3.319064 | 1.122777 |
'''Get the suitable parser type for the document.
Returns:
str
'''
is_xml = XMLDetector.is_file(file)
doctype = cls.parse_doctype(file, encoding=encoding) or ''
if not doctype and is_xml:
return 'xml'
if 'XHTML' in doctype:
r... | def detect_parser_type(cls, file, encoding=None) | Get the suitable parser type for the document.
Returns:
str | 5.235341 | 3.974987 | 1.317071 |
'''Return a new temporary file.'''
return tempfile.NamedTemporaryFile(
prefix='tmp-wpull-{0}-'.format(hint), suffix='.tmp', dir=directory) | def new_temp_file(directory=None, hint='') | Return a new temporary file. | 6.241229 | 6.400947 | 0.975048 |
'''Return the content of the file.
If this function is invoked, the contents of the entire file is read
and cached.
Returns:
``bytes``: The entire content of the file.
'''
if not self._content_data:
if is_seekable(self.file):
with... | def content(self) | Return the content of the file.
If this function is invoked, the contents of the entire file is read
and cached.
Returns:
``bytes``: The entire content of the file. | 4.799145 | 2.645949 | 1.813771 |
'''Return the size of the file.'''
try:
return os.fstat(self.file.fileno()).st_size
except io.UnsupportedOperation:
pass
if is_seekable(self.file):
with wpull.util.reset_file_offset(self.file):
self.file.seek(0, os.SEEK_END)
... | def size(self) | Return the size of the file. | 4.310344 | 4.142633 | 1.040484 |
'''Convert the body to a :class:`dict`.
Returns:
dict: The items are:
* ``filename`` (string, None): The path of the file.
* ``length`` (int, None): The size of the file.
'''
try:
name = self.file.name
except AttributeErro... | def to_dict(self) | Convert the body to a :class:`dict`.
Returns:
dict: The items are:
* ``filename`` (string, None): The path of the file.
* ``length`` (int, None): The size of the file. | 4.10712 | 2.215971 | 1.853418 |
# Taken from the session docs.
session = self._session_maker()
try:
yield session
session.commit()
except:
session.rollback()
raise
finally:
session.close() | def _session(self) | Provide a transactional scope around a series of operations. | 4.379582 | 2.975161 | 1.472049 |
'''Set SQLite pragmas.
Write-ahead logging, synchronous=NORMAL is used.
'''
_logger.debug('Setting pragmas.')
connection.execute('PRAGMA journal_mode=WAL')
connection.execute('PRAGMA synchronous=NORMAL') | def _apply_pragmas_callback(cls, connection, record) | Set SQLite pragmas.
Write-ahead logging, synchronous=NORMAL is used. | 8.549463 | 2.964961 | 2.8835 |
'''Return whether a parser has been created for the URL.'''
key = self.url_info_key(url_info)
return key in self._parsers | def has_parser(self, url_info: URLInfo) | Return whether a parser has been created for the URL. | 7.123025 | 3.858423 | 1.846098 |
'''Return whether the URL can be fetched.'''
key = self.url_info_key(url_info)
parser = self._parsers[key]
return parser.is_allowed(user_agent, url_info.url) | def can_fetch(self, url_info: URLInfo, user_agent: str) | Return whether the URL can be fetched. | 6.468658 | 5.188527 | 1.246723 |
'''Load the robot.txt file.'''
key = self.url_info_key(url_info)
parser = robotexclusionrulesparser.RobotExclusionRulesParser()
parser.parse(text)
self._parsers[key] = parser | def load_robots_txt(self, url_info: URLInfo, text: str) | Load the robot.txt file. | 5.507194 | 4.73897 | 1.162108 |
'''Create demux document scraper.'''
session.factory.new(
'DemuxDocumentScraper', cls._build_document_scrapers(session)) | def _build_demux_document_scraper(cls, session: AppSession) | Create demux document scraper. | 15.127653 | 10.777643 | 1.403614 |
'''Create the document scrapers.
Returns:
A list of document scrapers
'''
html_parser = session.factory['HTMLParser']
element_walker = session.factory.new('ElementWalker')
scrapers = [
session.factory.new(
'HTMLScraper',
... | def _build_document_scrapers(cls, session: AppSession) | Create the document scrapers.
Returns:
A list of document scrapers | 2.451197 | 2.326387 | 1.05365 |
'''Create the request factory.
A request factory is any callable object that returns a
:class:`.http.Request`. The callable must accept the same
arguments to Request.
Returns:
A callable object
'''
def request_factory(*args, **kwargs):
re... | def _build_request_factory(cls, session: AppSession) | Create the request factory.
A request factory is any callable object that returns a
:class:`.http.Request`. The callable must accept the same
arguments to Request.
Returns:
A callable object | 3.195158 | 2.325691 | 1.373853 |
'''Create the HTTP client.
Returns:
Client: An instance of :class:`.http.Client`.
'''
# TODO:
# recorder = self._build_recorder()
stream_factory = functools.partial(
HTTPStream,
ignore_length=session.args.ignore_length,
ke... | def _build_http_client(cls, session: AppSession) | Create the HTTP client.
Returns:
Client: An instance of :class:`.http.Client`. | 7.477415 | 5.584149 | 1.339043 |
'''Build Web Client.'''
cookie_jar = cls._build_cookie_jar(session)
http_client = cls._build_http_client(session)
redirect_factory = functools.partial(
session.factory.class_map['RedirectTracker'],
max_redirects=session.args.max_redirect
)
return... | def _build_web_client(cls, session: AppSession) | Build Web Client. | 4.448474 | 4.186852 | 1.062487 |
'''Build the cookie jar'''
if not session.args.cookies:
return
if session.args.load_cookies or session.args.save_cookies:
session.factory.set('CookieJar', BetterMozillaCookieJar)
cookie_jar = session.factory.new('CookieJar')
if session.args.loa... | def _build_cookie_jar(cls, session: AppSession) | Build the cookie jar | 3.254334 | 3.211534 | 1.013327 |
'''Build FTP client.'''
return session.factory.new(
'FTPClient',
connection_pool=session.factory['ConnectionPool'],
# TODO: recorder
# recorder=session.factory['DemuxRecorder'],
) | def _build_ftp_client(cls, session: AppSession) | Build FTP client. | 13.932327 | 12.159746 | 1.145775 |
'''Build MITM proxy server.'''
args = session.args
if not (args.phantomjs or args.youtube_dl or args.proxy_server):
return
proxy_server = session.factory.new(
'HTTPProxyServer',
session.factory['HTTPClient'],
)
cookie_jar = session.fa... | def process(self, session: AppSession) | Build MITM proxy server. | 4.955908 | 4.28089 | 1.157682 |
'''Create the Processor
Returns:
Processor: An instance of :class:`.processor.BaseProcessor`.
'''
web_processor = cls._build_web_processor(session)
ftp_processor = cls._build_ftp_processor(session)
delegate_processor = session.factory.new('Processor')
... | def _build_processor(cls, session: AppSession) | Create the Processor
Returns:
Processor: An instance of :class:`.processor.BaseProcessor`. | 4.273889 | 2.953026 | 1.447291 |
'''Build WebProcessor.'''
args = session.args
url_filter = session.factory['DemuxURLFilter']
document_scraper = session.factory['DemuxDocumentScraper']
file_writer = session.factory['FileWriter']
post_data = cls._get_post_data(session.args)
web_client = session.fa... | def _build_web_processor(cls, session: AppSession) | Build WebProcessor. | 3.371771 | 3.322777 | 1.014745 |
'''Build FTPProcessor.'''
ftp_client = session.factory['FTPClient']
fetch_params = session.factory.new(
'FTPProcessorFetchParams',
remove_listing=session.args.remove_listing,
retr_symlinks=session.args.retr_symlinks,
preserve_permissions=session.a... | def _build_ftp_processor(cls, session: AppSession) | Build FTPProcessor. | 4.733621 | 4.367434 | 1.083845 |
'''Return the post data.'''
if args.post_data:
return args.post_data
elif args.post_file:
return args.post_file.read() | def _get_post_data(cls, args) | Return the post data. | 4.028565 | 3.517935 | 1.145151 |
'''Build robots.txt checker.'''
if session.args.robots:
robots_txt_pool = session.factory.new('RobotsTxtPool')
robots_txt_checker = session.factory.new(
'RobotsTxtChecker',
web_client=session.factory['WebClient'],
robots_txt_pool=ro... | def _build_robots_txt_checker(cls, session: AppSession) | Build robots.txt checker. | 4.323327 | 4.059565 | 1.064973 |
'''Build proxy server and PhantomJS client. controller, coprocessor.'''
page_settings = {}
default_headers = NameValueRecord()
for header_string in session.args.header:
default_headers.parse(header_string)
# Since we can only pass a one-to-one mapping to PhantomJS,
... | def _build_phantomjs_coprocessor(cls, session: AppSession, proxy_port: int) | Build proxy server and PhantomJS client. controller, coprocessor. | 4.543847 | 4.127168 | 1.10096 |
'''Build youtube-dl coprocessor.'''
# Test early for executable
wpull.processor.coprocessor.youtubedl.get_version(session.args.youtube_dl_exe)
coprocessor = session.factory.new(
'YoutubeDlCoprocessor',
session.args.youtube_dl_exe,
(session.args.proxy... | def _build_youtube_dl_coprocessor(cls, session: AppSession, proxy_port: int) | Build youtube-dl coprocessor. | 7.809433 | 7.714942 | 1.012248 |
'''Put the application together.
'''
pipelines = self._build_pipelines()
self._factory.new('Application', pipelines)
return self._factory['Application'] | def build(self) -> Application | Put the application together. | 17.324858 | 13.666281 | 1.267708 |
'''Given the hints, return whether the document is supported.
Args:
file: A file object containing the document.
request (:class:`.http.request.Request`): An HTTP request.
response (:class:`.http.request.Response`): An HTTP response.
url_info (:class:`.ur... | def is_supported(cls, file=None, request=None, response=None,
url_info=None) | Given the hints, return whether the document is supported.
Args:
file: A file object containing the document.
request (:class:`.http.request.Request`): An HTTP request.
response (:class:`.http.request.Response`): An HTTP response.
url_info (:class:`.url.URLInfo`)... | 3.759959 | 2.134978 | 1.761123 |
'''Return the links.
This function is a convenience function for calling :meth:`iter_text`
and returning only the links.
'''
if context:
return [item for item in self.iter_text(file, encoding) if item[1]]
else:
return [item[0] for item in self.ite... | def iter_links(self, file, encoding=None, context=False) | Return the links.
This function is a convenience function for calling :meth:`iter_text`
and returning only the links. | 3.580039 | 2.108466 | 1.697935 |
'''Log the final statistics to the user.'''
time_length = datetime.timedelta(
seconds=int(stats.stop_time - stats.start_time)
)
file_size = wpull.string.format_size(stats.size)
if stats.bandwidth_meter.num_samples:
speed = stats.bandwidth_meter.speed()
... | def _print_stats(cls, stats: Statistics, human_format_speed: bool=True) | Log the final statistics to the user. | 3.485043 | 3.255297 | 1.070576 |
'''Return whether a content body is not expected.'''
if 'Content-Length' not in response.fields \
and 'Transfer-Encoding' not in response.fields \
and (
response.status_code in no_content_codes
or request.method.upper() == 'HEAD'
):
ret... | def is_no_body(request, response, no_content_codes=DEFAULT_NO_CONTENT_CODES) | Return whether a content body is not expected. | 3.932461 | 3.283056 | 1.197805 |
'''Send the request's HTTP status line and header fields.
This class will automatically connect the connection if the
connection is closed.
Coroutine.
'''
_logger.debug('Sending headers.')
if hasattr(request, 'prepare_for_send'):
request.prepare_for... | def write_request(self, request, full_url=False) | Send the request's HTTP status line and header fields.
This class will automatically connect the connection if the
connection is closed.
Coroutine. | 9.886933 | 6.343119 | 1.558686 |
'''Send the request's content body.
Coroutine.
'''
_logger.debug('Sending body.')
file_is_async = (asyncio.iscoroutine(file.read) or
asyncio.iscoroutinefunction(file.read))
_logger.debug(__('Body is async: {0}', file_is_async))
if leng... | def write_body(self, file, length=None) | Send the request's content body.
Coroutine. | 4.017354 | 3.713218 | 1.081906 |
'''Read the response's HTTP status line and header fields.
Coroutine.
'''
_logger.debug('Reading header.')
if response is None:
response = Response()
header_lines = []
bytes_read = 0
while True:
try:
data = yield... | def read_response(self, response=None) | Read the response's HTTP status line and header fields.
Coroutine. | 3.702469 | 3.219682 | 1.149949 |
'''Read the response's content body.
Coroutine.
'''
if is_no_body(request, response):
return
if not raw:
self._setup_decompressor(response)
read_strategy = self.get_read_strategy(response)
if self._ignore_length and read_strategy == 'le... | def read_body(self, request, response, file=None, raw=False) | Read the response's content body.
Coroutine. | 4.284655 | 3.953483 | 1.083767 |
'''Read the response until the connection closes.
Coroutine.
'''
_logger.debug('Reading body until close.')
file_is_async = hasattr(file, 'drain')
while True:
data = yield from self._connection.read(self._read_size)
if not data:
... | def _read_body_until_close(self, response, file) | Read the response until the connection closes.
Coroutine. | 4.015377 | 3.499984 | 1.147256 |
'''Read the connection specified by a length.
Coroutine.
'''
_logger.debug('Reading body by length.')
file_is_async = hasattr(file, 'drain')
try:
body_size = int(response.fields['Content-Length'])
if body_size < 0:
raise ValueEr... | def _read_body_by_length(self, response, file) | Read the connection specified by a length.
Coroutine. | 3.733983 | 3.394757 | 1.099927 |
'''Read the connection using chunked transfer encoding.
Coroutine.
'''
reader = ChunkedTransferReader(self._connection)
file_is_async = hasattr(file, 'drain')
while True:
chunk_size, data = yield from reader.read_chunk_header()
self._data_event... | def _read_body_by_chunk(self, response, file, raw=False) | Read the connection using chunked transfer encoding.
Coroutine. | 3.16885 | 2.909384 | 1.089182 |
'''Return the appropriate algorithm of reading response.
Returns:
str: ``chunked``, ``length``, ``close``.
'''
chunked_match = re.match(
r'chunked($|;)',
response.fields.get('Transfer-Encoding', '')
)
if chunked_match:
ret... | def get_read_strategy(cls, response) | Return the appropriate algorithm of reading response.
Returns:
str: ``chunked``, ``length``, ``close``. | 5.017975 | 2.589772 | 1.937613 |
'''Set up the content encoding decompressor.'''
encoding = response.fields.get('Content-Encoding', '').lower()
if encoding == 'gzip':
self._decompressor = wpull.decompression.GzipDecompressor()
elif encoding == 'deflate':
self._decompressor = wpull.decompression.... | def _setup_decompressor(self, response) | Set up the content encoding decompressor. | 3.461384 | 3.086381 | 1.121503 |
'''Decompress the given data and return the uncompressed data.'''
if self._decompressor:
try:
return self._decompressor.decompress(data)
except zlib.error as error:
raise ProtocolError(
'zlib error: {0}.'.format(error)
... | def _decompress_data(self, data) | Decompress the given data and return the uncompressed data. | 3.219438 | 2.955981 | 1.089127 |
'''Return any data left in the decompressor.'''
if self._decompressor:
try:
return self._decompressor.flush()
except zlib.error as error:
raise ProtocolError(
'zlib flush error: {0}.'.format(error)
) from error
... | def _flush_decompressor(self) | Return any data left in the decompressor. | 3.965215 | 3.205002 | 1.237196 |
'''Uncompress gzip data.
Args:
data (bytes): The gzip data.
truncated (bool): If True, the decompressor is not flushed.
This is a convenience function.
Returns:
bytes: The inflated data.
Raises:
zlib.error
'''
decompressor = SimpleGzipDecompressor()
in... | def gzip_uncompress(data, truncated=False) | Uncompress gzip data.
Args:
data (bytes): The gzip data.
truncated (bool): If True, the decompressor is not flushed.
This is a convenience function.
Returns:
bytes: The inflated data.
Raises:
zlib.error | 3.434039 | 2.19813 | 1.562255 |
'''Mark the item as processed without download.'''
_logger.debug(__(_('Skipping ‘{url}’.'), url=self.url_record.url))
self.app_session.factory['URLTable'].check_in(self.url_record.url, Status.skipped)
self._processed = True | def skip(self) | Mark the item as processed without download. | 20.841627 | 15.489302 | 1.34555 |
'''Mark the item with the given status.
Args:
status: a value from :class:`Status`.
increment_try_count: if True, increment the ``try_count``
value
'''
url = self.url_record.url
assert not self._try_count_incremented, (url, status)
... | def set_status(self, status: Status, increment_try_count: bool=True,
filename: str=None) | Mark the item with the given status.
Args:
status: a value from :class:`Status`.
increment_try_count: if True, increment the ``try_count``
value | 5.385348 | 4.369468 | 1.232495 |
'''Add links scraped from the document with automatic values.
Args:
url: A full URL. (It can't be a relative path.)
inline: Whether the URL is an embedded object.
link_type: Expected link type.
post_data: URL encoded form data. The request will be made us... | def add_child_url(self, url: str, inline: bool=False,
link_type: Optional[LinkType]=None,
post_data: Optional[str]=None,
level: Optional[int]=None,
replace: bool=False) | Add links scraped from the document with automatic values.
Args:
url: A full URL. (It can't be a relative path.)
inline: Whether the URL is an embedded object.
link_type: Expected link type.
post_data: URL encoded form data. The request will be made using
... | 4.806215 | 1.825543 | 2.63276 |
'''Return a child URLRecord.
This function is useful for testing filters before adding to table.
'''
url_record = URLRecord()
url_record.url = url
url_record.status = Status.todo
url_record.try_count = 0
url_record.level = self.url_record.level + 1 if lev... | def child_url_record(self, url: str, inline: bool=False,
link_type: Optional[LinkType]=None,
post_data: Optional[str]=None,
level: Optional[int]=None) | Return a child URLRecord.
This function is useful for testing filters before adding to table. | 2.628778 | 1.99103 | 1.32031 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.