Changeset 2:e30d192e9031


Ignore:
Timestamp:
Sep 1, 2013, 8:12:43 AM (7 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Message:

Fix PEP8 errors.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pyweek_upload.py

    r0 r2  
    44Handles authentication and gives upload progress feedback.
    55'''
    6 import sys, os, httplib, cStringIO, socket, time, getopt
    7 
    8 class Upload:
     6import sys
     7import os
     8import httplib
     9import cStringIO
     10import socket
     11import time
     12import getopt
     13
     14
     15class Upload(object):
    916    def __init__(self, filename):
    1017        self.filename = filename
     
    1320sep_boundary = '\n--' + boundary
    1421end_boundary = sep_boundary + '--'
     22
    1523
    1624def mimeEncode(data, sep_boundary=sep_boundary, end_boundary=end_boundary):
     
    2129    for key, value in data.items():
    2230        # handle multiple entries for the same name
    23         if type(value) != type([]): value = [value]
     31        if not isinstance(value, list):
     32            value = [value]
    2433        for value in value:
    2534            ret.write(sep_boundary)
    2635            if isinstance(value, Upload):
    27                 ret.write('\nContent-Disposition: form-data; name="%s"'%key)
     36                ret.write('\nContent-Disposition: form-data; name="%s"' % key)
    2837                filename = os.path.basename(value.filename)
    29                 ret.write('; filename="%s"\n\n'%filename)
     38                ret.write('; filename="%s"\n\n' % filename)
    3039                value = open(os.path.join(value.filename), "rb").read()
    3140            else:
    32                 ret.write('\nContent-Disposition: form-data; name="%s"'%key)
     41                ret.write('\nContent-Disposition: form-data; name="%s"' % key)
    3342                ret.write("\n\n")
    3443                value = str(value)
     
    3948    return ret.getvalue()
    4049
    41 class Progress:
     50
     51class Progress(object):
    4252    def __init__(self, info, data):
    4353        self.info = info
    4454        self.tosend = len(data)
    45         self.total = self.tosend/1024
     55        self.total = self.tosend / 1024
    4656        self.data = cStringIO.StringIO(data)
    4757        self.start = self.now = time.time()
     
    5262        self.display()
    5363
    54     def __iter__(self): return self
     64    def __iter__(self):
     65        return self
    5566
    5667    def next(self):
    5768        self.num += 1
    5869        if self.sent >= self.tosend:
    59             print self.info, 'done', ' '*(75-len(self.info)-6)
     70            print self.info, 'done', ' ' * (75 - len(self.info) - 6)
    6071            sys.stdout.flush()
    6172            raise StopIteration
     
    7990        steptime = sum(self.steptimes) / len(self.steptimes)
    8091        self.now = now
    81         eta = steptime * ((self.total - self.num)/self.stepsize)
     92        eta = steptime * ((self.total - self.num) / self.stepsize)
    8293
    8394        # tell it like it is (or might be)
     
    8899            S = eta % 60
    89100            if self.total:
    90                 s = '%s %2d%% (ETA %02d:%02d:%02d)'%(self.info,
     101                s = '%s %2d%% (ETA %02d:%02d:%02d)' % (self.info,
    91102                    self.num * 100. / self.total, H, M, S)
    92103            else:
    93                 s = '%s 0%% (ETA %02d:%02d:%02d)'%(self.info, H, M, S)
     104                s = '%s 0%% (ETA %02d:%02d:%02d)' % (self.info, H, M, S)
    94105        elif self.total:
    95             s = '%s %2d%%'%(self.info, self.num * 100. / self.total)
     106            s = '%s %2d%%' % (self.info, self.num * 100. / self.total)
    96107        else:
    97             s = '%s %d done'%(self.info, self.num)
    98         sys.stdout.write(s + ' '*(75-len(s)) + '\r')
     108            s = '%s %d done' % (self.info, self.num)
     109        sys.stdout.write(s + ' ' * (75 - len(s)) + '\r')
    99110        sys.stdout.flush()
     111
    100112
    101113class progressHTTPConnection(httplib.HTTPConnection):
     
    117129                p.display()
    118130
     131
    119132class progressHTTP(httplib.HTTP):
    120133    _connection_class = progressHTTPConnection
     134
    121135    def _setup(self, conn):
    122136        httplib.HTTP._setup(self, conn)
    123137        self.progress_send = self._conn.progress_send
    124138
     139
    125140def http_request(data, server, port, url):
    126141    h = progressHTTP(server, port)
     
    128143    data = mimeEncode(data)
    129144    h.putrequest('POST', url)
    130     h.putheader('Content-type', 'multipart/form-data; boundary=%s'%boundary)
     145    h.putheader('Content-type', 'multipart/form-data; boundary=%s' % boundary)
    131146    h.putheader('Content-length', str(len(data)))
    132147    h.putheader('Host', server)
     
    141156    f.close()
    142157
    143     print '%s %s'%(errcode, errmsg)
    144     if response: print response
     158    print '%s %s' % (errcode, errmsg)
     159    if response:
     160        print response
     161
    145162
    146163def usage():
     
    179196    url = None
    180197    for opt, arg in optlist:
    181         if opt == '-u': data['user'] = arg
    182         elif opt == '-p': data['password'] = arg
    183         elif opt == '-s': optional['is_screenshot'] = 'yes'
    184         elif opt == '-f': optional['is_final'] = 'yes'
    185         elif opt == '-d': data['description'] = arg
    186         elif opt == '-c': data['content_file'] = Upload(arg)
    187         elif opt == '-e': url = '/e/%s/oup/'%arg
    188         elif opt == '-h': host = arg
    189         elif opt == '-P': port = int(arg)
     198        if opt == '-u':
     199            data['user'] = arg
     200        elif opt == '-p':
     201            data['password'] = arg
     202        elif opt == '-s':
     203            optional['is_screenshot'] = 'yes'
     204        elif opt == '-f':
     205            optional['is_final'] = 'yes'
     206        elif opt == '-d':
     207            data['description'] = arg
     208        elif opt == '-c':
     209            data['content_file'] = Upload(arg)
     210        elif opt == '-e':
     211            url = '/e/%s/oup/' % arg
     212        elif opt == '-h':
     213            host = arg
     214        elif opt == '-P':
     215            port = int(arg)
    190216
    191217    if len(data) < 4 or url is None:
     
    196222    data.update(optional)
    197223    http_request(data, host, port, url)
    198 
Note: See TracChangeset for help on using the changeset viewer.