parent
2c3221810e
commit
73b2ea6c85
@ -1,12 +1,12 @@
|
||||
from trezor.crypto import hashlib
|
||||
|
||||
knownapps = {
|
||||
hashlib.sha256(b'https://bitbucket.org').digest() : 'Bitbucket',
|
||||
hashlib.sha256(b'https://www.dropbox.com/u2f-app-id.json').digest() : 'Dropbox',
|
||||
hashlib.sha256(b'https://www.fastmail.com').digest() : 'FastMail',
|
||||
hashlib.sha256(b'https://github.com/u2f/trusted_facets').digest() : 'GitHub',
|
||||
hashlib.sha256(b'https://gitlab.com').digest() : 'GitLab',
|
||||
hashlib.sha256(b'https://www.gstatic.com/securitykey/origins.json').digest() : 'Google',
|
||||
hashlib.sha256(b'https://slushpool.com/static/security/u2f.json').digest() : 'Slush Pool',
|
||||
hashlib.sha256(b'https://demo.yubico.com').digest() : 'Yubico U2F Demo',
|
||||
hashlib.sha256(b'https://bitbucket.org').digest(): 'Bitbucket',
|
||||
hashlib.sha256(b'https://www.dropbox.com/u2f-app-id.json').digest(): 'Dropbox',
|
||||
hashlib.sha256(b'https://www.fastmail.com').digest(): 'FastMail',
|
||||
hashlib.sha256(b'https://github.com/u2f/trusted_facets').digest(): 'GitHub',
|
||||
hashlib.sha256(b'https://gitlab.com').digest(): 'GitLab',
|
||||
hashlib.sha256(b'https://www.gstatic.com/securitykey/origins.json').digest(): 'Google',
|
||||
hashlib.sha256(b'https://slushpool.com/static/security/u2f.json').digest(): 'Slush Pool',
|
||||
hashlib.sha256(b'https://demo.yubico.com').digest(): 'Yubico U2F Demo',
|
||||
}
|
||||
|
@ -1,10 +1,9 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class Address(p.MessageType):
|
||||
FIELDS = {
|
||||
1: ('address', p.UnicodeType, 0), # required
|
||||
1: ('address', p.UnicodeType, 0), # required
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 30
|
||||
|
@ -1,7 +1,6 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class ButtonAck(p.MessageType):
|
||||
MESSAGE_WIRE_TYPE = 27
|
||||
|
@ -1,7 +1,6 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class Cancel(p.MessageType):
|
||||
MESSAGE_WIRE_TYPE = 20
|
||||
|
@ -1,7 +1,6 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class ClearSession(p.MessageType):
|
||||
MESSAGE_WIRE_TYPE = 24
|
||||
|
@ -1,17 +1,16 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class CoinType(p.MessageType):
|
||||
FIELDS = {
|
||||
1: ('coin_name', p.UnicodeType, 0),
|
||||
2: ('coin_shortcut', p.UnicodeType, 0),
|
||||
3: ('address_type', p.UVarintType, 0), # default=0
|
||||
3: ('address_type', p.UVarintType, 0), # default=0
|
||||
4: ('maxfee_kb', p.UVarintType, 0),
|
||||
5: ('address_type_p2sh', p.UVarintType, 0), # default=5
|
||||
5: ('address_type_p2sh', p.UVarintType, 0), # default=5
|
||||
8: ('signed_message_header', p.UnicodeType, 0),
|
||||
9: ('xpub_magic', p.UVarintType, 0), # default=76067358
|
||||
10: ('xprv_magic', p.UVarintType, 0), # default=76066276
|
||||
9: ('xpub_magic', p.UVarintType, 0), # default=76067358
|
||||
10: ('xprv_magic', p.UVarintType, 0), # default=76066276
|
||||
11: ('segwit', p.BoolType, 0),
|
||||
}
|
||||
|
@ -1,10 +1,9 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class DebugLinkDecision(p.MessageType):
|
||||
FIELDS = {
|
||||
1: ('yes_no', p.BoolType, 0), # required
|
||||
1: ('yes_no', p.BoolType, 0), # required
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 100
|
||||
|
@ -1,7 +1,6 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class DebugLinkGetState(p.MessageType):
|
||||
MESSAGE_WIRE_TYPE = 101
|
||||
|
@ -1,7 +1,6 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class DebugLinkStop(p.MessageType):
|
||||
MESSAGE_WIRE_TYPE = 103
|
||||
|
@ -1,14 +1,13 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class EncryptMessage(p.MessageType):
|
||||
FIELDS = {
|
||||
1: ('pubkey', p.BytesType, 0),
|
||||
2: ('message', p.BytesType, 0),
|
||||
3: ('display_only', p.BoolType, 0),
|
||||
4: ('address_n', p.UVarintType, p.FLAG_REPEATED),
|
||||
5: ('coin_name', p.UnicodeType, 0), # default=u'Bitcoin'
|
||||
5: ('coin_name', p.UnicodeType, 0), # default=u'Bitcoin'
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 49
|
||||
|
@ -1,10 +1,9 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class Entropy(p.MessageType):
|
||||
FIELDS = {
|
||||
1: ('entropy', p.BytesType, 0), # required
|
||||
1: ('entropy', p.BytesType, 0), # required
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 10
|
||||
|
@ -1,7 +1,6 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class EntropyRequest(p.MessageType):
|
||||
MESSAGE_WIRE_TYPE = 35
|
||||
|
@ -1,12 +1,11 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class EstimateTxSize(p.MessageType):
|
||||
FIELDS = {
|
||||
1: ('outputs_count', p.UVarintType, 0), # required
|
||||
2: ('inputs_count', p.UVarintType, 0), # required
|
||||
3: ('coin_name', p.UnicodeType, 0), # default=u'Bitcoin'
|
||||
1: ('outputs_count', p.UVarintType, 0), # required
|
||||
2: ('inputs_count', p.UVarintType, 0), # required
|
||||
3: ('coin_name', p.UnicodeType, 0), # default=u'Bitcoin'
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 43
|
||||
|
@ -1,10 +1,9 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class EthereumAddress(p.MessageType):
|
||||
FIELDS = {
|
||||
1: ('address', p.BytesType, 0), # required
|
||||
1: ('address', p.BytesType, 0), # required
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 57
|
||||
|
@ -1,7 +1,6 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class FirmwareErase(p.MessageType):
|
||||
MESSAGE_WIRE_TYPE = 6
|
||||
|
@ -1,11 +1,10 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class FirmwareUpload(p.MessageType):
|
||||
FIELDS = {
|
||||
1: ('payload', p.BytesType, 0), # required
|
||||
1: ('payload', p.BytesType, 0), # required
|
||||
2: ('hash', p.BytesType, 0),
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 7
|
||||
|
@ -1,15 +1,14 @@
|
||||
# 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),
|
||||
2: ('coin_name', p.UnicodeType, 0), # default=u'Bitcoin'
|
||||
2: ('coin_name', p.UnicodeType, 0), # default=u'Bitcoin'
|
||||
3: ('show_display', p.BoolType, 0),
|
||||
4: ('multisig', MultisigRedeemScriptType, 0),
|
||||
5: ('script_type', p.UVarintType, 0), # default=0
|
||||
5: ('script_type', p.UVarintType, 0), # default=0
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 29
|
||||
|
@ -1,10 +1,9 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class GetEntropy(p.MessageType):
|
||||
FIELDS = {
|
||||
1: ('size', p.UVarintType, 0), # required
|
||||
1: ('size', p.UVarintType, 0), # required
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 9
|
||||
|
@ -1,7 +1,6 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class GetFeatures(p.MessageType):
|
||||
MESSAGE_WIRE_TYPE = 55
|
||||
|
@ -1,13 +1,12 @@
|
||||
# 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),
|
||||
2: ('ecdsa_curve_name', p.UnicodeType, 0),
|
||||
3: ('show_display', p.BoolType, 0),
|
||||
4: ('coin_name', p.UnicodeType, 0), # default=u'Bitcoin'
|
||||
4: ('coin_name', p.UnicodeType, 0), # default=u'Bitcoin'
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 11
|
||||
|
@ -1,11 +1,10 @@
|
||||
# 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
|
||||
1: ('node', HDNodeType, 0), # required
|
||||
2: ('address_n', p.UVarintType, p.FLAG_REPEATED),
|
||||
}
|
||||
|
@ -1,14 +1,13 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class HDNodeType(p.MessageType):
|
||||
FIELDS = {
|
||||
1: ('depth', p.UVarintType, 0), # required
|
||||
2: ('fingerprint', p.UVarintType, 0), # required
|
||||
3: ('child_num', p.UVarintType, 0), # required
|
||||
4: ('chain_code', p.BytesType, 0), # required
|
||||
1: ('depth', p.UVarintType, 0), # required
|
||||
2: ('fingerprint', p.UVarintType, 0), # required
|
||||
3: ('child_num', p.UVarintType, 0), # required
|
||||
4: ('chain_code', p.BytesType, 0), # required
|
||||
5: ('private_key', p.BytesType, 0),
|
||||
6: ('public_key', p.BytesType, 0),
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class Initialize(p.MessageType):
|
||||
MESSAGE_WIRE_TYPE = 0
|
||||
|
@ -1,10 +1,9 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class PassphraseAck(p.MessageType):
|
||||
FIELDS = {
|
||||
1: ('passphrase', p.UnicodeType, 0), # required
|
||||
1: ('passphrase', p.UnicodeType, 0), # required
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 42
|
||||
|
@ -1,7 +1,6 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class PassphraseRequest(p.MessageType):
|
||||
MESSAGE_WIRE_TYPE = 41
|
||||
|
@ -1,10 +1,9 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class PinMatrixAck(p.MessageType):
|
||||
FIELDS = {
|
||||
1: ('pin', p.UnicodeType, 0), # required
|
||||
1: ('pin', p.UnicodeType, 0), # required
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 19
|
||||
|
@ -1,12 +1,11 @@
|
||||
# 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
|
||||
1: ('node', HDNodeType, 0), # required
|
||||
2: ('xpub', p.UnicodeType, 0),
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 12
|
||||
|
@ -1,12 +1,11 @@
|
||||
# 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),
|
||||
2: ('message', p.BytesType, 0), # required
|
||||
3: ('coin_name', p.UnicodeType, 0), # default=u'Bitcoin'
|
||||
2: ('message', p.BytesType, 0), # required
|
||||
3: ('coin_name', p.UnicodeType, 0), # default=u'Bitcoin'
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 38
|
||||
|
@ -1,14 +1,13 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class SignTx(p.MessageType):
|
||||
FIELDS = {
|
||||
1: ('outputs_count', p.UVarintType, 0), # required
|
||||
2: ('inputs_count', p.UVarintType, 0), # required
|
||||
3: ('coin_name', p.UnicodeType, 0), # default=u'Bitcoin'
|
||||
4: ('version', p.UVarintType, 0), # default=1
|
||||
5: ('lock_time', p.UVarintType, 0), # default=0
|
||||
1: ('outputs_count', p.UVarintType, 0), # required
|
||||
2: ('inputs_count', p.UVarintType, 0), # required
|
||||
3: ('coin_name', p.UnicodeType, 0), # default=u'Bitcoin'
|
||||
4: ('version', p.UVarintType, 0), # default=1
|
||||
5: ('lock_time', p.UVarintType, 0), # default=0
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 15
|
||||
|
@ -1,18 +1,17 @@
|
||||
# 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),
|
||||
2: ('outputs', TxOutputType, p.FLAG_REPEATED),
|
||||
3: ('transactions', TransactionType, p.FLAG_REPEATED),
|
||||
4: ('coin_name', p.UnicodeType, 0), # default=u'Bitcoin'
|
||||
5: ('version', p.UVarintType, 0), # default=1
|
||||
6: ('lock_time', p.UVarintType, 0), # default=0
|
||||
4: ('coin_name', p.UnicodeType, 0), # default=u'Bitcoin'
|
||||
5: ('version', p.UVarintType, 0), # default=1
|
||||
6: ('lock_time', p.UVarintType, 0), # default=0
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 16
|
||||
|
@ -1,17 +1,16 @@
|
||||
# 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),
|
||||
2: ('prev_hash', p.BytesType, 0), # required
|
||||
3: ('prev_index', p.UVarintType, 0), # required
|
||||
2: ('prev_hash', p.BytesType, 0), # required
|
||||
3: ('prev_index', p.UVarintType, 0), # required
|
||||
4: ('script_sig', p.BytesType, 0),
|
||||
5: ('sequence', p.UVarintType, 0), # default=4294967295
|
||||
6: ('script_type', p.UVarintType, 0), # default=0
|
||||
5: ('sequence', p.UVarintType, 0), # default=4294967295
|
||||
6: ('script_type', p.UVarintType, 0), # default=0
|
||||
7: ('multisig', MultisigRedeemScriptType, 0),
|
||||
8: ('amount', p.UVarintType, 0),
|
||||
}
|
||||
|
@ -1,10 +1,9 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class TxOutputBinType(p.MessageType):
|
||||
FIELDS = {
|
||||
1: ('amount', p.UVarintType, 0), # required
|
||||
2: ('script_pubkey', p.BytesType, 0), # required
|
||||
1: ('amount', p.UVarintType, 0), # required
|
||||
2: ('script_pubkey', p.BytesType, 0), # required
|
||||
}
|
||||
|
@ -1,15 +1,14 @@
|
||||
# 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),
|
||||
2: ('address_n', p.UVarintType, p.FLAG_REPEATED),
|
||||
3: ('amount', p.UVarintType, 0), # required
|
||||
4: ('script_type', p.UVarintType, 0), # required
|
||||
3: ('amount', p.UVarintType, 0), # required
|
||||
4: ('script_type', p.UVarintType, 0), # required
|
||||
5: ('multisig', MultisigRedeemScriptType, 0),
|
||||
6: ('op_return_data', p.BytesType, 0),
|
||||
}
|
||||
|
@ -1,13 +1,12 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class VerifyMessage(p.MessageType):
|
||||
FIELDS = {
|
||||
1: ('address', p.UnicodeType, 0),
|
||||
2: ('signature', p.BytesType, 0),
|
||||
3: ('message', p.BytesType, 0),
|
||||
4: ('coin_name', p.UnicodeType, 0), # default=u'Bitcoin'
|
||||
4: ('coin_name', p.UnicodeType, 0), # default=u'Bitcoin'
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 39
|
||||
|
@ -1,7 +1,6 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class WipeDevice(p.MessageType):
|
||||
MESSAGE_WIRE_TYPE = 5
|
||||
|
@ -1,10 +1,9 @@
|
||||
# Automatically generated by pb2py
|
||||
from micropython import const
|
||||
|
||||
import protobuf as p
|
||||
|
||||
|
||||
class WordAck(p.MessageType):
|
||||
FIELDS = {
|
||||
1: ('word', p.UnicodeType, 0), # required
|
||||
1: ('word', p.UnicodeType, 0), # required
|
||||
}
|
||||
MESSAGE_WIRE_TYPE = 47
|
||||
|
@ -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)
|
Loading…
Reference in new issue