mirror of
https://github.com/aykhans/AzSuicideDataVisualization.git
synced 2025-07-04 23:32:37 +00:00
first commit
This commit is contained in:
@ -0,0 +1,74 @@
|
||||
from win32com.shell import shell, shellcon
|
||||
import win32api
|
||||
import os
|
||||
|
||||
|
||||
def testSHFileOperation(file_cnt):
|
||||
temp_dir = os.environ["temp"]
|
||||
orig_fnames = [
|
||||
win32api.GetTempFileName(temp_dir, "sfo")[0] for x in range(file_cnt)
|
||||
]
|
||||
new_fnames = [
|
||||
os.path.join(temp_dir, "copy of " + os.path.split(orig_fnames[x])[1])
|
||||
for x in range(file_cnt)
|
||||
]
|
||||
|
||||
pFrom = "\0".join(orig_fnames)
|
||||
pTo = "\0".join(new_fnames)
|
||||
|
||||
shell.SHFileOperation(
|
||||
(
|
||||
0,
|
||||
shellcon.FO_MOVE,
|
||||
pFrom,
|
||||
pTo,
|
||||
shellcon.FOF_MULTIDESTFILES | shellcon.FOF_NOCONFIRMATION,
|
||||
)
|
||||
)
|
||||
for fname in orig_fnames:
|
||||
assert not os.path.isfile(fname)
|
||||
|
||||
for fname in new_fnames:
|
||||
assert os.path.isfile(fname)
|
||||
shell.SHFileOperation(
|
||||
(
|
||||
0,
|
||||
shellcon.FO_DELETE,
|
||||
fname,
|
||||
None,
|
||||
shellcon.FOF_NOCONFIRMATION | shellcon.FOF_NOERRORUI,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def testSHNAMEMAPPINGS(file_cnt):
|
||||
## attemps to move a set of files to names that already exist, and generated filenames should be returned
|
||||
## as a sequence of 2-tuples created from SHNAMEMAPPINGS handle
|
||||
temp_dir = os.environ["temp"]
|
||||
orig_fnames = [
|
||||
win32api.GetTempFileName(temp_dir, "sfo")[0] for x in range(file_cnt)
|
||||
]
|
||||
new_fnames = [win32api.GetTempFileName(temp_dir, "sfo")[0] for x in range(file_cnt)]
|
||||
pFrom = "\0".join(orig_fnames)
|
||||
pTo = "\0".join(new_fnames)
|
||||
rc, banyaborted, NameMappings = shell.SHFileOperation(
|
||||
(
|
||||
0,
|
||||
shellcon.FO_MOVE,
|
||||
pFrom,
|
||||
pTo,
|
||||
shellcon.FOF_MULTIDESTFILES
|
||||
| shellcon.FOF_NOCONFIRMATION
|
||||
| shellcon.FOF_RENAMEONCOLLISION
|
||||
| shellcon.FOF_WANTMAPPINGHANDLE,
|
||||
)
|
||||
)
|
||||
|
||||
for old_fname, new_fname in NameMappings:
|
||||
print("Old:", old_fname, "New:", new_fname)
|
||||
assert len(NameMappings) == file_cnt
|
||||
|
||||
|
||||
testSHFileOperation(10)
|
||||
testSHFileOperation(1)
|
||||
testSHNAMEMAPPINGS(5)
|
@ -0,0 +1,21 @@
|
||||
from win32com.shell import shell
|
||||
from win32com.shell.shellcon import *
|
||||
|
||||
sf = shell.SHGetDesktopFolder()
|
||||
print("Shell Folder is", sf)
|
||||
|
||||
names = []
|
||||
for i in sf: # Magically calls EnumObjects
|
||||
name = sf.GetDisplayNameOf(i, SHGDN_NORMAL)
|
||||
names.append(name)
|
||||
|
||||
# And get the enumerator manually
|
||||
enum = sf.EnumObjects(0, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN)
|
||||
num = 0
|
||||
for i in enum:
|
||||
num += 1
|
||||
if num != len(names):
|
||||
print("Should have got the same number of names!?")
|
||||
print("Found", len(names), "items on the desktop")
|
||||
for name in names:
|
||||
print(name)
|
@ -0,0 +1,68 @@
|
||||
# Test IShellItem and related interfaces
|
||||
from win32com.shell import shell, shellcon, knownfolders
|
||||
import unittest
|
||||
|
||||
|
||||
class TestShellItem(unittest.TestCase):
|
||||
def assertShellItemsEqual(self, i1, i2):
|
||||
n1 = i1.GetDisplayName(shellcon.SHGDN_FORPARSING)
|
||||
n2 = i2.GetDisplayName(shellcon.SHGDN_FORPARSING)
|
||||
self.assertEqual(n1, n2)
|
||||
|
||||
def test_idlist_roundtrip(self):
|
||||
pidl = shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP)
|
||||
item = shell.SHCreateItemFromIDList(pidl, shell.IID_IShellItem)
|
||||
pidl_back = shell.SHGetIDListFromObject(item)
|
||||
self.assertEqual(pidl, pidl_back)
|
||||
|
||||
def test_parsing_name(self):
|
||||
sf = shell.SHGetDesktopFolder()
|
||||
flags = shellcon.SHCONTF_FOLDERS | shellcon.SHCONTF_NONFOLDERS
|
||||
children = sf.EnumObjects(0, flags)
|
||||
child_pidl = next(children)
|
||||
name = sf.GetDisplayNameOf(child_pidl, shellcon.SHGDN_FORPARSING)
|
||||
|
||||
item = shell.SHCreateItemFromParsingName(name, None, shell.IID_IShellItem)
|
||||
# test the name we get from the item is the same as from the folder.
|
||||
self.assertEqual(name, item.GetDisplayName(shellcon.SHGDN_FORPARSING))
|
||||
|
||||
def test_parsing_relative(self):
|
||||
desktop_pidl = shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP)
|
||||
desktop_item = shell.SHCreateItemFromIDList(desktop_pidl, shell.IID_IShellItem)
|
||||
|
||||
sf = shell.SHGetDesktopFolder()
|
||||
flags = shellcon.SHCONTF_FOLDERS | shellcon.SHCONTF_NONFOLDERS
|
||||
children = sf.EnumObjects(0, flags)
|
||||
child_pidl = next(children)
|
||||
name_flags = shellcon.SHGDN_FORPARSING | shellcon.SHGDN_INFOLDER
|
||||
name = sf.GetDisplayNameOf(child_pidl, name_flags)
|
||||
|
||||
item = shell.SHCreateItemFromRelativeName(
|
||||
desktop_item, name, None, shell.IID_IShellItem
|
||||
)
|
||||
# test the name we get from the item is the same as from the folder.
|
||||
self.assertEqual(name, item.GetDisplayName(name_flags))
|
||||
|
||||
def test_create_in_known_folder(self):
|
||||
item = shell.SHCreateItemInKnownFolder(
|
||||
knownfolders.FOLDERID_Desktop, 0, None, shell.IID_IShellItem
|
||||
)
|
||||
# this will do for now :)
|
||||
|
||||
def test_create_item_with_parent(self):
|
||||
desktop_pidl = shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP)
|
||||
desktop_item = shell.SHCreateItemFromIDList(desktop_pidl, shell.IID_IShellItem)
|
||||
|
||||
sf = shell.SHGetDesktopFolder()
|
||||
flags = shellcon.SHCONTF_FOLDERS | shellcon.SHCONTF_NONFOLDERS
|
||||
children = sf.EnumObjects(0, flags)
|
||||
child_pidl = next(children)
|
||||
item1 = shell.SHCreateItemWithParent(
|
||||
desktop_pidl, None, child_pidl, shell.IID_IShellItem
|
||||
)
|
||||
item2 = shell.SHCreateItemWithParent(None, sf, child_pidl, shell.IID_IShellItem)
|
||||
self.assertShellItemsEqual(item1, item2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
Reference in New Issue
Block a user