Ejemplo n.º 1
0
def decorations_edit(cfg: Config, lang_filter: Sequence[str], name: str,
                     regex: str, decoration: str):
    """Edit existing decoration NAME."""
    events = get_events(cfg)

    for i, dec in enumerate(events['decorations']):
        if dec['name'] == name:
            regex, decoration = check_dec_args(
                regex or dec['regex'], decoration
                or json.dumps(dec['decoration']))
            new_dec = {
                'name': dec['name'],
                'filter': lang_filter or dec['filter'],
                'regex': regex,
                'decoration': decoration
            }
            print('{}\n{}\n{}'.format(
                DECORATION_FORMAT.format('Name', 'Filters', 'Regex',
                                         'Decoration'),
                DECORATION_FORMAT.format('<FROM', str(dec['filter']),
                                         dec['regex'],
                                         json.dumps(dec['decoration'])),
                DECORATION_FORMAT.format('>TO', str(new_dec['filter']),
                                         new_dec['regex'],
                                         json.dumps(new_dec['decoration']))))
            if are_you_sure('edit decoration: {}'.format(dec['name']), cfg):
                events['decoration'][i] = new_dec
                save_event_file(cfg, json.dumps(events))
            break
Ejemplo n.º 2
0
def decorations_list(cfg: Config):
    events = get_events(cfg)
    print(DECORATION_FORMAT.format('Name', 'Filters', 'Regex', 'Decoration'))
    for dec in events['decorations']:
        print(
            DECORATION_FORMAT.format(dec['name'],
                                     str(dec['filter']), dec['regex'],
                                     json.dumps(dec['decoration'])))
Ejemplo n.º 3
0
Archivo: motd.py Proyecto: ojeda/infra
def motd_update(cfg: Config, message: str):
    """Updates the message of the day to MESSAGE."""
    events = get_events(cfg)
    if are_you_sure(
            'update motd from: {} to: {}'.format(events['motd'], message),
            cfg):
        events['motd'] = message
        save_event_file(cfg, json.dumps(events))
Ejemplo n.º 4
0
def decorations_clear(cfg: Config):
    """Clear all decorations."""
    events = get_events(cfg)
    if are_you_sure(
            'clear all decorations (count: {})'.format(
                len(events['decorations'])), cfg):
        events['decorations'] = []
        save_event_file(cfg, json.dumps(events))
Ejemplo n.º 5
0
def ads_remove(cfg: Config, ad_id: int, force: bool):
    """Remove community ad number AD_ID."""
    events = get_events(cfg)
    for i, ad in enumerate(events['ads']):
        if ad['id'] == ad_id:
            if force or \
                    are_you_sure('remove ad: {}'.format(ADS_FORMAT.format(ad['id'], str(ad['filter']), ad['html'])),
                                 cfg):
                del events['ads'][i]
                save_event_file(cfg, json.dumps(events))
            break
Ejemplo n.º 6
0
def ads_add(cfg: Config, lang_filter: Sequence[str], html: str):
    """Add a community advert with HTML."""
    events = get_events(cfg)
    new_ad = {
        'html': html,
        'filter': lang_filter,
        'id': max([x['id'] for x in events['ads']]) + 1 if len(events['ads']) > 0 else 0
    }
    if are_you_sure('add ad: {}'.format(ADS_FORMAT.format(new_ad['id'], str(new_ad['filter']), new_ad['html'])), cfg):
        events['ads'].append(new_ad)
        save_event_file(cfg, json.dumps(events))
Ejemplo n.º 7
0
def decorations_remove(cfg: Config, name: str, force: bool):
    """Remove a decoration."""
    events = get_events(cfg)
    for i, dec in enumerate(events['decorations']):
        if dec['name'] == name:
            if force or \
                    are_you_sure('remove decoration: {}'.format(
                        DECORATION_FORMAT.format(dec['name'], str(dec['filter']), dec['regex'],
                                                 json.dumps(dec['decoration']))), cfg):
                del events['decorations'][i]
                save_event_file(cfg, json.dumps(events))
            break
Ejemplo n.º 8
0
def ads_edit(cfg: Config, ad_id: int, html: str, lang_filter: Sequence[str]):
    """Edit community ad AD_ID."""
    events = get_events(cfg)
    for i, ad in enumerate(events['ads']):
        if ad['id'] == ad_id:
            new_ad = {
                'id': ad['id'],
                'filter': lang_filter or ad['filter'],
                'html': html or ad['html']
            }
            print('{}\n{}\n{}'.format(ADS_FORMAT.format('Event', 'Filter(s)', 'HTML'),
                                      ADS_FORMAT.format('<FROM', str(ad['filter']), ad['html']),
                                      ADS_FORMAT.format('>TO', str(new_ad['filter']), new_ad['html'])))
            if are_you_sure('edit ad id: {}'.format(ad['id']), cfg):
                events['ads'][i] = new_ad
                save_event_file(cfg, json.dumps(events))
            break
Ejemplo n.º 9
0
def decorations_add(cfg: Config, lang_filter: Sequence[str], name: str,
                    regex: str, decoration: str):
    """
    Add a decoration called NAME matching REGEX resulting in json DECORATION.
    """
    events = get_events(cfg)
    if name in [d['name'] for d in events['decorations']]:
        raise RuntimeError(f'Duplicate decoration name {name}')
    regex, decoration = check_dec_args(regex, decoration)

    new_decoration = {
        'name': name,
        'filter': lang_filter,
        'regex': regex,
        'decoration': decoration
    }
    if are_you_sure(
            'add decoration: {}'.format(
                DECORATION_FORMAT.format(
                    new_decoration['name'], str(new_decoration['filter']),
                    new_decoration['regex'],
                    json.dumps(new_decoration['decoration']))), cfg):
        events['decorations'].append(new_decoration)
        save_event_file(cfg, json.dumps(events))
Ejemplo n.º 10
0
Archivo: motd.py Proyecto: ojeda/infra
def motd_clear(cfg: Config):
    """Clears the message of the day."""
    events = get_events(cfg)
    if are_you_sure('clear current motd: {}'.format(events['motd']), cfg):
        events['motd'] = ''
        save_events(cfg, events)
Ejemplo n.º 11
0
Archivo: motd.py Proyecto: ojeda/infra
def motd_show(cfg: Config):
    """Prints the message of the day."""
    events = get_events(cfg)
    print('Current motd: "{}"'.format(events['motd']))
Ejemplo n.º 12
0
def ads_clear(cfg: Config):
    """Clear all community ads."""
    events = get_events(cfg)
    if are_you_sure('clear all ads (count: {})'.format(len(events['ads'])), cfg):
        events['ads'] = []
        save_event_file(cfg, json.dumps(events))
Ejemplo n.º 13
0
def ads_list(cfg: Config):
    """List the existing community adverts."""
    events = get_events(cfg)
    print(ADS_FORMAT.format('ID', 'Filters', 'HTML'))
    for ad in events['ads']:
        print(ADS_FORMAT.format(ad['id'], str(ad['filter']), ad['html']))