Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
Jinhwa/dev (#44)
Browse files Browse the repository at this point in the history
enum class support

* add attributes to summary for enum class

* bug fix: enum class support

* Remove 'nameWithType' information

* bug fix: how to detect an enum class

enum type class should have 'Bases: enum.Enum' in its class docstring

* Add unit test for enum type support

* Change data format

* Change ut for enum

* fix indent error when resolve conflict
  • Loading branch information
bianliu1013 committed Jun 19, 2017
1 parent cfdb4ea commit c32d933
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 7 deletions.
9 changes: 5 additions & 4 deletions docfx_yaml/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,12 @@ def convert_module_to_package_if_needed(obj):
if 'example' in obj['syntax'] and obj['syntax']['example']:
obj.setdefault('example', []).append(obj['syntax'].pop('example'))

# append 'enum_attribute' to summary
# add content of temp list 'enum_attribute' to children and yaml_data
if 'enum_attribute' in obj['syntax'] and obj['syntax']['enum_attribute']:
enum_attribute = obj['syntax'].pop('enum_attribute')
if 'summary' in obj:
obj['summary'] += ('\n\n' + '\n\n'.join(enum_attribute) + '\n')
for attrData in enum_attribute:
obj.get('children', []).append(attrData['uid'])
yaml_data.append(attrData)

if 'references' in obj:
references.extend(obj.pop('references'))
Expand Down Expand Up @@ -496,8 +497,8 @@ def convert_module_to_package_if_needed(obj):
if found_node:
found_node.setdefault('items', []).append({'name': filename, 'uid': filename})
else:
print('No parent level module found: {}'.format(parent_level))
toc_yaml.append({'name': filename, 'uid': filename})

else:
toc_yaml.append({'name': filename, 'uid': filename})

Expand Down
29 changes: 27 additions & 2 deletions docfx_yaml/monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def transform_para(para_field):

return data


class PatchedDocFieldTransformer(docfields.DocFieldTransformer):

def __init__(self, directive):
Expand All @@ -225,7 +226,31 @@ def transform_all(self, node):
if _is_desc_of_enum_class(node):
for item in child:
if isinstance(item, desc_signature):
data.setdefault('enum_attribute', []).append(item.astext())
# capture enum attributes data and cache it
data.setdefault('enum_attribute', [])

curuid = item.get('ids', [''])[0]
parent = curuid[:curuid.rfind('.')]
name = item.children[0].astext()

enumData = {
'uid': curuid,
'id': name,
'parent': parent,
'langs': ['python'],
'name': name,
'fullName': curuid,
'type': item.parent.get('desctype'),
'module': item.get('module'),
'syntax': {
'content': item.astext(),
'return': {
'type': [parent]
}
}
}

data['enum_attribute'].append(enumData) # Add enum attributes data to a temp list

# Don't recurse into child nodes
continue
Expand All @@ -243,7 +268,7 @@ def transform_all(self, node):
else:
content = transform_node(child)

# skip 'Bases' in summary
# skip 'Bases' in summary
if not content.startswith('Bases: '):
summary.append(content)
if summary:
Expand Down
11 changes: 11 additions & 0 deletions tests/pyexample/example/enum_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from enum import Enum


class EnumFoo(Enum):
"""
Bases: enum.Enum.
This is a class for testing enum type support
"""

VALUE0 = 0
VALUE1 = 1
4 changes: 4 additions & 0 deletions tests/pyexample/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ Welcome to pyexample's documentation!
.. automodule:: example.nap
:members:
:undoc-members:

.. automodule:: example.enum_type
:members:
:undoc-members:
33 changes: 32 additions & 1 deletion tests/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def test_toc(self):
if 'items' in item:
self.assertEqual(
item['items'][0]['name'],
'example.example.Foo'
'example.enum_type.EnumFoo'
)
break

Expand All @@ -223,3 +223,34 @@ def test_examples(self):
""">>> print('docblock 2')"""
)

def test_enum(self):
"""
Test enum type support
"""
with sphinx_build('pyexample'):
with open('_build/text/docfx_yaml/example.enum_type.EnumFoo.yml') as yml_file:
data = yaml.safe_load(yml_file)
for item in data['items']:
if item['uid'] == 'example.enum_type.EnumFoo':
self.assertEqual(
item['children'],
['example.enum_type.EnumFoo.VALUE0', 'example.enum_type.EnumFoo.VALUE1']
)
if item['uid'] == 'example.enum_type.EnumFoo.VALUE0':
self.assertEqual(
item['syntax'],
{'content': 'VALUE0 = 0', 'return': {'type': ['example.enum_type.EnumFoo']}}
)
self.assertEqual(
item['type'],
'attribute'
)
if item['uid'] == 'example.enum_type.EnumFoo.VALUE1':
self.assertEqual(
item['syntax'],
{'content': 'VALUE1 = 1', 'return': {'type': ['example.enum_type.EnumFoo']}}
)
self.assertEqual(
item['type'],
'attribute'
)

0 comments on commit c32d933

Please sign in to comment.