Skip to content

Commit

Permalink
first version, mostly complete except Wi-Fi/Bluetooth carrier type re…
Browse files Browse the repository at this point in the history
…cords
  • Loading branch information
nehpetsde committed Jul 6, 2016
0 parents commit 21bd39c
Show file tree
Hide file tree
Showing 21 changed files with 4,643 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*~
*.pyc
*.egg-info
.tox
.cache
.coverage*
__pycache__/
docs/_build/
htmlcov
dist
15 changes: 15 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ISC License

Copyright (c) 2016, Stephen Tiedemann <stephen.tiedemann@gmail.com>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 changes: 22 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
==========================================
ndeflib - parse and generate NDEF messages
==========================================

The ``ndeflib`` is an `ISC <http://choosealicense.com/licenses/isc/>`_-licensed Python package for parsing and generating NFC Data Exchange Format messages:

.. code-block:: pycon
>>> import ndef
>>> hexstr = '9101085402656e48656c6c6f5101085402656e576f726c64'
>>> octets = bytearray.fromhex(hexstr)
>>> for record in ndef.message_decoder(octets): print(record)
NDEF Text Record ID '' Text 'Hello' Language 'en' Encoding 'UTF-8'
NDEF Text Record ID '' Text 'World' Language 'en' Encoding 'UTF-8'
>>> message = [ndef.TextRecord("Hello"), ndef.TextRecord("World")]
>>> b''.join(ndef.message_encoder(message)) == octets
True
The ``ndeflib`` package is currently in alpha development state and
documentation so far is only within the source code (for the Python
help command). However, the source published is considered quite
stable and fully tested for Python 2 and 3.
8 changes: 8 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[pytest]
minversion = 2.8
strict = true
addopts = -ra
testpaths = tests

[bdist_wheel]
universal = 1
77 changes: 77 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import codecs
import os
import re

from setuptools import setup, find_packages

###############################################################################

NAME = "ndeflib"
PACKAGES = find_packages(where="src")
META_PATH = os.path.join("src", "ndef", "__init__.py")
KEYWORDS = ["ndef", "nfc"]
CLASSIFIERS = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Natural Language :: English",
"License :: OSI Approved :: ISC License (ISCL)",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Topic :: Software Development :: Libraries :: Python Modules",
]
INSTALL_REQUIRES = []

###############################################################################

HERE = os.path.abspath(os.path.dirname(__file__))

def read(*parts):
"""
Build an absolute path from *parts* and and return the contents of the
resulting file. Assume UTF-8 encoding.
"""
with codecs.open(os.path.join(HERE, *parts), "rb", "utf-8") as f:
return f.read()

META_FILE = read(META_PATH)

def find_meta(meta):
"""
Extract __*meta*__ from META_FILE.
"""
meta_match = re.search(
r"^__{meta}__ = ['\"]([^'\"]*)['\"]".format(meta=meta),
META_FILE, re.M
)
if meta_match:
return meta_match.group(1)
raise RuntimeError("Unable to find __{meta}__ string.".format(meta=meta))

