Module Sys

Source Code for Module Sys

  1  # Blender.sys module 
  2   
  3  """ 
  4  The Blender.sys submodule. 
  5   
  6  sys 
  7  === 
  8   
  9  B{New}: L{expandpath}. 
 10   
 11  This module provides a minimal set of helper functions and data.  Its purpose 
 12  is to avoid the need for the standard Python module 'os', in special 'os.path', 
 13  though it is only meant for the simplest cases. 
 14   
 15  Example:: 
 16   
 17    import Blender 
 18   
 19    filename = "" 
 20    def f(name): # file selector callback 
 21      global filename 
 22      filename = name 
 23   
 24    Blender.Window.FileSelector(f) 
 25   
 26    if filename: 
 27      print 'basename:', Blender.sys.basename(filename) 
 28      print 'dirname:',  Blender.sys.dirname(filename) 
 29      print 'splitext:', Blender.sys.splitext(filename) 
 30   
 31    # what would basename(splitext(filename)[0]) print? 
 32   
 33  @type sep: char 
 34  @var sep: the platform-specific dir separator for this Blender: '/' 
 35      everywhere, except on Win systems, that use '\\'.  
 36  @type dirsep: char 
 37  @var dirsep: same as L{sep}. 
 38  @type progname: string 
 39  @var progname: the Blender executable (argv[0]). 
 40   
 41  @attention: The module is called sys, not Sys. 
 42  """ 
 43   
44 -def basename (path):
45 """ 46 Get the base name (filename stripped from dir info) of 'path'. 47 @type path: string 48 @param path: a path name 49 @rtype: string 50 @return: the base name 51 """
52
53 -def dirname (path):
54 """ 55 Get the dir name (dir path stripped from filename) of 'path'. 56 @type path: string 57 @param path: a path name 58 @rtype: string 59 @return: the dir name 60 """
61
62 -def join (dir, file):
63 """ 64 Join the given dir and file paths, using the proper separator for each 65 platform. 66 @type dir: string 67 @type file: string 68 @param dir: the dir name, like returned from L{dirname}. 69 @param file: the bare filename, like returned from L{basename}. 70 @rtype: string 71 @return: the resulting filename. 72 @warn: this simple function isn't intended to be a complete replacement for 73 the standard os.path.join() one, which handles more general cases. 74 """
75
76 -def splitext (path):
77 """ 78 Split 'path' into (root, ext), where 'ext' is a file extension including the full stop. 79 80 Example:: 81 82 import Blender 83 file, ext= Blender.sys.splitext('/tmp/foobar.blend') 84 print file, ext 85 # ('/tmp/foobar', '.blend') 86 87 @type path: string 88 @param path: a path name 89 @rtype: tuple of two strings 90 @return: (root, ext) 91 @note: This function will raise an error if the path is longer then 80 characters. 92 """
93
94 -def makename (path = "Blender.Get('filename')", ext = "", strip = 0):
95 """ 96 Remove extension from 'path', append extension 'ext' (if given) 97 to the result and return it. If 'strip' is non-zero, also remove 98 dirname from path. 99 100 Example:: 101 import Blender 102 from Blender.sys import * 103 print makename('/path/to/myfile.txt','.abc', 1) # returns 'myfile.abc' 104 105 print makename('/path/to/myfile.obj', '-01.obj') # '/path/to/myfile-01.obj' 106 107 print makename('/path/to/myfile.txt', strip = 1) # 'myfile' 108 109 # note that: 110 print makename(ext = '.txt') 111 # is equivalent to: 112 print sys.splitext(Blender.Get('filename'))[0]) + '.txt' 113 114 @type path: string 115 @param path: a path name or Blender.Get('filename'), if not given. 116 @type ext: string 117 @param ext: an extension to append. For flexibility, a dot ('.') is 118 not automatically included. 119 @rtype: string 120 @return: the resulting string 121 """
122
123 -def exists(path):
124 """ 125 Tell if the given pathname (file or dir) exists. 126 @rtype: int 127 @return: 128 - 0: path does not exist; 129 - 1: path is an existing filename; 130 - 2: path is an existing dirname; 131 - -1: path exists but is neither a regular file nor a dir. 132 """
133
134 -def time ():
135 """ 136 Get the current time in seconds since a fixed value. Successive calls to 137 this function are guaranteed to return values greater than the previous call. 138 @rtype: float 139 @return: the elapsed time in seconds. 140 """
141
142 -def sleep (millisecs = 10):
143 """ 144 Sleep for the specified amount of time. 145 @type millisecs: int 146 @param millisecs: the amount of time in milliseconds to sleep. The default 147 is 10 which is 0.1 seconds. 148 """
149
150 -def expandpath (path):
151 """ 152 Expand the given Blender 'path' into an absolute and valid path. 153 Internally, Blender recognizes two special character sequences in paths: 154 - '//' (used at the beginning): means base path -- the current .blend file's 155 dir; 156 - '#' characters in the filename will be replaced by the frame number. 157 The expanded string can be passed to generic python functions that don't 158 understand Blender's internal relative paths. 159 @note: this function is also useful for obtaining the name of the image 160 that will be saved when rendered. 161 @note: if the passed string doesn't contain the special characters it is 162 returned unchanged. 163 @type path: string 164 @param path: a path name. 165 @rtype: string 166 @return: the expanded (if necessary) path. 167 """
168
169 -def cleanpath (path):
170 """ 171 Clean the given 'path' by removing unneeded components such as "/./" and "/test/../" 172 @type path: string 173 @param path: a path name. 174 @rtype: string 175 @return: the cleaned (if necessary) path. 176 """
177