Skip to content

Commit

Permalink
Updated to support Python 3.12
Browse files Browse the repository at this point in the history
This commit also includes formatting changes from ruff
  • Loading branch information
Oliver Copping committed Apr 18, 2024
1 parent 70b030d commit 3e61eb4
Show file tree
Hide file tree
Showing 25 changed files with 1,587 additions and 1,122 deletions.
33 changes: 29 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
# Check build from .tar
- name: Source
os: ubuntu-latest
python: "3.9"
python: "3.12"
source: true

# Linux py builds x64
Expand Down Expand Up @@ -109,6 +109,13 @@ jobs:
piparch: manylinux2014_x86_64
numpy: numpy==1.23.5
cython: Cython==0.29.35

- name: linux 3.12 amd64
os: ubuntu-latest
pyver: cp312-cp312
piparch: manylinux2014_x86_64
numpy: numpy==1.26.4
cython: Cython==3.0.10
skip_cothread: yes

# Linux py builds x64
Expand Down Expand Up @@ -163,9 +170,13 @@ jobs:
- name: linux 3.9 i686
os: ubuntu-latest
pyver: cp39-cp39

- name: linux 3.12 i686
os: ubuntu-latest
pyver: cp312-cp312
piparch: manylinux2010_i686
numpy: numpy==1.19.3
cython: Cython==0.29.23
numpy: numpy==1.26.4
cython: Cython==3.0.10
pre: linux32

# numpy i386 wheels not built >= 3.10
Expand Down Expand Up @@ -212,13 +223,20 @@ jobs:
piparch: macosx_10_9_intel
numpy: numpy==1.23.5
cython: Cython==0.29.35

- name: osx 3.12 intel
os: macos-latest
python: "3.12"
piparch: macosx_10_9_intel
numpy: numpy==1.26.4
cython: Cython==3.0.10
skip_cothread: yes

# Windows py builds

## missing Microsoft Visual C++ 9.0
#- os: windows-latest
# python: "2.7"
# python: "3.12"
# piparch: win_amd64

- name: win64 3.5
Expand Down Expand Up @@ -270,6 +288,13 @@ jobs:
profile: latest
skip_cothread: yes

- name: win64 3.12
os: windows-latest
python: "3.12"
piparch: win_amd64
profile: latest
skip_cothread: yes

steps:
- uses: actions/checkout@v3
with:
Expand Down
42 changes: 24 additions & 18 deletions example/dynamicbox_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
$ pvget -m foo:list
"""

from __future__ import print_function

import sys
import time, logging

_log = logging.getLogger(__name__)

from threading import Lock
Expand All @@ -31,19 +30,19 @@

prefix = sys.argv[1]

list_type = NTScalar('as')
list_type = NTScalar("as")

types = {
'int':NTScalar('i').wrap(0),
'float':NTScalar('d').wrap(0.0),
'str':NTScalar('s').wrap(''),
'enum':NTEnum().wrap(0),
"int": NTScalar("i").wrap(0),
"float": NTScalar("d").wrap(0.0),
"str": NTScalar("s").wrap(""),
"enum": NTEnum().wrap(0),
}

pvs_lock = Lock()
pvs = {}

provider = StaticProvider('dynamicbox')
provider = StaticProvider("dynamicbox")


class MailboxHandler(object):
Expand All @@ -52,34 +51,40 @@ def put(self, pv, op):
pv.post(op.value())
op.done()

addpv = SharedPV(initial=NTScalar('s').wrap('Only RPC'))
delpv = SharedPV(initial=NTScalar('s').wrap('Only RPC'))

addpv = SharedPV(initial=NTScalar("s").wrap("Only RPC"))
delpv = SharedPV(initial=NTScalar("s").wrap("Only RPC"))
listpv = SharedPV(nt=list_type, initial=[])

provider.add(prefix + "add", addpv)
provider.add(prefix + "del", delpv)
provider.add(prefix + "list", listpv)
_log.info("add with %s, remove with %s, list with %s", prefix + "add", prefix + "del", prefix + "list")
_log.info(
"add with %s, remove with %s, list with %s",
prefix + "add",
prefix + "del",
prefix + "list",
)


@addpv.rpc
def adder(pv, op):
name = op.value().query.name
type = op.value().query.get('type', 'int')
type = op.value().query.get("type", "int")

if type not in types:
op.done(error='unknown type %s. Known types are %s'%(type, ', '.join(types)))
op.done(error="unknown type %s. Known types are %s" % (type, ", ".join(types)))
return

with pvs_lock:

if name in pvs:
op.done(error='PV already exists')
op.done(error="PV already exists")
return

pv = SharedPV(initial=types[type], handler=MailboxHandler())
provider.add(name, pv)
pvs[name] = pv
names = list(pvs) # makes a copy to ensure consistency outside lock
names = list(pvs) # makes a copy to ensure consistency outside lock

_log.info("Added mailbox %s", name)
listpv.post(names)
Expand All @@ -96,13 +101,14 @@ def remover(pv, op):
return
pv = pvs.pop(name)
provider.remove(name)
names = list(pvs) # makes a copy to ensure consistency outside lock
names = list(pvs) # makes a copy to ensure consistency outside lock

_log.info("Removed mailbox %s", name)
listpv.post(names)

op.done()


Server.forever(providers=[provider])

print('Done')
print("Done")
17 changes: 9 additions & 8 deletions example/lazycounter_server_cothread.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
$ pvget -m foo
"""

