Using the constants module

Create constants set for file attributes

File attributes are represented by a bitmask set of constants, all starting with FILE_ATTRIBUTES. Many of these are defined in the win32file module. Some were added more recently and have be added specifically.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import win32file
from winsys import constants

#
# Pull all file attributes known to win32file
#
FILE_ATTRIBUTE = constants.Constants.from_pattern (u"FILE_ATTRIBUTE_*", namespace=win32file)
FILE_ATTRIBUTE.dump ()

#
# Add extra file attributes added since win32file
#
extras = dict (
  SPARSE_FILE = 0x00000200,
  REPARSE_POINT = 0x00000400 
)
FILE_ATTRIBUTE.update (extras)
FILE_ATTRIBUTE.dump ()

Discussion

The most common way of initialising a Constants structure is by pulling in all constants following a naming pattern from one of the pywin32 modules. In this case, this leaves some useful constants undefined so we add them in by creating a dictionary with their values, and then passing that to the original object’s Constants.update() function which accepts any dictionary-like object.

Like all winsys objects, the constants objects have a core._WinSysObject.dump() method which provides a readable display of its values.

Show file access flags

File access is a wide bitmask of flags in each ACE in the filesystem DACL. It represents a set of access flags, named in the fs.FILE_ACCESS constants. Look at each ACE in turn and indicate the trustee and the set of operations permtted.

1
2
3
4
5
import sys
from winsys import fs, security

for dace in security.security (sys.executable).dacl:
  print dace.trustee, fs.FILE_ACCESS.names_from_value (dace.access)

Discussion

The Constants.names_from_value() method returns the list of constant names corresponding to the single value by comparing their bitmasks. No check is made to see whether any bits are left “unclaimed” nor whether any flags overlap partly or wholly (ie are synonyms).

Table Of Contents

Previous topic

Using the asyncio module

Next topic

Using the dialogs module

This Page