Helpers

Problematic

How can we avoid writing the same thing every time?

Documentation

The tool to check the availability or syntax of domains, IPv4 or URL.

██████╗ ██╗   ██╗███████╗██╗   ██╗███╗   ██╗ ██████╗███████╗██████╗ ██╗     ███████╗
██╔══██╗╚██╗ ██╔╝██╔════╝██║   ██║████╗  ██║██╔════╝██╔════╝██╔══██╗██║     ██╔════╝
██████╔╝ ╚████╔╝ █████╗  ██║   ██║██╔██╗ ██║██║     █████╗  ██████╔╝██║     █████╗
██╔═══╝   ╚██╔╝  ██╔══╝  ██║   ██║██║╚██╗██║██║     ██╔══╝  ██╔══██╗██║     ██╔══╝
██║        ██║   ██║     ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
╚═╝        ╚═╝   ╚═╝      ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝

This submodule will provide the helpers.

Author:
Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom
Special thanks:
https://pyfunceble.github.io/special-thanks.html
Contributors:
https://pyfunceble.github.io/contributors.html
Project link:
https://github.com/funilrys/PyFunceble
Project documentation:
https://pyfunceble.readthedocs.io/en/master/
Project homepage:
https://pyfunceble.github.io/

License:

MIT License

Copyright (c) 2017, 2018, 2019 Nissar Chababy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
class PyFunceble.helpers.Command(command, encoding='utf-8')[source]

Shell command execution.

Parameters:
  • command (str) – The command to execute.
  • encoding (str) – The encoding to use to decode the shell output.
_decode_output(to_decode)[source]

Decode the output of a shell command in order to be readable.

Parameters:to_decode (bytes) – Output of a command to decode.
Returns:The decoded output.
Return type:str
execute()[source]

Execute the given command.

Returns:The output of the command.
Return type:str
run()[source]

Run the given command and yield each line(s) one by one.

Note

The difference between this method and execute() is that execute() wait for the process to end in order to return its output while this method return each line one by one - as they are outputed.

class PyFunceble.helpers.Dict(main_dictionnary=None)[source]

Dictionary manipulations.

Parameters:main_dictionnary (dict) – The dict we are working with.
classmethod from_json(data)[source]

Convert a JSON formatted string into a dictionary.

Parameters:data (str) – A JSON formatted string to convert to dict format.
Returns:The dict representation of the JSON formatted string.
Return type:dict
classmethod from_yaml(data)[source]

Convert a YAML formatted string into a dictionary.

Parameters:data (str) – A YAML formatted string to convert to dict format.
Returns:The dict representation of the YAML formatted string.
Return type:dict
merge(to_merge, strict=True)[source]

Merge the content of to_merge into the main dictionnary.

Parameters:
  • to_merge (dict) – The dictionnary to merge.
  • strict (bool) –

    Tell us if we have to strictly merge lists.

    • True: We follow index.
    • False: We follow element (content/value)
Returns:

The merged dict.

Return type:

dict

remove_key(key_to_remove)[source]

Remove a given key from a given dictionary.

Parameters:key_to_remove (list|str) – The key(s) to delete.
Returns:The dict without the given key(s).
Return type:dict|None
rename_key(key_to_rename, strict=True)[source]

Rename the given keys from the given dictionary.

Parameters:
  • key_to_rename (dict) –

    The key(s) to rename.

    Expected format: {old:new}

  • strict (bool) – Tell us if we have to rename the exact index or the index which looks like the given key(s)
Returns:

The well formatted dict.

Return type:

dict|None

to_json(destination)[source]

Save a dictionnary into a JSON file.

Parameters:destination (str) – A path to a file where we’re going to write the converted dict into a JSON format.
to_yaml(destination, flow_style=False)[source]

Save a dictionnary into a YAML file.

Parameters:destination (str) – A path to a file where we’re going to write the converted dict into a JSON format.
class PyFunceble.helpers.Directory(directory)[source]

Directory manipulation.

Parameters:directory (str) – A path to the directory to manipulate.
fix_path(splited_path=None)[source]

Fix the path of the given path.

Note

We consider a path as fixed if it ends with the right directory separator.

Parameters:splited_path (list) – A list to convert to the right path.
Returns:The fixed path.
Return type:str
class PyFunceble.helpers.Download(link, destination=None, return_data=False, verify_certificate=True)[source]

Download or return the content of the given link.

Parameters:
  • link (str) – The link to download.
  • destination (str) –

    The location where we should save the downloaded content.

    Note

    If None is given, we return the downloaded document.

  • return_data (bool) – Tell us if we need to return the page content or write its content into the given destination.
  • verify_certificate (bool) – Tell us if we need to verify the SSL/TLS certificate.
text()[source]

Download the given link and return or save its requests.text at the given destination.

Return type:

mixed

Raises:
Exception

If the status code is not 200.

class PyFunceble.helpers.File(file)[source]

