comparison sat/bridge/bridge_constructor/constructors/mediawiki/constructor.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
1 #!/usr/bin/env python2 1 #!/usr/bin/env python2
2 #-*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # SàT: a XMPP client 4 # SàT: a XMPP client
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) 5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6 6
7 # This program is free software: you can redistribute it and/or modify 7 # This program is free software: you can redistribute it and/or modify
22 from datetime import datetime 22 from datetime import datetime
23 import re 23 import re
24 24
25 25
26 class MediawikiConstructor(base_constructor.Constructor): 26 class MediawikiConstructor(base_constructor.Constructor):
27
28 def __init__(self, bridge_template, options): 27 def __init__(self, bridge_template, options):
29 base_constructor.Constructor.__init__(self, bridge_template, options) 28 base_constructor.Constructor.__init__(self, bridge_template, options)
30 self.core_template = "mediawiki_template.tpl" 29 self.core_template = "mediawiki_template.tpl"
31 self.core_dest = "mediawiki.wiki" 30 self.core_dest = "mediawiki.wiki"
32 31
33 def _addTextDecorations(self, text): 32 def _addTextDecorations(self, text):
34 """Add text decorations like coloration or shortcuts""" 33 """Add text decorations like coloration or shortcuts"""
35 34
36 def anchor_link(match): 35 def anchor_link(match):
37 link = match.group(1) 36 link = match.group(1)
38 #we add anchor_link for [method_name] syntax: 37 # we add anchor_link for [method_name] syntax:
39 if link in self.bridge_template.sections(): 38 if link in self.bridge_template.sections():
40 return "[[#%s|%s]]" % (link, link) 39 return "[[#%s|%s]]" % (link, link)
41 print ("WARNING: found an anchor link to an unknown method") 40 print("WARNING: found an anchor link to an unknown method")
42 return link 41 return link
43 42
44 return re.sub(r"\[(\w+)\]", anchor_link, text) 43 return re.sub(r"\[(\w+)\]", anchor_link, text)
45 44
46 def _wikiParameter(self, name, sig_in): 45 def _wikiParameter(self, name, sig_in):
49 @param sig_in: signature in 48 @param sig_in: signature in
50 @return: string of the formated parameters""" 49 @return: string of the formated parameters"""
51 arg_doc = self.getArgumentsDoc(name) 50 arg_doc = self.getArgumentsDoc(name)
52 arg_default = self.getDefault(name) 51 arg_default = self.getDefault(name)
53 args_str = self.getArguments(sig_in) 52 args_str = self.getArguments(sig_in)
54 args = args_str.split(', ') if args_str else [] # ugly but it works :) 53 args = args_str.split(", ") if args_str else [] # ugly but it works :)
55 wiki = [] 54 wiki = []
56 for i in range(len(args)): 55 for i in range(len(args)):
57 if i in arg_doc: 56 if i in arg_doc:
58 name, doc = arg_doc[i] 57 name, doc = arg_doc[i]
59 doc = '\n:'.join(doc.rstrip('\n').split('\n')) 58 doc = "\n:".join(doc.rstrip("\n").split("\n"))
60 wiki.append("; %s: %s" % (name, self._addTextDecorations(doc))) 59 wiki.append("; %s: %s" % (name, self._addTextDecorations(doc)))
61 else: 60 else:
62 wiki.append("; arg_%d: " % i) 61 wiki.append("; arg_%d: " % i)
63 if i in arg_default: 62 if i in arg_default:
64 wiki.append(":''DEFAULT: %s''" % arg_default[i]) 63 wiki.append(":''DEFAULT: %s''" % arg_default[i])
68 """Format return doc with the wiki syntax 67 """Format return doc with the wiki syntax
69 @param name: name of the function 68 @param name: name of the function
70 """ 69 """
71 arg_doc = self.getArgumentsDoc(name) 70 arg_doc = self.getArgumentsDoc(name)
72 wiki = [] 71 wiki = []
73 if 'return' in arg_doc: 72 if "return" in arg_doc:
74 wiki.append('\n|-\n! scope=row | return value\n|') 73 wiki.append("\n|-\n! scope=row | return value\n|")
75 wiki.append('<br />\n'.join(self._addTextDecorations(arg_doc['return']).rstrip('\n').split('\n'))) 74 wiki.append(
75 "<br />\n".join(
76 self._addTextDecorations(arg_doc["return"]).rstrip("\n").split("\n")
77 )
78 )
76 return "\n".join(wiki) 79 return "\n".join(wiki)
77 80
78 def generateCoreSide(self): 81 def generateCoreSide(self):
79 signals_part = [] 82 signals_part = []
80 methods_part = [] 83 methods_part = []
81 sections = self.bridge_template.sections() 84 sections = self.bridge_template.sections()
82 sections.sort() 85 sections.sort()
83 for section in sections: 86 for section in sections:
84 function = self.getValues(section) 87 function = self.getValues(section)
85 print ("Adding %s %s" % (section, function["type"])) 88 print("Adding %s %s" % (section, function["type"]))
86 async_msg = """<br />'''This method is asynchronous'''""" 89 async_msg = """<br />'''This method is asynchronous'''"""
87 deprecated_msg = """<br />'''<font color="#FF0000">/!\ WARNING /!\ : This method is deprecated, please don't use it !</font>'''""" 90 deprecated_msg = """<br />'''<font color="#FF0000">/!\ WARNING /!\ : This method is deprecated, please don't use it !</font>'''"""
88 signature_signal = \ 91 signature_signal = (
89 """\ 92 """\
90 ! scope=row | signature 93 ! scope=row | signature
91 | %s 94 | %s
92 |-\ 95 |-\
93 """ % function['sig_in'] 96 """
94 signature_method = \ 97 % function["sig_in"]
95 """\ 98 )
99 signature_method = """\
96 ! scope=row | signature in 100 ! scope=row | signature in
97 | %s 101 | %s
98 |- 102 |-
99 ! scope=row | signature out 103 ! scope=row | signature out
100 | %s 104 | %s
101 |-\ 105 |-\
102 """ % (function['sig_in'], function['sig_out']) 106 """ % (
107 function["sig_in"],
108 function["sig_out"],
109 )
103 completion = { 110 completion = {
104 'signature': signature_signal if function['type'] == "signal" else signature_method, 111 "signature": signature_signal
105 'sig_out': function['sig_out'] or '', 112 if function["type"] == "signal"
106 'category': function['category'], 113 else signature_method,
107 'name': section, 114 "sig_out": function["sig_out"] or "",
108 'doc': self.getDoc(section) or "FIXME: No description available", 115 "category": function["category"],
109 'async': async_msg if "async" in self.getFlags(section) else "", 116 "name": section,
110 'deprecated': deprecated_msg if "deprecated" in self.getFlags(section) else "", 117 "doc": self.getDoc(section) or "FIXME: No description available",
111 'parameters': self._wikiParameter(section, function['sig_in']), 118 "async": async_msg if "async" in self.getFlags(section) else "",
112 'return': self._wikiReturn(section) if function['type'] == 'method' else ''} 119 "deprecated": deprecated_msg
120 if "deprecated" in self.getFlags(section)
121 else "",
122 "parameters": self._wikiParameter(section, function["sig_in"]),
123 "return": self._wikiReturn(section)
124 if function["type"] == "method"
125 else "",
126 }
113 127
114 dest = signals_part if function['type'] == "signal" else methods_part 128 dest = signals_part if function["type"] == "signal" else methods_part
115 dest.append("""\ 129 dest.append(
130 """\
116 == %(name)s == 131 == %(name)s ==
117 ''%(doc)s'' 132 ''%(doc)s''
118 %(deprecated)s 133 %(deprecated)s
119 %(async)s 134 %(async)s
120 {| class="wikitable" style="text-align:left; width:80%%;" 135 {| class="wikitable" style="text-align:left; width:80%%;"
124 %(signature)s 139 %(signature)s
125 ! scope=row | parameters 140 ! scope=row | parameters
126 | 141 |
127 %(parameters)s%(return)s 142 %(parameters)s%(return)s
128 |} 143 |}
129 """ % completion) 144 """
145 % completion
146 )
130 147
131 #at this point, signals_part, and methods_part should be filled, 148 # at this point, signals_part, and methods_part should be filled,
132 #we just have to place them in the right part of the template 149 # we just have to place them in the right part of the template
133 core_bridge = [] 150 core_bridge = []
134 template_path = self.getTemplatePath(self.core_template) 151 template_path = self.getTemplatePath(self.core_template)
135 try: 152 try:
136 with open(template_path) as core_template: 153 with open(template_path) as core_template:
137 for line in core_template: 154 for line in core_template:
138 if line.startswith('##SIGNALS_PART##'): 155 if line.startswith("##SIGNALS_PART##"):
139 core_bridge.extend(signals_part) 156 core_bridge.extend(signals_part)
140 elif line.startswith('##METHODS_PART##'): 157 elif line.startswith("##METHODS_PART##"):
141 core_bridge.extend(methods_part) 158 core_bridge.extend(methods_part)
142 elif line.startswith('##TIMESTAMP##'): 159 elif line.startswith("##TIMESTAMP##"):
143 core_bridge.append('Generated on %s' % datetime.now()) 160 core_bridge.append("Generated on %s" % datetime.now())
144 else: 161 else:
145 core_bridge.append(line.replace('\n', '')) 162 core_bridge.append(line.replace("\n", ""))
146 except IOError: 163 except IOError:
147 print ("Can't open template file [%s]" % template_path) 164 print("Can't open template file [%s]" % template_path)
148 sys.exit(1) 165 sys.exit(1)
149 166
150 #now we write to final file 167 # now we write to final file
151 self.finalWrite(self.core_dest, core_bridge) 168 self.finalWrite(self.core_dest, core_bridge)
152
153