1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
#!/usr/bin/env python
#
# Restriction Analysis Libraries.
# Copyright (C) 2004. Frederic Sohm.
#
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
#
"""Update the Rebase emboss files used by Restriction to build the
Restriction_Dictionary.py module."""
from __future__ import print_function
import os
import sys
import time
import optparse
try:
from urllib import FancyURLopener
except ImportError:
#Python 3
from urllib.request import FancyURLopener
from Bio.Restriction.RanaConfig import *
class RebaseUpdate(FancyURLopener):
def __init__(self, e_mail='', ftpproxy=''):
"""RebaseUpdate([e_mail[, ftpproxy]]) -> new RebaseUpdate instance.
if e_mail and ftpproxy are not given RebaseUpdate uses the corresponding
variable from RanaConfig.
e_mail is the password for the anonymous ftp connection to Rebase.
ftpproxy is the proxy to use if any."""
proxy = {'ftp' : ftpproxy or ftp_proxy}
global Rebase_password
Rebase_password = e_mail or Rebase_password
if not Rebase_password:
raise FtpPasswordError('Rebase')
if not Rebase_name:
raise FtpNameError('Rebase')
FancyURLopener.__init__(self, proxy)
def prompt_user_passwd(self, host, realm):
return (Rebase_name, Rebase_password)
def openRebase(self, name = ftp_Rebase):
print('\n Please wait, trying to connect to Rebase\n')
try:
self.open(name)
except:
raise ConnectionError('Rebase')
return
def getfiles(self, *files):
for file in self.update(*files):
print('copying %s' % file)
fn = os.path.basename(file)
#filename = os.path.join(Rebase, fn)
filename = os.path.join(os.getcwd(), fn)
print('to %s' % filename)
self.retrieve(file, filename)
self.close()
return
def localtime(self):
t = time.gmtime()
year = str(t.tm_year)[-1]
month = str(t.tm_mon)
if len(month) == 1:
month = '0' + month
return year+month
def update(self, *files):
if not files:
files = [ftp_emb_e, ftp_emb_s, ftp_emb_r]
return [x.replace('###', self.localtime()) for x in files]
def __del__(self):
if hasattr(self, 'tmpcache'):
self.close()
#
# self.tmpcache is created by URLopener.__init__ method.
#
return
class FtpNameError(ValueError):
def __init__(self, which_server):
print(" In order to connect to %s ftp server, you must provide a name.\
\n Please edit Bio.Restriction.RanaConfig\n" % which_server)
sys.exit()
class FtpPasswordError(ValueError):
def __init__(self, which_server):
print("\n\
\n In order to connect to %s ftp server, you must provide a password.\
\n Use the --e-mail switch to enter your e-mail address.\
\n\n" % which_server)
sys.exit()
class ConnectionError(IOError):
def __init__(self, which_server):
print('\
\n Unable to connect to the %s ftp server, make sure your computer\
\n is connected to the internet and that you have correctly configured\
\n the ftp proxy.\
\n Use the --proxy switch to enter the address of your proxy\
\n' % which_server)
sys.exit()
if __name__ == '__main__':
parser = optparse.OptionParser()
add = parser.add_option
add('-m', '--e-mail',
action = "store",
dest = 'rebase_password',
default = '',
help = "set the e-mail address to be used as password for the"
"anonymous ftp connection to Rebase.")
add('-p', '--proxy',
action = "store",
dest = 'ftp_proxy',
default = '',
help = "set the proxy to be used by the ftp connection.")
(option, args) = parser.parse_args()
Getfiles = RebaseUpdate(option.rebase_password, option.ftp_proxy)
Getfiles.openRebase()
Getfiles.getfiles()
Getfiles.close()
sys.exit()
|