code stringlengths 52 7.75k | docs stringlengths 1 5.85k |
|---|---|
def extract_user(user_value):
user = resolve_value(user_value)
if not user and user != 0 and user != '0':
return None
if isinstance(user, tuple):
return user[0]
if isinstance(user, six.integer_types):
return six.text_type(user)
return user.partition(':')[0] | Extract the user for running a container from the following possible input formats:
* Integer (UID)
* User name string
* Tuple of ``user, group``
* String in the format ``user:group``
:param user_value: User name, uid, user-group tuple, or user:group string.
:type user_value: int | tuple | uni... |
def get_shared_volume_path(container_map, vol, instance=None):
if isinstance(vol, HostVolume):
c_path = resolve_value(vol.path)
if is_path(c_path):
return c_path, get_host_path(container_map.host.root, vol.host_path, instance)
raise ValueError("Host-container-binding must be... | Resolves a volume alias of a container configuration or a tuple of two paths to the host and container paths.
:param container_map: Container map.
:type container_map: dockermap.map.config.main.ContainerMap
:param vol: SharedVolume or HostVolume tuple.
:type vol: dockermap.map.input.HostVolume | docker... |
def get_instance_volumes(instance_detail, check_names):
if 'Mounts' in instance_detail:
if check_names:
return {m['Destination']: m.get('Name') or m['Source']
for m in instance_detail['Mounts']}
return {m['Destination']: m['Source']
for m in insta... | Extracts the mount points and mapped directories or names of a Docker container.
:param instance_detail: Result from a container inspection.
:type instance_detail: dict
:param check_names: Whether to check for named volumes.
:type check_names: bool
:return: Dictionary of volumes, with the destinati... |
def web_upload(self, package):
pkg = request.files[package]
self.index.from_data(package, pkg.read())
return 'ok' | * Smart enough to not save things we already have
* Idempotent, you can call as many times as you need
* The caller names the package (its basename) |
def merge_list(merged_list, items):
if not items:
return
merged_set = set(merged_list)
merged_add = merged_set.add
merged_list.extend(item
for item in items
if item not in merged_set and not merged_add(item)) | Merges items into a list, appends ignoring duplicates but retaining the original order. This modifies the list and
does not return anything.
:param merged_list: List to append de-duplicated items to.
:type merged_list: list
:param items: Items to merge into the list.
:type items: collections.Iterab... |
def disconnect_all_containers(self, action, network_name, containers, **kwargs):
client = action.client
for c_name in containers:
disconnect_kwargs = self.get_network_disconnect_kwargs(action, network_name, c_name, kwargs=kwargs)
client.disconnect_container_from_network(... | Disconnects all containers from a network.
:param action: Action configuration.
:type action: dockermap.map.runner.ActionConfig
:param network_name: Network name or id.
:type network_name: unicode | str
:param containers: Container names or ids.
:type containers: collect... |
def connect_networks(self, action, container_name, endpoints, skip_first=False, **kwargs):
if not endpoints or (skip_first and len(endpoints) <= 1):
return
client = action.client
map_name = action.config_id.map_name
nname = self._policy.nname
if skip_first:
... | Connects a container to a set of configured networks. By default this assumes the container has just been
created, so it will skip the first network that is already considered during creation.
:param action: Action configuration.
:type action: dockermap.map.runner.ActionConfig
:param co... |
def disconnect_networks(self, action, container_name, networks, **kwargs):
client = action.client
for n_name in networks:
disconnect_kwargs = self.get_network_disconnect_kwargs(action, n_name, container_name, kwargs=kwargs)
client.disconnect_container_from_network(**disc... | Disconnects a container from a set of networks.
:param action: Action configuration.
:type action: dockermap.map.runner.ActionConfig
:param container_name: Container names or id.
:type container_name: unicode | str
:param networks: List of network names or ids.
:type net... |
def connect_all_networks(self, action, container_name, **kwargs):
kwargs.setdefault('skip_first', True)
self.connect_networks(action, container_name, action.config.networks, **kwargs) | Connects a container to all of its configured networks. Assuming that this is typically used after container
creation, where teh first endpoint is already defined, this skips the first configuration. Pass ``skip_first``
as ``False`` to change this.
:param action: Action configuration.
:... |
def save(self, name):
self.finalize()
with open(name, 'wb+') as f:
if six.PY3:
f.write(self.fileobj.getbuffer())
else:
f.write(self.fileobj.getvalue().encode('utf-8')) | Save the string buffer to a file. Finalizes prior to saving.
:param name: File path.
:type name: unicode | str |
def save(self, name):
self.finalize()
with open(name, 'wb+') as f:
buf = self._fileobj.read()
while buf:
f.write(buf)
buf = self._fileobj.read() | Copy the contents of the temporary file somewhere else. Finalizes prior to saving.
:param name: File path.
:type name: unicode | str |
def is_path(value):
return value and isinstance(value, six.string_types) and (value[0] == posixpath.sep or value[:2] == CURRENT_DIR) | Checks whether the given value represents a path, i.e. a string which starts with an indicator for absolute or
relative paths.
:param value: Value to check.
:return: ``True``, if the value appears to be representing a path.
:rtype: bool |
def get_list(value):
if value is None:
return []
elif value is NotSet:
return NotSet
elif isinstance(value, (list, tuple)):
return list(value)
elif isinstance(value, six.string_types + (lazy_type, )) or uses_type_registry(value):
return [value]
raise ValueError("... | Wraps the given value in a list. ``None`` returns an empty list. Lists and tuples are returned as lists. Single
strings and registered types are wrapped in a list.
:param value: Value to return as a list.
:return: List with the provided value(s).
:rtype: list |
def get_network_mode(value):
if not value or value == 'disabled':
return 'none'
if isinstance(value, (tuple, list)):
if len(value) == 2:
return tuple(value)
return ValueError("Tuples or lists need to have length 2 for container network references.")
if value in DEFAU... | Generates input for the ``network_mode`` of a Docker host configuration. If it points at a container, the
configuration of the container is returned.
:param value: Network mode input.
:type value: unicode | str | tuple | list | NoneType
:return: Network mode or container to re-use the network stack of.... |
def get_healthcheck(value):
if isinstance(value, HealthCheck):
return value
elif isinstance(value, six.string_types + (lazy_type,)) or uses_type_registry(value):
return HealthCheck(value)
elif isinstance(value, (tuple, list)):
return HealthCheck(*value)
elif isinstance(value... | Converts input into a :class:`HealthCheck` tuple. Input can be passed as string, tuple, list, or a dictionary. If
set to ``None``, the health check will be set to ``NONE``, i.e. override an existing configuration from the image.
:param value: Health check input.
:type value: unicode | str | tuple | list | ... |
def get_type_item(self, value):
if isinstance(value, (HostVolume, SharedVolume)):
return value
elif isinstance(value, six.string_types):
return SharedVolume(value, False)
elif isinstance(value, (list, tuple)):
return _shared_host_volume_from_tuple(*va... | Converts the input to a ``SharedVolume`` or ``HostVolume`` tuple for a host bind. Input can be a single string, a
list or tuple, or a single-entry dictionary.
Single values are assumed to be volume aliases for read-write access. Tuples or lists with two elements, can be
``(alias, read-only indic... |
def get_type_item(self, value):
if isinstance(value, (UsedVolume, SharedVolume)):
if value.readonly:
raise ValueError("Attached volumes should not be read-only.")
return value
elif isinstance(value, six.string_types):
return SharedVolume(value... | Converts the given value to a ``UsedVolume`` or ``SharedVolume`` tuple for attached volumes. It
accepts strings, lists, tuples, and dicts as input.
For strings and collections of a single element, the first item is considered to be an alias for lookup on the map.
It is converted to a ``SharedVo... |
def get_type_item(self, value):
if isinstance(value, ExecCommand):
return value
elif isinstance(value, six.string_types + (lazy_type,)):
return ExecCommand(value)
elif isinstance(value, (list, tuple)):
v_len = len(value)
if 1 <= v_len <= 3... | Converts the input to a ExecCommand tuple. It can be from a single string, list, or tuple. Single values (also
single-element lists or tuples) are considered a command string, whereas two-element items are read as
``(command string, user name)``.
:param value: Input value for conversion.
... |
def expand_instances(config_ids, ext_maps):
for type_map_config, items in itertools.groupby(sorted(config_ids, key=get_map_config), get_map_config):
config_type, map_name, config_name = type_map_config
instances = _get_nested_instances(items)
c_map = ext_maps[map_name]
try:
... | Iterates over a list of input configuration ids, expanding configured instances if ``None`` is specified. Otherwise
where instance names are specified as a tuple, they are expanded.
:param config_ids: Iterable of container configuration ids or (map, config, instance names) tuples.
:type config_ids: collect... |
def get_map_config_ids(value, maps, default_map_name=None, default_instances=None):
input_ids = InputConfigIdList(value, map_name=default_map_name, instances=default_instances)
return list(expand_instances(expand_groups(input_ids, maps), maps)) | From a value, which can be a string, a iterable of strings, or MapConfigId tuple(s), generates a list of MapConfigId
tuples with expanded groups, listing all input or configured instances, and sorted by map and configuration.
:param value: Input value(s).
:type value: str | unicode | dockermap.map.input.In... |
def create_network(self, action, n_name, **kwargs):
c_kwargs = self.get_network_create_kwargs(action, n_name, **kwargs)
res = action.client.create_network(**c_kwargs)
self._policy.network_names[action.client_name][n_name] = res['Id']
return res | Creates a configured network.
:param action: Action configuration.
:type action: dockermap.map.runner.ActionConfig
:param n_name: Network name.
:type n_name: unicode | str
:param kwargs: Additional keyword arguments to complement or override the configuration-based values.
... |
def remove_network(self, action, n_name, **kwargs):
c_kwargs = self.get_network_remove_kwargs(action, n_name, **kwargs)
res = action.client.remove_network(**c_kwargs)
del self._policy.network_names[action.client_name][n_name]
return res | Removes a network.
:param action: Action configuration.
:type action: dockermap.map.runner.ActionConfig
:param n_name: Network name or id.
:type n_name: unicode | str
:param kwargs: Additional keyword arguments.
:type kwargs: dict |
def get_container_host_config_kwargs(self, action, container_name, kwargs=None):
container_map = action.container_map
container_config = action.config
client_config = action.client_config
config_id = action.config_id
map_name = config_id.map_name
policy = self._p... | Generates keyword arguments for the Docker client to set up the HostConfig or start a container.
:param action: Action configuration.
:type action: ActionConfig
:param container_name: Container name or id. Set ``None`` when included in kwargs for ``create_container``.
:type container_na... |
def get_attached_container_create_kwargs(self, action, container_name, kwargs=None):
client_config = action.client_config
policy = self._policy
config_id = action.config_id
path = resolve_value(policy.default_volume_paths[config_id.map_name][config_id.instance_name])
use... | Generates keyword arguments for the Docker client to create an attached container.
:param action: Action configuration.
:type action: ActionConfig
:param container_name: Container name.
:type container_name: unicode | str
:param kwargs: Additional keyword arguments to complement... |
def get_attached_container_host_config_kwargs(self, action, container_name, kwargs=None):
if container_name:
c_kwargs = {'container': container_name}
else:
c_kwargs = {}
update_kwargs(c_kwargs, kwargs)
return c_kwargs | Generates keyword arguments for the Docker client to set up the HostConfig or start an attached container.
:param action: Action configuration.
:type action: ActionConfig
:param container_name: Container name or id. Set ``None`` when included in kwargs for ``create_container``.
:type co... |
def get_container_update_kwargs(self, action, container_name, update_values, kwargs=None):
c_kwargs = dict(container=container_name)
update_kwargs(c_kwargs, update_values, kwargs)
return c_kwargs | Generates keyword arguments for the Docker client to update the HostConfig of an existing container.
:param action: Action configuration.
:type action: ActionConfig
:param container_name: Container name or id.
:type container_name: unicode | str
:param update_values: Dictionary ... |
def get_container_wait_kwargs(self, action, container_name, kwargs=None):
c_kwargs = dict(container=container_name)
timeout = action.client_config.get('wait_timeout')
if timeout is not None:
c_kwargs['timeout'] = timeout
update_kwargs(c_kwargs, kwargs)
return... | Generates keyword arguments for the Docker client to wait for a container.
:param action: Action configuration.
:type action: ActionConfig
:param container_name: Container name or id.
:type container_name: unicode | str
:param kwargs: Additional keyword arguments to complement o... |
def get_container_stop_kwargs(self, action, container_name, kwargs=None):
c_kwargs = dict(
container=container_name,
)
stop_timeout = action.config.stop_timeout
if stop_timeout is NotSet:
timeout = action.client_config.get('stop_timeout')
if t... | Generates keyword arguments for the Docker client to stop a container.
:param action: Action configuration.
:type action: ActionConfig
:param container_name: Container name or id.
:type container_name: unicode | str
:param kwargs: Additional keyword arguments to complement or ov... |
def get_container_remove_kwargs(self, action, container_name, kwargs=None):
c_kwargs = dict(container=container_name)
update_kwargs(c_kwargs, kwargs)
return c_kwargs | Generates keyword arguments for the Docker client to remove a container.
:param action: Action configuration.
:type action: ActionConfig
:param container_name: Container name or id.
:type container_name: unicode | str
:param kwargs: Additional keyword arguments to complement or ... |
def get_network_create_kwargs(self, action, network_name, kwargs=None):
config = action.config
c_kwargs = dict(
name=network_name,
driver=config.driver,
options=config.driver_options,
)
if config.internal:
c_kwargs['internal'] = Tr... | Generates keyword arguments for the Docker client to create a network.
:param action: Action configuration.
:type action: ActionConfig
:param network_name: Network name or id.
:type network_name: unicode | str
:param kwargs: Additional keyword arguments to complement or override... |
def get_network_remove_kwargs(self, action, network_name, kwargs=None):
c_kwargs = dict(net_id=network_name)
update_kwargs(c_kwargs, kwargs)
return c_kwargs | Generates keyword arguments for the Docker client to remove a network.
:param action: Action configuration.
:type action: ActionConfig
:param network_name: Network name or id.
:type network_name: unicode | str
:param kwargs: Additional keyword arguments to complement or override... |
def get_network_create_endpoint_kwargs(self, action, endpoint_config, kwargs=None):
map_name = action.config_id.map_name
policy = self._policy
c_kwargs = dict(
ipv4_address=resolve_value(endpoint_config.ipv4_address),
ipv6_address=resolve_value(endpoint_config.ip... | Generates keyword arguments for Docker's ``create_endpoint_config`` utility / ``EndpointConfig`` type as well
as for ``connect_container_to_network``.
:param action: Action configuration.
:type action: ActionConfig
:param endpoint_config: Network endpoint configuration.
:type en... |
def get_network_connect_kwargs(self, action, network_name, container_name, endpoint_config=None, kwargs=None):
c_kwargs = dict(
container=container_name,
net_id=network_name,
)
if endpoint_config:
c_kwargs.update(self.get_network_create_endpoint_kwarg... | Generates keyword arguments for the Docker client to add a container to a network.
:param action: Action configuration.
:type action: ActionConfig
:param network_name: Network name or id.
:type network_name: unicode | str
:param container_name: Container name or id.
:typ... |
def get_network_disconnect_kwargs(self, action, network_name, container_name, kwargs=None):
c_kwargs = dict(
container=container_name,
net_id=network_name,
)
update_kwargs(c_kwargs, kwargs)
return c_kwargs | Generates keyword arguments for the Docker client to remove a container from a network.
:param action: Action configuration.
:type action: ActionConfig
:param container_name: Container name or id.
:type container_name: unicode | str
:param network_name: Network name or id.
... |
def get_exec_create_kwargs(self, action, container_name, exec_cmd, exec_user, kwargs=None):
c_kwargs = dict(
container=container_name,
cmd=resolve_value(exec_cmd),
)
if exec_user is not None:
c_kwargs['user'] = text_type(resolve_value(exec_user))
... | Generates keyword arguments for the Docker client to set up the HostConfig or start a container.
:param action: Action configuration.
:type action: ActionConfig
:param container_name: Container name or id.
:type container_name: unicode | str
:param kwargs: Additional keyword arg... |
def get_exec_start_kwargs(self, action, container_name, exec_id, kwargs=None):
c_kwargs = dict(exec_id=exec_id)
update_kwargs(c_kwargs, kwargs)
return c_kwargs | Generates keyword arguments for the Docker client to set up the HostConfig or start a container.
:param action: Action configuration.
:type action: ActionConfig
:param container_name: Container name or id.
:type container_name: unicode | str
:param kwargs: Additional keyword arg... |
def get_volume_create_kwargs(self, action, volume_name, kwargs=None):
config = action.config
c_kwargs = dict(name=volume_name)
if config:
c_kwargs['driver'] = config.driver
driver_opts = init_options(config.driver_options)
if driver_opts:
... | Generates keyword arguments for the Docker client to create a volume.
:param action: Action configuration.
:type action: ActionConfig
:param volume_name: Volume name.
:type volume_name: unicode | str
:param kwargs: Additional keyword arguments to complement or override the confi... |
def get_volume_remove_kwargs(self, action, volume_name, kwargs=None):
c_kwargs = dict(name=volume_name)
update_kwargs(c_kwargs, kwargs)
return c_kwargs | Generates keyword arguments for the Docker client to remove a volume.
:param action: Action configuration.
:type action: ActionConfig
:param volume_name: Volume name.
:type volume_name: unicode | str
:param kwargs: Additional keyword arguments to complement or override the confi... |
def cname(cls, map_name, container, instance=None):
if instance:
return '{0}.{1}.{2}'.format(map_name, container, instance)
return '{0}.{1}'.format(map_name, container) | Generates a container name that should be used for creating new containers and checking the status of existing
containers.
In this implementation, the format will be ``<map name>.<container name>.<instance>``. If no instance is
provided, it is just ``<map name>.<container name>``.
:par... |
def aname(cls, map_name, attached_name, parent_name=None):
if parent_name:
return '{0}.{1}.{2}'.format(map_name, parent_name, attached_name)
return '{0}.{1}'.format(map_name, attached_name) | Generates a container name that should be used for creating new attached volume containers and checking the
status of existing containers.
In this implementation, the format will be ``<map name>.<attached>``, or ``<map name>.<parent name>.<attached>``
if the parent container configuration name ... |
def nname(cls, map_name, network_name):
if network_name in DEFAULT_PRESET_NETWORKS:
return network_name
return '{0}.{1}'.format(map_name, network_name) | Generates a network name that should be used for creating new networks and checking the status of existing
networks on the client.
In this implementation, the format will be ``<map name>.<network name>``.
:param map_name: Container map name.
:type map_name: unicode | str
:param... |
def get_hostname(cls, container_name, client_name=None):
base_name = container_name
for old, new in cls.hostname_replace:
base_name = base_name.replace(old, new)
if not client_name or client_name == cls.default_client_name:
return base_name
client_suffix ... | Determines the host name of a container. In this implementation, replaces all dots and underscores of a
container name with a dash; then attaches another dash with the client name, unless there is just one default
client.
:param container_name: Name of the container.
:type container_nam... |
def adduser(username, uid=None, system=False, no_login=True, no_password=False, group=False, gecos=None, **kwargs):
return _format_cmd('adduser', username, __system=bool(system), __uid=uid, __group=bool(group), __gid=uid,
no_login=(no_login, _NO_CREATE_HOME, _NO_LOGIN),
... | Formats an ``adduser`` command.
:param username: User name.
:type username: unicode | str
:param uid: Optional user id to use.
:type uid: long | int
:param system: Create a system user account.
:type system: bool
:param no_login: Disable the login for this user. Not compatible with CentOS. ... |
def get_user_group(user_group):
if isinstance(user_group, tuple):
return '{0}:{1}'.format(*user_group)
elif isinstance(user_group, six.integer_types) or ':' not in user_group:
return '{0}:{0}'.format(user_group)
return user_group | Formats a user and group in the format ``user:group``, as needed for `chown`. If user_group is a tuple, this is used
for the fomatting. If a string or integer is given, it will be formatted as ``user:user``. Otherwise the input is
returned - this method does not perform any more checks.
:param user_group: ... |
def addgroupuser(username, uid, groupnames=None, system=False, no_login=True, no_password=False, gecos=None, sudo=False,
**kwargs):
group = addgroup(username, uid, system)
user = adduser(username, uid, system, no_login, no_password, False, gecos, **kwargs)
prefix = 'sudo ' if sudo else... | Generates a unix command line for creating user and group with the same name, assigning the user to the group.
Has the same effect as combining :func:`~addgroup`, :func:`~adduser`, and :func:`~assignuser`.
:param username: User name to create.
:type username: unicode | str
:param uid: User id to use.
... |
def mkdir(path, create_parent=True, check_if_exists=False):
cmd = _format_cmd('mkdir', path, _p=create_parent)
if check_if_exists:
return 'if [[ ! -d {0} ]]; then {1}; fi'.format(path, cmd)
return cmd | Generates a unix command line for creating a directory.
:param path: Directory path.
:type path: unicode | str
:param create_parent: Create parent directories, if necessary. Default is ``True``.
:type create_parent: bool
:param check_if_exists: Prepend a check if the directory exists; in that case,... |
def mkdir_chown(paths, user_group=None, permissions='ug=rwX,o=rX', create_parent=True, check_if_exists=False, recursive=False):
def _generate_str(path):
mkdir_str = mkdir(path, create_parent, check_if_exists)
chown_str = chown(user_group, path, recursive) if user_group else None
chmod_... | Generates a unix command line for creating a directory and assigning permissions to it. Shortcut to a combination of
:func:`~mkdir`, :func:`~chown`, and :func:`~chmod`.
Note that if `check_if_exists` has been set to ``True``, and the directory is found, `mkdir` is not called, but
`user_group` and `permissi... |
def bind(self, field_name, parent):
super(TranslatedFieldsField, self).bind(field_name, parent)
# Expect 1-on-1 for now. Allow using source as alias,
# but it should not be a dotted path for now
related_name = self.source or field_name
# This could all be done in __ini... | Create translation serializer dynamically.
Takes translatable model class (shared_model) from parent serializer and it
may create a serializer class on the fly if no custom class was specified. |
def to_representation(self, value):
if value is None:
return
# Only need one serializer to create the native objects
serializer = self.serializer_class(
instance=self.parent.instance, # Typically None
context=self.context,
partial=self.p... | Serialize translated fields.
Simply iterate over available translations and, for each language,
delegate serialization logic to the translation model serializer.
Output languages can be selected by passing a list of language codes,
`languages`, within the serialization context. |
def to_internal_value(self, data):
if data is None:
return
if not isinstance(data, dict):
self.fail('invalid')
if not self.allow_empty and len(data) == 0:
self.fail('empty')
result, errors = {}, {}
for lang_code, model_fields in data... | Deserialize data from translations fields.
For each received language, delegate validation logic to
the translation model serializer. |
def parse(self, text, layers=None):
params = {
"text": text,
"key": self.key,
}
if layers is not None:
# if it's string
if isinstance(layers, six.string_types):
params["layers"] = layers
# if it's another iter... | Parsing passed text to json.
Args:
text: Text to parse.
layers (optional): Special fields. Only one string
or iterable object (e.g "Data", ("Data", "Fio")).
Only these fields will be returned.
Returns:
The parsed text into a json obj... |
def generate(self, text):
if not text:
raise Exception("No text to speak")
if len(text) >= self.MAX_CHARS:
raise Exception("Number of characters must be less than 2000")
params = self.__params.copy()
params["text"] = text
self._data = requests.g... | Try to get the generated file.
Args:
text: The text that you want to generate. |
def save(self, path="speech"):
if self._data is None:
raise Exception("There's nothing to save")
extension = "." + self.__params["format"]
if os.path.splitext(path)[1] != extension:
path += extension
with open(path, "wb") as f:
for d in self... | Save data in file.
Args:
path (optional): A path to save file. Defaults to "speech".
File extension is optional. Absolute path is allowed.
Returns:
The path to the saved file. |
def create_translated_fields_serializer(shared_model, meta=None, related_name=None, **fields):
if not related_name:
translated_model = shared_model._parler_meta.root_model
else:
translated_model = shared_model._parler_meta[related_name].model
# Define inner Meta class
if not meta:
... | Create a Rest Framework serializer class for a translated fields model.
:param shared_model: The shared model.
:type shared_model: :class:`parler.models.TranslatableModel` |
def save(self, **kwargs):
translated_data = self._pop_translated_data()
instance = super(TranslatableModelSerializer, self).save(**kwargs)
self.save_translations(instance, translated_data)
return instance | Extract the translations and save them after main object save.
By default all translations will be saved no matter if creating
or updating an object. Users with more complex needs might define
their own save and handle translation saving themselves. |
def _pop_translated_data(self):
translated_data = {}
for meta in self.Meta.model._parler_meta:
translations = self.validated_data.pop(meta.rel_name, {})
if translations:
translated_data[meta.rel_name] = translations
return translated_data | Separate data of translated fields from other data. |
def save_translations(self, instance, translated_data):
for meta in self.Meta.model._parler_meta:
translations = translated_data.get(meta.rel_name, {})
for lang_code, model_fields in translations.items():
translation = instance._get_translated_model(lang_code, au... | Save translation data into translation objects. |
def load_conf(cfg_path):
global config
try:
cfg = open(cfg_path, 'r')
except Exception as ex:
if verbose:
print("Unable to open {0}".format(cfg_path))
print(str(ex))
return False
# Read the entire contents of the conf file
cfg_json = cfg.read()
... | Try to load the given conf file. |
def add_values(name, values):
int_values = map(int, values)
cv_dict[values_key][name] = int_values | Adds an alias with list of values to the channel/value dictionary. |
def are_valid_values(values):
try:
int_values = map(int, values)
for v in int_values:
if (v >= 0) and (v <= 255):
continue
else:
return False
except Exception as ex:
# print(ex)
return False
return True | Determines if a list of values are valid DMX values (0-255). |
def load_rc_file():
# If an rc file is named in the config, use it.
# Otherwise, fall back to looking in the HOME directory.
# The fall back won't work under RPi because HOME will be root.
if "uDMXrc" in config:
rcfile = config["uDMXrc"]
else:
if os.name == "nt":
# W... | Load the contents of the resource file ~/.uDMXrc |
def translate_message_tokens(message_tokens):
trans_tokens = []
if message_tokens[0] in cv_dict[channels_key]:
trans_tokens.append(cv_dict[channels_key][message_tokens[0]])
else:
trans_tokens.append(int(message_tokens[0]))
for token in message_tokens[1:]:
if token in cv_dic... | Translates alias references to their defined values.
The first token is a channel alias.
The remaining tokens are value aliases. |
def send_dmx_message(message_tokens):
# Open the uDMX USB device
dev = pyudmx.uDMXDevice()
if not dev.open():
print("Unable to find and open uDMX interface")
return False
# Translate the tokens into integers.
# trans_tokens[0] will be the one-based channel number (1-512) as an... | Send the DMX message defined by the command line arguments (message tokens).
The first argument/token is the DMX channel.
The remaining argument(s).token(s) are DMX values. |
def parse_headers(cls, msg):
return list(email.parser.Parser().parsestr(msg).items()) | Parse HTTP headers.
Args:
msg (str): HTTP message.
Returns:
(List[Tuple[str, str]): List of header tuples. |
def parse(cls, msg):
lines = msg.splitlines()
version, status_code, reason = lines[0].split()
headers = cls.parse_headers('\r\n'.join(lines[1:]))
return cls(version=version, status_code=status_code,
reason=reason, headers=headers) | Parse message string to response object. |
def parse(cls, msg):
lines = msg.splitlines()
method, uri, version = lines[0].split()
headers = cls.parse_headers('\r\n'.join(lines[1:]))
return cls(version=version, uri=uri, method=method, headers=headers) | Parse message string to request object. |
def sendto(self, transport, addr):
msg = bytes(self) + b'\r\n'
logger.debug("%s:%s < %s", *(addr + (self,)))
transport.sendto(msg, addr) | Send request to a given address via given transport.
Args:
transport (asyncio.DatagramTransport):
Write transport to send the message on.
addr (Tuple[str, int]):
IP address and port pair to send the message to. |
def open(self, vendor_id: int = 0x16c0, product_id: int = 0x5dc, bus: int = None, address: int = None) -> bool:
kwargs = {}
if vendor_id:
kwargs["idVendor"] = vendor_id
if product_id:
kwargs["idProduct"] = product_id
if bus:
kwargs["bus"] = bu... | Open the first device that matches the search criteria. Th default parameters
are set up for the likely most common case of a single uDMX interface.
However, for the case of multiple uDMX interfaces, you can use the
bus and address paramters to further specifiy the uDMX interface
to be o... |
def close(self):
# This may not be absolutely necessary, but it is safe.
# It's the closest thing to a close() method.
if self._dev is not None:
usb.util.dispose_resources(self._dev)
self._dev = None | Close and release the current usb device.
:return: None |
def send_single_value(self, channel: int, value: int) -> int:
SetSingleChannel = 1
n = self._send_control_message(SetSingleChannel, value_or_length=value, channel=channel, data_or_length=1)
return n | Send a single value to the uDMX
:param channel: DMX channel number, 1-512
:param value: Value to be sent to channel, 0-255
:return: number of bytes actually sent |
def send_multi_value(self, channel: int, values: Union[List[int], bytearray]) -> int:
SetMultiChannel = 2
if isinstance(values, bytearray):
ba = values
else:
ba = bytearray(values)
n = self._send_control_message(SetMultiChannel, value_or_length=len(ba),
... | Send multiple consecutive bytes to the uDMX
:param channel: The starting DMX channel number, 1-512
:param values: any sequence of integer values that can be converted
to a bytearray (e.g a list). Each value 0-255.
:return: number of bytes actually sent |
def add_boolean_argument(parser, name, default=False):
group = parser.add_mutually_exclusive_group()
group.add_argument('--set', action='store_true', dest=name,
default=default)
group.add_argument('--unset', action='store_false', dest=name,
default=default... | Add a boolean argument to an ArgumentParser instance. |
def send_rgb(dev, red, green, blue, dimmer):
cv = [0 for v in range(0, 512)]
cv[0] = red
cv[1] = green
cv[2] = blue
cv[6] = dimmer
sent = dev.send_multi_value(1, cv)
return sent | Send a set of RGB values to the light |
def main():
# Channel value list for channels 1-512
cv = [0 for v in range(0, 512)]
# Create an instance of the DMX controller and open it
print("Opening DMX controller...")
dev = pyudmx.uDMXDevice()
# This will automagically find a single Anyma-type USB DMX controller
dev.open()
... | How to control a DMX light through an Anyma USB controller |
def connect(self):
try:
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
if self.config['no_ssl_verify']:
requests.packages.urllib3.disable_warnings()
context.verify_mode = ssl.CERT_NONE
self.si = SmartConnectNoSSL(
... | Connect to vCenter server |
def list_objects(self):
vimtype = self.config['type']
vim_obj = "vim.%s" % vimtype
try:
container = self.content.viewManager.CreateContainerView(
self.content.rootFolder, [eval(vim_obj)], True)
except AttributeError:
print("%s is not a Ma... | Command Section: list
List available VMware objects |
def status(self):
vm = self.get_vm_failfast(self.config['name'])
extra = self.config['extra']
parserFriendly = self.config['parserFriendly']
status_to_print = []
if extra:
status_to_print = \
[["vmname", "powerstate", "ipaddress", "hostname",... | Check power status |
def shutdown(self):
vm = self.get_vm_failfast(self.config['name'])
if vm.runtime.powerState == vim.VirtualMachinePowerState.poweredOff:
print("%s already poweredOff" % vm.name)
else:
if self.guestToolsRunning(vm):
timeout_minutes = 10
... | Shutdown guest
fallback to power off if guest tools aren't installed |
def get_resource_pool(self, cluster, pool_name):
pool_obj = None
# get a list of all resource pools in this cluster
cluster_pools_list = cluster.resourcePool.resourcePool
# get list of all resource pools with a given text name
pool_selections = self.get_obj(
... | Find a resource pool given a pool name for desired cluster |
def get_obj(self, vimtype, name, return_all=False, path=""):
obj = list()
if path:
obj_folder = self.content.searchIndex.FindByInventoryPath(path)
container = self.content.viewManager.CreateContainerView(
obj_folder, vimtype, True
)
el... | Get the vsphere object associated with a given text name or MOID |
def get_host_system_failfast(
self,
name,
verbose=False,
host_system_term='HS'
):
if verbose:
print("Finding HostSystem named %s..." % name)
hs = self.get_host_system(name)
if hs is None:
print("Error: %s '%s'... | Get a HostSystem object
fail fast if the object isn't a valid reference |
def get_vm(self, name, path=""):
if path:
return self.get_obj([vim.VirtualMachine], name, path=path)
else:
return self.get_obj([vim.VirtualMachine], name) | Get a VirtualMachine object |
def get_vm_failfast(self, name, verbose=False, vm_term='VM', path=""):
if verbose:
print("Finding VirtualMachine named %s..." % name)
if path:
vm = self.get_vm(name, path=path)
else:
vm = self.get_vm(name)
if vm is None:
print("Err... | Get a VirtualMachine object
fail fast if the object isn't a valid reference |
def WaitForVirtualMachineShutdown(
self,
vm_to_poll,
timeout_seconds,
sleep_period=5
):
seconds_waited = 0 # wait counter
while seconds_waited < timeout_seconds:
# sleep first, since nothing shuts down instantly
second... | Guest shutdown requests do not run a task we can wait for.
So, we must poll and wait for status to be poweredOff.
Returns True if shutdown, False if poll expired. |
def request_patch(self, *args, **kwargs):
func = sup = super(FuturesSession, self).request
background_callback = kwargs.pop('background_callback', None)
if background_callback:
def wrap(*args_, **kwargs_):
resp = sup(*args_, **kwargs_)
# Patch the closure to return the ... | Maintains the existing api for Session.request.
Used by all of the higher level methods, e.g. Session.get.
The background_callback param allows you to do some processing on the
response in the background, e.g. call resp.json() so that json parsing
happens in the background thread. |
def _start_http_session(self):
api_logger.debug("Starting new HTTP session...")
self.session = FuturesSession(executor=self.executor, max_workers=self.max_workers)
self.session.headers.update({"User-Agent": self.user_agent})
if self.username and self.password:
api_lo... | Start a new requests HTTP session, clearing cookies and session data.
:return: None |
def _service_request(self, request_type, sub_uri, params=None, callback=None,
raise_for_status=True, raw=False, **kwargs):
api_logger.debug("Sending request: {} ({})".format(sub_uri, request_type))
if not self.session:
self._start_http_session()
uri ... | Base method for handling HTTP requests via the current requests session.
:param request_type: The request type as a string (e.g. "POST", "GET", "PUT", etc.)
:param sub_uri: The REST end point (sub-uri) to communicate with.
:param params: (Optional) HTTP Request parameters. Default: none
... |
def location(ip=None, key=None, field=None):
''' Get geolocation data for a given IP address
If field is specified, get specific field as text
Else get complete location data as JSON
'''
if field and (field not in field_list):
return 'Invalid field'
if field:
if ip:
... | Get geolocation data for a given IP address
If field is specified, get specific field as text
Else get complete location data as JSON |
async def main_loop(loop, password, user, ip): # pylint: disable=invalid-name
async with aiohttp.ClientSession(loop=loop) as session:
VAR['sma'] = pysma.SMA(session, ip, password=password, group=user)
await VAR['sma'].new_session()
if VAR['sma'].sma_sid is None:
_LOGGER.inf... | Main loop. |
def main():
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
parser = argparse.ArgumentParser(
description='Test the SMA webconnect library.')
parser.add_argument(
'ip', type=str, help='IP address of the Webconnect module')
parser.add_argument(
'user', help='inst... | Main example. |
def update(self):
result = self.api.github_api(*self._apicall_parameters())
if result is None:
# an error occurred, try again after BACKOFF
self._next_update = datetime.now() + timedelta(seconds=self.BACKOFF)
# assume an empty result until the error disappear... | Connect to GitHub API endpoint specified by `_apicall_parameters()`,
postprocess the result using `_apiresult_postprocess()` and trigger
a cache update if the API call was successful.
If an error occurs, cache the empty result generated by
`_apiresult_error()`. Additionally, set up retr... |
def data(self):
if self._next_update and datetime.now() > self._next_update:
self.update()
return self._data | Get a cached post-processed result of a GitHub API call. Uses Trac cache
to avoid constant querying of the remote API. If a previous API call did
not succeed, automatically retries after a timeout. |
def teams(self):
teams = self._teamlist.teams()
# find out which teams have been added or removed since the last sync
current_teams = set(self._teamobjects.keys())
new_teams = set(teams.keys()) # pylint: disable=no-member
added = new_teams - current_teams
remove... | Return a sequence of `GitHubTeam` objects, one for each team in this
org. |
def members(self):
allmembers = set()
for team in self.teams():
allmembers.update(team.members())
return sorted(allmembers) | Return a list of all users in this organization. Users are identified
by their login name. Note that this is computed from the teams in the
organization, because GitHub does not currently offer a WebHook for
organization membership, so converting org membership would lead to
stale data. |
def update_team(self, slug):
if slug not in self._teamobjects:
# This case is checked and handled further up, but better be safe
# than sorry.
return False # pragma: no cover
return self._teamobjects[slug].update() | Trigger an update and cache invalidation for the team identified by the
given `slug`. Returns `True` on success, `False` otherwise.
:param slug: The GitHub 'slug' that identifies the team in URLs |
def github_api(self, url, *args):
import requests
import urllib
github_api_url = os.environ.get("TRAC_GITHUB_API_URL", "https://api.github.com/")
formatted_url = github_api_url + url.format(*(urllib.quote(str(x)) for x in args))
access_token = _config_secret(self.access... | Connect to the given GitHub API URL template by replacing all
placeholders with the given parameters and return the decoded JSON
result on success. On error, return `None`.
:param url: The path to request from the GitHub API. Contains format
string placeholders that will be ... |
def update_team(self, slug):
if self._org:
if not self._org.has_team(slug):
return self._org.update()
return self._org.update_team(slug)
# self._org is created during Trac startup, so there should never
# be a case where we try to update an org be... | Trigger update and cache invalidation for the team identified by the
given `slug`, if any. Returns `True` if the update was successful,
`False` otherwise.
:param slug: GitHub 'slug' name for the team to be updated. |
def get_permission_groups(self, username):
if not self.organization or not self.username or not self.access_token:
return []
elif (self.username_prefix and
not username.startswith(self.username_prefix)):
return []
data = self._fetch_groups()
... | Return a list of names of the groups that the user with the specified
name is a member of. Implements an `IPermissionGroupProvider` API.
This specific implementation connects to GitHub with a dedicated user,
fetches and caches the teams and their users configured at GitHub and
converts ... |
def match_request(self, req):
match = self._request_re.match(req.path_info)
if match:
return True
if os.environ.get('TRAC_GITHUB_ENABLE_DEBUGGING', None) is not None:
debug_match = self._debug_request_re.match(req.path_info)
if debug_match:
... | Return whether the handler wants to process the given request.
Implements an `IRequestHandler` API. |
def process_debug_request(self, req):
req.send(json.dumps(self._fetch_groups()).encode('utf-8'), 'application/json', 200) | Debgging helper used for testing, processes the given request and dumps
the internal state of cached user to group mappings. Note that this is
only callable if TRAC_GITHUB_ENABLE_DEBUGGING is set in the
environment. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.