Skip to content

Commit

Permalink
bug fixes + replaced os with pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
vihdutta committed Dec 29, 2021
1 parent 553cb91 commit 3d3ec51
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 47 deletions.
19 changes: 11 additions & 8 deletions asfuncs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import shutil
from datetime import datetime
from pathlib import Path, PurePath
from random import randint

RICH_TEXT_FORE_NL = '<span style=\' font-family:"Segoe UI Light"; font-size:18pt; font-weight:400; color:#a89769;\' ><br>'
Expand All @@ -11,8 +11,8 @@
def filesnumrenamer(dstname, srcfiles, dstfiles, truepath, id_filelist):
filesidrenamer(dstname, srcfiles, dstfiles, truepath)

for i, file in enumerate(os.listdir(truepath)):
name, extension = os.path.splitext(file) #error #1
for file in Path(truepath).iterdir():
name, extension = PurePath(file).stem, PurePath(file).suffix #error #1
name = name.split('---', 1)[0]
id_filelist.append(name + '---' + extension)
fileamount = id_filelist.count(name + '---' + extension) - 1
Expand Down Expand Up @@ -45,11 +45,14 @@ def filesidrenamer(dstname, srcfiles, dstfiles, truepath):
def dstsort(dstname, truepath):
files = [f for f in os.listdir(truepath) if os.path.isfile(os.path.join(truepath, f))]
for file in files:
name, extension = os.path.splitext(file)
new_path = os.path.join(truepath, extension)
if not os.path.exists(new_path):
os.makedirs(new_path)
shutil.move(os.path.join(truepath, file), os.path.join(truepath, extension, name+extension))
name, extension = PurePath(file).stem, PurePath(file).suffix
new_dir = PurePath(truepath, extension).joinpath()
if not Path(new_dir).exists():
Path(new_dir).mkdir()
filename = name + extension
old_path = Path(truepath) / file
new_path = Path(truepath) / extension / filename
shutil.move(old_path, new_path)


def createid(srcfiles, dstfiles):
Expand Down
25 changes: 11 additions & 14 deletions autosort.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import shutil
from pathlib import Path, PurePath
from datetime import datetime
from asfuncs import (createid, dstsort, filesnumrenamer,
RICH_TEXT_FORE_NL, RICH_TEXT_FORE, PLAIN_TEXT_FORE, TEXTBACK)
Expand All @@ -16,11 +17,9 @@
def copyfromdirs(metadata, replace, fcmethod, sort, dstname):
all_srcfiles_len = 0

if fcmethod == 1:
if fcmethod == 'Single-folder creation':
folder_name = f'autosort {datetime.now().strftime("%H-%M-%S-%f")}'
os.mkdir(os.path.join(dstname, folder_name))
else:
pass
Path(PurePath(dstname, folder_name).joinpath()).mkdir()

for srcdir in srcdirs:
for root, dirs, files in os.walk(srcdir):
Expand All @@ -36,14 +35,12 @@ def copyfromdirs(metadata, replace, fcmethod, sort, dstname):
dstfiles.append(file)

for srcdir in srcdirs:
if fcmethod == 2:
if fcmethod == 'Multi-folder creation':
folder_name = f'autosort {datetime.now().strftime("%H-%M-%S-%f")}'
os.mkdir(os.path.join(dstname, folder_name))
else:
pass
Path(PurePath(dstname, folder_name).joinpath()).mkdir()

if 'folder_name' in locals():
truepath = os.path.join(dstname, folder_name)
truepath = PurePath(dstname, folder_name).joinpath()
else:
truepath = dstname

Expand All @@ -67,12 +64,12 @@ def copyfromdirs(metadata, replace, fcmethod, sort, dstname):
temp_all_srcfiles_len -= 1

if metadata:
shutil.copy2(os.path.join(root, file), os.path.join(truepath, file_ider))
shutil.copy2(PurePath(root, file).joinpath(), PurePath(truepath, file_ider).joinpath())
else:
shutil.copy(os.path.join(root, file), os.path.join(truepath, file_ider))
shutil.copy(PurePath(root, file).joinpath(), PurePath(truepath, file_ider).joinpath())
dstfiles.append(file_ider)

if fcmethod == 2:
if fcmethod == 'Multi-folder creation':
if not replace:
yield f'{RICH_TEXT_FORE_NL}Enumerating...{TEXTBACK}'
filesnumrenamer(dstname, srcfiles, dstfiles, truepath, id_filelist)
Expand All @@ -81,15 +78,15 @@ def copyfromdirs(metadata, replace, fcmethod, sort, dstname):
yield f'{RICH_TEXT_FORE}Finished{TEXTBACK}'
id_filelist.clear()

if fcmethod == 1:
if fcmethod == 'Single-folder creation':
if not replace:
yield f'{RICH_TEXT_FORE_NL}Enumerating...{TEXTBACK}'
filesnumrenamer(dstname, srcfiles, dstfiles, truepath, id_filelist)
if sort:
dstsort(dstname, truepath)
yield f'{RICH_TEXT_FORE}Finished{TEXTBACK}'

elif fcmethod == 0:
elif fcmethod == 'No folder creation':
if not replace:
yield f'{RICH_TEXT_FORE_NL}Enumerating...{TEXTBACK}'
filesnumrenamer(dstname, srcfiles, dstfiles, truepath, id_filelist)
Expand Down
39 changes: 21 additions & 18 deletions autosortgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QLabel
from autosort import copyfromdirs, srcdirs
from asfuncs import RICH_TEXT_FORE, TEXTBACK
from pathlib import Path
import qtfiles.qtresources.resources
import os
import threading
import time


