Caffe2 - Python API
A deep learning, cross platform ML framework
github.py
1 ## @package github
2 # Module caffe2.python.docs.github
3 from __future__ import absolute_import
4 from __future__ import division
5 from __future__ import print_function
6 from __future__ import unicode_literals
7 import argparse
8 import os
9 from caffe2.python.docs.formatter import Markdown
10 from caffe2.python.docs.generator import OpDocGenerator, DocUploader
11 from caffe2.python.docs.generator import OperatorDoc, OperatorEngine
12 
13 
14 class GHOpDocUploader(DocUploader):
15  def __init__(self):
16  pass
17 
18  def upload(self, content_body):
19  print(content_body)
20 
21 
22 class GHMarkdown(Markdown):
23  def addHeader(self, text, h=1):
24  self.addLine("\n{header} {text}\n".format(header=h * '#', text=text), True)
25 
26  def addDocHeader(self):
27  self.addLine("---")
28  self.addLine("docid: operators-catalog")
29  self.addLine("title: Operators Catalog")
30  self.addLine("layout: operators")
31  self.addLine("permalink: /docs/operators-catalogue.html")
32  self.addLine("---")
33  self.addLine("* TOC")
34  self.addLine("{:toc}")
35 
36  def addTable(self, table, noTitle=False):
37  self.addLinebreak()
38  assert(len(table) > 1)
39  self.addLine(' | '.join(['----------' for i in range(len(table[0]))]))
40  self.addLine(' | '.join(table[0]))
41  for row in table[1:]:
42  self.addLine(' | '.join(row))
43 
44  def addTableHTML(self, table, noTitle=False):
45  self.addRaw("<table>")
46  for row in table:
47  self.addRaw("<tr>")
48  for cell in row:
49  self.addRaw("<td>")
50  self.addLine("{cell}".format(cell=cell))
51  self.addRaw("</td>")
52  self.addRaw("</tr>")
53  self.addRaw("</table>")
54 
55 def getCodeLink(formatter, schema):
56  formatter = formatter.clone()
57  path = os.path.join("caffe2", os.path.relpath(schema.file, "caffe2"))
58  schemaLink = ('https://github.com/pytorch/pytorch/blob/master/{path}'
59  .format(path=path))
60  formatter.addLink('{path}'.format(path=path), schemaLink)
61  return formatter.dump()
62 
63 
64 class GHOperatorEngine(OperatorEngine):
65  def generateDoc(self, formatter):
66  for device, _ in self.getDeviceImpl():
67  formatter.addCode('{engine}'.format(engine=self.engine), True)
68  if device:
69  formatter.addRaw(' on ')
70  formatter.addEmphasis("{device}".format(device=device), 1)
71 
72 
73 class GHOperatorDoc(OperatorDoc):
74  def generateCodeLink(self, formatter):
75  formatter.addHeader("Code", 3)
76  formatter.addLinebreak()
77  formatter.addRaw(getCodeLink(formatter, self.schema))
78 
79  def getInfo(self, formatter, name, impl):
80  formatter = formatter.clone()
81  if impl:
82  formatter.addEmphasis('{name}'.format(name=name), 1)
83  formatter.addRaw(' ')
84  formatter.addCode('{impl}'.format(impl=impl), True)
85  return formatter.dump()
86 
87  def generateSchema(self, formatter):
88  formatter.addHeader(self.name, 2)
89  if self.schema:
90  self.generateDoc(formatter)
91  self.generateInterface(formatter)
92  self.generateCodeLink(formatter)
93  formatter.addBreak()
94  else:
95  formatter.addLine("No schema documented yet.")
96 
97 
98 class GHOpDocGenerator(OpDocGenerator):
99  def getOperatorDoc(self, name, schema, priority):
100  return GHOperatorDoc(name, schema, priority)
101 
102  def getOperatorEngine(self, name):
103  return GHOperatorEngine(name)
104 
105  def createBody(self):
106  self.formatter.addDocHeader()
107  operators = self.getOperators()
108 
109  for operator in operators:
110  operator.generateSchema(self.formatter)
111 
112  self.content_body += self.formatter.dump()
113 
114 
115 if __name__ == "__main__":
116  parser = argparse.ArgumentParser(description="Operators catalog generator.")
117  parser.add_argument('catalog_path', type=str,
118  help='operators-catalogue.md to write out to')
119  args = parser.parse_args()
120 
121  with open(args.catalog_path, 'w') as fp:
122  ops = GHOpDocGenerator(GHMarkdown(), GHOpDocUploader)
123  ops.createBody()
124  fp.write(ops.content_body)
125  print("Updated {}!".format(args.catalog_path))
def generateCodeLink(self, formatter)
Definition: github.py:74