code stringlengths 52 7.75k | docs stringlengths 1 5.85k |
|---|---|
def _ensure_module_folder_exists():
if not os.path.isdir(MODULES_FOLDER_PATH):
try:
os.mkdir(MODULES_FOLDER_PATH)
except OSError, e:
if "file already exists" in str(e):
raise RuntimeError("Could not create modules folder: file exists with the same name") | Checks to see if the module folder exists. If it does not, create it.
If there is an existing file with the same name, we raise a RuntimeError. |
def find_vm(self, name):
try:
domain = self.hyper.lookupByName(name)
VM = VirtualMachine(domain, self)
except libvirtError:
VM = None
return VM | Try and find a VM by name
:param name: Name of the VM
:type name: str |
def fast_clone(self, VM, clone_name, mem=None):
disks = VM.get_disks()
ints = VM.get_interfaces()
count = 0
new_disks = []
for disk in disks:
pool = disk.pool
new_disk_name = '{0}-disk{1}'.format(clone_name, count)
count += 1
... | Create a 'fast' clone of a VM. This means we make
a snapshot of the disk and copy some of the settings
and then create a new VM based on the snapshot and settings
The VM is transient so when it is shutdown it deletes itself
:param VM: The VM to base this clone on
:type VM: sham.m... |
def get_configuration_from_settings(self, setting_info):
settings = self.get_settings()
return dict([(name, settings.get(key))
for (name, key) in setting_info
if not settings.get(key, None) is None]) | Returns a dictionary with configuration names as keys and setting
values extracted from this configurator's settings as values.
:param setting_info: Sequence of 2-tuples containing the configuration
name as the first and the setting name as the second element. |
def add_repository(self, name, repository_type, repository_class,
aggregate_class, make_default, configuration):
repo_mgr = self.get_registered_utility(IRepositoryManager)
if name is None:
# If no name was given, this is assumed to be the ROOT repository
... | Generic method for adding a repository. |
def encrypt_email(email):
aes = SimpleAES(flask.current_app.config["AES_KEY"])
return aes.encrypt(email) | The default encryption function for storing emails in the database. This
uses AES and the encryption key defined in the applications configuration.
:param email:
The email address. |
def decrypt_email(enc_email):
aes = SimpleAES(flask.current_app.config["AES_KEY"])
return aes.decrypt(enc_email) | The inverse of :func:`encrypt_email`.
:param enc_email:
The encrypted email address. |
def shortlink_scanned(self, data):
# Inform log that we received an event
self.logger.info("Received shortlink_scanned event")
data = json.loads(data)
customer_token = str(data['object']['id'])
response = self.mapiclient.create_payment_request(
customer=cust... | Called when a shortlink_scanned event is received |
def pusher_connected(self, data):
# Inform user that pusher is done connecting
self.logger.info("Pusherclient connected")
# Bind the events we want to listen to
self.callback_client.bind("payment_authorized",
self.payment_authorized)
se... | Called when the pusherclient is connected |
def get(self, *args, **kwargs):
if self.query.where:
# If there is any other ``where`` filter on this QuerySet just call
# super. There will be a where clause if this QuerySet has already
# been filtered/cloned.
return super(CachingQuerySet, self).get(*ar... | Checks the cache to see if there's a cached entry for this pk. If not, fetches
using super then stores the result in cache.
Most of the logic here was gathered from a careful reading of
``django.db.models.sql.query.add_filter`` |
def find_route_by_view_name(self, view_name):
if not view_name:
return (None, None)
for uri, route in self.routes_all.items():
if route.name == view_name:
return uri, route
return (None, None) | Find a route in the router based on the specified view name.
:param view_name: string of view name to search by
:return: tuple containing (uri, Route) |
def _get(self, url, method, host):
url = host + url
# Check against known static routes
route = self.routes_static.get(url)
method_not_supported = self._invalid_usage(
'Method {} not allowed for URL {}'.format(
method, url), status_code=405)
i... | Get a request handler based on the URL of the request, or raises an
error. Internal method for caching.
:param url: request URL
:param method: request method
:return: handler, arguments, keyword arguments |
def is_stream_handler(self, request):
handler = self.get(request)[0]
if (hasattr(handler, 'view_class') and
hasattr(handler.view_class, request.method.lower())):
handler = getattr(handler.view_class, request.method.lower())
return hasattr(handler, 'is_stream'... | Handler for request is stream or not.
:param request: Request object
:return: bool |
def add_vertex(self, v):
self.graph.add_vertex(v)
self.vs.add(v) | Add a vertex to the graph
:param v: The vertex name. |
def add_outward_edge(self, v, e):
self.add_vertex(v)
self.graph.add_vertex(e)
self.es.add(e)
self.graph.add_edge(e, v) | Add an outward edge to a vertex
:param v: The source vertex.
:param e: The name of the outward edge. |
def fetch_path(self, name):
with codecs.open(self.lookup_path(name), encoding='utf-8') as fd:
return fd.read() | Fetch contents from the path retrieved via lookup_path.
No caching will be done. |
def execute(self, mold_id, data, wrapper_tag='div'):
template = self.load_mold(mold_id)
kwargs = {}
kwargs.update(data)
kwargs['_nunja_data_'] = 'data-nunja="%s"' % mold_id
kwargs['_template_'] = template
kwargs['_wrapper_tag_'] = wrapper_tag
return se... | Execute a mold `mold_id` by rendering through ``env``.
This is done using its default template, with data provided as
dict.
This returns the wrapped content, which contains the bits that
the client-side on-load script trigger will execute the index.js
defined for this mold; if ... |
def render(self, mold_id, data):
template = self.load_mold(mold_id)
return template.render(**data) | Render a mold `mold_id`. No wrappers are applied as only the
default template defined for the mold is rendered. |
def _get_model_table(self, part):
rows = self.parser.find(part).find_children('tr').list_results()
table = []
for row in rows:
table.append(self._get_model_row(self.parser.find(
row
).find_children('td,th').list_results()))
return self._g... | Returns a list that represents the table.
:param part: The table header, table footer or table body.
:type part: hatemile.util.html.htmldomelement.HTMLDOMElement
:return: The list that represents the table.
:rtype: list(list(hatemile.util.html.htmldomelement.HTMLDOMElement)) |
def _get_valid_model_table(self, ros):
# pylint: disable=no-self-use
new_table = []
if bool(ros):
length_table = len(ros)
for row_index in range(0, length_table):
cells_added = 0
original_row = [] + ros[row_index]
... | Returns a list that represents the table with the rowspans.
:param ros: The list that represents the table without the rowspans.
:type ros: list(list(hatemile.util.html.htmldomelement.HTMLDOMElement))
:return The list that represents the table with the rowspans.
:rtype: list(list(hatemi... |
def _get_model_row(self, row):
# pylint: disable=no-self-use
new_row = [] + row
size = len(row)
for i in range(0, size):
cell = row[i]
if cell.has_attribute('colspan'):
colspan = int(cell.get_attribute('colspan'))
if colsp... | Returns a list that represents the line of table with the colspans.
:param row: The list that represents the line of table without the
colspans.
:type row: list(hatemile.util.html.htmldomelement.HTMLDOMElement)
:return: The list that represents the line of table with the col... |
def _validate_header(self, hed):
# pylint: disable=no-self-use
if not bool(hed):
return False
length = -1
for row in hed:
if not bool(row):
return False
elif length == -1:
length = len(row)
elif len... | Validate the list that represents the table header.
:param hed: The list that represents the table header.
:type hed: list(list(hatemile.util.html.htmldomelement.HTMLDOMElement))
:return: True if the table header is valid or False if the table header
is not valid.
:rtyp... |
def _get_cells_headers_ids(self, hed, index):
# pylint: disable=no-self-use
ids = []
for row in hed:
if row[index].get_tag_name() == 'TH':
ids.append(row[index].get_attribute('id'))
return ids | Returns a list with ids of rows of same column.
:param hed: The list that represents the table header.
:type hed: list(list(hatemile.util.html.htmldomelement.HTMLDOMElement))
:param index: The index of columns.
:type index: int
:return: The list with ids of rows of same column.
... |
def _associate_data_cells_with_header_cells_of_row(self, element):
table = self._get_model_table(element)
for row in table:
headers_ids = []
for cell in row:
if cell.get_tag_name() == 'TH':
self.id_generator.generate_id(cell)
... | Associate the data cell with header cell of row.
:param element: The table body or table footer.
:type element: hatemile.util.html.htmldomelement.HTMLDOMElement |
def _prepare_header_cells(self, table_header):
cells = self.parser.find(table_header).find_children(
'tr'
).find_children('th').list_results()
for cell in cells:
self.id_generator.generate_id(cell)
cell.set_attribute('scope', 'col') | Set the scope of header cells of table header.
:param table_header: The table header.
:type table_header: hatemile.util.html.htmldomelement.HTMLDOMElement |
def white(self, kelvin):
whiteness = int(((kelvin - MIN_KELVIN) * 255)/(MAX_KELVIN-MIN_KELVIN))
whiteness = max(min(whiteness,255),0)
data = [CODE_COLOUR, self._id, 255 - whiteness, whiteness]
self._mode = MODE_WHITE
self._white = kelvin
self._effect = ''
self._send(data) | Supports between 2700K and 6500K white
:type kelvin int: |
def encrypt(clear_text) -> str:
if not isinstance(clear_text, bytes):
clear_text = str.encode(clear_text)
cipher = Fernet(current_app.config['KEY'])
return cipher.encrypt(clear_text).decode("utf-8") | Use config.json key to encrypt |
def decrypt(crypt_text) -> str:
cipher = Fernet(current_app.config['KEY'])
if not isinstance(crypt_text, bytes):
crypt_text = str.encode(crypt_text)
return cipher.decrypt(crypt_text).decode("utf-8") | Use config.json key to decrypt |
def get_volume(self, id):
# If the id is actually a path
if exists(id):
with open(id) as file:
size = os.lseek(file.fileno(), 0, os.SEEK_END)
return {'path': id, 'size': size}
return self.volume.get(id) | return volume information if the argument is an id or a path |
def randomize(self, device=None, percent=100, silent=False):
volume = self.get_volume(device)
# The number of blocks in the volume
blocks = int(volume['size'] / BLOCK_SIZE)
# How many writes should be to the device
# (based on the percentage requested)
num_writes... | Writes random data to the beginning of each 4MB block on a block device
this is useful when performance testing the backup process
(Without any optional arguments will randomize the first 32k of each
4MB block on 100 percent of the device) |
def read(self, device=None, offset=0, bs=None, count=1):
volume = self.get_volume(device)
block_size = bs or BLOCK_SIZE
offset = int(offset) * block_size
count = int(count)
print("Offset: ", offset)
total = 0
with directio.open(volume['path'], buffered=... | Using DIRECT_O read from the block device specified to stdout
(Without any optional arguments will read the first 4k from the device) |
def write(self, device=None, char=0, bs=None, count=None):
volume = self.get_volume(device)
block_size = bs or BLOCK_SIZE
# Calculate the number of blocks that are in the volume
count = count or (volume['size'] / block_size)
data = "".join([chr(int(char)) for i in rang... | Using DIRECT_O write a character in 4k chunks to a specified block
device (Without any optional arguments will write NULL's to the
entire device) |
def backup(self, id=None, src=None, timestamp=None):
# Set basic Logging
logging.basicConfig()
# Get the lunr logger
log = logger.get_logger()
# Output Debug level info
log.logger.setLevel(logging.DEBUG)
# Load the local storage configuration
conf... | This runs a backup job outside of the storage api,
which is useful for performance testing backups |
def clone(self, id=None, src=None, backup=None, size=None):
# Set basic Logging
logging.basicConfig()
# Get the lunr logger
log = logger.get_logger()
# Output Debug level info
log.logger.setLevel(logging.DEBUG)
# Load the local storage configuration
... | This runs a clone job outside of the storage api,
which is useful for performance testing backup restores
(Example: storage tools clone volume-clone
--backup volume-backup --src volume-original) |
def get_ip(request):
if getsetting('LOCAL_GEOLOCATION_IP'):
return getsetting('LOCAL_GEOLOCATION_IP')
forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if not forwarded_for:
return UNKNOWN_IP
for ip in forwarded_for.split(','):
ip = ip.strip()
if not ip.sta... | Return the IP address inside the HTTP_X_FORWARDED_FOR var inside
the `request` object.
The return of this function can be overrided by the
`LOCAL_GEOLOCATION_IP` variable in the `conf` module.
This function will skip local IPs (starting with 10. and equals to
127.0.0.1). |
def get_connection(self):
if self.conn:
return self.conn
redis_configs = getsetting('REDIS_CONNECTIONS')
if redis_configs:
config_name = getsetting('EVENTLIB_REDIS_CONFIG_NAME', 'default')
config = redis_configs[config_name]
host = confi... | Return a valid redis connection based on the following settings
* `REDIS_CONNECTIONS`
* `EVENTLIB_REDIS_CONFIG_NAME`
The first one is a dictionary in the following format:
>>> {
... 'server1': {'HOST': 'redis-server-1', 'POST': 9001},
... 'server2': {'HOS... |
def _run_setup_py(self, args, echo=True, echo2=True, ff=''):
python = self.python
if ff:
setup_py = '-c"%s"' % (RUN_SETUP % locals())
else:
setup_py = 'setup.py %s' % ' '.join(args)
rc, lines = self.process.popen(
'"%(python)s" %(setup_py)s'... | Run setup.py with monkey-patched setuptools.
The patch forces setuptools to use the file-finder 'ff'.
If 'ff' is the empty string, the patch is not applied.
'args' is the list of arguments that should be passed to
setup.py. |
def app_factory(global_settings, **local_settings): # pylint: disable=W0613
config = Configurator()
config.setup_registry(settings=local_settings,
root_factory=RootFactory())
if 'configure_zcml' in local_settings:
config.load_zcml(local_settings['configure_zcml'])
... | Default factory for creating a WSGI application using the everest
configurator and root factory.
:param dict global_settings: Global settings extracted from an ini file.
Not used in this default app factory.
:param dict local_settings: App settings extracted from an ini file. |
def factory(attr_type, data):
constructors = {
MFT_ATTR_STANDARD_INFORMATION: MftAttrStandardInformation,
MFT_ATTR_ATTRIBUTE_LIST: MftAttrAttributeList,
MFT_ATTR_FILENAME: MftAttrFilename,
MFT_ATTR_OBJECT_ID: MftAttrObjectId,
MFT_ATTR_SECURIT... | Returns Initialized attribute object based on attr_type \
(eg. :class:`MftAttrStandardInformation`)
Args:
attr_type (uint): Attribute type number (eg. 0x10 - \
$STANDARD_INFORMATION)
data (byte array): Data to initialize attribute object with. |
def bulk_get_or_create(self, data_list):
items_to_create = dict()
for record_key, record_config in data_list.items():
if record_key not in items_to_create:
record = self.get_instance(record_key)
if not record:
items_to_create[recor... | data_list is the data to get or create
We generate the query and set all the record keys based on passed in queryset
Then we loop over each item in the data_list, which has the keys already! No need to generate them. Should save a lot of time
Use values instead of the whole object, much faster
... |
async def fetch_page(session, host):
await asyncio.sleep(random.randint(0, 25) * 0.1)
start = time.time()
logger.info('Fetch from {}'.format(host))
try:
response = await session.get(host, allow_redirects=False)
except aiohttp.ClientResponseError as err:
# likely a 404 implying ... | Perform the page fetch from an individual host.
`session` - An aiohttp
[client session](http://aiohttp.readthedocs.io/en/stable/client_reference.html#client-session)
`host` - URL to fetch
`return` tuple with the following:
* The host parameter
* A vague status string
* Text ... |
async def asynchronous(urls=None, re_filter=None):
class _URLBase(str):
""" Convenient access to hostname (ip) portion of the URL """
@property
def hostname(self):
return urlsplit(self).hostname
http_devices = {}
qualified_devices = []
connection = aiohttp.TCPCo... | Asynchronous request manager for session. Returns list of responses that
match the filter.
`urls` - tuple of URLs to request
`re_filter` - a compiled regular expression
[object](https://docs.python.org/3/library/re.html#re-objects) |
def url_generator(network=None, path=''):
network_object = ipaddress.ip_network(network)
if network_object.num_addresses > 256:
# will need to batch process this case otherwise we run out of selectors
logger.error('Scan limited to 256 addresses, requested %d.', network_object.num_addresses)... | Return a tuple of URLs with path, one for each host on network
`network` - IP address and subnet mask compatible with
[ipaddress library](https://docs.python.org/3/library/ipaddress.html#ipaddress.ip_network)
`path` - Path portion of a URL as defined by
[url(un)split](https://docs.python.org/3/libr... |
def survey(network=None, path='', pattern='', log=False):
if log:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.CRITICAL)
network_scan = asyncio.ensure_future(asynchronous(
urls=url_generator(network=network, path=path),
re_filter=re.compile(pattern))
... | Search network for hosts with a response to path that matches pattern
`network` - IP address and subnet mask compatible with
[ipaddress library](https://docs.python.org/3/library/ipaddress.html#ipaddress.ip_network)
`path` - Path portion of a URL as defined by
[url(un)split](https://docs.python.org... |
def cli():
ch = logging.StreamHandler()
ch.setFormatter(logging.Formatter(
'%(asctime)s.%(msecs)03d %(levelname)s: %(message)s',
datefmt="%Y-%m-%d %H:%M:%S"
))
logger.addHandler(ch)
import argparse
parser = argparse.ArgumentParser(description="Search 'network' for hosts wit... | Command line interface |
def hidden_cursor():
if sys.stdout.isatty():
_LOGGER.debug('Hiding cursor.')
print('\x1B[?25l', end='')
sys.stdout.flush()
try:
yield
finally:
if sys.stdout.isatty():
_LOGGER.debug('Showing cursor.')
print('\n\x1B[?25h', end='')
... | Temporarily hide the terminal cursor. |
def display_status():
def print_status(msg, color):
"""Print the status message.
Args:
msg: The message to display (e.g. OK or FAILED).
color: The ANSI color code to use in displaying the message.
"""
print('\r' if sys.stdout.isatty() else '\t', end='')
... | Display an OK or FAILED message for the context block. |
def timed_display(msg):
def print_header(msg, newline=True):
"""Print a header line.
Args:
msg: A message to be printed in the center of the header line.
newline: Whether or not to print a newline at the end of the
header. This can be convenient for allo... | A timed block to run tasks with titles and success/failure messages.
Args:
msg: The header message to print at the beginning of the timed block. |
def run_tasks(header, tasks):
tasks = list(tasks)
with timed_display(header) as print_message:
with tqdm(tasks, position=1, desc='Progress', disable=None,
bar_format='{desc}{percentage:3.0f}% |{bar}|',
total=sum(t[2] if len(t) > 2 else 1 for t in tasks),
... | Run a group of tasks with a header, footer and success/failure messages.
Args:
header: A message to print in the header bar before the tasks are run.
tasks: A list of tuples containing a task title, a task, and a weight.
If the tuple only contains two values, the weight is assumed to be... |
def _pusher_connect_handler(self, data):
self.channel = self.pusher.subscribe(self.pos_callback_chan)
for listener in self.pusher_connected_listeners:
listener(data) | Event handler for the connection_established event. Binds the
shortlink_scanned event |
def _runForever(self, stop_event):
while(not stop_event.is_set()):
state = self.pusher.connection.state
if (state is not "connecting" and
state is not "connected"):
self.logger.warning(
"Pusher seems to be disconnected, tr... | Runs the main loop
Arguments:
stop_event: threading.Event() as a stop signal |
def stop(self):
self.pusherthread_stop.set()
self.pusher.disconnect()
# wait until pusher is down
while self.pusher.connection.state is "connected":
sleep(0.1)
logging.info("shutting down pusher connector thread") | Stops the pusherclient cleanly |
def load(self, filename, bs=512):
with open(filename, 'rb') as f:
f.seek(GPT_HEADER_OFFSET + 0x0C)
header_size = struct.unpack("<I", f.read(4))[0]
f.seek(GPT_HEADER_OFFSET)
header_data = f.read(header_size)
self.header = GPT_HEADER(header_dat... | Loads GPT partition table.
Args:
filename (str): path to file or device to open for reading
bs (uint): Block size of the volume, default: 512
Raises:
IOError: If file does not exist or not readable |
def __load_partition_entries(self, fd, bs):
fd.seek(self.header.part_lba * bs)
for p in range(0, self.header.num_partitions):
data = fd.read(self.header.part_size)
entry = GptPartitionEntry(data)
if entry.type_guid != uuid.UUID(
'{00000000-00... | Loads the list of :class:`GptPartition` partition entries
Args:
bs (uint): Block size of the volume |
def slice_local_to_global(self, index_slice, axis=0):
local_start = self.int_local_to_global_start(index_slice.start, axis)
local_stop = self.int_local_to_global_stop(index_slice.stop, axis)
return slice(local_start,local_stop,index_slice.step) | Calculate start and stop index for mapping sliced index
:param index_slice: sliced index?
:param axis: current axis to calculate
:return: slice object as calculated |
def local_to_global(self, index):
if (type(index) is int) or (type(index) is slice):
if len(self.__mask) > 1:
raise IndexError('check length of parameter index')
# 1D array
if type(index) is int:
return self.int_local_to_global(index)... | Calculate local index from global index
:param index: input index
:return: local index for data |
def slice_global_to_local(self, index_slice, axis=0):
if index_slice.stop < self.__mask[axis].start+self.__halos[0][axis]:
return None
if index_slice.start > self.__mask[axis].stop-self.__halos[1][axis]:
return None
local_start = self.int_global_to_local_start(... | Calculate start and stop index for mapping sliced index
:param index_slice: sliced index?
:param axis: current axis to calculate
:return: slice object as calculated |
def global_to_local(self, index):
if (type(index) is int) or (type(index) is slice):
if len(self.__mask) > 1:
raise IndexError('check length of parameter index')
# 1D array
if type(index) is int:
return self.int_global_to_local(index)
... | Calculate local index from global index
:param index: input index
:return: local index for data |
def int_global_to_local_start(self, index, axis=0):
if index >= self.__mask[axis].stop-self.__halos[1][axis]:
return None
if index < self.__mask[axis].start:
return 0
return index-self.__mask[axis].start | Calculate local index from global index from start_index
:param index: global index as integer
:param axis: current axis to process
:return: |
def int_global_to_local_stop(self, index, axis=0):
if index < self.__mask[axis].start+self.__halos[0][axis]:
return None
if index > self.__mask[axis].stop:
return self.__mask[axis].stop-self.__mask[axis].start
return index-self.__mask[axis].start | Calculate local index from global index from stop_index
:param index: global index as integer
:param axis: current axis to process
:return: |
def int_global_to_local(self, index, axis=0):
# Warum >= an dieser Stelle. Eigentlich sollte > ausreichend sein! Test!
if index >= self.__mask[axis].stop-self.__halos[1][axis]:
return None
if index < self.__mask[axis].start+self.__halos[0][axis]:
return None
... | Calculate local index from global index for integer input
:param index: global index as integer
:param axis: current axis to process
:return: |
def int_out_of_bounds(self, index, axis=0):
#if index >= self._global_shape[axis]:
if index > self._global_shape[axis]:
raise IndexError('index is larger than the upper bound')
# wrap around index if negative like in python
if index < 0:
index += self._g... | examples if index is out of local processing bounds
function is used to perform examples for index of type integer
:param index: global index to examples as type int
:param axis: current axis to examples
:return: return input or raise error |
def out_of_bounds(self, index):
if type(index) is int:
return self.int_out_of_bounds(index)
elif type(index) is slice:
return self.slice_out_of_bounds(index)
elif type(index) is tuple:
local_index = []
for k, item in enumerate(index):
... | Check index for out of bounds
:param index: index as integer, tuple or slice
:return: local index as tuple |
def reservoir(iterator, k):
sample = list(itertools.islice(iterator, 0, k))
for i, item in enumerate(iterator):
replace = random.randint(0, i + k)
if replace < k:
sample[replace] = item
return sample | Performs reservoir sampling of k items in iterator. Make sure that the iterator is a once-only iterator
(ie. not created using the "range" function).
:param iterator: set of items to sample from
:param k: sample k items
:return: list of sampled items |
def get_qpimage(self, idx):
if self._bgdata:
# The user has explicitly chosen different background data
# using `get_qpimage_raw`.
qpi = super(SeriesHdf5Qpimage, self).get_qpimage(idx)
else:
# We can use the background data stored in the qpimage h... | Return background-corrected QPImage of data at index `idx` |
def get_qpimage_raw(self, idx):
with self._qpseries() as qps:
qpi = qps.get_qpimage(index=idx).copy()
# Remove previously performed background correction
qpi.set_bg_data(None)
# Force meta data
for key in self.meta_data:
qpi[key] = self.meta_data[... | Return QPImage without background correction |
def verify(path):
valid = False
try:
h5 = h5py.File(path, mode="r")
qpi0 = h5["qpi_0"]
except (OSError, KeyError):
pass
else:
if ("qpimage version" in qpi0.attrs and
"phase" in qpi0 and
"amplitude" i... | Verify that `path` has the qpimage series file format |
def generate_requirements(output_path=None):
from django.conf import settings
reqs = set()
for app in settings.INSTALLED_APPS:
if app in mapping.keys():
reqs |= set(mapping[app])
if output_path is None:
print "--extra-index-url=http://opensource.washingtontimes.com/... | Loop through the INSTALLED_APPS and create a set of requirements for pip.
if output_path is ``None`` then write to standard out, otherwise write
to the path. |
def register_mbr_plugin(self, fs_id, plugin):
self.logger.debug('MBR: {}, FS ID: {}'
.format(self.__get_plugin_name(plugin), fs_id))
self.__mbr_plugins[fs_id].append(plugin) | Used in plugin's registration routine,
to associate it's detection method with given filesystem id
Args:
fs_id: filesystem id that is read from MBR partition entry
plugin: plugin that supports this filesystem |
def register_gpt_plugin(self, fs_guid, plugin):
key = uuid.UUID(fs_guid.lower())
self.logger.debug('GPT: {}, GUID: {}'
.format(self.__get_plugin_name(plugin), fs_guid))
self.__gpt_plugins[key].append(plugin) | Used in plugin's registration routine,
to associate it's detection method with given filesystem guid
Args:
fs_guid: filesystem guid that is read from GPT partition entry
plugin: plugin that supports this filesystem |
def detect_mbr(self, filename, offset, fs_id):
self.logger.debug('Detecting MBR partition type')
if fs_id not in self.__mbr_plugins:
return None
else:
plugins = self.__mbr_plugins.get(fs_id)
for plugin in plugins:
if plugin.detect(fil... | Used by rawdisk.session.Session to match mbr partitions against
filesystem plugins.
Args:
filename: device or file that it will read in order to detect
the filesystem fs_id: filesystem id to match (ex. 0x07)
offset: offset for the filesystem that is being matched
... |
def detect_gpt(self, filename, offset, fs_guid):
self.logger.debug('Detecting GPT partition type')
if fs_guid not in self.__gpt_plugins:
return None
else:
plugins = self.__gpt_plugins.get(fs_guid)
for plugin in plugins:
if plugin.dete... | Used by rawdisk.session.Session to match gpt partitions agains
filesystem plugins.
Args:
filename: device or file that it will read in order to detect the
filesystem
fs_id: filesystem guid to match
(ex. {EBD0A0A2-B9E5-4433-87C0-68B6B72699C7})
... |
def inject_documentation(**options):
import cog
loader = ConfigLoader(**options)
cog.out("\n" + loader.documentation + "\n\n") | Generate configuration documentation in reStructuredText_ syntax.
:param options: Any keyword arguments are passed on to the
:class:`ConfigLoader` initializer.
This methods injects the generated documentation into the output generated
by cog_.
.. _cog: https://pypi.python.org/pypi... |
def new_checksum(self):
if self.context.is_file(self.filename):
friendly_name = format_path(self.filename)
logger.debug("Calculating SHA1 of %s ..", friendly_name)
context = hashlib.sha1()
context.update(self.context.read_file(self.filename))
... | Get the SHA1 digest of the contents of :attr:`filename` (a string). |
def old_checksum(self):
if self.context.is_file(self.checksum_file):
logger.debug("Reading saved checksum from %s ..", format_path(self.checksum_file))
checksum = self.context.read_file(self.checksum_file).decode('ascii')
logger.debug("Saved checksum is %s.", checksu... | Get the checksum stored in :attr:`checksum_file` (a string or :data:`None`). |
def read_file(self, filename):
logger.info("Reading file: %s", format_path(filename))
contents = self.context.read_file(filename)
num_lines = len(contents.splitlines())
logger.debug("Read %s from %s.",
pluralize(num_lines, 'line'),
forma... | Read a text file and provide feedback to the user.
:param filename: The pathname of the file to read (a string).
:returns: The contents of the file (a string). |
def execute_file(self, filename):
logger.info("Executing file: %s", format_path(filename))
contents = self.context.execute(filename, capture=True).stdout
num_lines = len(contents.splitlines())
logger.debug("Execution of %s yielded % of output.",
format_path(... | Execute a file and provide feedback to the user.
:param filename: The pathname of the file to execute (a string).
:returns: Whatever the executed file returns on stdout (a string). |
def write_file(self, filename, contents):
logger.info("Writing file: %s", format_path(filename))
contents = contents.rstrip() + b"\n"
self.context.write_file(filename, contents)
logger.debug("Wrote %s to %s.",
pluralize(len(contents.splitlines()), "line"),
... | Write a text file and provide feedback to the user.
:param filename: The pathname of the file to write (a string).
:param contents: The new contents of the file (a string). |
def available_files(self):
matches = []
for pattern in self.filename_patterns:
logger.debug("Matching filename pattern: %s", pattern)
matches.extend(natsort(glob.glob(parse_path(pattern))))
return matches | The filenames of the available configuration files (a list of strings).
The value of :attr:`available_files` is computed the first time its
needed by searching for available configuration files that match
:attr:`filename_patterns` using :func:`~glob.glob()`. If you set
:attr:`available_... |
def documentation(self):
r
from humanfriendly.tables import format_rst_table
formatted_table = format_rst_table([
(directory,
self.get_main_pattern(directory).replace('*', r'\*'),
self.get_modular_pattern(directory).replace('*', r'\*'))
for direc... | r"""
Configuration documentation in reStructuredText_ syntax (a string).
The purpose of the :attr:`documentation` property is to provide
documentation on the integration of :class:`ConfigLoader` into other
projects without denormalizing the required knowledge via copy/paste.
..... |
def filename_patterns(self):
patterns = []
for directory in self.base_directories:
patterns.append(self.get_main_pattern(directory))
patterns.append(self.get_modular_pattern(directory))
return patterns | Filename patterns to search for available configuration files (a list of strings).
The value of :attr:`filename_patterns` is computed the first time it is
needed. Each of the :attr:`base_directories` generates two patterns:
1. A pattern generated by :func:`get_main_pattern()`.
2. A pat... |
def parser(self):
parser = configparser.RawConfigParser()
for filename in self.available_files:
friendly_name = format_path(filename)
logger.debug("Loading configuration file: %s", friendly_name)
loaded_files = parser.read(filename)
if len(loaded_... | A :class:`configparser.RawConfigParser` object with :attr:`available_files` loaded. |
def get_main_pattern(self, directory):
return os.path.join(directory, format(
'{prefix}{program_name}.{extension}',
extension=self.filename_extension.lstrip('.'),
program_name=self.program_name,
prefix=self.get_prefix(directory),
)) | Get the :func:`~glob.glob()` pattern to find the main configuration file.
:param directory: The pathname of a base directory (a string).
:returns: A filename pattern (a string).
This method generates a pattern that matches a filename based on
:attr:`program_name` with the suffix :attr:... |
def report_issue(self, message, *args, **kw):
if self.strict:
raise ValueError(format(message, *args, **kw))
else:
logger.warning(format(message, *args, **kw)) | Handle a problem by raising an exception or logging a warning (depending on :attr:`strict`). |
def validate_input(function):
@wraps(function)
def wrapper(*args, **kwargs):
try:
name = function.__name__ + '_validator' # find validator name
globals()[name](kwargs) # call validation function
return function(*args, **kwargs)
except KeyError:
... | Decorator that validates the kwargs of the function passed to it. |
def getImportFromObjects(node):
'''Returns a list of objects referenced by import from node'''
somenames = [x.asname for x in node.names if x.asname]
othernames = [x.name for x in node.names if not x.asname]
return somenames+othernamef getImportFromObjects(node):
'''Returns a list of objects referen... | Returns a list of objects referenced by import from node |
def mft_record_size(self):
if self.extended_bpb.clusters_per_mft < 0:
return 2 ** abs(self.extended_bpb.clusters_per_mft)
else:
return self.clusters_per_mft * self.sectors_per_cluster * \
self.bytes_per_sector | Returns:
int: MFT record size in bytes |
def mft_offset(self):
return self.bpb.bytes_per_sector * \
self.bpb.sectors_per_cluster * self.extended_bpb.mft_cluster | Returns:
int: MFT Table offset from the beginning of the partition in bytes |
def mft_mirror_offset(self):
return self.bpb.bytes_per_sector * \
self.bpb.sectors_per_cluster * self.extended_bpb.mft_mirror_cluster | Returns:
int: Mirror MFT Table offset from the beginning of the partition \
in bytes |
def clear_mappers():
# Remove our hybrid property constructs.
for mpr, is_primary in _mapper_registry.items():
if is_primary:
for attr_name in ('id', 'slug'):
try:
attr = object.__getattribute__(mpr.class_, attr_name)
if isinstance... | Clears all mappers set up by SA and also clears all custom "id" and
"slug" attributes inserted by the :func:`mapper` function in this module.
This should only ever be needed in a testing context. |
def as_slug_expression(attr):
slug_expr = sa_func.replace(attr, ' ', '-')
slug_expr = sa_func.replace(slug_expr, '_', '-')
slug_expr = sa_func.lower(slug_expr)
return slug_expr | Converts the given instrumented string attribute into an SQL expression
that can be used as a slug.
Slugs are identifiers for members in a collection that can be used in an
URL. We create slug columns by replacing non-URL characters with dashes
and lower casing the result. We need this at the ORM level... |
def synonym(name):
return hybrid_property(lambda inst: getattr(inst, name),
lambda inst, value: setattr(inst, name, value),
expr=lambda cls: getattr(cls, name)) | Utility function mimicking the behavior of the old SA synonym function
with the new hybrid property semantics. |
def map_system_entities(engine, metadata, reset):
# Map the user message system entity.
msg_tbl = Table('_user_messages', metadata,
Column('guid', String, nullable=False, primary_key=True),
Column('text', String, nullable=False),
Column('time_stam... | Maps all system entities. |
def inspect(orm_class, attribute_name):
key = (orm_class, attribute_name)
elems = OrmAttributeInspector.__cache.get(key)
if elems is None:
elems = OrmAttributeInspector.__inspect(key)
OrmAttributeInspector.__cache[key] = elems
return elems | :param attribute_name: name of the mapped attribute to inspect.
:returns: list of 2-tuples containing information about the inspected
attribute (first element: mapped entity attribute kind; second
attribute: mapped entity attribute) |
def trang(self, outfn=None, ext='.rng'):
from . import JARS
java = os.environ.get('java') or 'java'
trang_jar = os.path.join(JARS, 'trang.jar')
outfn = outfn or os.path.splitext(self.fn)[0] + ext
stderr = tempfile.NamedTemporaryFile()
try:
result = su... | use trang to convert the Schema to the given output filename or to the given extension
SIDE EFFECT: creates a new file on the filesystem. |
def schematron(self, fn=None, outfn=None, ext='.sch'):
from .xslt import XSLT
from . import PATH, XML, etree
fn = fn or self.fn
if os.path.splitext(fn)[-1].lower()==ext:
return fn
elif os.path.splitext(fn)[-1].lower()!='.rng':
fn = Schema(fn=fn).t... | convert the Schema to schematron and save at the given output filename or with the given extension. |
def from_tag(cls, tag, schemas, ext='.rnc'):
return cls(fn=cls.filename(tag, schemas, ext=ext)) | load a schema using an element's tag. schemas can be a string or a list of strings |
def filename(cls, tag, schemas, ext='.rnc'):
if type(schemas)==str:
schemas = re.split("\s*,\s*", schemas)
for schema in schemas:
fn = os.path.join(schema, cls.dirname(tag), cls.basename(tag, ext=ext))
if os.path.exists(fn):
return fn | given a tag and a list of schemas, return the filename of the schema.
If schemas is a string, treat it as a comma-separated list. |
def dirname(cls, namespace):
md = re.match("^\{?(?:[^:]+:/{0,2})?([^\}]+)\}?", namespace)
if md is not None:
dirname = re.sub("[/:]", '_', md.group(1))
else:
dirname = ''
return dirname | convert a namespace url to a directory name.
Also accepts an Element 'tag' with namespace prepended in {braces}. |
def errors_as_text(self):
errors = []
errors.append(self.non_field_errors().as_text())
errors_data = self.errors.as_data()
for key, value in errors_data.items():
field_label = self.fields[key].label
err_descn = ''.join([force_text(e.message) for e in valu... | only available to Django 1.7+ |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.