Skip to content

Commit 946d61c

Browse files
committed
address_space_sqlite: Py2.7 compatibility
1 parent b784f9b commit 946d61c

3 files changed

Lines changed: 30 additions & 11 deletions

File tree

opcua/common/sqlite3_backend.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11

2+
import sys
23
import time
34
import sqlite3
45
import threading
56
from multiprocessing import Lock
67

78
class SQLite3Backend(object):
9+
PY2 = sys.version_info < (3, 0)
810
CHECKP_INTERVAL = 90 # [sec] WAL checkpoint
911

1012
def __init__(self, sqlFile = None, readonly=True):
@@ -74,6 +76,8 @@ def _db_connect(self):
7476
detect_types = sqlite3.PARSE_DECLTYPES,
7577
check_same_thread = False
7678
)
79+
if SQLite3Backend.PY2:
80+
self._conn[CID].text_factory = bytes
7781
c = self._get_conn().cursor()
7882
if self.readonly is True:
7983
c.execute('PRAGMA query_only=1')

opcua/server/address_space_sqlite.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11

2+
import sys
23
import os.path
34
import time
45
import datetime
6+
import sqlite3
57
from struct import pack
68

79
from opcua import ua
@@ -83,6 +85,7 @@ class AddressSpaceSQLite(AddressSpace):
8385
Load the standard address space nodes from a SQLite database.
8486
Intended for slow devices, such as Raspberry Pi, to greatly improve start up time
8587
"""
88+
PY2 = sys.version_info < (3, 0)
8689
ATTR_TABLE_NAME = 'Attributes'
8790
REFS_TABLE_NAME = 'References'
8891
CUR_TIME_NODEID = NumericNodeId(ua.ObjectIds.Server_ServerStatus_CurrentTime, 0)
@@ -258,11 +261,18 @@ def _write_references(self, nodeid, ndata):
258261
def _read_nodedata(backend, nodeid, ndata):
259262
# Search key = numeric nodeid in opc-ua binary format
260263
numNodeId = AddressSpaceSQLite._nodeid_to_numeric(nodeid)
261-
hexNodeId = ua.ua_binary.nodeid_to_binary(numNodeId).hex()
264+
hexNodeId = AddressSpaceSQLite._to_hex(ua.ua_binary.nodeid_to_binary(numNodeId))
262265

263266
AddressSpaceSQLite._read_attributes(backend, hexNodeId, ndata)
264267
AddressSpaceSQLite._read_references(backend, hexNodeId, ndata)
265268

269+
@staticmethod
270+
def _to_hex(b):
271+
if AddressSpaceSQLite.PY2:
272+
return "".join("{:02x}".format(ord(c)) for c in b)
273+
else:
274+
return b.hex()
275+
266276
@staticmethod
267277
def _read_attributes(backend, hexNodeId, ndata, attrTable=ATTR_TABLE_NAME):
268278
cmd = 'SELECT * FROM "{tn}" WHERE NodeId = x\'{h}\''.format(tn=attrTable, h=hexNodeId)
@@ -339,15 +349,15 @@ def _insert_attribute(backend, nodeid, attrId, attr, table=ATTR_TABLE_NAME):
339349

340350
cmd = 'INSERT OR REPLACE INTO "{tn}" VALUES ({q})'.format(tn=table, q=', '.join('?'*10))
341351
params = (
342-
memoryview(primaryKey),
343-
memoryview(binNodeId),
352+
sqlite3.Binary(primaryKey),
353+
sqlite3.Binary(binNodeId),
344354
int(attrId),
345355
attr.value.ServerTimestamp,
346356
None if attr.value.ServerPicoseconds is None else int(attr.value.ServerPicoseconds),
347357
attr.value.SourceTimestamp,
348358
None if attr.value.SourcePicoseconds is None else int(attr.value.SourcePicoseconds),
349359
int(attr.value.StatusCode.value),
350-
memoryview(ua.ua_binary.variant_to_binary(attr.value.Value)),
360+
sqlite3.Binary(ua.ua_binary.variant_to_binary(attr.value.Value)),
351361
str(nodeid)
352362
)
353363
backend.execute_write(cmd, params=params)
@@ -425,18 +435,18 @@ def _insert_reference(backend, nodeid, ref, table=REFS_TABLE_NAME):
425435

426436
cmd = 'INSERT OR REPLACE INTO "{tn}" VALUES ({q})'.format(tn=table, q=', '.join('?'*13))
427437
params = (
428-
memoryview(primaryKey),
429-
memoryview(binNodeId),
430-
memoryview(ua.ua_binary.nodeid_to_binary(ref.ReferenceTypeId)),
438+
sqlite3.Binary(primaryKey),
439+
sqlite3.Binary(binNodeId),
440+
sqlite3.Binary(ua.ua_binary.nodeid_to_binary(ref.ReferenceTypeId)),
431441
int(bool(ref.IsForward)),
432-
memoryview(refNodeId),
442+
sqlite3.Binary(refNodeId),
433443
int(ref.BrowseName.NamespaceIndex),
434444
None if ref.BrowseName.Name is None else str(ref.BrowseName.Name),
435445
None if ref.DisplayName.Text is None else str(ref.DisplayName.Text),
436446
None if ref.DisplayName.Locale is None else str(ref.DisplayName.Locale),
437447
int(ref.DisplayName.Encoding),
438448
int(ref.NodeClass),
439-
memoryview(ua.ua_binary.nodeid_to_binary(ref.TypeDefinition)),
449+
sqlite3.Binary(ua.ua_binary.nodeid_to_binary(ref.TypeDefinition)),
440450
str(nodeid)
441451
)
442452
backend.execute_write(cmd, params=params)

opcua/ua/ua_binary.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ def unpack(data):
125125

126126

127127
class _Primitive1(object):
128+
PY2 = sys.version_info < (3, 0)
129+
128130
def __init__(self, fmt):
129131
self._fmt = fmt
130132
st = struct.Struct(fmt.format(1))
@@ -141,17 +143,20 @@ def pack_array(self, data):
141143
if data is None:
142144
return Primitives.Int32.pack(-1)
143145
sizedata = Primitives.Int32.pack(len(data))
144-
fmt = bytes(self._fmt.format(len(data)), 'utf-8')
146+
fmt = self._str_to_bytes(self._fmt.format(len(data)))
145147
return sizedata + struct.pack(fmt, *data)
146148

147149
def unpack_array(self, data, length):
148150
if length == -1:
149151
return None
150152
if length == 0:
151153
return ()
152-
fmt = bytes(self._fmt.format(length), 'utf-8')
154+
fmt = self._str_to_bytes(self._fmt.format(length))
153155
return struct.unpack(fmt, data.read(self.size * length))
154156

157+
@staticmethod
158+
def _str_to_bytes(_str, PY2=PY2):
159+
return bytes(_str) if PY2 else bytes(_str, 'utf-8')
155160

156161
class Primitives1(object):
157162
SByte = _Primitive1("<{:d}b")

0 commit comments

Comments
 (0)