Skip to content

Commit

Permalink
tools/checkpython: Checking - and formatting - python code
Browse files Browse the repository at this point in the history
Uses yapf [https://github.com/google/yapf] to format code. Currently,
is invoked as a subprocess, but being a Python module itself, that could
be changed in future.

A helper script gathers modified '.py' files and feeds them to yapf.
One can use git range format, such as HEAD~3, to define which patches
will have its Python files inspected, but the whole file is reformatted,
not only what was changed: that could be improved, as yapf accepts a
line range of file to format, but that sounded too much effort for now.

Current Python files were reformatted. Manual intervention was done to
tweak long strings: breaking them manually and applying format looked
better than:

  print(
      "Really long string"
      )
  • Loading branch information
edersondisouza authored and lucasdemarchi committed Feb 8, 2017
1 parent 0c6ca67 commit 6f74a68
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ connection there will also receive routed packets.
### Contributing ###

Pull-requests are accepted on GitHub. Make sure to check coding style with the
provided script in tools/checkpatch, check for memory leaks with valgrind and
test on real hardware.
provided script in tools/checkpatch and tools/checkpython, check for memory leaks
with valgrind and test on real hardware.

### Samples ###

Expand Down
4 changes: 1 addition & 3 deletions examples/arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,5 @@
mav = mavutil.mavlink_connection('udpin:' + sys.argv[1])
mav.wait_heartbeat()
mav.mav.command_long_send(mav.target_system, mav.target_component,
mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM,
0,
1,
mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM, 0, 1,
0, 0, 0, 0, 0, 0)
13 changes: 8 additions & 5 deletions examples/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@

if len(sys.argv) != 3:
print("Usage: %s <ip:udp_port> <system-id>" % (sys.argv[0]))
print("Receive mavlink heartbeats on specified interface. Respond with a ping message")
print("Receive mavlink heartbeats on specified interface. "
"Respond with a ping message")
quit()

srcSystem = int(sys.argv[2])
mav = mavutil.mavlink_connection('udpin:' + sys.argv[1], source_system = srcSystem)
mav = mavutil.mavlink_connection(
'udpin:' + sys.argv[1], source_system=srcSystem)

while(True):
while (True):
msg = mav.recv_match(blocking=True)
print("Message from %d: %s" % (msg.get_srcSystem(), msg))
if msg.target_system == 0:
Expand All @@ -40,5 +42,6 @@
print("\tMessage sent to me")
else:
print("\tMessage sent to other")
mav.mav.ping_send(int(time.time() * 1000), msg.seq, msg.get_srcSystem(), msg.get_srcComponent())

mav.mav.ping_send(
int(time.time() * 1000), msg.seq,
msg.get_srcSystem(), msg.get_srcComponent())
18 changes: 12 additions & 6 deletions examples/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,29 @@
import time

if len(sys.argv) != 4:
print("Usage: %s <ip:udp_port> <system-id> <target-system-id>" % (sys.argv[0]))
print("Send mavlink pings, using given <system-id> and <target-system-id>, to specified interface")
print("Usage: %s <ip:udp_port> <system-id> <target-system-id>" %
(sys.argv[0]))
print(
"Send mavlink pings, using given <system-id> and <target-system-id>, "
"to specified interface")
quit()

mav = mavutil.mavlink_connection('udpout:' + sys.argv[1], source_system = int(sys.argv[2]))
mav = mavutil.mavlink_connection(
'udpout:' + sys.argv[1], source_system=int(sys.argv[2]))


def pingloop():
i = 0
while(True):
mav.mav.ping_send(int(time.time() * 1000), i, int(sys.argv[3]), 0)
while (True):
mav.mav.ping_send(int(time.time() * 1000), i, int(sys.argv[3]), 1)
i = i + 1
sleep(1)


pingthread = Thread(target=pingloop)
pingthread.daemon = True
pingthread.start()

while(True):
while (True):
msg = mav.recv_match(blocking=True)
print("Message from %d: %s" % (msg.get_srcSystem(), msg))
35 changes: 35 additions & 0 deletions tools/checkpython
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python

from __future__ import print_function

import subprocess
import sys


def run_command(cmd):
try:
output = subprocess.check_output(
cmd, stderr=subprocess.STDOUT, shell=True, universal_newlines=True)
return output

except subprocess.CalledProcessError as e:
print("Could not run command `%s`: %s" % (cmd, e.output))
return None


revision = ""
if len(sys.argv) == 2:
revision = sys.argv[1]
if len(sys.argv) > 2:
print("Usage: %s [revision]")
quit()

cmd = "git diff --diff-filter=ACMR --oneline --name-only %s -- '*.py'" % revision
file_list = run_command(cmd)

if len(file_list) == 0:
print("No changes on current working dir and no revision specified. Nothing to do")
else:
# TODO Maybe use as a module, so we can output which files were changed?
cmd = "yapf -i %s" % (file_list.replace("\n", " "))
run_command(cmd)

0 comments on commit 6f74a68

Please sign in to comment.