1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 07:28:10 +00:00

travis: add make style (using flake8)

This commit is contained in:
Pavol Rusnak 2017-06-13 19:35:14 +02:00
parent 2c3221810e
commit 73b2ea6c85
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
98 changed files with 371 additions and 255 deletions

View File

@ -38,6 +38,7 @@ script:
- test "$GOAL" != "unix" || make build_unix TREZOR_NOUI=1
- test "$GOAL" != "unix" || make test
- test "$GOAL" != "unix" || make testpy
- test "$GOAL" != "unix" || make style
notifications:
webhooks:

View File

@ -50,6 +50,9 @@ testpy: ## run selected unit tests from python-trezor
pylint: ## run pylint on application sources
pylint --rcfile=pylint.rc -E $(shell find src -name *.py)
style: ## run code style check on application sources
flake8 --ignore=E221,E241,E402,E501,F401 $(shell find src -name *.py)
## build commands:
build: build_boardloader build_bootloader build_firmware build_unix build_cross ## build all

View File

@ -26,6 +26,7 @@ def strip(address_type, raw_address):
l = length(address_type)
return raw_address[l:]
def split(coin, raw_address):
l = None
for f in ['', '_p2sh', '_p2wpkh', '_p2wsh']:

View File

@ -2,6 +2,7 @@ from trezor.crypto.hashlib import sha256
from apps.wallet.sign_tx.signing import HashWriter, write_varint
def message_digest(coin, message):
h = HashWriter(sha256)

View File

@ -9,5 +9,6 @@ def dispatch_EthereumGetAddress(*args, **kwargs):
from .ethereum_get_address import layout_ethereum_get_address
return layout_ethereum_get_address(*args, **kwargs)
def boot():
register(EthereumGetAddress, protobuf_workflow, dispatch_EthereumGetAddress)

View File

@ -415,8 +415,7 @@ class ConfirmContent(ui.Widget):
name = knownapps.knownapps[app_id]
icon = res.load('apps/fido_u2f/res/u2f_%s.toif' % name.lower().replace(' ', '_'))
else:
name = ubinascii.hexlify(app_id[:4]) + '...' + \
ubinascii.hexlify(app_id[-4:])
name = '%s...%s' % (ubinascii.hexlify(app_id[:4]), ubinascii.hexlify(app_id[-4:]))
icon = res.load('apps/fido_u2f/res/u2f_unknown.toif')
self.app_name = name
self.app_icon = icon

View File

@ -7,7 +7,6 @@ async def layout_get_entropy(session_id, msg):
from trezor.messages.Entropy import Entropy
from trezor.crypto import random
l = min(msg.size, 1024)
await _show_entropy(session_id)

View File

