Mercurial > mamba
annotate mamba/forest.py @ 574:e6344f57886e
Try add colours to the irker messages.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Thu, 22 Nov 2012 00:57:15 +0200 |
parents | 1b720cf4d730 |
children | 1306f7d8ed35 |
rev | line source |
---|---|
328 | 1 """Where mamba's hang out with each other.""" |
2 | |
330 | 3 from werkzeug.utils import secure_filename |
4 from flask import Flask, request, abort | |
5 | |
496
0c5853bb7a14
Timestamp uncurated level files.
Simon Cross <hodgestar@gmail.com>
parents:
340
diff
changeset
|
6 from datetime import datetime |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
7 from ConfigParser import SafeConfigParser |
550
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
8 import xmlrpclib |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
9 import time |
330 | 10 import os |
11 import sys | |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
12 import socket |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
13 import json |
330 | 14 |
15 app = Flask(__name__) | |
16 | |
17 | |
340
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
18 def path(ctype): |
568
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
19 main = app.config.forest.main |
340
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
20 if ctype == "curated": |
568
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
21 return os.path.join(main.level_folder, 'curated') |
340
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
22 elif ctype == "uncurated": |
568
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
23 return os.path.join(main.level_folder, 'uncurated') |
340
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
24 abort(404, "Not found") |
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
25 |
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
26 |
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
27 def list_levels(folder): |
330 | 28 endl = len(".txt") |
340
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
29 files = [x[:-endl] for x in os.listdir(folder) |
330 | 30 if not x.startswith('.') and x.endswith('.txt')] |
31 return "\n".join(files) | |
328 | 32 |
33 | |
340
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
34 @app.route("/<ctype>/index") |
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
35 def index(ctype): |
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
36 ctype = path(ctype) |
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
37 return list_levels(ctype) |
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
38 |
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
39 |
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
40 @app.route("/<ctype>/level/<levelname>") |
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
41 def level(ctype, levelname): |
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
42 ctype = path(ctype) |
330 | 43 levelname = "%s.txt" % secure_filename(levelname) |
340
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
44 levelpath = os.path.join(ctype, levelname) |
330 | 45 if not os.path.isfile(levelpath): |
46 abort(404, "Level not found. Hsss.") | |
47 with open(levelpath) as level: | |
48 return level.read() | |
49 | |
328 | 50 |
330 | 51 @app.route("/save/<levelname>", methods=['GET', 'POST']) |
52 def save(levelname): | |
496
0c5853bb7a14
Timestamp uncurated level files.
Simon Cross <hodgestar@gmail.com>
parents:
340
diff
changeset
|
53 ts = datetime.now().strftime("%Y%m%d.%H%M%S") |
0c5853bb7a14
Timestamp uncurated level files.
Simon Cross <hodgestar@gmail.com>
parents:
340
diff
changeset
|
54 levelname = "%s.%s.txt" % (secure_filename(levelname), ts) |
340
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
55 levelpath = os.path.join(path("uncurated"), levelname) |
330 | 56 if request.method == 'POST': |
57 if os.path.exists(levelpath): | |
58 abort(409, "Mamba already resident.") | |
59 leveldata = request.form['data'].encode('ascii') | |
60 with open(levelpath, 'w') as level: | |
61 level.write(leveldata) | |
550
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
62 inform_cia(levelname, "New level uploaded.", branch="uncurated") |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
63 inform_irker(levelname, "New level uploaded.", branch="uncurated") |
330 | 64 return "Ssss." |
65 else: | |
66 abort(405, "Post levels here. Hsss.") | |
328 | 67 |
68 | |
550
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
69 MAMBA_VERSION = "0.1" |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
70 MAMBA_URL = "https://ctpug.org.za/hg/mamba" |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
71 CIA_URL = "http://cia.navi.cx" |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
72 CIA_MSG_TEMPLATE = """ |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
73 <message |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
74 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
75 xsi:noNamespaceSchemaLocation="schema.xsd"> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
76 <generator> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
77 <name>Mutable Mamba Level Server</name> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
78 <version>%(mamba_version)s</version> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
79 <url>%(mamba_url)s</url> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
80 </generator> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
81 <source> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
82 <project>%(project)s</project> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
83 <module>%(module)s</module> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
84 <branch>%(branch)s</branch> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
85 </source> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
86 <timestamp> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
87 %(timestamp)d |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
88 </timestamp> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
89 <body> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
90 <commit> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
91 <revision>%(revision)s</revision> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
92 <author>%(author)s</author> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
93 <files> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
94 <file>%(file)s</file> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
95 </files> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
96 <log> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
97 %(log)s |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
98 </log> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
99 </commit> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
100 </body> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
101 </message> |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
102 """ |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
103 |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
104 |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
105 def inform_cia(filename, log, branch='uncurated'): |
568
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
106 if app.config.forest.cia is None: |
550
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
107 return |
568
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
108 cia = app.config.forest.cia |
550
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
109 msg = CIA_MSG_TEMPLATE % { |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
110 'mamba_version': MAMBA_VERSION, |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
111 'mamba_url': MAMBA_URL, |
568
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
112 'project': cia.project, |
550
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
113 'module': 'level-server', |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
114 'branch': branch, |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
115 'timestamp': int(time.time()), |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
116 'revision': '0', |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
117 'author': 'unknown', |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
118 'file': filename, |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
119 'log': log, |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
120 } |
568
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
121 srv = xmlrpclib.Server(cia.url) |
550
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
122 srv.hub.deliver(msg) |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
123 |
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
124 |
574
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
125 def format_irker_message(msg_template, project, filename, log, |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
126 branch='uncurated', colours=None): |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
127 msg_params = { |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
128 "project": project, |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
129 "filename": filename, |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
130 "log": log, |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
131 "branch": branch, |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
132 } |
574
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
133 if colours: |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
134 msg_params.update(colours) |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
135 return msg_template % msg_params |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
136 |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
137 |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
138 def irker_colours(colour_style): |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
139 if colour_style == "mIRC": |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
140 return { |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
141 'bold': '\x02', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
142 'green': '\x0303', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
143 'blue': '\x0302', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
144 'red': '\x0305', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
145 'yellow': '\x0307', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
146 'brown': '\x0305', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
147 'magenta': '\x0306', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
148 'cyan': '\x0310', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
149 'reset': '\x0F', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
150 } |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
151 else: |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
152 return { |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
153 'bold': '', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
154 'green': '', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
155 'blue': '', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
156 'red': '', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
157 'yellow': '', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
158 'brown': '', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
159 'magenta': '', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
160 'cyan': '', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
161 'reset': '', |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
162 } |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
163 |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
164 |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
165 def inform_irker(filename, log, branch='uncurated'): |
568
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
166 if app.config.forest.irker is None: |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
167 return |
568
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
168 irker = app.config.forest.irker |
574
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
169 colours = irker_colours(irker.colour_style) |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
170 privmsg = format_irker_message(irker.msg_template, irker.project, |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
171 filename, log, branch, colours=colours) |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
172 message = json.dumps({"to": irker.channels, "privmsg": privmsg}) |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
173 try: |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
174 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
569
c296d10bd5f9
Fix call to socket.connect.
Simon Cross <hodgestar@gmail.com>
parents:
568
diff
changeset
|
175 sock.connect((irker.host, irker.port)) |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
176 sock.sendall(message + "\n") |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
177 finally: |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
178 sock.close() |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
179 |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
180 |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
181 class ForestConfig(object): |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
182 |
574
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
183 IRKER_PORT = 6659 |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
184 IRKER_MSG_TEMPLATE = ( |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
185 "%(bold)s%(project)s:%(reset)s " |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
186 "%(magenta)s%(branch)s%(reset)s * " |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
187 "%(bold)s%(filename)s%(reset)s: " |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
188 "%(log)s" |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
189 ) |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
190 |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
191 class SubConfig(object): |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
192 """Holder for sub-config.""" |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
193 |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
194 def __init__(self, filenames): |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
195 self._config = SafeConfigParser() |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
196 self._config.read(filenames) |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
197 self.main = self._parse("main") |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
198 self.irker = self._parse("irker") |
568
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
199 self.cia = self._parse("cia") |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
200 |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
201 def _parse(self, section): |
568
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
202 if self._config.has_section(section): |
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
203 conf = dict(self._config.items(section)) |
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
204 else: |
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
205 conf = {} |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
206 return getattr(self, "_parse_%s" % section)(conf) |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
207 |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
208 def _parse_main(self, conf): |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
209 main = self.SubConfig() |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
210 main.host = conf.get('host', '0.0.0.0') |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
211 main.port = int(conf['port']) |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
212 main.level_folder = conf['level_folder'] |
568
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
213 return main |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
214 |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
215 def _parse_irker(self, conf): |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
216 if 'project' not in conf: |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
217 return None |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
218 irker = self.SubConfig() |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
219 irker.project = conf['project'] |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
220 irker.host = conf.get('host', 'localhost') |
574
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
221 irker.port = int(conf.get('port', self.IRKER_PORT)) |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
222 irker.channels = conf['channels'] |
574
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
223 irker.msg_template = conf.get('msg_template', self.IRKER_MSG_TEMPLATE) |
e6344f57886e
Try add colours to the irker messages.
Simon Cross <hodgestar@gmail.com>
parents:
570
diff
changeset
|
224 irker.colour_style = conf.get('colour_style', 'mIRC') |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
225 return irker |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
226 |
568
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
227 def _parse_cia(self, conf): |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
228 if 'project' not in conf: |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
229 return None |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
230 cia = self.SubConfig() |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
231 cia.project = conf['project'] |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
232 cia.url = conf.get('url', CIA_URL) |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
233 return cia |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
234 |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
235 |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
236 USAGE = """Usage: python -m mamba.forest <config file> |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
237 |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
238 The config file should look like: |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
239 |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
240 [main] |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
241 host=0.0.0.0 # optional |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
242 port=8000 |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
243 level_folder=./my/levels |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
244 |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
245 [irker] |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
246 host=localhost # optional |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
247 port=8001 |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
248 project=mamba-levels |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
249 channels=channel1,channel2 |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
250 """ |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
251 |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
252 |
328 | 253 if __name__ == "__main__": |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
254 if len(sys.argv) != 2: |
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
255 print USAGE |
330 | 256 sys.exit(1) |
257 | |
568
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
258 app.config.forest = ForestConfig([sys.argv[1]]) |
7eb5ecda4667
Fix many bugs in new forest config usage.
Simon Cross <hodgestar@gmail.com>
parents:
567
diff
changeset
|
259 main = app.config.forest.main |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
260 |
340
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
261 for ctype in ("curated", "uncurated"): |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
262 folder = os.path.join(main.level_folder, ctype) |
340
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
263 if not os.path.exists(folder): |
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
264 os.makedirs(folder) |
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
265 if not os.path.isdir(folder): |
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
266 print "Level folder must be a folder." |
5456db312f7f
Curated and uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents:
330
diff
changeset
|
267 sys.exit(1) |
550
91e1a95343b2
Add option to inform CIA of new levels.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
268 cia_project = sys.argv[3] if len(sys.argv) > 3 else None |
330 | 269 |
270 # app.debug = True | |
567
16344424dfcc
First attempt at irker logging from level server.
Simon Cross <hodgestar@gmail.com>
parents:
550
diff
changeset
|
271 app.run(host=main.host, port=main.port) |