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
|
#!/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.
The Rebase EMBOSS files are used by `ranacompiler.py` to build the updated
`Restriction_Dictionary.py` module for `Bio.Restriction`.
"""
from __future__ import print_function
import os
import sys
import time
import optparse
try:
from urllib import FancyURLopener, urlcleanup
except ImportError:
# Python 3
from urllib.request import FancyURLopener, urlcleanup
from Bio.Restriction.RanaConfig import ftp_proxy, ftp_Rebase, Rebase_name
from Bio.Restriction.RanaConfig import ftp_emb_e, ftp_emb_s, ftp_emb_r
class RebaseUpdate(FancyURLopener):
def __init__(self, ftpproxy=''):
"""RebaseUpdate([ftpproxy]]) -> new RebaseUpdate instance.
if ftpproxy is not given RebaseUpdate uses the corresponding
variable from RanaConfig.
ftpproxy is the proxy to use if any.
"""
proxy = {'ftp': ftpproxy or ftp_proxy}
if not Rebase_name:
raise FtpNameError('Rebase')
if not proxy['ftp']:
proxy = {}
FancyURLopener.__init__(self, proxy)
def openRebase(self, name=ftp_Rebase):
print('\n Please wait, trying to connect to Rebase\n')
try:
self.open(name)
except Exception:
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)
try:
self.retrieve(file, filename)
# The following line is a workaround for an urllib bug in
# Python 2.7.11 - 2.7.xx (?). It does not seem to work on
# Python 3.xx. Try to remove the line in new Python versions.
urlcleanup()
except IOError as e:
print(e)
print('This error is probably due to a non-solved ftp bug in '
'recent Python versions. Please download the emboss '
'files manually from http://rebase.neb.com/rebase/'
'rebase.f37.html and then run ranacompiler.py. Find '
'more details in the Restriction manual.')
self.close()
return
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 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('-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.ftp_proxy)
Getfiles.openRebase()
Getfiles.getfiles()
Getfiles.close()
sys.exit()
|