File treatment/manipulations.

Parameters:file (str) – A path to the file to manipulate.
copy(destination)[source]

Copy the given file to the given destination.

Parameters:destination (str) – The destination of the copy.
delete()[source]

Delete the given file path.

read()[source]

Read a given file path and return its content.

Returns:The content of the given file path.
Return type:str
write(data_to_write, overwrite=False)[source]

Write or append data into the given file path.

Parameters:
  • data_to_write (str) – The data to write.
  • overwrite (bool) – Tell us if we have to overwrite the content of the file we are working with.
class PyFunceble.helpers.Hash(file_path=None, algorithm='sha512', only_hash=False, data=None)[source]

Get and return the hash a file with the given algorithm.

Parameters:
  • file_path (str) – The path to the file we have to hash.
  • algorithm (str) – The algorithm to use.
  • only_hash (bool) – Tell us if we only have to return the desired algorithm instead of the dummy dict format.

Note

The original version can be found at https://git.io/vFQrK.

_hash_data(algo)[source]

Get hash of the given data.

Parameters:algo (str) – The algorithm to use.
_hash_file(algo)[source]

Get the hash of the given file

Parameters:algo (str) – The algorithm to use.
Returns:The hexdigest of the data.
Return type:str
get()[source]

Return the hash of the given file

class PyFunceble.helpers.List(main_list=None)[source]

List manipulation.

Parameters:main_list (list) – The list to manipulate.
custom_format(key_method, reverse=False)[source]

Return a well formatted list. With the key_method as a function/method to format the elements before sorting.

Parameters:
  • key_method (function|method) – A function or method to use to format the readed element before sorting.
  • reverse (bool) – Tell us if we have to reverse the list.
Returns:

A sorted list.

Return type:

list

format()[source]

Return a well formatted list. Basicaly, it’s sort a list and remove duplicate.

Returns:A sorted, without duplicate, list.
Return type:list
merge(to_merge, strict=True)[source]

Merge to_merge into the given main list.

Parameters:
  • to_merge (list) – The list to merge.
  • strict (bool) – Tell us if we have to respect index (True) or not (False).
Returns:

The merged list.

Return type:

list

class PyFunceble.helpers.Regex(data, regex, **args)[source]

A simple implementation ot the python.re package

Parameters:
  • data (str) – The data to check.
  • regex (str) – The regex to match.
  • group (int) – The group to return.
  • rematch (bool) –

    Allow to return the matched groups into a formatted list.

    Note

    This is an implementation of Bash ${BASH_REMATCH}

  • replace_with (str) – The value to replace the matched regex with.
  • occurences (int) – The number of occurence(s) to replace.
  • return_type – Tell us if we have to return the matched data or simply check if we matched (True) or not (False)
match()[source]

Used to get exploitable result of re.search

Returns:The data of the match status.
Return type:mixed
matching_list()[source]

Return a list of the string which match the given regex.

not_matching_list()[source]

Return a list of string which don’t match the given regex.

replace()[source]

Used to replace a matched string with another.

Returns:The data after replacement.
Return type:str

Download()

class PyFunceble.helpers.Download(link, destination=None, return_data=False, verify_certificate=True)[source]

Download or return the content of the given link.

Parameters:
  • link (str) – The link to download.
  • destination (str) –

    The location where we should save the downloaded content.

    Note

    If None is given, we return the downloaded document.

  • return_data (bool) – Tell us if we need to return the page content or write its content into the given destination.
  • verify_certificate (bool) – Tell us if we need to verify the SSL/TLS certificate.
text()[source]

Download the given link and return or save its requests.text at the given destination.

Return type:

mixed

Raises:
Exception

If the status code is not 200.

Command()

class PyFunceble.helpers.Command(command, encoding='utf-8')[source]

Shell command execution.

Parameters:
  • command (str) – The command to execute.
  • encoding (str) – The encoding to use to decode the shell output.
_decode_output(to_decode)[source]

Decode the output of a shell command in order to be readable.

Parameters:to_decode (bytes) – Output of a command to decode.
Returns:The decoded output.
Return type:str
execute()[source]

Execute the given command.

Returns:The output of the command.
Return type:str
run()[source]

Run the given command and yield each line(s) one by one.

Note

The difference between this method and execute() is that execute() wait for the process to end in order to return its output while this method return each line one by one - as they are outputed.

Regex()

class PyFunceble.helpers.Regex(data, regex, **args)[source]

A simple implementation ot the python.re package

Parameters:
  • data (str) – The data to check.
  • regex (str) – The regex to match.
  • group (int) – The group to return.
  • rematch (bool) –

    Allow to return the matched groups into a formatted list.

    Note

    This is an implementation of Bash ${BASH_REMATCH}

  • replace_with (str) – The value to replace the matched regex with.
  • occurences (int) – The number of occurence(s) to replace.
  • return_type – Tell us if we have to return the matched data or simply check if we matched (True) or not (False)