class WorkerSignals(QtCore.QObject):
result = QtCore.pyqtSignal(object)
class Worker(QtCore.QRunnable):
def __init__(self, metadata, replace, fcmethod, sort, destdir):
def __init__(self, run_button, metadata, replace, fcmethod, sort, destdir):
super().__init__()
self.signals = WorkerSignals()
self.run_button = run_button
self.metadata = metadata
self.replace = replace
self.fcmethod = fcmethod
Expand All @@ -23,9 +21,11 @@ def __init__(self, metadata, replace, fcmethod, sort, destdir):

@QtCore.pyqtSlot()
def run(self):
self.run_button.setEnabled(False)
function = copyfromdirs(self.metadata, self.replace, self.fcmethod, self.sort, self.destdir)
for statement in function:
self.signals.result.emit(statement)
self.run_button.setEnabled(True)

class CustomQTextEdit(QtWidgets.QTextEdit):
clicked = pyqtSignal()
Expand Down Expand Up @@ -516,28 +516,28 @@ def setupUi(self, MainWindow):

def source_button_click(self):
sourcedir = QFileDialog.getExistingDirectory()
if os.path.isdir(sourcedir):
if Path(sourcedir).is_dir() and sourcedir != '':
srcdirs.append(sourcedir)
srcdirsstring = f'{RICH_TEXT_FORE}, {TEXTBACK}'.join(srcdirs)
self.sourcedisplay.setText(srcdirsstring)


def destination_button_click(self):
# bug: if user has selected destdir before then presses cancel on destdir file dialog.
self.destdir = QFileDialog.getExistingDirectory()
if os.path.isdir(self.destdir):
self.destinationdisplay.setText(self.destdir)
self.destinationdisplay.setText(self.destdir)


def run_button_click(self):
self.console.clear()
organization = self.organization_box.currentText()

if organization == 'Single-folder creation':
fcmethod = 1
fcmethod = 'Single-folder creation'
elif organization == 'Multi-folder creation':
fcmethod = 2
fcmethod = 'Multi-folder creation'
else:
fcmethod = 0
fcmethod = 'No folder creation'

replace = self.replace_button.isChecked()
metadata = not self.metadata_button.isChecked()
Expand All @@ -550,17 +550,17 @@ def run_button_click(self):
nosourceerror.setIcon(QMessageBox.Critical)
nosourceerror.exec_()
try:
if os.path.isdir(self.destdir) and srcdirs:
worker = Worker(metadata, replace, fcmethod, sort, self.destdir)
if Path(self.destdir).is_dir() and srcdirs:
worker = Worker(self.run_button, metadata, replace, fcmethod, sort, self.destdir)
worker.signals.result.connect(self.statement_returner)
self.threadpool.start(worker)
elif not os.path.isdir(self.destdir):
elif not Path(self.destdir).is_dir() or self.destdir == '':
nodestinationerror = QMessageBox()
nodestinationerror.setWindowTitle('Error')
nodestinationerror.setText('Destination directory field cannot be empty.')
nodestinationerror.setIcon(QMessageBox.Critical)
nodestinationerror.exec_()
except AttributeError: # if self.desdir hasn't been defined yet (user hasn't clicked the destination button)
except (AttributeError, TypeError): # if self.desdir hasn't been defined yet (user hasn't clicked the destination button)
nodestinationerror = QMessageBox()
nodestinationerror.setWindowTitle('Error')
nodestinationerror.setText('Destination directory field cannot be empty.')
Expand All @@ -571,13 +571,16 @@ def run_button_click(self):
def statement_returner(self, statement):
self.console.append(statement)


def sourcedisplay_clear(self):
self.sourcedisplay.clear()
srcdirs.clear()


def destinationdisplay_clear(self):
self.destinationdisplay.clear()
self.destdir = ''
self.destdir = False


def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
Expand Down Expand Up @@ -610,7 +613,7 @@ def retranslateUi(self, MainWindow):
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon(":/icons/newlogor.ico"))
app.setWindowIcon(QtGui.QIcon(":/icons/logo.ico"))
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
Expand Down
4 changes: 2 additions & 2 deletions autosortgui.spec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ block_cipher = None
a = Analysis(['autosortgui.py'],
pathex=['C:\\Users\\Duttas\\Desktop\\autosort'],
binaries=[],
datas=[('newlogor.ico', '.')],
datas=[('logo.ico', '.')],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
Expand All @@ -30,4 +30,4 @@ exe = EXE(pyz,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False , icon='newlogor.ico')
console=False , icon='logo.ico')
Binary file modified logo files/logov2.psd
Binary file not shown.
Binary file added logo files/logov2banner.PSD
Binary file not shown.
File renamed without changes.
8 changes: 4 additions & 4 deletions qtfiles/qtresources/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,10 @@
\x00\x6f\xa6\x53\
\x00\x69\
\x00\x63\x00\x6f\x00\x6e\x00\x73\
\x00\x0c\
\x04\xfc\x89\x3f\
\x00\x6e\
\x00\x65\x00\x77\x00\x6c\x00\x6f\x00\x67\x00\x6f\x00\x72\x00\x2e\x00\x69\x00\x63\x00\x6f\
\x00\x08\
\x05\xe2\x41\xff\
\x00\x6c\
\x00\x6f\x00\x67\x00\x6f\x00\x2e\x00\x69\x00\x63\x00\x6f\
"

qt_resource_struct_v1 = b"\
Expand Down
2 changes: 1 addition & 1 deletion qtfiles/qtresources/resources.qrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE RCC>
<RCC version="1.0">
<qresource prefix="icons">
<file alias="newlogor.ico">newlogor.ico</file>
<file alias="logo.ico">logo.ico</file>
</qresource>
</RCC>

0 comments on commit 3d3ec51

Please sign in to comment.