home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 4 / AACD04.ISO / AACD / Programming / Python / Lib / site-python / Dos.py < prev   
Encoding:
Python Source  |  1998-11-12  |  5.6 KB  |  226 lines

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