deleting garbage

cc
This commit is contained in:
Benjamín 2021-03-05 19:35:12 +01:00
parent cd4e179394
commit a4d947e082
6 changed files with 0 additions and 363 deletions

@ -1,285 +0,0 @@
import os
import copy
import json
# todo !!!! currently don't use . in directory names !!!
# todo also dont use name 'directory__'
class Directory:
# "c" is used in loaded files as content variable containing the loaded file
def __init__(self):
pass
def __str__(self):
return "directory__"
def append_data(self, name, data, auto_convert=True): # usually data about file
if name != "": # ignoring .smt files
if auto_convert:
if type(data) != type({}):
data = {
"directory__": False,
"c": data
}
else:
if "directory__" not in data.keys():
data["directory__"] = False
self.__setattr__(name, data)
def append_directory(self, name, path=None, load_local=False):
new_directory = Directory()
if load_local:
result = new_directory.load_local(path)
if result == "not_directory":
data = {
"path": path,
"filename": path.split("\\")[-1],
"filetype": "unknown",
"directory__": False
}
self.__setattr__(data["filename"], data)
self.__setattr__(name, new_directory)
def load(self, data_):
for data_key in data_.keys():
if data_key != "directory__":
if self.check_for_json_content(data_[data_key]):
data_[data_key]["c"] = self.unpack_dictionary(data_[data_key]["c"])
if hasattr(self, data_key):
obj = getattr(self, data_key)
if str(obj) == "directory__":
obj.load(data_[data_key])
else:
self.__setattr__(data_key, data_[data_key])
else:
if data_[data_key]["directory__"] is True:
new_dir = Directory()
self.__setattr__(data_key, new_dir)
new_dir.load(data_[data_key])
else:
self.__setattr__(data_key, data_[data_key])
def load_local(self, path=""):
current_directory = os.getcwd()
if path != "":
try:
os.chdir(path)
except:
return "not_directory"
new_directory = os.getcwd()
for file in os.listdir():
if len(file) == len(file.split(".")[0]):
self.append_directory(file, f"{new_directory}\\{file}", True)
else:
data = {
"path": f"{new_directory}\\{file}",
"filename": "".join(file.split(".")[:-1]),
"filetype": file.split(".")[-1],
"directory__": False
}
if data["filetype"] == "txt":
try:
with open(file, "r") as f:
data["c"] = f.read().split("\n")
except:
pass
elif data["filetype"] == "json":
try:
with open(file, "r") as f:
data["c"] = json.load(f)
data["c"] = self.unpack_dictionary(data["c"])
except:
pass
# making sure we don't overwrite something
if data["filename"] in os.listdir():
data["filename"] += "_"
while hasattr(self, data["filename"]):
data["filename"] += "_"
self.append_data(data["filename"], data)
os.chdir(current_directory)
return "successful"
def get_dictionary(self, root=True):
dictionary = {}
if root is False:
dictionary["directory__"] = True
for attribute in vars(self):
obj = getattr(self, attribute)
if str(obj) == "directory__":
dictionary[attribute] = obj.get_dictionary(False)
else:
dictionary[attribute] = copy.copy(obj)
if self.check_for_json_content(obj):
dictionary[attribute]["c"] = self.pack_to_dictionary(obj["c"])
return dictionary
def unpack_dictionary(self, dict_):
root = Directory()
for key in dict_.keys():
if type(dict_[key]) == type({}):
obj = self.unpack_dictionary(dict_[key])
root.__setattr__(key, obj)
else:
root.append_data(key, dict_[key], auto_convert=False)
return root
def pack_to_dictionary(self, obj):
dict_ = {}
for attribute in vars(obj):
if str(getattr(obj, attribute)) == "directory__":
dict_[attribute] = self.pack_to_dictionary(getattr(obj, attribute))
else:
dict_[attribute] = getattr(obj, attribute)
return dict_
def search(self, name, directories=True, filetype="any", exact=True):
# this func only searches for files
# for unpacked json use search_iuj as search in unpacked json
occurrences = []
for attribute in vars(self):
search = True
is_directory = False
obj = getattr(self, attribute)
if str(obj) == "directory__":
is_directory = True
if directories is False or (filetype != "any" and filetype != "directory"):
search = False
occurrences += obj.search(name, directories, filetype, exact)
else:
if filetype != "any":
if type(obj) == type({}):
if "filetype" in obj.keys():
if obj["filetype"] != filetype:
search = False
if search:
if exact:
if name == attribute.strip("_"):
if str(obj) == "directory__":
obj.name__ = attribute
occurrences.append(obj)
elif type(obj) == type({}):
if obj.keys() != ["directory__", "c"]:
occurrences.append(obj)
else:
occurrences.append({attribute: obj["c"]})
else:
occurrences.append({attribute: obj})
else:
if name in attribute:
if str(obj) == "directory__":
obj.name__ = attribute
occurrences.append(obj)
elif type(obj) == type({}):
if obj.keys() != ["directory__", "c"]:
occurrences.append(obj)
else:
occurrences.append({attribute: obj["c"]})
else:
occurrences.append({attribute: obj})
return occurrences
def search_iuj(self, name, exact=True, in_content=False):
occurrences = []
if in_content is False:
for attribute in vars(self):
obj = getattr(self, attribute)
if str(obj) == "directory__":
occurrences += obj.search_iuj(name, exact=exact)
else:
if self.check_for_json_content(obj):
occurrences += obj["c"].search_iuj(name, exact=exact, in_content=True)
else:
for attribute in vars(self):
obj = getattr(self, attribute)
if str(obj) == "directory__":
occurrences += obj.search_iuj(name, exact=exact, in_content=True)
if exact:
if name == attribute:
if str(obj) == "directory__":
obj.name__ = attribute
occurrences.append(obj)
else:
occurrences.append({attribute: obj})
else:
if name in attribute:
if str(obj) == "directory__":
obj.name__ = attribute
occurrences.append(obj)
else:
occurrences.append({attribute: obj})
return occurrences
@staticmethod
def check_for_json_content(data):
if type(data) == type({}):
if "filetype" in data.keys():
if data["filetype"] == "json":
if "c" in data.keys():
return True
return False
def save_files(self): # saves txt and json files
for attribute in vars(self):
obj = getattr(self, attribute)
if str(obj) == "directory__":
obj.save_files()
else:
if obj["filetype"] == "txt":
with open(obj["path"], "w") as f:
f.write("".join(obj["c"]))
elif obj["filetype"] == "json":
with open(obj["path"], "w") as f:
json.dump(self.pack_to_dictionary(obj["c"]), f, indent=4)

