dsdl/common.py
Go to the documentation of this file.
1 #
2 # Copyright (C) 2014-2015 UAVCAN Development Team <uavcan.org>
3 #
4 # This software is distributed under the terms of the MIT License.
5 #
6 # Author: Pavel Kirienko <pavel.kirienko@zubax.com>
7 # Ben Dyer <ben_dyer@mac.com>
8 #
9 
10 from __future__ import division, absolute_import, print_function, unicode_literals
11 import os
12 import struct
13 from pyuavcan_v0 import UAVCANException
14 
15 
17  '''
18  This exception is raised in case of a parser failure.
19  Fields:
20  file Source file path where the error has occurred. Optional, will be None if unknown.
21  line Source file line number where the error has occurred. Optional, will be None if unknown.
22  '''
23 
24  def __init__(self, text, file=None, line=None):
25  Exception.__init__(self, text)
26  self.file = file
27  self.line = line
28 
29  def __str__(self):
30  '''Returns nicely formatted error string in GCC-like format (can be parsed by e.g. Eclipse error parser)'''
31  if self.file and self.line:
32  return '%s:%d: %s' % (pretty_filename(self.file), self.line, Exception.__str__(self))
33  if self.file:
34  return '%s: %s' % (pretty_filename(self.file), Exception.__str__(self))
35  return Exception.__str__(self)
36 
37 
38 def pretty_filename(filename):
39  '''Returns a nice human readable path to 'filename'.'''
40  try:
41  a = os.path.abspath(filename)
42  r = os.path.relpath(filename)
43  except ValueError:
44  # Catch relpath exception. Happens, because it can not produce relative path
45  # if wroking directory is on different drive.
46  a = r = filename
47  return a if '..' in r else r
48 
49 
50 def crc16_from_bytes(bytes, initial=0xFFFF):
51  # CRC-16-CCITT
52  # Initial value: 0xFFFF
53  # Poly: 0x1021
54  # Reverse: no
55  # Output xor: 0
56  # Check string: '123456789'
57  # Check value: 0x29B1
58 
59  try:
60  if isinstance(bytes, basestring): # Python 2.7 compatibility
61  bytes = map(ord, bytes)
62  except NameError:
63  if isinstance(bytes, str): # This branch will be taken on Python 3
64  bytes = map(ord, bytes)
65 
66  crc = initial
67  for byte in bytes:
68  crc ^= byte << 8
69  for bit in range(8):
70  if crc & 0x8000:
71  crc = ((crc << 1) ^ 0x1021) & 0xFFFF
72  else:
73  crc = (crc << 1) & 0xFFFF
74  return crc & 0xFFFF
75 
76 
77 def bytes_from_crc64(crc64):
78  # Cast to str explicitly for Python 2.7 compatibility when
79  # unicode_literals is enabled
80  return bytes(struct.pack(str("<Q"), crc64))
pyuavcan_v0.dsdl.common.DsdlException
Definition: dsdl/common.py:16
pyuavcan_v0.dsdl.common.DsdlException.line
line
Definition: dsdl/common.py:27
pyuavcan_v0.dsdl.common.pretty_filename
def pretty_filename(filename)
Definition: dsdl/common.py:38
libuavcan_dsdl_compiler.str
str
Definition: libuavcan_dsdl_compiler/__init__.py:22
pyuavcan_v0.UAVCANException
Definition: pyuavcan/pyuavcan_v0/__init__.py:45
pyuavcan_v0.dsdl.common.bytes_from_crc64
def bytes_from_crc64(crc64)
Definition: dsdl/common.py:77
pyuavcan_v0.dsdl.common.DsdlException.file
file
Definition: dsdl/common.py:26
pyuavcan_v0.dsdl.common.crc16_from_bytes
def crc16_from_bytes(bytes, initial=0xFFFF)
Definition: dsdl/common.py:50
pyuavcan_v0.dsdl.common.DsdlException.__init__
def __init__(self, text, file=None, line=None)
Definition: dsdl/common.py:24
pyuavcan_v0.dsdl.common.DsdlException.__str__
def __str__(self)
Definition: dsdl/common.py:29


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02