from __future__ import print_function

import time, logging

_log = logging.getLogger(__name__)

import cothread
Expand All @@ -24,10 +23,11 @@
logging.basicConfig(level=logging.DEBUG)

types = {
False:NTScalar('I'),
True:NTScalar('d'),
False: NTScalar("I"),
True: NTScalar("d"),
}


class LazyCounter(object):
def __init__(self):
self.timer = None
Expand All @@ -50,7 +50,7 @@ def _tick(self):
# no clients connected
if self.pv.isOpen():
self.pv.close()
self.select = not self.select # toggle type for next clients
self.select = not self.select # toggle type for next clients

# cancel timer until a new first client arrives
self.timer.cancel()
Expand Down Expand Up @@ -78,13 +78,14 @@ def put(self, pv, op):
self.count = op.value().value
op.done()


pv = SharedPV(handler=LazyCounter())

with Server(providers=[{'foo': pv}]):
print('Running')
with Server(providers=[{"foo": pv}]):
print("Running")
try:
cothread.WaitForQuit()
except KeyboardInterrupt:
pass

print('Done')
print("Done")
47 changes: 29 additions & 18 deletions example/mailbox_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,35 @@
$ pvinfo foo
"""

from __future__ import print_function

import time, logging

from p4p.nt import NTScalar
from p4p.server import Server, StaticProvider
from p4p.server.thread import SharedPV

help_type = NTScalar('s')
help_type = NTScalar("s")
types = {
'int':NTScalar('i').wrap(0),
'float':NTScalar('d').wrap(0.0),
'str':NTScalar('s').wrap(''),
"int": NTScalar("i").wrap(0),
"float": NTScalar("d").wrap(0.0),
"str": NTScalar("s").wrap(""),
}


class MailboxHandler(object):
type = None

def rpc(self, pv, op):
V = op.value()
print("RPC", V, V.query.get('help'), V.query.get('newtype'))
if V.query.get('help') is not None:
op.done(help_type.wrap('Try newtype=int (or float or str)'))
print("RPC", V, V.query.get("help"), V.query.get("newtype"))
if V.query.get("help") is not None:
op.done(help_type.wrap("Try newtype=int (or float or str)"))
return

newtype = types[V.query.newtype]

op.done(help_type.wrap('Success'))
op.done(help_type.wrap("Success"))

pv.close() # disconnect client
pv.close() # disconnect client
pv.open(newtype)

def put(self, pv, op):
Expand All @@ -54,27 +54,38 @@ def put(self, pv, op):
# Notify the client making this PUT operation that it has now completeted
op.done()


def getargs():
from argparse import ArgumentParser

P = ArgumentParser()
P.add_argument('name', nargs='+')
P.add_argument('-v','--verbose', action='store_const', default=logging.INFO, const=logging.DEBUG)
P.add_argument("name", nargs="+")
P.add_argument(
"-v",
"--verbose",
action="store_const",
default=logging.INFO,
const=logging.DEBUG,
)
return P.parse_args()


def main(args):
provider = StaticProvider('mailbox') # 'mailbox' is an arbitrary name
provider = StaticProvider("mailbox") # 'mailbox' is an arbitrary name

pvs = [] # we must keep a reference in order to keep the Handler from being collected
pvs = [] # we must keep a reference in order to keep the Handler from being collected
for name in args.name:
pv = SharedPV(initial=types['int'], handler=MailboxHandler())
pv = SharedPV(initial=types["int"], handler=MailboxHandler())

provider.add(name, pv)
pvs.append(pv)

Server.forever(providers=[provider])

print('Done')
if __name__=='__main__':
print("Done")


if __name__ == "__main__":
args = getargs()
logging.basicConfig(level=args.verbose)
main(args)
5 changes: 3 additions & 2 deletions example/monitor_client.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#!/usr/bin/env python

from __future__ import print_function

import sys, time, logging

from p4p.client.thread import Context

logging.basicConfig(level=logging.DEBUG)


def cb(value):
print("update", value)


print("Create Context")
with Context('pva') as ctxt:
with Context("pva") as ctxt:
print("Subscribe to", sys.argv[1])
S = ctxt.monitor(sys.argv[1], cb)

Expand Down
9 changes: 5 additions & 4 deletions example/monitor_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
# cf.
# example/monitor_client.py

from __future__ import print_function

import sys, time, logging

from p4p.client.thread import Context

logging.basicConfig(level=logging.INFO)


def cb(value):
if not value.raw.changed('value'):
if not value.raw.changed("value"):
print("Meta update")
for fld in value.raw.asSet():
print(" ",fld,value.raw[fld])
print(" ", fld, value.raw[fld])


print("Create Context")
with Context('pva') as ctxt:
with Context("pva") as ctxt:
print("Subscribe to", sys.argv[1])
S = ctxt.monitor(sys.argv[1], cb)

Expand Down
Loading

0 comments on commit 3e61eb4

Please sign in to comment.