Profile¶
This module implements the superclass for all profiles and contains globals
and the @commands
-Decorator.
-
class
profile.
Profile
(options=None, directory=None, parent=None)¶ Bases:
object
This class provides the "API" aka commands for creating links and stores the state of the set options and directory.
-
__old_builtins
¶ A backup of the overwritten builtins
Type: dict
-
name
¶ Identifier/Class name of the profile
Type: str
-
executed
¶ True, if a result was already generated
Type: bool
-
options
¶ Stores options that will be set with
opt()
as well as the tags. Equalsconstants.DEFAULTS
if this a root profile.Type: dict
-
directory
¶ The directory that the profile is using as current working directory. Equals
constants.DIR_DEFAULT
if this a root profile.Type: str
-
subprofiles
¶ A list of all subprofiles
Type: list
-
result
¶ The result of
generate()
. Contains name, parent, generated links and the result of all subprofiles.Type: dict
-
_Profile__create_link_descriptor
(target, directory='', **kwargs)¶ Creates an entry in
self.result["links"]
with current options and a given target.Furthermore lets you set the directory like
cd()
.Parameters: - target (str) -- Full path to target file
- directory (str) -- A path to change the cwd
- kwargs (dict) -- A set of options that will be overwritten just for this call
Raises: GenerationError
-- One or more options were misused
-
_Profile__generate_scripts
()¶ Generates event scripts from attributes. Stores them as shell scripts if they changed.
- Raises
GenerationError
: One of the attributes isn't a- string nor a function that returned a string
-
_Profile__reset_builtins
()¶ Restores old Builtin mappings.
-
_Profile__set_builtins
()¶ Maps functions from
custom_builtins
to builtins, so commands don't need to be called usingself
everytime.
-
__init__
(options=None, directory=None, parent=None)¶ Constructor.
Uses
constants.DEFAULTS
as default forself.options
ifoptions
isNone
.Uses
constants.DIR_DEFAULT
as default forself.directory
ifdirectory
isNone
.
-
__setattr__
(name, value)¶ Setter for
self.directory
.Makes sure that
self.directory
is always expanded and noramlized.
-
_gen_err
(msg)¶ A wrapper to raise a
GenerationError
with the profilename.
-
_make_read_opt
(kwargs)¶ Creates a function that looks up options but prefers options of kwargs.
Parameters: kwargs (dict) -- kwargs of a command Returns: function -- A function that looks up and returns the value for a key in kwargs. If the key is not in kwargs it uses self.options
for look up.
-
afterInstall
= None¶ This field can be implemented/set by the user. This has to be a string or a function that return a string. The string will be stored as shell script (only if it doesn't equal the last script stored) and will be executed after the profile gets installed for the first time.
-
afterUninstall
= None¶ This field can be implemented/set by the user. This has to be a string or a function that return a string. The string will be stored as shell script (only if it doesn't equal the last script stored) and will be executed after the profile gets uninstalled.
-
afterUpdate
= None¶ This field can be implemented/set by the user. This has to be a string or a function that return a string. The string will be stored as shell script (only if it doesn't equal the last script stored) and will be executed after the profile gets updated and only if the profile was already installed.
-
beforeInstall
= None¶ This field can be implemented/set by the user. This has to be a string or a function that return a string. The string will be stored as shell script (only if it doesn't equal the last script stored) and will be executed before the profile gets installed for the first time.
-
beforeUninstall
= None¶ This field can be implemented/set by the user. This has to be a string or a function that return a string. The string will be stored as shell script (only if it doesn't equal the last script stored) and will be executed before the profile gets uninstalled.
-
beforeUpdate
= None¶ This field can be implemented/set by the user. This has to be a string or a function that return a string. The string will be stored as shell script (only if it doesn't equal the last script stored) and will be executed before the profile gets updated and only if the profile was already installed.
-
cd
(directory)¶ Sets
self.directory
. Unix-like cd.Note
This function is a command. It can be called without the use of
self
withingenerate()
.The
directory
can be an absolute or relative path and it expands environment variables.Parameters: directory (str) -- The path to switch to
-
decrypt
(target)¶ Creates an
EncryptedFile
instance from a target, updates and returns it.Note
This function is a command. It can be called without the use of
self
withingenerate()
.The target can be either just the name of a file that will be searched for or it can be another dynamic file that already provides a generated file.
Parameters: target (str/ DynamicFile
) -- The target file that will be used as source of theEncryptedFile
Returns: EncryptedFile
-- The dynamic file that holds the decrypted target
-
default
(*options)¶ Resets
self.options
back toconstants.DEFAULTS
. If called without arguments, it resets all options and tags.Note
This function is a command. It can be called without the use of
self
withingenerate()
.Parameters: *options (list) -- A list of options that will be reset
-
extlink
(path, **kwargs)¶ Link any file specified by its absolute path.
Note
This function is a command. It can be called without the use of
self
withingenerate()
.Parameters: - path (str) -- The path of the target
- **kwargs (dict) -- A set of options that will be overwritten just for this call
-
find
(target)¶ Find a dotfile in
TARGET_FILES
. Depends on the current set tags.Note
This function is a command. It can be called without the use of
self
withingenerate()
.This can be overwritten to change the searching behaviour of a profile. Furthermore it can be used by the user to just find a dotfile without linking it directly.
Parameters: target (str) -- A filename, without preceding tag Raises: GenerationError
-- More than one file was foundReturns: str -- The full path of the file or None
if no file was found
-
generate
()¶ Implemeted by users for actual link configuration.
Warning
Do NOT call this function without its wrapper
generator()
.
-
generator
()¶ This is the wrapper for
generate()
. It overwrites the builtins and maps it own commands to them.generate()
must not be called without this wrapper.Warning
Do NOT call this from within the same profile, only from outside!!
Returns: dict -- The result dictionary self.result
-
has_tag
(tag)¶ Returns true if a tag is set.
Note
This function is a command. It can be called without the use of
self
withingenerate()
.Parameters: tag (str) -- A tag that will be checked for Returns: bool -- True, if tag is set
-
link
(*targets, **kwargs)¶ Link one ore more targets with current options.
Note
This function is a command. It can be called without the use of
self
withingenerate()
.Parameters: - *targets (list) -- One ore more targets that shall be linked. Targets can be just file names or any dynamic files.
- **kwargs (dict) -- A set of options that will be overwritten just for this call
Raises: GenerationError
-- One of the targets were not found
-
links
(target_pattern, encrypted=False, match_path=False, **kwargs)¶ Calls
link()
for all targets matching a pattern.Note
This function is a command. It can be called without the use of
self
withingenerate()
.Furthermore it allows to ommit the
replace_pattern
in favor of thetarget_pattern
and to decrypt matched files first.Parameters: - target_pattern (str) -- The regular expression that matches the file names
- encrypted (bool) -- True, if the targets shall be decrypted
- **kwargs (dict) -- A set of options that will be overwritten just for this call
Raises: GenerationError
-- No files or multiple file with the same name were found with this pattern
-
merge
(name, targets)¶ Creates a
SplittedFile
instance from a list of targets, updates and returns it.Note
This function is a command. It can be called without the use of
self
withingenerate()
.The target can be either just the name of a file that will be searched for or it can be another dynamic file that already provides a generated file.
Parameters: targets (list) -- The list of targets that will be used as source of the SplittedFile
Returns: SplittedFile
-- The dynamic file that holds the merged target
-
opt
(**kwargs)¶ Sets options permanently. The set options will be used in all future calls of commands and subprofiles as long as the commands don't override them temporarily.
Note
This function is a command. It can be called without the use of
self
withingenerate()
.Parameters: **kwargs -- A set of options that will be set permanently Raises: GenerationError
-- One of to be set option does not exist
-
pipe
(target, shell_command)¶ Creates a
FilteredFile
instance from a target, updates and returns it.Note
This function is a command. It can be called without the use of
self
withingenerate()
.Parameters: - target (str/
DynamicFile
) -- The target file that will be used as source of theFilteredFile
- shell_command (str) -- The shell command that the content of target will be piped into
Returns: FilteredFile
-- The dynamic file that holds the output of the shell command- target (str/
-
prepare_script
= None¶ This field can be set by the user. This has to be a string and will be prepended to the event scripts of this profile or any of its subprofiles.
Removes a list of tags.
Note
This function is a command. It can be called without the use of
self
withingenerate()
.Parameters: tags (list) -- A list of tags that will be removed
-
subprof
(*profilenames, **kwargs)¶ Executes a list of profiles by name.
Note
This function is a command. It can be called without the use of
self
withingenerate()
.Parameters: - *profilenames (list) -- A list of profilenames that will be executed
- **kwargs (dict) -- A set of options that will be overwritten just for this call
Raises: GenerationError
-- Profile were executed in a cycly or recursively
Adds a list of tags.
Note
This function is a command. It can be called without the use of
self
withingenerate()
.Parameters: tags (list) -- A list of tags that will be added
-
-
profile.
command
(func)¶ Adding this decorator to a function will make it available in
generate()
as a builtin.Furthermore this adds some documentation to the function.
Parameters: func (function) -- The function that will become a command
-
profile.
custom_builtins
= ['find', 'decrypt', 'merge', 'pipe', 'link', 'extlink', 'links', 'cd', 'default', 'rmtags', 'tags', 'has_tag', 'opt', 'subprof']¶ A list of custom builtins that the profiles will map its functions to before executing
generate()
.Don't add functions here, just apply the
@command
decorator to functions that shall become a custom builtin.