forked from LBRYCommunity/lbry-sdk
moved enum into script to avoid weird import conflict with Python built-in enum module
This commit is contained in:
parent
f459b6833a
commit
c54373bd9c
2 changed files with 43 additions and 57 deletions
|
@ -1,54 +0,0 @@
|
||||||
# Copyright (c) 2016, Neil Booth
|
|
||||||
#
|
|
||||||
# All rights reserved.
|
|
||||||
#
|
|
||||||
# See the file "LICENCE" for information about the copyright
|
|
||||||
# and warranty status of this software.
|
|
||||||
|
|
||||||
"""An enum-like type with reverse lookup.
|
|
||||||
|
|
||||||
Source: Python Cookbook, http://code.activestate.com/recipes/67107/
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
class EnumError(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class Enumeration:
|
|
||||||
|
|
||||||
def __init__(self, name, enumList):
|
|
||||||
self.__doc__ = name
|
|
||||||
|
|
||||||
lookup = {}
|
|
||||||
reverseLookup = {}
|
|
||||||
i = 0
|
|
||||||
uniqueNames = set()
|
|
||||||
uniqueValues = set()
|
|
||||||
for x in enumList:
|
|
||||||
if isinstance(x, tuple):
|
|
||||||
x, i = x
|
|
||||||
if not isinstance(x, str):
|
|
||||||
raise EnumError("enum name {} not a string".format(x))
|
|
||||||
if not isinstance(i, int):
|
|
||||||
raise EnumError("enum value {} not an integer".format(i))
|
|
||||||
if x in uniqueNames:
|
|
||||||
raise EnumError("enum name {} not unique".format(x))
|
|
||||||
if i in uniqueValues:
|
|
||||||
raise EnumError("enum value {} not unique".format(x))
|
|
||||||
uniqueNames.add(x)
|
|
||||||
uniqueValues.add(i)
|
|
||||||
lookup[x] = i
|
|
||||||
reverseLookup[i] = x
|
|
||||||
i = i + 1
|
|
||||||
self.lookup = lookup
|
|
||||||
self.reverseLookup = reverseLookup
|
|
||||||
|
|
||||||
def __getattr__(self, attr):
|
|
||||||
result = self.lookup.get(attr)
|
|
||||||
if result is None:
|
|
||||||
raise AttributeError('enumeration has no member {}'.format(attr))
|
|
||||||
return result
|
|
||||||
|
|
||||||
def whatis(self, value):
|
|
||||||
return self.reverseLookup[value]
|
|
|
@ -27,15 +27,55 @@
|
||||||
"""Script-related classes and functions."""
|
"""Script-related classes and functions."""
|
||||||
|
|
||||||
|
|
||||||
import struct
|
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
from torba.server.enum import Enumeration
|
|
||||||
from torba.server.hash import hash160
|
|
||||||
from torba.server.util import unpack_le_uint16_from, unpack_le_uint32_from, \
|
from torba.server.util import unpack_le_uint16_from, unpack_le_uint32_from, \
|
||||||
pack_le_uint16, pack_le_uint32
|
pack_le_uint16, pack_le_uint32
|
||||||
|
|
||||||
|
|
||||||
|
class EnumError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Enumeration:
|
||||||
|
|
||||||
|
def __init__(self, name, enumList):
|
||||||
|
self.__doc__ = name
|
||||||
|
|
||||||
|
lookup = {}
|
||||||
|
reverseLookup = {}
|
||||||
|
i = 0
|
||||||
|
uniqueNames = set()
|
||||||
|
uniqueValues = set()
|
||||||
|
for x in enumList:
|
||||||
|
if isinstance(x, tuple):
|
||||||
|
x, i = x
|
||||||
|
if not isinstance(x, str):
|
||||||
|
raise EnumError("enum name {} not a string".format(x))
|
||||||
|
if not isinstance(i, int):
|
||||||
|
raise EnumError("enum value {} not an integer".format(i))
|
||||||
|
if x in uniqueNames:
|
||||||
|
raise EnumError("enum name {} not unique".format(x))
|
||||||
|
if i in uniqueValues:
|
||||||
|
raise EnumError("enum value {} not unique".format(x))
|
||||||
|
uniqueNames.add(x)
|
||||||
|
uniqueValues.add(i)
|
||||||
|
lookup[x] = i
|
||||||
|
reverseLookup[i] = x
|
||||||
|
i = i + 1
|
||||||
|
self.lookup = lookup
|
||||||
|
self.reverseLookup = reverseLookup
|
||||||
|
|
||||||
|
def __getattr__(self, attr):
|
||||||
|
result = self.lookup.get(attr)
|
||||||
|
if result is None:
|
||||||
|
raise AttributeError('enumeration has no member {}'.format(attr))
|
||||||
|
return result
|
||||||
|
|
||||||
|
def whatis(self, value):
|
||||||
|
return self.reverseLookup[value]
|
||||||
|
|
||||||
|
|
||||||
class ScriptError(Exception):
|
class ScriptError(Exception):
|
||||||
"""Exception used for script errors."""
|
"""Exception used for script errors."""
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue