def _get_structure(self): """ This method return the structure we are goinng to work with. """ structure_file = "" req = "" if path.isfile(self.structure): structure_file = self.structure elif path.isfile(self.base + "dir_structure_production.json"): structure_file = self.base + "dir_structure_production.json" else: if "dev" not in PyFunceble.VERSION: req = requests.get(PyFunceble.LINKS["dir_structure"]) else: req = requests.get(PyFunceble.LINKS["dir_structure"].replace( "master", "dev")) if structure_file.endswith("_production.json"): structure = Dict().from_json(File(structure_file).read()) return self._update_structure_from_config(structure) elif structure_file.endswith(".json"): return Dict().from_json(File(structure_file).read()) return self._update_structure_from_config(Dict().from_json(req.text))
def restore(self): """ Restore the 'output/' directory structure based on the `dir_structure.json` file. """ structure = self._get_structure() list_of_key = list(structure.keys()) structure = structure[list_of_key[0]] parent_path = list_of_key[0] + directory_separator for directory in structure: base = self.base + parent_path + directory + directory_separator self._create_directory(base) for file in structure[directory]: file_path = base + file content_to_write = structure[directory][file]["content"] online_sha = structure[directory][file]["sha512"] content_to_write = Regex(content_to_write, "@@@", escape=True, replace_with="\\n").replace() git_to_keep = file_path.replace("gitignore", "keep") keep_to_git = file_path.replace("keep", "gitignore") if self._restore_replace(): if path.isfile(file_path) and Hash( file_path, "sha512", True).get() == online_sha: rename(file_path, git_to_keep) write = False else: File(file_path).delete() file_path = git_to_keep write = True else: if path.isfile(keep_to_git) and Hash( file_path, "sha512", True).get() == online_sha: rename(file_path, keep_to_git) write = False else: File(keep_to_git).delete() file_path = keep_to_git write = True if write: File(file_path).write(content_to_write + "\n", True)
def before_header(self): """ Print informations about PyFunceble and the date of generation of a file into a given path, if doesn't exist. """ if not PyFunceble.CONFIGURATION[ "no_files"] and self.output and not path.isfile(self.output): link = ("# File generated with %s\n" % PyFunceble.LINKS["repo"]) date_of_generation = ("# Date of generation: %s \n\n" % PyFunceble.CURRENT_TIME) if self.template in [ "Generic_File", PyFunceble.STATUS["official"]["up"], PyFunceble.STATUS["official"]["down"], PyFunceble.STATUS["official"]["invalid"], "Less", ]: header = self._header_constructor(self.currently_used_header, None)[0] + "\n" try: File(self.output).write(link + date_of_generation + header) except UnboundLocalError: File(self.output).write(link + date_of_generation)
def _retrieve(self): """ Return the current content of the inactive-db.json file. """ if path.isfile(self.inactive_db_path): PyFunceble.CONFIGURATION["inactive_db"] = Dict().from_json( File(self.inactive_db_path).read()) else: PyFunceble.CONFIGURATION["inactive_db"] = {} return
def __init__(self): if PyFunceble.CONFIGURATION["auto_continue"]: self.autocontinue_log_file = PyFunceble.CURRENT_DIRECTORY + PyFunceble.OUTPUTS[ "parent_directory"] + PyFunceble.OUTPUTS["logs"]["filenames"][ "auto_continue"] if path.isfile(self.autocontinue_log_file): self.backup_content = Dict().from_json( File(self.autocontinue_log_file).read()) else: self.backup_content = {} File(self.autocontinue_log_file).write(str( self.backup_content))
def write(self, data_to_write, overwrite=False): """ Write or append data into the given file path. Argument: - data_to_write: str The data to write. """ if data_to_write and isinstance(data_to_write, str): if overwrite or not path.isfile(self.file): with open(self.file, "w", encoding="utf-8") as file: file.write(data_to_write) else: with open(self.file, "a", encoding="utf-8") as file: file.write(data_to_write)
def install_iana_config(cls): """ This method download `iana-domains-db.json` if not present. """ iana_link = "https://raw.githubusercontent.com/funilrys/PyFunceble/master/iana-domains-db.json" # pylint: disable=line-too-long destination = PyFunceble.CURRENT_DIRECTORY + "iana-domains-db.json" if "dev" in PyFunceble.VERSION: iana_link = iana_link.replace("master", "dev") else: iana_link = iana_link.replace("dev", "master") if not path.isfile(destination): return Download(iana_link, destination).text() return True
def write(self, data_to_write, overwrite=False): """ Write or append data into the given file path. :param data_to_write: The data to write. :type data_to_write: str :param overwrite: Tell us if we have to overwrite the content of the file we are working with. :type overwrite: bool """ if overwrite or not path.isfile(self.file): # * We have to overwrite the file data. # or # * The file path does not already exist. with open(self.file, "w", encoding="utf-8") as file: # We prepare the file for writting. if data_to_write and isinstance(data_to_write, str): # * A data to write is given. # and # * The data to write is a string # We write the string into the file. file.write(data_to_write) else: # * We do not have to overwrite the file data. # or # * The file path does already exist. with open(self.file, "a", encoding="utf-8") as file: # We prepare the file for append writting. if data_to_write and isinstance(data_to_write, str): # * A data to write is given. # and # * The data to write is a string # We append the string into the file. file.write(data_to_write)
def _extract_domain_from_file(cls): """ This method extract all non commented lines. Returns: list Each line of the file == an element of the list. """ result = [] if path.isfile(PyFunceble.CONFIGURATION["file_to_test"]): with open(PyFunceble.CONFIGURATION["file_to_test"]) as file: for line in file: if not line.startswith("#"): result.append(line.rstrip("\n").strip()) else: raise FileNotFoundError(PyFunceble.CONFIGURATION["file_to_test"]) return result
def get(self): """ Return the hash of the given file """ result = {} if path.isfile(self.path) and self.algorithm in self.valid_algorithms: if self.algorithm == "all": del self.valid_algorithms[0] for algo in self.valid_algorithms: result[algo] = None result[algo] = self.hash_data(algo) else: result[self.algorithm] = None result[self.algorithm] = self.hash_data(self.algorithm) else: return None if self.algorithm != "all" and self.only_hash: return result[self.algorithm] return result
def get(self): """ Return the hash of the given file """ # We initiate a variable which will save the result we are going # to return. result = {} if self.algorithm in self.valid_algorithms: # * The parsed path exist. # and # * The parsed algorithm is in the list of valid algorithms. if self.algorithm == "all": # The parsed algorithm is `all`. # We remove `all` (the first element of the list) from # the list of valid algorithms because we are going to # loop through the list of valid algorithms. del self.valid_algorithms[0] for algo in self.valid_algorithms: # We loop through the list of valid algorithms. if self.path and path.isfile(self.path): # The file path exist. # We save the hash into the result variable. result[algo] = self._hash_file(algo) elif self.data: # * The path does not exist. # and # * The given data is not empty. # We save the hash into the result variable. result[algo] = self._hash_data(algo) else: # pragma: no cover # All other case are met. # We return None. return None else: # The parsed algorithm is a specific one. if self.path and path.isfile(self.path): # The file path exist. # We save the hash into the result variable. result[self.algorithm] = self._hash_file(self.algorithm) elif self.data: # * The path does not exist. # and # * The given data is not empty. # We save the hash into the result variable. result[self.algorithm] = self._hash_data(self.algorithm) else: # All the other case are met. # We return None. return None else: # pragma: no cover # The parsed algorithm is not in the list of valid algorithms. return None if self.algorithm != "all" and self.only_hash: # * The parsed algorithm is not equal to `all`. # and # * We only have to return the selected hash. # We return the selected algorithm. return result[self.algorithm] # * The parsed algorithm is equal to `all`. # or # * We do not have to return the selected hash. # We return all hashes. return result