Package schrodinger :: Package utils :: Module fileutils
[hide private]
[frames] | no frames]

Module fileutils

A module of file utilities to deal with common file issues.

NOTE: This module is used in scripts that need to be able to run without a Schrodinger license, and therefore can't depend on the pymmlibs.

The force_remove and force_rename functions deal with the fact that os.remove() and os.rename() don't work on Windows if write permissions are not enabled.

Classes [hide private]
  chdir
A context manager that carries out commands inside of the specified directory and restores the current directory when done.
Functions [hide private]
 
force_remove(filename)
Remove the file 'filename' in a platform independent way without an exception, regardless of presence of the file or the lack of write permission.
 
force_rmtree(dirname, ignore_errors=False)
Remove the directory 'dirname', using force_remove to remove any difficult to remove files or sub-directories.
 
force_rename(old, new)
Rename a file, even if a file at the new name exists, and even if that file doesn't have write permission, and even if old and new are on different devices.
a tuple containing two strings
splitext(p)
Split the extension from a pathname.
 
get_structure_file_format(filename)
Return the format of a structure file, based on the filename extension.
bool
is_maestro_file(filename)
Returns True if specified filename represents a Maestro file.
bool
is_sd_file(filename)
Returns True if specified filename represents a SD file.
bool
is_csv_file(filename)
Returns True if specified filename represents a CSV file.
bool
is_smiles_file(filename)
Returns True if specified filename represents a Smiles file.
bool
is_poseviewer_file(filename)
Returns True if specified filename represents a Maestro PoseViewer file.
 
get_basename(filename)
Returns the final component of specified path name minus the extension.
 
is_valid_jobname(jobname)
Returns True if specified job name is valid, does not contain any illegal characters, and does not start with ".".
 
get_jobname(filename)
Returns a job name derived from the specified filename.
 
get_next_filename_prefix(path, midfix, zfill_width=0)
Return next filename prefix in series <root><midfix><number>.
 
get_next_filename(path, midfix, zfill_width=0)
Return next filename in series <root><midfix><number>.<ext>.
 
remote_mmshare_dir()
 
remote_scripts_dir()
str
get_directory_path(which_directory)
This function returns the schrodinger specific directory.
 
get_directory(which_directory)
str
get_home_dir()
Return the current user's home directory.
str
get_appdata_dir()
Return the directory for storing schrodinger application data that can be shared.
str
get_local_appdata_dir()
Return the directory for storing schrodinger application data that is local to the current machine.
str or None
get_desktop_dir()
Return the user's desktop directory.
str
get_mydocuments_dir()
Return the directory where user documents are stored by default.
str (Windows) or None (Linux and MacOSX)
get_mynetworkplaces_dir()
Linux: None MacOSX: None Windows: CSIDL_NETHOOD
str
get_userdata_dir()
Return the directory for storing user data files, like config files.
str
get_schrodinger_temp_dir()
Return the directory for creating temporary job directories and other temporary files.
str
get_startup_dir()
Return the default startup directory for Schrodinger applications that aren't started from the commandline.
str
get_csidl_dir(csidl_name)
Returns: The absolute pathname for the requested directory.
str or None
_check_for_pymol_by_platform(path)
Check for platform-specific executable file or script in the directory path provided.
str
_locate_pymol_in_registry(root_key, keypath, valuename=None)
Find out pymol installation path under given keypath and keyname.
str or None
locate_pymol()
Find the executable or script we use to launch PyMOL.
list
get_pymol_cmd(use_x11=False)
Get a cmd list for launching Pymol.
 
create_hard_link(source, link_name)
Create a hard link pointing to source named link_name.
Variables [hide private]
  _winreg = None
