home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Lib / site-python / Dos.py < prev   
Encoding:
Python Source  |  2000-10-01  |  5.7 KB  |  229 lines

  1. """
  2. AMIGA LIBRARY INTERFACE FOR dos.library
  3. ©Irmen de Jong, disclaimer applies!
  4.  
  5. $VER: Dos.py 1.3 (1.10.00)
  6. """
  7.  
  8.  
  9. import string
  10. import os,time
  11. import errno
  12.  
  13. from Doslib import *
  14.  
  15. # Modes for SetConsoleMode()
  16. MODE_CON = 0
  17. MODE_RAW = 1
  18.  
  19. # Bits that signal you that a user has issued a break
  20. SIGBREAKF_CTRL_C = 1<<12
  21. SIGBREAKF_CTRL_D = 1<<13
  22. SIGBREAKF_CTRL_E = 1<<14
  23. SIGBREAKF_CTRL_F = 1<<15
  24.     
  25. # FILE PROTECTION BITS
  26. # USUAL
  27. FIBF_SCRIPT    = 1<<6
  28. FIBF_PURE    = 1<<5
  29. FIBF_ARCHIVE    = 1<<4
  30. FIBF_READ    = 1<<3
  31. FIBF_WRITE    = 1<<2
  32. FIBF_EXECUTE    = 1<<1
  33. FIBF_DELETE    = 1<<0
  34. # "OTHER"
  35. FIBF_OTR_READ    = 1<<15
  36. FIBF_OTR_WRITE    = 1<<14
  37. FIBF_OTR_EXECUTE= 1<<13
  38. FIBF_OTR_DELETE    = 1<<12
  39. # "GROUP"
  40. FIBF_GRP_READ    = 1<<11
  41. FIBF_GRP_WRITE    = 1<<10
  42. FIBF_GRP_EXECUTE= 1<<9
  43. FIBF_GRP_DELETE    = 1<<8
  44.  
  45. # FLAGS FOR DateToStr AND StrToDate
  46. DTF_SUBST    = 1<<0    # substitute Today, Tomorrow, etc.
  47. DTB_FUTURE    = 1<<1    # day of the week is in future
  48.  
  49. # FORMATS FOR DateToStr AND StrToDate
  50. FORMAT_DOS    = 0    # dd-mmm-yy
  51. FORMAT_INT    = 1    # yy-mm-dd
  52. FORMAT_USA    = 2    # mm-dd-yy
  53. FORMAT_CDN    = 3    # dd-mm-yy
  54.  
  55.  
  56. ### EXAMINE struct members (FileInfoBlock)
  57. fib_FileName    = 0
  58. fib_Size        = 1
  59. fib_DirEntryType= 2        # if <0: file, if >0: dir
  60. fib_Protection    = 3
  61. fib_DiskKey        = 4
  62. fib_NumBlocks    = 5
  63. fib_Date        = 6
  64. fib_Comment        = 7
  65. fib_OwnerUID    = 8
  66. fib_OwnerGID    = 9
  67.  
  68.  
  69. ### INFO struct members (InfoData)
  70. id_NumSoftErrors    = 0
  71. id_UnitNumber        = 1
  72. id_DiskState        = 2        # see defines below
  73. id_NumBlocks        = 3
  74. id_NumBlocksUsed    = 4
  75. id_BytesPerBlock    = 5
  76. id_DiskType            = 6        # 4-byte FileSystem type code
  77. #id_VolumeNode  not used in Python
  78. id_InUse            = 7        # flag, zero if not in use
  79.  
  80. ### INFO struct DiskState types
  81. ID_WRITE_PROTECTED    = 80
  82. ID_VALIDATING        = 81
  83. ID_VALIDATED        = 82
  84.  
  85.  
  86. #### ARGPARSER ##################################
  87.  
  88. class ArgParser:
  89.     """
  90.     Argument string parser class.
  91.     To be used for parsing dos.library/ReadArgs() arguments
  92.     (the Amiga style for command lines).
  93.     """
  94.     def __init__(self,template):            # create
  95.         self.new(template)
  96.     def new(self,template):                 # new template
  97.         self.template=template
  98.         self.reset()
  99.     def reset(self):                        # reset types & defaults
  100.         self.defaults,self.types = self.parsetempl(self.template)
  101.     def parse(self,args):
  102.         result=ReadArgs(self.template,args,self.types)
  103.         for k in result.keys():
  104.             if not result[k]:
  105.                 dflt = self.defaults[k]
  106.                 if type(dflt)!=type([]):
  107.                     result[k]=dflt
  108.                 else:
  109.                     result[k]=dflt[:]   # make slice copy of list
  110.         return result               
  111.  
  112.     # Below this point are private members.
  113.  
  114.     def parsetempl(self,templ):
  115.         # Parse template.
  116.         # This function builds the default argument dictionary and
  117.         # the argument type list.
  118.  
  119.         # first check special case: empty template
  120.         if not templ:
  121.             return ({},())
  122.  
  123.         for c in templ:
  124.             if not c in '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_.,=/ ':
  125.                 raise ValueError,'invalid char in template ('+c+')'
  126.  
  127.         # Split the template twice (first by ',' then by '/')
  128.         templ=map(lambda x: string.split(x,'/'),string.split(templ,','))
  129.         # Build the type list
  130.         types=[]; defdict={}; lists=0; keywdic={}
  131.         defaults = {'S':0, 'T':0, 'N':None, 'X':None, 'A':[], 'I': []}
  132.         for a in templ:
  133.             if a[0]=='':
  134.                 raise ValueError,'missing keyword in template'
  135.  
  136.             # Now convert argument spec --> type & modifiers.
  137.             # 6 different output types are possible:
  138.             # X     - string            \
  139.             # S,T   - bool (integer)     > can have /F as modifier
  140.             #    (note: currently \T is NOT SUPPORTED)
  141.             # N     - integer           /
  142.             # X/M   - array of strings ('A')
  143.             # N/M   - array of ints ('I')
  144.             # All 6 can have /A or /K as additional modifiers.
  145.             # However, /F /A and /K have no influence on the returned type. (/M does)
  146.  
  147.             type=None; modifiers='';
  148.             for s in map(string.upper,a[1:]):
  149.                 if not s:
  150.                     raise ValueError,'invalid template'
  151.                 elif s=='T':
  152.                     raise SystemError,'/T not (yet) supported, sorry'
  153.                 elif s in 'SNT':        # possible template option types
  154.                     if type:
  155.                         raise ValueError,'invalid switch combination at '+a[0]
  156.                     type=s
  157.                 elif s in 'KAMF':       # template option modifiers
  158.                     if s in modifiers or  \
  159.                        (s=='M' and 'F' in modifiers) or \
  160.                        (s=='F' and 'M' in modifiers):
  161.                         # do not allow /F/M or /M/F or /A/A etc 
  162.                         raise ValueError,'invalid switch combination at '+a[0]
  163.                     modifiers=modifiers+s
  164.                 else:
  165.                     raise ValueError,'unknown switch /'+s
  166.  
  167.             if not type:
  168.                 type = 'X'      # special type: string (default type)
  169.  
  170.             if 'M' in modifiers:
  171.                 lists=lists+1
  172.                 if lists>1:
  173.                     raise ValueError,'multiple /M switches'
  174.                 if type=='X': type='A'      # array of strings
  175.                 elif type=='N': type='I'    # array of ints
  176.                 else:
  177.                     raise ValueError,'wrong /M combination'
  178.  
  179.             # All done, add type to type list, and find default value.
  180.  
  181.             types.append(a[0],type)
  182.             try:
  183.                 keywdic[a[0]]=keywdic[a[0]]+1
  184.             except KeyError:
  185.                 keywdic[a[0]]=1
  186.             if not 'A' in modifiers:
  187.                 defdict[a[0]]=defaults[type]
  188.             
  189.         keywdic=keywdic.values()
  190.         if keywdic.count(1) != len(keywdic):
  191.             raise ValueError,'clashing keywords'
  192.  
  193.         return (defdict,tuple(types))
  194.  
  195.  
  196.  
  197.  
  198. ##################### Various Functions #################
  199.  
  200. def touch(file, tme=None):
  201.     if not tme:
  202.         tme=time.time()
  203.     tme = time2DS(tme)
  204.     # first, check if the file exists
  205.     try:
  206.         os.stat(file)
  207.         # file exists... continue
  208.     except os.error, x:
  209.         if(x[0]==errno.ENOENT):
  210.             # file does not exist, create it first
  211.             open(file,'w')
  212.         else:
  213.             # other error...
  214.             raise os.error,x
  215.  
  216.     # now, set the filedate
  217.     SetFileDate(file,tme)
  218.  
  219.  
  220. def AddBuffers(drive, buffers):
  221.     return os.system('c:addbuffers >NIL: "%s" %d'%(drive,buffers))
  222.  
  223. def AssignAdd(name, target):
  224.     return os.system('c:assign >NIL: "%s" "%s" ADD'%(name,target))
  225.  
  226. def AssignRemove(name):
  227.     return os.system('c:assign >NIL: "%s" REMOVE'%name)
  228.  
  229.