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

Add the ruff linter and formatter to recceiver #98

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 9 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
repos:
# - repo: https://github.com/psf/black
# rev: 22.8.0
# hooks:
# - id: black
# - repo: https://github.com/charliermarsh/ruff-pre-commit
# rev: v0.0.238
# hooks:
# - id: ruff
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.9.0
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
#- repo: https://github.com/pre-commit/mirrors-clang-format
# rev: v15.0.7
# hooks:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ RecSync
=======

The record synchronizer project includes two parts.
A client (RecCaster) which runing as part of an EPICS
IOC, and a server (RecCeiver) which is a stand alone
A client ([RecCaster](./client/README.md)) which runing as part of an EPICS
IOC, and a server ([RecCeiver](./server/README.md)) which is a stand alone
daemon. Together they work to ensure the the server(s)
have a complete list of all records currently provided
by the client IOCs.
Expand Down
1 change: 1 addition & 0 deletions client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Reccaster
28 changes: 0 additions & 28 deletions server/README

This file was deleted.

63 changes: 63 additions & 0 deletions server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Recceiver

Application for talking between IOCs (via [reccaster](../client)) and ChannelFinder (via [pyCFClient](https://github.com/ChannelFinder/pyCFClient)).

Written using [twistd](https://twisted.org/).

## Formatting and Linting

Recceiver uses [ruff](https://docs.astral.sh/ruff/) for formatting and linting. See website for installation instructions.


```bash
ruff check
```

```bash
ruff check --fix
```

```bash
ruff format
```


## Server testing

Setup

```bash
sqlite3 test.db -init recceiver.sqlite3 .exit
```

Run (for twistd <= 16.0.3)

```bash
twistd -n recceiver -f demo.conf
```

or (see below for discussion)

```bash
twistd -r poll -n recceiver -f demo.conf
```


Run (for twistd >= 16.0.4)

```bash
PYTHONPATH=$PWD twistd -r poll -n recceiver -f demo.conf
```

At some point 'twistd' stopped implicitly searching the working directory.

May need to uncomment `addrlist = 127.255.255.255:5049` in demo.conf
when doing local testing on a computer w/ a firewall.

Twisted 14.0.2 seems to have a problem with the epoll() reactor
which raises 'IOError: [Errno 2] No such file or directory'
during startup. Try with the poll() reactor.

```bash
twistd -r poll -n recceiver -f demo.conf
```
38 changes: 22 additions & 16 deletions server/recceiver/announce.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,69 @@
import struct

from twisted.internet import protocol
from twisted.internet.error import MessageLengthError
import logging

_log = logging.getLogger(__name__)


_Ann = struct.Struct('>HH4sHHI')
_Ann = struct.Struct(">HH4sHHI")

__all__ = ["Announcer"]

__all__ = ['Announcer']

class Announcer(protocol.DatagramProtocol):
def __init__(self, tcpport, key=0,
tcpaddr='\xff\xff\xff\xff',
udpaddrs=[('<broadcast>',5049)],
period=15.0):
def __init__(
self,
tcpport,
key=0,
tcpaddr="\xff\xff\xff\xff",
udpaddrs=[("<broadcast>", 5049)],
period=15.0,
):
from twisted.internet import reactor

self.reactor = reactor

if sys.version_info[0] < 3:
self.msg = _Ann.pack(0x5243, 0, tcpaddr, tcpport, 0, key)
else:
self.msg = _Ann.pack(0x5243, 0, tcpaddr.encode('latin-1'), tcpport, 0, key)
self.msg = _Ann.pack(0x5243, 0, tcpaddr.encode("latin-1"), tcpport, 0, key)

self.delay = period
self.udps = udpaddrs
self.udpErr = set()
self.D = None
if len(self.udps)==0:
raise RuntimeError('Announce list is empty at start time...')
if len(self.udps) == 0:
raise RuntimeError("Announce list is empty at start time...")

def startProtocol(self):
_log.info('Setup Announcer')
_log.info("Setup Announcer")
self.D = self.reactor.callLater(0, self.sendOne)
# we won't process any receieved traffic, so no reason to wake
# up for it...
self.transport.pauseProducing()

def stopProtocol(self):
_log.info('Stop Announcer')
_log.info("Stop Announcer")
self.D.cancel()
del self.D

def datagramReceived(self, src):
pass # ignore
pass # ignore

def sendOne(self):
self.D = self.reactor.callLater(self.delay, self.sendOne)
for A in self.udps:
try:
_log.debug('announce to {s}'.format(s=A))
_log.debug("announce to {s}".format(s=A))
self.transport.write(self.msg, A)
try:
self.udpErr.remove(A)
_log.warning('announce OK to {s}'.format(s=A))
_log.warning("announce OK to {s}".format(s=A))
except KeyError:
pass
except:
except MessageLengthError:
if A not in self.udpErr:
self.udpErr.add(A)
_log.exception('announce Error to {s}'.format(s=A))
_log.exception("announce Error to {s}".format(s=A))
Loading
Loading