Back to index

python-biopython  1.60
Update.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 #      Restriction Analysis Libraries.
00004 #      Copyright (C) 2004. Frederic Sohm.
00005 #
00006 # This code is part of the Biopython distribution and governed by its
00007 # license.  Please see the LICENSE file that should have been included
00008 # as part of this package.
00009 #
00010 
00011 """Update the Rebase emboss files used by Restriction to build the
00012 Restriction_Dictionary.py module."""
00013 
00014 import os
00015 import sys
00016 import sre
00017 import time
00018 import gzip
00019 from urllib import FancyURLopener
00020 
00021 from Bio.Restriction.RanaConfig import *
00022 
00023 
00024 class RebaseUpdate(FancyURLopener):
00025     
00026     def __init__(self, e_mail='', ftpproxy=''):
00027         """RebaseUpdate([e_mail[, ftpproxy]]) -> new RebaseUpdate instance.
00028 
00029         if e_mail and ftpproxy are not given RebaseUpdate uses the corresponding
00030         variable from RanaConfig.
00031 
00032         e_mail is the password for the anonymous ftp connection to Rebase.
00033         ftpproxy is the proxy to use if any."""
00034         proxy = {'ftp' : ftpproxy or ftp_proxy}
00035         global Rebase_password
00036         Rebase_password = e_mail or Rebase_password
00037         if not Rebase_password:
00038             raise FtpPasswordError('Rebase')
00039         if not Rebase_name:
00040             raise FtpNameError('Rebase')
00041         FancyURLopener.__init__(self, proxy)
00042 
00043     def prompt_user_passwd(self, host, realm):
00044         return (Rebase_name, Rebase_password)
00045 
00046     def openRebase(self, name = ftp_Rebase):
00047         print '\n Please wait, trying to connect to Rebase\n'
00048         try:
00049             self.open(name)
00050         except:
00051             raise ConnectionError('Rebase')
00052         return
00053 
00054     def getfiles(self, *files):
00055         print '\n',
00056         for file in self.update(*files):
00057             print 'copying', file
00058             fn = os.path.basename(file)
00059             #filename = os.path.join(Rebase, fn)
00060             filename = os.path.join(os.getcwd(), fn)
00061             print 'to', filename
00062             self.retrieve(file, filename)
00063         self.close()
00064         return
00065 
00066     def localtime(self):
00067         t = time.gmtime()
00068         year = str(t.tm_year)[-1]
00069         month = str(t.tm_mon)
00070         if len(month) == 1 : month = '0'+month
00071         return year+month
00072 
00073     def update(self, *files):
00074         if not files:
00075             files = [ftp_emb_e, ftp_emb_s, ftp_emb_r]
00076         return [x.replace('###', self.localtime()) for x in files]
00077 
00078     def __del__(self):
00079         if hasattr(self, 'tmpcache') : self.close()
00080         #
00081         #   self.tmpcache is created by URLopener.__init__ method.
00082         #
00083         return
00084 
00085 
00086 class FtpNameError(ValueError):
00087 
00088     def __init__(self, which_server):
00089         print " In order to connect to %s ftp server, you must provide a name.\
00090         \n Please edit Bio.Restriction.RanaConfig\n" % which_server
00091         sys.exit()
00092 
00093 class FtpPasswordError(ValueError):
00094 
00095     def __init__(self, which_server):
00096         print "\n\
00097         \n In order to connect to %s ftp server, you must provide a password.\
00098         \n Use the --e-mail switch to enter your e-mail address.\
00099         \n\n" % which_server
00100         sys.exit()
00101 
00102 
00103 class ConnectionError(IOError):
00104 
00105     def __init__(self, which_server):
00106         print '\
00107         \n Unable to connect to the %s ftp server, make sure your computer\
00108         \n is connected to the internet and that you have correctly configured\
00109         \n the ftp proxy.\
00110         \n Use the --proxy switch to enter the address of your proxy\
00111         \n' % which_server
00112         sys.exit()
00113         
00114 
00115 
00116 __all__ = ['RebaseUpdate', 'FtpNameError', 'FtpPasswordError']