source: proto/charsetcompiler/EBCDIC.py @ 4174

Last change on this file since 4174 was 2974, checked in by cameron, 6 years ago

Charset compiler clean-up

File size: 1000 bytes
Line 
1import codecs
2from charset_def import *
3def ascii2ebcdic_chardeflist(defs):
4        encoder = codecs.getencoder('cp037')
5        return [xlate_chardef(d, encoder) for d in defs]
6
7def xlate_char_or_range(charset_item, encoder):
8    if len(charset_item) == 1:
9        return encoder(charset_item[0])
10    elif len(charset_item) == 3:
11        if charset_item[1] == '-' and ord(charset_item[0]) <= ord(charset_item[2]):
12             return Make_Range(ord(charset_item[0]), ord(charset_item[2]))
13    print charset_item
14    raise BadCharSetItem
15       
16def xlate_chardef(chardef, encoder):
17  if isinstance(chardef, CharDef):
18    return CharDef(chardef.name, encoder(chardef.items[0])[0], chardef.complemented)
19  else:
20    cdefs = []
21    for item in chardef.items:
22        if len(item) == 1: cdefs.append(encoder(item)[0])
23        elif len(item) == 3:
24          for v in range(ord(item[0]), ord(item[-1])+1):
25            cdefs.append(encoder(chr(v))[0])
26        else: raise BadCharSetItem
27    return CharSetDef(chardef.name, cdefs, chardef.complemented)
28
29
Note: See TracBrowser for help on using the repository browser.