if __name__ == "__main__":
setup(
name=NAME,
description=find_meta("description"),
license=find_meta("license"),
url=find_meta("uri"),
version=find_meta("version"),
author=find_meta("author"),
author_email=find_meta("email"),
maintainer=find_meta("author"),
maintainer_email=find_meta("email"),
keywords=KEYWORDS,
long_description=read("README.rst"),
packages=PACKAGES,
package_dir={"": "src"},
zip_safe=False,
classifiers=CLASSIFIERS,
install_requires=INSTALL_REQUIRES,
)
121 changes: 121 additions & 0 deletions src/ndef/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# -*- coding: utf-8 -*-
"""A package for parsing, handling, and generating NDEF messages.
The NFC Data Exchange Format (NDEF) is a binary message format that
can be used to encapsulate one or more application-defined payloads
into a single message construct for exchange between NFC Devices and
Tags. Each payload is described by a type, a length, and an optional
identifier. The payload type determines the syntactical requirements
for decoding and encoding of the payload octets. The optional payload
identifier allows an application to locate a specific record within
the NDEF Message.
An NDEF Message is a sequence of one or more NDEF Records, started
with a Message Begin (MB) flag in the first record and terminated with
a Message End (ME) flag in the last record. By convention, the first
record provides the processing context for the whole NDEF Message.
An NDEF Message can be decoded from bytes, bytearray, or a file-like,
byte-oriented stream with the ndef.message_decoder() generator
function. Note that the NDEF Records are decoded sequentially while
iterating, it is thus possible to successfully decode until some
malformed record later in the message.
>>> octets = bytearray.fromhex('910301414243005903010158595a30ff')
>>> for record in ndef.message_decoder(octets): print(record)
...
NDEF Record urn:nfc:wkt:ABC PAYLOAD 1 byte 00
NDEF Record urn:nfc:wkt:XYZ ID 0 PAYLOAD 1 byte ff
An NDEF Message can be encoded to bytes or into a stream with the
ndef.message_encoder() generator function. When not encoding into a
stream the generator returns the bytes for each encoded record,
joining them produces the complete message bytes.
>>> record_1 = ndef.Record('urn:nfc:wkt:ABC', None, b'\\x00')
>>> record_2 = ndef.Record('urn:nfc:wkt:XYZ', '0', b'\\xff')
>>> octets = b''.join(ndef.message_encoder([record_1, record_2]))
>>> bytearray(octets).hex() # octets.encode('hex') for Python 2
'910301414243005903010158595a30ff'
An ndef.Record is initialized with the record type, name, and
data. The data argument is the sequence of octets that will become the
NDEF Record PAYLOAD. The name argument gives the NDEF Record ID with
up to 255 latin characters. The type argument gives the NDEF Record
TNF (Type Name Format) and TYPE field in a combined string
representation. For NFC Forum well-known and external record types
this is the prefix 'urn:nfc:wkt:' and 'urn:nfc:ext:' followed by
TYPE. The media-type and absolute URI type name formats ares
recognized by '<media-type>/<subtype>' and '<scheme>://<here-part>
pattern matching. The unknown and unchanged (chunked) type name
formats are selected with the single words 'unknown' and
'unchanged'. Finally, an empty string (or None) selects the empty
record type name format.
The ndef.Record type, name, and data attributes provide access to the
init arguments (None arguments are set as to defaults). The type
attribute is generally read-only while the name attribute is
read-writable. The data attribute is read-only but is a bytearray that
can itself be modified. Note that for derived record classes the data
attribute is an non-mutable bytes object.
The ndef.message_decoder() may return and the ndef.message_encoder()
does also accept ndef.Record derived class instances. Those are either
implemented within the ndef package or may be registered by the
application with ndef.Record.register_type().
>>> class MyRecord(ndef.Record):
... _type = 'urn:nfc:ext:nfcpy.org:MyRecord'
... def __init__(self, integer):
... self._value = value
... def _encode_payload(self):
... return struct.pack('>H', self._value)
... _decode_min_payload_length = 2
... _decode_max_payload_length = 2
... @classmethod
... def _decode_payload(cls, octets, errors, known_types):
... return cls(struct.unpack('>H', octets)[0])
...
>>> ndef.Record.register_type(MyRecord)
The ndef package provides a number of well-known record type classes,
specifically the NFC Forum Text, URI, Smartposter and Connection
Handover Records, as well as the Bluetooth SIG and Wi-Fi Alliance
Carrier Configuration Records for connection handover. They are
documented in the package contents files.
"""
import sys
if sys.version_info < (2,7): # pragma: no cover
raise ImportError("The ndef module requires Python 2.6 or newer!")

from .message import message_decoder, message_encoder
from .record import Record, DecodeError, EncodeError
from .text import TextRecord
from .uri import UriRecord
from .smartposter import SmartposterRecord
from .handover import HandoverRequestRecord
from .handover import HandoverSelectRecord
from .handover import HandoverMediationRecord
from .handover import HandoverInitiateRecord
from .handover import HandoverCarrierRecord
from .deviceinfo import DeviceInformationRecord
#from .wifi import WifiSimpleConfigRecord
#from .wifi import WifiPeerToPeerRecord
#from .bluetooth import BluetoothConfigRecord

### METADATA ##################################################################

__version__ = "0.0.0"

__title__ = "ndef"
__description__ = "NFC Data Exchange Format message parser and generator."
__uri__ = "https://ndeflib.readthedocs.io/"

__author__ = "Stephen Tiedemann"
__email__ = "stephen.tiedemann@gmail.com"

__license__ = "ISC"
__copyright__ = "Copyright (c) 2016 Stephen Tiedemann"

###############################################################################
Loading

0 comments on commit 21bd39c

Please sign in to comment.