def get_messages_by_subscription(session, subscription): all_tags = list_tags() messages = {'all': set(), 'new': set()} messages_bytag = {} messages_read = session.user.get('readmsgs', set()) # this looks like perl code for tag_pattern in subscription: messages_bytag[tag_pattern] = collections.defaultdict(set) for tag_match in fnmatch.filter(all_tags, tag_pattern): msg_indicies = list_msgs(tags=(tag_match,)) messages['all'].update(msg_indicies) messages_bytag[tag_pattern]['all'].update(msg_indicies) messages_bytag[tag_pattern]['new'] = ( messages_bytag[tag_pattern]['all'] - messages_read) # now occlude all private messages :) all_private = list_privmsgs(None) messages['new'] -= all_private messages['all'] -= all_private # and make a list of only our own messages['private'] = list_privmsgs(session.user.handle) # and calculate 'new' messages messages['new'] = (messages['all'] | messages['private']) - messages_read return messages, messages_bytag
def get_messages_by_subscription(session, subscription): all_tags = list_tags() messages = {'all': set(), 'new': set()} messages_bytag = {} messages_read = session.user.get('readmsgs', set()) # now occlude all private messages :) all_private = list_privmsgs(None) # this looks like perl code for tag_pattern in subscription: messages_bytag[tag_pattern] = collections.defaultdict(set) for tag_match in fnmatch.filter(all_tags, tag_pattern): msg_indicies = list_msgs(tags=(tag_match, )) messages['all'].update(msg_indicies - all_private) messages_bytag[tag_pattern]['all'].update(msg_indicies - all_private) messages_bytag[tag_pattern]['new'] = ( messages_bytag[tag_pattern]['all'] - messages_read) # and make a list of only our own messages['private'] = list_privmsgs(session.user.handle) # and calculate 'new' messages messages['new'] = (messages['all'] | messages['private']) - messages_read return messages, messages_bytag
def validate_tag_patterns(tag_patterns): all_tags = list_tags() or set([u'public']) removed = [] for tag_pattern in set(tag_patterns): if not fnmatch.filter(all_tags, tag_pattern): removed.append(tag_pattern) tag_patterns.remove(tag_pattern) return removed, tag_patterns
def prompt_tags(session, term, msg, colors, public=True): xpos = max(0, (term.width // 2) - (80 // 2)) # conditionally enforce tag moderation moderated = get_ini("msg", "moderated_tags", getter="getboolean") tag_moderators = set(get_ini("msg", "tag_moderators", split=True)) # enforce 'public' tag if public and "public" not in msg.tags: msg.tags.add("public") elif not public and "public" in msg.tags: msg.tags.remove("public") # describe all available tags, as we oft want to do. do_describe_available_tags(term, colors) # and remind ourselves of the available network tags, description = get_network_tag_description(term, colors) if description: show_description(term=term, color=None, description=description) echo( u"".join( (term.move_x(xpos), term.clear_eos, u"Enter tags, separated by commas.\r\n", term.move_x(xpos), u":: ") ) ) all_tags = list_tags() while True: inp = LineEditor( subject_max_length, u", ".join(sorted(msg.tags)), colors={"highlight": colors["backlight"]} ).read() if inp is None: echo(u"".join((term.move_x(xpos), colors["highlight"]("Message canceled."), term.clear_eol))) term.inkey(1) return False msg.tags = set(filter(None, set(map(unicode.strip, inp.split(","))))) if moderated and not (tag_moderators | session.user.groups): cannot_tag = [_tag for _tag in msg.tags if _tag not in all_tags] if cannot_tag: echo( u"".join( ( u"\r\n", term.move_x(xpos), u", ".join((quote(tag, colors) for tag in cannot_tag)), u": not allowed; this system is moderated.", ) ) ) term.inkey(2) echo(term.move_up) map(msg.tags.remove, cannot_tag) continue return True
def do_describe_available_tags(term, colors): sorted_tags = sorted([(len(list_msgs(tags=(tag,))), tag) for tag in list_tags() or [u"public"]], reverse=True) decorated_tags = [ colors["text"](tag) + colors["lowlight"]("({0})".format(num_msgs)) for num_msgs, tag in sorted_tags ] description = u"".join( (colors["highlight"](u"available tags"), ": ", colors["text"](u", ").join(decorated_tags), colors["text"](u".")) ) return show_description(term, description, color=None)
def prompt_tags(session, term, msg, colors, public=True): xpos = max(0, (term.width // 2) - (80 // 2)) # conditionally enforce tag moderation moderated = get_ini('msg', 'moderated_tags', getter='getboolean') tag_moderators = set(get_ini('msg', 'tag_moderators', split=True)) # enforce 'public' tag if public and 'public' not in msg.tags: msg.tags.add('public') elif not public and 'public' in msg.tags: msg.tags.remove('public') # describe all available tags, as we oft want to do. do_describe_available_tags(term, colors) # and remind ourselves of the available network tags, description = get_network_tag_description(term, colors) if description: show_description(term=term, color=None, description=description) echo(u''.join( (term.move_x(xpos), term.clear_eos, u'Enter tags, separated by commas.\r\n', term.move_x(xpos), u':: '))) all_tags = list_tags() while True: inp = LineEditor(subject_max_length, u', '.join(sorted(msg.tags)), colors={ 'highlight': colors['backlight'] }).read() if inp is None: echo(u''.join( (term.move_x(xpos), colors['highlight']('Message canceled.'), term.clear_eol))) term.inkey(1) return False msg.tags = set(filter(None, set(map(unicode.strip, inp.split(','))))) if moderated and not (tag_moderators | session.user.groups): cannot_tag = [_tag for _tag in msg.tags if _tag not in all_tags] if cannot_tag: echo(u''.join((u'\r\n', term.move_x(xpos), u', '.join( (quote(tag, colors) for tag in cannot_tag)), u': not allowed; this system is moderated.'))) term.inkey(2) echo(term.move_up) map(msg.tags.remove, cannot_tag) continue return True
def do_describe_available_tags(term, colors): sorted_tags = sorted([(len(list_msgs(tags=(tag,))), tag) for tag in list_tags() or [u'public'] ], reverse=True) decorated_tags = [ colors['text'](tag) + colors['lowlight']('({0})'.format(num_msgs)) for num_msgs, tag in sorted_tags] description = u''.join(( colors['highlight'](u'available tags'), ': ', colors['text'](u', ').join(decorated_tags), colors['text'](u'.'), )) return show_description(term, description, color=None)
def do_describe_available_tags(term, colors): sorted_tags = sorted([(len(list_msgs(tags=(tag, ))), tag) for tag in list_tags() or [u'public']], reverse=True) decorated_tags = [ colors['text'](tag) + colors['lowlight']('({0})'.format(num_msgs)) for num_msgs, tag in sorted_tags ] description = u''.join(( colors['highlight'](u'available tags'), ': ', colors['text'](u', ').join(decorated_tags), colors['text'](u'.'), )) return show_description(term, description, color=None)
def set_subscription_tags(session, term): """ This function is called to assign a new set of subscription tags for a user. If escape is pressed, the existing value is used, or '*' is used if not previously set. This should be called for first-time users, and optionally at any later time to change a subscription. """ line_no = display_banner(art_file, encoding=art_encoding) # introduce the user to the concept ... description = ( u"{term.yellow}You can think of tags as a system of providing " u"context to any message stored on this system. A tag might signify " u"that it was received on a particular message network, or it might " u"provide the topic of conversation, such as " u"{term.bold_yellow}python{term.normal}{term.yellow} or " u"{term.bold_yellow}rock music{term.normal}{term.yellow}. " u"This is similar to flicker's tags, or twitter hashtags. " u"Use glob expressions as a comma-delimited list, for example, " u"the expression {term.bold_yellow}x84net{term.normal}{term.yellow}, " u"{term.bold_yellow}sysop{term.normal}{term.yellow}, " u"{term.bold_yellow}python*{term.normal}{term.yellow} " u"will subscribe to all tags of the x84net message network, " u"sysop messages, and any topics that begin with the phrase " u"{term.bold_yellow}python{term.normal}{term.yellow}. You can " u"subscribe to all topics using the expression, " u"{term.bold_yellow}*{term.normal}{term.yellow}.".format(term=term) ) echo(u"\r\n") line_no += 1 line_no += show_description(description, color=None) description = u" ".join( [u"tags available:"] + ["{tag}{term.yellow},{term.normal}".format(tag=tag, term=term) for tag in list_tags()] ) echo(u"\r\n\r\n") line_no += 2 # display our prompt prefix, input_prefix: input_prefix = u" {sep} {key:>18}: ".format(sep=term.bright_yellow(u"::"), key="subscription tags") echo(input_prefix) xloc = term.length(input_prefix) # and prompt an editor on that row editor = ScrollingEditor( xloc=xloc, yloc=line_no, width=(term.width - xloc - 2), colors={"highlight": term.black_on_yellow} ) value = editor.read() or u"" echo(term.normal + u"\r\n\r\n") return map(unicode.strip, value.split(","))