Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New pyln modules. #3733

Merged
merged 18 commits into from
Jun 12, 2020
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
ca71845
pyln: add Makefile
rustyrussell May 18, 2020
31fa55e
pyln: add pyln.proto.message.
rustyrussell May 28, 2020
16297af
patch message-export-types.patch
rustyrussell Jun 3, 2020
1bff330
pyln.proto.message: use BufferedIOBase instead of bytes for binary ops.
rustyrussell Jun 4, 2020
46151ed
pyln.proto.message: expose fundamental MessageTypes as variables.
rustyrussell Jun 4, 2020
e274226
pyln: add (undocumented) u8 fundamental type.
rustyrussell Jun 4, 2020
59e2064
message: support option fields.
rustyrussell Jun 4, 2020
784d138
pyln.proto.message: separate fundamental types from other subtypes.
rustyrussell Jun 4, 2020
a4eb933
pyln: new module pyln.proto.message.bolts
rustyrussell Jun 4, 2020
42fc48f
new modules: pyln.proto.message.{bolt1,bolt2,bolt4,bolt7}
rustyrussell Jun 4, 2020
3ed6831
pyln.proto.message: support adding two namespaces.
rustyrussell Jun 4, 2020
360bcaf
pyln.proto.message: python-fluency feedback from @darosior
rustyrussell Jun 4, 2020
4a336db
pyln.proto.message: export more.
rustyrussell Jun 4, 2020
efd38a4
pyln.proto.message: allow fields with options to be missing.
rustyrussell Jun 4, 2020
bef68d3
pyln.proto.message.*: Add Makefile to do mypy checks.
rustyrussell Jun 4, 2020
851122d
pyln.proto.message.*: add type annotations.
rustyrussell Jun 12, 2020
e0d3174
pyln.proto.message: expose array types, add set_field for Message class.
rustyrussell Jun 12, 2020
5dbec8f
pyln.proto.message: fix handling of missing optional fields.
rustyrussell Jun 12, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions contrib/pyln-proto/pyln/proto/message/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ class MessageNamespace(object):
domain, such as within a given BOLT"""
def __init__(self, csv_lines=[]):
self.subtypes = {}
self.fundamentaltypes = {}
self.tlvtypes = {}
self.messagetypes = {}

# For convenience, basic types go in every namespace
for t in fundamental_types():
self.add_subtype(t)
self.add_fundamentaltype(t)

self.load_csv(csv_lines)

Expand All @@ -26,6 +27,10 @@ def add_subtype(self, t):
return ValueError('Already have {}'.format(prev))
self.subtypes[t.name] = t

def add_fundamentaltype(self, t):
assert not self.get_type(t.name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be weakened a bit to be

assert(self.get_type(t.name) is None or self.get_type(t.name) == t)

Which would no longer fail outright if we happen to add the same type multiple times.

self.fundamentaltypes[t.name] = t

def add_tlvtype(self, t):
prev = self.get_type(t.name)
if prev:
Expand All @@ -51,6 +56,11 @@ def get_msgtype_by_number(self, num):
return m
return None

def get_fundamentaltype(self, name):
if name in self.fundamentaltypes:
return self.fundamentaltypes[name]
return None
Comment on lines +74 to +76
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An idiomatic way of doing this is with the getter and default value:

return self.fundamentaltypes.get(name, None)


def get_subtype(self, name):
if name in self.subtypes:
return self.subtypes[name]
Expand All @@ -62,7 +72,9 @@ def get_tlvtype(self, name):
return None

def get_type(self, name):
t = self.get_subtype(name)
t = self.get_fundamentaltype(name)
if not t:
t = self.get_subtype(name)
if not t:
t = self.get_tlvtype(name)
return t
Expand Down