@ -1 +0,0 @@
for testing

@ -1,15 +0,0 @@
{
"hello": "chello",
"x": {
"aa": 55,
"bb": 44,
"cc": {
"oo": 11
}
},
"list": [
1,
2,
3
]
}

@ -1,11 +0,0 @@
{
"D_backround": "files/bg2.pmg",
"D_menu": "Pova\u017esk\u00fd hrad",
"D_text": "info o hrade!",
"prehliadka": [
"pdf",
"files/prehliadky/prehliadka1.pdf",
"show"
],
"hello": "chello"
}

@ -1,20 +0,0 @@
{
"D_backround": "files/bg3.pmg",
"D_menu": "Zauj\u00edmavosti Pova\u017esk\u00e9ho hradu",
"D_text": "Tu dole si m\u00f4\u017ete pozrie\u0165 zauj\u00edmavosti",
"historia": [
"txt",
"files/txt/txt1.txt",
"menu"
],
"novinky": [
"txt",
"files/txt/txt2.txt",
"download"
],
"aktu\u00e1lny hrad": [
"mp4",
"files/video/video1.mp4",
"show"
]
}

@ -1,31 +0,0 @@
from Python.MyLibraries.Directory.directory import Directory
filesystem = Directory()
filesystem.load_local() # loads local directory and unpacks jsons
filesystem.files.welcome["c"].D_text = "info o hrade!" # changes attribute D_text in welcome
# "c" as content
filesystem.save_files() # saves all changes to txt, json files
filesystem_dictionary = filesystem.get_dictionary() # saves as dictionary
"""send(filesystem_dictionary)""" # pseudocode
filesystem_2 = Directory()
filesystem_2.load(filesystem_dictionary) # loads from dictionary
welcome = filesystem.search("welcome") # searches for welcome
zaujimavosti = filesystem.search("mavosti", exact=False) # searches for names that include mavosti
json_files = filesystem.search("", filetype="json", exact=False) # searches for filetype json
folders = filesystem.search("", filetype="directory", exact=False) # searches for directories/folders
files = filesystem.search("files", directories=False) # search doesnt include directories
test = filesystem.search("test", filetype="json")
test_dict = test[0]["c"].get_dictionary()
oo = filesystem.search_iuj("oo")
attributes = test[0]["c"].search_iuj("o", exact=False, in_content=True)
print("run in debugger please") # use breakpoints, for less confusion try keeping urself in main.py