-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lot of stuff, Mainly in the pythoncardx/framework part
- Loading branch information
Showing
10 changed files
with
240 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class NotAlwaysStatic(object): | ||
""" This makes a function both static and not static | ||
credits goes there (in decreasing order of importance). | ||
http:https://users.rcn.com/python/download/Descriptor.htm | ||
and there: | ||
http:https://stackoverflow.com/questions/114214/class-method-differences-in-python-bound-unbound-and-static/114289#114289 | ||
and here: | ||
http:https://code.activestate.com/recipes/52304-static-methods-aka-class-methods-in-python/ | ||
For examples of uses see pythoncardx/framework/tlv/bertag.py | ||
""" | ||
def __init__(self, boundname, staticname): | ||
self.boundname = boundname | ||
self.staticname = staticname | ||
|
||
def __get__(self, obj, objtype=None): | ||
if obj is not None: | ||
return getattr(obj, self.boundname) | ||
else: | ||
return getattr(objtype, self.staticname) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from python.lang import NullPointerException, ArithmeticException | ||
|
||
def from_bcd(data): | ||
""" | ||
>>> from_bcd(0x59) | ||
59 | ||
""" | ||
return int(hex(data)[2:]) | ||
|
||
def from_hex(data): | ||
""" | ||
>>> from_hex(0x59) | ||
786 | ||
""" | ||
return data | ||
|
||
class BigNumber(object): | ||
FORMAT_BCD = 1 | ||
FORMAT_HEX = 2 | ||
|
||
def __init__(self, maxBytes): | ||
if maxBytes <= 0: | ||
raise ArithmeticException() | ||
self.maxBytes = maxBytes | ||
self._value = None | ||
|
||
def init(self, bArray, bOff, bLen, arrayFormat): | ||
if bLen == 0: | ||
raise ArithmeticException() | ||
if arrayFormat not in (self.FORMAT_BCD, self.FORMAT_HEX): | ||
raise ArithmeticException() | ||
if bArray is None: | ||
raise NullPointerException() | ||
if arrayFormat == self.FORMAT_BCD: | ||
self._value = 0 | ||
for i in xrange(bLen): | ||
self._value *= 100 | ||
self._value += from_bcd(bArray[bOff+i]) | ||
elif arrayFormat == self.FORMAT_HEX: | ||
self._value = 0 | ||
for i in xrange(bLen): | ||
self._value = self._value << 8 | ||
self._value += bArray[bOff+i] | ||
|
||
@staticmethod | ||
def getMaxBytesSupported(): | ||
# We have to lie there for practical reasons | ||
return 20 | ||
|
||
def multiply(self, bArray, bOff, bLen, arrayFormat): | ||
other = BigNumber(70) | ||
other.init(bArray, bOff, bLen, arrayFormat) | ||
self._value *= other._value | ||
|
||
def compareTo(self, operand): | ||
if self._value == operand._value: | ||
return 0 | ||
elif self._value > operand._value: | ||
return 1 | ||
else: | ||
return -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import warnings | ||
|
||
def signed(value, depth): | ||
""" | ||
return the signed value of the number on the specified depth | ||
""" | ||
mask = (1 << (depth*8)) - 1 | ||
if value > ((1 << (depth*8)-1) - 1): | ||
return -(~(value-1) & mask) | ||
else: | ||
return value | ||
|
||
def s4(i): | ||
return signed(i, 4) | ||
|
||
def sanatize(array): | ||
for i in range(len(array)): | ||
array[i] = signed(array[i], 1) | ||
|
||
class JCint(object): | ||
|
||
@staticmethod | ||
|
||
def getInt(bArray, bOff): | ||
i = bArray[bOff] << 24 + \ | ||
bArray[bOff + 1] << 16 + \ | ||
bArray[bOff + 2] << 8 + \ | ||
bArray[bOff + 3] | ||
return s4(i) | ||
|
||
@staticmethod | ||
def makeInt(*args): | ||
if len(args) == 4: | ||
(b1, b2, b3, b4) = args | ||
i = b1 << 24 + b2 << 16 + b3 << 8 + b4 | ||
return s4(i) | ||
else: | ||
(s1, s2) = args | ||
i = s1 << 16 + s2 | ||
return s4(i) | ||
|
||
@staticmethod | ||
def makeTransientIntArray(length, event): | ||
warnings.warn("Array is not transient") | ||
return [0 for i in range(length)] | ||
|
||
@staticmethod | ||
def setInt(bArray, bOff, iValue1, iValue2): | ||
# ints are transmited as two params ... | ||
iVal = JCint.makeInt(iValue1, iValue2) | ||
print bArray, bOff, iVal | ||
bArray[bOff] = (iVal >> 24) & 0xff | ||
bArray[bOff + 1] = iVal >> 16 & 0xff | ||
bArray[bOff + 2] = iVal >> 8 & 0xff | ||
bArray[bOff + 3] = iVal & 0xff | ||
return bOff + 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import unittest | ||
|
||
from pythoncardx.framework.tlv import BERTag, PrimitiveBERTag, ConstructedBERTag, \ | ||
BERTLV, PrimitiveBERTLV, ConstructedBERTLV | ||
|
||
class BERTLVTest(unittest.TestCase): | ||
|
||
def testConstrInit(self): | ||
tag = ConstructedBERTag() | ||
tag.init(3, 1) | ||
self.assertEquals(3, tag._tagClass) | ||
self.assertEquals(1, tag._PC) | ||
self.assertEquals(1, tag._tagNumber) | ||
array = [0 for i in range(10)] | ||
tag.toBytes(array, 0) | ||
tlv = BERTLV.getInstance(array, 0, 2) | ||
self.assertEquals(tag, tlv.getTag()) | ||
|
||
class StaticTest(unittest.TestCase): | ||
|
||
def testtoBytes(self): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters