def mod_desc(stmt, text): desc = stmt.search_one('description') if desc: desc.arg += ' ' + text else: top = stmt.top or stmt desc = statements.new_statement(top, stmt, stmt.pos, 'description', text) stmt.substmts.append(desc) # XXX there may be a better idiom for this if hasattr(stmt, 'i_children'): for child in stmt.i_children: mod_desc(child, text)
def update_or_add_stmt(stmt, keyword, arg, index=None): child = stmt.search_one(keyword) currarg = child.arg if child else None (argval, replace) = get_arg_value(arg, currarg) if argval is None: child = None elif child: if not replace and child.arg and child.arg != argval and child.arg \ != 'TBD': sys.stderr.write('%s: not replacing existing %s %r with %r\n' % (child.pos, keyword, child.arg, argval)) else: child.arg = argval else: child = statements.new_statement(stmt.top, stmt, None, keyword, argval) if index is None: index = len(stmt.substmts) # XXX this hack ensures that 'reference' is always last if index > 0 and stmt.substmts[index - 1].keyword == 'reference': index -= 1 stmt.substmts.insert(index, child) return child
def add_type(leaf, type_name): type_ = statements.new_statement(leaf.top, leaf, leaf.pos, 'type', type_name) leaf.substmts.append(type_) return type_
def add_leaf(parent, name, type_name): leaf = statements.new_statement(parent.top, parent, parent.pos, 'leaf', name) parent.substmts.append(leaf) add_type(leaf, type_name) return leaf
def set_revision_details(ctx, stmt, lastrev): revision_done = False # relevant options opts = { 'olddate': ctx.opts.edit_delete_revisions_after, 'newdate': ctx.opts.edit_revision_date, 'description': ctx.opts.edit_revision_description, 'reference': ctx.opts.edit_revision_reference } # the logic is quite tricky; here's what we want to achieve: # * 'olddate' is the date of the oldest revision to be retained; if not # supplied, any existing revisions are deleted # * if 'newdate' is supplied, it's the date of the next published # revision and is to be inserted at the start of any remaining # revisions # * reuse rather than delete the oldest revision statement, purely in # order to retain any blank lines after it # default action is to do nothing action = '' #sys.stderr.write('revision %s (lastrev %s)\n' % (stmt.arg, lastrev)) # only adjust revisions if either olddate or newdate is supplied olddate = opts.get('olddate', None) newdate = opts.get('newdate', None) if olddate is not None or newdate is not None: # determine whether to delete this old revision if olddate is None or stmt.arg > olddate: action = 'delete' #sys.stderr.write('-> delete (olddate %s)\n' % olddate) # determine whether to insert the new revision if newdate is not None and (action != 'delete' or lastrev): action = 'replace' if action == 'delete' else 'insert' #sys.stderr.write('-> %s (newdate %s)\n' % (action, newdate)) # if deleting, return an empty list replstmts = None if action == 'delete': replstmts = [] # replace and insert logic is quite similar: # * if replacing, modify this statement and return a list containing # only it # * if inserting, create a new statement and return a list containing # the new and the original statement elif action == 'replace' or action == 'insert': if action == 'replace': revstmt = stmt revstmt.arg = newdate else: revstmt = statements.new_statement(stmt.top, stmt.parent, None, 'revision', newdate) other_keywords = set(opts.keys()) - {'olddate', 'newdate'} for keyword in other_keywords: update_or_add_stmt(revstmt, keyword, opts[keyword]) if action == 'replace': replstmts = [revstmt] else: replstmts = [revstmt, stmt] revision_done = True #sys.stderr.write( # '= %s\n' % ([s.arg for s in replstmts] if replstmts else None)) return replstmts, revision_done