signature
stringlengths 8
3.44k
| body
stringlengths 0
1.41M
| docstring
stringlengths 1
122k
| id
stringlengths 5
17
|
|---|---|---|---|
def all(self):
|
return tuple([WLogicalVolume(x) for x in self.lvm_command().lvm_info()])<EOL>
|
Return every logical volume in the system
:return: tuple of WLogicalVolume
|
f9893:c4:m1
|
def volume_path(self):
|
return self.lvm_entity()[<NUM_LIT:0>]<EOL>
|
Return logical volume path
:return: str
|
f9893:c4:m2
|
def volume_name(self):
|
return os.path.basename(self.volume_path())<EOL>
|
Return logical volume name
:return: str
|
f9893:c4:m3
|
def volume_group_name(self):
|
return self.lvm_entity()[<NUM_LIT:1>]<EOL>
|
Return volume group name
:return: str
|
f9893:c4:m4
|
def volume_group(self):
|
return WVolumeGroup(self.volume_group_name(), sudo=self.lvm_command().sudo())<EOL>
|
Return volume group
:return: WVolumeGroup
|
f9893:c4:m5
|
def sectors_count(self):
|
return int(self.lvm_entity()[<NUM_LIT:6>])<EOL>
|
Return logical volume size in sectors
:return: int
|
f9893:c4:m6
|
def extents_count(self):
|
return int(self.lvm_entity()[<NUM_LIT:7>])<EOL>
|
Return current logical extents associated to logical volume
:return: int
|
f9893:c4:m7
|
def device_number(self):
|
return int(self.lvm_entity()[<NUM_LIT:11>]), int(self.lvm_entity()[<NUM_LIT:12>])<EOL>
|
Return tuple of major and minor device number of logical volume
:return: tuple of int
|
f9893:c4:m8
|
def uuid(self):
|
uuid_file = '<STR_LIT>' % os.path.basename(os.path.realpath(self.volume_path()))<EOL>lv_uuid = open(uuid_file).read().strip()<EOL>if lv_uuid.startswith('<STR_LIT>') is True:<EOL><INDENT>return lv_uuid[<NUM_LIT:4>:]<EOL><DEDENT>return lv_uuid<EOL>
|
Return UUID of logical volume
:return: str
|
f9893:c4:m9
|
@verify_type(snapshot_size=(int, float), snapshot_suffix=str)<EOL><INDENT>@verify_value(snapshot_size=lambda x: x > <NUM_LIT:0>, snapshot_suffix=lambda x: len(x) > <NUM_LIT:0>)<EOL>def create_snapshot(self, snapshot_size, snapshot_suffix):<DEDENT>
|
size_extent = math.ceil(self.extents_count() * snapshot_size)<EOL>size_kb = self.volume_group().extent_size() * size_extent<EOL>snapshot_name = self.volume_name() + snapshot_suffix<EOL>lvcreate_cmd = ['<STR_LIT>'] if self.lvm_command().sudo() is True else []<EOL>lvcreate_cmd.extend([<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>' % size_kb, '<STR_LIT>', '<STR_LIT>', snapshot_name, '<STR_LIT>', '<STR_LIT:r>', self.volume_path()<EOL>])<EOL>subprocess.check_output(lvcreate_cmd, timeout=self.__class__.__lvm_snapshot_create_cmd_timeout__)<EOL>return WLogicalVolume(self.volume_path() + snapshot_suffix, sudo=self.lvm_command().sudo())<EOL>
|
Create snapshot for this logical volume.
:param snapshot_size: size of newly created snapshot volume. This size is a fraction of the source \
logical volume space (of this logical volume)
:param snapshot_suffix: suffix for logical volume name (base part is the same as the original volume \
name)
:return: WLogicalVolume
|
f9893:c4:m10
|
def remove_volume(self):
|
lvremove_cmd = ['<STR_LIT>'] if self.lvm_command().sudo() is True else []<EOL>lvremove_cmd.extend(['<STR_LIT>', '<STR_LIT>', self.volume_path()])<EOL>subprocess.check_output(lvremove_cmd, timeout=self.__class__.__lvm_snapshot_remove_cmd_timeout__)<EOL>
|
Remove this volume
:return: None
|
f9893:c4:m11
|
def snapshot_allocation(self):
|
check_cmd = ['<STR_LIT>', self.volume_path(), '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>output = subprocess.check_output(check_cmd, timeout=self.__class__.__lvm_snapshot_check_cmd_timeout__)<EOL>output = output.decode().strip()<EOL>if len(output) == <NUM_LIT:0>:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>return float(output.replace('<STR_LIT:U+002C>', '<STR_LIT:.>', <NUM_LIT:1>))<EOL>
|
Return allocated size (fraction of total snapshot volume space). If this is not a snapshot volume,
than RuntimeError exception is raised.
:return: float
|
f9893:c4:m12
|
def snapshot_corrupted(self):
|
return self.snapshot_allocation() > self.__class__.__snapshot_maximum_allocation__<EOL>
|
Check if this snapshot volume is corrupted or not
:return: bool (True if corrupted, False - otherwise)
|
f9893:c4:m13
|
@classmethod<EOL><INDENT>@verify_type('<STR_LIT>', file_path=str, sudo=bool)<EOL>@verify_value('<STR_LIT>', file_path=lambda x: len(x) > <NUM_LIT:0>)<EOL>def logical_volume(cls, file_path, sudo=False):<DEDENT>
|
mp = WMountPoint.mount_point(file_path)<EOL>if mp is not None:<EOL><INDENT>name_file = '<STR_LIT>' % mp.device_name()<EOL>if os.path.exists(name_file):<EOL><INDENT>lv_path = '<STR_LIT>' % open(name_file).read().strip()<EOL>return WLogicalVolume(lv_path, sudo=sudo)<EOL><DEDENT><DEDENT>
|
Return logical volume that stores the given path
:param file_path: target path to search
:param sudo: same as 'sudo' in :meth:`.WLogicalVolume.__init__`
:return: WLogicalVolume or None (if file path is outside current mount points)
|
f9893:c4:m14
|
def local_tz():
|
return timezone(timedelta(<NUM_LIT:0>, (time.timezone * -<NUM_LIT:1>)))<EOL>
|
Return current system timezone shift from UTC
:return: datetime.timezone
|
f9894:m0
|
@verify_type(dt=(datetime, None), local_value=bool)<EOL>def utc_datetime(dt=None, local_value=True):
|
<EOL>if dt is None:<EOL><INDENT>return datetime.now(tz=timezone.utc)<EOL><DEDENT>result = dt<EOL>if result.utcoffset() is None:<EOL><INDENT>if local_value is False:<EOL><INDENT>return result.replace(tzinfo=timezone.utc)<EOL><DEDENT>else:<EOL><INDENT>result = result.replace(tzinfo=local_tz())<EOL><DEDENT><DEDENT>return result.astimezone(timezone.utc)<EOL>
|
Convert local datetime and/or datetime without timezone information to UTC datetime with timezone
information.
:param dt: local datetime to convert. If is None, then system datetime value is used
:param local_value: whether dt is a datetime in system timezone or UTC datetime without timezone information
:return: datetime in UTC with tz set
|
f9894:m1
|
@verify_type(dt=(datetime, None), utc_value=bool)<EOL>def local_datetime(dt=None, utc_value=True):
|
<EOL>if dt is None:<EOL><INDENT>return datetime.now(tz=local_tz())<EOL><DEDENT>result = dt<EOL>if result.utcoffset() is None:<EOL><INDENT>if utc_value is False:<EOL><INDENT>return result.replace(tzinfo=local_tz())<EOL><DEDENT>else:<EOL><INDENT>result = result.replace(tzinfo=timezone.utc)<EOL><DEDENT><DEDENT>return result.astimezone(local_tz())<EOL>
|
Convert UTC datetime and/or datetime without timezone information to local datetime with timezone
information
:param dt: datetime in UTC to convert. If is None, then system datetime value is used
:param utc_value: whether dt is a datetime in UTC or in system timezone without timezone information
:return: datetime for system (local) timezone with tz set
|
f9894:m2
|
@abstractmethod<EOL><INDENT>def context_name(self):<DEDENT>
|
raise NotImplementedError('<STR_LIT>')<EOL>
|
Return this context name
:return: str
|
f9895:c0:m0
|
@abstractmethod<EOL><INDENT>def context_value(self):<DEDENT>
|
raise NotImplementedError('<STR_LIT>')<EOL>
|
Return this context value (can be None)
:return: str or None
|
f9895:c0:m1
|
@abstractmethod<EOL><INDENT>def linked_context(self):<DEDENT>
|
raise NotImplementedError('<STR_LIT>')<EOL>
|
Return link to 'parent'/'higher' context
:return: WContextProto or None
|
f9895:c0:m2
|
def __len__(self):
|
return len([x for x in self])<EOL>
|
Return linked context count
:return: int
|
f9895:c0:m3
|
def __iter__(self):
|
context = self<EOL>while context is not None:<EOL><INDENT>yield context<EOL>context = context.linked_context()<EOL><DEDENT>
|
Iterate over
:return:
|
f9895:c0:m4
|
def __eq__(self, other):
|
if isinstance(other, WContextProto) is False:<EOL><INDENT>return False<EOL><DEDENT>context_a = self<EOL>context_b = other<EOL>while context_a is not None and context_b is not None:<EOL><INDENT>if context_a.context_name() != context_b.context_name():<EOL><INDENT>return False<EOL><DEDENT>context_a = context_a.linked_context()<EOL>context_b = context_b.linked_context()<EOL><DEDENT>if context_b is not None:<EOL><INDENT>return False<EOL><DEDENT>elif context_a is not None:<EOL><INDENT>return False<EOL><DEDENT>return True<EOL>
|
Compare two context. Two context are equal if they have the same context_name and each their own
linked context are equal also.
:param other: context to compare
:return: bool
|
f9895:c0:m5
|
@verify_type(context_name=str, context_value=(str, None), linked_context=(WContextProto, None))<EOL><INDENT>def __init__(self, context_name, context_value=None, linked_context=None):<DEDENT>
|
self.__context_name = context_name<EOL>self.__context_value = context_value<EOL>self.__linked_context = linked_context<EOL>
|
Create new context request
:param context_name: context name
:param context_value: context value
:param linked_context: linked context
|
f9895:c1:m0
|
def context_name(self):
|
return self.__context_name<EOL>
|
:meth:`.WContextProto.context_name` implementation
|
f9895:c1:m1
|
def context_value(self):
|
return self.__context_value<EOL>
|
:meth:`.WContextProto.context_value` implementation
|
f9895:c1:m2
|
def linked_context(self):
|
return self.__linked_context<EOL>
|
:meth:`.WContextProto.linked_context` implementation
|
f9895:c1:m3
|
@classmethod<EOL><INDENT>@verify_type(context=(WContextProto, None))<EOL>def export_context(cls, context):<DEDENT>
|
if context is None:<EOL><INDENT>return<EOL><DEDENT>result = [(x.context_name(), x.context_value()) for x in context]<EOL>result.reverse()<EOL>return tuple(result)<EOL>
|
Export the specified context to be capable context transferring
:param context: context to export
:return: tuple
|
f9895:c1:m4
|
@classmethod<EOL><INDENT>@verify_type(context=(tuple, list, None))<EOL>def import_context(cls, context):<DEDENT>
|
if context is None or len(context) == <NUM_LIT:0>:<EOL><INDENT>return<EOL><DEDENT>result = WContext(context[<NUM_LIT:0>][<NUM_LIT:0>], context[<NUM_LIT:0>][<NUM_LIT:1>])<EOL>for iter_context in context[<NUM_LIT:1>:]:<EOL><INDENT>result = WContext(iter_context[<NUM_LIT:0>], context_value=iter_context[<NUM_LIT:1>], linked_context=result)<EOL><DEDENT>return result<EOL>
|
Import context to corresponding WContextProto object (:meth:`WContext.export_context` reverse operation)
:param context: context to import
:return: WContext
|
f9895:c1:m5
|
@classmethod<EOL><INDENT>@verify_type(context_specs=str)<EOL>def specification(cls, *context_specs):<DEDENT>
|
import_data = []<EOL>for name in context_specs:<EOL><INDENT>import_data.append((name, None))<EOL><DEDENT>return cls.import_context(import_data)<EOL>
|
Return linked context as adapter specification (is used by :class:`.WCommandContextAdapter`)
:param context_specs: context names
:return: WContext
|
f9895:c1:m6
|
@verify_type(context_specifications=(WContextProto, None))<EOL><INDENT>def __init__(self, context_specifications):<DEDENT>
|
self.__spec = context_specifications<EOL>
|
Create adapter
:param context_specifications: context for what this adapter works
|
f9895:c3:m0
|
def specification(self):
|
return self.__spec<EOL>
|
Return adapter specification
:return: WContextProto or None
|
f9895:c3:m1
|
@verify_type(command_context=(WContextProto, None))<EOL><INDENT>def match(self, command_context=None, **command_env):<DEDENT>
|
spec = self.specification()<EOL>if command_context is None and spec is None:<EOL><INDENT>return True<EOL><DEDENT>elif command_context is not None and spec is not None:<EOL><INDENT>return command_context == spec<EOL><DEDENT>return False<EOL>
|
Check if context request is compatible with adapters specification. True - if compatible,
False - otherwise
:param command_context: context to check
:param command_env: command environment
:return: bool
|
f9895:c3:m2
|
@abstractmethod<EOL><INDENT>@verify_type(command_tokens=str, command_context=(WContextProto, None))<EOL>def adapt(self, *command_tokens, command_context=None, **command_env):<DEDENT>
|
raise NotImplementedError('<STR_LIT>')<EOL>
|
Adapt the given command tokens with this adapter
:param command_tokens: command tokens to adapt
:param command_context: context
:param command_env: command environment
:return: list of str
|
f9895:c3:m3
|
@verify_type(command=WCommandProto, context_adapter=WCommandContextAdapter)<EOL><INDENT>def __init__(self, base_command, context_adapter):<DEDENT>
|
WCommandProto.__init__(self)<EOL>self.__command = base_command<EOL>self.__adapter = context_adapter<EOL>
|
Create new command
:param base_command: basic command that does real magic
:param context_adapter: adapter for command tokens modification
|
f9895:c4:m0
|
def original_command(self):
|
return self.__command<EOL>
|
Return source command
:return: WCommandProto
|
f9895:c4:m1
|
def adapter(self):
|
return self.__adapter<EOL>
|
Return command adapter
:return: WCommandAdapter
|
f9895:c4:m2
|
@verify_type('<STR_LIT>', command_tokens=str, command_context=(WContextProto, None))<EOL><INDENT>def match(self, *command_tokens, command_context=None, **command_env):<DEDENT>
|
if self.adapter().match(command_context, **command_env) is False:<EOL><INDENT>return False<EOL><DEDENT>command_tokens = self.adapter().adapt(*command_tokens, command_context=command_context, **command_env)<EOL>return self.original_command().match(*command_tokens, command_context=command_context, **command_env)<EOL>
|
Match command
:param command_tokens: command tokens to check
:param command_context: command context
:param command_env: command environment
:return: bool
|
f9895:c4:m3
|
@verify_type('<STR_LIT>', command_tokens=str, command_context=(WContextProto, None))<EOL><INDENT>def exec(self, *command_tokens, command_context=None, **command_env):<DEDENT>
|
if self.adapter().match(command_context, **command_env) is False:<EOL><INDENT>cmd = WCommandProto.join_tokens(*command_tokens)<EOL>spec = self.adapter().specification()<EOL>if spec is not None:<EOL><INDENT>spec = [x.context_name() for x in spec]<EOL>spec.reverse()<EOL>spec = '<STR_LIT:U+002C>'.join(spec)<EOL><DEDENT>raise RuntimeError('<STR_LIT>' % (cmd, spec))<EOL><DEDENT>command_tokens = self.adapter().adapt(*command_tokens, command_context=command_context, **command_env)<EOL>return self.original_command().exec(*command_tokens, command_context=command_context, **command_env)<EOL>
|
Execute command
:param command_tokens: command tokens to execute
:param command_context: command context
:param command_env: command environment
:return: WCommandResultProto
|
f9895:c4:m4
|
@abstractmethod<EOL><INDENT>@verify_type(command_tokens=str)<EOL>def match(self, *command_tokens, **command_env):<DEDENT>
|
raise NotImplementedError('<STR_LIT>')<EOL>
|
Checks whether this command can be called with the given tokens. Return True - if tokens match this
command, False - otherwise
:param command_tokens: command to check
:param command_env: command environment
:return: bool
|
f9896:c0:m0
|
@abstractmethod<EOL><INDENT>@verify_type(command_tokens=str)<EOL>def exec(self, *command_tokens, **command_env):<DEDENT>
|
raise NotImplementedError('<STR_LIT>')<EOL>
|
Execute valid command (that represent as tokens)
:param command_tokens: command to execute
:param command_env: command environment
:return: WCommandResultProto
|
f9896:c0:m1
|
@staticmethod<EOL><INDENT>@verify_type(command_str=str)<EOL>def split_command(command_str):<DEDENT>
|
return shlex.split(command_str)<EOL>
|
Split command string into command tokens
:param command_str: command to split
:return: tuple of str
|
f9896:c0:m2
|
@staticmethod<EOL><INDENT>@verify_type(command_tokens=str)<EOL>def join_tokens(*command_tokens):<DEDENT>
|
return '<STR_LIT:U+0020>'.join([shlex.quote(x) for x in command_tokens])<EOL>
|
Join tokens into a single string
:param command_tokens: tokens to join
:return: str
|
f9896:c0:m3
|
@verify_type(command_tokens=str)<EOL><INDENT>def __init__(self, *command_tokens):<DEDENT>
|
WCommandProto.__init__(self)<EOL>self.__command = tuple(command_tokens)<EOL>
|
Create new command
:param command_tokens: tokens (command) that call this command, like 'help' or ('create', 'object')
|
f9896:c1:m0
|
def command(self):
|
return self.__command<EOL>
|
Return command tokens
:return: tuple of str
|
f9896:c1:m1
|
@verify_type(command_tokens=str)<EOL><INDENT>def match(self, *command_tokens, **command_env):<DEDENT>
|
command = self.command()<EOL>if len(command_tokens) >= len(command):<EOL><INDENT>return command_tokens[:len(command)] == command<EOL><DEDENT>return False<EOL>
|
:meth:`.WCommandProto.match` implementation
|
f9896:c1:m2
|
@abstractmethod<EOL><INDENT>@verify_type('<STR_LIT>', command_tokens=str)<EOL>def _exec(self, *command_tokens, **command_env):<DEDENT>
|
raise NotImplementedError('<STR_LIT>')<EOL>
|
Derived classes must implement this function, in order to do a real command work.
:param command_tokens: command to execute
:param command_env: command environment
:return: WCommandResultProto
|
f9896:c1:m3
|
@verify_type(command_tokens=str)<EOL><INDENT>def exec(self, *command_tokens, **command_env):<DEDENT>
|
if self.match(*command_tokens, **command_env) is False:<EOL><INDENT>raise RuntimeError('<STR_LIT>' % self.join_tokens(*command_tokens))<EOL><DEDENT>return self._exec(*command_tokens, **command_env)<EOL>
|
:meth:`.WCommandProto.exec` implementation
(throws RuntimeError if tokens are invalid, and calls :meth:`.WCommand._exec` method)
|
f9896:c1:m4
|
def __init__(self):
|
self.__commands = []<EOL>
|
Create new storage/selector
|
f9896:c2:m0
|
@verify_type(command_obj=WCommandProto)<EOL><INDENT>def add(self, command_obj):<DEDENT>
|
self.__commands.append(command_obj)<EOL>
|
Add command to selector
:param command_obj: command to add
:return: None
|
f9896:c2:m1
|
@verify_type(command_tokens=str)<EOL><INDENT>def select(self, *command_tokens, **command_env):<DEDENT>
|
for command_obj in self:<EOL><INDENT>if command_obj.match(*command_tokens, **command_env):<EOL><INDENT>return command_obj<EOL><DEDENT><DEDENT>
|
Select suitable command, that matches the given tokens. Each new command to check is fetched with
this object iterator (:meth:`.WCommandSelector.__iter__`)
:param command_tokens: command
:param command_env: command environment
:return: WCommandProto
|
f9896:c2:m2
|
def __iter__(self):
|
for command in self.__commands:<EOL><INDENT>yield command<EOL><DEDENT>
|
Iterate over internal storage and yield next command
|
f9896:c2:m3
|
def __len__(self):
|
return len(self.__commands)<EOL>
|
Return command count
:return: int
|
f9896:c2:m4
|
@verify_type(default_priority=int)<EOL><INDENT>def __init__(self, default_priority=<NUM_LIT:30>):<DEDENT>
|
WCommandSelector.__init__(self)<EOL>self.__default_priority = default_priority<EOL>self.__priorities = {}<EOL>
|
Create new selector
:param default_priority: priority for commands, that were added via \
:meth:`.WCommandPrioritizedSelector.add` method
|
f9896:c3:m0
|
@verify_type(command_obj=WCommandProto)<EOL><INDENT>def add(self, command_obj):<DEDENT>
|
self.add_prioritized(command_obj, self.__default_priority)<EOL>
|
:meth:`.WCommandSelector.add` redefinition (sets default priority for the given command)
|
f9896:c3:m1
|
@verify_type(command_obj=WCommandProto, priority=int)<EOL><INDENT>def add_prioritized(self, command_obj, priority):<DEDENT>
|
if priority not in self.__priorities.keys():<EOL><INDENT>self.__priorities[priority] = []<EOL><DEDENT>self.__priorities[priority].append(command_obj)<EOL>
|
Add command with the specified priority
:param command_obj: command to add
:param priority: command priority
:return: None
|
f9896:c3:m2
|
def __iter__(self):
|
priorities = list(self.__priorities.keys())<EOL>priorities.sort()<EOL>for priority in priorities:<EOL><INDENT>for command in self.__priorities[priority]:<EOL><INDENT>yield command<EOL><DEDENT><DEDENT>
|
Iterate over internal storage and yield next command. Commands with lower priority will be yielded
first
|
f9896:c3:m3
|
def __len__(self):
|
result = <NUM_LIT:0><EOL>for commands in self.__priorities.values():<EOL><INDENT>result += len(commands)<EOL><DEDENT>return result<EOL>
|
Return command count
:return: int
|
f9896:c3:m4
|
@verify_type(command_selector=(WCommandSelector, None), follow_vars=(list, tuple, set, None))<EOL><INDENT>def __init__(self, command_selector=None, tracked_vars=None):<DEDENT>
|
self.__commands = command_selector if command_selector is not None else WCommandSelector()<EOL>self.__tracked_vars = tuple(tracked_vars) if tracked_vars is not None else tuple()<EOL>self.__vars = {}<EOL>
|
Create new set
:param command_selector: selector (storage) for commands to use
:param tracked_vars: if it is specified - tuple/list/set of variables names, that must be kept \
between commands calls
|
f9896:c4:m0
|
def commands(self):
|
return self.__commands<EOL>
|
Return used command selector
:return: WCommandSelector
|
f9896:c4:m1
|
def tracked_vars(self):
|
return self.__tracked_vars<EOL>
|
Return variables names that are kept (tracked) by this command set
:return: tuple of str
|
f9896:c4:m2
|
def has_var(self, var_name):
|
return var_name in self.__vars.keys()<EOL>
|
Return True - if a environment variable with a specified name is kept by this command set.
Otherwise - False is returned
:param var_name: variable name to check
:return: bool
|
f9896:c4:m3
|
def var_value(self, var_name):
|
return self.__vars[var_name]<EOL>
|
Return value of environment variable that is kept by this command set.
:note: No checks are made if there is a such variable. It implies that there is a such variable.
For any doubt - use :meth:`.WCommandSet.has_var` method
:param var_name: target variable name
:return: anything
|
f9896:c4:m4
|
@verify_type('<STR_LIT>', command_str=str)<EOL><INDENT>def exec(self, command_str, **command_env):<DEDENT>
|
env = self.__vars.copy()<EOL>env.update(command_env)<EOL>command_tokens = WCommandProto.split_command(command_str)<EOL>command_obj = self.commands().select(*command_tokens, **env)<EOL>if command_obj is None:<EOL><INDENT>raise WCommandSet.NoCommandFound('<STR_LIT>' % command_str)<EOL><DEDENT>result = command_obj.exec(*command_tokens, **env)<EOL>self.__track_vars(result)<EOL>return result<EOL>
|
Execute the given command (command will be split into tokens, every space that is a part of a token
must be quoted)
:param command_str: command to execute
:param command_env: command environment
:return: WCommandResultProto
|
f9896:c4:m5
|
@verify_type(command_result=WCommandResultProto)<EOL><INDENT>def __track_vars(self, command_result):<DEDENT>
|
command_env = command_result.environment()<EOL>for var_name in self.tracked_vars():<EOL><INDENT>if var_name in command_env.keys():<EOL><INDENT>self.__vars[var_name] = command_env[var_name]<EOL><DEDENT><DEDENT>
|
Check if there are any tracked variable inside the result. And keep them for future use.
:param command_result: command result tot check
:return:
|
f9896:c4:m6
|
@verify_type(selector=WCommandSelector)<EOL><INDENT>def __init__(self, selector):<DEDENT>
|
WCommandProto.__init__(self)<EOL>self.__selector = selector<EOL>
|
Create new command alias
:param selector: selector that has commands, that will be run from this one
|
f9896:c5:m0
|
def selector(self):
|
return self.__selector<EOL>
|
Return original command selector
:return: WCommandSelector
|
f9896:c5:m1
|
@abstractmethod<EOL><INDENT>def mutate_command_tokens(self, *command_tokens):<DEDENT>
|
raise NotImplementedError('<STR_LIT>')<EOL>
|
Modify the input command so it can be called from the command selector
:param command_tokens:
:return:
|
f9896:c5:m2
|
@verify_type(command_tokens=str)<EOL><INDENT>def match(self, *command_tokens, **command_env):<DEDENT>
|
mutated_command_tokens = self.mutate_command_tokens(*command_tokens)<EOL>if mutated_command_tokens is None:<EOL><INDENT>return False<EOL><DEDENT>return self.selector().select(*mutated_command_tokens, **command_env) is not None<EOL>
|
:meth:`.WCommandProto.match` implementation
|
f9896:c5:m3
|
@verify_type(command_tokens=str)<EOL><INDENT>def exec(self, *command_tokens, **command_env):<DEDENT>
|
mutated_command_tokens = self.mutate_command_tokens(*command_tokens)<EOL>if mutated_command_tokens is not None:<EOL><INDENT>command = self.selector().select(*mutated_command_tokens, **command_env)<EOL>if command is not None:<EOL><INDENT>return command.exec(*mutated_command_tokens, **command_env)<EOL><DEDENT><DEDENT>raise RuntimeError('<STR_LIT>' % self.join_tokens(*command_tokens))<EOL>
|
:meth:`.WCommandProto.exec` implementation
|
f9896:c5:m4
|
@verify_type('<STR_LIT>', selector=WCommandSelector)<EOL><INDENT>@verify_type(reduce_tokens=str)<EOL>def __init__(self, selector, *reduce_tokens):<DEDENT>
|
WCommandAlias.__init__(self, selector)<EOL>if len(reduce_tokens) == <NUM_LIT:0>:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>self.__reduce_tokens = reduce_tokens<EOL>
|
Create new command
:param selector: selector to use
:param reduce_tokens: section names (aliases)
|
f9896:c6:m0
|
def reduce_tokens(self):
|
return self.__reduce_tokens<EOL>
|
Return section names (aliases)
:return: tuple of str
|
f9896:c6:m1
|
def mutate_command_tokens(self, *command_tokens):
|
if len(command_tokens) > <NUM_LIT:0>:<EOL><INDENT>if command_tokens[<NUM_LIT:0>] in self.reduce_tokens():<EOL><INDENT>return command_tokens[<NUM_LIT:1>:]<EOL><DEDENT><DEDENT>
|
:meth:`.WCommandAlias.mutate_command_tokens` implementation
|
f9896:c6:m2
|
@verify_type('<STR_LIT>', command_tokens=str)<EOL><INDENT>@verify_type(template=WTemplate, template_context=(dict, None))<EOL>def __init__(self, template, *command_tokens, template_context=None):<DEDENT>
|
WCommand.__init__(self, *command_tokens)<EOL>self.__template = template<EOL>self.__template_context = template_context if template_context is not None else {}<EOL>
|
Create new static template command
:param template: template to use as result
:param command_tokens: tokens that is used for command matching
:param template_context: context that is used for rendering the template
|
f9898:c1:m0
|
def template(self):
|
return self.__template<EOL>
|
Return template object (that will be used in result)
:return: WTemplate
|
f9898:c1:m1
|
def template_context(self):
|
return self.__template_context<EOL>
|
Return context with which template will be rendered
:return: dict
|
f9898:c1:m2
|
def result_template(self, *command_tokens, **command_env):
|
result = WCommandResultTemplate(self.template())<EOL>result.update_context(**self.template_context())<EOL>return result<EOL>
|
Generate template result. command_tokens and command_env arguments are used for template
detailing
:param command_tokens: same as command_tokens in :meth:`.WCommandProto.match` and \
:meth:`.WCommandProto.exec` methods (so as :meth:`.WCommand._exec`)
:param command_env: same as command_env in :meth:`.WCommandProto.match` and \
:meth:`.WCommandProto.exec` methods (so as :meth:`.WCommand._exec`)
:return: WCommandResultTemplate
|
f9898:c1:m3
|
def match(self, *command_tokens, **command_env):
|
if command_tokens == self.command():<EOL><INDENT>return True<EOL><DEDENT>return False<EOL>
|
same as :meth:`.WCommand.meth` method, but checks for extra arguments also (no extra arguments is
allowed)
|
f9898:c1:m4
|
def _exec(self, *command_tokens, **command_env):
|
return self.result_template(*command_tokens, **command_env)<EOL>
|
:meth:`.WCommand._exec` implementation
|
f9898:c1:m5
|
@verify_type(argument_name=str, required=bool, flag_mode=bool, multiple_values=bool, help_info=(str, None))<EOL><INDENT>@verify_type(meta_var=(str, None), default_value=(str, None))<EOL>@verify_value(argument_name=lambda x: len(x) > <NUM_LIT:0>)<EOL>def __init__(<EOL>self, argument_name, required=False, flag_mode=False, multiple_values=False, help_info=None,<EOL>meta_var=None, default_value=None, casting_helper=None<EOL>):<DEDENT>
|
if (flag_mode is True and multiple_values is True) or(flag_mode is True and default_value is not None) or(multiple_values is True and default_value is not None):<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>if casting_helper is not None:<EOL><INDENT>flag_helper = WCommandArgumentDescriptor.FlagArgumentCastingHelper<EOL>general_helper = WCommandArgumentDescriptor.ArgumentCastingHelper<EOL>if flag_mode is True and isinstance(casting_helper, flag_helper) is False:<EOL><INDENT>raise TypeError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>elif flag_mode is False and isinstance(casting_helper, general_helper) is False:<EOL><INDENT>raise TypeError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT><DEDENT>self.__argument_name = argument_name<EOL>self.__flag_mode = flag_mode<EOL>self.__multiple_values = multiple_values<EOL>self.__default_value = default_value<EOL>self.__required = required<EOL>self.__help_info = help_info<EOL>self.__meta_var = meta_var<EOL>if casting_helper is not None:<EOL><INDENT>self.__casting_helper = casting_helper<EOL><DEDENT>elif flag_mode is True:<EOL><INDENT>self.__casting_helper = WCommandArgumentDescriptor.FlagArgumentCastingHelper()<EOL><DEDENT>else:<EOL><INDENT>self.__casting_helper = WCommandArgumentDescriptor.StringArgumentCastingHelper()<EOL><DEDENT>
|
note: 'required' is useless for flag-mode attribute
|
f9900:c1:m0
|
def __init__(cls, name, bases, namespace):
|
ABCMeta.__init__(cls, name, bases, namespace)<EOL>cls.__class_capabilities__ = {}<EOL>for i in dir(cls):<EOL><INDENT>i = getattr(cls, i)<EOL>if i is not None and hasattr(i, '<STR_LIT>'):<EOL><INDENT>cap_name = i.__capability_name__<EOL>if cap_name in cls.__class_capabilities__:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' %<EOL>(cap_name, name, i.__name__, cls.__class_capabilities__[cap_name])<EOL>)<EOL><DEDENT>cls.__class_capabilities__[cap_name] = i.__name__<EOL><DEDENT><DEDENT>
|
Generate new class with this metaclass. Find and register every "capability", that were defined
by a specified class
:param name: same as 'name' in :meth:`.ABCMeta.__init__` method
:param bases: same as 'bases' in :meth:`.ABCMeta.__init__` method
:param namespace: same as 'namespace' in :meth:`.ABCMeta.__init__` method
|
f9902:c0:m0
|
@staticmethod<EOL><INDENT>@verify_type(cap_name=str)<EOL>def capability(cap_name):<DEDENT>
|
def fn_decorator(original_function):<EOL><INDENT>original_function.__capability_name__ = cap_name<EOL>return original_function<EOL><DEDENT>return fn_decorator<EOL>
|
This is a decorator, that mark the specified function with a capability name. Later on, marked
functions will be processed by this metaclass and registered
:param cap_name: capability name with which the decorated function will be marked. It must be unique
within a single class
:return: decorating function
|
f9902:c0:m1
|
@verify_type(cap_name=str)<EOL><INDENT>def capability(self, cap_name):<DEDENT>
|
if cap_name in self.__class_capabilities__:<EOL><INDENT>function_name = self.__class_capabilities__[cap_name]<EOL>return getattr(self, function_name)<EOL><DEDENT>
|
Return capability by its name
:param cap_name: name of a capability to return
:return: bounded method or None (if a capability is not found)
|
f9902:c1:m0
|
@verify_type(cap_names=str)<EOL><INDENT>def has_capabilities(self, *cap_names):<DEDENT>
|
for name in cap_names:<EOL><INDENT>if name not in self.__class_capabilities__:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>return True<EOL>
|
Check if class has all of the specified capabilities
:param cap_names: capabilities names to check
:return: bool
|
f9902:c1:m1
|
def __call__(self, cap_name, *args, **kwargs):
|
fn = self.capability(cap_name)<EOL>if fn is None:<EOL><INDENT>raise WCapabilitiesHolder.UndefinedCapabilityCall(self, cap_name)<EOL><DEDENT>return fn(*args, **kwargs)<EOL>
|
Execute a capability with arguments and return its result
:param cap_name: name of a capability to execute
:param args: arguments for a capability function
:param kwargs: keyword arguments for a capability function
:return: anything that the specified capability may return
|
f9902:c1:m2
|
def size(self):
|
return len(self.__history)<EOL>
|
Returns history entries count
:return: int
|
f9905:c0:m1
|
@verify_type(pos=(int, None))<EOL><INDENT>@verify_value(pos=lambda x: x is None or x >= <NUM_LIT:0>)<EOL>def position(self, pos=None):<DEDENT>
|
if pos is not None:<EOL><INDENT>if pos >= len(self.__history):<EOL><INDENT>raise IndexError('<STR_LIT>')<EOL><DEDENT>self.__history_position = pos<EOL><DEDENT>return self.__history_position<EOL>
|
Get current and/or set history cursor position
:param pos: if value is not None, then current position is set to pos and new value is returned
:return: int or None (if position have never been changed)
|
f9905:c0:m2
|
@verify_type(value=str)<EOL><INDENT>def add(self, value):<DEDENT>
|
index = len(self.__history)<EOL>self.__history.append(value)<EOL>return index<EOL>
|
Add new record to history. Record will be added to the end
:param value: new record
:return: int record position in history
|
f9905:c0:m3
|
@verify_type(position=int)<EOL><INDENT>def entry(self, position):<DEDENT>
|
return self.__history[position]<EOL>
|
Get record from history by record position
:param position: record position
:return: str
|
f9905:c0:m4
|
@verify_type(value=str, position=(int, None))<EOL><INDENT>@verify_value(position=lambda x: x is None or x >= <NUM_LIT:0>)<EOL>def update(self, value, position):<DEDENT>
|
self.__history[position] = value<EOL>
|
Change record in this history
:param value: new record to save
:param position: record position to change
:return: None
|
f9905:c0:m5
|
def history(self):
|
return self.__editable_history<EOL>
|
Return changeable history
:return: WConsoleHistory or None
|
f9905:c1:m1
|
@verify_type(mode_value=(bool, None))<EOL><INDENT>def history_mode(self, mode_value=None):<DEDENT>
|
if mode_value is not None:<EOL><INDENT>self.__history_mode = mode_value<EOL><DEDENT>return self.__history_mode<EOL>
|
Get and/or set current history mode.
History mode defines what row will be changed with :meth:`.WConsoleProto.update_row` or can be got by
:meth:`.WConsoleProto.row` call. If history mode disabled, then :meth:`.WConsoleProto.update_row` and
:meth:`.WConsoleProto.row` affects current row prompt. If history mode is enabled, then
:meth:`.WConsoleProto.update_row` and :meth:`.WConsoleProto.row` affects current entry in
history :class:`.WConsoleHistory` (entry at :meth:`.WConsoleHistory.position`)
History mode is turned off by default.
:param mode_value: True value enables history mode. False - disables. None - do nothing
:return: bool
|
f9905:c1:m2
|
def start_session(self):
|
self.__current_row = '<STR_LIT>'<EOL>self.__history_mode = False<EOL>self.__editable_history = deepcopy(self.__history)<EOL>self.__prompt_show = True<EOL>self.refresh_window()<EOL>
|
Start new session and prepare environment for new row editing process
:return: None
|
f9905:c1:m3
|
def fin_session(self):
|
self.__prompt_show = False<EOL>self.__history.add(self.row())<EOL>self.exec(self.row())<EOL>
|
Finalize current session
:return: None
|
f9905:c1:m4
|
@verify_type(value=str)<EOL><INDENT>def update_row(self, value):<DEDENT>
|
if not self.__history_mode:<EOL><INDENT>self.__current_row = value<EOL><DEDENT>else:<EOL><INDENT>self.history().update(value, self.history().position())<EOL><DEDENT>
|
Change row
:param value: new row
:return: None
|
f9905:c1:m5
|
def row(self):
|
if not self.__history_mode:<EOL><INDENT>return self.__current_row<EOL><DEDENT>else:<EOL><INDENT>return self.history().entry(self.history().position())<EOL><DEDENT>
|
Get row
:return: str
|
f9905:c1:m6
|
def prompt_show(self):
|
return self.__prompt_show<EOL>
|
Return flag, that shows, whether to display prompt and current command at the window end, or not
:return: bool
|
f9905:c1:m7
|
@abstractmethod<EOL><INDENT>def prompt(self):<DEDENT>
|
raise NotImplementedError('<STR_LIT>')<EOL>
|
Return prompt, that would be printed before row. Prompt length must be the same
within every session
:return: str
|
f9905:c1:m8
|
@abstractmethod<EOL><INDENT>def refresh_window(self):<DEDENT>
|
raise NotImplementedError('<STR_LIT>')<EOL>
|
Refresh current screen. Simple clear and redraw should work
:return: None
|
f9905:c1:m9
|
@abstractmethod<EOL><INDENT>@verify_type(row=str)<EOL>def exec(self, row):<DEDENT>
|
raise NotImplementedError('<STR_LIT>')<EOL>
|
Must execute given command
:param row: command to execute
:return: None
|
f9905:c1:m10
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.