@ -53,7 +53,6 @@ def sign_challenge(seckey: bytes,
from trezor.crypto.curve import ed25519
from ..common.signverify import message_digest
if sigtype == 'gpg':
data = challenge_hidden
elif sigtype == 'ssh':
@ -63,8 +62,7 @@ def sign_challenge(seckey: bytes,
data = challenge_hidden
else:
# sigtype is coin
challenge = sha256(challenge_hidden).digest() + \
sha256(challenge_visual).digest()
challenge = sha256(challenge_hidden).digest() + sha256(challenge_visual).digest()
data = message_digest(sigtype, challenge)
if curve == 'secp256k1':

View File

@ -162,7 +162,7 @@ class MessageType(Type):
@classmethod
async def load(cls, source=None, target=None):
if target is None:
target = build_protobuf_message(cls)
target = build_message(cls)
if source is None:
source = StreamReader()
try:

View File

@ -48,6 +48,7 @@ class __dummy:
def __getitem__(self, *args):
return object
__t = __dummy()
for __n in __names_get:
@ -64,4 +65,5 @@ def TypeVar(*args):
def NewType(*args):
return lambda x: x
TYPE_CHECKING = False

View File

@ -59,25 +59,23 @@ def decode(string: str) -> bytes:
return bytes((b for b in reversed(result + [0] * (origlen - newlen))))
def encode_check(data: bytes, digestfunc=None) -> str:
def _dsha256_32(data: bytes) -> bytes:
from .hashlib import sha256
return sha256(sha256(data).digest()).digest()[:4]
def encode_check(data: bytes, digestfunc=_dsha256_32) -> str:
'''
Convert bytes to base58 encoded string, append checksum.
'''
if digestfunc is None:
from .hashlib import sha256
digestfunc = lambda x: sha256(sha256(x).digest()).digest()[:4]
return encode(data + digestfunc(data))
def decode_check(string: str, digestfunc=None) -> bytes:
def decode_check(string: str, digestfunc=_dsha256_32) -> bytes:
'''
Convert base58 encoded string to bytes and verify checksum.
'''
result = decode(string)
if digestfunc is None:
from .hashlib import sha256
digestfunc = lambda x: sha256(sha256(x).digest()).digest()[:4]
digestlen = len(digestfunc(b''))
result, check = result[:-digestlen], result[-digestlen:]

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class Address(p.MessageType):
FIELDS = {
1: ('address', p.UnicodeType, 0), # required

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class ApplySettings(p.MessageType):
FIELDS = {
1: ('language', p.UnicodeType, 0),

View File

@ -1,7 +1,6 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class ButtonAck(p.MessageType):
MESSAGE_WIRE_TYPE = 27

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class ButtonRequest(p.MessageType):
FIELDS = {
1: ('code', p.UVarintType, 0),

View File

@ -1,7 +1,6 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class Cancel(p.MessageType):
MESSAGE_WIRE_TYPE = 20

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class ChangePin(p.MessageType):
FIELDS = {
1: ('remove', p.BoolType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class CipherKeyValue(p.MessageType):
FIELDS = {
1: ('address_n', p.UVarintType, p.FLAG_REPEATED),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class CipheredKeyValue(p.MessageType):
FIELDS = {
1: ('value', p.BytesType, 0),

View File

@ -1,7 +1,6 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class ClearSession(p.MessageType):
MESSAGE_WIRE_TYPE = 24

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class CoinType(p.MessageType):
FIELDS = {
1: ('coin_name', p.UnicodeType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class DebugLinkDecision(p.MessageType):
FIELDS = {
1: ('yes_no', p.BoolType, 0), # required

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class DebugLinkFlashErase(p.MessageType):
FIELDS = {
1: ('sector', p.UVarintType, 0),

View File

@ -1,7 +1,6 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class DebugLinkGetState(p.MessageType):
MESSAGE_WIRE_TYPE = 101

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class DebugLinkLog(p.MessageType):
FIELDS = {
1: ('level', p.UVarintType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class DebugLinkMemory(p.MessageType):
FIELDS = {
1: ('memory', p.BytesType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class DebugLinkMemoryRead(p.MessageType):
FIELDS = {
1: ('address', p.UVarintType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class DebugLinkMemoryWrite(p.MessageType):
FIELDS = {
1: ('address', p.UVarintType, 0),

View File

@ -1,9 +1,8 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
from .HDNodeType import HDNodeType
class DebugLinkState(p.MessageType):
FIELDS = {
1: ('layout', p.BytesType, 0),

View File

@ -1,7 +1,6 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class DebugLinkStop(p.MessageType):
MESSAGE_WIRE_TYPE = 103

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class DecryptMessage(p.MessageType):
FIELDS = {
1: ('address_n', p.UVarintType, p.FLAG_REPEATED),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class DecryptedMessage(p.MessageType):
FIELDS = {
1: ('message', p.BytesType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class ECDHSessionKey(p.MessageType):
FIELDS = {
1: ('session_key', p.BytesType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class EncryptMessage(p.MessageType):
FIELDS = {
1: ('pubkey', p.BytesType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class EncryptedMessage(p.MessageType):
FIELDS = {
1: ('nonce', p.BytesType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class Entropy(p.MessageType):
FIELDS = {
1: ('entropy', p.BytesType, 0), # required

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class EntropyAck(p.MessageType):
FIELDS = {
1: ('entropy', p.BytesType, 0),

View File

@ -1,7 +1,6 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class EntropyRequest(p.MessageType):
MESSAGE_WIRE_TYPE = 35

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class EstimateTxSize(p.MessageType):
FIELDS = {
1: ('outputs_count', p.UVarintType, 0), # required

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class EthereumAddress(p.MessageType):
FIELDS = {
1: ('address', p.BytesType, 0), # required

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class EthereumGetAddress(p.MessageType):
FIELDS = {
1: ('address_n', p.UVarintType, p.FLAG_REPEATED),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class EthereumSignTx(p.MessageType):
FIELDS = {
1: ('address_n', p.UVarintType, p.FLAG_REPEATED),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class EthereumTxAck(p.MessageType):
FIELDS = {
1: ('data_chunk', p.BytesType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class EthereumTxRequest(p.MessageType):
FIELDS = {
1: ('data_length', p.UVarintType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class Failure(p.MessageType):
FIELDS = {
1: ('code', p.UVarintType, 0),

View File

@ -1,9 +1,8 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
from .CoinType import CoinType
class Features(p.MessageType):
FIELDS = {
1: ('vendor', p.UnicodeType, 0),

View File

@ -1,7 +1,6 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class FirmwareErase(p.MessageType):
MESSAGE_WIRE_TYPE = 6

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class FirmwareRequest(p.MessageType):
FIELDS = {
1: ('offset', p.UVarintType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class FirmwareUpload(p.MessageType):
FIELDS = {
1: ('payload', p.BytesType, 0), # required

View File

@ -1,9 +1,8 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
from .MultisigRedeemScriptType import MultisigRedeemScriptType
class GetAddress(p.MessageType):
FIELDS = {
1: ('address_n', p.UVarintType, p.FLAG_REPEATED),

View File

@ -1,9 +1,8 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
from .IdentityType import IdentityType
class GetECDHSessionKey(p.MessageType):
FIELDS = {
1: ('identity', IdentityType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class GetEntropy(p.MessageType):
FIELDS = {
1: ('size', p.UVarintType, 0), # required

View File

@ -1,7 +1,6 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class GetFeatures(p.MessageType):
MESSAGE_WIRE_TYPE = 55

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class GetPublicKey(p.MessageType):
FIELDS = {
1: ('address_n', p.UVarintType, p.FLAG_REPEATED),

View File

@ -1,9 +1,8 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
from .HDNodeType import HDNodeType
class HDNodePathType(p.MessageType):
FIELDS = {
1: ('node', HDNodeType, 0), # required

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class HDNodeType(p.MessageType):
FIELDS = {
1: ('depth', p.UVarintType, 0), # required

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class IdentityType(p.MessageType):
FIELDS = {
1: ('proto', p.UnicodeType, 0),

View File

@ -1,7 +1,6 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class Initialize(p.MessageType):
MESSAGE_WIRE_TYPE = 0

View File

@ -1,9 +1,8 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
from .HDNodeType import HDNodeType
class LoadDevice(p.MessageType):
FIELDS = {
1: ('mnemonic', p.UnicodeType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class MessageSignature(p.MessageType):
FIELDS = {
1: ('address', p.UnicodeType, 0),

View File

@ -1,9 +1,8 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
from .HDNodePathType import HDNodePathType
class MultisigRedeemScriptType(p.MessageType):
FIELDS = {
1: ('pubkeys', HDNodePathType, p.FLAG_REPEATED),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class PassphraseAck(p.MessageType):
FIELDS = {
1: ('passphrase', p.UnicodeType, 0), # required

View File

@ -1,7 +1,6 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class PassphraseRequest(p.MessageType):
MESSAGE_WIRE_TYPE = 41

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class PinMatrixAck(p.MessageType):
FIELDS = {
1: ('pin', p.UnicodeType, 0), # required

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class PinMatrixRequest(p.MessageType):
FIELDS = {
1: ('type', p.UVarintType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class Ping(p.MessageType):
FIELDS = {
1: ('message', p.UnicodeType, 0),

View File

@ -1,9 +1,8 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
from .HDNodeType import HDNodeType
class PublicKey(p.MessageType):
FIELDS = {
1: ('node', HDNodeType, 0), # required

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class RecoveryDevice(p.MessageType):
FIELDS = {
1: ('word_count', p.UVarintType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class ResetDevice(p.MessageType):
FIELDS = {
1: ('display_random', p.BoolType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class SetU2FCounter(p.MessageType):
FIELDS = {
1: ('u2f_counter', p.UVarintType, 0),

View File

@ -1,9 +1,8 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
from .IdentityType import IdentityType
class SignIdentity(p.MessageType):
FIELDS = {
1: ('identity', IdentityType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class SignMessage(p.MessageType):
FIELDS = {
1: ('address_n', p.UVarintType, p.FLAG_REPEATED),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class SignTx(p.MessageType):
FIELDS = {
1: ('outputs_count', p.UVarintType, 0), # required

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class SignedIdentity(p.MessageType):
FIELDS = {
1: ('address', p.UnicodeType, 0),

View File

@ -1,11 +1,10 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
from .TxInputType import TxInputType
from .TxOutputType import TxOutputType
from .TransactionType import TransactionType
class SimpleSignTx(p.MessageType):
FIELDS = {
1: ('inputs', TxInputType, p.FLAG_REPEATED),

View File

@ -1,9 +1,8 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
from .HDNodeType import HDNodeType
class Storage(p.MessageType):
FIELDS = {
1: ('version', p.UVarintType, 0), # required

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class Success(p.MessageType):
FIELDS = {
1: ('message', p.UnicodeType, 0),

View File

@ -1,11 +1,10 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
from .TxInputType import TxInputType
from .TxOutputBinType import TxOutputBinType
from .TxOutputType import TxOutputType
class TransactionType(p.MessageType):
FIELDS = {
1: ('version', p.UVarintType, 0),

View File

@ -1,9 +1,8 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
from .TransactionType import TransactionType
class TxAck(p.MessageType):
FIELDS = {
1: ('tx', TransactionType, 0),

View File

@ -1,9 +1,8 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
from .MultisigRedeemScriptType import MultisigRedeemScriptType
class TxInputType(p.MessageType):
FIELDS = {
1: ('address_n', p.UVarintType, p.FLAG_REPEATED),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class TxOutputBinType(p.MessageType):
FIELDS = {
1: ('amount', p.UVarintType, 0), # required

View File

@ -1,9 +1,8 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
from .MultisigRedeemScriptType import MultisigRedeemScriptType
class TxOutputType(p.MessageType):
FIELDS = {
1: ('address', p.UnicodeType, 0),

View File

@ -1,10 +1,9 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
from .TxRequestDetailsType import TxRequestDetailsType
from .TxRequestSerializedType import TxRequestSerializedType
class TxRequest(p.MessageType):
FIELDS = {
1: ('request_type', p.UVarintType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class TxRequestDetailsType(p.MessageType):
FIELDS = {
1: ('request_index', p.UVarintType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class TxRequestSerializedType(p.MessageType):
FIELDS = {
1: ('signature_index', p.UVarintType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class TxSize(p.MessageType):
FIELDS = {
1: ('tx_size', p.UVarintType, 0),

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class VerifyMessage(p.MessageType):
FIELDS = {
1: ('address', p.UnicodeType, 0),

View File

@ -1,7 +1,6 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class WipeDevice(p.MessageType):
MESSAGE_WIRE_TYPE = 5

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class WordAck(p.MessageType):
FIELDS = {
1: ('word', p.UnicodeType, 0), # required

View File

@ -1,8 +1,7 @@
# Automatically generated by pb2py
from micropython import const
import protobuf as p
class WordRequest(p.MessageType):
FIELDS = {
1: ('type', p.UVarintType, 0),

View File

@ -20,4 +20,5 @@ def gettext(message):
'''
return message
_ = gettext

View File

@ -42,9 +42,9 @@ class Swipe(ui.Widget):
# check if its vertical scroll up
velya = abs(pdy / td) if td > 0 else 1
ratio = int(pdxa / pdya * 100) if pdya > 0 else 100
if (velya >= _SWIPE_VELOCITY_THRESHOLD
and pdya >= _SWIPE_DISTANCE_THRESHOLD
and ratio <= _SWIPE_RATIO_THRESHOLD):
if (velya >= _SWIPE_VELOCITY_THRESHOLD and
pdya >= _SWIPE_DISTANCE_THRESHOLD and
ratio <= _SWIPE_RATIO_THRESHOLD):
light = ui.display.backlight()
if light > self.light_target:
light -= 5
@ -68,17 +68,17 @@ class Swipe(ui.Widget):
# Horizontal direction
velxa = abs(pdx / td)
ratio = int(pdya / pdxa * 100) if pdxa > 0 else 100
if (velxa >= _SWIPE_VELOCITY_THRESHOLD
and pdxa >= _SWIPE_DISTANCE_THRESHOLD
and ratio <= _SWIPE_RATIO_THRESHOLD):
if (velxa >= _SWIPE_VELOCITY_THRESHOLD and
pdxa >= _SWIPE_DISTANCE_THRESHOLD and
ratio <= _SWIPE_RATIO_THRESHOLD):
return SWIPE_RIGHT if pdx > 0 else SWIPE_LEFT
else:
# Vertical direction
velya = abs(pdy / td)
ratio = int(pdxa / pdya * 100) if pdya > 0 else 100
if (velya >= _SWIPE_VELOCITY_THRESHOLD
and pdya >= _SWIPE_DISTANCE_THRESHOLD
and ratio <= _SWIPE_RATIO_THRESHOLD):
if (velya >= _SWIPE_VELOCITY_THRESHOLD and
pdya >= _SWIPE_DISTANCE_THRESHOLD and
ratio <= _SWIPE_RATIO_THRESHOLD):
if pdy < 0:
ui.display.backlight(self.light_origin)
return SWIPE_DOWN if pdy > 0 else SWIPE_UP

View File

@ -3,6 +3,8 @@ CURDIR=$(pwd)
mkdir -p $CURDIR/pb2/
echo > $CURDIR/pb2/__init__.py
mkdir -p ../src/trezor/messages
INDEX=../src/trezor/messages/wire_types.py
rm -f $INDEX
echo '# Automatically generated by pb2py' >> $INDEX
@ -18,7 +20,7 @@ done
for i in types messages storage ; do
# Convert google protobuf library to trezor's internal format
cd $CURDIR
../../trezor-common/tools/pb2py -m -p $CURDIR -i $INDEX $i ../src/trezor/messages/
./pb2py -m -p $CURDIR -i $INDEX $i ../src/trezor/messages/
done
rm -rf $CURDIR/pb2/

189
tools/pb2py Executable file
View File

@ -0,0 +1,189 @@
#!/usr/bin/env python
# Converts Google's protobuf python definitions of TREZOR wire messages
# to plain-python objects as used in TREZOR Core and python-trezor
import sys
import os
import argparse
from google.protobuf.internal.enum_type_wrapper import EnumTypeWrapper
def process_type(t, cls, msg_id, indexfile, is_upy):
print(" * type %s" % t)
imports = ['import protobuf as p']
out = ["", "", "class %s(p.MessageType):" % t, ]
if cls.DESCRIPTOR.fields_by_name:
out.append(" FIELDS = {")
elif msg_id is None:
out.append(" pass")
for v in sorted(cls.DESCRIPTOR.fields_by_name.values(), key=lambda x: x.number):
number = v.number
fieldname = v.name
type = None
repeated = v.label == 3
required = v.label == 2
# print v.has_default_value, v.default_value
if v.type in (4, 13, 14):
# TYPE_UINT64 = 4
# TYPE_UINT32 = 13
# TYPE_ENUM = 14
type = 'p.UVarintType'
elif v.type == 9:
# TYPE_STRING = 9
type = 'p.UnicodeType'
elif v.type == 8:
# TYPE_BOOL = 8
type = 'p.BoolType'
elif v.type == 12:
# TYPE_BYTES = 12
type = 'p.BytesType'
elif v.type == 11:
# TYPE_MESSAGE = 1
type = v.message_type.name
imports.append("from .%s import %s" %
(v.message_type.name, v.message_type.name))
else:
raise Exception("Unknown field type %s for field %s" %
(v.type, fieldname))
if required:
comment = ' # required'
elif v.has_default_value:
comment = ' # default=%s' % repr(v.default_value)
else:
comment = ''
if repeated:
flags = 'p.FLAG_REPEATED'
else:
flags = '0'
out.append(" %d: ('%s', %s, %s),%s" %
(number, fieldname, type, flags, comment))
# print fieldname, number, type, repeated, comment
# print v.__dict__
# print v.CPPTYPE_STRING
# print v.LABEL_REPEATED
# print v.enum_type
# v.has_default_value, v.default_value
# v.label == 3 # repeated
# print v.number
if cls.DESCRIPTOR.fields_by_name:
out.append(" }")
if msg_id is not None:
out.append(" MESSAGE_WIRE_TYPE = %d" % msg_id)
if indexfile is not None:
if is_upy:
indexfile.write("%s = const(%d)\n" % (t, msg_id))
else:
indexfile.write("%s = %d\n" % (t, msg_id))
return imports + out
def process_enum(t, cls, is_upy):
out = []
if is_upy:
out += ("from micropython import const", "")
print(" * enum %s" % t)
for k, v in cls.items():
# Remove type name from the beginning of the constant
# For example "PinMatrixRequestType_Current" -> "Current"
if k.startswith("%s_" % t):
k = k.replace("%s_" % t, '')
# If type ends with *Type, but constant use type name without *Type, remove it too :)
# For example "ButtonRequestType & ButtonRequest_Other" => "Other"
if t.endswith("Type") and k.startswith("%s_" % t.replace("Type", '')):
k = k.replace("%s_" % t.replace("Type", ''), '')
if is_upy:
out.append("%s = const(%s)" % (k, v))
else:
out.append("%s = %s" % (k, v))
return out
def find_msg_type(msg_types, t):
for k, v in msg_types:
msg_name = k.replace('MessageType_', '')
if msg_name == t:
return v
def process_module(mod, genpath, indexfile, is_upy):
print("Processing module %s" % mod.__name__)
types = dict([(name, cls)
for name, cls in mod.__dict__.items() if isinstance(cls, type)])
msg_types = __import__('pb2', globals(), locals(), [
'messages_pb2', ]).messages_pb2.MessageType.items()
for t, cls in types.items():
# Find message type for given class
msg_id = find_msg_type(msg_types, t)
out = process_type(t, cls, msg_id, indexfile, is_upy)
write_to_file(genpath, t, out)
enums = dict([(name, cls) for name, cls in mod.__dict__.items()
if isinstance(cls, EnumTypeWrapper)])
for t, cls in enums.items():
out = process_enum(t, cls, is_upy)
write_to_file(genpath, t, out)
def write_to_file(genpath, t, out):
# Write generated sourcecode to given file
f = open(os.path.join(genpath, "%s.py" % t), 'w')
out = ["# Automatically generated by pb2py"] + out
data = "\n".join(out) + "\n"
f.write(data)
f.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('modulename', type=str, help="Name of module to generate")
parser.add_argument('genpath', type=str, help="Directory for generated source code")
parser.add_argument('-i', '--indexfile', type=str, help="[optional] Generate index file of wire types")
parser.add_argument('-p', '--protopath', type=str, help="[optional] Path to search for pregenerated Google's python sources")
parser.add_argument('-m', '--micropython', action='store_true', help="Use micropython-favoured source code")
args = parser.parse_args()
if args.indexfile:
indexfile = open(args.indexfile, 'a')
else:
indexfile = None
if args.protopath:
sys.path.append(args.protopath)
# Dynamically load module from argv[1]
tmp = __import__('pb2', globals(), locals(), ['%s_pb2' % args.modulename])
mod = getattr(tmp, "%s_pb2" % args.modulename)
process_module(mod, args.genpath, indexfile, args.micropython)