From: Tom Hunger Date: Fri, 16 Jan 2015 10:37:08 +0000 (+0000) Subject: Fix a fair amount of flake8 complaining. X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=43e222ace964023102eb71548671772e197c6fbe;p=sirius.git Fix a fair amount of flake8 complaining. Ignoring certain cases where readability > consistency. --- diff --git a/sirius/coding/decoders.py b/sirius/coding/decoders.py index 9f14be2..776ef91 100644 --- a/sirius/coding/decoders.py +++ b/sirius/coding/decoders.py @@ -42,19 +42,20 @@ class DeviceEventConst(object): EVENT_DID_POWER_ON_SIZE_SHORT = 58 # No fragmentation RESET_DICT = { - 0x0000 : "Undeterminable cause", - 0x0100 : "FIB bootloader", - 0x0200 : "Ember bootloader", - 0x0300 : "External reset", - 0x0400 : "Power on", - 0x0500 : "Watchdog", - 0x0600 : "Software triggered", - 0x0700 : "Software crash", - 0x0800 : "Flash failure", - 0x0900 : "Fatal error", - 0x0a00 : "Access fault", + 0x0000: "Undeterminable cause", + 0x0100: "FIB bootloader", + 0x0200: "Ember bootloader", + 0x0300: "External reset", + 0x0400: "Power on", + 0x0500: "Watchdog", + 0x0600: "Software triggered", + 0x0700: "Software crash", + 0x0800: "Flash failure", + 0x0900: "Fatal error", + 0x0a00: "Access fault", } + def decode_message(data): """Decodes a single incoming message from data. @@ -185,7 +186,7 @@ def _decode_device_event(data): else: return messages.MalformedEvent( data, 'power_on payload wrong size. Is {}. Expected: {}'.format( - len(payload_length), + len(payload_length), )) deviceType, firmwareBuildVersion, loaderBuildVersion, protocolVersion, resetDescription = power_on_fields diff --git a/sirius/coding/image_encoding.py b/sirius/coding/image_encoding.py index 13909b3..4ce2bd1 100644 --- a/sirius/coding/image_encoding.py +++ b/sirius/coding/image_encoding.py @@ -7,7 +7,8 @@ import tempfile WHITE = 0 BLACK = 1 THRESHOLD = 127 -TRANSLATE = [ (1536, 255), (1152, 254), (768, 253), (384, 252), (251, 251) ] +TRANSLATE = [(1536, 255), (1152, 254), (768, 253), (384, 252), (251, 251)] + def pixel_to_bw(p): if p == (0, 0, 0, 0): @@ -17,7 +18,11 @@ def pixel_to_bw(p): else: return BLACK -def ilen(i): return sum(1 for _ in i) + +def ilen(i): + "Returns length of an iterator in constant space." + return sum(1 for _ in i) + # so what we have is a list of runs, alternating white and black, # starting with white. @@ -66,7 +71,7 @@ def rle_image(im): # create list of tuples (WHITE|BLACK, run_length) groups = [] for k, g in grouped: - groups.append( (k, ilen(g)) ) + groups.append((k, ilen(g))) # the decoder assumes that the first run is white, so if the # first pixel is black, add a zero run of white @@ -87,24 +92,24 @@ def rle_image(im): def html_to_png(html): - """ This works: - - - - """ - from selenium import webdriver - try: - driver = webdriver.PhantomJS('phantomjs') - driver.set_window_size(384, 5) - - # note that the .html suffix is required to make phantomjs - # pick up the mime-type and render correctly. - with tempfile.NamedTemporaryFile(suffix='.html') as f: - f.write(html) - f.flush() - driver.get('file://' + f.name) - p = driver.get_screenshot_as_png() - - return Image.open(io.BytesIO(p)) - finally: - driver.quit() + """ This works: + + + + """ + from selenium import webdriver + try: + driver = webdriver.PhantomJS('phantomjs') + driver.set_window_size(384, 5) + + # note that the .html suffix is required to make phantomjs + # pick up the mime-type and render correctly. + with tempfile.NamedTemporaryFile(suffix='.html') as f: + f.write(html) + f.flush() + driver.get('file://' + f.name) + p = driver.get_screenshot_as_png() + + return Image.open(io.BytesIO(p)) + finally: + driver.quit() diff --git a/sirius/coding/test_claim_coding.py b/sirius/coding/test_claim_coding.py index b0980b8..1583c63 100644 --- a/sirius/coding/test_claim_coding.py +++ b/sirius/coding/test_claim_coding.py @@ -1,7 +1,6 @@ import unittest from sirius.coding import claiming -from sirius.coding import bitshuffle CC_TEST_VECTORS = [ diff --git a/sirius/coding/test_image_coding.py b/sirius/coding/test_image_coding.py index fd8fcd4..4033129 100644 --- a/sirius/coding/test_image_coding.py +++ b/sirius/coding/test_image_coding.py @@ -2,6 +2,7 @@ import unittest from sirius.coding import image_encoding + class ImageCase(unittest.TestCase): def test_normal_text(self): diff --git a/sirius/fake/commands.py b/sirius/fake/commands.py index cfdec7c..dc0580b 100644 --- a/sirius/fake/commands.py +++ b/sirius/fake/commands.py @@ -10,11 +10,14 @@ from sirius.models.db import db from sirius.coding import bitshuffle from sirius.coding import claiming + def sub_opts(app, **kwargs): pass + fake_manager = script.Manager(sub_opts, usage="Fake printer interactions.") + @fake_manager.command def printer(): device_address = os.urandom(8).encode('hex') diff --git a/sirius/models/user.py b/sirius/models/user.py index 35d43c1..c21efe0 100644 --- a/sirius/models/user.py +++ b/sirius/models/user.py @@ -8,6 +8,7 @@ from sirius.coding import claiming from sirius.models.db import db from sirius.models import hardware + class User(db.Model): id = db.Column(db.Integer, primary_key=True) created = db.Column(db.DateTime, default=datetime.datetime.utcnow) @@ -70,6 +71,6 @@ class TwitterOAuth(db.Model): TwitterOAuth.user = db.relationship( User, - primaryjoin=TwitterOAuth.user_id==User.id, + primaryjoin=TwitterOAuth.user_id == User.id, backref=db.backref('twitteroauth', lazy='dynamic'), ) diff --git a/sirius/protocol/messages.py b/sirius/protocol/messages.py index 8fe1577..8668976 100644 --- a/sirius/protocol/messages.py +++ b/sirius/protocol/messages.py @@ -64,7 +64,7 @@ DeviceCommandResponse = namedtuple('DeviceCommandResponse', [ SetDeliveryAndPrint = namedtuple('SetDeliveryAndPrint', [ 'device_address', - 'pixels', # 384 pixels wide + 'pixels', # 384 pixels wide ]) SetDelivery = namedtuple('SetDelivery', [ @@ -73,7 +73,7 @@ SetDelivery = namedtuple('SetDelivery', [ ]) SetLogLevel = namedtuple('SetLogLevel', [ - 'level', # lower-case string, e.g. 'critical' + 'level', # lower-case string, e.g. 'critical' ]) EnableCloudLogging = namedtuple('EnableCloudLogging', []) @@ -83,11 +83,11 @@ DisableCloudLogging = namedtuple('DisableCloudLogging', []) Leave = namedtuple('Leave', []) Form = namedtuple('Form', [ - 'channels', # list of numbers + 'channels', # list of numbers ]) Pjoin = namedtuple('Pjoin', [ - 'duration', # int + 'duration', # int ]) Restart = namedtuple('Restart', []) @@ -95,11 +95,11 @@ Restart = namedtuple('Restart', []) Reboot = namedtuple('Reboot', []) SetCommandPollFrequency = namedtuple('SetCommandPollFrequency', [ - 'frequency', # int in seconds, clamped to [10, 30] + 'frequency', # int in seconds, clamped to [10, 30] ]) SetBridgeLogFrequency = namedtuple('SetBridgeLogFrequency', [ - 'frequency', # int in seconds, clamped to [10, 30] + 'frequency', # int in seconds, clamped to [10, 30] ]) EnableSlowEvents = namedtuple('EnableSlowEvents', []) diff --git a/sirius/protocol/protocol_loop.py b/sirius/protocol/protocol_loop.py index a689d81..72682d5 100644 --- a/sirius/protocol/protocol_loop.py +++ b/sirius/protocol/protocol_loop.py @@ -124,7 +124,7 @@ def _decoder_loop(ws): while True: raw_data = ws.receive() if raw_data == '': - break # websocket closed by client + break # websocket closed by client try: data = json.loads(raw_data) @@ -150,7 +150,7 @@ def _accept_step(x, bridge_state): bridge_state.connected_devices.discard(x.device_address) elif type(x) == messages.BridgeLog: - pass # TODO - write log to a place + pass # TODO - write log to a place elif type(x) == messages.EncryptionKeyRequired: hardware.Printer.phone_home(x.device_address) diff --git a/sirius/web/login.py b/sirius/web/login.py index bb71238..bf4c705 100644 --- a/sirius/web/login.py +++ b/sirius/web/login.py @@ -4,6 +4,7 @@ from sirius.models import user manager = login.LoginManager() + @manager.user_loader def load_user(user_id): return user.User.query.get(user_id) diff --git a/sirius/web/test_claiming.py b/sirius/web/test_claiming.py index 6d14cbd..5b43103 100644 --- a/sirius/web/test_claiming.py +++ b/sirius/web/test_claiming.py @@ -7,6 +7,7 @@ from sirius.models import hardware from sirius.models.db import db from sirius.web import webapp + class Base(testing.TestCase): def create_app(self): diff --git a/sirius/web/webapp.py b/sirius/web/webapp.py index 3f4a705..0b0ab47 100644 --- a/sirius/web/webapp.py +++ b/sirius/web/webapp.py @@ -15,7 +15,7 @@ from sirius.protocol import protocol_loop from sirius import stats from sirius import config - # Import models so they get picked up for migrations. +# Import models so they get picked up for migrations. from sirius.models import db from sirius.models import hardware from sirius.models import user