match()[source]

Used to get exploitable result of re.search

Returns:The data of the match status.
Return type:mixed
matching_list()[source]

Return a list of the string which match the given regex.

not_matching_list()[source]

Return a list of string which don’t match the given regex.

replace()[source]

Used to replace a matched string with another.

Returns:The data after replacement.
Return type:str

Dict()

class PyFunceble.helpers.Dict(main_dictionnary=None)[source]

Dictionary manipulations.

Parameters:main_dictionnary (dict) – The dict we are working with.
classmethod from_json(data)[source]

Convert a JSON formatted string into a dictionary.

Parameters:data (str) – A JSON formatted string to convert to dict format.
Returns:The dict representation of the JSON formatted string.
Return type:dict
classmethod from_yaml(data)[source]

Convert a YAML formatted string into a dictionary.

Parameters:data (str) – A YAML formatted string to convert to dict format.
Returns:The dict representation of the YAML formatted string.
Return type:dict
merge(to_merge, strict=True)[source]

Merge the content of to_merge into the main dictionnary.

Parameters:
  • to_merge (dict) – The dictionnary to merge.
  • strict (bool) –

    Tell us if we have to strictly merge lists.

    • True: We follow index.
    • False: We follow element (content/value)
Returns:

The merged dict.

Return type:

dict

remove_key(key_to_remove)[source]

Remove a given key from a given dictionary.

Parameters:key_to_remove (list|str) – The key(s) to delete.
Returns:The dict without the given key(s).
Return type:dict|None
rename_key(key_to_rename, strict=True)[source]

Rename the given keys from the given dictionary.

Parameters:
  • key_to_rename (dict) –

    The key(s) to rename.

    Expected format: {old:new}

  • strict (bool) – Tell us if we have to rename the exact index or the index which looks like the given key(s)
Returns:

The well formatted dict.

Return type:

dict|None

to_json(destination)[source]

Save a dictionnary into a JSON file.

Parameters:destination (str) – A path to a file where we’re going to write the converted dict into a JSON format.
to_yaml(destination, flow_style=False)[source]

Save a dictionnary into a YAML file.

Parameters:destination (str) – A path to a file where we’re going to write the converted dict into a JSON format.

List()

class PyFunceble.helpers.List(main_list=None)[source]

List manipulation.

Parameters:main_list (list) – The list to manipulate.
custom_format(key_method, reverse=False)[source]

Return a well formatted list. With the key_method as a function/method to format the elements before sorting.

Parameters:
  • key_method (function|method) – A function or method to use to format the readed element before sorting.
  • reverse (bool) – Tell us if we have to reverse the list.
Returns:

A sorted list.

Return type:

list

format()[source]

Return a well formatted list. Basicaly, it’s sort a list and remove duplicate.

Returns:A sorted, without duplicate, list.
Return type:list
merge(to_merge, strict=True)[source]

Merge to_merge into the given main list.

Parameters:
  • to_merge (list) – The list to merge.
  • strict (bool) – Tell us if we have to respect index (True) or not (False).
Returns:

The merged list.

Return type:

list

Directory()

class PyFunceble.helpers.Directory(directory)[source]

Directory manipulation.

Parameters:directory (str) – A path to the directory to manipulate.
fix_path(splited_path=None)[source]

Fix the path of the given path.

Note

We consider a path as fixed if it ends with the right directory separator.

Parameters:splited_path (list) – A list to convert to the right path.
Returns:The fixed path.
Return type:str

File()

class PyFunceble.helpers.File(file)[source]

File treatment/manipulations.

Parameters:file (str) – A path to the file to manipulate.
copy(destination)[source]

Copy the given file to the given destination.

Parameters:destination (str) – The destination of the copy.
delete()[source]

Delete the given file path.

read()[source]

Read a given file path and return its content.

Returns:The content of the given file path.
Return type:str
write(data_to_write, overwrite=False)[source]

Write or append data into the given file path.

Parameters:
  • data_to_write (str) – The data to write.
  • overwrite (bool) – Tell us if we have to overwrite the content of the file we are working with.

Hash()

class PyFunceble.helpers.Hash(file_path=None, algorithm='sha512', only_hash=False, data=None)[source]

Get and return the hash a file with the given algorithm.

Parameters:
  • file_path (str) – The path to the file we have to hash.
  • algorithm (str) – The algorithm to use.
  • only_hash (bool) – Tell us if we only have to return the desired algorithm instead of the dummy dict format.

Note

The original version can be found at https://git.io/vFQrK.

_hash_data(algo)[source]

Get hash of the given data.

Parameters:algo (str) – The algorithm to use.
_hash_file(algo)[source]

Get the hash of the given file

Parameters:algo (str) – The algorithm to use.
Returns:The hexdigest of the data.
Return type:str
get()[source]

Return the hash of the given file