-
Notifications
You must be signed in to change notification settings - Fork 57
/
example.py
151 lines (114 loc) · 4.48 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals, print_function
import atexit
import io
import os
import shutil
import tempfile
import textwrap
import codecs
from docutils.parsers.rst import Directive
from docutils import nodes
from sphinx.util.nodes import set_source_info
from asdf import AsdfFile
from asdf.constants import ASDF_MAGIC, BLOCK_FLAG_STREAMED
from asdf import versioning
version_string = versioning.version_to_string(versioning.default_version)
TMPDIR = tempfile.mkdtemp()
def delete_tmpdir():
shutil.rmtree(TMPDIR)
GLOBALS = {}
LOCALS = {}
FLAGS = {
BLOCK_FLAG_STREAMED: "BLOCK_FLAG_STREAMED"
}
class RunCodeDirective(Directive):
has_content = True
optional_arguments = 1
def run(self):
code = textwrap.dedent('\n'.join(self.content))
cwd = os.getcwd()
os.chdir(TMPDIR)
try:
try:
exec(code, GLOBALS, LOCALS)
except:
print(code)
raise
literal = nodes.literal_block(code, code)
literal['language'] = 'python'
set_source_info(self, literal)
finally:
os.chdir(cwd)
if 'hidden' not in self.arguments:
return [literal]
else:
return []
class AsdfDirective(Directive):
required_arguments = 1
def run(self):
filename = self.arguments[0]
cwd = os.getcwd()
os.chdir(TMPDIR)
parts = []
try:
ff = AsdfFile()
code = AsdfFile._open_impl(ff, filename, _get_yaml_content=True)
code = '{0} {1}\n'.format(ASDF_MAGIC, version_string) + code.strip().decode('utf-8')
literal = nodes.literal_block(code, code)
literal['language'] = 'yaml'
set_source_info(self, literal)
parts.append(literal)
with AsdfFile.open(filename) as ff:
for i, block in enumerate(ff.blocks.internal_blocks):
data = codecs.encode(block.data.tostring(), 'hex')
if len(data) > 40:
data = data[:40] + '...'.encode()
allocated = block._allocated
size = block._size
data_size = block._data_size
flags = block._flags
if flags & BLOCK_FLAG_STREAMED:
allocated = size = data_size = 0
lines = []
lines.append('BLOCK {0}:'.format(i))
human_flags = []
for key, val in FLAGS.items():
if flags & key:
human_flags.append(val)
if len(human_flags):
lines.append(' flags: {0}'.format(' | '.join(human_flags)))
if block.compression:
lines.append(' compression: {0}'.format(block.compression))
lines.append(' allocated_size: {0}'.format(allocated))
lines.append(' used_size: {0}'.format(size))
lines.append(' data_size: {0}'.format(data_size))
lines.append(' data: {0}'.format(data))
code = '\n'.join(lines)
literal = nodes.literal_block(code, code)
literal['language'] = 'yaml'
set_source_info(self, literal)
parts.append(literal)
internal_blocks = list(ff.blocks.internal_blocks)
if (len(internal_blocks) and
internal_blocks[-1].array_storage != 'streamed'):
buff = io.BytesIO()
ff.blocks.write_block_index(buff, ff)
block_index = buff.getvalue()
literal = nodes.literal_block(block_index, block_index)
literal['language'] = 'yaml'
set_source_info(self, literal)
parts.append(literal)
finally:
os.chdir(cwd)
result = nodes.admonition()
textnodes, messages = self.state.inline_text(filename, self.lineno)
title = nodes.title(filename, '', *textnodes)
result += title
result += parts
return [result]
def setup(app):
app.add_directive('runcode', RunCodeDirective)
app.add_directive('asdf', AsdfDirective)
atexit.register(delete_tmpdir)