145 lines
5.8 KiB
Python
145 lines
5.8 KiB
Python
|
from random import randint
|
|||
|
import pickle
|
|||
|
import shutil
|
|||
|
import sys
|
|||
|
import os
|
|||
|
import shelve
|
|||
|
import glob
|
|||
|
|
|||
|
class FileManager():
|
|||
|
|
|||
|
def __init__(self):
|
|||
|
self.root = os.path.abspath(os.path.expanduser("~"))
|
|||
|
self.catalogues = ["/lab5/", "/lab6/", "/lab7/", "/lab7/Shved/"]
|
|||
|
|
|||
|
def make_path(self, path):
|
|||
|
return os.path.abspath(self.root + path)
|
|||
|
|
|||
|
def make_directory(self, path):
|
|||
|
target_directory = self.make_path(path)
|
|||
|
if not os.path.exists(target_directory):
|
|||
|
print(f"Створення каталогу {path} у {self.root}")
|
|||
|
os.mkdir(target_directory)
|
|||
|
else:
|
|||
|
print(f"{path} вже існує.")
|
|||
|
|
|||
|
def copy(self, source, destination):
|
|||
|
shutil.copy(source, self.make_path(destination))
|
|||
|
|
|||
|
def halve_text(self, source, destination1, destination2):
|
|||
|
import re
|
|||
|
|
|||
|
with open(self.make_path(source), "r", encoding = "WINDOWS-1251") as source:
|
|||
|
raw_text = source.read()
|
|||
|
|
|||
|
stringifyed_text = raw_text.replace("\n", " ")
|
|||
|
|
|||
|
subbed_text = re.sub(r"\s+", r" ", stringifyed_text)
|
|||
|
subbed_text = re.sub(r"([.,\?\!\":])\s?(\-?\s?\"?[А-ЯA-Z])", r"\1|\2", subbed_text)
|
|||
|
subbed_text = re.sub(r"([A-ZА-Я])\s+(\-?\s?\"?[А-ЯA-Z])", r"\1|\2", subbed_text)
|
|||
|
subbed_text = re.sub(r"([A-ZА-Я])\s+(\-?\s?\"?[А-ЯA-Z])", r"\1|\2", subbed_text)
|
|||
|
|
|||
|
sentences = subbed_text.split("|")
|
|||
|
|
|||
|
sentences_length = len(sentences)
|
|||
|
sentences_halved = len(sentences) // 2
|
|||
|
|
|||
|
with open(self.make_path(destination1), "w", encoding = "WINDOWS-1251") as d1, open(self.make_path(destination2), "w", encoding = "WINDOWS-1251") as d2:
|
|||
|
d1.write("\n".join(sentences[:sentences_halved]))
|
|||
|
d2.write("\n".join(sentences[sentences_halved:]))
|
|||
|
|
|||
|
self.sentences = sentences
|
|||
|
|
|||
|
def symbol_count(self, path):
|
|||
|
return os.path.getsize(self.make_path(path))
|
|||
|
|
|||
|
def odd(self, destination):
|
|||
|
with open(self.make_path(destination), "w", encoding = "WINDOWS-1251") as f:
|
|||
|
f.write("\n".join([self.sentences[i] for i in range(0, len(self.sentences), 2)]))
|
|||
|
|
|||
|
def picklify(self, destination):
|
|||
|
import Lab_5
|
|||
|
with open(self.make_path(destination), "wb") as f:
|
|||
|
pickle.dump([Lab_5.surnames, Lab_5.competitors, Lab_5.winners], f)
|
|||
|
del Lab_5
|
|||
|
|
|||
|
def modify_pickles(self, source, destination):
|
|||
|
with open(self.make_path(source), "rb") as f:
|
|||
|
lab_pickle = pickle.loads(f.read())
|
|||
|
|
|||
|
for i in range(10):
|
|||
|
lab_pickle[1][randint(0, len(lab_pickle[1])-1)] = str(i*randint(0, 5))
|
|||
|
|
|||
|
with open(self.make_path(destination), "wb") as f:
|
|||
|
pickle.dump(lab_pickle, f)
|
|||
|
|
|||
|
def put_on_shelve(self, destination):
|
|||
|
import Lab_6
|
|||
|
with shelve.open(self.make_path(destination)) as db:
|
|||
|
db["months"] = Lab_6.year
|
|||
|
del Lab_6
|
|||
|
|
|||
|
def modify_shelve(self, path):
|
|||
|
with open(path, "rb+") as f:
|
|||
|
f_length = len(f.read())
|
|||
|
f.truncate(round(f_length / 2))
|
|||
|
|
|||
|
os.chmod(path, 0o700)
|
|||
|
|
|||
|
with open(path, "ab") as f:
|
|||
|
for i in range(5):
|
|||
|
f.seek(randint(0, f_length), 0)
|
|||
|
f.write("Багато зайвих даних".encode("UTF-8"))
|
|||
|
|
|||
|
def do_the_lab(self):
|
|||
|
print("Створюємо папки.")
|
|||
|
for path in self.catalogues:
|
|||
|
self.make_directory(path)
|
|||
|
|
|||
|
print("Копіюємо файл у папку з лабораторною роботою.")
|
|||
|
self.copy("2.txt", "/lab7/Shved/2.txt")
|
|||
|
|
|||
|
print("Розділяємо файл за реченнями.")
|
|||
|
self.halve_text("/lab7/Shved/2.txt", "/lab7/Shved/2part1.txt", "/lab7/Shved/2part2.txt")
|
|||
|
|
|||
|
sc1 = self.symbol_count("/lab7/Shved/2.txt")
|
|||
|
sc2 = self.symbol_count("/lab7/Shved/2part1.txt")
|
|||
|
sc3 = self.symbol_count("/lab7/Shved/2part2.txt")
|
|||
|
|
|||
|
print(f"Кількість символів у початкаовому файлі: {sc1}\n"
|
|||
|
f"Кількість символів у першій частині: {sc2}\n"
|
|||
|
f"Кількість символів у другій частині: {sc3}")
|
|||
|
|
|||
|
print("Створимо файл з непарними реченнями.")
|
|||
|
self.odd("/lab7/Shved/odd.txt")
|
|||
|
|
|||
|
print("Серіалізуемо та зберігаємо об'єкти 5-ї лабораторної роботи.")
|
|||
|
self.picklify("/lab7/Shved/Lab_5.pickle")
|
|||
|
|
|||
|
print("Переміщуємо файл у необхідну папку.")
|
|||
|
shutil.move(self.make_path("/lab7/Shved/Lab_5.pickle"), self.make_path("/lab5/Lab_5.pickle"))
|
|||
|
|
|||
|
print("Доповнюємо файл і переіменовуємо його.")
|
|||
|
self.modify_pickles("/lab5/Lab_5.pickle", "/lab5/Lab_5-extended.pickle")
|
|||
|
|
|||
|
print("Зберігаємо об'єкти 6-ї лабораторної роботи.")
|
|||
|
self.put_on_shelve("/lab7/Shved/Lab_6-db")
|
|||
|
|
|||
|
print("Переміщуємо файл бази даних у необхідну папку.")
|
|||
|
for file_to_move in glob.glob(self.make_path("/lab7/Shved/Lab_6-db*"), recursive = True):
|
|||
|
if not os.path.exists(self.make_path("/lab6/" + os.path.basename(file_to_move))):
|
|||
|
shutil.move(file_to_move, self.make_path("/lab6/"))
|
|||
|
else:
|
|||
|
print(f"{os.path.basename(file_to_move)} вже існує у {self.make_path('/lab6/')}")
|
|||
|
os.remove(file_to_move)
|
|||
|
|
|||
|
print("Модифікуємо файли.")
|
|||
|
for file_to_alter in glob.glob(self.make_path("/lab6/Lab_6-db*"), recursive = True):
|
|||
|
self.modify_shelve(file_to_alter)
|
|||
|
print("Файли бази данних змінено.")
|
|||
|
|
|||
|
print("Завдання виконане.")
|
|||
|
|
|||
|
o = FileManager()
|
|||
|
o.do_the_lab()
|