Remote file and directory functions
| 150 | |
| 151 | |
| 152 | class remoteHandler: |
| 153 | """Remote file and directory functions""" |
| 154 | def __init__(self, ftp, root): |
| 155 | self.ftp = ftp |
| 156 | self.root = root |
| 157 | self.host = ftp.host |
| 158 | |
| 159 | def storefile(self, src, dst): |
| 160 | fh = open(src) |
| 161 | self.ftp.storbinary('STOR %s' % dst, fh) |
| 162 | fh.close() |
| 163 | |
| 164 | def storetext(self, text, dst): |
| 165 | fh = StringIO.StringIO(text) |
| 166 | self.ftp.storlines('STOR %s' % dst, fh) |
| 167 | fh.close() |
| 168 | |
| 169 | def readlines(self, path): |
| 170 | buffer = [] |
| 171 | self.ftp.retrlines('RETR %s' % path, buffer.append) |
| 172 | return buffer |
| 173 | |
| 174 | def list(self, dir, skip_mtime=False): |
| 175 | month_to_int = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, |
| 176 | 'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8, 'Sep': 9, |
| 177 | 'Oct': 10, 'Nov': 11, 'Dec': 12} |
| 178 | try: |
| 179 | buffer = [] |
| 180 | self.ftp.dir('-a ', dir, buffer.append) |
| 181 | except ftplib.error_temp: |
| 182 | buffer = [] |
| 183 | self.ftp.dir(dir, buffer.append) |
| 184 | dirs = [] |
| 185 | files = {} |
| 186 | for line in buffer: |
| 187 | cols = line.split(None, 8) |
| 188 | name = os.path.split(cols[8])[1] |
| 189 | if cols[0] == 'total' or name in ('.', '..'): |
| 190 | continue |
| 191 | if cols[0].startswith('d'): |
| 192 | dirs.append(name) |
| 193 | else: |
| 194 | if skip_mtime: |
| 195 | mtime = 0 |
| 196 | else: |
| 197 | month = month_to_int[cols[5]] |
| 198 | day = int(cols[6]) |
| 199 | if cols[7].find(':') == -1: |
| 200 | year = int(cols[7]) |
| 201 | hour = minute = 0 |
| 202 | else: |
| 203 | year = datetime.date.today().year |
| 204 | hour, minute = [int(s) for s in cols[7].split(':')] |
| 205 | mtime = datetime.datetime(year, month, day, hour, minute) |
| 206 | mtime = int(time.mktime(mtime.timetuple())) |
| 207 | size = int(cols[4]) |
| 208 | files[name] = { |
| 209 | 'size': size, |