~sirn/fanboi2

ref: 7789db5cdfdad45cdee835212cfd2c1b07e3089a fanboi2/fanboi2/scripts/board_update.py -rw-r--r-- 2.6 KiB
7789db5cKridsada Thanabulpong Add utility for syncing topic bumped at. 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import json
import optparse
import os
import sys
import tempfile
import transaction
from pyramid.paster import setup_logging, get_appsettings
from sqlalchemy import engine_from_config
from sqlalchemy.orm.exc import NoResultFound
from subprocess import call
from fanboi2 import DBSession
from fanboi2.models import Board, JsonType


DESCRIPTION = "Update board settings."
USAGE = "Usage: %prog config arguments"


def main(argv=sys.argv):
    parser = optparse.OptionParser(usage=USAGE, description=DESCRIPTION)
    parser.add_option('-f', '--field', dest='field', type='string')
    parser.add_option('-s', '--slug', dest='slug', type='string')

    if not argv or len(argv) < 2:
        parser.print_help()
        sys.exit(1)

    config_uri = argv[1]
    argv = argv[2:]

    options, args = parser.parse_args(argv)
    if options.field is None:
        parser.error('You must provide --field')
    if options.slug is None:
        parser.error('You must provide --slug')

    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    with transaction.manager:
        original = None
        modified = None
        board = None
        serialized = False

        try:
            board = DBSession.query(Board).filter_by(slug=options.slug).one()
        except NoResultFound:
            print('No board with %s slug found.' % options.slug)
            sys.exit(1)

        try:
            original = getattr(board, options.field)
        except AttributeError:
            print('No field %s found in board.' % options.field)
            sys.exit(1)

        with tempfile.NamedTemporaryFile(suffix='.tmp') as tmp:
            if original is not None:
                if isinstance(getattr(Board, options.field).type, JsonType):
                    serialized = True
                    dumps = json.dumps(original, indent=4)
                    tmp.write(bytes(dumps.encode('utf8')))
                else:
                    tmp.write(bytes(str(original).encode('utf8')))
                tmp.flush()
            call([os.environ.get('EDITOR', 'vi'), tmp.name])
            modified = open(tmp.name, "r").read()

        if serialized:
            modified = json.loads(modified)
        else:
            original = original if original is not None else str()
            modified = modified.rstrip("\r\n")

        if modified == original:
            print('Not modified.')
        else:
            setattr(board, options.field, modified)
            DBSession.add(board)
            print('Successfully updated %s for %s.' %
                  (options.field, board.title))