From f2b4dd37129178e577c7d07bb972083d41e8944b Mon Sep 17 00:00:00 2001 From: Erik Osheim Date: Thu, 23 Jul 2009 16:26:59 -0400 Subject: [PATCH] lots of clean-up and documentation --HG-- branch : pmacs2 --- aesutil.py | 27 +++++++++++------ application.py | 6 ++++ cache.py | 31 ++++++++++--------- color.py | 33 +++++++++----------- mode/lily.py | 82 ++++++++++++++++++++++++++------------------------ util.py | 3 ++ 6 files changed, 100 insertions(+), 82 deletions(-) diff --git a/aesutil.py b/aesutil.py index fd18ef1..8e76ffc 100644 --- a/aesutil.py +++ b/aesutil.py @@ -5,31 +5,40 @@ try: except: has_aes = False +class CrypterInitError(Exception): + pass + class Crypter(object): - ALIGNMENT = 16 - def __init__(self, password, salt='aes.py'): + '''Wraps AES functionality provided by pycrypto''' + def __init__(self, password, salt='aes.py', alignment=16): + '''Initializes a crypter object''' if not has_aes: - raise Exception("pycrypto not installed") - self.password = password - self.salt = salt - self.hash = Crypto.Hash.SHA256.new(password + salt) - self.cipher = Crypto.Cipher.AES.new(self.hash.digest()) + raise CrypterInitError("pycrypto not installed") + self.password = password + self.salt = salt + self.hash = Crypto.Hash.SHA256.new(password + salt) + self.cipher = Crypto.Cipher.AES.new(self.hash.digest()) + self.alignment = alignment def pad(self, s): - xtra = len(s) % self.ALIGNMENT + '''Add NULL padding to create aligned data''' + xtra = len(s) % self.alignment if xtra: - return s + '\x00' * (self.ALIGNMENT - xtra) + return s + '\x00' * (self.alignment - xtra) else: return s def unpad(self, s): + '''Remove NULL padding to restore original data''' l = len(s) while l > 0 and s[l - 1] == '\x00': l -= 1 return s[:l] def encrypt(self, data): + '''Creates encrypted data string from original input''' return self.cipher.encrypt(self.pad(data)) def decrypt(self, data): + '''Creates original data string from encrypted input''' return self.unpad(self.cipher.decrypt(self.pad(data))) diff --git a/application.py b/application.py index f99b842..b6d2a30 100755 --- a/application.py +++ b/application.py @@ -1127,6 +1127,8 @@ if __name__ == "__main__": help='open arguments in MODE') parser.add_option('-x', '--exec', action='callback', callback=exec_cb, type='string', metavar='CMD', help='run CMD after launching') + parser.add_option('-T', '--term', dest='term', metavar='TYPE', + help='force pmacs to use TERM=TYPE') (opts, args) = parser.parse_args(argv) @@ -1134,6 +1136,10 @@ if __name__ == "__main__": if opts.debug: mode.DEBUG = True + # override $TERM if we need to + if opts.term: + os.putenv('TERM', opts.term) + # if -b but no -m, then use -m hex if opts.binary and not opts.mode: opts.mode = 'hex' diff --git a/cache.py b/cache.py index 0bbb1d6..f2083a0 100644 --- a/cache.py +++ b/cache.py @@ -5,31 +5,30 @@ class CacheDict(dict): constraints on its size. Once that size is reached, the key that was inserted or accessed the least recently is removed every time a new key is added.""" - def __init__(self, max_size=1000000): - '''CacheDict(max_size=1000000): build a cache''' - # once max_size is reached, the oldest cache entry will be - # pushed out to make room for each new one + def __init__(self, max_size=10000): + '''build a cache''' + # once max_size is reached, the oldest cache entry will be pushed out to + # make room for each new one. self.max_size = max_size dict.__init__(self) - # _times_dict will map keys to timestamps + # _times_dict will map keys to timestamps. self._times_dict = {} - # _times_list will store (timestamp, key) pairs in sorted - # order (oldest first) + # _times_list will store (timestamp, key) pairs, sorted oldest first. self._times_list = [] - def timestamp(self, key): - '''find the timestamp for key''' + def _timestamp_index(self, key): + '''find the index in the list of timestamps for the given key''' assert key in self - # construct a (timestamp, key) item item = (self._times_dict[key], key) # look for the item in the (sorted) list i = bisect.bisect_left(self._times_list, item) # make sure the index we are returning really is valid if item != self._times_list[i]: - raise LookupError + raise LookupError("key %r was not found" % key) return i def __getitem__(self, key): + '''implements d[key]''' # find the value in the dict value = dict.__getitem__(self, key) # do this to update the timestamp on this key @@ -37,9 +36,10 @@ class CacheDict(dict): return value def __setitem__(self, key, value): + '''implements d[key] = value''' # delete any old instance of the key to make way for the new if key in self: - del self._times_list[self.timestamp(key)] + del self._times_list[self._timestamp_index(key)] # remove old keys until we have enough space to add this one while len(self._times_list) >= self.max_size: key = self._times_list[0][1] @@ -53,8 +53,9 @@ class CacheDict(dict): bisect.insort_left(self._times_list, (t, key)) def __delitem__(self, key): - # we need to make sure we delete this key out of all three of - # our data structures - del self._times_list[self.timestamp(key)] + '''implements del d[key]''' + # we need to make sure we delete this key out of all three of our data + # structures + del self._times_list[self._timestamp_index(key)] del self._times_dict[key] dict.__delitem__(self, key) diff --git a/color.py b/color.py index 4ee2776..80609a2 100644 --- a/color.py +++ b/color.py @@ -13,7 +13,7 @@ attributes = { 'standout': curses.A_STANDOUT, } -inited = False +inited = False default_color = True ascii_map = { @@ -23,11 +23,14 @@ rev_ascii_map = { 'bold': '*', } +# add a particular color def add_color(name, value, abbrev): colors[name] = value ascii_map[abbrev] = name rev_ascii_map[name] = abbrev +# assign every RGB triple (0-5) to one of six basic colors (red, yellow, green, +# cyan, blue, magenta) based on some semi-arbitrary rules i came up with. def iter256(): for r in range(0, 6): for g in range(0, 6): @@ -50,26 +53,25 @@ def iter256(): yield (name, r, g, b) raise StopIteration +# if we can support 256 colors, create all of the color names and map them to +# the correct RGB value. def color256(name, fallback, abbrev, r, g, b): name2 = '%s%d%d%d' % (name, r, g, b) abbrev2 = '%s%d%d%d' % (abbrev, r, g, b) if curses.COLORS == 256: value = 16 + r * 36 + g * 6 + b if curses.can_change_color(): - try: - curses.init_color(value, r * 200, g * 200, b* 200) - except: - raise Exception("arghh: %d, %d, %d, %d", value, r * 200, g * 200, b * 200) + curses.init_color(value, r * 200, g * 200, b* 200) else: value = fallback add_color(name2, value, abbrev2) -# PUBLIC METHODS def init(): global _pairs, inited - if inited: - return + if inited: return + inited = True + # create the basic 8 colors of curses add_color('cyan', curses.COLOR_CYAN, 'c') add_color('blue', curses.COLOR_BLUE, 'b') add_color('green', curses.COLOR_GREEN, 'g') @@ -79,33 +81,31 @@ def init(): add_color('black', curses.COLOR_BLACK, 'B') add_color('white', curses.COLOR_WHITE, 'w') + # initialize the "default" color if possible if default_color: colors['default'] = -1 ascii_map['d'] = 'default' rev_ascii_map['default'] = 'd' - inited = True - + # add in hex aliases to 256 colors; used by color-data buffers for i in range(0, 256): name = 'f%02x' % i abbrev = 'f%02x' % i add_color(name, i, abbrev) - # GREY + # create the 24 flavors of grey in 256 colors for i in range(0, 24): name2 = 'grey%d' % i abbrev2 = 'G%d' % i if curses.COLORS == 256: value = 232 + i if curses.can_change_color(): - try: - curses.init_color(value, i * 41, i * 41, i * 41) - except: - raise Exception("arghh: %d, %d, %d, %d", value, i * 41, i * 41, i * 41) + curses.init_color(value, i * 41, i * 41, i * 41) else: value = curses.COLOR_WHITE add_color(name2, value, abbrev2) + # create 256 colors for name, r, g, b in iter256(): color256(name, colors[name], rev_ascii_map[name], r, g, b) @@ -117,9 +117,6 @@ def build(fg, bg, *attr): v = v | x return v - return build_attr(cattr, *attr) - -# PRIVATE METHODS def pairs(fg, bg): if not curses.has_colors(): return curses.color_pair(0) diff --git a/mode/lily.py b/mode/lily.py index 54cc21f..8a9918d 100644 --- a/mode/lily.py +++ b/mode/lily.py @@ -17,30 +17,32 @@ class LilyGrammar(Grammar): RegionRule(r'comment', r'%\{', CommentGrammar, r'%\}'), RegionRule(r'string', r'#?"', StringGrammar2, r'"'), PatternRule(r'boolean', r'#?#[tf]'), - PatternMatchRule(r'x', r'(\\new)( +)([a-zA-Z_]+)', r'directive', r'spaces', r'lily_class'), - PatternMatchRule(r'x', r'(\\set|\\override)( +)([a-zA-Z_]+)', r'directive', r'spaces', r'lily_var'), - PatternRule(r'tie', r'~'), - PatternRule(r'augmentation', r'\.+'), - PatternRule(r'directive', r'\\[a-zA-Z_][a-zA-Z_0-9]*'), + PatternMatchRule(r'x', r'(\\new)( +)([a-zA-Z_]+)', r'lily.directive', + r'spaces', r'lily.class'), + PatternMatchRule(r'x', r'(\\set|\\override)( +)([a-zA-Z_]+)', + r'lily.directive', r'spaces', r'lily.var'), + PatternRule(r'lily.tie', r'~'), + PatternRule(r'lily.augmentation', r'\.+'), + PatternRule(r'lily.directive', r'\\[a-zA-Z_][a-zA-Z_0-9]*'), PatternRule(r'delimiter', r'(?:[={}]|<<|>>)'), - RegionRule(r'text', r'^"', StringGrammar2, r'"'), - RegionRule(r'markup', r'(?<=\\markup) *{', MarkupGrammar, '}'), - PatternRule(r'huh', r'\\!'), + RegionRule(r'lily.text', r'^"', StringGrammar2, r'"'), + RegionRule(r'lily.markup', r'(?<=\\markup) *{', MarkupGrammar, '}'), + PatternRule(r'lily.huh', r'\\!'), PatternRule(r'spaces', ' +'), PatternRule(r'eol', '\n'), - PatternRule(r'chord', r'[<>]'), - PatternRule(r'beam', r'[\[\]]'), - PatternRule(r'slur', r'[()]'), - PatternRule(r'pslur', r'\\[()]'), - PatternRule(r'articulation', r'[-_+][-.>^+_]'), - PatternRule(r'fingering', r'[-_+][0-9]+'), - PatternRule(r'time', r'[0-9]+/[0-9]+'), - PatternRule(r'note', r"[a-g][sf]?[,']*[0-9]*(?![a-z-])"), - PatternRule(r'rest', r"r[0-9]*(?![a-z-])"), - PatternRule(r'srest', r"s[0-9]*(?![a-z-])"), - PatternRule(r'measure', r'(?:[0-9]+\.)?[0-9]+\\(?:mm|cm|in)'), - RegionRule(r'scheme', r'#\(', SchemeGrammar, '\)'), - PatternRule(r'bareword', r'[a-zA-Z][a-zA-Z_0-9-]*[a-zA-Z0-9]'), + PatternRule(r'lily.chord', r'[<>]'), + PatternRule(r'lily.beam', r'[\[\]]'), + PatternRule(r'lily.slur', r'[()]'), + PatternRule(r'lily.pslur', r'\\[()]'), + PatternRule(r'lily.articulation', r'[-_+][-.>^+_]'), + PatternRule(r'lily.fingering', r'[-_+][0-9]+'), + PatternRule(r'lily.time', r'[0-9]+/[0-9]+'), + PatternRule(r'lily.note', r"[a-g][sf]?[,']*[0-9]*(?![a-z-])"), + PatternRule(r'lily.rest', r"r[0-9]*(?![a-z-])"), + PatternRule(r'lily.srest', r"s[0-9]*(?![a-z-])"), + PatternRule(r'lily.measure', r'(?:[0-9]+\.)?[0-9]+\\(?:mm|cm|in)'), + RegionRule(r'lily.scheme', r'#\(', SchemeGrammar, '\)'), + PatternRule(r'lily.bareword', r'[a-zA-Z][a-zA-Z_0-9-]*[a-zA-Z0-9]'), ] class LilyTabber(tab.StackTabber): @@ -57,25 +59,25 @@ class Lily(mode.Fundamental): grammar = LilyGrammar commentc = '%' colors = { - 'boolean': ('magenta', 'default', 'bold'), - 'lily_class': ('magenta', 'default', 'bold'), - 'lily_var': ('cyan', 'default', 'bold'), - 'directive': ('blue', 'default', 'bold'), - 'markup.start': ('cyan', 'default'), - 'markup.data': ('cyan', 'default'), - 'markup.end': ('cyan', 'default'), - 'time': ('yellow', 'default'), - 'note': ('yellow', 'default', 'bold'), - 'rest': ('yellow', 'default'), - 'srest': ('yellow', 'default'), - 'scheme.start': ('magenta', 'default'), - 'scheme.end': ('magenta', 'default'), - 'measure': ('green', 'default'), - 'beam': ('red', 'default'), - 'slur': ('red', 'default'), - 'plur': ('red', 'default'), - 'tie': ('red', 'default'), - 'huh': ('red', 'default'), + 'lily.boolean': ('magenta', 'default', 'bold'), + 'lily.class': ('magenta', 'default', 'bold'), + 'lily.var': ('cyan', 'default', 'bold'), + 'lily.directive': ('blue', 'default', 'bold'), + 'lily.markup.start': ('cyan', 'default'), + 'lily.markup.data': ('cyan', 'default'), + 'lily.markup.end': ('cyan', 'default'), + 'lily.time': ('yellow', 'default'), + 'lily.note': ('yellow', 'default', 'bold'), + 'lily.rest': ('yellow', 'default'), + 'lily.srest': ('yellow', 'default'), + 'lily.scheme.start': ('magenta', 'default'), + 'lily.scheme.end': ('magenta', 'default'), + 'lily.measure': ('green', 'default'), + 'lily.beam': ('red', 'default'), + 'lily.slur': ('red', 'default'), + 'lily.plur': ('red', 'default'), + 'lily.tie': ('red', 'default'), + 'lily.huh': ('red', 'default'), } opentokens = ('delimiter',) opentags = {'<<': '>>', '{': '}'} diff --git a/util.py b/util.py index 4f04252..0131cea 100644 --- a/util.py +++ b/util.py @@ -3,6 +3,8 @@ import pwd import re import regex +# character buffers use sequences like [r:d]foo[d:d] to indicate that foo +# should be fg=red,bg=default. as such, we have to escape \, [ and ]. cbuf_re = re.compile(r'[\[\]\\]') def cbuf_escape(s): '''escape characters which have special meaning in color buffers''' @@ -68,6 +70,7 @@ def count_leading_whitespace(s): assert m, "count leading whitespace failed somehow" return m.end() - m.start() +# TODO: move these somewhere more sensible. they aren't really utilities. def get_margin_limit(w, def_limit=80): lname = '%s.margin' % w.mode.name.lower() if lname in w.application.config: