Caffe2 - Python API
A deep learning, cross platform ML framework
github.py
1 # Copyright (c) 2016-present, Facebook, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 ##############################################################################
15 
16 ## @package github
17 # Module caffe2.python.docs.github
18 from __future__ import absolute_import
19 from __future__ import division
20 from __future__ import print_function
21 from __future__ import unicode_literals
22 import argparse
23 import os
24 from caffe2.python.docs.formatter import Markdown
25 from caffe2.python.docs.generator import OpDocGenerator, DocUploader
26 from caffe2.python.docs.generator import OperatorDoc, OperatorEngine
27 
28 
29 class GHOpDocUploader(DocUploader):
30  def __init__(self):
31  pass
32 
33  def upload(self, content_body):
34  print(content_body)
35 
36 
37 class GHMarkdown(Markdown):
38  def addHeader(self, text, h=1):
39  self.addLine("\n{header} {text}\n".format(header=h * '#', text=text), True)
40 
41  def addDocHeader(self):
42  self.addLine("---")
43  self.addLine("docid: operators-catalog")
44  self.addLine("title: Operators Catalog")
45  self.addLine("layout: operators")
46  self.addLine("permalink: /docs/operators-catalogue.html")
47  self.addLine("---")
48  self.addLine("* TOC")
49  self.addLine("{:toc}")
50 
51  def addTable(self, table, noTitle=False):
52  self.addLinebreak()
53  assert(len(table) > 1)
54  self.addLine(' | '.join(['----------' for i in range(len(table[0]))]))
55  self.addLine(' | '.join(table[0]))
56  for row in table[1:]:
57  self.addLine(' | '.join(row))
58 
59  def addTableHTML(self, table, noTitle=False):
60  self.addRaw("<table>")
61  for row in table:
62  self.addRaw("<tr>")
63  for cell in row:
64  self.addRaw("<td>")
65  self.addLine("{cell}".format(cell=cell))
66  self.addRaw("</td>")
67  self.addRaw("</tr>")
68  self.addRaw("</table>")
69 
70 def getCodeLink(formatter, schema):
71  formatter = formatter.clone()
72  path = os.path.join("caffe2", os.path.relpath(schema.file, "caffe2"))
73  schemaLink = ('https://github.com/caffe2/caffe2/blob/master/{path}'
74  .format(path=path))
75  formatter.addLink('{path}'.format(path=path), schemaLink)
76  return formatter.dump()
77 
78 
79 class GHOperatorEngine(OperatorEngine):
80  def generateDoc(self, formatter):
81  for device, _ in self.getDeviceImpl():
82  formatter.addCode('{engine}'.format(engine=self.engine), True)
83  if device:
84  formatter.addRaw(' on ')
85  formatter.addEmphasis("{device}".format(device=device), 1)
86 
87 
88 class GHOperatorDoc(OperatorDoc):
89  def generateCodeLink(self, formatter):
90  formatter.addHeader("Code", 3)
91  formatter.addLinebreak()
92  formatter.addRaw(getCodeLink(formatter, self.schema))
93 
94  def getInfo(self, formatter, name, impl):
95  formatter = formatter.clone()
96  if impl:
97  formatter.addEmphasis('{name}'.format(name=name), 1)
98  formatter.addRaw(' ')
99  formatter.addCode('{impl}'.format(impl=impl), True)
100  return formatter.dump()
101 
102  def generateSchema(self, formatter):
103  formatter.addHeader(self.name, 2)
104  if self.schema:
105  self.generateDoc(formatter)
106  self.generateInterface(formatter)
107  self.generateCodeLink(formatter)
108  formatter.addBreak()
109  else:
110  formatter.addLine("No schema documented yet.")
111 
112 
113 class GHOpDocGenerator(OpDocGenerator):
114  def getOperatorDoc(self, name, schema, priority):
115  return GHOperatorDoc(name, schema, priority)
116 
117  def getOperatorEngine(self, name):
118  return GHOperatorEngine(name)
119 
120  def createBody(self):
121  self.formatter.addDocHeader()
122  operators = self.getOperators()
123 
124  for operator in operators:
125  operator.generateSchema(self.formatter)
126 
127  self.content_body += self.formatter.dump()
128 
129 
130 if __name__ == "__main__":
131  parser = argparse.ArgumentParser(description="Operators catalog generator.")
132  parser.add_argument('catalog_path', type=str,
133  help='operators-catalogue.md to write out to')
134  args = parser.parse_args()
135 
136  with open(args.catalog_path, 'w') as fp:
137  ops = GHOpDocGenerator(GHMarkdown(), GHOpDocUploader)
138  ops.createBody()
139  fp.write(ops.content_body)
140  print("Updated {}!".format(args.catalog_path))
def generateCodeLink(self, formatter)
Definition: github.py:89