Skip to content

Commit

Permalink
working on _folders mainly
Browse files Browse the repository at this point in the history
  • Loading branch information
quintijn committed Dec 20, 2022
1 parent 3289c04 commit 97ffbcf
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 29 deletions.
68 changes: 42 additions & 26 deletions src/unimacro/UnimacroGrammars/_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@
doRecentFolderCommand = True
# some child windows have to behave as top window (specified in ini file):
# note: title is converted to lowercase, only full title is recognised
try:
thisGrammar
except NameError:
thisGrammar = None

ancestor = natbj.IniGrammar
class ThisGrammar(ancestor):
Expand Down Expand Up @@ -164,6 +168,8 @@ def initialize(self):
self.catchRemember = ""
self.activeFolder = None
self.prevDisplayRecentFolders = None # displaying recent folders list
self.subfoldersDict = {}
self.foldersSet = set()
if not self.language:
print("no valid language in grammar "+__name__+" grammar not initialized")
return
Expand Down Expand Up @@ -222,6 +228,7 @@ def handleTrackFilesAndFolders(self, activeFolder):
"""
if self.activeFolder == activeFolder:
return

if self.activeFolder:
self.emptyListsForActiveFolder()
# print 'empty lists for active folder %s, now: %s'% (self.activeFolder, activeFolder)
Expand All @@ -230,7 +237,9 @@ def handleTrackFilesAndFolders(self, activeFolder):
if activeFolder and os.path.isdir(activeFolder):
self.fillListsForActiveFolder(activeFolder)
print('set %s (sub)files and %s subfolders'% (len(self.subfilesDict), len(self.subfoldersDict)))

self.activeFolder = activeFolder
return
print(f'_folders, handleTrackFilesAndFolders, invalid activeFolder: {activeFolder}')

def fillList(self, listName):
"""fill a list in the grammar from the data of the inifile
Expand Down Expand Up @@ -463,7 +472,11 @@ def fillGrammarLists(self, listOfLists=None, ignoreFromIni='general',
ancestor.fillGrammarLists(self)

## this one is ignored in the` parent class version of this function
self.fillList('recentfolders')
## when recentfolders is not needed in the grammar, it raises a ValueError
try:
self.fillList('recentfolders')
except ValueError:
pass

def resolveVirtualDrives(self, wantedVirtualDrives):
"""check the virtual drives, possibly recursive
Expand Down Expand Up @@ -736,11 +749,14 @@ def catchTimerRecentFolders(self, hndle=None, className=None):
When the buffer grows too large, the first inserted items are removed from the list
(QH, March 2020)
"""
# print('_folders, catchTimerRecentFolders')
activeFolder = self.getActiveFolder(hndle, className)
if not activeFolder:
return
if activeFolder != self.activeFolder:
print(f'get new folder {activeFolder}')
if activeFolder == self.activeFolder:
return
self.activeFolder = activeFolder
print(f'get new folders for {activeFolder}')
# activeFolder = os.path.normcase(activeFolder)
if self.recentfoldersDict and activeFolder == list(self.recentfoldersDict.values())[-1]:
return
Expand Down Expand Up @@ -803,6 +819,7 @@ def startRecentFolders(self):
def stopRecentFolders(self):
self.doTrackFoldersHistory = True
natlinktimer.setTimerCallback(self.catchTimerRecentFolders, 0)
self.dumpRecentFoldersDict()
self.recentfoldersDict = {}
self.emptyList('recentfolders')
print("track folders history is stopped, the timer callback is off")
Expand Down Expand Up @@ -2137,7 +2154,7 @@ def gotoFolder(self, f):
print('_folders, gotoFolder: no window handle found, return')
# Iam2x = prog == '2xexplorer'
IamExplorer = prog == 'explorer'
browser = prog in ['iexplore', 'firefox','opera', 'netscp', 'brave']
_browser = prog in ['iexplore', 'firefox','opera', 'netscp', 'brave']
## print 'iambrowser:', browser
## print 'xx: %s, Iam2x: %s, IamExplorer: %s'% (xx, Iam2x, IamExplorer)
##
Expand Down Expand Up @@ -2293,8 +2310,7 @@ def getValidDirectory(self, f, remote):
(if E: is a valid backup drive)
"""
fdrive, fdir = os.path.splitdrive(f)
remdrive, rempart = os.path.splitdrive(remote)
_fdrive, fdir = os.path.splitdrive(f)
fparts = [part for part in fdir.split(os.sep) if part]
while fparts:
fpart = os.sep.join(fparts)
Expand All @@ -2305,8 +2321,7 @@ def getValidDirectory(self, f, remote):
print('_folders, no valid remote folder found for %s and remote: %s'% (f, remote))

def getValidFile(self, f, remote):
fdrive, fdir = os.path.splitdrive(f)
remdrive, rempart = os.path.splitdrive(remote)
_fdrive, fdir = os.path.splitdrive(f)
fparts = [part for part in fdir.split(os.sep) if part]
while fparts:
fpart = os.sep.join(fparts)
Expand Down Expand Up @@ -2695,29 +2710,30 @@ def dumpToPickle(data, picklePath):
def collection_iter(collection):
for index in range(collection.Count):
yield collection.Item(index)
# standard stuff Joel (adapted for possible empty gramSpec, QH, unimacro)
thisGrammar = ThisGrammar()
if thisGrammar.gramSpec:
thisGrammar.initialize()
else:
thisGrammar = None


# standard stuff Joel (adapted for different calling methods, including tests QH, unimacro)
def unload():
global thisGrammar, dialogGrammar
#pylint:disable=W0603
global thisGrammar
# print("function unload in _folders.py")
if thisGrammar:
# print("unloading folders grammar")
natlinktimer.setTimerCallback(None, 0)
# make recentfoldersDict persistf across
try:
thisGrammar.dumpRecentFoldersDict()
except:
pass
thisGrammar.stopRecentFolders() # stopping the timer callback
thisGrammar.unload()
print("unloaded folders grammar")
time.sleep(5)

thisGrammar = None
thisGrammar = None

if __name__ == "__main__":
print(get_selected_files())
## interactive use, for debugging:
natlink.natConnect()
try:
thisGrammar = ThisGrammar()
thisGrammar.startInifile(modName = '_folders')
thisGrammar.initialize()
finally:
natlink.natDisconnect()
elif __name__.find('.') == -1:
# standard startup when Dragon starts:
thisGrammar = ThisGrammar()
thisGrammar.initialize()
1 change: 1 addition & 0 deletions src/unimacro/_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ def gotResults_edit(self,words,fullResults):
filepath = __file__
else:
unimacrogrammarsdir = status.getUnimacroGrammarsDirectory()
print(f'_control, unimacrogrammarsdir: {unimacrogrammarsdir}, module: {moduleName}')
filepath = os.path.join(unimacrogrammarsdir, moduleName + '.py')
if not os.path.isfile(filepath):
print(f'_control: cannot find grammar file for "{gramName}",\n\t{filepath} does not exist')
Expand Down
4 changes: 2 additions & 2 deletions src/unimacro/natlinkutilsbj.py
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ def correctIniFile(self, translateWords, newTranslateWords, obsoleteTranslateWor
self.ini.set(notTransSection, 'info1', 'De volgende grammatica woorden kunnen worden vertaald.')
else:
self.ini.set(notTransSection, 'info1', 'These grammar words can be translated.')
self.ini.set(notTransSection, 'info2', 'See http://qh.antenna.nl/unimacro/features/translations for more info')
self.ini.set(notTransSection, 'info2', 'See https://qh.antenna.nl/unimacro/features/translations for more info')
self.ini.write()
else:
if self.ini.get(notTransSection):
Expand Down Expand Up @@ -2650,7 +2650,7 @@ def removeFromList(self, L, toRemove):
if not L:
if not toRemove:
return
self.error('removedFromList, list is empty, but toRemove is not empty: %s'% toRemove)
print(f'{self.name}, removedFromList: list is empty, but toRemove is not empty: {toRemove}')
return
if not toRemove:
return
Expand Down
33 changes: 33 additions & 0 deletions tests/test_grammar_folders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pylint:disable=E1101
from pathlib import Path
import pytest
import natlink
from natlinkcore import natlinkstatus
status = natlinkstatus.NatlinkStatus

thisDir = Path(__file__).parent

def mock_unimacro_user_dir(tmp_dir, _self):

mock_folder= tmp_dir / "mock_unimacro_userdir"
print(f"Mock unimacro folder {mock_folder} in {__file__}") #just for understanding remove eventually
if not mock_folder.is_dir():
mock_folder.mkdir()
return str(mock_folder)

def test_fill_folders_list():
"""see if (with debugging) the list folders (foldersDict) is filled
"""
# monkeypatch.setattr(status, "getUnimacroUserDirectory", mock_unimacro_user_dir)

natlink.natConnect()
try:
from unimacro.UnimacroGrammars._folders import ThisGrammar
thisGrammar = ThisGrammar()
thisGrammar.startInifile() #modName = '_folders')
thisGrammar.initialize()
finally:
natlink.natDisconnect()

if __name__ == "__main__":
pytest.main(['test_grammar_folders.py'])
2 changes: 1 addition & 1 deletion tests/test_grammar_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ def test_gototask(monkeypatch):
natlink.natDisconnect()

if __name__ == "__main__":
pytest.main()
pytest.main(['test_grammar_tasks.py'])

0 comments on commit 97ffbcf

Please sign in to comment.