unicode support
This commit is contained in:
parent
926e95143c
commit
2244527665
29
protocol
29
protocol
|
@ -1,4 +1,5 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
################################################################################
|
################################################################################
|
||||||
# ____ _ _ #
|
# ____ _ _ #
|
||||||
# | _ \ _ __ ___ | |_ ___ ___ ___ | | #
|
# | _ \ _ __ ___ | |_ ___ ___ ___ | | #
|
||||||
|
@ -63,6 +64,8 @@ from datetime import date
|
||||||
from constants import *
|
from constants import *
|
||||||
import specs
|
import specs
|
||||||
|
|
||||||
|
reload(sys)
|
||||||
|
sys.setdefaultencoding('utf8')
|
||||||
|
|
||||||
# CLASS DEFINITIONS
|
# CLASS DEFINITIONS
|
||||||
class ProtocolException(Exception):
|
class ProtocolException(Exception):
|
||||||
|
@ -72,8 +75,8 @@ class ProtocolException(Exception):
|
||||||
def __init__(self, errmsg):
|
def __init__(self, errmsg):
|
||||||
self.errmsg=errmsg
|
self.errmsg=errmsg
|
||||||
|
|
||||||
def __str__(self):
|
def __unicode__(self):
|
||||||
return str(self.errmsg)
|
return self.errmsg
|
||||||
|
|
||||||
|
|
||||||
class Protocol():
|
class Protocol():
|
||||||
|
@ -194,6 +197,7 @@ class Protocol():
|
||||||
else:
|
else:
|
||||||
raise ProtocolException("FATAL: Invalid value for 'numbers' option (%s)" % value)
|
raise ProtocolException("FATAL: Invalid value for 'numbers' option (%s)" % value)
|
||||||
elif var.lower() in ["oddchar", "evenchar", "startchar", "endchar", "sepchar"]:
|
elif var.lower() in ["oddchar", "evenchar", "startchar", "endchar", "sepchar"]:
|
||||||
|
value = value.decode("utf-8")
|
||||||
if len(value)>1 or len(value)<=0:
|
if len(value)>1 or len(value)<=0:
|
||||||
raise ProtocolException("FATAL: Invalid value for '%s' option (%s)" % (var, value))
|
raise ProtocolException("FATAL: Invalid value for '%s' option (%s)" % (var, value))
|
||||||
else:
|
else:
|
||||||
|
@ -272,7 +276,7 @@ class Protocol():
|
||||||
Processes the list of protocol fields that we got from the spec and turns
|
Processes the list of protocol fields that we got from the spec and turns
|
||||||
it into something that we can print easily (useful for cases when we have
|
it into something that we can print easily (useful for cases when we have
|
||||||
protocol fields that span more than one line). This is just a helper
|
protocol fields that span more than one line). This is just a helper
|
||||||
function to make __str__()'s life easier.
|
function to make __unicode__()'s life easier.
|
||||||
"""
|
"""
|
||||||
new_fields=[]
|
new_fields=[]
|
||||||
bits_in_line=0
|
bits_in_line=0
|
||||||
|
@ -330,7 +334,7 @@ class Protocol():
|
||||||
|
|
||||||
|
|
||||||
# Convert to string
|
# Convert to string
|
||||||
def __str__(self):
|
def __unicode__(self):
|
||||||
"""
|
"""
|
||||||
Converts the protocol specification stored in the object to a nice
|
Converts the protocol specification stored in the object to a nice
|
||||||
ASCII diagram like the ones that appear in RFCs. Conversion supports
|
ASCII diagram like the ones that appear in RFCs. Conversion supports
|
||||||
|
@ -366,9 +370,9 @@ class Protocol():
|
||||||
|
|
||||||
# If the field text is too long, we truncate it, and add a dot
|
# If the field text is too long, we truncate it, and add a dot
|
||||||
# at the end.
|
# at the end.
|
||||||
if len(field_text) > (field_len*2)-1:
|
if len(field_text.decode("utf-8")) > (field_len*2)-1:
|
||||||
field_text=field_text[0:(field_len*2)-1]
|
field_text=field_text[0:(field_len*2)-1]
|
||||||
if len(field_text)>1:
|
if len(field_text.decode("utf-8"))>1:
|
||||||
field_text=field_text[0:-1]+"."
|
field_text=field_text[0:-1]+"."
|
||||||
|
|
||||||
# If we have space for the whole field in the current line, go
|
# If we have space for the whole field in the current line, go
|
||||||
|
@ -383,7 +387,7 @@ class Protocol():
|
||||||
current_line+=self._get_separator()
|
current_line+=self._get_separator()
|
||||||
|
|
||||||
# Add the whole field
|
# Add the whole field
|
||||||
current_line+=str.center(field_text, (field_len*2)-1)
|
current_line+=str.center(field_text, (field_len*2)-1-len(field_text.decode("utf8"))+len(field_text))
|
||||||
|
|
||||||
# Update counters
|
# Update counters
|
||||||
bits_in_line+=field_len
|
bits_in_line+=field_len
|
||||||
|
@ -465,14 +469,16 @@ class Protocol():
|
||||||
start_line=" "*self.do_print_line_number+self.hdr_char_start
|
start_line=" "*self.do_print_line_number+self.hdr_char_start
|
||||||
end_line=self.hdr_char_end
|
end_line=self.hdr_char_end
|
||||||
else:
|
else:
|
||||||
start_line=str(line_number).rjust(self.do_print_line_number-1)+" "+self.hdr_char_sep
|
start_line=self.hdr_char_sep
|
||||||
line_number+=1
|
if self.do_print_line_number:
|
||||||
|
start_line=str(line_number).rjust(self.do_print_line_number-1)+" "+start_line
|
||||||
|
line_number+=1
|
||||||
end_line=self.hdr_char_sep
|
end_line=self.hdr_char_sep
|
||||||
|
|
||||||
# This is the line where we need to print the field
|
# This is the line where we need to print the field
|
||||||
# text.
|
# text.
|
||||||
if i == central_line:
|
if i == central_line:
|
||||||
lines.append(start_line + str.center(field_text, (self.bits_per_line*2)-1) + end_line)
|
lines.append(start_line + str.center(field_text, (self.bits_per_line*2)-1-len(field_text.decode("utf8"))+len(field_text)) + end_line)
|
||||||
# This is a line we need to leave blank
|
# This is a line we need to leave blank
|
||||||
else:
|
else:
|
||||||
lines.append(start_line + (" " * ((self.bits_per_line*2)-1)) + end_line)
|
lines.append(start_line + (" " * ((self.bits_per_line*2)-1)) + end_line)
|
||||||
|
@ -490,7 +496,8 @@ class Protocol():
|
||||||
|
|
||||||
result= "\n".join(lines)
|
result= "\n".join(lines)
|
||||||
return result
|
return result
|
||||||
|
def __str__(self):
|
||||||
|
return unicode(self).encode('utf-8')
|
||||||
|
|
||||||
class Main():
|
class Main():
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user