libcloud.utils package¶
Submodules¶
libcloud.utils.compression module¶
libcloud.utils.connection module¶
-
libcloud.utils.connection.
get_response_object
(url, method='GET', headers=None)[source]¶ Utility function which uses libcloud’s connection class to issue an HTTP request.
Parameters: - url (
str
) – URL to send the request to. - method (
str
) – HTTP method. - headers (
dict
) – Optional request headers.
Returns: Response object.
Return type: Response
.- url (
libcloud.utils.decorators module¶
libcloud.utils.dist module¶
-
libcloud.utils.dist.
get_data_files
(dname, ignore=None, parent=None)[source]¶ Get all the data files that should be included in this distutils Project.
‘dname’ should be the path to the package that you’re distributing.
‘ignore’ is a list of sub-packages to ignore. This facilitates disparate package hierarchies. That’s a fancy way of saying that the ‘twisted’ package doesn’t want to include the ‘twisted.conch’ package, so it will pass [‘conch’] as the value.
‘parent’ is necessary if you’re distributing a subpackage like twisted.conch. ‘dname’ should point to ‘twisted/conch’ and ‘parent’ should point to ‘twisted’. This ensures that your data_files are generated correctly, only using relative paths for the first element of the tuple (‘twisted/conch/*‘). The default ‘parent’ is the current working directory.
libcloud.utils.files module¶
-
libcloud.utils.files.
read_in_chunks
(iterator, chunk_size=None, fill_size=False, yield_empty=False)[source]¶ Return a generator which yields data in chunks.
Parameters: - iterator (
object
which implements iterator interface.) – An object which implements an iterator interface or a File like object with read method. - chunk_size (
int
) – Optional chunk size (defaults to CHUNK_SIZE) - fill_size (
bool
) – If True, make sure chunks are exactly chunk_size in length (except for last chunk). - yield_empty (
bool
) – If true and iterator returned no data, yield empty bytes object before raising StopIteration.
TODO: At some point in the future we could use byte arrays here if version >= Python 3. This should speed things up a bit and reduce memory usage.
- iterator (
libcloud.utils.iso8601 module¶
Copyright (c) 2007 Michael Twomey
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ISO 8601 date time string parsing
Basic usage: >>> import iso8601 >>> iso8601.parse_date(“2007-01-25T12:00:00Z”) datetime.datetime(2007, 1, 25, 12, 0, tzinfo=<iso8601.iso8601.Utc ...>) >>>
-
libcloud.utils.iso8601.
parse_date
(datestring, default_timezone=<libcloud.utils.iso8601.Utc object>)[source]¶ Parses ISO 8601 dates into datetime objects
The timezone is parsed from the date string. However it is quite common to have dates without a timezone (not strictly correct). In this case the default timezone specified in default_timezone is used. This is UTC by default.
-
exception
libcloud.utils.iso8601.
ParseError
[source]¶ Bases:
exceptions.Exception
Raised when there is a problem parsing a date string
libcloud.utils.logging module¶
-
class
libcloud.utils.logging.
ExtraLogFormatter
(fmt=None, datefmt=None)[source]¶ Bases:
logging.Formatter
Custom log formatter which attaches all the attributes from the “extra” dictionary which start with an underscore to the end of the log message.
For example: extra={‘_id’: ‘user-1’, ‘_path’: ‘/foo/bar’}
Initialize the formatter with specified format strings.
Initialize the formatter either with the specified format string, or a default as described above. Allow for specialized date formatting with the optional datefmt argument (if omitted, you get the ISO8601 format).
libcloud.utils.misc module¶
-
libcloud.utils.misc.
get_driver
(drivers, provider)[source]¶ Get a driver.
Parameters: - drivers – Dictionary containing valid providers.
- provider (
libcloud.types.Provider
) – Id of provider to get driver
-
libcloud.utils.misc.
set_driver
(drivers, provider, module, klass)[source]¶ Sets a driver.
Parameters: - drivers – Dictionary to store providers.
- provider (
libcloud.types.Provider
) – Id of provider to set driver for - module (L) – The module which contains the driver
- klass – The driver class name
-
libcloud.utils.misc.
merge_valid_keys
(params, valid_keys, extra)[source]¶ Merge valid keys from extra into params dictionary and return dictionary with keys which have been merged.
Note: params is modified in place.
-
libcloud.utils.misc.
get_new_obj
(obj, klass, attributes)[source]¶ Pass attributes from the existing object ‘obj’ and attributes dictionary to a ‘klass’ constructor. Attributes from ‘attributes’ dictionary are only passed to the constructor if they are not None.
-
libcloud.utils.misc.
str2dicts
(data)[source]¶ Create a list of dictionaries from a whitespace and newline delimited text.
For example, this: cpu 1100 ram 640
cpu 2200 ram 1024
becomes: [{‘cpu’: ‘1100’, ‘ram’: ‘640’}, {‘cpu’: ‘2200’, ‘ram’: ‘1024’}]
-
libcloud.utils.misc.
dict2str
(data)[source]¶ Create a string with a whitespace and newline delimited text from a dictionary.
For example, this: {‘cpu’: ‘1100’, ‘ram’: ‘640’, ‘smp’: ‘auto’}
becomes: cpu 1100 ram 640 smp auto
cpu 2200 ram 1024
-
libcloud.utils.misc.
get_secure_random_string
(size)[source]¶ Return a string of
size
random bytes. Returned string is suitable for cryptographic use.Parameters: size ( int
) – Size of the generated string.Returns: Random string. Return type: str
-
libcloud.utils.misc.
retry
(retry_exceptions=(<class 'libcloud.common.exceptions.RateLimitReachedError'>, <class 'socket.error'>, <class 'socket.gaierror'>, <class 'httplib.NotConnected'>, <class 'httplib.ImproperConnectionState'>), retry_delay=None, timeout=None, backoff=None)[source]¶ Retry method that helps to handle common exception.
Parameters: - retry_exceptions – types of exceptions to retry on.
- retry_delay – retry delay between the attempts.
- timeout – maximum time to wait.
- backoff – multiplier added to delay between attempts.
Example: retry_request = retry(timeout=1, retry_delay=1, backoff=1) retry_request(self.connection.request)()
libcloud.utils.networking module¶
-
libcloud.utils.networking.
is_private_subnet
(ip)[source]¶ Utility function to check if an IP address is inside a private subnet.
Parameters: ip ( str
) – IP address to checkReturns: bool
if the specified IP address is private.
-
libcloud.utils.networking.
is_public_subnet
(ip)[source]¶ Utility function to check if an IP address is inside a public subnet.
Parameters: ip ( str
) – IP address to checkReturns: bool
if the specified IP address is public.
-
libcloud.utils.networking.
is_valid_ip_address
(address, family=2)[source]¶ Check if the provided address is valid IPv4 or IPv6 address.
Parameters: - address (
str
) – IPv4 or IPv6 ddress to check. - family (
int
) – Address family (socket.AF_INTET / socket.AF_INET6).
Returns: bool
True if the provided address is valid.- address (