hash(x)
  fsenc = 'ANSI_X3.4-1968'
  EXTENSIONS = {'maestro': ['.bld', '.cms', '.cms.gz', '.cmsgz',...
  CSIDL = {'APPDATA': 26, 'DESKTOP': 0, 'LOCAL_APPDATA': 28, 'NE...
  APPDATA = 1
  DESKTOP = 5
  DOCUMENTS = 6
  HOME = 0
  LOCAL_APPDATA = 2
  MAESTRO = 'maestro'
  MOL2 = 'mol2'
  NETWORK = 7
  PDB = 'pdb'
  SD = 'sd'
  SMILES = 'smiles'
  SMILESCSV = 'smilescsv'
  TEMP = 4
  USERDATA = 3
hash(x)
  __package__ = 'schrodinger.utils'
Function Details [hide private]

force_remove(filename)

 

Remove the file 'filename' in a platform independent way without an exception, regardless of presence of the file or the lack of write permission.

Parameters:
  • filename (str) - the pathname for the file to remove

force_rmtree(dirname, ignore_errors=False)

 

Remove the directory 'dirname', using force_remove to remove any difficult to remove files or sub-directories.

Parameters:
  • dirname (str) - the directory to remove

force_rename(old, new)

 

Rename a file, even if a file at the new name exists, and even if that file doesn't have write permission, and even if old and new are on different devices.

Parameters:
  • old (str) - Path to the file source.
  • new (str) - Path to the file destination.

Note: Renaming may not be an atomic operation. If the 'new' file exists then it is first removed then renamed in two operations. Similarly, if old and new are not on the same device then the file is copied to 'new' then the 'old' file is removed.

splitext(p)

 

Split the extension from a pathname. Returns "(root, ext)". Equivalent to os.path.splitext(), except that for gzip compressed files, such as *.mae.gz files, ".mae.gz" is split off instead of ".gz". *.sdf.gz, *.sd.gz, *.mol.gz

Parameters:
  • p (str) - a pathname
Returns: a tuple containing two strings
The root filename and the file extension.

get_structure_file_format(filename)

 

Return the format of a structure file, based on the filename extension. None is returned if the file extension is not recognized.

is_maestro_file(filename)

 

Returns True if specified filename represents a Maestro file.

Parameters:
  • filename (string) - a filename
Returns: bool
Is this filename a maestro file?

is_sd_file(filename)

 

Returns True if specified filename represents a SD file.

Parameters:
  • filename (string) - a filename
Returns: bool
Is this filename an SD file?

is_csv_file(filename)

 

Returns True if specified filename represents a CSV file.

Parameters:
  • filename (string) - a filename
Returns: bool
Is this filename a csv file?

is_smiles_file(filename)

 

Returns True if specified filename represents a Smiles file.

Parameters:
  • filename (string) - a filename
Returns: bool
Is this filename a smiles file?

is_poseviewer_file(filename)

 

Returns True if specified filename represents a Maestro PoseViewer file. Checks if the filename ends with *_pv.mae, *_pv.maegz, *_pv.mae.gz, etc.

Parameters:
  • filename (string) - a filename
Returns: bool
Is this filename a PV file?

get_jobname(filename)

 

Returns a job name derived from the specified filename. Same as get_basename(), except that illegal characters are removed.

get_next_filename_prefix(path, midfix, zfill_width=0)

 

Return next filename prefix in series <root><midfix><number>.

Given a path (absolute or relative) to a filename or filename prefix, return the next prefix in the sequence implied by path and midfix. For example, with a path of /full/path/to/foo.mae, path/to/foo.mae or foo.mae, or /full/path/to/foo, path/to/foo or foo, and a midifix of '-', this function will return "foo-3" if any file whose prefix foo-2 (and no higher-numbered foo-*) is present. It will return foo-1 if no file whose prefix is foo-<number> is present. The net effect is that any file-name extension in the path argument will be ignored.

This function differs from next_filename() in that here, all files sharing the prefix contained in the path are searched, regardless of extension, and the next filename prefix is returned.

The search is case sensitive or not depending on the semantics of the file system. The leading directory of the path, if any, is included in the return value.

Usage note: you might use this when the filename prefix could be exhibited by many files and you don't want to overwrite any of them. For example, you are starting up a job which will create many files with the same prefix.

get_next_filename(path, midfix, zfill_width=0)

 

Return next filename in series <root><midfix><number>.<ext>.

Given a path (absolute or relative) to a filename, return the next filename in the sequence implied by path and midfix. For example, with a path of /full/path/to/foo.mae, path/to/foo.mae or foo.mae and a midifix of '-', this function will return "foo-3.mae" if file foo-2.mae (and no higher-numbered foo-*.mae )is present. It will return foo-1.mae if no file named foo-<number>.mae is present.

This function differs from next_filename_prefix() in that here, only files with the specified extension are searched, and the next full filename is retured.

The search is case sensitive or not depending on the semantics of the file system. The leading directory of the path, if any, is included in the return value.

Usage note: You might use this when you are expecting to update only a single file: the one whose filename is given in the path. For example, you are exporting structures to a .mae file and you want to pick a non-conflicting name based on a user's filename specification.

get_directory_path(which_directory)

 

This function returns the schrodinger specific directory.

If an invalid which_directory is specified, then a ValueError is thrown.

Parameters:
  • which_directory (constant) - value should be one of following...
    • HOME : To get user's home dir
    • APPDATA : To get the Schrodinger application shared data dir
    • LOCAL_APPDATA : To get the Schrodinger application local data dir
    • USERDATA : To get user's data dir
    • TEMP : To get default temporary data dir
    • DESKTOP : To get user's desktop dir (only for Windows)
    • DOCUMENTS : To get user's 'My Documents' dir (only for Windows)
    • NETWORK : To get user's 'My Network places' dir (only for Windows)
Returns: str
Directory path.

get_directory(which_directory)

 

Deprecated: Because this function behaves in a non-standard way by returning an mmlib status, get_directory_path is preferred.

get_home_dir()

 

Return the current user's home directory. Under Unix, this is the directory specified by $HOME, or else the home directory recorded in the system password database.

Linux: ~/ MacOSX: ~/ Windows: $HOME or CSIDL_PROFILE

Returns: str
absolute pathname for the home directory

get_appdata_dir()

 

Return the directory for storing schrodinger application data that can be shared.

Linux: ~/.schrodinger MacOSX: ~/.schrodinger Windows: CSIDL_APPDATA/Schrodinger

Returns: str
absolute pathname for the appdata directory

get_local_appdata_dir()

 

Return the directory for storing schrodinger application data that is local to the current machine.

Linux: ~/.schrodinger Windows: CSIDL_LOCAL_APPDATA/Schrodinger

Returns: str
absolute pathname for the local appdata directory

get_desktop_dir()

 

Return the user's desktop directory.

Linux: ~/Desktop (if it exists) MacOSX: ~/Desktop (if it exists) Windows: CSIDL_DESKTOP

Returns: str or None
absolute pathname for the home directory. If no desktop directory is found, None is returned.

get_mydocuments_dir()

 

Return the directory where user documents are stored by default.

Linux: ~/ MacOSX: ~/Documents Windows: CSIDL_PERSONAL

Returns: str
absolute pathname for the documents directory

get_mynetworkplaces_dir()

 

Linux: None MacOSX: None Windows: CSIDL_NETHOOD

Returns: str (Windows) or None (Linux and MacOSX)
absolute pathname for the Network Places directory, or None on non-Windows systems. desktop directory is found, None is returned.

get_userdata_dir()

 

Return the directory for storing user data files, like config files.

Linux: ~/.schrodinger MacOSX: ~/.schrodinger Windows: CSIDL_PROFILE/Schrodinger

Returns: str
absolute pathname for the user data directory

get_schrodinger_temp_dir()

 

Return the directory for creating temporary job directories and other temporary files. Creates the directory if it does not exist.

Linux: $SCHRODINGER_TMPDIR or ~/.schrodinger/tmp MacOSX: $SCHRODINGER_TMPDIR or ~/.schrodinger/tmp Windows: $SCHRODINGER_TMPDIR or CSIDL_LOCAL_APPDATA/Schrodinger/tmp

Returns: str
absolute pathname for the temp directory

get_startup_dir()

 

Return the default startup directory for Schrodinger applications that aren't started from the commandline.

Linux: ~/ Darwin: ~/Documents/Schrodinger Windows: CSIDL_PERSONAL/Schrodinger

Returns: str
absolute pathname for the startup directory

get_csidl_dir(csidl_name)

 
Parameters:
  • csidl_name (str) - The name of the CSIDL_* constant for some standard directory. The only supported values are: PROFILE, APPDATA, LOCAL_APPDATA, DESKTOP, PERSONAL, NETHOOD and SYSTEM.
Returns: str
The absolute pathname for the requested directory.

_check_for_pymol_by_platform(path)

 

Check for platform-specific executable file or script in the directory path provided.

Parameters:
  • path (str) - The path to modify
Returns: str or None
The path to the PyMOL executable file, or None if PyMOL was not found in the given path

_locate_pymol_in_registry(root_key, keypath, valuename=None)

 

Find out pymol installation path under given keypath and keyname. Check for standard pymol programs and return pymol_launch_command path.

Parameters:
  • root_key (_winreg key) - root registry key
  • keypath (str) - registry keypath
  • valuename (None) - valuename of the key - is always None for this module
Returns: str
the path to the Pymol executable, or None if not found

Note: Any exception encountered during registry lookup results in None being returned

locate_pymol()

 

Find the executable or script we use to launch PyMOL.

Returns: str or None
The pymol launch command or None if PyMOL was not found

get_pymol_cmd(use_x11=False)

 

Get a cmd list for launching Pymol. This may include extra platform- specific arguments.

Parameters:
  • use_x11 (bool) - if True causes -m to be added to the launch command on Mac
Returns: list
a cmd list with the executable as first element and any other options following it.

create_hard_link(source, link_name)

 

Create a hard link pointing to source named link_name.

On Windows, uses CreateHardLinkA() and will raise RuntimeError() on failure.

On other OSes uses os.link(), and will raise OSError on failure.


Variables Details [hide private]

EXTENSIONS

Value:
{'maestro': ['.bld',
             '.cms',
             '.cms.gz',
             '.cmsgz',
             '.mae',
             '.mae.gz',
             '.maegz'],
 'mol2': ['.mol2'],
...

CSIDL

Value:
{'APPDATA': 26,
 'DESKTOP': 0,
 'LOCAL_APPDATA': 28,
 'NETHOOD': 19,
 'PERSONAL': 5,
 'PROFILE': 40,
 'SYSTEM': 37}